We need to solve: There is a start island (no treasure, but possibly with a bridge capacity? Actually the start island has nothing, and there are bridges connecting it to each of the n treasure islands. Each bridge has a capacity? Wait: The problem description: "he found that there was an old bridge between this island and each of all other n islands." So each bridge connects start to a treasure island. So there is no direct bridge between treasure islands? It seems only start island connects to each treasure island. So the graph is a star: start node (0) and n leaf nodes (1..n). Each leaf has two numbers: treasure amount a_i, and maximal amount that he can take when he crosses the bridge to the island (i.e., the bridge capacity? Actually: "the amount of treasures of the island and the maximal amount that he can take when he crosses the bridge to the islands, respectively." So for island i, there is a bridge connecting start to island i. The bridge can only support a certain weight: when he crosses it, if he is carrying total treasures more than or equal to some threshold? Actually "the old bridges will break if he cross it with taking certain or more amount of treasures." It says: "the old bridges will break if he cross it with taking certain or more amount of treasures." So each bridge has a capacity: the maximal amount he can carry while crossing that bridge. The input gives for each island: the amount of treasure on that island (call t_i) and the maximal amount he can carry when crossing the bridge to that island (call c_i). Possibly this c_i is the capacity for the bridge to that island? Typically such problems: start at home, need to visit all islands (like nodes with weights), each edge has capacity limit; you can only cross an edge if your current load ≤ capacity. You start with load 0, you need to collect all treasures, bringing them back to start. Since the graph is a star, you must go from start to each island possibly multiple times? But you can only cross each bridge at most twice? Actually you need to collect all treasures. Since all islands are leaves, you must go to each island at least once to collect its treasure. You start at start with load 0. When you go to an island, you cross the bridge from start to island. At that point, you carry some load (the treasures you've collected so far). Then you pick up the treasure on that island (you can pick it up instantly? Probably you need to be on the island to pick up). Then you may return to start via the same bridge, crossing back, or go to another island? But there is no direct bridge between islands, so you must return to start to go to another island. So essentially each island must be visited in a sequence: start -> island i -> start -> island j -> start -> ... At the end, you must be back at start. So each visit consists of a round trip: from start to island and back. On the outward trip, you carry some load L (treasures collected so far). When you arrive at island, you pick up the new treasure, so your load becomes L + t_i. Then you need to return to start. On the return trip, you carry load L + t_i across the bridge in the reverse direction. The bridge capacity is the same in both directions? It likely is symmetric. The problem statement: "the maximal amount that he can take when he crosses the bridge to the islands" - might refer to crossing from start to island. But crossing back also involves the same bridge. Usually capacities are symmetric. So we assume that for each island i, the bridge has a capacity c_i: you cannot cross it carrying more than c_i, in either direction? It might be that the capacity is specified for crossing to the island, but crossing back maybe also constrained? Most likely both directions have same limit. Because if the bridge breaks if you carry certain or more amount of treasures, that would be true regardless of direction.
Thus the problem reduces to: Given n islands with treasure t_i and bridge capacity c_i, can we find an order of visiting them (each exactly once) such that for each island i visited at some point, during the outward crossing we have current load L <= c_i, and during the return crossing we have load L + t_i <= c_i? But careful: The return crossing occurs immediately after picking up the treasure. However, we could potentially deposit some treasure? He is bringing all treasures with him always. So after picking up, load increases and then he returns. So both outward and return must satisfy capacity constraint.
Alternatively, maybe the order of visits can be such that we do not return to start after each island? But there are no inter-island bridges, so you must return to start to go to another island. So indeed each island requires a round trip.
But note: The start island initially has no treasure; he lands there. He must start by going to some island, then return, etc.
Now, we need to check if there exists a permutation (order) of islands 1..n such that if we let current load L0 = 0 initially. For step k (visiting island i_k):
- Outward crossing: L_before = current load (sum of treasures collected so far). Must have L_before <= c_i.
- After picking up, load becomes L_before + t_i. Must satisfy L_before + t_i <= c_i for return crossing? Actually return crossing also requires that the load being carried while crossing (which is L_before + t_i) <= c_i. So effectively both conditions combine to: L_before <= c_i and L_before + t_i <= c_i. The second implies L_before <= c_i - t_i. Since t_i>0 (treasure amount presumably positive? It says amount of treasures; could be zero? Probably >0, but maybe 0? Possibly zero? But n≥1, and they have treasures, so t_i >0). So the stricter condition is L_before <= c_i - t_i? Actually if t_i > 0, then c_i - t_i < c_i. So the return crossing constraint is more restrictive because you have added weight. So we need L_before <= c_i - t_i? Wait: For outward crossing, you carry L_before. For return crossing, you carry L_before + t_i. Both must be ≤ c_i. So the most restrictive is max(L_before, L_before + t_i) = L_before + t_i ≤ c_i. So that gives L_before ≤ c_i - t_i. However, there is also outward crossing: L_before ≤ c_i, which is automatically satisfied if L_before ≤ c_i - t_i (since c_i - t_i ≤ c_i). So the only necessary condition is L_before ≤ c_i - t_i. But is outward crossing actually required? Yes, but the condition is weaker.
But careful: What about the final return? At the end, after collecting all treasures, he must be back at start. That means after the last island visit, he returns to start. So after the last return, he is at start with total load T = sum t_i. But we don't need to cross any bridge after that, so no constraint after final return. However, the return crossing for the last island must be done with load L_before_last + t_last = T, and this must satisfy ≤ c_last. So for the last island, we need L_before_last ≤ c_last - t_last? Actually we need T ≤ c_last, i.e., total sum ≤ c_last. But wait: For the last island, outward crossing also requires L_before_last ≤ c_last, but that's automatically satisfied if T ≤ c_last? Not necessarily because L_before_last = T - t_last. So outward condition: T - t_last ≤ c_last, which is weaker than T ≤ c_last if t_last >0. So the last island's return condition is T ≤ c_last. So we must have that for the island visited last, its capacity must be at least total sum.
But is that consistent with L_before_last ≤ c_last - t_last? L_before_last = total - t_last. Condition L_before_last ≤ c_last - t_last is equivalent to total - t_last ≤ c_last - t_last → total ≤ c_last. Yes, same.
Thus the condition for each island i visited at step k is: current load before visiting (sum of treasures of previously visited islands) ≤ c_i - t_i? For all islands except maybe the last? Actually we derived that the return crossing gives L_before + t_i ≤ c_i, i.e., L_before ≤ c_i - t_i. This holds for any island, including the last. Because after picking up you must return. So for every island, we need L_before ≤ c_i - t_i. So indeed for every visit, the load before visiting must not exceed c_i - t_i.
But wait: Is it necessary to return immediately after picking up? Could he possibly not return immediately? He could stay on the island? But eventually he must return to start. However, there is no advantage to staying because he cannot go to another island without crossing the bridge back to start. So he must cross back at some point. Could he cross back later? But he cannot cross to another island directly, so he must go back to start anyway. So the return crossing will occur at some point after picking up the treasure, but maybe he could cross back carrying the same load L_before + t_i. There is no way to reduce load (cannot deposit treasure anywhere else). So the crossing back will be with that load. So condition holds.
Thus the problem reduces to: Given items each with (t_i, c_i), can we order them such that for each prefix sum S_{k-1} (sum of t of first k-1 items) ≤ c_{i_k} - t_{i_k} for each k? Additionally, we must also ensure that the total sum T satisfies something? Actually for the last item, condition is S_{n-1} ≤ c_{i_n} - t_{i_n}. That's already included. There's no further condition.
But is there any other nuance? The start island has no treasure, but maybe the capacity also applies when he leaves start? The bridge is old, and crossing from start to island also must satisfy capacity. For the first island, L_before = 0, so condition 0 ≤ c_i - t_i => t_i ≤ c_i. That is, the capacity must be at least the treasure amount? Actually if c_i - t_i ≥ 0, i.e., t_i ≤ c_i. For the first island, we need t_i ≤ c_i. Otherwise you couldn't return because after picking up you exceed capacity. So for each island, we need t_i ≤ c_i? Possibly not all islands, because if we visit an island later, we have load L_before > 0, so we need c_i - t_i ≥ L_before, which may be stricter. So some islands with t_i > c_i are impossible to ever visit because even with zero load you would exceed capacity on return. So we can eliminate any island where t_i > c_i, because then even if you go there first, you'd have load 0 outward, but after picking up t_i, you'd have t_i > c_i, so you can't return. So such islands make the answer "No". The problem might have such cases.
Now, the condition is: Find an ordering of items such that for each item i placed at position k, prefix sum before it (S) ≤ c_i - t_i. This is reminiscent of scheduling with constraints, like the classic "collecting treasures with weight limits" problem, which often reduces to sorting by something? Consider that if we define d_i = c_i - t_i (the "margin" after taking the treasure). Then condition is that S ≤ d_i. The initial total load before first is 0, so we need d_i ≥ 0 for first. After picking up, S becomes S + t_i. So essentially we have a process: We start with S=0. For each selected island i, we require S ≤ d_i, then S += t_i. Need to do all islands.
This is similar to the problem of "can we do all tasks" where each task has a requirement of available resource and consumes something. It's like you have a resource limit that changes? Actually it's like you have a maximum load capacity for each bridge crossing, but the condition only applies before picking up the treasure. This is reminiscent of the classic "picking items with constraints" that appears in many problems: each item has a requirement (like minimal required fuel) and gives fuel (like treasure). But here it's different: S must be ≤ d_i (the slack) before taking, and then S increases by t_i. Actually d_i = c_i - t_i is like the maximum additional load you can have before crossing? If we think of the bridge's capacity is c_i, and you have S before, you need S + t_i ≤ c_i → S ≤ d_i. So it's like each island i has a "limit" d_i that must be at least current load. After visiting, load increases by t_i.
Thus we need to order the islands to satisfy S ≤ d_i before each. This is analogous to scheduling tasks where each task has a "deadline" but not exactly.
We can think of it as: We have items with two parameters: t_i (positive) and d_i = c_i - t_i (which could be negative, but then impossible). We need to check if we can sequence them so that cumulative sum never exceeds the d_i of the next item. This is like the problem of "Can we rearrange boxes so that each box's weight is supported?" But more precisely, we need to find if there exists a permutation such that for all k, sum_{j<k} t_{i_j} ≤ d_{i_k}. Since the d_i can be different, we need to check feasibility.
This is similar to checking if we can sort items in nondecreasing order of d_i? Let's test. Suppose we sort by d_i ascending. Then the most restrictive (smallest d) is first. Since S starts at 0, we need d >= 0, so the smallest d must be >= 0? Actually if any d_i < 0, impossible. If we sort ascending, we try to satisfy S ≤ d_i. As S increases, later items need larger d to accommodate. So it seems we should process islands in increasing order of d_i. Because if we have a small d, we want to do it early when S is small. This is a known greedy: for tasks that have a requirement that the current sum S must be ≤ some threshold, the optimal order is by threshold ascending (like scheduling with deadlines, but here it's a resource constraint). More formally, if we have items with (t_i, d_i) and need to find an order such that for each item, prior sum ≤ d_i, then a necessary and sufficient condition for existence is that after sorting by d_i ascending, we have for each prefix, sum_{j=1..k} t_{i_j} ≤ d_{i_k}? Actually the condition for the sorted order is: for each k, sum_{j=1..k-1} t_{i_j} ≤ d_{i_k}? Because at the moment before processing item k, the sum S_{k-1} is sum of first k-1 items. The condition for item k is S_{k-1} ≤ d_{i_k}. But after processing k, S_k = S_{k-1} + t_{i_k}. So we need to check that for each k, S_{k-1} ≤ d_{i_k}. This is not exactly that the prefix sum after including k is ≤ d_i; it's before. However, we can check that after sorting by d_i ascending, if there exists any order, then this sorted order works? Let's consider a classic problem: There are tasks that require a certain amount of resource to start and then consume some resource? Actually there is a known problem: "Can you complete all levels where each level requires a certain amount of energy to enter and gives some energy?" That is similar but with inequalities reversed: you need to have at least some amount to enter, and then you gain or lose energy. That's like the "stamina" problem. For that, the optimal order is to sort by the requirement? Actually for the "energy" problem, you have tasks that require a minimum energy to start, and after completing you gain some energy (could be negative). The classic solution is to process tasks with net positive gain first? But here it's different: We need the current sum (which is total collected treasures) to be ≤ d_i. So d_i is like a ceiling on the current load. As we collect more, the load increases, making it harder to satisfy ceilings. So we want to do tasks with low ceilings early.
Is it always optimal to sort by d_i ascending? Let's test simple cases. Suppose we have two items: A: t=5, d=5 (c=10? d=c-t => if t=5, d=5). B: t=6, d=6. Sorted by d: A then B. Check: start S=0 ≤ d_A=5, okay; then S becomes 5; before B, S=5 ≤ d_B=6, okay. So works. Reverse: B then A: S=0 ≤ d_B=6 ok; S=6; before A, S=6 ≤ d_A=5 fails. So sorted ascending works.
Suppose A: t=10, d=10; B: t=1, d=1. Sorted ascending: B (d=1) then A (d=10). Check: S=0 ≤ 1 ok; S=1; before A: 1 ≤ 10 ok. Works. Reverse: A then B: S=0 ≤10 ok; S=10; before B: 10 ≤1 fails. So ascending works.
Now consider a more complex case: A: t=3, d=3; B: t=4, d=4; C: t=5, d=5. Sorted ascending works. But if we have A: t=3, d=10; B: t=4, d=4. Here d_B=4 < d_A=10. Sorted ascending: B then A. Check: S=0 ≤4 ok; S=4; before A: 4 ≤10 ok. Works. Reverse: A then B: S=0 ≤10 ok; S=3; before B: 3 ≤4 ok also works? Actually check: A then B: After A, S=3. Before B, S=3 ≤ d_B=4, okay. So both orders work. So ascending does not break.
But is there any case where ascending order fails but some other order works? Let's try to find a counterexample. We need items where some item with larger d also has large t, and if we do it later it may cause violation. Suppose A: t=100, d=100; B: t=1, d=1; C: t=50, d=60. Sorted ascending: B (d=1) then C (d=60) then A (d=100). Check: after B: S=1; before C: 1 ≤60 ok; after C: S=51; before A: 51 ≤100 ok. Works. What about another order: C, B, A? Start: S=0 ≤60 ok; S=50; before B: 50 ≤1? fails. So ascending works.
Consider A: t=2, d=2; B: t=3, d=5; C: t=4, d=6. Sorted ascending: A (2), B (5), C (6). Check: after A: S=2; before B: 2 ≤5 ok; after B: S=5; before C: 5 ≤6 ok. Works.
Now try to find a scenario where sorted ascending fails. Suppose A: t=5, d=5; B: t=6, d=7; C: t=4, d=6. Sorted ascending by d: A (5), C (6), B (7). Check: after A: S=5; before C: 5 ≤6 ok; after C: S=9; before B: 9 ≤7 fails. So sorted ascending fails. Is there another order that works? Let's test all permutations.
Items:
A: t=5, d=5
B: t=6, d=7
C: t=4, d=6
Total sum T = 15.
We need an ordering such that before each, S ≤ d.
Try B first: S=0 ≤7 ok; after B: S=6.
Then A: before A, S=6 ≤ d_A=5? fails.
Then C: before C, S=6 ≤ d_C=6? actually equals, okay, but after C: S=10; but then need to do A after? Let's try B, C, A: after B:6; before C:6 ≤6 ok; after C:10; before A:10 ≤5 fails.
Try C first: S=0 ≤6 ok; after C:4; before A:4 ≤5 ok; after A:9; before B:9 ≤7 fails.
C, B, A: after C:4; before B:4 ≤7 ok; after B:10; before A:10 ≤5 fails.
A, B, C: after A:5; before B:5 ≤7 ok; after B:11; before C:11 ≤6 fails.
A, C, B: after A:5; before C:5 ≤6 ok; after C:9; before B:9 ≤7 fails.
All fail? Let's check A, C, B we already did: fails at B. So all permutations fail? Let's double-check B, A, C? B first:6; before A:6≤5 fails. So indeed all fail. So sorted ascending's failure indicates impossibility, which matches.
Now find a case where sorted ascending fails but some other order works? Possibly if there are ties or different structure. Let's try to construct a scenario. We need that after some steps, the sum is too high for the next item in ascending order, but by taking a different order we could keep sums lower? Actually ascending order puts smallest d first, which is good because it requires low S. But if we have an item with a moderately large d but very large t, doing it early might increase S a lot, making later small-d items impossible. So ascending order seems to greedily minimize S early. But is it always optimal? We need to prove or find counterexample.
This is analogous to scheduling with constraints: we have to process all items, each with a "maximum permissible prior sum" d_i. This is similar to the problem of "arranging numbers to satisfy a_i <= something"? Let's think: define for each item i, the condition S_{before} <= d_i. After processing, S increases by t_i. The question: existence of permutation. This is like we have a set of tasks with consumption t_i and a requirement that the resource level before starting is <= d_i. Starting with resource 0, the resource increases by t_i after each task (i.e., it's like we are accumulating load). Equivalent to we need to order such that the cumulative sum before each step does not exceed some threshold. This can be transformed: Let prefix sums be S_0=0, S_k = sum_{j=1..k} t_{i_j}. Condition: S_{k-1} <= d_{i_k}. So S_{k-1} must be <= d_{i_k}. Since S_{k} = S_{k-1}+t_{i_k}, we also need S_{k} <= d_{i_k}+t_{i_k} = c_i? But we don't need that.
One typical approach: treat d_i as "deadline" for the sum before i? Not exactly.
Alternatively, we can view the process as: we need to assign each item to a position such that the sum of t's of items before it is <= d_i. This is like we want to order by d_i ascending, because items with smaller d_i should appear earlier (when cumulative sum is small). This is reminiscent of the concept of "feasible sequence" where you have jobs with processing times and release times? Actually it's like we have constraints that the start time (which is cumulative sum of previous t's) must be <= d_i. Since start times are exactly the sum of previous t's, we are free to order. So we need to find an ordering such that for each job i, the sum of processing times of jobs scheduled before i is <= d_i. This is exactly the problem of scheduling jobs with given "deadlines" where the start time must be <= deadline, but here "deadline" is on start time, not completion time. This is a known problem: given jobs with processing times t_i and "start deadlines" d_i, is there a sequence? This can be solved by sorting by d_i ascending and checking that for each k, sum_{j=1..k} t_{i_j} <= d_{i_k}? Wait: In such a schedule, the start time of job i_k is S_{k-1}. The condition is S_{k-1} <= d_{i_k}. For the sorted order by d_i ascending, we have d_{i_1} <= d_{i_2} <= ... <= d_{i_n}. The condition for job i_1: S_0 = 0 <= d_{i_1}. So we need d_{i_1} >= 0. For job i_2: S_1 = t_{i_1} <= d_{i_2}. Since d_{i_2} >= d_{i_1}, it's not automatically satisfied; we need t_{i_1} <= d_{i_2}. For job i_3: S_2 = t_{i_1}+t_{i_2} <= d_{i_3}. So the condition is that for each k, the sum of the first k-1 t's is <= d_{i_k}. Since d_i are sorted ascending, the most restrictive condition for the sum of first k-1 is at the largest k? Actually the sum increases, and d_i also increases. The condition might be hardest at the largest k? Possibly.
But is it sufficient to check the sorted order? Suppose we have a feasible ordering. Can we transform it into sorted order without breaking feasibility? This is like exchange argument: If there exist two adjacent jobs i, j where d_i > d_j but i is before j, swapping them might not violate constraints? Let's examine: Suppose we have a sequence ... i, j ... where i before j, and d_i > d_j. Let S be the sum before i. Then we have constraints: S <= d_i, and S + t_i <= d_j. After swapping to j, i, we need to check: before j (now at position of i), S <= d_j must hold (since before j, sum is S). Since original had S <= d_i, but d_i > d_j, S <= d_i doesn't guarantee S <= d_j. So swapping might violate. Also after swapping, we need to ensure that after j, before i, the sum is S + t_j <= d_i. Original had S + t_i <= d_j, but that doesn't give info about S + t_j <= d_i. So swapping could break. So sorted ascending may not be derivable from any feasible sequence.
But maybe the condition is not just d_i; perhaps we need to consider both t_i and c_i in a different way. There is a known problem: "Collecting treasures on islands with bridges" might be similar to a classic problem known as "Treasure Island" or "Treasure Hunter" where you need to decide order based on something like "c_i" vs "c_j"? Wait, there is a known problem: "Treasure Hunt" from some contest. I recall a problem: There is a start point and n islands, each with treasure a_i and capacity b_i (the maximum load you can carry when crossing the bridge to that island). You need to collect all and return to start. The solution is to check if there exists an order such that for each island, the total weight of treasures you have before crossing to that island is ≤ b_i - a_i. And the greedy is to sort islands by b_i? Or by a_i? Let's derive more carefully.
We have constraints: For each island i, when you go to it (with load L), you need L ≤ b_i (on outward) and L + a_i ≤ b_i (on return) → L ≤ b_i - a_i. So the key is L ≤ (b_i - a_i). So it's exactly as we set d_i = b_i - a_i. So condition: L ≤ d_i.
Thus the problem reduces to: given t_i and d_i, can we sequence?
This is reminiscent of the "balanced sequence" problem where you need to sort by something like (t_i + d_i)? Actually think about it as: For each island, we have two numbers: the "required free space" before visiting? Or think of it as the bridge can hold up to c_i. When we go, we carry L. So we need L ≤ c_i - t_i. So the island "tolerates" a load up to (c_i - t_i). After collecting, we have L + t_i. For feasibility, we need to order such that the cumulative load never exceeds the tolerance of the next island.
Now, note that if we consider the reverse order (starting from the end), maybe it's easier: At the end, after collecting all, we need to return from the last island, which imposes total sum T ≤ c_last. But that's equivalent to d_last + t_last = c_last, so T ≤ c_last => T - t_last ≤ d_last. Actually we already have condition for last: before last, L_{n-1} ≤ d_last. And L_{n-1} = T - t_last. So it's the same.
Now consider the process in reverse: Suppose we decide an order to leave the start island and come back. Alternatively, think of it as we need to cross each bridge twice. There is also a known concept: "You have to bring items across a bridge with capacity" but here it's star.
This is similar to the problem of "Can we carry all items across a river with a boat that can carry a certain weight?" But that is different.
Maybe we can treat this as a kind of sorting problem with constraints reminiscent of the "Canoeist" problem? Or like "Collecting coins with weight limits" from AtCoder? Let's search memory: There is a problem from JOI or similar: "Treasures" with n ≤ 25, that suggests maybe we need to use DP over subsets (bitmask) because n=25 is small enough for 2^25 = 33 million, which might be borderline but with optimization might be okay if there are multiple datasets? But typical time limits, 2^25 DP might be a bit high but possibly okay if we use bitset or prune? But 2^25 = 33,554,432 states. For each state we need to consider adding one island. That's about 25 * 2^25 = ~800 million operations, which might be too slow in Python. But maybe we can use meet-in-the-middle or other techniques? However, n ≤ 25 suggests that perhaps the intended solution is DP with bitmask, as 2^25 is about 33 million, which might be borderline but could be done in C++ with optimization. In Python, 33 million states is likely too slow, especially for multiple datasets. But maybe the number of datasets is small? However, the problem statement says "Input consists of several datasets." Could be many. So we need a more efficient approach.
But perhaps there is a polynomial-time solution, like sorting by some criterion. Let's explore further.
We have d_i = c_i - t_i. Condition: There exists order such that prefix sums ≤ d_i for each step.
This is similar to scheduling with "start time constraints". This is known to be solvable by sorting by d_i ascending if the tasks are "preemptive"? But let's try to think: For any two islands i and j, if we compare them, what condition ensures that i should come before j in an optimal order? Consider having current load L before visiting either. If we visit i then j, the conditions are: L ≤ d_i, then after i, L' = L + t_i, then need L' ≤ d_j. If we visit j then i, conditions: L ≤ d_j, then L + t_j ≤ d_i. So for a given L, which ordering is possible? For both orders to be possible, we need both sets of inequalities. If we want an ordering that works for all possible L (but L is determined by earlier islands), it's not straightforward.
However, perhaps we can derive a necessary and sufficient condition based on sorting by d_i? Let's test more examples.
Let's try to find a counterexample where sorting by d_i ascending fails but some other order works. To construct, we need that an item with small d also has large t, and an item with larger d but small t. Sorted ascending will place the small d first, which may increase S too much for the next small d? But since it's small d, it's early. Actually consider:
Item A: t=10, d=10 (small d but large t)
Item B: t=1, d=100 (large d, small t)
Sorted ascending: A then B. Check: S0=0 ≤10 ok; after A S=10; before B: 10 ≤100 ok. Works. So ascending works.
Now suppose we have items where there is a medium d with medium t, and a small d with small t, but the small d's t is huge? Wait, small d with huge t is impossible because d = c - t, if t is huge then d might be negative unless c is larger. So small d can be associated with either small t or large t? Since d = c - t, if t is large, c must be larger to keep d small? Actually d small means c is just a bit larger than t. So it could be large t with c just a bit larger, so d small. So item with large t and small d is possible. That item, if done early, will increase S a lot, but its own d is small, so it requires that S before it is small (which is fine if it's first). Then after it, S becomes large, which might break later items with moderate d. So maybe we need to postpone that large t item to later when S is already large? But if we postpone it, before it S is large, but its d is small, requiring S before it to be ≤ small, which would be impossible if S is large. So a large t item with small d must be done early (when S is small). So it's forced to be early. So sorted ascending correctly places it early.
Now consider an item with moderate d but huge t, and another with small d but tiny t. The tiny t with small d can be done early without increasing S much, but its small d is okay early. The huge t with moderate d would then be done later; but before it, S will be sum of previous items (including tiny t), which is small, so that's okay as long as S ≤ moderate d, which is easy. So both orders might work. So it seems sorted ascending might always be a valid order if any order exists.
But is it always? Let's try to construct a tricky case: Items:
A: t=5, d=5 (d small)
B: t=5, d=5 (same)
C: t=6, d=6 (slightly larger d, but larger t)
Sorted ascending: A, B, C (or any order among equal). Check: after A:5; before B:5 ≤5 ok; after B:10; before C:10 ≤6 fails. So ascending fails. But maybe another order works? Try A, C, B: after A:5; before C:5 ≤6 ok; after C:11; before B:11 ≤5 fails. Try C, A, B: after C:6; before A:6 ≤5 fails. So no order works? Let's test all combos: B, C, A: after B:5; before C:5 ≤6 ok; after C:11; before A:11 ≤5 fails. C, B, A: after C:6; before B:6 ≤5 fails. B, A, C: after B:5; before A:5 ≤5 ok; after A:10; before C:10 ≤6 fails. So all fail. So ascending's failure indicates impossibility.
But need a case where ascending fails but some other order works. Let's try to systematically search small n with brute force to see if any such instance exists. Since n is small, we can try to enumerate possibilities with small t and d values. But let's reason mathematically.
We have set of items (t_i, d_i). We need a permutation π such that for all k: sum_{j<k} t_{π(j)} ≤ d_{π(k)}. This is exactly the condition for a feasible sequence. This is known as a "schedule with start-time deadlines" where each job has a deadline for its start time. This problem is known to be solvable by sorting by deadlines (d_i) ascending. Actually, I recall a known scheduling problem: We have jobs with processing times p_i and deadlines d_i (meaning the job must start by time d_i). The question: Is there a sequence to schedule all jobs (non-preemptive) on a single machine such that each job starts by its deadline? Since the start time of job i is the sum of processing times of jobs before it. This is exactly our condition: start time S_i ≤ d_i. The problem is known to be NP-hard? Let's think: This is similar to "1|r_i|Lmax" but with start deadlines? Wait, classic scheduling: Jobs have release times and deadlines. Here we have only deadlines for start times, not completion times. The problem of scheduling jobs with start deadlines on a single machine, where all jobs are available at time 0, and we need to schedule them non-preemptively, and each job i must start by its deadline d_i. Is this polynomially solvable? I think it's equivalent to scheduling with deadlines on completion times? Not exactly.
Consider transformation: Let completion time C_i = S_i + t_i. The condition S_i ≤ d_i. So C_i ≤ d_i + t_i. So we have deadlines on completion times: each job i has a deadline D_i = d_i + t_i. This is exactly the classic "single machine scheduling to meet deadlines" problem where each job has processing time t_i and deadline D_i, and we need to find a sequence such that each job completes by its deadline. However, note: In classic scheduling with deadlines (the Moore-Hodgson algorithm for minimizing number of late jobs, or feasibility of meeting all deadlines) it's known that the necessary and sufficient condition for existence of a schedule where all jobs meet their deadlines (with all jobs available at time 0) is that when jobs are sorted by deadline (Earliest Deadline First), the completion times all satisfy C_i ≤ D_i. That is, if you sort by deadlines non-decreasing and check that for each k, sum_{j=1..k} t_{π(j)} ≤ D_{π(k)}. This is a standard result: The schedule that orders jobs by increasing deadlines (EDD) is optimal for feasibility of meeting all deadlines (if any schedule exists, EDD works). Actually, it's known that for the problem of minimizing maximum lateness, EDD is optimal. But for feasibility of meeting all deadlines (i.e., all jobs on time), EDD gives a schedule that meets deadlines if and only if some schedule exists? I think yes: If there exists a schedule where all jobs meet their deadlines, then the EDD schedule will also meet all deadlines. The proof: The EDD schedule minimizes maximum lateness, so if the maximum lateness ≤ 0, then EDD also has ≤ 0. So EDD is optimal for meeting deadlines. So indeed, sorting by deadlines (D_i) works. In our case, D_i = d_i + t_i = c_i. So deadlines are c_i! Let's see: Because condition S_i ≤ d_i is equivalent to C_i = S_i + t_i ≤ d_i + t_i = c_i. So each island must be completed (i.e., returned to start after collecting treasure?) Actually careful: The moment we finish visiting an island? When do we consider "completion"? In our process, we go to island, pick up treasure, and return to start. The return crossing happens immediately after picking up. So after we finish the visit to island i, we are back at start, and the total load is now increased by t_i. The time (or total weight) when we finish the round trip is after we cross back. But the condition for crossing back is that at the moment of crossing, we have load S_{i-1} + t_i, and that must be ≤ c_i. That is exactly S_{i-1} + t_i ≤ c_i. But note that S_{i-1} is the load before going to island i. So the condition S_{i-1} + t_i ≤ c_i is equivalent to C_i? If we think of the cumulative sum after processing i as S_i = S_{i-1} + t_i. The condition for island i is S_i ≤ c_i. But also we have S_{i-1} ≤ c_i - t_i = d_i. So both are constraints: S_{i-1} ≤ d_i and S_i ≤ c_i. However, S_i = S_{i-1} + t_i. Since we always have S_{i-1} ≥ 0, S_i ≤ c_i is implied by S_{i-1} ≤ d_i? Actually d_i = c_i - t_i, so S_{i-1} ≤ d_i implies S_i = S_{i-1}+t_i ≤ c_i. So it's equivalent.
Now consider the ordering condition: For each i in order, after processing up to i, the total sum S_i must not exceed the capacity c_i of that island? But careful: The condition S_i ≤ c_i is required for the return crossing of island i. However, after we have processed island i, we are back at start. For subsequent islands, the load is S_i. There's no condition linking S_i to future islands' capacities except through the outward crossing (S_i ≤ d_j for next island j). So the deadlines interpretation: When we finish a job (island i), we must have finished by time? If we think of the total load as "time", then each island i has a deadline c_i by which its processing must be completed? Actually the constraint S_i ≤ c_i must hold immediately after processing i. In classic scheduling, job i has processing time t_i, and it must complete by its deadline D_i. If we schedule jobs sequentially, start at time 0, after finishing job i, time = sum of processing times of jobs done so far. The condition that job i completes by its deadline is exactly sum_{j up to i} t_j ≤ D_i (assuming jobs scheduled in some order). That matches: if we define completion time C_i = total processing time up to and including job i, then we need C_i ≤ D_i. And in classic scheduling, if all jobs are available at time 0, a schedule exists if and only if when jobs are sorted by deadlines (nondecreasing), we have for each k, sum_{j=1..k} t_{π(j)} ≤ D_{π(k)}. And indeed EDD (Earliest Deadline First) yields a feasible schedule if any exists. So our problem reduces to: Can we order islands such that for each island i, the total weight after collecting it (i.e., the cumulative sum up to that island) does not exceed c_i? And we also have the outward condition which is implied? Actually we need both S_{i-1} ≤ c_i - t_i and S_i ≤ c_i. The latter is exactly the completion deadline condition: S_i ≤ c_i. The former is stricter? But if S_i ≤ c_i, does it guarantee S_{i-1} ≤ c_i - t_i? Not necessarily: S_i ≤ c_i does not guarantee S_{i-1} ≤ c_i - t_i because S_{i-1} could be > c_i - t_i while still S_i = S_{i-1}+t_i ≤ c_i? If S_{i-1} > c_i - t_i, then S_i = S_{i-1}+t_i > c_i, contradiction. Actually S_i ≤ c_i and S_{i-1} > c_i - t_i implies S_{i-1}+t_i > c_i, so S_i > c_i, contradiction. So S_i ≤ c_i implies S_{i-1} ≤ c_i - t_i. So the two conditions are equivalent: S_i ≤ c_i implies S_{i-1} ≤ c_i - t_i. Because S_{i-1} = S_i - t_i. If S_i ≤ c_i, then S_{i-1} = S_i - t_i ≤ c_i - t_i. Conversely, if S_{i-1} ≤ c_i - t_i, then S_i ≤ c_i. So they are equivalent. Therefore the only necessary condition for each island i (in the order) is that after we finish its round trip, the total load (cumulative sum) must be ≤ c_i. This is a simpler condition: In the sequence, for each position k, the sum of treasures of the first k islands (the ones visited so far) must be ≤ the capacity c_{i_k} of the island that was visited at step k.
But careful: The capacity c_i is the maximal load he can take when crossing the bridge to island i. But after returning, he is back at start. The condition for return crossing is that the load during return (which is S_i) must be ≤ c_i. So yes, after finishing island i, he has load S_i and has just crossed the bridge back; that crossing must be safe. So the condition is exactly S_i ≤ c_i for each i in the order. However, note: For the first island, after visiting it, we have S_1 = t_1, and we need t_1 ≤ c_1. That's same as t_1 ≤ c_1. For the second, S_2 = t_1 + t_2 ≤ c_2, etc.
Thus the problem reduces to: Given n items with weight t_i and capacity c_i (where c_i is the maximum total load allowed after collecting that island), can we order them so that for each prefix, the total weight is at most the capacity of the last collected island? Wait: The condition applies to each island individually: after finishing island i, the total weight S after that step must be ≤ c_i. This condition must hold for each island i in the sequence, but note that it's not a global condition that all prefixes must be ≤ some function; it's that the cumulative sum after step k (which is sum of the first k items) must be ≤ the capacity of the k-th island (the one visited at that step). So if we reorder islands, the capacity associated with the k-th position is the capacity of the island placed at that position.
Thus we need to find an ordering π such that for all k = 1..n, ∑{j=1..k} t{π(j)} ≤ c_{π(k)}.
This is exactly the problem of scheduling with deadlines where each job has processing time t_i and deadline D_i = c_i, and we need that each job completes by its deadline. Because if we process jobs in order π, completion time of job π(k) is C_{π(k)} = ∑{j=1..k} t{π(j)}. The requirement C_{π(k)} ≤ D_{π(k)}. That's a classic scheduling problem: given jobs with processing times and deadlines, can we schedule all jobs to meet deadlines? This is a well-known NP-hard problem when we consider that jobs have different release times, but when all release times are zero, it's actually solvable by sorting by deadlines? Wait, the problem of meeting all deadlines on a single machine with all jobs available at time 0: The Earliest Deadline First (EDF) order, i.e., sort jobs by non-decreasing deadlines, and then check if each job's completion time ≤ its deadline. If that schedule fails, then no schedule exists. Is that true? Let's recall: For a single machine, if all jobs are available at time 0, then the schedule that orders jobs by increasing deadlines (EDD) minimizes the maximum lateness. So if there exists a schedule where all jobs meet their deadlines (i.e., maximum lateness ≤ 0), then the EDD schedule will also have maximum lateness ≤ 0. Therefore, checking the EDD order is sufficient. Indeed, it's a standard result: The problem of deciding feasibility of meeting all deadlines is polynomial; you can sort by deadlines and check. However, is that always true? I recall that for unit release times, EDD works. Actually, consider the following: Job A: t=5, deadline=10; Job B: t=6, deadline=6. Sorted by deadline: B then A. Completion times: B finishes at 6 (meets 6), A finishes at 11 (exceeds 10). So schedule fails. Is there any other schedule? A then B: A finishes at 5 (≤10), B finishes at 11 (≤6? 11 > 6). So both fail. So EDD correctly identifies infeasibility.
Now consider job A: t=5, deadline=10; Job B: t=6, deadline=9. Sorted: B then A gives B finishes at 6 (≤9), A finishes at 11 (>10) fails. Try A then B: A finishes at 5 (≤10), B finishes at 11 (>9) fails. So infeasible.
Now consider job A: t=5, deadline=7; Job B: t=3, deadline=6. Sorted by deadline: B (deadline 6) then A (deadline 7). B finishes at 3 (≤6), A finishes at 8 (>7) fails. Try A then B: A finishes at 5 (≤7), B finishes at 8 (>6) fails. So infeasible.
Now consider job A: t=5, deadline=5; Job B: t=3, deadline=8. Sorted: A then B: A finishes at 5 (≤5), B finishes at 8 (≤8) works. So EDD works.
Thus it seems EDD is indeed optimal for meeting all deadlines. The proof is standard: if a schedule exists, then the EDD schedule (obtained by swapping any inversion where a job with later deadline is before an earlier deadline) will not increase lateness. More precisely, swapping adjacent jobs where the first has a later deadline than the second cannot increase maximum lateness, and if there is a feasible schedule, after swapping all such inversions we get EDD which is also feasible. So yes, it's a necessary and sufficient condition to check the sorted order by deadlines. So the problem reduces to: sort islands by c_i (capacity) ascending, and then check that for each k, sum_{j=1..k} t_{π(j)} ≤ c_{π(k)}. If this holds, answer Yes; else No.
But wait, is there any nuance about the outward crossing condition? We've argued that S_i ≤ c_i implies S_{i-1} ≤ c_i - t_i, so the outward condition is automatically satisfied if the return condition holds. However, we must also consider that the outward crossing also requires that the load before crossing is ≤ c_i (not just c_i - t_i). But S_{i-1} ≤ c_i is automatically true if S_{i-1} ≤ c_i - t_i? Not necessarily: if c_i - t_i < c_i, then S_{i-1} ≤ c_i - t_i implies S_{i-1} ≤ c_i, so outward condition is satisfied. So indeed both are covered by S_i ≤ c_i. So the condition S_i ≤ c_i is sufficient.
Thus the problem reduces to a simple greedy: sort by c_i ascending, and check prefix sums.
But is that all? Let's test with the given examples.
Example 1:
n=3
2 3 => t=2, c=3, d=1
3 6 => t=3, c=6, d=3
1 2 => t=1, c=2, d=1
Sort by c: c values: 2,3,6. Let's order by ascending c: island3 (1,2), island1 (2,3), island2 (3,6).
Prefix sums: after island3: sum=1 ≤ c=2 OK; after island1: sum=1+2=3 ≤ c=3 OK; after island2: sum=3+3=6 ≤ c=6 OK. So Yes.
Example 2:
n=3
2 3 => t=2,c=3,d=1
3 5 => t=3,c=5,d=2
1 2 => t=1,c=2,d=1
Sort by c: island3 (1,2), island1 (2,3), island2 (3,5). Prefix: after island3 sum=1 ≤2 OK; after island1 sum=3 ≤3 OK; after island2 sum=6 ≤5? 6 >5 => fails. So No. matches.
Thus the example matches our greedy.
Now, is there any hidden catch? Could there be a scenario where sorting by c_i works but the answer should be No? Or where sorting by c_i fails but some other order works? According to the scheduling theory, if a feasible schedule exists, then the EDD schedule (by deadline = c_i) is feasible. So sorting by c_i ascending should work. But we must confirm that deadlines are indeed c_i (the capacity). In scheduling terms, we need each job to be completed by its deadline. However, in our problem, the "completion" of a job (visiting island) happens after we return to start with the treasure. At that moment, we have collected that treasure, and we need to have the total load ≤ c_i. That's exactly completion time ≤ deadline. So yes.
Thus the problem seems solved by sorting by c_i (capacity) and checking prefix sums. But is there any nuance about the start? The start island has no bridge capacity? Actually, we start at start island with load 0. There's no constraint on leaving start initially? We just go to an island; the outward crossing requires that the load at that time (0) ≤ c_i. That is automatically satisfied if c_i ≥ 0, which is true as capacities are positive? But what if c_i < 0? Not possible since c_i is maximal amount he can take, presumably non-negative? Input could be zero? It says amount of treasures (positive integer) and maximal amount (non-negative?). Probably both are positive integers. But if c_i = 0 and t_i >0, then impossible because after picking up t_i >0, you have >0. But such case would be caught by prefix condition: if c_i < t_i, then even if visited first, sum after = t_i > c_i, fails. So the greedy will catch that.
But wait, the start island: The bridge to each island is only from start to that island. There is no direct connection between islands. So the only way to collect treasures is round trips. So our model is correct.
Thus answer seems straightforward.
But why n ≤ 25? Possibly to mislead? Or maybe there is more complexity: There is also the possibility that the thief can decide not to return to start after each island? But there is no direct bridge between islands, so must return. However, could he go from island A to start and then immediately to island B? That's what we considered (each island visited via a round trip). So yes.
But wait, is there any possibility to do multiple islands in one trip? Like go start -> A -> start -> B is separate trips, but he always returns to start. There's no way to go A -> B directly because no bridge. So our model is correct.
Thus problem reduces to check if there exists a permutation such that cumulative sums ≤ c_i for each in order, which is exactly the EDD check.
Now, is there any scenario where the outward crossing condition could be tighter than the return crossing? For an island i, outward requires L ≤ c_i, return requires L + t_i ≤ c_i. So return is tighter if t_i > 0. So S_i ≤ c_i is enough.
But what about the final state? After finishing all islands, he must be back at start (which he is after each round trip). There's no further condition. So done.
Thus solution is: For each dataset, read n, then arrays t_i, c_i. Sort by c_i ascending. Compute running sum s = 0. For each island in sorted order, s += t_i. If at any point s