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

codeforces rating system | Codeforces rating Newbie to Legendary Grandmaster

 Codeforces rating system | Codeforces rating Newbie to Legendary Grandmaster- Codeforces is one of the most popular platforms for competitive programmers and  codeforces rating matters a lot  .  Competitive Programming  teaches you to find the easiest solution in the quickest possible way. CP enhances your problem-solving and debugging skills giving you real-time fun. It's brain-sport. As you start solving harder and harder problems in live-contests your analytical and rational thinking intensifies. To have a good codeforces profile makes a good impression on the interviewer. If you have a good  codeforces profile so it is very easy to get a referral for product base company like amazon, google , facebook etc.So in this blog I have explained everything about codeforces rating system. What are different titles on codeforces- based on rating codeforces divide rating into 10 part. Newbie Pupil Specialist Expert Candidate Codemaster Master International Master Grandmaster Internat

Apple Division CSES Problem Set Solution | CSES Problem Set Solution Apple division with code

 Apple Division CSES Problem Set Solution | CSES Problem Set Solution Apple division with code - Apple Division CSES Problem Solution Easy Explanation. Apple division is problem is taken form cses introductory problem set.Let's Read Problem statement first. Problem Statement- Time limit:  1.00 s   Memory limit:  512 MB There are  n n  apples with known weights. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal. Input The first input line has an integer  n n : the number of apples. The next line has  n n  integers  p 1 , p 2 , … , p n p 1 , p 2 , … , p n : the weight of each apple. Output Print one integer: the minimum difference between the weights of the groups. Constraints 1 ≤ n ≤ 20 1 ≤ n ≤ 20 1 ≤ p i ≤ 10 9 1 ≤ p i ≤ 10 9 Example Input: 5 3 2 7 4 1 Output: 1 Explanation: Group 1 has weights 2, 3 and 4 (total weight 9), and group 2 has weights 1 and 7 (total weight 8). Join Telegram channel for code discussi

Movie Festival CSES problem set solution

 Movie Festival CSES problem set solution - Problem statement-  Time limit:  1.00 s   Memory limit:  512 MB In a movie festival  n n  movies will be shown. You know the starting and ending time of each movie. What is the maximum number of movies you can watch entirely? Input The first input line has an integer  n n : the number of movies. After this, there are  n n  lines that describe the movies. Each line has two integers  a a  and  b b : the starting and ending times of a movie. Output Print one integer: the maximum number of movies. Constraints 1 ≤ n ≤ 2 ⋅ 10 5 1 ≤ n ≤ 2 ⋅ 10 5 1 ≤ a < b ≤ 10 9 1 ≤ a < b ≤ 10 9 Example Input: 3 3 5 4 9 5 8 Output: 2 Solution - Step -1 take input in a vector as a pair first element is ending time ans second element is starting time Step -2 sort the vector in increasing order because we will watch that movie which is ending first. Step -3 iterate over vector and calculate ans .           follow code below-