We need to analyze problem: Jafar has n cans, each with current volume a_i and capacity b_i. He wants to pour all the remaining cola into just two cans. This means we can pour cola from other cans into two target cans. We can choose any two cans (maybe the same? No, just 2 cans distinct? It says "into just 2 cans", likely meaning at the end only two cans contain cola, all others empty. Typically in such problems, we can pour cola arbitrarily between cans, but capacity constraints: you can't exceed the capacity of a can. You can pour from any can to any other can, possibly in multiple steps, but the goal is to end with exactly two cans having some volume (and they must not exceed capacity). The rest should be empty.
We need to check if it's possible.
Given all a_i and b_i. We can select two cans, maybe we can also empty some cans by pouring into others. The question: Is there a way to redistribute the total volume (sum of a_i) into two cans such that each selected can does not exceed its capacity, and the rest are empty? Also, we are allowed to pour only among the cans; we can't discard cola; we need to preserve total volume S = sum_i a_i.
We need to see if we can choose two cans i and j such that their capacities are at least the amounts we pour into them, and the total volume equals S, i.e., we can fill them with amounts x and S - x, where 0 <= x <= b_i, and 0 <= S - x <= b_j. Also, we must be able to empty all other cans. But pouring from others to these two is possible if we can transfer all volume from other cans to these two. However, there might be a constraint: the total capacity of the two selected cans must be at least S (obviously). But also, the capacities of the other cans might matter? Because we need to pour out from them. However, we can always pour out from a can as long as we have somewhere to pour it. Since we only have two destination cans, if those two have enough capacity to hold all volume, we can empty all others into them. But can we do it without exceeding capacity during intermediate steps? Typically, we assume we can pour arbitrarily, so intermediate steps are not constrained (or we can do multiple steps so that we never exceed capacity because we can pour gradually). Usually, in such problems, you can always pour as long as the final state satisfies constraints, because you can always pour from a can into another in a way that does not exceed capacities if the final amount fits. But careful: There could be a scenario where the total volume is less than or equal to the sum of capacities of the two selected cans, but perhaps some intermediate step might require a can to hold more than its capacity if we try to pour all at once. But we can always pour in small amounts, and since the destination capacity is not exceeded at final, we can always arrange to pour sequentially from other cans into the two without exceeding capacities? Let's think: Suppose we have two cans i and j, with capacities b_i and b_j, and initial volumes a_i and a_j. Other cans have some volumes. The maximum volume that can be added to can i is b_i - a_i (the available space). Similarly for j. We can gradually pour from other cans into i and j, possibly also pouring between i and j if needed. But to empty all other cans, we must transfer all their volume to i and/or j. As long as total volume to transfer (sum of a_k for k not i,j) plus initial volumes a_i, a_j is <= b_i + b_j, we can distribute that total between i and j. But we also need that the distribution can be achieved given the capacities of i and j individually: we need to find amounts x_i and x_j such that x_i <= b_i, x_j <= b_j, and x_i + x_j = S, and also x_i >= a_i? Actually, we can also pour from i to j or vice versa. So the final amounts in i and j can be any pair that sums to S and each <= capacity. However, we must also consider that the initial amounts in i and j are already there; we could pour from i to j or j to i. So we can adjust.
But is there any other constraint? For example, if we choose two cans with capacities b_i and b_j, we need S <= b_i + b_j. Because the total volume must fit within the two selected cans. Also, we need each can individually not exceed its capacity. So we need existence of x_i, x_j such that 0 <= x_i <= b_i, 0 <= x_j <= b_j, and x_i + x_j = S.
This is equivalent to: max(0, S - b_j) <= b_i and max(0, S - b_i) <= b_j? Actually, condition: there exists x_i with 0 <= x_i <= b_i and S - x_i <= b_j => x_i >= S - b_j. So we need interval [max(0, S - b_j), min(b_i, S)] non-empty. So condition: max(0, S - b_j) <= min(b_i, S). Which simplifies to: S - b_j <= b_i (since S - b_j could be negative, but we also need S - b_j <= b_i automatically if S <= b_i + b_j?). Let's derive: The necessary and sufficient condition for existence is S <= b_i + b_j and also S >= 0 (trivial) and also we need that the lower bound is <= upper bound. Lower bound L = max(0, S - b_j). Upper bound U = min(b_i, S). Condition: L <= U. Since S <= b_i + b_j implies S - b_j <= b_i. Also S - b_j could be negative, then L = 0. Then condition is 0 <= min(b_i, S). That's always true because min(b_i, S) >= 0. So indeed the condition reduces to S <= b_i + b_j. Wait, what about if S > b_i? For S > b_i, then U = b_i. L = max(0, S - b_j). Since S > b_i, and we also have S <= b_i + b_j => S - b_j <= b_i, so L <= b_i. But need L <= b_i. That's satisfied. But also we need L <= U => max(0, S - b_j) <= b_i. If S - b_j > 0, then condition is S - b_j <= b_i, which is exactly S <= b_i + b_j. If S - b_j <= 0, then condition is 0 <= b_i, which holds. So yes, the only condition is S <= b_i + b_j. But also we must have that each can can hold at least the volume we pour into it; but if S <= b_i + b_j, we can always partition S into two parts that fit? For example, S = 10, b_i = 3, b_j = 7 => 3+7=10, works: we can put 3 and 7. If b_i=3, b_j=6 => 3+6=9 <10, S > sum => fails. So condition is S <= b_i + b_j. That seems necessary and sufficient.
But is there any other constraint? Consider the initial volumes in the two cans: We might need to pour from these two cans themselves. For instance, suppose we choose can i and can j. Initially they have a_i and a_j. To achieve final amounts x_i and x_j, we may need to pour some of their current volume out? But we can pour between i and j as well. Actually, we can pour from i to j and vice versa. So we can adjust. However, if we want to achieve a particular distribution, we can pour from one to the other. But is there any limitation because we can only pour from cans that have volume? But we can pour arbitrarily. So we can always adjust as long as we have the two cans. For example, suppose we choose i and j. Initially they have a_i and a_j. The total volume in all cans is S. The volume in other cans is S - a_i - a_j. We can pour from other cans into i and j. But can we achieve any x_i, x_j? Let's think: we have control. We can first pour all other cans' volume into i and j in any proportion, as long as we don't exceed capacities during the process. But if final x_i and x_j are within capacities, we can always do it by gradually filling them. Possibly we might need to pour from i to j to adjust, but that might cause intermediate overflow? But we can pour directly from other cans to the destination cans, and also we can pour between i and j. The question: can we always achieve any (x_i, x_j) with x_i + x_j = S, 0 <= x_i <= b_i, 0 <= x_j <= b_j, given that initially i has a_i and j has a_j and others have the rest? I think yes, because we can do: First, pour all other cans into i and j arbitrarily to fill them to target amounts, but we need to ensure we don't exceed capacities during the process. If we pour from a source can gradually, we can keep track. The only risk is if we try to pour too much into a can at once, but we can pour in small increments. However, the total amount added to i is x_i - a_i (if x_i >= a_i) or we may need to remove some if x_i < a_i. To reduce i's volume, we would need to pour from i to j or to other cans. But we can pour from i to j. Since we can pour between i and j, we can adjust after adding others. Consider a plan: Pour all other cans' volume into one of the two (say i) up to its capacity, then the remaining into j. But what if j's capacity is not enough to hold the rest? Then that plan might exceed j's capacity. But we can instead distribute appropriately. Because we have full control over the order, we can ensure that at no point does any can exceed its capacity. Typically, these problems assume that you can pour arbitrarily (like you can always move cola from one can to another without restrictions except final capacities). Most solutions for similar problems (like "pour into two bottles") only check the condition that total volume <= sum of two largest capacities? Actually, there is a known problem: "Jafar and cans" maybe from Codeforces? I recall a problem: "Jafar and ...?" Actually, I think it's "Jafar and ..."? Not sure. Let's search memory: There is a problem from Codeforces: "Jafar and Two Cans" or something similar. I recall a problem with n cans, capacities, and ask if you can pour all into two cans. Usually solution: Check if total sum <= sum of two largest capacities? But that might be too simplistic? Let's examine examples.
Example 1:
n=2, a=[3,5], b=[3,6]
S=8, b_i+b_j=3+6=9, S<=9 => YES. Output YES.
Example 2:
n=3, a=[6,8,9], b=[6,10,12]
S=6+8+9=23. Two largest capacities? b = 6,10,12 => two largest are 10+12=22, S=23>22 => NO. Output NO. So condition fails? But maybe other pair? Could we pick 6 and 12: 6+12=18 <23, 6+10=16, 10+12=22. All less than 23 => no pair satisfies S <= b_i+b_j. So answer NO.
Example 3:
n=5, a=[0,0,5,0,0], b=[1,1,8,10,5]
S=5. Need two cans such that sum capacities >=5. Many pairs: e.g., (8,10): 18 >=5; (8,5):13; (10,5):15; (1,1):2 <5; but we must choose two cans. Condition S <= b_i+b_j. So if any pair satisfies, answer YES. Let's check pairs: can3 capacity8, can4 capacity10, can5 capacity5, can1 capacity1, can2 capacity1. Pair (3,4): 8+10=18 >=5, yes. So YES. Output YES.
Example 4:
n=4, a=[4,1,0,3], b=[5,2,2,3]
S=4+1+0+3=8. Capacities: 5,2,2,3. Need two cans with sum capacities >=8. Pairs: (5,2)=7<8; (5,2) (the other 2)=7; (5,3)=8 => exactly 8. So condition satisfied: (5+3)=8 >=8. Output YES.
All examples match the simple condition: There exists two cans (i, j) such that b_i + b_j >= S.
But is it sufficient? Let's try to construct a counterexample. Suppose we have n=3, capacities: b1=10, b2=10, b3=1. Volumes: a1=9, a2=9, a3=0. S=18. Two largest capacities sum = 10+10=20 >=18. So condition satisfied. Can we pour all into cans 1 and 2? Initially, can1 has 9, can2 has 9, can3 empty. We need final amounts in can1 and can2 summing to 18, each <=10. So possible final amounts: (10,8) or (9,9) or (8,10). To achieve (10,8): we need to move 1 from can2 to can1? Actually from can2 to can1? But can1 initial 9, to become 10 needs +1; can2 initial 9, to become 8 needs -1. So we can pour 1 from can2 to can1. That is allowed because can1 capacity 10, after adding 1 becomes 10, okay; can2 after removing 1 becomes 8. So it's fine. But is there any issue with can3? It's empty, nothing to pour. So yes, it seems possible.
Now consider a scenario where total sum S is <= b_i + b_j, but the initial volumes in the two selected cans are such that it's impossible to redistribute because other cans have volume but you can't pour them without exceeding capacities? For example, suppose we have two cans with capacities b_i=5, b_j=5. Others: a third can with volume 5, and initial a_i=5, a_j=0. S=10. Two selected: i and j. Condition: b_i+b_j=10 >=10, okay. But can we achieve? We need final amounts x_i, x_j with sum 10 and each <=5 => must be exactly (5,5). Initially, i is full (5), j empty (0), third can has 5. To achieve (5,5), we need to pour the 5 from third can into j. That's straightforward: pour 5 into j, j becomes 5, done. But intermediate: j capacity 5, we pour 5 into empty j, okay. So fine.
Consider a more tricky case: b_i=5, b_j=5, but initial a_i=4, a_j=4, third can has 2. S=10. Need final (5,5). So we need to move 1 from third can to i (to make 5) and 1 to j (to make 5). That's possible; we pour 1 into i, i becomes 5, then pour remaining 1 into j, j becomes 5. Or pour all 2 into j (j would become 6, exceeding capacity). But we can pour carefully. So fine.
What about scenario where the two cans selected are not the ones with largest capacities but still sum >= S. Could there be a situation where the other cans have large volumes and the two selected cans have limited capacities, but S is small enough to fit? That seems fine.
But consider scenario: The two cans we select have capacities that sum to >= S, but one of them initially contains a volume that exceeds its capacity? That can't happen because a_i <= b_i by input. So initial amounts are within capacities.
Potential tricky issue: The pouring process may require temporary storage or may cause overflow when pouring from a source can to a destination can because the destination may exceed capacity if we pour too much at once. But we can pour in small amounts. If we pour from a source can to a destination can, we can pour just enough to fill the destination to capacity, then stop, and pour the rest elsewhere. That's fine. So we can always avoid overflow by checking how much space is left.
Thus, it seems the condition is exactly: Is there a pair of cans (i, j) (i != j) such that b_i + b_j >= S? But wait, what if we choose the same can twice? The problem says "into just 2 cans", so presumably two distinct cans. Usually, it means exactly two cans contain cola at the end, and they are presumably distinct cans. So we need two distinct cans.
Now, is that all? Let's test more edge cases. Suppose n=2, but the two cans already contain some cola. Condition S <= b1+b2 is always true because a1 <= b1 and a2 <= b2, so S = a1+a2 <= b1+b2? Not necessarily: Could have a1=5, b1=5; a2=5, b2=5; S=10, b1+b2=10 => okay. Could have a1=5, b1=5; a2=6? But a2 <= b2, so a2 <= b2. So a1+a2 <= b1+b2? Not necessarily: Suppose b1=5, a1=5; b2=5, a2=5; then equality. If b1=5, a1=5; b2=4, a2=4? Then a2<=4, S=9, b1+b2=9, okay. Actually, because a_i <= b_i, so sum a_i <= sum b_i. For n=2, S <= b1+b2 holds automatically. So answer always YES for n=2. That matches sample.
But what about n>2? Could there be a case where sum a_i <= b_i+b_j for some i,j but still impossible due to something like the two cans are not large enough to receive all the cola from the other cans because the other cans have capacities that limit pouring? For instance, suppose we have three cans: Can1: a=0, b=5; Can2: a=0, b=5; Can3: a=10, b=10. S=10. We can choose can3 and can1? But we need two cans. If we choose can3 and can1, capacities 10+5=15 >=10. But can we pour all cola into these two? Initially, can3 has 10, can1 0, can2 0. If we want only can3 and can1 to contain cola, we need to empty can2 (already empty) and keep can3 and can1. But can2 is already empty. So final state could be can3=10, can1=0. That's fine. But we might need to have exactly two cans containing cola, not necessarily non-zero. The problem says "pour all remaining cola into just 2 cans". It doesn't say they must be non-zero. So we can have one can empty, the other full. So that's okay.
Now consider a case: n=3, capacities: b1=10, b2=1, b3=1. Volumes: a1=9, a2=1, a3=1. S=11. Two largest capacities: 10+1=11 exactly. So condition holds for pair (1,2) or (1,3). Let's test: Choose cans 1 and 2. Need to end with only those two containing cola, total 11, capacities 10 and 1. Must have (10,1) exactly. Initially: can1=9, can2=1, can3=1. To achieve final: can1=10, can2=1, can3=0. So we need to move 1 from can3 to can1. Can1 capacity 10, after adding 1 becomes 10. Can2 stays at 1. Can3 becomes 0. That works. So possible.
Now consider a more complex scenario where the two selected cans have large capacities but the other cans have very small capacities that might prevent pouring? Actually, pouring from a can to another does not depend on its capacity; you can always pour out from a can regardless of its capacity. So no issue.
Thus, condition appears sufficient.
But wait, could there be a scenario where we need to pour cola from one of the two selected cans to the other because we need to adjust, but the capacities may be limited such that you can't pour from one to the other without exceeding? For example, suppose we choose two cans i and j. Initially, a_i is high, a_j is low, but we want final x_i less than a_i, i.e., we need to move some volume from i to j. That's possible as long as j can accept that volume without exceeding b_j. Since we can pour any amount from i to j, we can do it as long as a_i + a_j <= b_i + b_j? Actually, if we want to reduce i, we pour from i to j. The maximum we can pour from i to j is limited by b_j - a_j (the space in j). If we need to move more than that, we might need to involve other cans. But since we are also moving volume from other cans into these two, we might need to manage.
Consider: We choose cans i and j. We need final amounts x_i and x_j. The net change for i is Δ_i = x_i - a_i; for j is Δ_j = x_j - a_j. And total net from other cans must be transferred: sum_{k≠i,j} a_k = S - a_i - a_j = Δ_i + Δ_j (since total added to i and j equals the volume from others, but we might also transfer between i and j). Actually, we can pour from i to j or j to i. So effectively, we can adjust the distribution arbitrarily as long as final volumes are within capacities. The process: We have sources: all cans. We can pour from any can to any of the two selected cans. But we can also pour between the two selected cans. So the allowed operations: we can move cola from any can to any of the two cans, and also between the two cans. This effectively means we can arbitrarily reassign the total volume between the two cans, as long as each does not exceed its capacity. The only constraint is that we must be able to move volume from the other cans into the two without intermediate overflow. However, we can always pour from a source can directly into a destination can until it is full, then pour the rest into the other. But if we need to pour some from i to j to make room for some from others, we can do that before adding others. In other words, we can always achieve any (x_i, x_j) with x_i ≤ b_i, x_j ≤ b_j, and x_i + x_j = S, provided that we can transfer the volume from others appropriately. A more formal argument: Since the total volume to transfer from others is T = S - a_i - a_j. We can first pour all T into can i, but if can i cannot hold all (b_i - a_i < T), we pour until i is full, then the remainder into j. But j might also have limited space. However, we also have the ability to adjust by first pouring some from i to j to create space. So we can achieve any distribution as long as there is a feasible final distribution. Let's prove: Given target (x_i, x_j) feasible, we need to show that we can get from initial (a_i, a_j) and others with total T to final (x_i, x_j) with others empty. The net transfers from others to i and j must sum to T. In addition, we may transfer between i and j. Let y be the amount we transfer from i to j (if negative, it's from j to i). Then we have: final i = a_i + (inflow from others to i) - y (if y>0 means outflow from i to j) or + y if y negative? Let's define y as net amount moved from i to j (positive if i -> j). Then: final i = a_i + in_i - y; final j = a_j + in_j + y. And in_i + in_j = T. So we have: x_i = a_i + in_i - y, x_j = a_j + in_j + y. We can choose in_i, in_j, y. The unknowns are in_i, in_j, y with constraints in_i >=0, in_j >=0, in_i+in_j=T, and also during the process we must not exceed capacities, but final okay. We need to show there exists a set that can be implemented without intermediate overflow if we choose the order carefully. But simpler: We can first adjust i and j by moving cola between them to create suitable space, then pour from others. Let's propose a plan:
We want final (x_i, x_j). Compute required net change: Δ_i = x_i - a_i, Δ_j = x_j - a_j. Note Δ_i + Δ_j = T.
Case 1: Δ_i >= 0 and Δ_j >= 0. That means we need to add to both. So we need to pour from others into both. Since Δ_i and Δ_j are nonnegative, we can directly pour Δ_i into i and Δ_j into j from others. As long as we don't exceed capacities while pouring. While pouring Δ_i into i, we need to ensure i doesn't exceed capacity. Since final x_i <= b_i, and initial a_i <= b_i, adding Δ_i gradually will not exceed b_i as long as we stop at x_i. Same for j. So we can pour exactly Δ_i into i and Δ_j into j. Since others have exactly T volume, it's fine. So possible.
Case 2: Δ_i >= 0, Δ_j < 0 (need to remove from j and add to i). So T = Δ_i + Δ_j, but Δ_j negative, so T could be less than Δ_i. Actually Δ_i + Δ_j = T, so Δ_i = T - Δ_j, with Δ_j negative, so Δ_i > T. That seems contradictory because Δ_i positive and Δ_j negative, sum equals T which is non-negative (since T >=0). But if Δ_j is negative, its magnitude could be up to a_j. So we need to pour from others into i and also move some from j to i? Or we could move from j to i and also pour from others into i? Let's think differently. Instead of splitting into two cases, we can always first pour all from others into i (or into j) and then adjust between i and j. Let's try a general algorithm:
Step 1: Pour all cola from other cans (not i or j) into can i. But we might exceed capacity of i if b_i - a_i < T. However, we can first pour from others into i until i is full, then pour the rest into j. That works if we have enough capacity in j to hold the remainder. But what if after filling i, the remainder doesn't fit into j? That is exactly the condition b_i + b_j >= S? Let's see: After pouring all others into i until full, we fill i to b_i. The amount poured into i = b_i - a_i. The leftover from others is T - (b_i - a_i). That leftover must go into j. So we need that j can accommodate that leftover: initial a_j plus leftover <= b_j => a_j + (T - (b_i - a_i)) <= b_j => a_j + T - b_i + a_i <= b_j => (a_i + a_j + T) - b_i <= b_j => S - b_i <= b_j => S <= b_i + b_j. So if S <= b_i + b_j, then the leftover fits into j. But j might also exceed capacity? Actually, the condition ensures that the final amount in j = a_j + (T - (b_i - a_i)) = S - b_i <= b_j, so okay. However, we need to check that during the pouring, we don't exceed j's capacity before we pour leftover? But we pour into j after i is full, so we pour the leftover directly into j. Since we pour exactly the leftover amount, and j has a_j initially, final will be S - b_i which is <= b_j. So no overflow. But what if we pour into i and j in a different order? The algorithm described works: first pour from others into i until full, then pour remaining into j. This works as long as S <= b_i + b_j. So if condition holds, there is a simple pouring scheme: for the selected two cans, pour all other cans' cola into one of them (say i) until it's full, then pour the rest into the other. This scheme never exceeds capacities because we stop when i is full and then pour exactly the rest into j, which we already checked fits. Also, we don't need to pour between i and j. So indeed, if S <= b_i + b_j, it is possible.
But what about if we need to empty some cans into j first? The algorithm works regardless of initial amounts because we consider the total volume from others and the initial amounts in i and j. Let's formalize:
We pick two cans p and q. Let S = total volume. We want to end with only p and q non-empty. We can do: Pour from all other cans (not p,q) into p until p is full. While pouring, we monitor p's volume; we stop when p reaches capacity. Any remaining cola from others we pour into q. Also, we might have cola already in p and q initially. But the algorithm essentially combines: Initially, p has a_p, q has a_q. Others have T = S - a_p - a_q. We pour from others into p. The amount we can pour into p without exceeding capacity is up to b_p - a_p. So we pour min(T, b_p - a_p) into p. Let poured_to_p = min(T, b_p - a_p). Then p becomes a_p + poured_to_p. Remaining others volume = T - poured_to_p. Then we pour all remaining others into q. q becomes a_q + (T - poured_to_p). We need to check that q does not exceed its capacity: a_q + (T - poured_to_p) <= b_q? That's the condition for success. Also we need that after pouring into p, p is not overfull (we ensured not). So condition: a_q + T - min(T, b_p - a_p) <= b_q.
If T <= b_p - a_p, then min = T, so q gets no extra, stays at a_q, and condition a_q <= b_q holds (true). So success.
If T > b_p - a_p, then min = b_p - a_p, so q gets a_q + T - (b_p - a_p). Condition: a_q + T - (b_p - a_p) <= b_q => (a_p + a_q + T) - b_p <= b_q => S - b_p <= b_q => S <= b_p + b_q.
Thus, condition is exactly S <= b_p + b_q. So we have a constructive pouring strategy that works if and only if S <= b_p + b_q. Thus, the problem reduces to checking if there exist two distinct cans with capacities summing to at least S.
Therefore, solution: Compute total sum S = sum a_i. Then find two largest capacities? Because if the sum of the two largest capacities is >= S, then obviously there exists a pair (the two largest) that satisfies. However, is it sufficient to check only the two largest? Could there be a case where the two largest capacities sum is < S, but some other pair (not necessarily the two largest) sums to >= S? If the two largest sum is less than S, then any other pair, which uses at most one of the largest or smaller, will have sum <= sum of two largest (since replacing any can with a smaller one reduces sum). Indeed, the maximum possible sum of capacities from any two distinct cans is achieved by the two largest capacities. So if the sum of the two largest capacities is < S, then no pair can have sum >= S. Conversely, if the sum of the two largest capacities is >= S, then that pair works. So the condition reduces to: Is total sum S <= max1 + max2, where max1 and max2 are the two largest capacities among b_i? However, careful: The two cans must be distinct. So if there are multiple cans, we take the two highest b_i.
But is there any nuance about using the same can twice? No, must be distinct.
Thus, answer is YES if sum(a) <= sum of two largest b_i; otherwise NO.
But wait, check sample 4: b = [5,2,2,3]. Two largest are 5 and 3 = 8, sum a = 8, so YES.
All examples align.
Now, let's test some edge cases: n=100000, all a_i=0, sum S=0. Then condition: two largest capacities sum >= 0 always true. So YES. But can we pour all cola into two cans? All are empty, we can just leave any two cans (maybe empty) and empty others? Since total volume is zero, we can have all cans empty, but the problem says "pour all remaining cola into just 2 cans", which might imply that at the end only two cans contain cola, but if all are empty, then all cans are empty, which is not exactly "just 2 cans contain cola"? It says "pour all remaining cola into just 2 cans", meaning we want to end up with only two cans having cola, and the rest empty. If all cola is zero, then all cans are empty; that satisfies that only two cans contain cola? Actually, if all are empty, then no can contains cola. But the requirement might be ambiguous. Usually, "pour all remaining cola into just 2 cans" implies that after pouring, exactly two cans (maybe some cans are empty) hold all the cola. If total cola is zero, then it's trivially true: we can have any two cans empty and the rest empty. But does that count as "into just 2 cans"? Since there is no cola, it's vacuously true? I think it's YES. Most problems would accept YES. Because condition sum <= capacities will be true, so answer YES. That's fine.
Now, what about case where the two largest capacities are the same can? We need two distinct cans. If there is only one can with high capacity and others lower, we still need two distinct cans. The two largest capacities might be the same value but from two different cans. If there are at least two cans, we can pick them. If n=2, then it's the two anyway.
If n=1? But constraints say n>=2, so no problem.
Thus, the solution is simply: compute total sum S, find the two largest b_i. If S <= max1 + max2, output YES, else NO.
But wait, there is a nuance: The two cans we pick must be the ones we pour into. But we also need to consider that we may need to pour from one of the selected cans to the other if necessary? However, as we argued, the pouring scheme works. But is there a scenario where the sum condition holds but we cannot physically pour because the two selected cans are the same? No, we pick two distinct.
But let's double-check the algorithm of pouring from others into p until full then remainder into q. That algorithm works regardless of which cans we pick? The algorithm used p as the first target. But what if the initial volume in q is high, and after pouring remainder, q might exceed capacity? Condition S <= b_p + b_q ensures that a_q + (T - (b_p - a_p)) <= b_q, as derived. So fine.
But what about the scenario where we cannot pour from others directly into p because p might already be full? If p is initially full (a_p = b_p), then b_p - a_p = 0. Then poured_to_p = min(T, 0) = 0. So all T goes to q. Then condition becomes a_q + T <= b_q? But S = a_p + a_q + T, so a_q + T = S - a_p. Condition S - a_p <= b_q => S <= a_p + b_q. Since a_p = b_p, that becomes S <= b_p + b_q. So condition holds. So algorithm works: we pour all others into q. But we need to ensure that while pouring into q, we don't exceed capacity? Since we pour total T into q, we need a_q + T <= b_q, which is exactly the condition. Since S <= b_p + b_q and a_p = b_p, we have a_q + T = S - b_p <= b_q. So okay.
Thus, the condition is sufficient.
Now, what about the case where the two selected cans are not the two largest, but we use the algorithm. The condition S <= b_i + b_j holds, so algorithm works. So we don't need to specifically pick the two largest; if any pair satisfies, then answer YES. But for checking existence, it's enough to check the two largest because if there exists any pair, then the two largest will definitely satisfy S <= their sum? Actually, if the two largest sum is < S, then no pair can have sum >= S. So condition is equivalent.
Thus, the solution seems correct.
But is there any hidden constraint: we must pour all cola into exactly two cans, meaning that after pouring, exactly two cans must contain cola, and the others must be completely empty. In our algorithm, we pour all from others into p and q, so others become empty. And we end with p and q containing cola. So it's fine.
Edge case: The two selected cans might be such that after pouring, one of them might be empty? That's allowed because "into just 2 cans" doesn't require both to be non-empty. So okay.
One more nuance: Could there be a scenario where S <= b_i + b_j but it's impossible because one of the cans is needed to hold cola that originally was in the other can, but we can't pour from a can to itself? That doesn't matter.
Thus, problem reduces to trivial.
But I recall a Codeforces problem: "Jafar and Two Cans" might have a twist: The capacities are given, and we need to check if we can pour all cola into two cans. Many participants solved by checking sum of two largest capacities. However, I want to double-check if there is any additional nuance like the fact that we can't pour cola from a can that we intend to be one of the final two if that would require emptying it and then filling it again? But we can pour between them.
Let's test some random cases with brute force small n to be sure. But I'm reasoning mathematically.
Given total sum S, we want to allocate to two cans with capacities b_i, b_j. The necessary and sufficient condition for existence of allocation is S <= b_i + b_j. This is clear. The question: Is it always possible to achieve such allocation through pouring operations given the initial volumes? We gave a constructive algorithm: pour from all other cans into one of the two until it's full, then pour the rest into the other. That algorithm only uses pouring from others to the two cans, not between the two cans. But it assumes we can pour from others directly into both cans, which is allowed. It also assumes that when we pour from others into p, we can stop when p reaches capacity. That is possible because we can control how much we pour. So it's feasible.
But consider scenario where we choose p and q, and p is not the one we pour first. The algorithm works regardless of which of the two is designated as the "first" to fill. The only requirement is that the amount we pour into q after filling p does not exceed its capacity. That is exactly the condition S <= b_p + b_q.
However, what about the fact that we might need to pour from a can that is not among the two and that can might have limited capacity? But pouring from a can doesn't depend on its capacity; we can always pour out all its contents regardless of its capacity. So no issue.
Thus, the answer is YES if and only if total volume <= sum of two largest capacities.
But wait, is there a scenario where total volume is <= sum of two largest capacities, but the two largest capacities belong to the same can? That cannot happen because we need two distinct cans. But the two largest capacities could be from two different cans. If there are two cans with same capacity, both are largest. If there is only one can with that capacity, the second largest is smaller. So it's fine.
Now, double-check the examples: Example 2, S=23, two largest b: 12 and 10 sum=22<23 => NO. Example 1: two largest sum=6+3=9, S=8 => YES. Example 3: two largest b: 10 and 8 sum=18, S=5 => YES. Example 4: two largest b:5 and 3 sum=8, S=8 => YES.
Thus, matches.
But wait, what about Example 4: a=[4,1,0,3], b=[5,2,2,3]. The two largest capacities are 5 and 3, sum=8, S=8. So condition holds. But is there a specific pouring scheme? Let's try: choose can1 (cap5, a=4) and can4 (cap3, a=3). Others: can2 (a=1, cap2), can3 (a=0, cap2). Total others T=1+0=1. b1 - a1 =1, b4 - a4 =0. If we pour from others into can1 first: we can pour the 1 into can1, filling it to 5. Then nothing left for can4. Final: can1=5, can4=3, others empty. That's valid. If we poured into can4 first: can4 is full, so cannot accept any; then we would pour 1 into can1, can1 becomes 5, same. So yes.
Thus, it works.
Now, let's consider a borderline case: Suppose we have cans: b1=5, a1=5; b2=5, a2=0; b3=5, a3=5; total S=10. Two largest capacities are 5 and 5 (both many). Condition S=10 <=10 => YES. Pick cans 1 and 2. Initially: can1=5, can2=0, can3=5. We need final only 1 and 2 contain cola. According to algorithm: p=1, q=2. b1 - a1 =0, so poured_to_p = min(T,0)=0, where T=5 (from can3). Then we pour all 5 into q. q becomes 0+5=5. Final: can1=5, can2=5, can3=0. That works. However, note that q's capacity is 5, so we pour exactly 5 into it, no overflow. Good.
Now consider if we had chosen cans 1 and 3? Then both already have 5, sum=10, but we need to empty can2 (0) and keep only 1 and 3. That's already the case. So it's fine.
Now consider a scenario: b1=4, a1=4; b2=4, a2=0; b3=5, a3=5; S=9. Two largest capacities: 5 and 4 =9. Condition holds. Choose cans 2 (cap4, a2=0) and 3 (cap5, a3=5). T = S - a2 - a3 = 9 - 0 -5 =4. b2 - a2 =4, b3 - a3 =0. Pour into p=2 first: we can pour all 4 into can2, making it 4 (full). Then nothing left for can3. Final: can2=4, can3=5. Works. Alternatively, if we pour into can3 first, can3 is full, so we pour all 4 into can2. Works. So yes.
Now consider a scenario where the condition holds but the algorithm might fail because after pouring into p, the remaining volume may need to go into q, but we might have to pour from p to q as well if p is not full enough? But our algorithm only pours from others, not between p and q. That is sufficient.
Thus, the solution is extremely simple.
But before finalizing, let's consider if there is any scenario where S <= b_i + b_j but the algorithm fails because we cannot pour from others into p due to the fact that others might be the same as p or q? But we are only pouring from cans that are not p or q. That's fine.
Maybe there is a hidden constraint: "pour all remaining cola into just 2 cans" could be interpreted as you must select two cans at the start and pour everything else into them, but you cannot pour between the two selected cans. However, the problem statement doesn't specify restrictions on pouring. Usually, you can pour arbitrarily. So I think it's fine.
Given the simplicity, the intended solution is probably to check sum of two max capacities.
Let's search memory: There's a Codeforces Round problem 799? Not sure. I recall a problem "Jafar and Two Cans" from maybe Codeforces 346A? Actually, there is a known problem "Jafar and Two Cans" from ACM? Not sure. But it's likely that.
Thus, solution: read n, read a array, compute sum S. Read b array, find two largest values. If S <= max1 + max2 print YES else NO.
Complexities: O(n) time, O(1) memory.
But wait: The input constraints: n up to 100,000, a_i up to 1e9, b_i up to 1e9, sum could be up to 1e14, so use 64-bit integer (long long in C++).
Now, just to be absolutely thorough, let's examine the possibility that the two selected cans must be distinct and maybe we are allowed to use any two, but also maybe we could pour from the two selected cans to others temporarily? But we need final only two have cola, but intermediate we can use others as intermediate vessels? However, if we can use others as intermediate, that only makes it easier, so condition remains sufficient. But could it be that condition is not sufficient if we restrict pouring to only from other cans to the two selected, and we cannot pour between the two selected? But the statement doesn't impose any restriction. It says "Jafar has decided to pour all remaining cola into just 2 cans". It doesn't say he can't pour between those two. Usually, you can pour between any cans. So it's fine.
I'll now test a more complex scenario: n=4, b=[10,1,1,1], a=[9,1,1,1] => S=12. Two largest capacities: 10+1=11 <12 => NO. Could there be a way? We need two cans to hold total 12. The capacities are 10,1,1,1. Any pair sum <=11, so impossible. So NO.
Now consider b=[10,5,5], a=[10,0,5] => S=15. Two largest capacities: 10+5=15 => YES. Choose cans1 (10) and can3 (5). T = S - a1 - a3 = 15-10-5=0, so already only those two have cola? Actually, can2 has 0, so it's fine. But note can1 is full, can3 is full. So condition holds.
Now consider b=[10,5,5], a=[9,5,1] => S=15. Two largest capacities sum=15 => YES. Choose cans1 (10, a=9) and can2 (5, a=5). T = S - a1 - a2 = 15-9-5=1 (from can3). b1 - a1 =1, b2 - a2=0. Pour the 1 into can1, becomes 10. Final: can1=10, can2=5. Works.
Now consider b=[10,5,5], a=[5,5,5] => S=15. Choose cans2 and3 (both cap5, a=5 each). T = S - a2 - a3 = 15-5-5=5 (from can1). b2 - a2 =0, b3 - a3 =0. Pour all 5 into either can? Both are full initially. Our algorithm: choose p=2 (cap5, a=5), then poured_to_p = min(T, b2 - a2) = min(5,0)=0. Then pour remainder (5) into q=3, but q also has a_q=5, capacity 5, so a_q + T =5+5=10 >5 => overflow! So our algorithm fails if we pick two cans that are both already full and there is remaining volume. But condition S <= b2 + b3? b2+b3=10, S=15, so condition fails. That's why we don't pick them. So we must pick a pair where S <= b_i + b_j. So in this scenario, the two largest capacities are 10 and 5 = 15, so we would pick can1 (10) and one of the 5's. So algorithm works.
Now consider a scenario where the two largest capacities sum >= S, but both are already full, and there is extra volume from other cans? But if both are full, then their sum of capacities is exactly S? Let's design: b1=10, a1=10; b2=5, a2=5; b3=0, a3=0? Actually, we need S = total volume = a1 + a2 + a3. If b1=10, a1=10; b2=5, a2=5; and there is other can(s) with some volume, then S would be >15? Because a1 + a2 =15, and others add more, so S >15, contradicting S <= b1+b2=15. So if both are full, S must be at least a1 + a2 = b1 + b2. But S <= b1+b2, so S = b1+b2 exactly. Then there is no other volume (others must have zero). So no extra volume to pour. So algorithm works because T = S - a1 - a2 = 0. So fine.
Now consider scenario where one of the selected cans is full and the other has some free space. That is fine.
Thus, condition seems correct.
Now, is there any case where S <= b_i + b_j but the algorithm fails because we might need to pour from a can that is one of the selected ones into the other, but we are not doing that? Our algorithm only pours from others. But suppose we pick cans i and j. Initially, a_i is high, a_j is low. T = others volume. But maybe b_i - a_i is small, and b_j - a_j is large. Our algorithm pours first into i until full, then remainder into j. That works as we derived. Alternatively, if we had poured first into j, then remainder into i, also works. So at least one order works? Actually, both orders might work as long as condition holds. But is it always that at least one order works? According to the derivation, if S <= b_i + b_j, then both orders work? Let's check: For order (i first, then j) we need S - b_i <= b_j. That's equivalent to S <= b_i + b_j. For order (j first, then i) we need S - b_j <= b_i, which is the same condition. So both orders are equivalent; both work if condition holds. So we can choose either.
Thus, it's fine.
One more nuance: The pouring process might involve pouring from a can that is one of the two into the other to make space for others. But we don't need to.
Thus, solution is correct.
Now, check constraints: n up to 100k, so O(n) is fine.
Implementation: Read n, read array a (maybe we can compute sum without storing all a's), read array b, find the two largest b. While reading b, keep max1 and max2. Use long long for sum.
Edge cases: n=2, sum = a1+a2, and two largest are b1,b2. Since a_i <= b_i, sum a <= sum b, so condition automatically holds? Actually, for n=2, we have S = a1+a2, and b1+b2 >= a1+a2 because a1 <= b1, a2 <= b2 => sum a <= sum b. So condition always holds, so answer always YES. That's consistent.
But is there any scenario where n=2 but a1+a2 > b1+b2? Could happen if a1 > b1 or a2 > b2? No, because input guarantees a_i <= b_i individually, so sum <= sum. So always YES.
Thus, problem is trivial for n=2.
Now, let's consider if the two cans we select could be the same can? No, "just 2 cans" implies two distinct cans. If you could use the same can twice, it would be like pouring all into one can, but that would require capacity >= S, and condition would be S <= b_i (for a single can). But the problem says 2 cans, so distinct.
Thus, final solution: compute sum, find two largest b. If sum <= max1+max2, print YES else NO.
But wait, I recall a Codeforces problem "Pouring Rain" or "Jafar and ..." but anyway.
Double-check sample 3: a: 0 0 5 0 0, sum=5, b: 1 1 8 10 5. Two largest: 10 and 8 => 18 >=5 => YES. But note there is a can with capacity 5 as well. So condition holds.
Thus, answer.
But before writing final answer, let's test with some random cases via mental simulation:
Case: n=3, b=[2,2,2], a=[2,2,0] => S=4. Two largest sum = 2+2=4 => YES. Choose any two cans. Say cans1 and2: both full initially. T=0 => already only those two have cola? Actually, can3 empty, so cans1 and2 have cola, can3 empty => satisfies only two cans contain cola? But also can1 and can2 are the selected ones. Yes, it's already done. So YES.
Case: b=[2,2,2], a=[2,1,1] => S=4. Two largest sum=4 => YES. Pick cans1 and2: a1=2, a2=1, T=1 (can3). b1 - a1 =0, b2 - a2 =1. Pour from can3 into can2: can2 becomes 2, final: can1=2, can2=2. Works.
Case: b=[2,2,2], a=[2,0,2] => S=4, choose cans1 and3: a1=2, a3=2, T=0, already done.
Case: b=[2,2,2], a=[1,1,2] => S=4, choose cans1 and3: a1=1, a3=2, T=1 (can2). b1 - a1 =1, b3 - a3 =0. Pour can2 into can1 -> can1 becomes 2, final: can1=2, can3=2. Works.
Now consider a case where S <= sum of two largest capacities but the two largest capacities are from cans that are not the ones we might need? But we can choose those two largest. So it's fine.
Now, what about scenario where total sum is less than or equal to sum of two largest capacities, but the two largest capacities are the same can? That can't happen because there are at least two cans. If there is only one can with max capacity, then the second largest is smaller. That's fine.
Now, test a case: n=3, b=[100,1,1], a=[50,50,0] => S=100. Two largest: 100+1=101 >=100 => YES. Choose cans1 (100) and can2 (1). T = S - a1 - a2 = 100 - 50 - 50 = 0? Wait a2=50? But a2 must be <= b2=1, but a2=50 violates input constraint. So not allowed. So a2 must be <=1. Let's adjust: b=[100,1,1], a=[98,1,1] => S=100. Two largest sum=101>=100 => YES. Choose cans1 and2: a1=98, a2=1, T=1 (can3 volume 1). b1 - a1 =2, b2 - a2 =0. Pour T=1 into can1: can1 becomes 99, final: can1=99, can2=1. Both within capacities. Works. However, note can2 is already at capacity 1, so it's full; final can2 remains 1. So total 100. Yes.
Now, consider a tricky scenario: b=[100,100,1], a=[100,0,100]? But a3 <= b3=1, so a3 cannot be 100. So invalid.
Thus, input constraints ensure a_i <= b_i, so such extreme mismatches don't occur. But that doesn't affect condition.
Now, I'll reason about if there is any other hidden condition: Perhaps we cannot pour cola from a can that we intend to be one of the final two because we might need to use it as a source? But that's allowed. There's no restriction.
Thus, the solution is correct.
Let's quickly implement in C++.
Pseudo:
int main() {
int n;
cin >> n;
vector a(n); or just compute sum.
long long sum = 0;
for (int i=0; i<n; i++) {
int x; cin >> x; sum += x;
}
int max1 = -1, max2 = -1;
for (int i=0; i<n; i++) {
int b; cin >> b;
if (b > max1) {
max2 = max1;
max1 = b;
} else if (b > max2) {
max2 = b;
}
}
if (sum <= max1 + max2) cout << "YES" << endl;
else cout << "NO" << endl;
}
But careful: max1 and max2 are ints, but b_i up to 1e9, sum up to 1e14. So max1+max2 up to 2e9, which fits in 64-bit. Use long long for sum and for comparison. max1 and max2 could be int (since <=1e9, fits in 32-bit signed). But sum may be 1e14, need long long. So condition: sum <= (long long)max1 + max2.
Alternatively, store max1, max2 as long long.
Now, is there any scenario where we need to consider that the two cans we pick must not be the same? We already ensure distinct by tracking two largest different indices? But if there are duplicate values, it's fine; max1 and max2 can be equal if the two largest are equal, that's okay. But if there is only one can? n>=2, so at least two cans. So we will have two distinct cans (though they could have same capacity). The algorithm to find two largest should handle that: if the first element sets max1, then second element may become max2; if later we see same as max1, we update max2 if it's >= max2? Actually, typical code:
max1 = max2 = -1;
for each b:
if b > max1: max2 = max1; max1 = b;
else if b > max2: max2 = b;
This yields the two largest distinct values? But if there are multiple same large values, e.g., b = [5,5,3], then first 5 -> max1=5, max2=-1; second 5: since b > max1? 5 >5 false, then else if b > max2? 5 > -1 true -> max2=5. So we get max1=5, max2=5. That's correct: the two largest values are both 5, from two different cans. So condition holds.
If there is only one 5 and others smaller, max2 gets the next largest, which is smaller, but that's correct.
Now, test borderline: n=2, b=[5,5], max1=5, max2=5 -> sum capacities 10. S <= 10 always true because a1<=5, a2<=5 => S<=10. So YES.
Thus, solution works.
But is there any situation where we need to consider that we can pour cola from a can into another, but the capacities of intermediate cans might matter because we might need to use a third can as temporary? But we can pour directly. So no.
Thus, answer is simple.
Wait, I recall a known problem: "Jafar and Two Cans" might be from Codeforces Round #... I think the solution was indeed to sort capacities and compare with sum. But maybe there is a twist: The two cans chosen can be any two, but also we might need to consider that we can pour cola from a can into another only if the source can is not empty. That's obvious.
One more nuance: The problem statement: "Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!" It doesn't explicitly say that you can pour cola in any order or that you can pour from any can to any other. In such problems, usually, you can pour from any can to any other can, possibly in multiple steps. But sometimes there are restrictions like you can only pour from a can to another if the receiving can has enough capacity. But that's standard. We've accounted for capacity constraints during pouring. But we assumed we can stop when full. That's fine.
Thus, final answer: YES if sum(a) <= sum of two largest b.
Now, just to be extra safe, let's consider the possibility that the pouring process might require that at each step, the amount poured must be the entire content of the source can? Usually, you can pour any amount. But sometimes in such puzzles, you can only pour the entire content of a can at once (like typical water jug problems). However, the problem doesn't specify that. Usually, in Codeforces problems about pouring, you can pour any amount. The statement: "Jafar has decided to pour all remaining cola into just 2 cans" suggests he can pour cola from one can to another, probably any amount. There's no mention of restrictions. So we assume any amount can be poured.
But let's check if the problem could be interpreted as: You can pour cola from one can to another, but you must pour until the source is empty or the destination is full? Actually, typical real-life pouring: you can pour some amount, not necessarily all. So it's fine.
If the rule were that you must pour the entire content of a can at once (like you cannot partially pour), would that change the condition? Possibly yes. For example, consider b=[3,3,3], a=[2,2,2] S=6. Two largest sum=6 => YES under our condition. But if you must pour whole cans, can you achieve? Initially three cans each 2. You want only two cans to have cola. You could pour one full can into another, but that would make one can 4, exceeding capacity 3. So you cannot pour entire can because it would overflow. But you could pour partially: pour 1 from one can into another to fill it to 3, then the source becomes 1, etc. With partial pours, it's possible. If you must pour entire can, maybe not. But problem statement does not specify such restriction, so we assume partial pours allowed. Usually in algorithmic problems, you can pour any amount, unless stated otherwise. For example, Codeforces problem "Vasya and Socks" no. But there is "Pouring Rain" where you can pour any. So it's safe.
Thus, solution stands.
Now, final step: produce answer with implementation.
But before final, let's read problem statement again as given in the prompt: "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!" Input, output examples. There's no additional note about pouring restrictions. So we can pour arbitrarily.
Thus, answer is correct.
Now, I will write the solution in Python (since the user might expect Python solution). The function: read input, compute sum a, find two largest b, compare.
Implementation details: Use sys.stdin.read().split() for speed.
Edge Cases: None.
I'll produce final answer.
But the user might be expecting a reasoning? The question: "Jafar has n cans of cola..." I'm to answer? The chat context: The user provided the problem statement. Likely they want a solution explanation and code. Since it's a coding problem, I'll provide a solution in Python with explanation.
Given the instructions: I should respond with final answer, likely code. But the initial query: "Jafar has n cans of cola..." It's a problem statement. Possibly the user wants me to solve it. So I'll provide solution.
Thus, output: Python code.
But maybe the user wants a YES/NO answer for given input? No, they gave examples, but not specific. So they want generic solution.
Thus, final answer: print YES if sum(a) <= sum of two largest capacities else NO.
Now, I'll produce final response with explanation and code.
But note: The output should be "YES" or "NO". Case insensitive, but usually uppercase.
I'll produce Python code.
Complexities: O(n) time, O(1) extra.
Now, final answer. The problem reduces to checking whether the total amount of cola can fit into two cans.
Given the total volume (S = \sum a_i), it is possible if and only if there exist two cans with capacities whose sum is at least (S).
The maximum possible sum of two capacities is obtained from the two largest values in (b).
Therefore, we simply compute (S), find the two largest capacities, and output YES if (S \le \text{max1} + \text{max2}), otherwise NO.
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
# read a_i
total = 0
for _ in range(n):
total += int(next(it))
# find two largest capacities
max1 = -1
max2 = -1
for _ in range(n):
b = int(next(it))
if b > max1:
max2 = max1
max1 = b
elif b > max2:
max2 = b
if total <= max1 + max2:
print("YES")
else:
print("NO")
if __name__ == "__main__":
main()