We are given:
n entrances (2 ≤ n ≤ 1e5). Each entrance i has a_i people initially. Each minute, each entrance lets one person from its queue enter (if any). So each minute, for each i, if a_i > 0 then a_i decreases by 1 (since one person enters). But Allen is also present, initially at the back of queue at entrance 1. Allen's strategy: At each minute, if he is at the front of the queue (i.e., he is the first in that queue and that entrance allows entry that minute), then he enters and process ends. If he is not allowed (i.e., there is at least one person ahead of him in that queue, so he is not the first), then after that minute (or at the end of the minute? Let's parse carefully.)
Description: "Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance)."
Interpretation: At the start of minute, presumably each entrance processes one person from the front of its queue. So if Allen is at the front of a queue at the start of a minute, then he will be allowed in during that minute (since one person enters, and if he is first, he enters). If he is not at the front, then during that minute he is not allowed in, so after that minute (or maybe after the processing?), he leaves that queue and moves to the next entrance, appending himself at the end of its queue.
But need to define timing: At minute 1, initially Allen stands at end of queue of entrance 1. At that moment, there are a_1 people ahead. During minute 1, entrance 1 will process one person (the front). Allen is not the front (since a_1 >=0; if a_1=0 then queue is empty and Allen is the only one? But including Allen? Actually initial a_i are number of people excluding Allen. So if a_1=0, then the queue at entrance 1 initially has only Allen (but a_1=0). In that case, at the start, Allen is at the front? Actually he is at the end, but if queue is empty, then being at the end means he is the only one, so he is at the front as well? Typically if you are the only person, you are both front and back. So we need to clarify: At minute 1, before processing, Allen is at the end of queue. If a_1=0, then the queue length is 1 (Allen). So he is the first. Then during minute 1, he should be allowed in. According to strategy: "Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves ..." So if he is the first, then he is allowed, so he enters and stops. So for a_1=0, answer would be 1. That seems plausible.
Now if a_1 >0, then there are a_1 people ahead. At minute 1, one of them (the front) is processed and enters, reducing a_1 by 1. Allen does not enter, because he wasn't first. After minute 1 (or at the end of minute), he leaves the queue at entrance 1 and moves to the end of queue at entrance 2 (or to 1 if n=1 but n>=2). Note that after minute 1, a_1 has decreased by 1. When Allen moves to entrance 2, he will be at the end of that queue. But what about the people who were processed from entrance 2 during minute 1? The minute 1 also processed entrance 2: one person from queue 2 entered (if a_2>0). So a_2 decreased by 1. But Allen was not there at that moment; he arrives after minute 1. So he arrives after the processing of minute 1. Thus at the start of minute 2, Allen is at the end of queue 2. The queue at entrance 2 now has some number of people: originally a_2, then one person left, so now a_2 - 1 (if a_2>0) but we must also consider that if Allen is appended, we have that many plus Allen. But note: The description: "stands in the end of the queue of the next entrance." So after moving, he stands at the end of the queue. So effectively, the queue length for entrance 2 for the next minute includes the remaining people (a_2 - 1 if a_2>0, else 0) and Allen at the back. But careful: When Allen arrives after minute 1, does he see the updated queue after the minute's processing? Yes. So at the start of minute 2, the queue of entrance 2 consists of (original a_2 - 1) people (since one entered) plus Allen at the back. But what if a_2 = 0 originally? Then during minute 1, no one entered (since queue empty). After minute 1, the queue is empty. Allen arrives and stands at the end, so now the queue has Allen only. So he will be first at minute 2.
Thus overall, we can think of a process where at each minute, all queues reduce by 1 if they have any people (they let one person in). Then after the minute, if Allen hasn't entered (i.e., he wasn't at the front at that minute), he moves to the next entrance and gets appended to that queue (after the reduction). He continues.
We need to determine which entrance he finally enters from.
Observations:
Allen's journey is deterministic. He cycles through entrances repeatedly until he finally gets to the front of some queue at a minute and enters.
Let’s denote the queues' initial lengths (excluding Allen) as a_i. Over time, these lengths decrease by 1 each minute as long as they are positive? Actually careful: At each minute, for each entrance i, if there is at least one person in its queue, then exactly one person enters (the front). So the length decreases by 1 if it was >0. But if there are 0, it stays 0. So it's like a_i(t) = max(a_i - t, 0)? Not exactly, because the reduction happens each minute for each queue, independent of Allen's presence. Indeed, each entrance processes one person per minute if there is anyone. So after t minutes, the number of people that have entered from entrance i is min(t, a_i). So the remaining people at entrance i after t minutes (excluding Allen) is a_i - min(t, a_i) = max(a_i - t, 0). So yes, a_i(t) = max(0, a_i - t). So it's independent of Allen's movement. Allen just moves among queues.
Now Allen's position: He starts at entrance 1, at the back of queue. At time 0 (before any minutes), he is at entrance 1, and there are a_1 people ahead. Then minute 1: if a_1 = 0, then he is first, so he enters at entrance 1 immediately? But careful: He is initially at end. At minute 1 processing: if a_1 = 0, then he is the only one. So during minute 1, he enters. So answer 1.
If a_1 > 0, then during minute 1, the first person (not Allen) enters. After minute 1, the number of people ahead of Allen at entrance 1 reduces to a_1 - 1. But Allen is not allowed; he leaves and goes to entrance 2. At this point (after minute 1), the queue at entrance 2 has a_2' = max(0, a_2 - 1) people, and Allen appends. So now at the start of minute 2, Allen is at entrance 2, with number of people ahead = a_2' = max(0, a_2 - 1). If that number is 0, then he is first and will enter during minute 2, so answer 2.
If not, then after minute 2, he moves to entrance 3, etc.
But note: When Allen moves to an entrance after minute t, the queue at that entrance has already been reduced by t minutes of processing. However, Allen's own position in that queue: he is appended at the end, after all existing people. So his "position" relative to the queue is: number of people ahead = current length of that queue (excluding himself) = max(0, a_i - t). Then during the next minute (minute t+1), the queue processes one person from the front. If Allen is at the front (i.e., number ahead = 0), then he enters during minute t+1. Otherwise, after that minute, the number ahead decreases by 1 (since the front person leaves), but note that Allen also stays behind those people? Actually if there were k >0 people ahead, after processing one, there are k-1 people ahead (since one person at front left). But then Allen is not yet at front unless k-1=0. At the end of minute t+1, if Allen didn't enter (k >0), he then moves to next entrance. So his time at entrance i lasts exactly one minute: minute (t+1) where t is the minute count when he arrived. At the start of minute (t+1), he is at entrance i with k = max(0, a_i - t) ahead. Then during minute (t+1), one person leaves from front; if k=0, Allen (being first) leaves (enters). If k>0, then after the minute, the number ahead becomes k-1, but Allen does not move up because he leaves? Actually after the minute, if he didn't enter, he leaves the queue entirely and moves to next entrance. So he does not stay in that queue for subsequent minutes.
Thus we can think of Allen's path as: for t = 0,1,2,... (t is minutes passed, also number of processed minutes so far). At the beginning (t=0), he is at entrance 1, with ahead count = a_1. But we need to check entry at minute 1: He enters if a_1 == 0. In general, at the moment he arrives at entrance i after t minutes have passed (t from 0 to ...), he will be at entrance i = (t mod n) + 1? Let's see: Starting at entrance 1 at time t=0. After minute 1, if he didn't enter, he moves to entrance 2. So arrival at entrance 2 occurs after t=1 minutes have passed. Then minute 2 he stays at entrance 2. If no entry, after minute 2 he moves to entrance 3, etc. So arrival at entrance i (for i from 1 to n) happens at time t = i-1? Actually after t minutes passed, he has had t moves? Let's list:
-
At start (before any minute), he is at entrance 1. Let's call this state at time t=0, before minute 1. He will process minute 1 at entrance 1.
-
After minute 1 (t=1), if not entered, he moves to entrance 2. So at time t=1 (after processing minute 1), he is at entrance 2.
-
Then minute 2 processes at entrance 2. After minute 2 (t=2), if not entered, he moves to entrance 3.
So in general, during minute t+1 (where t = 0,1,2,...), he is at entrance (t mod n) + 1? Let's test: t=0 -> entrance 1; t=1 -> entrance 2; t=2 -> entrance 3; ... t=n-1 -> entrance n; t=n -> entrance 1; etc. Yes.
Now for each minute m (starting from 1), the entrance index i = ((m-1) mod n) + 1. At the start of minute m, Allen is at entrance i. The number of people ahead of him at that entrance is: the remaining people at entrance i after (m-1) minutes of processing (since m-1 minutes have already passed) = max(0, a_i - (m-1)). But also note: does his own previous presence at that queue affect the count? He hasn't been at that queue before this minute (except possibly if he stayed multiple minutes, but he doesn't; he arrives after previous minute). So the queue consists solely of original people that haven't entered yet. Since he is appended at the end after the previous minute's processing, his position is at the end. So at the start of minute m, the number of people ahead is exactly the number of original people remaining at entrance i after (m-1) minutes: r_i = max(0, a_i - (m-1)).
Now during minute m, one person (if any) enters. If r_i = 0, then Allen is at front, so he enters, and we output i. If r_i > 0, then the first among those r_i people enters, so after minute m, the remaining becomes r_i - 1, but Allen leaves, so he doesn't stay.
Thus condition for entrance i at minute m: r_i = max(0, a_i - (m-1)) = 0.
But careful: if r_i = 0, that means at the start of minute m there are no original people ahead. Since Allen is at the end, the queue consists only of Allen? Actually if r_i = 0, then there are no original people remaining. But there may be other people? Only Allen is present because he appended at the end. So yes, the queue has only Allen. So he is first. So he will be allowed in during minute m.
Thus Allen will enter at the first minute m >= 1 such that for i = ((m-1) mod n) + 1, we have a_i - (m-1) <= 0, i.e., a_i <= m-1. But note that a_i is integer, so condition a_i <= m-1 means that after (m-1) minutes, all original people at entrance i have entered. So basically, the minute at which he is at entrance i and that entrance has already emptied (i.e., all original people have entered before minute m) then he will be the next (and only) person there and thus enters.
But wait, need to consider the case when there are zero people at an entrance initially (a_i=0). Then at m=1, i=1: condition a_1 <= 0? No, m-1=0, a_1=0, so a_1 <= 0 is true. So he enters at minute 1. That matches.
For a_i>0, we need to find the smallest m such that a_i <= m-1 for the i that he is at.
But is it possible that he enters even if a_i > m-1? Suppose that at the start, there are some people ahead, but maybe after some minutes of waiting in the same queue (but he doesn't wait, he moves after each minute). Could he enter when he is not at the front? No, only when he is at front. So condition is exactly that.
Thus the answer is: Find the smallest m (starting from 1) such that for i = ((m-1) mod n) + 1, we have a_i <= m-1. Then answer is i.
Alternatively, we can think in terms of "time" t = m-1 minutes passed. So Allen enters at time t (minutes passed) such that at the moment he is about to start minute t+1, he is at entrance i and a_i <= t. Equivalent to: find smallest t >= 0 such that a_{i(t)} <= t, where i(t) = (t mod n) + 1? Let's define: after t minutes have passed, Allen is at entrance i = (t mod n) + 1. But careful: after t minutes, he has just moved? Actually after t minutes, if he hasn't entered, he has just moved to the next entrance. At that point, he is at entrance i = (t mod n) + 1 (since after minute 1, t=1, i=2). But then the next minute (t+1) will be processed at that entrance. However, condition for entry during minute t+1 is that at the start of minute t+1, a_i <= t (since m-1 = t). So that condition is a_i <= t. So we need the smallest t >= 0 such that a_{i(t)} <= t, where i(t) = (t mod n) + 1 (with t starting from 0). Then answer is i(t).
Check example 1: n=4, a=[2,3,2,0]. Compute t=0: i=1, a1=2 >0? condition a1 <= 0? false.
t=1: i=2, a2=3 <=1? false.
t=2: i=3, a3=2 <=2? true (2 <= 2). So answer i=3. That matches output 3.
Example 2: n=2, a=[10,10]. t=0: i=1, 10<=0? false.
t=1: i=2, 10<=1? false.
t=2: i=1, 10<=2? false.
t=3: i=2, 10<=3? false.
...
We need t where condition holds. Since a_i are 10 each, we need t >= 10 and also i(t) such that condition holds. t=10: i = (10 mod 2)+1 = (0)+1 =1? Actually 10 mod 2 =0, so i=1. a1=10 <=10 true. So answer i=1. That matches output 1. Indeed the process described ended with entrance 1? Wait example output says 1. The note shows process until [0,0] then Allen enters? Actually note says after many steps, they show both queues become 0 simultaneously and then Allen enters through entrance 1. That matches.
Example 3: n=6, a=[5,2,6,5,7,4]. Compute t values:
t=0: i=1,5<=0? no.
t=1: i=2,2<=1? no.
t=2: i=3,6<=2? no.
t=3: i=4,5<=3? no.
t=4: i=5,7<=4? no.
t=5: i=6,4<=5? yes. So answer i=6. Output 6. matches.
Thus the problem reduces to: find minimal t >= 0 such that a_{(t mod n) + 1} <= t. Then output the entrance number i = (t mod n) + 1.
Given n up to 1e5, a_i up to 1e9. The t can be as large as something like max(a_i) + n? Possibly up to 1e9 + 1e5. Brute force iterating t from 0 upward until condition holds could be too slow if t is large. But we can compute directly.
Observation: The condition involves each entrance i periodically: It is checked at times t such that t mod n = i-1. Specifically, for entrance i, the condition a_i <= t is checked at times t = i-1, i-1 + n, i-1 + 2n, ... So we need for each i the smallest t in that arithmetic progression that satisfies a_i <= t. That smallest t is: t_i = the smallest t >= i-1 such that t ≡ i-1 (mod n) and t >= a_i. But note that t must be >= a_i? Actually condition is a_i <= t. So the minimal t with that congruence that is >= a_i. However, also t must be at least i-1 (since the first time we check i is at t = i-1). So t_i = the smallest number in the arithmetic progression {i-1 + k*n, k >= 0} that is >= a_i. That is, t_i = i-1 + n * ceil((a_i - (i-1))/n). If a_i <= i-1, then t_i = i-1 (since at the first check it's already satisfied). So t_i = i-1 + n * max(0, ceil((a_i - (i-1))/n). But careful: ceil of non-negative rational. Let d = a_i - (i-1). If d <= 0, then t_i = i-1. Otherwise, t_i = i-1 + n * ceil(d / n).
Then Allen will enter at the smallest t among all t_i (because the process checks each entrance at its times, and the first time any condition becomes true is when Allen enters). And the entrance that achieves that minimal t is the answer. If there are ties (multiple entrances would satisfy at same t), which one does Allen choose? Since at time t, he is at exactly one entrance. But if two different entrances have the same t_i, can that happen? For t to be same, they must satisfy t mod n = i-1 and also j-1, but if i≠j then mod n differs, so same t would give different residues mod n, impossible. So t_i are distinct mod n. However, it's possible that t_i = t_j if they are different? For t to be equal, it would require i-1 ≡ j-1 mod n, which would imply i=j mod n, so i=j since they are in 1..n. So no two different entrances can have the same t_i. Thus the minimal t_i is unique.
Thus answer is the i that minimizes t_i.
Hence we can compute for each i: t_i = i-1 + n * max(0, ceil((a_i - (i-1))/n). Then answer = argmin_i t_i.
But wait, we must confirm that the condition a_i <= t indeed guarantees that at the start of minute t+1, Allen is at entrance i and there are no people ahead. But we also need to consider that when t = i-1, that's the first time Allen visits entrance i. However, could it happen that Allen enters at an earlier t even if a_i > t for all i? That seems impossible because the condition is necessary and sufficient. So the minimal t_i across i gives the entry time.
But is there any subtlety about when Allen moves? Let's double-check with an example: Suppose n=3, a=[0,5,5]. According to our formula: For i=1: a1=0 <= i-1? i-1=0, so t1=0. That would be smallest, answer 1. That matches intuition: At t=0 (before any minutes), Allen at entrance 1, a1=0, so he will enter at minute 1. So answer 1.
Suppose a=[1,0,0]. i=1: t1 = i-1 + nceil((a1 - (i-1))/n) = 0 + 3ceil((1-0)/3)=31=3? Actually ceil(1/3)=1, so t1=3. i=2: t2 = 1 + 3ceil((0-1)/3) but since a2-(i-1) = 0-1 = -1 <=0, so t2= i-1 =1. i=3: t3 = 2 + 3*ceil((0-2)/3)=2 (since negative) => t3=2. The minimal t is t2=1, answer entrance 2. Let's simulate: Start at entrance 1, a1=1 >0, not front. Minute 1: entrance1 processes one (the 1 person). After minute1, Allen moves to entrance2. At that time, a2=0 initially, but after minute1, entrance2 processed one? Wait a2=0 initially, so there was no one to process. So after minute1, entrance2 remains empty. Allen arrives at entrance2 after minute1. At start of minute2, Allen at entrance2 with no one ahead, so during minute2 he enters. So answer 2. t=1 corresponds to after minute1? In our t representation, t is minutes passed. At t=1, we are after minute1. But condition for entry at minute2 is a_i <= t? At t=1, i=2, a2=0 <=1 true. So minimal t is 1, i=2. So matches.
Thus formula seems correct.
But wait: For i=1, t1=3, meaning that at t=3 (after 3 minutes), condition holds for entrance1. Let's simulate: At t=3, after minute3, Allen would be at entrance? Let's see timeline:
t=0: start entrance1.
Minute1: process entrance1; after minute1, Allen moves to entrance2.
t=1: at entrance2.
Minute2: process entrance2? Since a2=0, no one; Allen is front, so he should enter at minute2? That would happen before t becomes 2. According to our condition, at t=1 we found true, so entry at minute2. So indeed he never gets to t=3. So t1=3 is not reached.
Thus algorithm: For each i, compute t_i as above, find i with minimum t_i, output that i.
But careful: The definition of t_i as "the smallest t such that t ≡ i-1 (mod n) and t >= a_i" might be correct, but need to ensure t_i >= i-1? The congruence ensures that t >= i-1 if we consider k>=0. So yes.
Now we can compute t_i efficiently: For each i, compute d = a_i - (i-1). If d <= 0, t_i = i-1. Else, t_i = i-1 + n * ((d + n - 1) // n). Because ceil(d/n) = (d + n - 1)//n.
But is there any possibility that t_i is not minimal among all? Could there be a scenario where Allen enters at a time that is not the minimal t_i? Since the condition is exactly that the entry occurs at the smallest t where a_{i(t)} <= t, and t_i as defined is the smallest t for that particular i that satisfies the condition. The overall smallest t across i will be exactly the entry time. So answer is argmin t_i.
Thus solution is O(n) compute t_i, find min.
But we must ensure we are interpreting the "time" correctly: t = number of minutes passed before the minute in which Allen will attempt entry. Actually the entry occurs during minute t+1. But t_i is the minutes passed before that minute. So the ordering of t_i is same as ordering of entry times. So min t_i gives earliest entry.
Now one more nuance: In the case where multiple entrances have t_i equal? As argued, impossible because t_i mod n is unique. But what if we have two different i with same t? For them to have same t, we need i-1 ≡ j-1 mod n, so i ≡ j mod n, and since both in 1..n, they must be equal. So no ties.
Thus answer is unique.
Let's test with more examples.
Consider n=4, a=[0,10,10,10]. Compute:
i=1: d=0-0=0 => t1=0
i=2: d=10-1=9 => ceil(9/4)=3 => t2=1+43=13
i=3: d=10-2=8 => ceil(8/4)=2 => t3=2+8=10? Actually 2+42=10
i=4: d=10-3=7 => ceil(7/4)=2 => t4=3+8=11
min t=0 => answer 1. That's correct: immediate entry.
Consider n=4, a=[1,0,0,0].
i=1: d=1-0=1 => ceil(1/4)=1 => t1=0+4=4
i=2: d=0-1=-1 => t2=1
i=3: d=0-2=-2 => t3=2
i=4: d=0-3=-3 => t4=3
min t=1 => answer 2. Simulate: start entrance1, a1=1 >0, minute1: process one (the 1 person), after minute1 Allen moves to entrance2. At start minute2, entrance2 empty, enters => answer 2. Good.
Consider n=2, a=[0,1].
i=1: d=0 => t1=0
i=2: d=1-1=0? Actually a2=1, i-1=1, d=0 => t2=1
min t=0 => answer 1. Simulate: start entrance1, a1=0, so he enters immediately at minute1. So answer 1. That's fine.
Consider n=2, a=[1,1].
i=1: d=1 => ceil(1/2)=1 => t1=0+2=2
i=2: d=1-1=0 => t2=1
min t=1 => answer 2. Simulate: start entrance1 (a1=1). minute1: process entrance1, one person enters, Allen not front. After minute1, move to entrance2. At that time, a2 initially 1 but processed during minute1? Actually minute1 also processed entrance2: one person from entrance2 entered (since a2=1). So after minute1, entrance2 becomes 0. Allen arrives to entrance2 after minute1. At start minute2, entrance2 empty -> enters. So answer 2. Correct.
Consider n=3, a=[2,0,0].
i=1: d=2 => ceil(2/3)=1 => t1=0+3=3
i=2: d=0-1=-1 => t2=1
i=3: d=0-2=-2 => t3=2
min t=1 => answer 2. Simulate: t0 entrance1, a1=2 >0. minute1: process entrance1, reduces to 1. Allen not front. After minute1 move to entrance2. At that time entrance2 originally 0, processed minute1? a2=0 so no change. So entrance2 empty. So minute2: Allen at entrance2 front -> enters. So answer2.
Consider n=3, a=[3,0,0].
i=1: d=3 => ceil(3/3)=1 => t1=0+3=3
i=2: d=0-1=-1 => t2=1
i=3: d=0-2=-2 => t3=2
min t=1 => answer2. Simulate: minute1: process entrance1, reduces from 3 to 2, Allen not front. After minute1 move to entrance2 (empty). minute2: enters. Correct.
Consider n=3, a=[4,0,0].
i=1: d=4 => ceil(4/3)=2 => t1=0+6=6
i=2: t2=1
i=3: t3=2
min t=1 => answer2. Simulate: minute1: reduce a1 to 3, Allen not front, move to entrance2 empty, minute2 enters. Works.
Consider n=3, a=[2,1,0].
i=1: d=2 => ceil(2/3)=1 => t1=3
i=2: d=1-1=0 => t2=1
i=3: d=0-2=-2 => t3=2
min t=1 => answer2. Simulate: start 1: a1=2 >0, minute1: process1 -> a1 becomes 1. Not front. After minute1 move to entrance2. At that moment, a2 originally 1, but minute1 processed entrance2: one person entered, so a2 becomes 0. So Allen arrives to entrance2 which is empty. Minute2: enters. So answer2. Correct.
Consider n=3, a=[2,2,0].
i=1: t1=3 (2-> ceil(2/3)=1=>3)
i=2: d=2-1=1=> ceil(1/3)=1=> t2=1+3=4
i=3: t3=2 (since d<=0)
Min t=2? t3=2. So answer 3. Simulate: minute1: process entrance1 (a1=2->1), Allen not front, after minute1 move to entrance2. At start of minute2, what's queue at entrance2? a2 initially 2, minute1 processed entrance2: one entered, so a2 becomes 1. Allen arrives after minute1, so at start of minute2, entrance2 has 1 person ahead (the remaining original), plus Allen at back. So Allen not front. Minute2: process entrance2: the front person (the original) enters, leaving a2 = 0, but Allen still there? Actually during minute2, if Allen is not front, then the front person (the original) enters, and after minute2, Allen (still not front? after the front leaves, if there was exactly 1 ahead, then after that minute the queue becomes empty and Allen would be front now? But according to strategy, if he is not allowed during the minute (meaning he wasn't first at the start), he leaves after the minute. So at the start of minute2, there was 1 ahead. He is not first, so he is not allowed. After minute2, he leaves (regardless of whether he becomes first after that minute). He does not wait to see if he becomes first. He leaves immediately after the minute ends, before any potential shift. So after minute2, he leaves entrance2 and goes to entrance3. So he doesn't get to benefit from the queue emptying during that minute unless he was first at the start. So in this case, after minute2, he moves to entrance3. At that moment, entrance3 originally 0, processed minute2? minute2 also processed entrance3: since a3=0, no change. So entrance3 empty. Then minute3: at entrance3, he is front, enters. So answer 3. That matches t3=2 (since t=2 is after minute2? Wait our condition: t is minutes passed. At t=2 (after minute2), Allen is at entrance3? Actually after minute2, he moves to entrance3, so at t=2 he is at entrance3. Condition for entry at minute3 is a3 <= 2? a3=0 <=2 true. So minimal t where condition holds is t=2 for i=3. Indeed t3=2, which is less than t2=4 and t1=3. So answer 3. So simulation matches.
Thus the formula holds.
Now we must confirm the case where a_i is large and t_i computed might be large but within 64-bit. Since a_i up to 1e9, n up to 1e5, t_i could be up to roughly (i-1) + n * ceil((1e9 - (i-1))/n) <= (n-1) + n * (1e9/n + 1) <= n-1 + 1e9 + n ≈ 1e9 + 2e5. So within 32-bit? 1e9+2e5 ~ 1.0002e9 < 2^31 (~2.147e9). So int works if using 32-bit signed? But safe to use 64-bit (long long) to avoid overflow when computing n * ceil(...). Since n up to 1e5 and ceil up to about 1e9/n ~ 1e4, product up to 1e9, which fits in 32-bit, but if a_i = 1e9, n=1e5, then ceil((1e9 - (i-1))/n) could be ceil(approx 1e4) = 10000, product = 1e510000=1e9, still fits. But if a_i=1e9, i-1 small, n=1e5, d ≈ 1e9, ceil(d/n) = ceil(10000) exactly 10000 if d%n=0 else 10001 maybe? Let's compute max: d max = a_i - (i-1) could be as high as 1e9 (if i-1=0). Then ceil(d/n) = ceil(1e9 / 1e5) = ceil(10000) = 10000 if exactly divisible? 1e9 / 1e5 = 10000 exactly because 1e9 = 10000 * 100000? 10000100000=1,000,000,000 indeed. So product = 1e5 * 10000 = 1e9. But if d=1e9-1 then ceil = 10000? 1e9-1 / 1e5 = 9999.99999 -> ceil = 10000, product = 1e5*10000 = 1e9 still. So t_i max about 1e9 + n ≈ 1e9+1e5. That fits 32-bit signed (max 2,147,483,647). So we can use 64-bit or 32-bit. But in C++ int is usually 32-bit, up to 2e9, so okay. However, to avoid any risk, we can use long long.
Now we need to compute ceil((a_i - (i-1))/n) for positive difference. Actually we can compute k = (a_i - (i-1) + n - 1) / n. But careful: if a_i - (i-1) is exactly divisible, then ceil = that quotient. But we want to multiply by n and add i-1: t_i = i-1 + n * k.
But note: If a_i - (i-1) <= 0, then k = 0, t_i = i-1.
We must be careful with integer overflow in intermediate (a_i - (i-1) + n - 1) may be up to about 1e9 + 1e5, still safe.
Thus solution: Iterate i from 1 to n, compute t_i, track min_t and ans_i.
But is there any case where t_i is not actually the entry time because Allen might have entered earlier due to being at front even if a_i > t? Wait, condition a_i <= t is necessary and sufficient? Let's double-check with some scenario: Suppose at entrance i at start of minute m, there are some people ahead (r_i > 0). Could it be that after processing the front person during minute m, and if r_i = 1 (so exactly one ahead), then after that minute, the queue would become empty (since the only person ahead leaves). However, Allen leaves immediately after the minute if he was not allowed, so he doesn't get to be front after the minute. Could the rule be interpreted differently? The problem statement: "Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance)." So it says: During the minute, if he is not allowed, then he leaves after the minute? Actually "if he is not allowed into the fan zone during the minute ... he leaves the current queue and stands in the end of the queue of the next entrance." That implies that at the end of the minute (or after determining he wasn't allowed), he moves. So he does not wait to see if the queue becomes empty. So indeed, the only way to enter is to be first at the start of the minute.
Thus condition is correct.
Now we should also consider if Allen might be at the front even if a_i > t? That would require that the queue at entrance i has zero original people but maybe there are other people? The only people are original ones and Allen. So a_i > t means at time t, there are a_i - t > 0 original people remaining (since a_i - t > 0). So there are at least one original person ahead. So Allen cannot be front. So condition a_i <= t is necessary and sufficient.
Thus solution is straightforward.
But wait: Could there be a scenario where Allen enters at entrance i at time t even if a_i > t, because the queue length at entrance i at the start of minute is not exactly a_i - t? But that's the number of original people remaining. However, what about the people who moved from other entrances? There are no other people moving; only Allen moves. The original people stay in their own queues; they never move. So the queue at entrance i consists solely of original people (except when Allen is there). So yes.
Thus solution stands.
Now let's verify with some random tests via simulation for small n and small a_i, to be sure.
We'll simulate mentally:
n=3, a=[1,2,1].
Compute t_i:
i1: d=1-0=1, ceil(1/3)=1 => t1=0+3=3.
i2: d=2-1=1, ceil(1/3)=1 => t2=1+3=4.
i3: d=1-2=-1 => t3=2.
Min t=2 => answer 3.
Simulate:
t0: entrance1, a1=1.
Minute1: process entrance1: the 1 person enters, a1 becomes 0. Allen not front initially (since 1 ahead) -> not allowed. After minute1, move to entrance2.
t=1: at entrance2. At this time, a2 initially 2, processed minute1: one enters -> a2 becomes 1. So queue at entrance2: 1 original + Allen at back. So not front.
Minute2: process entrance2: the 1 original enters, a2 becomes 0. Allen not front at start, so not allowed. After minute2, move to entrance3.
t=2: at entrance3. At this time, a3 initially 1, processed minute2: one enters? minute2 also processed entrance3: a3=1 -> one enters -> a3 becomes 0. So after minute2, entrance3 empty. Allen arrives to empty queue. At start of minute3, entrance3 empty -> Allen front -> enters during minute3. So answer entrance3. t=2 condition: at t=2, i=3, a3=1 <=2? 1 <=2 true. So correct. t_i for i=3 is 2, matches.
Now check if any earlier t could be possible: At t=1, i=2, a2=2 >1, false. At t=0, i=1, a1=1 >0 false. So indeed min t=2.
Now n=3, a=[2,2,2]:
i1: d=2 => ceil(2/3)=1 => t1=3
i2: d=2-1=1 => ceil(1/3)=1 => t2=4
i3: d=2-2=0 => t3=2? Actually d=0 => t3= i-1 =2? But d=0 => means a3 <= i-1? a3=2, i-1=2, so a3=2 <=2 true, so t3=2.
Min t=2 => answer 3.
Simulate:
t0: entrance1 (2)
Minute1: process1: reduces to 1. Allen not front (2 ahead). After minute1 -> entrance2.
t1: entrance2, a2 initially 2, processed minute1 -> reduces to 1. So queue: 1 original + Allen. Not front.
Minute2: process2: reduces to 0 (the one original leaves). Allen not front (since at start there was 1 ahead). After minute2, Allen moves to entrance3.
t2: entrance3, a3 initially 2, processed minute2? minute2 processes entrance3: reduces from 2 to 1. So after minute2, a3=1? Wait careful: minute2 processing: all entrances process. So at minute2, entrance3 also processes one person, so after minute2, a3 becomes 1 (since started at 2). But Allen arrives after minute2, so at t=2, entrance3 has a3=1 remaining. But condition says a3 <=2, which is true because a3=1 <=2. So at start of minute3, will Allen be front? The queue at entrance3 has 1 original person and Allen at back. So there is 1 ahead -> not front. So he would not enter at minute3. Hmm conflict: t3=2 suggests that at t=2 (after minute2), Allen is at entrance3 and condition a3 <=2 holds, meaning that at the start of minute3, he should be front? Wait we derived condition: at start of minute m (where m = t+1), Allen is at entrance i, and the number of original people ahead is a_i - t (since t minutes passed). If a_i <= t, then that number is <=0. But if a_i <= t, it could be that a_i - t <= 0, meaning there are zero or negative original people remaining. But if a_i - t = 0, that means exactly zero people remain, so Allen is the only one. If a_i - t < 0, that would mean more minutes have passed than original people, but that's not possible because we can't have negative remaining; it's max(0, a_i - t). But in our derivation we used a_i - t, but if a_i - t < 0 then the actual remaining is 0. So the condition for Allen to be front is that the actual remaining = 0, i.e., a_i - t <= 0. So if a_i <= t, then remaining = 0. That means at start of minute t+1, there are no original people ahead. So he is front.
But in the simulation above, at t=2, a3=2, but after 2 minutes, we computed a3 after 2 minutes as max(0, 2-2)=0? Wait earlier I said minute2 processing reduces a3 from 2 to 1, but that is after 2 minutes? Let's carefully compute time:
We need to define t as number of minutes elapsed. At t=0 (before any minutes), a3=2.
Minute 1: processing reduces a3 from 2 to 1. After minute 1, t=1, a3=1.
Minute 2: processing reduces a3 from 1 to 0. After minute 2, t=2, a3=0.
Thus after 2 minutes, a3 = 0. Indeed a3 - t = 0. So at t=2, after minute2, a3=0. But earlier I erroneously said a3 becomes 1 after minute2? That's because I forgot that minute2 processing reduces from 1 to 0. Yes, correct. So after minute2, a3=0. So when Allen arrives after minute2, the queue at entrance3 is empty. So at start of minute3, Allen is front. So he will enter at minute3. Good.
Now check if Allen can enter at minute3? At start of minute3, he is front. So he enters. So answer 3. Works.
So simulation matches t3=2.
Now consider n=3, a=[2,3,1]:
i1: d=2 -> ceil(2/3)=1 -> t1=3
i2: d=3-1=2 -> ceil(2/3)=1 -> t2=4
i3: d=1-2=-1 -> t3=2
min t=2 -> answer 3.
Simulate:
t0: ent1 (2)
min1: process1 -> 1 remains. Allen not front -> after min1 move to ent2.
t1: ent2; a2 initially 3, after min1 -> 2 remains. So queue: 2 ahead + Allen.
min2: process2 -> 1 remains (front leaves). Allen not front -> after min2 move to ent3.
t2: ent3; a3 initially 1, after min2 -> 0? a3=1, min1: a3->0? Actually min1 also processed ent3: a3=1 -> after min1 becomes 0. Wait careful: minute1 processed all entrances: ent3 with a3=1 -> becomes 0. So after minute1, a3=0. Minute2 also processes ent3: but since a3=0, it stays 0. So after minute2, a3=0. So at t2, ent3 empty. So start min3: Allen front -> enters. Answer 3. Works.
Now consider edge case where a_i is huge, but t_i computed may be large. The algorithm O(n) will be fine.
Now we must double-check that the t_i formula gives the smallest t for each i. For a given i, we need smallest t >= i-1 such that t ≡ i-1 mod n and t >= a_i. Since a_i could be less than i-1, then the smallest t is i-1 (since t must be at least i-1 and satisfy congruence). That's correct.
But is it possible that Allen could enter at an entrance i at a time t' that is not congruent to i-1 mod n? Because he visits entrance i only at times t where t mod n = i-1 (after t minutes passed). So yes.
Thus answer is argmin of t_i.
Now we have to output the entrance number.
Thus solution seems trivial.
But wait, the problem statement: "Allen wants to enter a fan zone that occupies a round square and has n entrances. There already is a queue of a_i people in front of the i-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute. Allen uses the following strategy to enter the fan zone:
- Initially he stands in the end of the queue in front of the first entrance.
- Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance)."
The phrase "round square" might imply something about circular arrangement but not important.
Thus answer is straightforward.
But I recall that this is a known Codeforces problem: "Queue at the School"? Actually it's Codeforces Round #... Problem? Possibly "Allen and fans" or something. It might be from CF Round #... but I'm not sure. However, the typical solution is to compute t_i = (a_i - i + n) // n? Actually many solutions use a different approach: For each i, compute (a_i - i + n) / n (integer division) and then find min of something. Let's derive that.
We have t_i = i-1 + n * ceil((a_i - (i-1))/n) for a_i > i-1. If a_i <= i-1, t_i = i-1.
Let's denote k_i = max(0, ceil((a_i - (i-1))/n)). But perhaps we can compute an effective "value" that is monotonic with t_i. Another approach: For each i, compute the number of full cycles needed: (a_i - i + 1 + n - 1) // n? Because some solutions use: For each entrance i, compute (a_i - i) // n? Let's check.
Given t_i = i-1 + n * ceil((a_i - (i-1))/n). For a_i > i-1, define cycles = ceil((a_i - (i-1))/n). Then t_i = i-1 + ncycles. For a_i <= i-1, t_i = i-1 = i-1 + n0.
Now we could think in terms of "time" measured in cycles. But to compare t_i, we might compute something like (a_i - i + n) // n? Actually define value = ceil((a_i - i + 1)/n)? Not exactly.
Simplify: Let’s define for each i, compute x_i = (a_i - i + n) // n? Let's test.
Suppose we want to compute cycles = ceil((a_i - (i-1))/n). This equals floor((a_i - (i-1) + n - 1)/n). So cycles = (a_i - (i-1) + n - 1) // n.
Then t_i = i-1 + n * cycles = i-1 + n * ((a_i - (i-1) + n - 1) // n).
Now note that comparing t_i is equivalent to comparing (cycles, i-1) perhaps? Since t_i = i-1 + n * cycles. Since i-1 < n, the ordering of t_i is determined primarily by cycles, then by i-1 if cycles tie? Actually if cycles are different, the one with smaller cycles yields smaller t_i, regardless of i-1. If cycles equal, then the smaller i-1 yields smaller t_i. But could cycles be equal for different i? Yes. For example, a_i = [5,2,6,5,7,4] from sample 3. Compute cycles? Let's compute:
i=1: a1=5, i-1=0, d=5, cycles = ceil(5/6)=1? n=6 => ceil(5/6)=1. t1=0+6*1=6.
i=2: a2=2, i-1=1, d=1, cycles = ceil(1/6)=1? Actually ceil(1/6)=1? No, ceil(1/6)=1? Since 1/6>0, the smallest integer >= 0.1 is 1. So cycles=1, t2=1+6=7.
i=3: a3=6, i-1=2, d=4, cycles=ceil(4/6)=1, t3=2+6=8.
i=4: a4=5, i-1=3, d=2, cycles=ceil(2/6)=1, t4=3+6=9.
i=5: a5=7, i-1=4, d=3, cycles=ceil(3/6)=1, t5=4+6=10.
i=6: a6=4, i-1=5, d=-1? Actually a6=4, i-1=5 => d = -1 <=0 => cycles=0, t6=5+0=5. So min t=5 => i=6.
But cycles for i=6 is 0, which is smaller than others' cycles=1. So indeed min cycles.
Thus we can compute for each i the "cycle count" cycles_i = max(0, ceil((a_i - (i-1))/n)). Then t_i = i-1 + n * cycles_i.
We need to find i minimizing t_i. Since t_i is increasing in cycles_i primarily, and for same cycles_i, it's increasing in i-1. So we can compute for each i, the value cycles_i. But careful: For some i, a_i <= i-1, cycles_i=0. Among those, t_i = i-1. So the minimal t among these would be the smallest i such that a_i <= i-1? Actually t_i = i-1. Since i-1 increases with i, the smallest t_i among cycles_i=0 is for the smallest i that satisfies a_i <= i-1. That could be the answer if such i exists early. For example, in sample 1, i=4 has a4=0 <=3, so cycles_i=0, t4=3. But i=3 gave t3=2, which is smaller, despite cycles_i=1? Actually for i=3, a3=2, i-1=2 => d=0 => cycles_i=0? Wait a3=2, i-1=2, d=0, so cycles_i=0 as well? Because d=0 <=0, so cycles_i=0. So i=3 has cycles_i=0, t3=2. So indeed i=3 is cycles_i=0. So sample1: i=1: a1=2>0 -> d=2 >0 cycles=ceil(2/4)=1? Actually n=4, a1=2, i-1=0 => d=2, cycles=ceil(2/4)=1, t1=4. i=2: a2=3, i-1=1 => d=2, cycles=ceil(2/4)=1, t2=5. i=3: a3=2, i-1=2 => d=0 => cycles=0, t3=2. i=4: a4=0, i-1=3 => d=-3 => cycles=0, t4=3. So min t=2 at i=3. So answer 3.
So indeed cycles_i can be zero for many i.
Thus straightforward computation works.
Potential pitfalls:
- Use 0-based or 1-based indexing correctly.
- Ensure we compute ceil correctly for positive numbers.
Now, we should also consider whether there is any off-by-one in the t definition relative to a_i. Let's re-derive with a bit more formalism to confirm.
Define t = number of minutes that have passed (i.e., number of processing minutes completed). At time t (right after t minutes have been processed), Allen moves to entrance i = (t mod n) + 1 (if he hasn't entered yet). Then minute t+1 begins: He is at entrance i, and the number of original people still in that queue is max(0, a_i - t). If max(0, a_i - t) = 0, then he is first and will enter during minute t+1. So the entry condition is a_i <= t. So we need the smallest t such that a_{ (t mod n) + 1 } <= t.
Now, we want to find the minimal t satisfying this. Our t_i approach finds for each i the minimal t with that residue that satisfies a_i <= t. That yields the earliest t for that residue. Then the global minimum among those t_i gives the overall minimal t. That is correct because for any t that satisfies the condition, it corresponds to some i = (t mod n)+1, and for that i, t is at least the minimal t_i for that residue. So the minimal over i of t_i yields the overall minimal t.
Thus answer is argmin_i t_i.
Now implement:
- Read n.
- Read array a[1..n] (0-index or 1-index).
- Initialize best_time = large (maybe LLONG_MAX), best_entrance = 0.
- For i from 1 to n:
idx = i (1-indexed).
// compute t_i
let offset = i-1.
if (a_i <= offset) {
t_i = offset;
} else {
// d = a_i - offset > 0
// cycles = (d + n - 1) / n (integer division ceiling)
long long cycles = (a_i - offset + n - 1) / n;
t_i = offset + cycles * n;
}
if (t_i < best_time) {
best_time = t_i;
best_entrance = i;
}
- Print best_entrance.
That's all.
But there is nuance: In the else case, we used cycles = (a_i - offset + n - 1) // n. This works for positive d. However, careful: d could be negative? We have else only if a_i > offset, so d >0.
Now confirm with sample 2: n=2, a=[10,10].
i=1: offset=0, a1=10>0 => cycles = (10-0+2-1)//2 = (11)//2 =5? 11//2=5 (since integer division). So t1=0+52=10.
i=2: offset=1, a2=10 >1 => d=9, cycles = (9+2-1)//2 = (10)//2=5, t2=1+52=11.
best_time=10, best_entrance=1. Output 1.
Matches.
Another test: n=4, a=[0,0,0,0]
i=1: offset=0, a1=0 <=0 => t1=0.
i=2: offset=1, a2=0 <=1 => t2=1.
i=3: offset=2, a3=0 <=2 => t3=2.
i=4: offset=3, a4=0 <=3 => t4=3.
best=0, ans=1. That's correct: immediate entry.
Edge: n=2, a=[1000000000, 0]
i=1: offset=0, a1=1e9 >0 => cycles = (1e9+2-1)//2 = (1000000001)//2 = 500000000? Actually (1e9+1)//2? 1e9=1000000000, +1 =1000000001, integer division by 2 gives 500000000 (since 2500000000=1000000000, remainder 1). So t1=0+5000000002=1000000000.
i=2: offset=1, a2=0 <=1 => t2=1.
best=1, ans=2. Simulate: start entrance1 (1e9 people). After minute1, reduces to 1e9-1, Allen not front, moves to entrance2 (empty after processing minute1? a2=0 remains 0). At start minute2, entrance2 empty, Allen front -> enters. So answer 2. Good.
Edge: n=2, a=[0, 1000000000]
i=1: t1=0, i=2: offset=1, a2=1e9 >1 => cycles = (1e9-1+2-1)//2 = (1e9)//2? Actually 1e9 -1 = 999999999, +1 = 1000000000? Wait d = a2 - offset = 1e9 -1 = 999999999. cycles = (999999999 + 2 - 1)//2 = (1000000000)//2 = 500000000. t2 = 1 + 500000000*2 = 1000000001. best=0, ans=1. So he enters at entrance1 immediately.
All good.
Thus solution is O(n).
But wait, let's check a scenario where a_i = 0 for all i except one, but that one is large. Example: n=5, a=[0,0,0,0,100]. According to formula:
i=1: t1=0
i=2: t2=1
i=3: t3=2
i=4: t4=3
i=5: offset=4, a5=100>4 => cycles = (100-4+5-1)//5 = (100)//5? 100-4=96, +4=100, //5=20, t5=4+20*5=104.
Min t=0 -> ans=1. So he enters at entrance1. That's correct: immediate entry.
Now scenario: n=5, a=[1,0,0,0,0]. i=1: offset=0, a1=1>0 => cycles = (1+5-1)//5 = (5)//5=1, t1=0+5=5. i=2: t2=1, i=3:2, i=4:3, i=5:4. Min t=1 -> ans=2. Simulate: minute1 at entrance1 (1 person ahead) -> not front, after minute1 move to entrance2 (empty) -> minute2 enters at entrance2. So answer 2. Good.
Now scenario: n=5, a=[2,0,0,0,0]. i=1: offset=0, a1=2>0 => cycles = (2+4)//5? Actually (2+5-1)//5 = (6)//5=1, t1=5. i=2: t2=1, i=3:2, i=4:3, i=5:4. min=1->ans=2. Simulate: minute1: entrance1 reduces to 1, Allen not front, move to entrance2 empty, minute2 enters. So ans=2. t2=1 matches.
Now scenario: n=5, a=[5,0,0,0,0]. i=1: d=5, cycles = (5+4)//5 =9//5=1? Actually (5+5-1)=9, //5=1, t1=5. i=2: t2=1. So ans=2. But simulation: minute1: entrance1 reduces to 4, Allen not front -> move to entrance2 empty. minute2: at entrance2 empty -> enters. So ans=2. So even if a1=5, he still enters at entrance2. So not necessarily the smallest initial queue.
Now scenario: n=5, a=[5,1,0,0,0].
i=1: d=5, cycles= (5+4)//5=9//5=1 -> t1=5.
i=2: offset=1, a2=1 <=1? Actually a2=1, offset=1 => d=0, so t2=1.
i=3: t3=2, i=4:3, i=5:4. Min t=1 -> ans=2. Simulate: minute1: entrance1 reduces to 4; Allen not front -> move to entrance2. At that time, a2 initially 1, processed minute1: reduces to 0. So entrance2 empty. minute2: Allen at entrance2 front -> enters. So ans=2. Good.
Now scenario: n=5, a=[5,2,0,0,0].
i=1: t1=5
i=2: offset=1, a2=2>1 => d=1, cycles=(1+4)//5=5//5=1 => t2=1+5=6.
i=3: t3=2
i=4:4? Actually i=4 offset=3, a4=0<=3 => t4=3.
i=5: t5=4.
Min t=2 -> ans=3. Simulate: minute1: entrance1->4, Allen not front -> move to entrance2. At that time, a2=2 -> processed minute1 -> becomes 1. So queue at entrance2: 1 original + Allen. Minute2: process entrance2: the 1 original enters, becomes 0. Allen not front at start (since 1 ahead) -> not allowed. After minute2 move to entrance3. At that time, a3=0 (processed minute2? Actually minute2 also processed entrance3: a3=0 stays 0). So entrance3 empty. minute3: Allen at entrance3 front -> enters. So ans=3. t3=2 matches.
Thus all good.
Now implement.
But wait: Could there be a scenario where a_i = 0 but i-1 is large, t_i = i-1, but that's not minimal because some other entrance with earlier t_i might be smaller. So we need to compute all.
Now consider the possibility that t_i can be huge, but we can compute.
Now, is there any scenario where the minimal t_i is not the actual entry time due to Allen potentially entering at a different entrance because of some weird interaction where he might be at front earlier due to the queue at an entrance emptying exactly at the moment he arrives? But we already accounted: At time t, when he arrives, the queue is after t minutes of processing. If a_i - t = 0, then empty. So condition holds.
Now ensure that the condition a_i <= t is exactly the condition that a_i - t <= 0. Since a_i and t are integers, a_i <= t means a_i - t <= 0. That implies that the remaining original people is max(0, a_i - t) = 0. So he is front. So it's correct.
Thus solution is done.
But we could also think about alternative approach: For each i, compute the number of full cycles before Allen can enter at that entrance: cycles = ceil((a_i - i + 1)/n). But maybe simpler to compute as above.
Let's derive another expression: Let's define x_i = ((a_i - i + n) // n)? Actually many solutions compute something like (a_i - i) // n? Let's check.
We have t_i = i-1 + n * ceil((a_i - (i-1))/n). Define k_i = ceil((a_i - (i-1))/n). Then t_i = i-1 + n*k_i. For comparison, we can compute something like val_i = k_i * n + (i-1). But some may define a value w_i = (a_i - i) // n? Because if we compute floor division: floor((a_i - i)/n)? Not straightforward.
But anyway.
Now just implement.
Potential pitfalls: Since we need to output entrance number, and there is only one answer.
Now we can double-check with some larger random test via simulation code mentally? Not needed.
Thus answer: compute t_i and take min.
But wait, there might be a twist: The problem says "Each entrance allows one person from its queue to enter the fan zone in one minute." That could also be interpreted that each minute, only one entrance is allowed? But that doesn't make sense because "each entrance" suggests each entrance operates independently. The description of queues and Allen moving across entrances suggests all entrances process simultaneously. The examples confirm this: they show all queues decreasing each minute. So it's as we thought.
Now, there is a nuance: The queues' lengths given are a_i, not including Allen. When Allen stands in a queue, does he count as a person for the purpose of the queue? Yes, he is a person. But the entrance allows one person from its queue per minute. So if he is at the front, he will be allowed. So correct.
Now, confirm that our t_i computation for a_i = 0 and i-1 = something is correct. For i such that a_i = 0, offset = i-1. Since a_i <= offset? 0 <= i-1, yes. So t_i = i-1. That means at time t = i-1 (after i-1 minutes have passed), Allen will be at entrance i, and he will find it empty and will enter at minute i. That matches: Starting at entrance 1, after each minute he moves forward; after i-1 minutes, he has moved to entrance i (since at t=0 he's at entrance1, after 1 minute at entrance2, after i-1 minutes at entrance i). At that moment, the queue is empty (since a_i=0 and t = i-1, so a_i - t = -i+1? Actually max(0, a_i - t)=0). So he enters at minute i. That seems plausible. But is it always that if a_i=0, he will definitely enter at entrance i after i-1 minutes? Check n=4, a=[0,0,0,0]. According to our min t_i is t1=0, so he enters at entrance1. That's earlier than t4=3. So answer 1, not 4. Because he checks entrance1 first. So if a1=0, he enters immediately, doesn't even go to entrance4. So t_i for i>1 are larger and not minimal. So the global min picks earliest.
Now, what about if a1 is huge, but a2=0? Then t2=1 (since offset=1, a2=0 <=1). So he will enter at entrance2 at minute2, as long as no earlier entrance meets condition. t2=1 is less than t1 which is at least? t1 is offset=0 + n*ceil(a1/n). For a1 >0, t1 >= n? Actually if a1=1, t1 = n? Wait n=5, a1=1 => cycles = ceil(1/5)=1 => t1=5. So t2=1 is less, so answer 2. That matches simulation: at minute1 he's at entrance1 with someone ahead, after minute1 moves to entrance2 empty, minute2 enters. So he doesn't wait for entrance1 to clear. So indeed, he may enter earlier than waiting for the first entrance to clear.
Thus algorithm is fine.
Now I should think if there is any scenario where the minimal t_i is not unique? As argued, it's unique because residues mod n are distinct. But what if two entrances have the same t? Suppose n=3, i=1 t1=2? But t1 mod 3 = 0? Actually t1 would have residue 0 mod n? t1 = i-1 + n*cycles, so t1 mod n = i-1. So different i give different residues mod n. If t_i = t_j, then i-1 ≡ j-1 mod n, so i=j. So uniqueness holds.
Thus answer is unique.
Now implement.
One more nuance: In our t_i formula for a_i > offset, we compute cycles = (a_i - offset + n - 1) / n. But note that if a_i - offset is exactly divisible by n, cycles = (a_i - offset)/n. Example: a_i = offset + kn. Then condition a_i <= t? At t = offset + kn, we have a_i = offset + kn, so a_i <= t holds (equal). So he would enter at that t? Let's check: At t = offset + kn, after t minutes, he arrives at entrance i. Since a_i = offset + kn, then a_i - t = 0, so empty, so he will enter at next minute. So that t is valid. Could there be an earlier t for same i? The earlier times with same residue are t' = offset + (k-1)n, which is less than a_i (since a_i = offset + kn). Then a_i > t', so condition fails. So the minimal t is indeed offset + kn. So our formula correct.
Now implement in C++:
#include
using namespace std;
int main() {
int n;
cin >> n;
long long best_time = 1e18; // large
int ans = 0;
for (int i = 1; i <= n; ++i) {
long long a;
cin >> a;
long long offset = i - 1;
long long t;
if (a <= offset) {
t = offset;
} else {
long long d = a - offset;
// ceil(d / n)
long long cycles = (d + n - 1) / n;
t = offset + cycles * n;
}
if (t < best_time) {
best_time = t;
ans = i;
}
}
cout << ans << endl;
return 0;
}
Works.
Now check with sample 3: n=6, a=[5,2,6,5,7,4].
Compute:
i=1: offset=0, a=5>0, d=5, cycles=(5+5)/6? Actually (5+5)/6 =10/6=1 (integer division), t=0+6=6.
i=2: offset=1, a=2>1? a=2 >1? yes, d=1, cycles=(1+5)/6=6/6=1, t=1+6=7.
i=3: offset=2, a=6>2? yes, d=4, cycles=(4+5)/6=9/6=1, t=2+6=8.
i=4: offset=3, a=5>3? yes, d=2, cycles=(2+5)/6=7/6=1, t=3+6=9.
i=5: offset=4, a=7>4? yes, d=3, cycles=(3+5)/6=8/6=1, t=4+6=10.
i=6: offset=5, a=4 <=5? 4<=5 true, t=5.
best_time=5, ans=6. Output 6.
Matches.
Now, we must consider if there is any scenario where the t_i computed as above is not actually reachable because Allen might have entered earlier at another entrance, thus the process stops before reaching that t. But we are selecting the minimal t_i, which is the earliest possible entry time. So it's the one that actually occurs. So it's correct.
Thus solution is O(n). The constraints are up to 1e5, so O(n) is fine.
Now, we should think about if there is any possible misinterpretation of the problem: Could the movement happen before the processing of the minute? The description: "Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance)."
One might interpret that at the start of each minute, he checks if he is first; if not, he immediately moves to the next entrance before that minute's processing? But the phrase "during the minute" suggests that the check is whether he was allowed during that minute. That is, if he is not allowed during the minute, then after the minute he moves. The examples support our interpretation because they show the queues decreasing each minute and Allen moving after the minute.
Let's examine the note for example 1: "the number of people (not including Allen) changes as follows: [2, 3, 2, 0] → [1, 2, 1, 0] → [0, 1, 0, 0]. The number in bold is the queue Allen stands in." They show the first transition after minute1: [2,3,2,0] → [1,2,1,0]. That indicates that after minute1, all queues decreased by 1 (if possible). And they mention "the number in bold is the queue Allen stands in." They show presumably that initially he is at queue1 (first element bold?), but they didn't show bold in text. But the note: "We see that he will enter the fan zone through the third entrance." So they likely simulated as we did.
Thus our interpretation matches the example.
Now, double-check: In example 2, they show a series of transitions: [10,10] → [9,9] → ... → [0,0]. And they mention "In the second example the number of people (not including Allen) changes as follows: ..." They don't show bold, but they end with [0,0]. At that point, after those minutes, both queues become 0 simultaneously. Then Allen enters? Actually they might indicate that after the queues become [0,0], Allen is at entrance1? Let's simulate according to our model: n=2, a=[10,10]. According to our earlier calculation, t=10 is the first t such that a_{i} <= t. At t=10, i=1, a1=10 <=10 true. So he will enter at minute11? Wait our t is minutes passed before the minute of entry. At t=10, after 10 minutes, he arrives at entrance1? Let's simulate step by step:
Start: t=0, entrance1.
Minute1: process; after minute1, t=1, move to entrance2.
Minute2: process; after minute2, t=2, move to entrance1.
...
We need to find when he enters. According to our condition, entry occurs at minute t+1 where t is first such that a_{i(t)} <= t.
We computed t=10, i=1. So at t=10 (after 10 minutes), he is at entrance1, and a1 <=10. Since a1=10, a1 - t =0, so queue1 empty. So at start of minute 11, he is front, so he enters during minute11. However, the example note says the numbers change to [0,0] after some steps, and then presumably he enters. How many minutes to get to [0,0]? Starting [10,10], each minute both decrease by 1 until both become 0 after 10 minutes. So after minute10, the queues become [0,0]. Indeed after 10 minutes (t=10), we have [0,0]. So at that moment (after minute10), Allen is at entrance? Let's track:
t (minutes passed) and his location after moving:
We can compute his location at the start of each minute:
Minute1: at entrance1 (t=0)
Minute2: at entrance2 (t=1)
Minute3: at entrance1 (t=2)
Minute4: at entrance2 (t=3)
Minute5: at entrance1 (t=4)
Minute6: at entrance2 (t=5)
Minute7: at entrance1 (t=6)
Minute8: at entrance2 (t=7)
Minute9: at entrance1 (t=8)
Minute10: at entrance2 (t=9)
Minute11: at entrance1 (t=10)
So at start of minute11, he is at entrance1 and both queues are [0,0]. So he enters during minute11. But the example note ends with [0,0] and doesn't explicitly say when he enters, but they say "In the second example the number of people ... changes as follows: [10,10] → [9,9] → ... → [0,0]." That suggests that after 10 transitions, the queues become [0,0], but they don't show further. Probably they imply that after that, Allen will be at entrance1 and will enter. And the output is 1, so indeed he enters via entrance1. So consistent.
Thus all good.
Now we can also think of a more efficient solution: compute for each i the value of cycles, etc.
But given the simplicity, we implement as above.
Now, verify extreme case: n=1e5, a_i = 1e9 for all i. Compute t_i for i=1: offset=0, d=1e9, cycles = ceil(1e9 / n). n=100000, ceil(1e9/1e5)= ceil(10000)=10000. So t1=0+10000100000=1e9. i=2: offset=1, d=1e9-1, cycles = ceil((1e9-1)/1e5) = ceil(9999.99999)=10000, t2=1+10000100000=1e9+1? Actually 1 + 10000100000 = 1 + 1e9 = 1000000001. So t1=1000000000, t2=1000000001, ... i=100000: offset=99999, d=1e9-99999 = 999900001? Actually 1e9 - 99999 = 999900001. cycles = ceil(999900001/100000) = ceil(9999.00001) = 10000? Wait 999900001 / 100000 = 9999.00001, ceil = 10000. So t_i = 99999 + 10000100000 = 99999 + 1e9 = 1000999999? That is larger than t1. So the smallest t is for i=1, t1=1e9. So answer would be 1. Is that correct? Let's simulate conceptually: All entrances have huge queues. He starts at entrance1. Each minute, he moves to next entrance. He will only enter when he finds a queue that has emptied. Since all queues are same huge size, the first queue to empty will be entrance1 after a1 minutes? Actually entrance1 processes one per minute; it will become empty after a1 minutes (1e9 minutes). But he is not staying at entrance1; he moves each minute. However, he may encounter a queue that empties earlier because it had a smaller initial size? But here all are equal. The earliest any queue empties is after a_i minutes. That's 1e9 minutes. But entrance1 empties at t=1e9 (after 1e9 minutes). But at that moment, where is Allen? Let's compute t=1e9 mod 2? Actually with n=100000, at t=1e9, Allen will be at entrance (t mod n)+1 = (1e9 mod 100000)+1. 1e9 = 100000*10000, so remainder 0, so entrance1. So at t=1e9, he arrives at entrance1 just as it empties (since a1=1e9). So he will be front and enter at next minute. That matches t1=1e9. Could he enter earlier? Possibly if some other entrance empties earlier due to having a smaller a_i? But all are same, so no. So answer 1. So correct.
Now if there is variation, the one with minimal t_i will be answer.
Thus solution stands.
Now I think the problem is solved.
But wait: There is another interpretation: Could the queues also include Allen in the count for processing? For example, when he stands in a queue, does that count as a person for the purpose of "allows one person from its queue"? Yes, but that doesn't affect the remaining original people. So our modeling is correct.
Now, I'm thinking about potential pitfalls with integer division and large numbers. Use long long for t_i and cycles.
One more nuance: In the else branch, we compute cycles = (a_i - offset + n - 1) / n. This is integer division. But if a_i - offset is a huge positive number, adding n-1 may cause overflow if we use 32-bit ints. But we will use long long, so fine.
Now, we should also consider the possibility that best_time could be as high as 1e9+1e5, which fits in 32-bit signed? 1e9+1e5 = 1,000,100,000 which is less than 2^31-1 = 2,147,483,647. So int (32-bit) could hold it. However, cycles * n could be up to 1e9, and offset up to 1e5, sum fits in int. But to be safe, we can use long long.
Now code in Python also trivial.
Now before finalizing, double-check with some more random scenarios via mental simulation or logic.
Consider n=3, a=[0,5,5]. Our formula: i1: t1=0; i2: offset=1, a2=5>1 => d=4, cycles=ceil(4/3)=2? Wait (4+2)//3=6//3=2, t2=1+23=7; i3: offset=2, a3=5>2 => d=3, cycles=ceil(3/3)=1, t3=2+3=5? Actually 2+13=5. So min t = 0 -> ans=1. So he enters immediately at entrance1. That makes sense because a1=0.
Consider n=3, a=[1,5,5]. i1: offset=0, a1=1>0 => cycles=ceil(1/3)=1, t1=3; i2: offset=1, a2=5>1 => d=4, cycles=ceil(4/3)=2, t2=1+6=7; i3: offset=2, a3=5>2 => d=3, cycles=ceil(3/3)=1, t3=2+3=5; min t=3? Actually t1=3, t3=5, so min=3 -> ans=1. So he would enter at entrance1? But is that correct? Simulate: start entrance1 (a1=1). Minute1: process entrance1, the one person enters, queue becomes empty. Allen not front at start? He had 1 ahead, so not front. After minute1, he moves to entrance2. At that time, entrance2 originally 5, processed minute1 -> becomes 4. So entrance2 has 4 people. Minute2: Allen at entrance2, not front (4 ahead). After minute2, moves to entrance3. At that time, entrance3 originally 5, processed minute2? After minute2, entrance3 becomes 3 (since processed twice). So entrance3 has 3 people. Minute3: Allen at entrance3, not front (3 ahead). After minute3, moves to entrance1. At that time, entrance1 is empty (since processed three times? Actually after minute3, entrance1 has been processed three times? Starting a1=1, after minute1 became 0; minutes2 and 3, since a1=0, it remains 0). So entrance1 empty. So at start of minute4, Allen at entrance1, empty -> enters. So he enters at entrance1 at minute4? Wait we said entry at minute4. But our t for entrance1 was 3, which means at t=3 (after minute3), he is at entrance1 and condition holds (a1 <= 3). That would mean he enters at minute4. Yes, our condition gives entry at minute t+1 = 4. So answer entrance1. That matches. So correct.
Now consider n=3, a=[2,2,2]. Our earlier gave answer 3. Let's recalc: i1: offset=0, a1=2 => cycles=ceil(2/3)=1, t1=3; i2: offset=1, a2=2 => d=1, cycles=ceil(1/3)=1, t2=4; i3: offset=2, a3=2 => d=0 => t3=2; min t=2 => ans=3. Simulate to confirm entry at entrance3 at minute3? According to t=2, entry at minute3. Let's simulate step by step:
Minute1 (t=0 start): entrance1, queue: [2] (people). Allen not front. After minute1, entrance1 ->1, Allen moves to entrance2. At that moment, entrance2 processed minute1 -> from 2 to 1. So entrance2 has 1 person.
Minute2: entrance2, queue has 1 original + Allen. Allen not front (1 ahead). After minute2, entrance2 processes that 1 person -> becomes 0. Allen moves to entrance3. At that moment, entrance3 processed minute2? Starting a3=2, minute1: to 1, minute2: to 0. So entrance3 empty.
Minute3: entrance3, empty -> Allen front, enters. So entrance3, minute3. Good.
Thus answer 3.
Now consider n=3, a=[3,3,3]. i1: offset=0, a1=3 => cycles=ceil(3/3)=1, t1=3; i2: offset=1, a2=3 => d=2, cycles=ceil(2/3)=1, t2=4; i3: offset=2, a3=3 => d=1, cycles=ceil(1/3)=1, t3=5? Wait offset=2, cycles=1 -> t3=2+3=5? Actually 2+3=5. But t1=3, t2=4, t3=5. So min t=3 -> ans=1. So entrance1 is answer. Simulate:
Minute1: entrance1, queue 3 -> after minute1 becomes 2, Allen not front -> move to entrance2 (after minute1, entrance2 from 3 to 2)
Minute2: entrance2, queue 2 + Allen, not front -> after minute2, entrance2 becomes 1, move to entrance3 (after minute2, entrance3 from 3 to 1? Actually minute1: a3->2, minute2: a3->1)
Minute3: entrance3, queue 1 + Allen, not front -> after minute3, entrance3 becomes 0, move to entrance1 (after minute3, entrance1 from 2 to 1? Actually minute3 processes entrance1: from 2 to 1)
Minute4: entrance1, queue 1 + Allen? Actually at start of minute4, Allen at entrance1. What is the queue at entrance1? After minute3, a1=1 (since started 3, processed three times: minute1,2,3). So there is 1 original person ahead. So not front. After minute4, that last person enters, a1 becomes 0. Allen moves to entrance2.
Minute5: entrance2, after minute4, a2? Let's track: a2 started 3, minute1:2, minute2:1, minute3:0? Wait minute3 also processes entrance2? Yes, all entrances processed each minute. So a2 after minute3: from 1 -> 0. So after minute3, a2=0. Minute4 processes entrance2: stays 0. So after minute4, a2=0. So at start of minute5, entrance2 empty. So Allen would enter at minute5 at entrance2. But according to our predicted answer entrance1 at minute4? Wait we got min t=3 for entrance1, meaning entry at minute4 (t+1=4). But simulation suggests at minute4, entrance1 had 1 person ahead, so Allen not front, so he would not enter at minute4. Let's simulate carefully from beginning.
We need to simulate correctly with simultaneous processing each minute.
We have n=3, a=[3,3,3].
Define time steps:
At time t=0 (before any minute), Allen at entrance1. Queues: [3,3,3].
Minute 1 (processing):
- Entrance1: front person (one of the 3) enters. Queue becomes 2.
- Entrance2: front person enters -> becomes 2.
- Entrance3: front person enters -> becomes 2.
Allen is in queue1 at the back, not front. So after minute1, he is not allowed. So after minute1, he leaves queue1 and moves to entrance2, appending at the end.
State after minute1 (t=1):
- Queues: a1=2, a2=2, a3=2.
- Allen now at entrance2, at the back. So entrance2 queue now: 2 original + Allen => effectively length 3 but original count is 2.
Minute 2:
- Process all entrances:
Entrance1: front person enters -> a1 becomes 1.
Entrance2: front person (original) enters -> a2 becomes 1 (since one original leaves). Now queue2: remaining original count =1, Allen still at back (now after one original left, Allen is still behind the remaining original). Since Allen was at back, after the front leaves, he is now second? Actually if queue had [orig1, orig2, Allen] (orig1 front, then orig2, then Allen), after orig1 leaves, queue becomes [orig2, Allen]. So Allen is now second, still not front.
Entrance3: front person enters -> a3 becomes 1.
Allen at entrance2, not front at start of minute2. So after minute2, he is not allowed, so he leaves entrance2 and moves to entrance3 (appending).
State after minute2 (t=2):
- Queues: a1=1, a2=1, a3=1.
- Allen at entrance3, at the back. So entrance3 queue: 1 original + Allen.
Minute 3:
- Process all:
Entrance1: front enters -> a1 becomes 0.
Entrance2: front enters -> a2 becomes 0.
Entrance3: front (original) enters -> a3 becomes 0. Now queue3: originally had 1 original + Allen; after processing, the original leaves, leaving only Allen (since Allen was at back, after the only original leaves, Allen becomes the only person, i.e., front). However, Allen was not front at the start of minute3 (since there was one original ahead). So he is not allowed during minute3. The rule says: if he is not allowed during the minute, he leaves after the minute. But what if during the minute, after the front person leaves, he becomes first? Does that matter? The rule says "if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue)" then he leaves. So it is based on whether he was allowed during the minute. Since he was not first at the start, he was not allowed. So regardless of whether he becomes first after the front leaves, he still leaves after the minute. So after minute3, he leaves entrance3 and moves to entrance1 (since it's circular).
State after minute3 (t=3):
- Queues: a1=0, a2=0, a3=0.
- Allen now at entrance1, appending to an empty queue. So entrance1 queue now only Allen.
Minute 4:
- At start of minute4, Allen at entrance1, queue empty => he is first. So he is allowed during minute4, and enters.
Thus he enters at entrance1, minute4. So answer entrance1. Good! Our simulation earlier mistakenly thought after minute3, entrance1 had 1 person, but actually it became 0 after minute3 (since it was processed). Let's recount: a1 started 3, after minute1:2, minute2:1, minute3:0. Yes. So at t=3 after minute3, a1=0. So Allen at entrance1 finds empty queue and enters at minute4. That matches t1=3. So our earlier quick simulation error corrected.
Thus answer entrance1 is correct.
Now, consider a scenario where two entrances have same minimal t? As argued impossible.
Now, is there any scenario where the minimal t_i is not reachable because Allen would have entered earlier at a different entrance with the same t? But t_i are distinct, so the minimal t_i is unique and would be the first time any condition becomes true. Since the process checks conditions at each t in increasing order, the smallest t where condition holds will be when Allen actually enters.
Thus solution stands.
Now we can also consider a more direct formula: t_i = ((a_i - i + n) / n) * n + (i-1) maybe? Actually from our expression: if a_i > i-1, t_i = i-1 + n * ceil((a_i - (i-1))/n). Write ceil((a_i - (i-1))/n) = floor((a_i - i + n)/n)? Because (a_i - (i-1) + n - 1) // n = (a_i - i + n) // n? Let's check: (a_i - (i-1) + n - 1) = a_i - i + 1 + n - 1 = a_i - i + n. Yes! So cycles = (a_i - i + n) // n. Because (a_i - (i-1) + n - 1) // n = (a_i - i + n) // n.
Let's verify: For a_i > i-1, we have cycles = (a_i - i + n) // n. For example, n=4, i=1, a1=2: a_i - i + n = 2 - 1 + 4 = 5, //4 =1 => cycles=1, t1=0+4=4. Works. i=1, a1=5: 5-1+4=8, //4=2, t1=0+8=8? But earlier we computed for a1=5 with n=4: offset=0, d=5, cycles=ceil(5/4)=2, t1=8. Yes.
But note: This formula also gives correct cycles when a_i <= i-1? Let's test: i=3, a3=2, n=4: a_i - i + n = 2 - 3 + 4 = 3, //4 = 0 (since integer division). That would give cycles=0, t_i = i-1 + n*0 = 2. But our earlier condition for a_i <= offset would set t_i = offset =2. So indeed this formula also works when a_i <= i-1? Check i=3, offset=2, a3=2, then a_i <= offset? 2<=2 true. The formula yields cycles = (2-3+4)//4 = 3//4 =0, t_i=2. Good. For i=4, a4=0, n=4: a_i - i + n = 0-4+4=0, //4=0, t_i=3. But offset=3, a4=0 <=3, so t_i=3. Works.
But what about a_i - i + n being negative? For i=1, a1=0, n=4: 0-1+4=3 positive, //4=0, t1=0. That's fine. For i=2, a2=0: 0-2+4=2, //4=0, t2=1. Good. So the expression (a_i - i + n) // n yields 0 when a_i - i + n < n, i.e., a_i - i < 0 => a_i < i. Actually if a_i < i, then a_i - i + n < n, integer division by n yields 0. That corresponds to cycles=0. If a_i >= i, then a_i - i + n >= n, and division yields >=1. But careful: If a_i = i-1, then a_i - i + n = n-1, //n = 0, cycles=0, t_i = i-1. That's correct because condition a_i <= i-1 is equality? Actually if a_i = i-1, then a_i <= offset? offset = i-1, so a_i = offset, so t_i = offset. Good. So the formula t_i = (i-1) + n * ((a_i - i + n) // n) works for all cases. Let's test with a_i = i (e.g., i=2, a2=2, n=4): a_i - i + n = 2-2+4=4, //4=1, t_i=1+4=5. Is that correct? offset=1, a2=2 >1, d=1, cycles=ceil(1/4)=1, t_i=1+4=5. Yes.
Thus we can simplify: For each i (1-indexed), compute t_i = (i-1) + n * ((a_i - i + n) // n). But careful: This uses integer division in C++ which truncates toward zero for positive numbers, which is fine since numerator is non-negative? a_i - i + n could be negative if a_i is very small and i large? For i = n, a_i could be 0, then a_i - i + n = 0 - n + n = 0, non-negative. For i = n, a_i = 0, it's 0. For any i, a_i >= 0, so a_i - i + n >= -i + n >= -(n-1) + n = 1? Actually the minimum occurs when a_i = 0 and i = n: then value = 0 - n + n = 0. If a_i = 0 and i = n-1: 0 - (n-1) + n = 1. So minimum is 0. So numerator is always >=0. So safe.
Thus t_i = (i-1) + n * ((a_i - i + n) / n) integer division.
Let's test with i=1, a1=0: (0-1+ n)/n = (n-1)/n = 0 (since integer division), t=0. good.
Thus we can compute simply.
But note: This formula yields t_i possibly equal to i-1 + n * floor((a_i - i + n)/n). Since (a_i - i + n)/n integer division is floor for non-negative integers. That's equivalent to ceil((a_i - (i-1))/n) as derived? Let's verify equivalence: Let x = a_i - (i-1). Then ceil(x/n) = floor((x + n - 1)/n). Now x + n - 1 = a_i - (i-1) + n - 1 = a_i - i + n. So floor((a_i - i + n)/n) = ceil(x/n). Yes. So it's consistent.
Thus we can compute t_i = (i-1) + n * ((a_i - i + n) / n). But careful: In C++ integer division, for positive numerator, (a_i - i + n)/n yields floor division. Since numerator is int and n is int, but we must ensure we use integer types.
But we have to consider that a_i up to 1e9, i up to 1e5, so a_i - i + n can be as large as 1e9 + 1e5, still fits in 32-bit signed? 1e9+1e5=1000100000 < 2^31, so ok. However, to avoid overflow if we use int for a_i (which is up to 1e9, fits in 32-bit signed), then a_i - i + n might be negative? Actually minimum is 0, maximum is ~1e9+1e5, both within int range. So we can use int.
But if a_i is 1e9 and i=1, n=1e5, then a_i - i + n = 1e9 -1 + 1e5? Actually i=1: -i = -1, +n = +1e5, so sum = 1e9 -1 + 1e5 = 1000000000 -1 + 100000 = 1000099999, still < 2^31. So fine.
But we might want to use long long to be safe.
Simplify: t_i = (i-1) + n * ((a_i - i + n) / n). However, note that ((a_i - i + n) / n) is integer division. In C++ if we use int, (a_i - i + n) might overflow if we use signed 32-bit? Actually if a_i is 1e9, i small, n=1e5, sum = 1,000,099,999 which is less than 2,147,483,647, so safe. So int is okay. But to be cautious, use long long.
But we can also compute as earlier with if-else.
Now we need to output the entrance with minimal t_i. If tie, we would need to break by something, but tie not possible as argued.
Thus we can implement.
Now, but let's consider a scenario where two t_i might be equal due to overflow? No.
Now, verify the formula with some cases:
n=4, a=[2,3,2,0]:
i=1: (0) + 4 * ((2-1+4)/4) = 4*((5)/4)=41=4, t1=4? But we computed earlier t1=4? Actually earlier we computed t1=4? Let's recalc: earlier for i=1, offset=0, a1=2 >0 => cycles=ceil(2/4)=1, t1=0+4=4. So t1=4. Good.
i=2: (1) + 4 * ((3-2+4)/4) =1+4((5)/4)=1+41=5 -> t2=5.
i=3: (2) + 4 * ((2-3+4)/4)=2+4((3)/4)=2+40=2 -> t3=2.
i=4: (3) + 4 * ((0-4+4)/4)=3+4(0/4)=3 -> t4=3.
Min=2 -> ans=3.
Matches.
Now example 2: n=2, a=[10,10]:
i=1:0+2*((10-1+2)/2)=0+2*((11)/2)=0+25=10
i=2:1+2((10-2+2)/2)=1+2*((10)/2)=1+2*5=11
min=10 -> ans=1.
Example 3: n=6, a=[5,2,6,5,7,4]:
i=1:0+6*((5-1+6)/6)=0+6*((10)/6)=0+61=6? Actually 10/6 integer division =1, so 6.
i=2:1+6((2-2+6)/6)=1+6*(6/6)=1+6=7.
i=3:2+6*((6-3+6)/6)=2+6*((9)/6)=2+61=8? 9/6=1, so 8.
i=4:3+6((5-4+6)/6)=3+6*((7)/6)=3+61=9.
i=5:4+6((7-5+6)/6)=4+6*((8)/6)=4+61=10? 8/6=1.
i=6:5+6((4-6+6)/6)=5+6*((4)/6)=5+6*0=5.
min=5 -> ans=6.
Matches.
Thus formula works.
Now we can implement in a few lines.
We should also consider that using integer division directly may produce correct cycles for a_i <= i-1, as we tested.
Thus algorithm:
- Read n.
- Initialize best = large, ans = -1.
- Loop i from 1 to n:
read a
compute t = (i-1) + n * ((a - i + n) / n) (using integer division)
if t < best:
best = t
ans = i
- print ans
But careful: In C++, (a - i + n) / n: division of ints truncates toward zero, which is fine as numerator non-negative.
But if we use integer types, we must ensure that the product n * ((a - i + n) / n) does not overflow. n up to 1e5, the quotient up to about (a_i + n)/n ≈ 1e9/1e5 = 1e4, product up to 1e9, fits in 32-bit. But to be safe, we can use long long.
But we can also compute as: long long t = (i-1) + n * ((a - i + n) / n). Since (a - i + n) / n is integer, we can cast to long long.
Now, any possible off-by-one due to integer division? Let's test with a_i = i-1 exactly: For i=3, a=2, n=4: a - i + n = 2-3+4=3, /4=0, t=2+0=2. Good.
For a_i = i: i=3, a=3, n=4: a - i + n = 3-3+4=4, /4=1, t=2+4=6? Wait (i-1)=2, plus n*1=4 => 6. But we earlier computed? Let's compute manually: offset=2, a=3>2, d=1, cycles=ceil(1/4)=1, t=2+4=6. So t=6. Good.
Now check a_i = i + n - 1: i=3, a=3+3=6, n=4? Actually i + n -1 = 3+3=6. a - i + n = 6-3+4=7, /4=1, t=2+4=6. But is t=6 correct? offset=2, a=6, d=4, cycles=ceil(4/4)=1, t=2+4=6. Good. For a_i = i + n: i=3, a=3+4=7, then a - i + n = 7-3+4=8, /4=2, t=2+8=10. offset=2, a=7, d=5, cycles=ceil(5/4)=2, t=2+8=10. Good.
Thus formula works.
Now we can code.
But wait: Could there be any situation where a_i - i + n is negative? As argued, min is 0 when a_i=0 and i=n. For i=n, a_i=0 gives 0 - n + n = 0. So non-negative. For other combos, it's positive. So safe.
Thus final solution is simple.
Now we can also think about if n is large and we use integer division, but be careful about using (a - i + n) might be a large integer, but fine.
Now I'll produce final answer.
But before finalizing, let's think about a corner case: n=2, a=[0, 1000000000]. i=1: t = 0 + 2*((0-1+2)/2) = 2*((1)/2)=2*0=0? Actually (0-1+2)=1, /2 integer =0, t=0. So ans=1. That's correct.
Now n=2, a=[1, 1000000000]. i=1: t=0+2*((1-1+2)/2)=2*((2)/2)=21=2. i=2: t=1+2((1000000000-2+2)/2)=1+2*(1000000000/2)=1+2*500000000=1000000001. min=2 ans=1? But earlier we might have thought answer maybe 2? Let's simulate: a1=1, a2=1e9. Minute1: entrance1: the 1 person enters, Allen not front (1 ahead). After minute1, move to entrance2. At that time, entrance2 processed minute1: from 1e9 to 1e9-1? Still huge. So entrance2 has huge queue. Minute2: entrance2, not front, after minute2 move to entrance1 (after minute2, entrance1: from 0? Actually a1 after minute2? a1 after minute1 became 0, minute2 stays 0). So after minute2, entrance1 empty. At start of minute3, Allen at entrance1 empty -> enters at minute3. So he enters at entrance1. That's answer 1. So t1=2 means entry at minute3, correct. So ans=1.
Now what about a1=2, a2=1? n=2. i=1: t1=0+2*((2-1+2)/2)=2*((3)/2)=21=2. i=2: t2=1+2((1-2+2)/2)=1+2*((1)/2)=1+2*0=1. min=1 ans=2. Simulate: a1=2, a2=1. Minute1: entrance1: one enters -> a1=1, Allen not front -> move to entrance2 after minute1. At that time, entrance2 processed minute1: from 1 to 0. So entrance2 empty. Minute2: Allen at entrance2 empty -> enters at minute2. So answer 2. Good.
Thus algorithm passes.
Now, we should also consider if the input numbers can be up to 10^9, but we use int, which typically holds up to 2^31-1=2,147,483,647, so it's safe. But we must ensure to use long for t to avoid overflow when adding. However, product n * ((a_i - i + n) / n) is at most n * ((a_i + n)/n) ≈ a_i + n, but could be slightly more if ceil? Actually t_i = i-1 + n * ceil((a_i - (i-1))/n). Since ceil((a_i - (i-1))/n) ≤ (a_i - (i-1) + n - 1)/n, so t_i ≤ i-1 + a_i - (i-1) + n -1 = a_i + n - 1. So t_i ≤ a_i + n - 1. Since a_i ≤ 1e9, t_i ≤ 1e9 + 1e5. So within 32-bit signed. So we can store best_time in int.
But to be safe with multiplication, we can compute t = i-1 + 1LL * n * ((a - i + n) / n) or just use long long. In C++, it's fine to use long long.
Now finalize solution.
Potential alternative interpretation: Could the minute when Allen moves happen before the processing? For completeness, let's consider the alternative: At the start of each minute, he checks if he is first; if not, he immediately moves to the next entrance and then that entrance processes one person (including possibly him if he is now first). This would change the dynamics. Let's test with example 1 under that interpretation.
Alternative: At minute 1 start, he is at entrance1, not first (since a1=2 >0). So he moves to entrance2 before processing. Then minute1 processes: entrance2 (where he is now) processes one person from its queue. He is at the end, not first, so not allowed. After minute1, maybe he moves again? But the description says "Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance." This wording suggests the check occurs during the minute, not before. The phrase "during the minute" indicates the allowance is evaluated based on his status at the start of the minute (or during the processing). So I think our interpretation is correct. Additionally, the example transitions show that the queues all decrease each minute, and they track his location after the minute. The note: "The number in bold is the queue Allen stands in." They show after each transition, the bold number corresponds to the queue he is in after moving. In the first example, they show transitions: [2,3,2,0] → [1,2,1,0] → [0,1,0,0]. They likely show state after each minute. The bold indicates his queue after the minute. At first step, after minute1, he is at queue2? Actually they mention "The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance." They didn't specify bold in text, but presumably the bold numbers show his position. In the second transition, they show [0,1,0,0] with bold on the third element? Actually we can't see but we trust our interpretation because it matches output.
Thus the solution is correct.
Now we can produce final answer.
Let's write the solution in Python as well for completeness, but the problem is from Codeforces and we can implement in any language.
Now finalize.
But before concluding, consider if there's any nuance with very large a_i and n such that a_i - i + n might exceed 2^31? For n=1e5, a_i=1e9, i=1: 1e9 -1 +1e5 = 1000099999 < 2^31. So fine.
Now code.
One more check: n=100000, a_i all = 10^9. i=100000: a_i - i + n = 1e9 - 100000 + 100000 = 1e9. So integer division by n yields 10000 (since 1e9/1e5 = 10000). t_i = (100000-1) + 100000*10000 = 99999 + 1e9 = 1000999999. That's less than 2^31. So int safe.
Thus we can implement.
Now I'll write final solution.
In C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long best = 1e18;
int ans = -1;
for (int i = 1; i <= n; i++) {
long long a;
cin >> a;
long long t = (i-1) + n * ((a - i + n) / n);
if (t < best) {
best = t;
ans = i;
}
}
cout << ans << endl;
return 0;
}
We used (a - i + n) / n. Since all values are positive, integer division works.
Alternatively, using if-else:
if (a <= i-1) t = i-1; else t = i-1 + n * ((a - (i-1) + n - 1) / n);
Both fine.
Now test with sample.
Edge: n=2, a=[0,0] -> i=1: t=0; i=2: t=1; ans=1. He enters immediately.
Now, let's test a scenario: n=5, a=[100,1,100,100,100]. According to formula:
i=1: t=0+5*((100-1+5)/5)=5*((104)/5)=5*20=100? Actually 104/5=20 (int division), t=100.
i=2: offset=1, a=1 <=1? Actually a2=1, i-1=1, so a2<=1 true, t=1.
Thus ans=2. Simulate: minute1: entrance1: processes 1, reduces to 99, Allen not front -> after minute1 move to entrance2. At that time, a2 initially 1, processed minute1? Since a2=1, minute1 processed entrance2: one person enters -> a2 becomes 0. So entrance2 empty. At start of minute2, Allen at entrance2 empty -> enters. So answer 2. Good.
Thus algorithm correct.
Now final answer.
But there is also possibility to solve via finding min over (a_i + n - i) // n? Let's see: Since t_i = i-1 + n * ceil((a_i - (i-1))/n). If we factor something: Define k_i = ceil((a_i - (i-1))/n). Then t_i = i-1 + n*k_i. We can also consider value v_i = (a_i - (i-1) + n - 1) // n. Then t_i = i-1 + n * v_i. So to compare t_i, we could compare v_i primarily, then i-1. But we don't need to; just compute t_i.
Now, I'll consider the problem's constraints and ensure it runs within time. O(n) is fine.
Now produce final answer.
But wait, the problem statement says "round square". That might be irrelevant.
Now, let's double-check with a scenario where a_i values are huge and we might have overflow in computing (a - i + n) if we use 32-bit signed and a_i close to 2e9? But a_i ≤ 1e9, so safe.
Now produce final solution.
Now I'll also consider if there is any case where a_i - i + n might be computed as negative if a_i is small and i large, but as argued minimum is 0. So safe.
Now final answer: implement as described.
Thus we are done. We need to determine the entrance through which Allen will enter the fan zone.
Interpretation:
- There are
n entrances, each with an initial queue of a_i people (excluding Allen).
- Every minute, each entrance admits one person from the front of its queue (if any).
- Allen starts at the back of queue 1.
- At the start of each minute, if Allen is at the front of his current queue, he enters during that minute.
- If he is not at the front, then after the minute ends (and one person from that entrance has entered), he moves to the back of the next entrance (cyclically).
Let t be the number of minutes that have already passed. After t minutes, Allen is at entrance i = (t mod n) + 1. At the beginning of minute t+1, the number of original people still in front of him at entrance i is max(0, a_i - t). He will enter during minute t+1 exactly when this number is 0, i.e., when a_i ≤ t.
Therefore we need the smallest t ≥ 0 such that a_{(t mod n)+1} ≤ t. For each entrance i, the times when it is checked are t = i-1, i-1 + n, i-1 + 2n, …. The smallest such t for entrance i is
t_i = i-1 + n * ceil( (a_i - (i-1)) / n ) (if a_i > i-1)
and t_i = i-1 if a_i ≤ i-1.
Equivalently, using integer arithmetic:
t_i = (i-1) + n * ((a_i - i + n) / n)
where the division is integer division (since the numerator is non‑negative).
Allen will enter at the entrance i with the smallest t_i. Because residues modulo n are distinct, the minimum is unique.
We simply compute t_i for all i, track the minimum, and output the corresponding entrance.
Complexity: O(n), fits constraints (n ≤ 10⁵).
Memory: O(1) besides the input array.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
long long best_time = LLONG_MAX;
int ans = 0;
for (int i = 1; i <= n; ++i) {
long long a;
cin >> a;
// t_i = (i-1) + n * ceil((a - (i-1))/n)
// Using the equivalent integer formula:
long long t = (i - 1) + n * ((a - i + n) / n);
if (t < best_time) {
best_time = t;
ans = i;
}
}
cout << ans << '\n';
return 0;
}