We need to solve the problem: given n volumes (positive integers? non-negative, so can be zero). The chef pours the same amount of juice into all cups initially. Then exactly one pouring event may have happened: some amount v (positive integer) is poured from cup a to cup b (a != b). This results in decreasing cup a by v and increasing cup b by v (assuming no overflow, infinite capacity). So the volumes after pouring are given. We need to determine if there was zero pourings (i.e., all volumes equal), or exactly one pouring event (i.e., we can find a pair a, b and v such that after pouring from a to b we get the given volumes from a state where all cups had equal initial amount). Or if impossible, output "Unrecoverable configuration."
Important: the initial state is that all cups had the same volume X (unknown integer, non-negative, not necessarily integer? The problem says the chef always pour the same number of milliliters of juice to all cups. So initial amount X for each cup. Then exactly one pouring event may have occurred: they poured some integer v from one cup to another. So after that, we have volumes: For cup a: X - v, cup b: X + v, all others: X.
But note: The input volumes are non-negative integers. Since initial X is non-negative and v integer, it's possible that v > X, but since cups are bottomless, pouring from a to b can only reduce a to negative? Actually if bottomless, pouring from cup to another: you can pour from a only as much as it has? Or do they allow pouring more than it has? The problem says: "cups to be bottomless so that the juice never overfills a cup and pours out, however much it can be." That suggests cups have no upper limit, but they still have contents; if you pour from one cup to another, you must have enough juice in the source cup. They didn't explicitly say that you cannot pour more than what's in the source cup. But typical meaning: you pour some amount from cup A to B; cup A must have that amount to pour. Since initially all cups had X, after pouring v, cup A would have X - v; that must be >= 0 because you cannot have negative juice. So we require X >= v. Also volumes are non-negative. So indeed X >= v.
Thus the set of final volumes must be: all but two are equal; one is lower by v, one is higher by v, with v > 0 integer. And the lower and higher volumes differ by 2v? Actually lower = X - v, higher = X + v. So the average of all volumes is X because (sum = nX). Indeed sum = nX, so X = sum/n. For X to be integer, sum must be divisible by n. But is it required? The initial X is the equal amount poured; it could be any integer? The chef pours integer milliliters? The problem says "one can only pour to a cup or from one cup to another an integer number of milliliters of juice." That suggests that all pours are integer. The chef initially pours the same number into all cups; it's ambiguous whether that number must be integer. Usually it's integer because volumes are integers. Since input volumes are integers, the chef likely pours integer amounts. So X is integer. Then sum must be divisible by n. However, is this strictly necessary? Let's examine: If there is exactly one pouring, the sum of final volumes equals sum of initial volumes: nX. So sum is nX, so X = sum/n. For X to be integer, sum must be divisible by n. But could X be non-integer? The problem states the chef always pours the same number of milliliters of juice to all cups. That number would be integer because pouring integer milliliters. So X integer. So if sum not divisible by n, then it's impossible to be a one-pour situation. However, the "Exemplary pages" case is when no pouring: all equal. That also implies sum divisible by n, obviously. So the condition sum % n == 0 is necessary for both zero and one-pouring cases. But note: For zero pouring, all volumes equal, so average is that volume, so sum divisible by n. For one pouring, we require sum divisible by n and that the volumes differ from the average in a specific pattern.
But we must also consider the case where there is no pouring but volumes are all equal, but sum divisible by n is automatically true. So okay.
But there could be scenario where sum divisible by n, but volumes are not all equal and not exactly one-pour pattern. That yields unrecoverable.
Edge cases: n = 1. If only one cup, can there be a pouring? Pouring from cup to cup would require two distinct cups. So only possible scenarios: either no pouring (so volumes are same across all cups; but only one cup, trivial) or they could pour from the only cup to itself? That would be weird and not allowed because "from one cup to another" implies different cups. So for n=1, any volume is fine? The chef pours same amount to all cups (just one cup). So if there was a pouring, there would need to be two cups. So only possibility is no pouring. So output "Exemplary pages." But is it possible that the pages poured from the only cup to another cup? No, there is only one cup. So no pouring. So always exemplary? But what if the input is something else? The problem says "from which cup juice was poured to which one" so they consider at least two cups? n could be 1. But the scenario of one pouring would require two distinct cups. So for n=1, it's impossible to have a one-pouring. So if the volume is any non-negative integer, it's either exemplary (since all cups equal trivially) or maybe unrecoverable if we consider that they could have poured from this cup to a non-existent cup? That doesn't make sense. So we should output "Exemplary pages." But is that always correct? Let's test: Input: n=1, volume=5. All cups have same volume, so pages didn't pour. So exemplary. That matches.
But what if there is an odd scenario: The pages could have poured from this cup to another cup and then removed that cup? But the input is the final state of n cups. So no. So n=1 is straightforward.
Now n=2: The possible one-pouring scenario: Starting with both cups equal X. Then pour v from cup a to cup b. Then final volumes: one is X - v, other is X + v. So they are symmetric around X. So essentially, the two volumes must sum to 2X, and they are not equal (unless v=0). So if they are equal, exemplary; if they are not equal, we need to check if they can be obtained from one pour. Since any two numbers that sum to an even number and are not equal can be expressed as X-v and X+v, where X = sum/2 (must be integer) and v = difference/2 (must be integer). So condition: sum even, and both volumes integers, obviously they are. So for n=2, any two numbers with even sum? Actually sum = a+b; X = sum/2. Then v = |a - b|/2. So we need v integer, which requires a and b have same parity (since difference even). So if a and b are both even or both odd, then it's possible. And also we need v > 0 (i.e., a != b). Also we need to identify which cup gave and which received. Since one is lower than X, one higher. So if a < b, then a is source, b destination. But if a == b then exemplary. If a and b have different parity, then it's unrecoverable because cannot be obtained from one integer pour. So that matches.
But note: The problem says "you can determine the volume of poured juice ... exactly one juice pouring". So we need to output the volume and the cup numbers (source and destination). It might be ambiguous if there are more than one possible pair? For example, for n>2, we need exactly one pair of cups that deviate from the average, and all others exactly equal to the average. If there are more than two cups differing from average, or one cup differing but not exactly paired, then it's unrecoverable. Also if exactly two cups differ but they are not symmetric around average (i.e., one is X - v, other X + v), then unrecoverable.
We must also ensure that the source cup (the one that gave) is the one with volume less than X, and the destination cup is the one with volume greater than X. If both are below or above, impossible. Also, if there is exactly one cup not equal to average and all others equal, impossible because that would change total sum? Actually if only one cup differs, sum would not equal nX unless that cup equals X as well. So impossible. So we need exactly two cups not equal to average, with one above, one below, and the amounts differ from average by same magnitude v (positive integer). Also average X must be integer (so sum divisible by n). Also v > 0 integer.
But what about cases where multiple cups have the same deviation but there are more than two deviants? For instance, n=4, volumes: 10, 10, 20, 20. Average = 15. The deviations: -5, -5, +5, +5. This could be result of two separate pourings? The problem says exactly one pouring. Could one pouring affect only two cups? Yes, only two cups change. So with two source-destination pairs would require at least two pourings. So it's not allowed. So unrecoverable. Good.
Also consider case where exactly two cups deviate but deviations not equal in magnitude: e.g., volumes: 9, 15, 15, 15 (n=4). Average = (9+45)=54/4=13.5 not integer, so unrecoverable anyway. If average integer but deviations not equal magnitude? Example: 8, 12, 10, 10 (n=4). Sum=40, average=10. Deviations: -2, +2, 0, 0 works. That's good. Another: 7, 13, 10, 10: sum=40, avg=10, deviations: -3, +3, 0, 0 works. So magnitudes equal. That's necessary.
But also need to check that v is positive integer; it will be integer if the differences from average are integers; since average integer, the differences are integers. So fine.
Now there is a nuance: The pages could have poured from cup a to cup b, but maybe the amount v is such that after pouring, cup a becomes equal to X-v, but what if that equals another cup's volume? That's fine.
Also, the problem says "determine from which cup juice was poured to which one". It also says "if you can determine the volume of juice poured during exactly one juice pouring, print ...". So we must be able to uniquely determine a and b. In a valid scenario, there will be exactly one cup with volume less than average and exactly one cup with volume greater than average, and all others exactly average. So it's unique. But what if there are multiple cups with the same lower volume? That would be ambiguous, so not a valid one-pour scenario. So we need exactly one cup below and exactly one cup above.
But consider n=3, volumes: 5, 10, 10. Sum=25, average = 25/3 ≈ 8.333 not integer, so unrecoverable. If we have 5, 11, 8? sum=24 avg=8, deviations: -3, +3, 0 works? 5 (8-3), 11 (8+3), 8 (8). That yields two deviants: one below, one above. That is valid. So for n=3, it's fine.
Now, what about zero volumes? Cup could be empty initially? Chef pours same amount to all cups, so initial X could be zero? Possibly if he pours 0 ml? That seems unlikely but not prohibited. The cups initially have same number, could be zero. Then a pour of v from a to b results in a: -v? That would be negative, which is impossible. So for zero initial, the only possible pour is v=0, but then no pouring. So if X=0 and we see a positive volume in some cup and negative in another? Not possible because volumes non-negative. So for X=0, any non-zero volume indicates unrecoverable because you would need to get positive from somewhere. But sum >0 would imply X>0 because sum = nX. So X is average. So if any cup >0, X>0. So zero X only if all zero. So okay.
Now, algorithm: Read n and volumes array a[1..n]. Compute sum = Σ a[i]. Check if sum % n != 0. If not, output "Unrecoverable configuration." because cannot have equal initial or one pour? Actually for one pour, sum divisible by n is necessary, as argued. So if not divisible, unrecoverable.
Otherwise, X = sum / n.
Now we need to find indices where a[i] != X. Let low = indices with a[i] < X, high = indices with a[i] > X. Count them.
- If there are no such indices (i.e., all a[i] == X), output "Exemplary pages."
- Else if number of low != 1 or number of high != 1, then output "Unrecoverable configuration." Because exactly one cup below and exactly one cup above. (Note: Could there be a case where one cup is below by some amount and another cup is above by exactly the same amount, but there are also other cups that are not equal to X? That would be more than two deviants, so not allowed.)
- Else, we have exactly one low index i and one high index j. Check if X - a[i] == a[j] - X (let v = X - a[i] = a[j] - X). If not equal, then unrecoverable. (But if we have exactly one low and one high, it's almost guaranteed they differ by same amount? Not necessarily: e.g., X=10, low=5 (diff=5), high=12 (diff=2). Then v would be 5 vs 2. But they'd both be deviating, but sum check would still hold: 5+12+others =? Let's test: n=3, volumes: 5,12,13? sum=30, avg=10. But we have 5,12,13? That sum=30? 5+12+13=30, avg=10. Then low: 5 (diff5), high: 12 (diff2) and also 13 (diff3) so more than two deviants. So having exactly one low and one high but their deviations not equal cannot happen because sum of deviations must be zero: Σ (a[i]-X)=0. If only two indices deviate, then (a[i]-X) + (a[j]-X) = 0 => a[i]-X = -(a[j]-X) => deviations are opposites. So they must be equal magnitude. So if we have exactly one low and one high, automatically deviations are opposite? Let's see: Σ (a[k]-X) = (a[i]-X) + (a[j]-X) + sum over others (0) = 0 => a[i]-X = -(a[j]-X). So indeed they are opposite. So the magnitudes are equal. So we don't need an explicit check; it follows from sum condition. But to be safe, we can still check. However, note that sum of all deviations is zero by definition of X. So if there are exactly two indices with non-zero deviations, they must sum to zero, i.e., one positive, one negative of same magnitude. So it's automatic. So if we have exactly one low and one high, then the condition holds. However, we must also ensure that the deviation amounts are integers, which they are because X integer and a[i] integer. So v = X - a[i] (or a[j] - X) > 0. Since they are not equal to X, v > 0.
Thus scenario is valid. Output: "v ml. from cup #i to cup #j." where i is the source (the one that gave, i.e., the one with lower volume) and j is the destination (higher volume). Ensure i and j are the correct order.
Now edge: what if there are zero deviations? That's the exemplary case. So we have covered.
Now, check for possible pitfalls: Input volumes up to 10^4, n up to 1000, sum up to 10^7, fits in int. Use 32-bit signed integer okay.
Now, what about the case where sum divisible by n but there are exactly two deviants but one of them is exactly X? That can't be, because deviants by definition are != X. So fine.
Now consider scenario where there are exactly two deviants but both are on same side? That would be two cups above X and no cups below, but then sum of deviations would be positive, contradicting sum zero. So impossible.
Thus algorithm is straightforward.
But wait: The problem statement says: "If you can determine the volume of juice poured during exactly one juice pouring, print ..." It also says: "Finally, if the given juice's volumes cannot be obtained using no more than one pouring (for example, the pages poured the juice from one cup to another more than once or the royal kitchen maids poured the juice into the cups incorrectly), print ..." So the output is exactly as described.
Thus we implement.
Now let's test examples:
Example 1:
5
270
250
250
230
250
sum = 270+250+250+230+250 = 1250? Let's compute: 270+250=520, +250=770, +230=1000, +250=1250. n=5 => X=250. Then deviations: cup1 (270) > X by 20; cup4 (230) < X by 20; others 250. Exactly one low (4) and one high (1). v=20. Output: "20 ml. from cup #4 to cup #1." matches.
Example2:
5 all 250: sum=1250, X=250, all equal => exemplary.
Example3:
5
270
250
249
230
250
sum=270+250+250? Wait: 270+250=520, +249=769, +230=999, +250=1249. 1249/5 = 249.8, not integer => unrecoverable. Output matches.
Another test: n=2, cups: 5, 7. sum=12, X=6, v=1? Actually 5=6-1, 7=6+1. So one low (1), one high (2). Output: "1 ml. from cup #1 to cup #2." That seems correct.
But what about n=2, cups: 6,6 => exemplary.
n=2, cups: 5,6 => sum=11, X=5.5 not integer => unrecoverable.
n=3, cups: 10, 5, 15. sum=30, X=10. deviations: cup1=0? Actually cup1=10 (equal), cup2=5 (-5), cup3=15 (+5). So one low (2), one high (3). v=5. Output: "5 ml. from cup #2 to cup #3."
Now what about scenario where there are exactly two deviants but the deviations not opposite? Not possible due to sum. So okay.
Now, consider n=3, volumes: 9, 11, 10. sum=30, X=10. Deviations: -1, +1, 0. Works.
Now scenario where there are more than two deviants but they could still be result of one pouring? Could one pouring affect more than two cups? No, pouring from one cup to another only changes two cups. So no.
Thus algorithm stands.
Now we must consider the possibility that the pages could have poured from cup a to cup b, but also perhaps they could have poured zero? That's same as no pouring. So fine.
One more nuance: The problem says "one needs to keep an eye on the control whether they have set everything correctly. This time the royal chef Gerasim had the impression that the pages have played a prank again: they had poured the juice from one cup to another. Now Gerasim wants to check his hypothesis." So we are to determine if his hypothesis is plausible. That is, there exists exactly one pouring event (or zero) that yields given volumes. So we check if the configuration is reachable with at most one pouring. So our logic is correct.
But is there any alternative scenario where the pages poured from one cup to another, but the amount poured is zero? That would be no pouring. So exemplary covers that.
Now, what about the case where the sum is divisible by n, and there is exactly one cup deviating from X, and the rest are X? That would require that the deviation is zero because sum must be nX, but if only one deviates, then that deviation must be zero to keep sum correct. So that cup would also be X. So not possible.
Now what about the case where there are exactly two deviants but both are equal to X? That's not deviants.
Thus algorithm is simple.
Now, we need to be careful with output formatting: "v ml. from cup #a to cup #b." Note the period at end. And in exemplary: "Exemplary pages." with period. And unrecoverable: "Unrecoverable configuration." with period. Also note the space after ml? The example: "20 ml. from cup #4 to cup #1." So "v ml." with a dot after ml? Actually it says "20 ml. from cup #4 to cup #1." So it's "ml." (abbreviation) and then " from". So output exactly: e.g., f"{v} ml. from cup #{src} to cup #{dst}." But careful: If v is integer, print integer without extra spaces. There's a space after number, then "ml.", then space, then "from", etc.
Now check for multiple testcases? No, single input.
Implementation steps:
- Read integer n.
- Read list a of length n (1-indexed).
- Compute total = sum(a).
- If total % n != 0: print("Unrecoverable configuration.") and exit.
- X = total // n.
- Initialize low_idx = None, high_idx = None, low_count = 0, high_count = 0.
- For i in 1..n:
if a[i] < X:
low_idx = i; low_count += 1
elif a[i] > X:
high_idx = i; high_count += 1
Also, if low_count > 1 or high_count > 1: we can early exit? But must also check that any cup exactly equal is fine.
- After loop:
if low_count == 0 and high_count == 0:
print("Exemplary pages.")
elif low_count == 1 and high_count == 1:
v = X - a[low_idx] # or a[high_idx] - X
# verify v > 0, but it will be.
print(f"{v} ml. from cup #{low_idx} to cup #{high_idx}.")
else:
print("Unrecoverable configuration.")
That's it.
But wait: What about the possibility that there are exactly two deviants but they are both below X? That would require sum of deviations negative, contradicting total sum = nX. So impossible. Similarly both above impossible.
Now, consider n=2, low_count =1, high_count =1 automatically. So works.
Now, consider scenario where total divisible by n but there are exactly two deviants but they are not exactly one low and one high? Could it be that one is low and one is also low? That would make sum < nX, impossible. So not.
Thus done.
Now, let's double-check sample 3: total=1249, n=5, not divisible, unrecoverable.
Now test edge: n=1, volume=0: total=0, X=0, low_count=0, high_count=0 => exemplary. So output "Exemplary pages." which is correct.
n=1, volume=5: total=5, X=5, all equal => exemplary.
n=3, volumes 0,0,6: total=6, X=2, but then a: 0<2 (low), 0<2 (low), 6>2 (high). low_count=2, high_count=1 => unrecoverable.
n=3, volumes 1,3,2: total=6, X=2, deviations: -1, +1, 0 => low_idx? Actually a[1]=1 (<2) low_count=1; a[2]=3 (>2) high_count=1; a[3]=2 equal. Works. v=1. Output "1 ml. from cup #1 to cup #2."
Now, check for potential pitfalls: The output must be exactly as specified, with period at the end and no extra spaces. In f-string, careful to include the period after ml and at the end. Example: f"{v} ml. from cup #{low_idx} to cup #{high_idx}."
But note: The problem statement says "v ml. from cup #a to cup #b." So after ml there is a dot. So we have "ml.".
Now, note: The volume v is integer. So print as integer.
Now, are there any tricky cases where v could be zero? That would happen if low and high exist but deviations zero? Not possible because low and high are defined as not equal to X, so v>0.
Now, what about the case where there is exactly one low and one high but the magnitude of deviation is not equal? As argued, impossible because sum of deviations zero. But we can still compute v = X - a[low_idx] and also check that a[high_idx] - X equals v; if not, something inconsistent might happen due to integer overflow? Not necessary but safe. So we can add a check if v != a[high_idx] - X: print unrecoverable. But if our code logic is correct, it won't happen. But to be robust, we can add it.
Now, also consider if sum divisible by n but X may be negative? No, volumes are non-negative, sum >=0, so X >=0.
Now, what about n=0? Not allowed (n>=1).
Thus solution seems trivial.
But wait, the problem statement says "if the given juice's volumes cannot be obtained using no more than one pouring (for example, the pages poured the juice from one cup to another more than once or the royal kitchen maids poured the juice into the cups incorrectly)". That indicates that maybe the initial pouring by the chef could be incorrectly done? No, it says the royal kitchen maids poured the juice into the cups incorrectly. That suggests that maybe the initial state (equal amounts) might not be achieved because the maids made a mistake. So our check must confirm that there exists an integer X (the amount the chef intended to pour into each cup) such that after at most one pouring, we get the given volumes. The chef always pours the same amount to all cups, but maybe the maids could have poured incorrectly initially? Actually the problem statement: "the royal chef Gerasim always pour the same number of milliliters of juice to all cups in the royal kitchen." So that is the initial state. So the maids didn't pour; the chef did. So the initial state is always equal amounts. The pages might have poured after that. So the configuration must be reachable from some equal amount X via at most one pour. So our logic is correct.
Now, are there any scenarios where there is exactly one pouring but the source and destination cups have the same index? No, must be distinct.
Thus solution is ready.
Now, I'll write the code accordingly.
Time complexity O(n). Memory O(1) aside from storing list.
Now, consider possible pitfalls: The input reading may contain extra spaces. We'll use standard input reading.
Now, write final solution in Python.
But before finalizing, I will test with some custom cases:
Case 1: n=4, volumes: 10, 10, 10, 10 => exemplary.
Case 2: n=4, volumes: 8, 12, 10, 10 => sum=40, X=10, low_idx=1 (8), high_idx=2 (12), v=2 => "2 ml. from cup #1 to cup #2."
Case 3: n=4, volumes: 8, 12, 10, 11 => sum=41, not divisible => unrecoverable.
Case 4: n=4, volumes: 8, 13, 10, 9 => sum=40, X=10, deviations: -2, +3, 0, -1? Actually 8 (-2), 13 (+3), 10 (0), 9 (-1). So low indices? <X: 1 (8), 4 (9) => low_count=2, high_count=1 => unrecoverable.
Case 5: n=4, volumes: 7, 11, 10, 12? sum=40, X=10, deviations: -3, +1, 0, +2 => low_count=1 (7), high_count? 11 (+1) and 12 (+2) => high_count=2 => unrecoverable.
Case 6: n=2, volumes: 0, 2 => sum=2, X=1, v=1 => "1 ml. from cup #1 to cup #2." But check: initial X=1. Cup1 gave 1 to cup2, then cup1 becomes 0, cup2 becomes 2. That works. Output is correct.
Case 7: n=2, volumes: 0, 1 => sum=1, X=0.5 not integer => unrecoverable.
Case 8: n=2, volumes: 0, 0 => exemplary.
Now, what about n large? not needed.
Thus code seems fine.
Now, I will produce final answer with explanation.
But there is an additional nuance: The problem says "one can only pour to a cup or from one cup to another an integer number of milliliters of juice." So v must be integer. Our derived v is integer because X and a[i] integers. Good.
Now, output formatting: In the exemplary pages case, it's "Exemplary pages." not "Exemplary pages."? Actually it's "Exemplary pages." (capital E). In unrecoverable, "Unrecoverable configuration." (capital U). And the third case: "v ml. from cup #a to cup #b."
Check sample: "20 ml. from cup #4 to cup #1." There's a period after ml and after the sentence. So output exactly: e.g., "20 ml. from cup #4 to cup #1." (Note: no extra spaces). In Python, we can do: print(f"{v} ml. from cup #{src} to cup #{dst}.")
Now ensure no extra spaces at end.
Now, also consider if v could be 0? That would be exemplary, because then no pour needed. But our condition for exemplary is all equal; if they are all equal, v would be 0 if we considered low/high but we don't. So fine.
Thus solution done.
But before final answer, I want to double-check the logic for the scenario where sum divisible by n, but there is exactly one low and one high, but the deviations are not equal? As argued, impossible because sum of deviations = 0 => (a_low - X) + (a_high - X) = 0 => a_high - X = -(a_low - X). So they are opposites. So equality holds automatically. However, this relies on the assumption that all other cups exactly equal X. In our counting, we only ensure that there is exactly one low and one high; we don't explicitly check that all others are exactly X. But if there is exactly one low and one high, and we have exactly n-2 other cups, it's possible that some of those other cups are not equal to X but still there is exactly one low and one high? That would require that some other cups are also either low or high but equal to X? No, if they are not equal to X, they'd be either low or high, increasing count beyond one each. So indeed, if low_count == 1 and high_count == 1, then all other cups must be exactly X (since they are neither low nor high). So the condition is sufficient.
Thus code stands.
Now, potential pitfalls: The volumes can be up to 10^4, sum up to 10^7, fits in int.
Now, what about reading input: The lines may have extra whitespace. Use int(input().strip()).
Now, produce final solution.
But let me think if there is any misinterpretation: Could the pages have poured from one cup to another, but maybe they poured multiple times? The problem says we need to determine if exactly one pouring (or zero) could produce the volumes. So multiple pourings not allowed. Our logic covers that.
However, there is an interesting twist: The problem statement says "you can determine the volume of juice poured during exactly one juice pouring". It doesn't say that the initial state is unknown but we must infer it. So indeed we need to find if there exists X and a pour such that volumes result.
Now, what about the possibility that the initial X is not necessarily the average? If they poured from one cup to another, the total sum remains the same. So the average is X. So X must be total/n.
Thus fine.
Now, any scenario where they poured from cup a to cup b, but v might not be integer? Problem says integer pours only. So X must be integer because initial amounts are integer (since chef pours integer). So if sum divisible by n, X integer; else unrecoverable.
Thus done.
Now, produce final answer with explanation.
But wait: The problem might have a nuance: The chef pours the same amount to all cups, but maybe that amount is not necessarily the average because the pages could have poured from one cup to another and maybe also the chef made a mistake? No.
Thus final.
Now, I will write final solution in Python.
But the answer should include the algorithm description, complexity, and proof? The user asked: "You are an AI assistant that responds to queries... Write a program that solves this problem." So the final answer should be the code. Possibly with explanation? The instruction: "You are given a problem statement, write a solution." Typically we output just the code. But as ChatGPT we can provide explanation and then code. However, in this platform, the final answer is the code block. I'll provide a brief explanation and then the code.
Now, I'll write the code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [0] * n
total = 0
for i in range(n):
val = int(next(it))
a[i] = val
total += val
if total % n != 0:
print("Unrecoverable configuration.")
return
X = total // n
low_idx = -1
high_idx = -1
low_cnt = 0
high_cnt = 0
for i in range(n):
if a[i] < X:
low_cnt += 1
low_idx = i
elif a[i] > X:
high_cnt += 1
high_idx = i
if low_cnt == 0 and high_cnt == 0:
print("Exemplary pages.")
elif low_cnt == 1 and high_cnt == 1:
v = X - a[low_idx] # same as a[high_idx] - X
# Optional check: if v != a[high_idx] - X, unrecoverable, but it should.
print(f"{v} ml. from cup #{low_idx+1} to cup #{high_idx+1}.")
else:
print("Unrecoverable configuration.")
if __name__ == "__main__":
solve()
Note: Cup numbering is 1-indexed. So we output low_idx+1 and high_idx+1.
Test with given samples.
First sample: n=5, a... low_idx = 3? Actually a[3] is 230? Index: 0:270,1:250,2:250,3:230,4:250. low_idx = 3 (zero-based), high_idx = 0. v = X - a[3] = 250-230=20. Output: "20 ml. from cup #4 to cup #1." matches.
Second: all equal => exemplary.
Third: total%5 != 0 => unrecoverable.
All good.
Now, check for potential errors: If low_cnt and high_cnt both 1, but v = 0? That would imply a[low_idx] == X and a[high_idx] == X, but then they wouldn't be counted as low/high. So v>0.
Now, possible overflow? Not.
Thus final answer.
I'll provide final answer with code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [0] * n
total = 0
for i in range(n):
val = int(next(it))
a[i] = val
total += val
# average must be integer because the initial equal amount is integer
if total % n != 0:
print("Unrecoverable configuration.")
return
X = total // n
low_idx = -1
high_idx = -1
low_cnt = 0
high_cnt = 0
for i, v in enumerate(a):
if v < X:
low_cnt += 1
low_idx = i
elif v > X:
high_cnt += 1
high_idx = i
if low_cnt == 0 and high_cnt == 0:
print("Exemplary pages.")
elif low_cnt == 1 and high_cnt == 1:
poured = X - a[low_idx] # same as a[high_idx] - X
# cups are numbered from 1 in the output
print(f"{poured} ml. from cup #{low_idx+1} to cup #{high_idx+1}.")
else:
print("Unrecoverable configuration.")
if name == "main":
solve()