Skip to main content

The Delivery Dilemma Codeforces Problem code solution

 The Delivery Dilemma Codeforces Problem code solution -

Problem statement-
C. The Delivery Dilemma
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.

Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:

  • the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in ai minutes,
  • Petya goes to the restaurant i on his own and picks up the dish, he will spend bi minutes on this.

Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.

For example, if Petya wants to order n=4 dishes and a=[3,7,4,5], and b=[2,1,2,4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 3 minutes, the courier of the fourth restaurant will bring the order in 5 minutes, and Petya will pick up the remaining dishes in 1+2=3 minutes. Thus, in 5 minutes all the dishes will be at Petya's house.

Find the minimum time after which all the dishes can be at Petya's home.

Input

The first line contains one positive integer t (1t2105) — the number of test cases. Then t test cases follow.

Each test case begins with a line containing one integer n (1n2105) — the number of dishes that Petya wants to order.

The second line of each test case contains n integers a1an (1ai109) — the time of courier delivery of the dish with the number i.

The third line of each test case contains n integers b1bn (1bi109) — the time during which Petya will pick up the dish with the number i.

The sum of n over all test cases does not exceed 2105.

Output

For each test case output one integer — the minimum time after which all dishes can be at Petya's home.

Example
input
Copy
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2
output
Copy
5
3
2
3

Solution-

Sort the array according to the delivery time and iterate over pair of vector.

store sum of pickup time till sum< delivery time .

then print the curr delivery time .


The Delivery Dilemma Codeforces Problem code solution

Follow code below-


#include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
int t;
cin>>t;
while(t--){
vector<int > v1;
vector<int > v2;
int n;
cin>>n;
int maxx=-1;
int sum=0;
for(int i=0;i<n;i++){
int k;
cin>>k;
// maxx=max(k,maxx);
v1.push_back(k);
}
for(int i=0;i<n;i++){
int k;
cin>>k;
sum+=k;
v2.push_back(k);
}
vector<pair<int ,int> > v3;
for(int i=0;i<n;i++){
v3.push_back(make_pair(v1[i],v2[i]));
}
sort(v3.begin(),v3.end());
int ans=0;
int fans=0;
for(int i=n-1;i>=0;i--){
ans+=v3[i].second;
if(ans<v3[i].first){
continue ;
}
else if(v3[i].first<ans){
fans=max(ans-v3[i].second,v3[i].first);
break;
}
else{
fans+=v3[i].first;
break;
}
}
if(fans!=0){
cout<<fans<<endl;
}
else{
cout<<sum<<endl;
}
}
return 0;
}





Comments

Popular posts from this blog

Vaccine Distribution Codechef December Long challenge solution 2020

 Vaccine Distribution  Codechef December Long challenge solution 2020- This Problem vaccine distribution is taken from December long challenge 2020. lets read problem statement. Problem statement- Finally, a COVID vaccine is out on the market and the Chefland government has asked you to form a plan to distribute it to the public as soon as possible. There are a total of  N N  people with ages  a 1 , a 2 , … , a N a 1 , a 2 , … , a N . There is only one hospital where vaccination is done and it is only possible to vaccinate up to  D D  people per day. Anyone whose age is  ≥ 80 ≥ 80  or  ≤ 9 ≤ 9  is considered to be  at risk . On each day, you may not vaccinate both a person who is at risk and a person who is not at risk. Find the smallest number of days needed to vaccinate everyone. Input The first line of the input contains a single integer  T T  denoting the number of test cases. The description of  T T  test cases follows. The first line of each test case contains two space-separ

Codechef February long challenge solution 2021

   Codechef February  long challenge solution 2021- Hey guys Welcome Again, A small update CodeChef February  long challenge solution 2021 starting from 1st week of beb  2021 . Codechef long challenge is one of the popular contests of CodeChef you should participate in codechef Feb  long challenge.This is first contest of codechef in 2021 so good luck. And wish you happy new year. Contest link- https://www.codechef.com/FEB21/?itm_medium=navmenu&itm_campaign=FEB21 I will post the solution after Contest.Stay tuned. About CodeChef  February  long challenge solution 2021: CodeChef Long Challenge is a 10-day monthly coding contest where you can show off your computer programming skills. The significance being - it gives you enough time to think about a problem, try different ways of attacking the problem, read the concepts etc. If you’re usually slow at solving problems and have ample time at hand, this is ideal for you. We also put in a lot of efforts in getting quality problems, w

Merge Two sorted array Without Extra Space

Problem statement-  Given two sorted arrays arr1[] and arr2[] of sizes N and M in non-decreasing order. Merge them in sorted order without using any extra space. Modify arr1 so that it contains the first N elements and modify arr2 so that it contains the last M elements.    Example 1: Input:  N = 4, arr1[] = [1 3 5 7]  M = 5, arr2[] = [0 2 6 8 9] Output:  arr1[] = [0 1 2 3] arr2[] = [5 6 7 8 9] Explanation: After merging the two  non-decreasing arrays, we get,  0 1 2 3 5 6 7 8 9.   Example 2: Input:  N = 2, arr1[] = [10, 12]  M = 3, arr2[] = [5 18 20] Output:  arr1[] = [5 10] arr2[] = [12 18 20] Explanation: After merging two sorted arrays  we get 5 10 12 18 20. Your Task: You don't need to read input or print anything. You only need to complete the function merge() that takes arr1, arr2, N and M as input parameters and modifies them in-place so that they look like the sorted merged array when concatenated. Expected Time Complexity:  O((n+m) log(n+m)) Expected Auxilliary Space: O(1