Codeforces Round #681 (Div. 2) C. The Delivery Dilemma(贪心)

题目链接:https://codeforc.es/contest/1443/problem/C

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 (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.

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

The second line of each test case contains n integers a1…an (1≤ai≤109) — the time of courier delivery of the dish with the number i.

The third line of each test case contains n integers b1…bn (1≤bi≤109) — 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 2⋅105.

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

Example

input

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

5
3
2
3

分析

我们发现如果我们选了 ai ,那么 a 中所有小于 ai 的我们都可以选,从而不选 b ,因为 ai 已经比他们大了,选择他们对答案不会影响,并且可以避免选择 b 。
于是我们可以先将 a 升序排列,然后维护 b 的前缀和,从 1 到 n 遍历,ans = min(ans, max(a[i].a, sum[n] - sum[i])) ,答案就在这里面产生。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int t;
int n;
struct node{
	ll a;
	ll b;
}a[200007];
ll sum[200007];

bool cmp(node xx, node yy)
{
	return xx.a < yy.a;
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i].a);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i].b);
		sort(a + 1, a + 1 + n, cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i] = 0;
			sum[i] = a[i].b + sum[i - 1];
		}
		ll ans = sum[n];
		for(int i=1;i<=n;i++)
		{
			ans = min(ans, max(a[i].a, sum[n] - sum[i]));
		}
		printf("%lld\n",ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值