本文共 1547 字,大约阅读时间需要 5 分钟。
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai ≤ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 10^9) — volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 10^9) — capacities of the cans.
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Input
2
3 5 3 6Output
YES
Input
3
6 8 9 6 10 12Output
NO
Input
5
0 0 5 0 0 1 1 8 10 5Output
YES
Input
4
4 1 0 3 5 2 2 3Output
YES
In the first sample, there are already 2 cans, so the answer is "YES".
其实这个题意是比较简单的,要求第一行给出一个整数n表示n罐可乐,第二行是n个数来表示n个罐中剩余的可乐的体积,第三行是n个数来表示这n个罐的容量。然后来判断这些剩余的所有可乐能否全部装到n个罐中任选的两个罐中。
计算出所有剩余可乐的体积,然后与n个罐中体积最大的两个罐来比较就可以了。
#include#include using namespace std;int main(){ long long i,n,x,y,sum=0; long long max1=0,max2=0; //max1来存放最大的可乐罐的体积,max2来存放第二大的可乐罐体积 scanf("%lld",&n); for(i=0;i =max1) { max2=max1; max1=y; } else if(y>max2) { max2=y; } } if(sum <= max1+max2) printf("Yes\n"); else printf("No\n"); return 0;}
版权声明:本文为博主原创文章,未经博主允许不得转载。