We need to understand the problem: There are n friends. Each round, exactly one of them is the supervisor (i.e., not playing), and the other n-1 people are players. For each person i, we are given a desired number of rounds they want to play (ai). They must play at least ai rounds; they can play more. We need to find the minimum total number of rounds M such that it's possible to schedule who is supervisor each round, so that each person i plays at least ai rounds.
This is a classic scheduling/feasibility problem. Equivalent to: We have n people. In each round, we choose one to be supervisor (sit out), and the other n-1 play. After M rounds, let xi be number of rounds i plays. We need xi >= ai for all i, and xi <= M (since they can't play more than rounds). Also, sum_i xi = (n-1) * M, because each round exactly n-1 people play. So sum_i xi = (n-1)M.
Given M, can we determine if it's possible? The constraints: 0 <= xi <= M, xi integer, xi >= ai, sum xi = (n-1) M.
We need to find minimal M such that there exist xi satisfying these.
We can think: Since xi must be at least ai, total needed sum of minimum plays is sum_i ai = S. But total available plays is (n-1)M. So we need (n-1)M >= S. That's necessary. But also we need to respect that no one can play more than M: since we might need to allocate extra plays to some persons beyond ai to meet the total sum. The constraint xi <= M may become problematic if some ai are too high relative to M. Specifically, if for some i, ai > M, impossible. Also if sum_i ai > (n-1)M, impossible. But also because each xi <= M, the maximum total sum we can achieve given lower bounds ai is at most sum_i min(M, something?) Actually, given M, we must assign xi between ai and M. So total sum achievable is between sum_i ai and n*M. But we need total sum exactly (n-1)M. So feasibility condition: There exist xi in [ai, M] such that sum xi = (n-1)M. This is like we have lower bounds, upper bounds, and a target sum. Feasibility exists iff sum_i ai <= (n-1)M <= sum_i M = n M. The lower bound is necessary. The upper bound is automatically satisfied if M is high? Actually (n-1)M <= nM always true. The more restrictive condition is that we cannot exceed each person's max, but we need total sum (n-1)M. This sum is less than nM, so we have some slack: total "slack" capacity is nM - (n-1)M = M. But we need to ensure that we can achieve exactly (n-1)M without violating upper bounds. Equivalent to: we can allocate "extra plays" beyond ai to some persons, but each person can take at most M - ai extra. The total extra we need above the minimum sum is E = (n-1)M - S. Each person i can contribute at most (M - ai) extra (if we set xi = M). So we need sum_i (M - ai) >= E. Let's compute: sum_i (M - ai) = n M - S. This must be >= E = (n-1)M - S. Indeed, n M - S >= (n-1)M - S simplifies to M >= 0, always true. So it seems the upper bound condition is automatically satisfied? Wait, check: The extra needed E must be <= total capacity for extra, which is n M - S. Since E = (n-1)M - S, compare: n M - S >= (n-1)M - S => M >= 0, true. So as long as M is non-negative, the total capacity is at least the needed extra. But is it always possible to allocate exactly E extra given the per-person caps? This is a classic feasibility of distribution of extra amounts with upper bounds: we need to assign nonnegative increments di for each i, such that 0 <= di <= M - ai, and sum di = E. Since sum of maximum capacities (M - ai) = nM - S, which is >= E, and also E >= 0 if (n-1)M >= S. But is it always possible? Not necessarily; we need to check if there is any additional constraint from integrality? But di can be any integer (since rounds are integer). The necessary and sufficient condition for existence of di in [0, cap_i] with sum E is that E <= sum cap_i, and also E >= 0, and also each cap_i is nonnegative. Typically, when you have lower bound 0, it's always possible to allocate any E up to the sum, because you can just add to some persons until you reach E, as long as each cap_i is enough. But there might be a subtlety: if some cap_i are negative? But M >= ai needed; otherwise impossible. So if M < ai for some i, then cap_i = M - ai < 0, but we cannot have negative lower bound? Actually we have lower bound xi >= ai, so if M < ai, it's impossible because that person cannot even meet their required plays; they'd need to be supervisor in some rounds, but they'd have to play at least ai, but maximum plays is M, so need M >= ai. So we must have M >= max(ai). So then all cap_i >= 0.
Now the only condition for existence of di summing to E is that sum cap_i >= E, which holds because sum cap_i = nM - S and E = (n-1)M - S, and the difference is M, which is >=0. So yes, there is slack of M in total capacity beyond the needed extra. But we need to be able to allocate exactly E. Since we have slack, we could allocate less than full caps. The question: can we always choose di such that sum di = E, given that di <= cap_i? Since we have total capacity > E, we need to ensure that we can hit exactly E. This is equivalent to checking that the minimum possible sum (0) is less than E, which it is if E>0, and the maximum possible sum is sum cap_i. Since we have some flexibility, we can adjust. Usually, if we have a set of caps and we need to achieve some total between 0 and sum cap_i, it's always possible to choose di to achieve any integer total as long as it's within [0, sum cap_i]? Not necessarily; if caps are integers, the set of achievable sums from integers di with 0 <= di <= cap_i is all integers from 0 to sum cap_i because these are like a knapsack but with unit increments? Actually, if you have multiple items with capacities, the set of achievable sums is indeed all integers from 0 to sum cap_i, because you can think of each di as an integer; it's known that if you have multiple variables with upper bounds, the set of possible sums is all integers in that range if you can arbitrarily assign values? Let's check: Suppose two caps: cap1 = 1, cap2 = 1. Achievable sums: 0,1,2. Yes, all integers. cap1 = 2, cap2 = 2: sums 0,1,2,3,4. But can we get 3? Yes, di1=1,di2=2 gives 3. Actually 1+2=3, both within caps. So yes. In general, if you have at least two caps, you can achieve any sum up to total. But is there a counterexample? Suppose only one variable? Here we have n variables, n >=3 by problem constraint (n>=3). So at least 3 caps. So any integer between 0 and sum cap_i is achievable, because you can treat it as you can increment one by one while staying within caps. More formally, the set of achievable sums of integers di with 0<=di<=cap_i is all integers from 0 to sum cap_i because you can take di=0 for all and then add 1 to some di as long as not exceeding cap_i; you can achieve any integer by a greedy algorithm. Since each increment of 1 can be done by increasing a di that hasn't reached its cap. With multiple di, you can always adjust. So yes, any integer in that range is achievable.
Thus, the necessary and sufficient conditions for M to be feasible are:
- M >= max(ai) (since each person must play at most M rounds, but they need at least ai, so M must be at least ai)
- (n-1)M >= S, where S = sum ai.
But is condition 1 automatically implied by condition 2? Not exactly: suppose n=3, a = [100, 1, 1]. Then S=102, (n-1)M = 2M. Condition 2 requires 2M >= 102 => M >= 51. But max(ai)=100, so M>=51 does not guarantee M>=100. So we need explicit M>=max(ai). So both conditions are necessary. Are they sufficient? Let's test: n=3, a=[100,1,1], M=100. Check: S=102, (n-1)M=200 >=102, M >= max=100. So conditions satisfied. Can we schedule? We need xi >= ai, sum xi = 200. Minimum sum = 102, need extra 98. Caps: for person1: M - a1 = 0; for others: M - a2 = 99, M - a3 = 99. So total extra capacity = 198. Extra needed 98 <=198. Also person1's cap is 0, so x1 must be exactly M=100 (since min is 100, max is 100). So x1=100. Then x2+x3 = 200-100=100. They need at least 1 each, so x2>=1, x3>=1, and x2+x3=100. Feasible? x2=1, x3=99 works, and both <=100. Yes. So M=100 works. But is M=51 feasible? M=51, then M>=max? 51 < 100, so fails condition 1. So indeed need both.
But wait: is there any additional constraint regarding that each round only one person is supervisor, which might impose that the number of times each person sits out (i.e., is supervisor) is exactly M - xi. And each round someone sits out; the vector of supervisor counts must be such that each person sits out some non-negative integer times, and sum of supervisor counts = M (since each round one supervisor). That's automatically satisfied if we define xi. Because xi + (supervisor count) = M for each i. So constraints on xi are exactly what we used. So conditions are necessary and sufficient.
Therefore the problem reduces to: find minimal integer M such that M >= max(ai) and (n-1)M >= sum ai.
But wait: is it always minimal to satisfy both? Let's test examples:
Example 1: n=3, a=[3,2,2]. max=3, S=7. (n-1)=2. Condition (2M >=7) => M >= 4 (since M integer). Condition M>=3 => M>=3. So minimal M satisfying both is 4. Output 4. matches.
Example 2: n=4, a=[2,2,2,2]. max=2, S=8. (n-1)=3 => 3M >=8 => M >= 3 (since M=2 gives 6<8). Condition M>=2 satisfied. So M=3. Output 3. matches.
Thus the answer is max( max(ai), ceil( sum(ai) / (n-1) ) ).
But is there any scenario where we need to consider something else? Let's think about possible off-by-one: Because sum xi = (n-1)M must be integer; M integer, so (n-1)M integer. That's fine. The condition we derived is exactly that. However, there is a nuance: xi must be integer; but if we set M as integer, and ai are integer, then we need integer xi. Our feasibility argument gave existence of integer xi. So okay.
But is it always sufficient to consider these two conditions? Let's test edge cases: n=3, a=[1,1,1]. max=1, S=3, (n-1)M=2M >=3 => M >= 2 (since ceil(3/2)=2). M>=1 satisfied, so M=2. Can we schedule? M=2 rounds. Need each plays at least 1. Total plays = (n-1)M = 4. We need x1+x2+x3=4, each >=1. That's possible: e.g., x1=2,x2=1,x3=1. But we must also ensure that each person sits out (M-xi) rounds is non-negative integer, which holds as x1=2 => sits out 0; x2=1 => sits out 1; x3=1 => sits out 1. In two rounds, need one supervisor each round: could be round1: person2 supervisor? Then plays: p1,p3 play; round2: person3 supervisor => p1,p2 play. Then counts: p1 plays 2, p2 plays 1, p3 plays 1. Works.
What about n=3, a=[2,2,2]? max=2, S=6, 2M >=6 => M>=3. M>=2 satisfied, so M=3. Total plays=6. We need each >=2, sum=6 => each exactly 2. So each sits out 1 round. That's a round-robin schedule where each is supervisor once. Works.
What about n=3, a=[100,0,0]? But ai >= 1 per constraints (1 ≤ ai ≤ 10^9). So all ai >=1.
What about n=3, a=[1000000000, 1, 1]? max huge, S huge, answer = max(max, ceil(S/(n-1))) = max(1e9, ceil((1e9+2)/2)) = max(1e9, 500000001) = 1e9. That seems plausible.
But wait, is it always possible when M = max(ai) but (n-1)M might be less than S? Then we need to increase M. So the answer is the max of these two.
Thus solution is straightforward: compute sumA, maxA, then answer = max(maxA, (sumA + n - 2) / (n - 1) )? Actually careful: ceil( sumA / (n-1) ) = (sumA + (n-2)) // (n-1). Because integer division ceiling: (sumA + (n-2)) // (n-1). Let's verify: If sumA divisible by (n-1), ceil = sumA/(n-1). (sumA + n-2)//(n-1) gives (k*(n-1) + (n-2))//(n-1) = k + (n-2)//(n-1) = k (since (n-2)//(n-1) is 0). Works. If sumA = k*(n-1) + r with 1 <= r <= n-2, then ceil = k+1. (sumA + n-2)//(n-1) = (k*(n-1)+r + n-2)//(n-1) = k + (r + n-2)//(n-1). Since r + n-2 is between n-1 and 2n-4, //(n-1) is 1 if r+n-2 >= n-1, which is true as r>=1 => r+n-2 >= n-1, and also <= 2n-4 < 2(n-1) for n>2? For n=3, n-1=2, 2n-4=2, exactly 2 which equals 2, but integer division (2)//2=1, so k+1, correct. Actually careful: for n=3, n-2=1, (sumA+1)//2 gives ceil? For sumA=3, (3+1)//2=2, ceil(3/2)=2. For sumA=4, (4+1)//2=2, ceil(4/2)=2. Works. For n=4, n-2=2, (sumA+2)//3: if sumA=8, (8+2)//3=10//3=3, ceil(8/3)=3. Good. So formula works.
So answer = max(maxA, (sumA + n - 2) // (n - 1) ).
But wait, consider potential overflow: sumA can be up to n * 1e9 = 1e14, fits in 64-bit (10^5 * 10^9 = 10^14). So we need 64-bit integer (long long).
But is that always the answer? Let's think: Are there constraints about the number of rounds where a person can be supervisor? They can be supervisor any number of rounds as long as they still get to play enough. There's no limit like a person cannot be supervisor too many times. So indeed no further constraints.
One might worry: if we set xi >= ai, sum xi = (n-1)M, and xi <= M. We already accounted for that. But what about the fact that exactly one person sits out each round? That is covered by sum xi = (n-1)M.
Thus the answer is straightforward.
But wait: There is known Codeforces problem "Mafia" (maybe CF 349A?), I recall the solution is indeed max(max ai, ceil(sum ai / (n-1))). I've seen it before.
Nevertheless, let's double-check with some edge cases:
Case: n=3, a=[1,1,100]? Actually same as earlier, answer = max(100, ceil(102/2)=51) = 100.
Test if we can do M=100: need x1>=1, x2>=1, x3>=100, sum=200. So x3 can be 100, x1+x2=100 => allocate x1=1, x2=99 works. That's valid. Could we do M=99? Check: M=99, max=100? fails M>=100 so impossible. So M=100 is indeed minimal.
Case: n=3, a=[50,50,50] sum=150, ceil(150/2)=75, max=50, answer=75. M=75: need each >=50, sum=150, so sum of x = 150. With each xi <=75, we need exactly 150 total. Since each max 75, the sum of maxes =225, but we need total 150. Could we assign xi? Need x1+x2+x3=150, each between 50 and 75 inclusive. Is that possible? Yes, e.g., 50,50,50 sum 150 (all min). But wait, sum of mins is 150, so we must set each exactly 50. That's within max, fine. So M=75 works, but is it minimal? Let's try M=74: 2M=148 <150 so fails sum condition. So answer=75.
Another case: n=5, a=[10,10,10,10,100] sum=140, n-1=4, ceil(140/4)=35, max=100 => answer=100. M=100: sum needed total = 400, we need x5>=100 (must be exactly 100 because max=100) and x1...x4 >=10, total min sum=100+10+10+10+10=140, extra needed=260. caps: x5 cap=0, others cap=90 each => total extra capacity=360, so we can allocate extra 260 among four others. For example, give each 65? 65*4=260, each within 90. So feasible. Could we do M=99? M=99<100, fails max condition, so not possible. So answer=100.
Seems correct.
Now consider a scenario where ceil(sum/(n-1)) > max, but the resulting M might still not be sufficient because of the per-person max? But we have ensured M >= ceil(...), so M >= sum/(n-1). However, we also have M itself being the max per person. So if M >= max(ai), it's fine. But what if ceil(sum/(n-1)) is larger than max, then M = ceil(sum/(n-1)). That M is >= max, so condition 1 satisfied. But then we need to check if it's feasible with M equal to that value. We earlier argued feasibility holds given both conditions. So it's fine.
But wait, could there be a scenario where sum/(n-1) is not integer, and the M = ceil(sum/(n-1)) may be less than max(ai)? Then we take max(ai) and that's > ceil(...). So condition 2? Need to verify (n-1)M >= sum? If M = max(ai) > ceil(sum/(n-1)), then (n-1)*max(ai) might be >= sum? Not necessarily. Example: n=3, max=10, sum=19, ceil(19/2)=10, so max=10 equals ceil, so fine. If sum=21, ceil=11, max=10, then M=11, both conditions satisfied. If sum=20, ceil=10, max=10, M=10, (n-1)*10 =20 >=20, works. If sum=22, ceil=11, max=10, M=11, (n-1)*11=22 >=22. So it seems if max is less than ceil, then ceil is larger and condition 1 satisfied. So answer works.
Thus answer is straightforward.
But before finalizing, we should also consider that n can be as large as 10^5, but computation is O(n). So solution is trivial.
Now, some may think of binary search, but we have closed form.
Nevertheless, we must ensure there's no hidden trap: The problem states "minimum number of rounds of the 'Mafia' game they need to play to let each person play at least as many rounds as they want?" That means after some number of rounds, each person has played at least ai times. We are free to choose supervisor each round. It might be that we can stop earlier than M if we overshoot some, but we need to minimize M.
Yes.
Edge Cases:
-
n=3, a=[1,1000000000,1]? sum=1000000002, ceil(1000000002/2)=500000001, max=1000000000, answer=1000000000. Check feasibility: M=1e9, sum total needed = 2e9, x2 must be at least 1e9, but cannot exceed M, so x2=1e9, then x1+x3=1e9. x1>=1, x3>=1, so possible. So M=1e9 works. Could we do M=500000001? But then max condition fails because 5e8 < 1e9, so impossible.
-
n=10^5, all a_i=1. Then sum=100000, n-1=99999, ceil(100000/99999)=2, max=1, answer=2. M=2, total plays = (n-1)2 = 199998, need each at least 1, sum min=100000, extra needed=98998, caps: each can take up to 1 extra (since M=2, a_i=1 => cap=1). Total caps = 100000. Need extra 98998, which is <= 100000, and also extra <= each cap sum, but we need to allocate 98998 among 100000 persons each with cap 1. That's possible: we need 98998 persons to get +1, and the rest 100000-98998 = 1002 persons stay at min 1. So xi distribution: 98998 persons get 2, 1002 persons get 1. Sum=989982 + 10021 = 197996+1002=198998? Wait recalc: 989982=197996, plus 1002=199... Actually 197996+1002 = 198998? That seems 197996+1000=198996, +2=198998. That's not 199998. Let's compute properly: n=100000, M=2, total plays = (n-1)M = 999992 = 199998. We need sum xi = 199998. Minimum sum = sum ai = 100000. Extra needed = 199998 - 100000 = 99998. Caps: each person can have at most 1 extra (since M - ai = 2-1 = 1). So total caps = 100000. So we need to distribute 99998 extra units among 100000 persons, each can take 0 or 1. That's fine: we can give +1 to 99998 persons, and +0 to 2 persons. Then sum = 100000 + 99998 = 199998. Yes works. So answer 2 is feasible.
-
n=3, a=[2,3,4] sum=9, ceil(9/2)=5, max=4, answer=5? Actually max=4, ceil=5, answer=5. Check M=5: total plays=10. Need each at least: 2,3,4; sum min=9, extra=1. Caps: M - a = 3,2,1. So we need to allocate 1 extra to someone who can take it; e.g., give to person3 (cap1) -> x3=5, others at min: x1=2,x2=3 sum=10, works. Person3 plays 5 rounds, but that's fine because they are allowed to play more. Could we do M=4? M=4: total plays=8 < sum min=9, fails. So 5 minimal.
Thus formula works.
But is there any case where the answer is greater than max( max, ceil(sum/(n-1)) )? Let's try to think about possibility that even if both conditions hold, maybe some person can't meet both lower and upper bound simultaneously because of the need to balance the supervisor counts? But we already argued the existence of xi. Let's check more systematically:
We need to find integer xi such that ai <= xi <= M and sum xi = (n-1)M.
Given M, define di = xi - ai >= 0. Then sum di = (n-1)M - sum ai = E. And di <= M - ai = Ci. So we need to assign nonnegative integers di <= Ci with sum E. Since sum Ci = nM - sum ai = M + E. So total capacity is E + M. So we have extra slack of M beyond needed E. For a feasible assignment, we need to check if E can be expressed as sum of di within caps. As argued, with n>=3, any integer E between 0 and sum Ci is achievable because we can use a greedy algorithm. But let's be rigorous: The set of achievable sums from n numbers where each can be any integer between 0 and Ci is all integers from 0 to sum Ci? Actually it's not always all integers if caps are not sufficiently large? Consider if one cap is 0 and others are 1? For n=2, caps [0,1], achievable sums: 0,1. That's all integers from 0 to 1, okay. For n=3, caps [0,0,1], achievable sums: 0,1. That's all integers from 0 to 1. For caps [0,1,1], sums: 0,1,2. All from 0 to 2. For caps [0,2,2], sums: 0,1,2,3,4. Can we achieve 1? Yes: set one of the 2's to 1. Can we achieve 3? Yes: one 2 and the other 2 -> 1+2? Actually to get 3: one 2 and other 1? but we have two caps of 2: if we set first to 2, second to 1 => sum=3, both within caps. Yes. So indeed any integer in range is achievable. For any set of caps with at least one cap >=1 (which is true because if all caps are 0, then sum Ci=0, and E must be 0; that's fine). For any set where sum Ci > 0, the set of achievable sums is the whole interval [0, sum Ci] because we can generate any sum by incrementing from 0. As we increase sum, we can adjust values. More formally, the set of possible sums of numbers with individual upper bounds is the set of all integers between 0 and sum Ci, inclusive, provided that the maximum cap is at least 1? Actually there is a known result: The set of all possible sums of n integers xi with 0 <= xi <= Ci is exactly all integers from 0 to sum Ci. This is true because the "Frobenius coin problem" with coin denominations 1 and arbitrary caps? But we can think: The generating function is (1 + x + ... + x^{C1}) * ... * (1 + x + ... + x^{Cn}). The product yields all coefficients from 0 up to sum Ci because the polynomials are unimodal and have no gaps? Actually there might be gaps if the caps are not 1? Let's test a counterexample: caps [1,1,1]? Achievable sums: 0,1,2,3. That's all. caps [1,2]? Achievable sums: 0,1,2,3. That's all. caps [2,2]? Achievable: 0,1,2,3,4. All. caps [3,3]? Achievable: 0,1,2,3,4,5,6. Can we get 1? Yes: (1,0). 2: (2,0) or (1,1). 3: (3,0). 4: (3,1). 5: (3,2). So all. caps [1,3]? 0,1,2,3,4. All. caps [2,5]? 0,1,...,7. Check 6: (2,4) works, (1,5) works, etc. So seems always continuous. Is there a case where there is a gap? Suppose caps are [0,0,3]? then sums: 0,3 only. Gap at 1,2. But wait, sum Ci = 3, but achievable sums are 0 and 3, not 1,2. So indeed there is a gap if only one positive cap and others zero. So condition: we need at least two positive caps to fill the gaps? Actually with caps [0,0,3], there is only one variable that can take positive values. The sum can only be multiples of the step of that variable, but the variable can take any integer between 0 and 3, so sums 0,1,2,3 are possible? Wait, if only one variable has cap 3 and the others are forced 0, then sum = x where x is between 0 and 3 integer. So indeed you can get 1,2 as well. Because the single variable can be set to 1 or 2. So caps [0,0,3] means we have one variable with cap 3, others zero. So possible sums: 0,1,2,3. All integers from 0 to 3. So that's continuous. The earlier thought that only multiples of something is not correct because the variable itself can take any integer between 0 and cap. So indeed any integer in [0, sum Ci] is achievable. Because if we have at least one variable with cap >=1, we can achieve any integer by adjusting that variable accordingly, while others stay at 0. Wait, if there is exactly one variable with cap C >0, and others zero, then sum can be any integer from 0 to C. So that's continuous as well. So the only potential gap would be if there is a lower bound that is not zero? But we have di >=0, so no lower bound. So indeed any integer E in [0, sum Ci] is achievable: choose d1 = E if E <= C1; else set d1 = C1, then E' = E - C1, then d2 = min(C2, E'), etc. This greedy works because we can assign any amount to each up to cap. So as long as we have enough total capacity, we can always achieve exactly E. There's no constraint like di must be integer? They are integer. So indeed for any integer E between 0 and sum Ci inclusive, there exists an assignment. Because we can treat it as a simple resource allocation: we have bins with capacities, we need to fill total of E units; we can fill arbitrarily as long as not exceed each cap. Since we can split E arbitrarily among bins (each bin can take any integer amount), it's always possible if sum capacities >= E. This is straightforward: sort caps descending, fill the largest first, etc. The greedy algorithm works: for i from 1 to n, assign di = min(Ci, remaining). This yields a distribution where we might end up with leftover if we run out of capacities? Actually if sum capacities >= E, greedy will always succeed because we can always put as much as needed into each bin; we can simply assign di = Ci for all but the last one where we put the remainder. That's fine. So yes, always possible.
Thus our conditions are both necessary and sufficient.
Therefore the answer is indeed max( max(ai), ceil( sum ai / (n-1) ) ).
Now we just implement.
One nuance: The problem statement says: "During each round of the game some player must be the supervisor and other n - 1 people take part in the game." That means exactly one supervisor per round. So correct.
Now we need to output answer as integer.
Implementation steps:
- Read n.
- Read array a of size n.
- Compute sumA (use long long), maxA.
- Compute rounds_needed_by_sum = (sumA + n - 2) / (n - 1) // integer division ceiling.
- Answer = max(maxA, rounds_needed_by_sum)
- Print answer.
Edge Cases: n=3, (n-2)=1, sumA+1 //2 yields ceil.
n=10^5, fine.
But wait, careful: (sumA + n - 2) / (n - 1) may overflow? sumA up to 10^14, plus n up to 10^5, still within 64-bit. So safe.
But is there any possibility that the answer can exceed 2*10^9? Possibly, but within 64-bit.
Now, double-check with n=3, a=[1,1,1] => sum=3, (3+1)//2=2, max=1, answer=2.
Now, I recall a similar problem: CF 319B? Actually problem "Mafia" maybe 349A? Let's search memory: I think it's CF #261 Div2 B "Pashmak and Flowers"? No. Actually I remember problem "Mafia" from Codeforces Round #202 (Div. 1) maybe? Anyway.
But there is also a twist: The problem might be from Codeforces 348A? Actually there is problem "Mafia" (CF 348A). Let me recall: There is a problem "Mafia" that asks exactly this. The editorial says answer = max(max a, ceil(sum a/(n-1))). So it's correct.
Thus, we can produce the solution.
But wait, the problem statement includes: "Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier." This suggests we should use long long and either cin/cout or %I64d.
Now, I should write a clean solution in C++.
Now let's consider potential pitfalls: The answer could be as high as max(ai) if ai is huge and sum/(n-1) is lower. However, if max(ai) is huge and sum/(n-1) is even larger? That's fine.
Check if there's any scenario where M = max(ai) but (n-1)M = sumA? Actually if max(ai) is large, but maybe we can schedule with M less than max(ai) if that person doesn't need to play that many? But they need to play at least ai, so if ai > M then they can't reach their required plays because they can only play at most M rounds. So M must be at least ai. So max(ai) is a lower bound.
Now, could there be a scenario where M = ceil(sumA/(n-1)) but also max(ai) is less than that, but the schedule might require some person to play more than M? Not if we set xi appropriately. We can keep xi <= M because we can always set xi = M for some but not exceed. Actually we need sum xi = (n-1)M, and each xi <= M. If we set all xi <= M, the maximum total sum possible is nM. But we need exactly (n-1)M, which is less than nM, so it's fine. So we can always assign some persons less than M to keep others at M. So no one exceeds M.
Now think about if we need to allocate di such that some persons get di = M - ai (i.e., play all rounds), that's allowed. So any allocation works.
Thus solution stands.
But wait, is there any constraint that each person cannot be supervisor in consecutive rounds? No.
Thus answer is as above.
Now, I will write solution accordingly.
But I want to double-check with more random tests using brute force for small n to ensure formula always matches minimal M. Let's manually test some small cases with n=3, enumerating all possible M from max(ai) to something and check feasibility by solving integer constraints. Since we have reasoning, but let's do mental verification for a few:
n=3, a=[1,2,3]. sum=6, ceil(6/2)=3, max=3 => ans=3. Check M=3: sum xi=6, each >= 1,2,3. Minimum sum=6, so need exactly min values: x1=1,x2=2,x3=3 gives sum=6. All <=3? x3=3 okay. So works. M=2? fails sum condition.
n=3, a=[5,5,1] sum=11, ceil(11/2)=6, max=5 => ans=6. M=6: total=12, need x's: min sum=11, extra=1. caps: 1,1,5. Assign extra to person3? cap5, then x3=2, others 5 and 5 => sum=12. works. M=5: total=10 < 11, fails.
n=4, a=[5,5,5,5] sum=20, ceil(20/3)=7 (since 20/3=6.66, ceil=7), max=5 => ans=7. M=7: total = 3*7=21, need sum=21, min sum=20, extra=1. caps: 2 each. So give extra to one person, works. M=6: total=18 < 20, fails.
n=4, a=[10,1,1,1] sum=13, ceil(13/3)=5, max=10 => ans=10. M=10: total=30, min sum=13, extra=17, caps: 0,9,9,9, sum caps=27, extra 17 <=27, works. Could we do M=9? 9<10 fails max. So ans=10.
All good.
Now, could there be scenario where ceil(sum/(n-1)) > max(ai) but M=ceil(...) is not feasible because some person might be forced to play more than M? No, because we can always keep that person's play count at exactly ai (or less than M). But wait, we need sum of all xi = (n-1)M. If we set each xi to its minimum ai, we get sum = S. If S < (n-1)M, we need to add extra plays to some persons. Those persons will have xi > ai. But they can be any persons. So we can avoid adding extra to persons that already have ai = max(ai) maybe to avoid exceeding M? But if max(ai) is less than M, then any person can increase up to M. So we can distribute extra among those whose caps are positive. So it's fine.
Thus solution is trivial.
Now, is there any trick where sum/(n-1) rounding up yields M, but we need to consider that M must be at least max(ai), and if M is less than max, we take max, but we also need to check that (n-1)*max >= sum. Actually if we take M = max(ai) because it's larger than ceil(sum/(n-1)), then (n-1)*max >= sum? Not necessarily. Example: n=3, a=[100,1,1], sum=102, max=100, ceil(sum/2)=51, so M=100. (n-1)max = 2100 =200 >=102, so ok. If we have n=3, a=[100,100,1] sum=201, ceil(201/2)=101, max=100, so answer=101. Because max=100, ceil=101, answer=101. If we had taken max=100, condition (n-1)*100=200 < 201, so fails sum condition. So we need the larger of the two.
Thus final answer.
Now, let's formalize in code.
C++:
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
int maxa = 0;
for (int i=0; i<n; ++i) {
int a;
cin >> a;
sum += a;
if (a > maxa) maxa = a;
}
// compute ceil( sum / (n-1) )
long long rounds_from_sum = (sum + n - 2) / (n - 1);
long long ans = max((long long)maxa, rounds_from_sum);
cout << ans << endl;
return 0;
}
Check for n=3, a=[3,2,2]: sum=7, rounds_from_sum=(7+1)/2=4, max=3, ans=4.
n=4, a=[2,2,2,2]: sum=8, rounds_from_sum=(8+2)/3=10/3=3, ans=3.
Edge: n=3, a=[1,1,1]: sum=3, (3+1)/2=2, ans=2.
All good.
But note: There is a possibility of integer overflow: sum + n - 2 could exceed 2^63? sum up to 1e14, plus 1e5 ~ still far below 9e18. So fine.
Now, is there any subtlety about the answer being too large for int? Must use long long.
Thus final answer.
But wait, is it possible that n=1? Constraint says n>=3, so fine.
Now, after confirming, produce final answer.
Thus the minimal number of rounds is max(max(ai), ceil(S/(n-1))). That's the solution.
Now, the problem originally is from Codeforces, many solutions exist.
Let's double-check with some random test from CF discussion: I recall a test: n=5, a=[5,5,5,5,5] => sum=25, ceil(25/4)=7, max=5, ans=7. That seems correct.
Another test: n=3, a=[1,2,2] => sum=5, ceil(5/2)=3, max=2, ans=3.
But could there be a scenario where ceil(S/(n-1)) > max(ai) but still not enough because the extra needed might force some person to exceed M? Let's try n=4, a=[1,1,1,4] sum=7, ceil(7/3)=3, max=4, so ans=4. But what if we try M=3? M=3 fails max because person4 needs at least 4, so impossible. So ans=4. But what about scenario where max is low but extra distribution may require someone to exceed M? For example, n=4, a=[2,2,2,2] sum=8, ceil=3, max=2, M=3 works as we saw. Now consider n=4, a=[3,3,0? but ai>=1]. So not.
Consider n=4, a=[3,1,1,1] sum=6, ceil(6/3)=2, max=3 => ans=3. M=3: total=9, min=6, extra=3, caps: 0,2,2,2, sum caps=6, extra 3 possible: give extra to person2=2, person3=1 => works. M=2: fails max.
Consider n=5, a=[1,1,1,1,5] sum=9, ceil(9/4)=3, max=5 => ans=5. That's fine.
Now consider scenario where sum/(n-1) is integer, but max is lower, and maybe we can't allocate because extra would force someone to exceed M? But M = sum/(n-1) >= max? Not necessarily: if max is lower than sum/(n-1) then M = sum/(n-1) > max. So M > max, so all caps are positive, extra distribution is possible. Example: n=4, a=[3,3,3,0? invalid]. Okay.
Thus, solution stands.
Now I will produce final answer with explanation.
But as per instruction, we need to output single integer. So that's it.
Now before finalizing, I want to ensure that the formula for ceil is correct: (sum + (n-2)) / (n-1). Because when sum is exactly divisible by (n-1), we want sum/(n-1). (sum + n-2) // (n-1) gives (k*(n-1) + n-2)//(n-1) = k + (n-2)//(n-1) = k. Good. When sum = k*(n-1) + r with 1 <= r <= n-2, then (sum + n-2) = k*(n-1) + r + n-2 = k*(n-1) + (r + n-2). Since r + n-2 is in [n-1, 2n-4]. For n>=3, 2n-4 >= n-1? For n=3, 2n-4=2, n-1=2, so maximum is 2n-4 = 2(n-2) = 2n-4. Is it always less than 2(n-1)? For n>2, 2n-4 < 2n-2 = 2(n-1). So r+n-2 < 2(n-1). So (r+n-2)//(n-1) = 1 if r+n-2 >= n-1, which is true because r>=1 gives r+n-2 >= n-1. So result = k+1. Good. So formula works for n>=3.
Now, final code.
But maybe we need to consider using long long for (n-2) as well to avoid overflow? It's fine.
Now, double-check edge case: n=3, a=[1000000000, 1000000000, 1000000000] sum=3000000000, rounds_from_sum = (3000000000 + 1) // 2 = 1500000000.5? Actually 3000000000 is 3e9, plus 1 = 3000000001, //2 = 1500000000 (since integer division truncates) but we need ceil(3e9/2)=1.5e9=1500000000, so correct. max=1e9, ans=1500000000. That works.
Now, possible scenario: sum may be up to 10^14, n up to 10^5, so rounds_from_sum up to ~10^14 / 2 (if n=3) => 5e13, which fits in 64-bit.
Thus solution works.
Now, deliver final answer.
But the problem might also be solved via binary search. But closed form is better.
Thus, I will produce the solution and explanation accordingly.
In the final answer, I will provide explanation with reasoning, then code.
But the user asked: "One day n friends gathered together to play "Mafia". ... Please, do not use the %lld specifier ... Output ...". So they likely want code.
Thus, final.
```cpp
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
int maxA = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
sum += a;
maxA = max(maxA, a);
}
// ceil( sum / (n-1) )
long long roundsFromSum = (sum + n - 2) / (n - 1);
long long answer = max(roundsFromSum, (long long)maxA);
cout << answer << endl;
return 0;
}