We are given n eggs, each egg i has two prices: ai (price by A) and gi (price by G), with ai+gi = 1000. Uncle J wants to assign each egg to exactly one child (A or G). For assignment, he pays the child the price they named for that egg. So total paid to A: Sa = sum over eggs assigned to A of ai; total to G: Sg = sum over eggs assigned to G of gi. We need |Sa - Sg| <= 500. Output a string of length n with 'A' or 'G' indicating assignment, if possible; else -1.
We need to find a selection (choose which eggs go to A) such that Sa - Sg is within [-500,500]. Since ai+gi=1000, note that if we assign egg to A, contribution to Sa - Sg is +ai (since Sa increases by ai, Sg unchanged). If assign to G, Sa unchanged, Sg increases by gi, so contribution to Sa - Sg is -gi. Alternatively, we can think of Sa - Sg = sum_i (xi*ai - (1-xi)*gi?) Actually if we assign to A, contribution = ai; to G, contribution = -gi. So overall difference D = sum_i (if assigned to A then ai else -gi). We need -500 <= D <= 500.
Also note that since ai+gi=1000, we have gi = 1000 - ai. So for each egg, if we assign to A, D contribution = ai; if to G, D contribution = -(1000-ai) = ai - 1000? Actually -gi = - (1000-ai) = ai - 1000. So two possible contributions: ai or ai-1000. That's interesting: each egg's contribution to D is either ai (if A) or ai-1000 (if G). So D = sum_i (ai - 1000 * y_i) where y_i = 0 if A, 1 if G? Let's derive: if assigned to G, contribution = -gi = - (1000-ai) = ai - 1000. If assigned to A, contribution = ai. So D = sum_i (ai - 1000 * t_i) where t_i = 0 for A, 1 for G. Yes because for A: ai - 0 = ai; for G: ai - 1000 = ai-1000. So D = sum_i ai - 1000 * (number of eggs assigned to G). Let T = total number of eggs assigned to G. Then D = S_a_total - 1000T? Wait sum_i ai is sum of all ai across all eggs, which is a constant A_total = Σ ai. So D = A_total - 1000 * T. Since T = number of eggs assigned to G. Also note that number assigned to A is n - T. Then Sa = sum of ai for A-assigned =? Actually Sa = sum over i assigned to A of ai. That's not the same as A_total - sum of ai for G-assigned. But D = Sa - Sg. We derived D = Σ ai - 1000 T. Let's verify:
Sa = sum_{i in A} ai, Sg = sum_{i in G} gi = sum_{i in G} (1000 - ai) = 1000 T - sum_{i in G} ai.
Thus D = Sa - Sg = (sum_{i in A} ai) - (1000 T - sum_{i in G} ai) = sum_{i in A} ai + sum_{i in G} ai - 1000 T = Σ ai - 1000 T. Yes correct.
So D = A_total - 1000 T. So D is linear in T. And T is an integer between 0 and n. So D can take values: A_total - 1000k for k=0..n. Condition: |A_total - 1000 T| <= 500.
Therefore we need to find T (number of eggs given to G) such that -500 <= A_total - 1000 T <= 500. That is: A_total - 500 <= 1000 T <= A_total + 500. Or dividing by 1000? Actually we need integer T satisfying:
1000 T ∈ [A_total - 500, A_total + 500].
But T must be integer. So we need to check if there exists integer T in [0,n] such that 1000 T is within 500 of A_total.
Given A_total, we can compute candidate T = round(A_total/1000) maybe. More precisely, we need |A_total - 1000T| <= 500. That means 1000T is within 500 of A_total. This is equivalent to T = floor((A_total+500)/1000) or ceil((A_total-500)/1000) as long as within bounds.
Alternatively, we can note that for any integer T, the difference is at most 500 if A_total mod 1000 is not too far from 0 or 1000? Actually A_total - 1000T is essentially the remainder when A_total is divided by 1000, but with T chosen appropriately. If we take T = round(A_total/1000) (to nearest integer), then the absolute difference is |A_total - 1000round| = | (A_total mod 1000) - 500? Not exactly. Let's think: Write A_total = 1000q + r, where 0 <= r < 1000. Then if we take T = q, D = r. Condition |r| <= 500? But r is non-negative. Actually D = A_total - 1000q = r. So |r| <= 500 => r <= 500. If r <= 500, then T = q works. If r > 500, then consider T = q+1. Then D = A_total - 1000(q+1) = r - 1000. That's negative: r-1000, absolute value = 1000 - r. Condition: 1000 - r <= 500 => r >= 500. So if r >= 500, T = q+1 works (since 1000 - r <= 500). Actually careful: For r > 500, r is >500, so 1000 - r < 500 (since r>500 => 1000-r<500). So T = q+1 gives |D| = 1000 - r <= 500. So for any r, either r <= 500 works with T=q, or 1000 - r <= 500 works with T=q+1. But note that if r = 500 exactly, both conditions hold: r <=500 and 1000-r=500 <=500, so both T=q and T=q+1 work. So for any A_total, we can always find an integer T such that |A_total - 1000T| <= 500? Because the two possibilities are T = floor(A_total/1000) and T = ceil(A_total/1000). But is it always true that either r <= 500 or 1000-r <= 500? Since r is between 0 and 999 inclusive, one of r <=500 or r>=500 is always true. Actually if r <= 500, then T=q works. If r >= 500, then T=q+1 works. So for any r, one of the two works because the intervals overlap at 500. So there is always an integer T that satisfies the inequality? But we also need T between 0 and n inclusive. That's the only additional constraint. So the problem reduces to: can we choose T (number of eggs assigned to G) between 0 and n such that T is either floor(A_total/1000) or ceil(A_total/1000) and the resulting |A_total - 1000T| <= 500. However, note that floor and ceil of A_total/1000 are integers; but if A_total/1000 is integer, then r=0, T = A_total/1000 works with difference 0 <=500. So existence of T is guaranteed for the inequality alone, but T must be feasible: we need to assign exactly T eggs to G. But we can assign any subset of eggs to G, as long as exactly T are G. However, does the assignment of which specific eggs to G affect Sa - Sg beyond the count T? According to our formula D = A_total - 1000T, it is independent of which eggs are assigned to G. Wait is that always true? Let's re-check: D = Σ ai - 1000 T. Indeed, regardless of which eggs are given to G, the sum of ai is fixed, and T is just the number of G assignments. So D depends only on T, not on which eggs. That's interesting! So if we fix T, any assignment with exactly T eggs given to G yields the same D? Let's verify with an example: two eggs, (a1,g1) and (a2,g2). If we assign egg1 to G and egg2 to A, then Sa = a2, Sg = g1, D = a2 - g1. But A_total = a1+a2. T=1, so A_total - 1000*1 = a1+a2-1000. But a2 - g1 = a2 - (1000 - a1) = a1+a2 -1000. Yes matches. If instead assign egg1 to A, egg2 to G, then D = a1 - g2 = a1 - (1000 - a2)= a1+a2-1000. Same. So indeed D depends only on T.
Thus the condition reduces to existence of T ∈ {0,1,...,n} such that |A_total - 1000T| <= 500. As argued, there always exist integers T (like floor and ceil) that satisfy the inequality, but they might be outside [0,n]. So the only obstruction is if both possible T values that satisfy the inequality are outside the range [0,n]. But typically A_total is between 0 and 1000n. So the possible T that make |A_total-1000T|<=500 are roughly around A_total/1000. There will be at least one integer T in the interval [(A_total-500)/1000, (A_total+500)/1000]. Since A_total ∈ [0,1000n], this interval is within [ -0.5, n+0.5] essentially, so there is always at least one integer T within [0,n]? Not necessarily: if A_total is near 0, then (A_total-500)/1000 is negative, (A_total+500)/1000 is positive but maybe less than 0? Let's check extremes.
If A_total = 0, then condition |0-1000T|<=500 => 1000T <=500 => T=0 only (since T integer). T=0 is within [0,n] for any n>=0. So OK.
If A_total is small, say A_total = 10, then we need |10-1000T|<=500 => -500 <= 10-1000T <=500 => -510 <= -1000T <= 490 => 1000T <= 510 and 1000T >= -490? Actually let's solve: 10-1000T >= -500 => -1000T >= -510 => 1000T <= 510 => T <= 0.51 => T=0 works? Check: T=0 gives D=10, |10|<=500 yes. T=1 gives D=10-1000= -990, abs 990>500. So T=0 works. So always T=0 works if A_total <=500? Actually if A_total <=500, then T=0 gives D = A_total <=500, so condition holds. Similarly if A_total >= 1000n - 500, then T=n gives D = A_total - 1000n = (A_total - 1000n) which is >= -500? Actually if A_total >= 1000n - 500, then A_total - 1000n >= -500, and also <=? A_total - 1000n could be positive? But max A_total=1000n, so A_total-1000n <=0? Wait A_total max is 1000n (if all ai=1000, but ai+gi=1000, so ai can be at most 1000? Actually ai can be 1000, then gi=0, so yes A_total max = 1000n). So for A_total close to max, T=n gives D = A_total-1000n <=0, but abs = 1000n - A_total <=500 if A_total >=1000n-500. So T=n works if A_total >=1000n-500.
Thus for any A_total, there is always a T in [0,n] that satisfies? Let's check if there is any gap: Suppose A_total = 600, n=1. A_total = 600, n=1. T can be 0 or 1. T=0 gives D=600 >500 not allowed. T=1 gives D=600-1000=-400, abs=400 <=500. So T=1 works. So yes.
In general, for any integer A_total between 0 and 1000n, we claim there exists integer T in [0,n] such that |A_total - 1000T| <= 500. Is this always true? Consider n=1, we have two possible T: 0 and 1. For T=0, difference = A_total. For T=1, difference = |A_total-1000| = 1000 - A_total (since A_total <=1000). So condition is A_total <=500 or 1000-A_total <=500 -> A_total >=500. So the union covers all A_total from 0 to 1000. For A_total between 0 and 500, T=0 works; between 500 and 1000, T=1 works; at 500 both work. So yes.
For n=2, possible T:0,1,2. We need to see if any A_total in [0,2000] fails. Condition for T=0: A_total <=500; T=1: |A_total-1000|<=500 -> A_total in [500,1500]; T=2: A_total >=1500? Actually T=2 gives |A_total-2000|<=500 -> A_total in [1500,2500] but max 2000 so [1500,2000]. So union: [0,500] ∪ [500,1500] ∪ [1500,2000] = [0,2000]. So covered. In general, the intervals for T=k: [1000k-500, 1000k+500] intersected with [0,1000n] produce overlapping intervals because each interval length is 1000 and they start at -500, 500, 1500,... So they cover continuously. The intervals: for k from 0 to n, we get [1000k-500, 1000k+500]. Their union over k=0..n is from -500 to 1000n+500. But we care about A_total in [0,1000n]. Since each interval overlaps with the next at the boundary (the intervals touch at 1000k+500 = 1000(k+1)-500). So union covers the whole [0,1000n] without gaps. Indeed because the step is 1000 and each interval length is 1000, they exactly abut at the midpoints? Let's check: interval for k: [1000k-500, 1000k+500]; interval for k+1: [1000(k+1)-500, 1000(k+1)+500] = [1000k+500, 1000k+1500]. So they meet at the point 1000k+500. So continuous coverage. Since the first interval starts at -500, it covers from 0 to 500 for k=0 (since negative part irrelevant). The last interval ends at 1000n+500, covering up to 1000n. So indeed for any A_total in [0,1000n] there exists some T (maybe more than one) satisfying |A_total - 1000T| <= 500.
Thus there is always a feasible T? That would imply the answer is always possible? But the problem statement suggests sometimes it's impossible. Let's check examples: They gave input:
2
1 999
999 1
Output: AG
That works. Another example:
3
400 600
400 600
400 600
Output: AGA (so possible). The problem asks to output -1 if impossible. According to our reasoning, it's always possible. But perhaps we missed something: The assignment must also be such that each egg is assigned to exactly one child, but we can choose any T. So it seems always possible. Wait, but we need to output an actual assignment string. Since D only depends on T, we can choose any T that satisfies the condition. Then we need to assign exactly T eggs to G and the rest to A. But does any assignment with exactly T eggs to G work? Yes, because D is independent of which eggs are G. So we can arbitrarily pick any T eggs to be G, and the rest A. However, we must ensure that the assignment is possible, i.e., we can assign any egg to either child? There's no further constraint. So it seems always possible.
But why would the problem be posed as sometimes impossible? Perhaps I misinterpret: The condition is |Sa - Sg| <= 500, where Sa is total money paid to A (sum of ai for eggs assigned to A) and Sg is total money paid to G (sum of gi for eggs assigned to G). This we used. But also the price each child named is per egg, and they are paid exactly those amounts for the eggs they get. So it seems fine.
Wait, there might be a nuance: The sum of prices for each egg is 1000, but ai and gi are integers. Our analysis holds. So perhaps the problem is indeed always solvable? But then why would the problem ask to output -1 if impossible? Could it be that there is an additional hidden constraint? Let's read the problem statement again: "Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500." That's it. So the condition is exactly |Sa - Sg| <= 500.
Given that ai+gi = 1000, and we have freedom to assign each egg, it's always possible to achieve any T number of G eggs? But we must assign exactly T eggs to G. But we can always choose any subset of eggs to be G. So yes.
Wait, maybe there's a twist: The eggs are to be painted, and the children have named their prices for each egg. Uncle J wants to distribute eggs between children so that each child paints some eggs, and he pays each child the sum of the prices that child named for the eggs they paint. But does it have to be that each child paints at least one egg? The problem doesn't say each must get at least one; could be zero. So T can be 0 or n.
Thus our analysis suggests the problem is trivial: always possible. But the problem is from Codeforces, I recall problem "Egg painting" maybe? Actually this is Codeforces Round #333 (Div. 2) problem B? Or maybe it's "Uncle J wants to have n eggs painted" - it's CF 399B? Wait I recall a problem "Egg painting" maybe 394A? Let's search memory: There's Codeforces problem 408B? Not sure. But I've seen similar problems. Actually there is a known Codeforces problem: "The Child and Toy" etc. But this one: I think it's Codeforces 405B? No.
Let me think: There is a known problem: "Gena's Code" no. Another: "Uncle J and painting eggs" maybe is from Codeforces 374B? I'm not sure.
But given the problem statement includes "-1" output, maybe there are cases where it's impossible. Let's test with some random small n. Suppose n=1, ai=0, gi=1000. Then A_total=0. T=0 gives D=0 ok, assignment: "A"? But if we assign to A, we pay 0 to A and G gets nothing? Actually Sa=0, Sg=0 (since no eggs to G), so |0-0|=0 <=500. So "A" works. Also T=1 gives D = -1000, abs=1000>500 not allowed. So choose T=0. That's fine. So possible.
Another: n=1, ai=1000, gi=0. A_total=1000. T=1 gives D=0, works. So possible.
n=2: (0,1000) and (1000,0). A_total = 1000. We need T such that |1000-1000T|<=500. T=1 gives 0, works. So assign one egg to G, one to A. That yields Sa = 0? Wait if we assign egg1 (ai=0,gi=1000) to A gives Sa+=0; egg2 (ai=1000,gi=0) to A gives Sa+=1000? But we need exactly T=1 G. So we can assign egg1 to G (pay G 1000) and egg2 to A (pay A 1000). Then Sa=1000, Sg=1000? Actually check: egg1 to G: pay G gi=1000; egg2 to A: pay A ai=1000. So Sa=1000, Sg=1000, difference=0. Or egg1 to A (pay A 0) and egg2 to G (pay G 0) then Sa=0, Sg=0. But T=1 requires exactly one G, so second assignment: egg1 to A (G count 0) egg2 to G (G count 1). That yields Sa=0 (from egg1) +? Actually egg2 to G, we pay G gi=0, so Sg=0. So Sa=0, Sg=0 difference 0. So works. So possible.
So it seems always possible. Could there be a scenario where the sum of all ai is such that the only T that satisfies the inequality is not integer? But T must be integer, but we have integer T possibilities. The intervals cover all integer points in [0,n]? Actually we need T integer in [0,n]. For A_total in [0,1000n], there is always at least one integer T in [0,n] with |A_total-1000T|<=500. This is because the intervals [1000k-500, 1000k+500] for k=0..n cover the entire range [0,1000n] as argued, with overlaps at boundaries. So for any A_total, pick k = floor((A_total+500)/1000). Let's see: define k = round(A_total/1000) but ensure within bounds. More formally, set T = floor((A_total+500)/1000). Since 0 <= A_total <= 1000n, we have 0 <= (A_total+500)/1000 <= n+0.5, so floor is between 0 and n. Then we have: T <= (A_total+500)/1000 < T+1 => 1000T <= A_total+500 < 1000(T+1) => A_total -500 < 1000T <= A_total+500? Actually from 1000T <= A_total+500 we get A_total -1000T >= -500? Not exactly. Let's check that this T works: if T = floor((A_total+500)/1000), then we have 1000T <= A_total+500 and 1000(T+1) > A_total+500 => 1000T > A_total -500? Because 1000(T+1) > A_total+500 => 1000T > A_total -500. So we have A_total -500 < 1000T <= A_total+500. That means |A_total - 1000T| <= 500? The inequality 1000T <= A_total+500 is A_total - 1000T >= -500, i.e., -500 <= A_total - 1000T. And A_total -500 < 1000T gives 1000T > A_total-500 => A_total - 1000T < 500. But we need also <=500, but we have strictly less than 500? Actually from A_total-500 < 1000T we get 1000T > A_total-500 => A_total - 1000T < 500. Combined with 1000T <= A_total+500 gives A_total - 1000T >= -500. So -500 <= A_total - 1000T < 500. That's almost |A_total - 1000T| <= 500, but the upper bound is strict? Actually we have A_total - 1000T < 500, which is okay because we need <=500; <500 implies <=500. So it's fine. Also if equality holds? It could be exactly 500? If A_total - 1000T = 500, then from 1000T <= A_total+500 we get 1000T <= 1000T+500+500 => 0 <= 1000? Actually if A_total - 1000T = 500, then 1000T = A_total-500, so (A_total+500)/1000 = (1000T+1000)/1000 = T+1 exactly. Then floor((A_total+500)/1000) = T+1, not T. So our formula yields T+1. So that T+1 will satisfy condition with difference -500? Because A_total - 1000(T+1) = -500, which is allowed. So using T = floor((A_total+500)/1000) ensures that we never have a T where difference is exactly +500; instead we might get -500. So condition holds. So always possible.
But then why problem includes "-1"? Could it be that the children's prices are not exactly summing to 1000? Actually given: ai+gi=1000, so yes. Wait, the statement: "It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000." So that's a condition. So sum is always 1000.
Thus it appears always possible. However, maybe there is an additional constraint that Uncle J must pay each child the total amount they asked for the eggs they get, and the total payment to each child must be integer? Already are integers. No.
Perhaps I misinterpreted the condition: "Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500." That could be interpreted as the absolute difference |Sa - Sg| <= 500, same as we used.
Maybe there is a catch: The children's prices are per egg, but Uncle J may have to assign each egg to exactly one child, but he might also be able to decide which child paints which egg? That's what we used.
Thus it's always solvable. But maybe there is a twist: The assignment must also be such that the total amount paid to each child is not necessarily just the sum of the prices they named? No.
Wait, I recall a Codeforces problem "Painting Eggs" (CF 282B?). Actually there is a known problem: "Painting Eggs" from Codeforces Round #282 (Div. 1)? Let's search memory: There is problem "283B - Painting Eggs" maybe. I'm going to recall: There is Codeforces problem 282B? Actually I think it's problem "282B" or "279B"? I'm not sure.
Let me quickly think: There is a Codeforces problem "319B - Painting Eggs"? Not sure.
Better to google memory: There is problem "Eggs" from Codeforces: I recall a problem where you need to output a string of A and G such that the difference of sums is bounded, and the solution was greedy: start with difference 0, for each egg assign to the one that doesn't make difference exceed 500, or something. That's because the naive greedy works: process eggs one by one, keep current difference D = Sa - Sg. For each egg, if assigning to A would make D > 500, assign to G; else assign to A. This is a typical solution for "B. Painting Eggs" from Codeforces Round #282 (Div. 2)? Actually I think it's from round 282B. I recall that problem: "B. Painting Eggs" where you have pairs (ai, gi) with ai+gi=1000, and you need to assign each egg to A or G so that |Sa - Sg| <= 500. And the solution is greedy: maintain current difference, for each egg, try to assign to A if it doesn't make diff exceed 500 in absolute, else assign to G. And it's proven that if at any step both assignments lead to diff > 500, then impossible; otherwise you get a solution. That problem indeed has -1 output. So maybe it's not always possible? Wait but our analysis suggests it's always possible given the sum condition. Let's double-check: In the greedy approach, we consider the current difference D. When processing an egg with prices (a, g=1000-a). If we assign to A, new D' = D + a; if assign to G, new D' = D - (1000-a) = D + a - 1000. So two possible increments: +a or +a -1000. The greedy algorithm may fail if both lead to |D'| > 500? Is that possible? Let's test: Suppose current D = 500, and next egg has a = 600 (so g = 400). Then assign to A: D' = 500+600=1100 -> |1100|>500. Assign to G: D' = 500 + 600 -1000 = 100 -> |100| <=500. So assign to G works. Suppose D = 500, a=1000, g=0: A gives 1500, G gives 500-0? Actually G gives D' = 500+1000-1000=500, okay. Suppose D = 500, a=0, g=1000: A gives 500, G gives 500-1000=-500, both okay? Actually |500|=500 okay; |-500|=500 okay. So both fine.
Consider D = -500, a=600: A gives -500+600=100 ok; G gives -500+600-1000 = -900 -> | -900| >500. So assign to A works. So at extremes, maybe both options exceed 500? Let's try to find D and a such that both |D+a| > 500 and |D + a - 1000| > 500. That would mean D+a > 500 or D+a < -500, and also D+a-1000 > 500 or D+a-1000 < -500. Since a between 0 and 1000, D between -500 and 500 initially? Actually we start D=0, then after each step we keep D within [-500,500] if we maintain condition. But if we want to see if impossible, we need to see if there exists a sequence where no assignment keeps D within bounds.
Let D be current difference before processing an egg. We must choose assignment so that new D stays in [-500,500]. For a given D and a, we need at least one of D+a or D+a-1000 to be in [-500,500]. Equivalent to: there exists t ∈ {0,1} such that -500 ≤ D + a - 1000t ≤ 500. So we need the intervals for t=0 and t=1 to cover the possible D values. The condition that both are out of range would mean: D+a > 500 and D+a-1000 < -500? Or D+a < -500 and D+a-1000 > 500? But the latter is impossible because D+a-1000 is always 1000 less than D+a, so if D+a < -500, then D+a-1000 < -1500, which is definitely < -500, so both < -500. Or if D+a > 500, then D+a-1000 > -500? Actually if D+a > 500, then D+a-1000 could be either >500? Not possible because subtracting 1000 reduces it. So if D+a > 500, D+a-1000 <= D+a-0? Actually max D+a can be? Let's systematically analyze:
We have two candidate new D values: X = D+a, Y = D+a-1000. Since a ∈ [0,1000], Y = X - 1000. So Y is exactly 1000 less than X. The allowed interval is [-500,500]. So we need either X ∈ [-500,500] or Y ∈ [-500,500]. Since Y = X-1000, if X is > 500, then X-1000 could be > -500? Actually if X > 500, then X-1000 > -500? For example X=600, X-1000=-400 which is within [-500,500]. So if X is moderately above 500, Y might be within range. Conversely, if X < -500, then X-1000 < -1500, which is far below -500, so Y is not within range. But then X itself is out (too low). So both out would occur if X > 500 and Y > 500? But Y = X-1000, so Y > 500 would require X > 1500. But X = D+a, D at most 500? Actually D is current difference and we maintain within [-500,500] after each step. So D ∈ [-500,500]. a max 1000, so X max = 1500. So it's possible X > 500 and Y > 500 if X > 1500? But max X = 1500, Y=500 exactly if X=1500? Actually if X=1500, Y=500, that's within range. So Y > 500 would need X > 1500, impossible. So that case not possible. Similarly both out if X < -500 and Y < -500? X < -500 => X <= -501, then Y = X-1000 <= -1501 < -500. So both out if X < -500. So both assignments fail if X < -500. X = D+a. Since D >= -500 (if we maintain the invariant), D could be -500 and a could be 0, then X = -500, which is allowed (edge). For X < -500, we need D + a < -500. Since D >= -500, this requires a < 0? Actually D could be -500 and a = 0 gives X=-500, not less. To get X < -500, need D = -500 and a < 0? But a >= 0. So impossible. Wait: If D = -500, then X = -500 + a. Since a >= 0, X >= -500. So X can't be less than -500. So the case X < -500 cannot happen if D >= -500. But we must maintain D within [-500,500] after each step, but at the start D=0. The greedy algorithm ensures we only proceed if we can keep D in [-500,500]. But if at some step both assignments lead to |D'| > 500, that would mean that X > 500 and Y > 500? Or X > 500 and Y < -500? But Y is always X-1000, so if X > 500, Y = X-1000 <= 500-1000 = -500? Actually if X > 500, then X-1000 could be > -500 if X > 500? Let's compute: X-1000 > -500 => X > 500. So if X > 500, then Y > -500? Actually X > 500 => X-1000 > -500? Example: X=600, X-1000=-400 > -500? Yes, -400 > -500, so Y is greater than -500. Also Y = X-1000 will be less than 500 because X <= 1500 gives Y <= 500. So Y could be within [-500,500] if X ∈ (500, 1500]? Actually if X ∈ (500, 1500], then Y ∈ (-500, 500]. In particular, if X ∈ (500, 1500], Y ∈ (-500, 500]? Let's check boundaries: X=500 (exact) gives Y=-500, both within? X=500 is within allowed? Yes, 500 is allowed. Y=-500 is allowed. X=1500 gives Y=500 allowed. So for any X between 500 and 1500 inclusive, Y is between -500 and 500. That means if X is in [500,1500], then at least one of X or Y is within [-500,500]? Actually we need to check: If X ∈ [500,1500], then Y = X-1000 ∈ [-500,500]. So Y is within range. Therefore, if X ∈ [500,1500], then assigning to G (Y) yields D' in range. If X < 500, then X might be in range, else if X > 1500, then Y > 500? But X cannot exceed 1500 because D max 500 and a max 1000 => X max 1500. So X > 1500 impossible. So it seems that for any D in [-500,500] and any a in [0,1000], we always have at least one of X or Y within [-500,500]? Let's examine edge cases: D=500, a=1000 => X=1500, Y=500 => Y in range. D=500, a=0 => X=500, Y=-500 => both in range. D=-500, a=1000 => X=500, Y=-500 => both in range. D=-500, a=0 => X=-500, Y=-1500 => X in range, Y out. D=500, a=500 => X=1000, Y=0 => Y in range. D=500, a=400 => X=900, Y=-100 => Y in range. D=-500, a=200 => X=-300, Y=-1300 => X in range. So indeed seems always at least one option is valid. Could there be a case where X > 500 and Y < -500 simultaneously? For that we need X > 500 and X-1000 < -500 => X < 500. Contradiction. So not possible. Similarly X < -500 and Y > 500 impossible because X < -500 gives Y < -1500. So indeed for any D in [-500,500] and any a in [0,1000], either X or Y is in [-500,500] (or both). But is it always? Let's test D= -500, a=1: X = -499 (in range). Good. D= -500, a=1000: X=500 (in range). D= -500, a=500: X=0 (in range). D= -500, a=0: X=-500 (in range). So yes.
Thus greedy always works, and solution always exists. So why does the problem include -1? Possibly because there is an additional constraint: Uncle J wants to pay each child the amount they named for the eggs they paint, but also the total money paid to A must be different from total money paid to G by no more than 500. However, maybe there is also a requirement that the assignment is such that each child paints at least one egg? The problem doesn't state that. However, maybe there is an implicit requirement that both children must paint at least one egg? Let's read: "Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child." It doesn't say each child must get at least one egg. So zero is allowed.
Another thought: Could it be that the prices are not integers? They are integers, given constraints.
Maybe the twist is that the total money paid to A must be different from the total money paid to G by no more than 500, meaning |Sa - Sg| ≤ 500, but also perhaps they want the absolute difference to be strictly less than 500? "by no more than 500" means ≤500.
Given the analysis, the problem seems always solvable. But maybe there is a mistake: In our derivation D = A_total - 1000 T, which is independent of assignment. However, note that Sa is sum of ai for eggs assigned to A, not sum of all ai minus something? Actually we derived D = Σ ai - 1000 T, where T = number of eggs given to G. That's correct. But is it always independent of which eggs? Yes, because Σ ai is constant, and T is just count. So any assignment with the same number of G eggs yields the same D. So the condition reduces to existence of integer T in [0,n] such that |A_total - 1000 T| ≤ 500. Since intervals cover the whole range, such T always exists. So always possible.
Wait, but is there any restriction that each child must be assigned at least one egg? Let's search memory: I recall a Codeforces problem "282B - Painting Eggs". I think it's exactly this. Let me recall: In problem "Painting Eggs" from Codeforces Round #173 (Div. 2) maybe? Actually I'm going to search my memory: There is a problem "CF 282B" where you have to output a string of A and G such that the difference of sums is <= 500. I think I've seen editorial that proves that solution always exists. But I also recall that the problem output is never -1, but they included -1 for completeness? I'm not sure.
Let me check Codeforces 282B: "Painting Eggs". I recall that problem: There are n eggs, each egg has ai and bi (prices for Anita and Goran). Sum ai+bi = 1000. Need to assign each egg to one painter such that |Sa - Sb| <= 500. And the solution is greedy: maintain current difference, for each egg if adding ai to Sa would make diff exceed 500, then assign to G, else assign to A. And it's always possible, so answer never -1. I think I saw a comment that the answer always exists. So indeed -1 is never output. But the problem statement includes -1 to keep it general, but due to constraints it's always possible. However, could there be cases where it's impossible? Let's try to construct a counterexample. Suppose we have n eggs where all ai are 0. Then A_total = 0. We need T such that |0 - 1000 T| <= 500 => 1000 T <= 500 => T = 0 only (since T integer). So T must be 0. That means assign all eggs to A. That's fine. So possible.
Suppose all ai are 1000. Then A_total = 1000 n. Need T such that |1000n - 1000 T| <= 500 => |n - T| <= 0.5? Actually divide by 1000: |n - T| <= 0.5, so T must be n (since n and T are integers, difference must be 0). So T = n. Assign all to G. That's fine.
Now suppose mixture. So always possible.
Wait: I recall that in some versions, there is an additional requirement: the total money paid to A must be different from the total money paid to G, i.e., not equal? The phrase "different from ... by no more than 500" means the difference is not more than 500, not that they must be different. So equality allowed.
Thus seems always possible.
But why would the problem be rated as Div2 B and have many solutions with greedy and possible -1? Perhaps I'm missing something: The children's prices are per egg, and uncle pays each child the sum of their named prices for the eggs they get. But there is also a condition that the total money paid to A and G must be exactly 1000 per egg? No, that's per egg sum.
Maybe the catch is that the children are named A and G, and the assignment must be such that the total amount paid to A is "different from" the total paid to G by no more than 500, meaning that the absolute difference is at most 500. That's what we used.
But maybe the twist is that the total money paid to each child is the sum of the prices they named for the eggs they paint, but each egg is painted by exactly one child. That's what we used.
So I'm convinced it's always possible. But let me double-check the derivation: D = Sa - Sg = sum_{i in A} ai - sum_{i in G} gi. But gi = 1000 - ai. So D = sum_{i in A} ai - sum_{i in G} (1000 - ai) = sum_{i in A} ai - 1000*|G| + sum_{i in G} ai = sum_{i} ai - 1000*|G|. Yes.
Thus D depends only on |G|, not which eggs.
Thus the problem reduces to: choose integer k = number of G eggs, 0 ≤ k ≤ n, such that |A_total - 1000k| ≤ 500. Since A_total is known, we need to find such k. As argued, there always exists k. But is it possible that the only k satisfying the condition is not an integer? But k must be integer. Let's check: For any integer A_total between 0 and 1000n, there exists integer k such that |A_total - 1000k| ≤ 500. Because the set {1000k : k integer} is an arithmetic progression with step 1000. The maximum distance from any number to the nearest multiple of 1000 is at most 500. Indeed, for any real x, the distance to the nearest multiple of 1000 is at most 500. Since A_total is an integer, the nearest multiple of 1000 is either floor(A_total/1000)*1000 or ceil(A_total/1000)*1000, and the distance to that multiple is min(r, 1000-r) where r = A_total mod 1000. And since r is integer between 0 and 999, min(r,1000-r) ≤ 500 (with equality when r=500). So there always exists an integer k (either floor or ceil) such that |A_total - 1000k| ≤ 500. But is that k guaranteed to be between 0 and n? For A_total in [0, 1000n], the nearest multiple of 1000 might be 0 or 1000n, which are within range. For A_total near 0, the nearest multiple is 0, which is k=0. For A_total near 1000n, the nearest multiple is 1000n (k=n). For middle values, the nearest multiple is some k between 0 and n. Indeed, since A_total ∈ [0,1000n], the nearest multiple of 1000 is within [0,1000n] because the extreme case: if A_total < 500, nearest multiple is 0; if A_total > 1000n - 500, nearest multiple is 1000n. So k is always within 0..n. Therefore always feasible.
Thus answer always exists. So the problem is trivial: compute A_total = sum of ai. Find k = round(A_total / 1000) but ensure integer and in range. Specifically, let q = A_total // 1000, r = A_total % 1000. If r <= 500, then choose k = q; else choose k = q+1. This k will satisfy condition and be between 0 and n because when A_total is close to 1000n, q = n if r=0, else q = n-1? Actually if A_total = 1000n - 300, then r = 1000n - 300 mod 1000 = (1000n mod 1000 = 0) so r = (1000n - 300) mod 1000 = 1000 - 300? Wait careful: A_total = 1000n - 300. Then A_total // 1000 = n-1 (since 1000n - 300 // 1000 = n-1, because 1000*(n-1)=1000n-1000, remainder 700). Actually 1000n - 300 = 1000*(n-1) + 700, so q = n-1, r=700. Since r=700 > 500, we choose k = q+1 = n. Then check |A_total - 1000n| = |1000n - 300 - 1000n| = 300 <= 500, good. And k=n is valid. Similarly, if A_total = 1000n - 800, then q = n-1, r=200 <=500, choose k=n-1, diff=200 <=500, ok.
So always possible. So we can simply output any assignment with exactly k G's. However, there is a subtlety: we need to output a string of length n with exactly k 'G' characters and n-k 'A' characters. But does the order matter? No, any assignment works. So we can just output, say, the first k eggs as G and the rest as A? But is it allowed to assign arbitrarily? Yes, because D only depends on count. So we can simply output a string with k 'G's and n-k 'A's in any order.
But wait, is there any condition that the assignment must be such that each child paints the eggs in the order? No.
Thus the answer is trivial: compute total ai, decide k as above, then output k 'G's followed by (n-k) 'A's (or any pattern). However, is there any risk that the computed k might be > n or <0? Since A_total <= 1000n, for A_total close to 1000n, r might be >500 leading to k = q+1 = floor(A_total/1000)+1. If A_total = 1000n, r=0 => k = n. If A_total = 1000n - 400, q = n-1 (since 1000n-400 = 1000(n-1)+600), r=600>500 => k = n. That's valid because n <= n. So k never exceeds n because if A_total = 1000n - something, q+1 <= n? The maximum q+1 occurs when A_total is just below 1000n, say 1000n-1, then q = floor((1000n-1)/1000) = n-1, r=999, so k = n (since 999>500). So k=n, valid. Similarly, minimum k: if A_total=1, q=0, r=1<=500 => k=0, valid. So k is always between 0 and n.
Thus the problem seems always solvable. But then why would the problem have examples and require a solution? Maybe I've misread the problem: Could it be that the condition is that the absolute difference between the total money paid to A and the total money paid to G must be strictly less than 500? It says "different from ... by no more than 500" which includes 500. So ≤.
Maybe the condition is that the total money paid to A should be no more than the total money paid to G plus 500, and vice versa? That's the same.
Maybe there's another twist: The children's prices per egg are such that the sum for each egg is 1000, but they are not necessarily integers? The input says integers, so fine.
I recall a Codeforces problem "B. Painting Eggs" (CF 282B) indeed has this property: The answer always exists. In fact, many solutions use greedy and never output -1. I think the editorial says that the answer always exists. But the problem statement includes -1 just in case, maybe to not give away that it's always possible? But it's typical for problems to include -1 even if never used.
Let's check Codeforces 282B: I'm going to recall: The problem statement: "The Bitlandians are quite weird people. They have very peculiar customs." That rings a bell. I think that's exactly CF 282B "Painting Eggs". I remember that problem: There are n eggs, each with ai and bi, with ai+bi=1000. Need to assign each egg to one of two girls, such that the absolute difference between total payments is at most 500. Output any assignment or -1 if impossible. Many participants solved with greedy. I think it's known that a solution always exists, but they included -1 for completeness. Actually, I recall a hack: Some people thought it might be impossible and checked for -1, but it's always possible. But there is a twist: In some versions, the prices are not necessarily summing to 1000? But here they do.
Wait, maybe there is an additional constraint that the assignment must be such that the total money paid to A is **not