We are given: H (initial hp, up to 1e12), n (up to 2e5). Sequence d_i of length n. Each round lasts n minutes, and the same d_i sequence repeats. At minute i (1-indexed) of each round, hp changes by d_i. We want the first minute after which monster's hp becomes <= 0. If it never happens, output -1.
Interpretation: At minute 1, we apply d1, then check after that minute (so after applying d1) if hp <=0? The problem says "first minute after which the monster dies. The monster dies if its hp is less than or equal to 0." So we check after each minute. So if initial H <= 0, then answer 0? But H >= 1. So positive. So we simulate minutes 1,2,3,... until hp <=0. The rounds repeat indefinitely. So we have a prefix sum pattern: Let prefix sums P_k = sum_{i=1}^{k} d_i for k from 1 to n. After k minutes in a round (or after finishing k minutes of a round), the hp is H + P_k. After complete rounds: after r full rounds (i.e., rn minutes), hp = H + r * S, where S = sum_{i=1}^{n} d_i = P_n. Then after additional k minutes (1<=k<=n) of the next round, hp = H + rS + P_k.
Goal: find smallest positive minute t such that at the end of minute t, hp <= 0.
We can think in terms of rounds. Since n up to 2e5 and H up to 1e12, we need O(n log something) or O(n). Brute force simulating minute by minute is impossible if many rounds needed.
Let S = total sum per round. Cases:
-
If there exists some prefix within first round (i.e., before completing a round) that makes hp <=0, then answer is that minute index.
-
Otherwise, if S >= 0 (i.e., non-negative net change per round), then hp will never decrease sufficiently to <=0 after first round, because after each round hp either increases or stays same, and within a round, the sequence could lower it but we already checked first round and it didn't kill. Actually, could it kill in later rounds? Suppose S = 0, then hp after r rounds is H + r*0 = H. So after each round, hp resets to H (since no net change). During a round, the pattern is the same. If it didn't kill in the first round, it will never kill because the sequence of changes relative to start of round is identical, and the starting hp for each round is H (if S=0) or larger (if S>0). Actually if S > 0, hp increases each round, making it even less likely to die. So if S >= 0 and the first round didn't kill, answer is -1.
-
If S < 0, then each round reduces hp by |S|. Over many rounds, eventually hp at the start of a round will be low enough that during that round it dies. We need to find the earliest minute.
Approach: Compute prefix sums P[1..n] (cumulative sum after i minutes). For first round, check if any P[i] <= -H (since we need H + P[i] <= 0 -> P[i] <= -H). If yes, answer the smallest i.
Otherwise, since S < 0, we need to consider multiple rounds. For round r (starting from 0 for first round, we have already considered r=0), after r complete rounds, start hp = H + rS. Then during round r+1, after k minutes (k from 1..n), hp = H + rS + P[k] <= 0 -> P[k] <= - (H + rS). Since S is negative, -(H + rS) becomes larger as r increases (because H + rS decreases). We need the smallest total minute t = rn + k such that the condition holds.
We can think: For each k (1..n), we want to find smallest r >= 0 integer such that H + rS + P[k] <= 0. For a fixed k, this inequality is: rS <= -H - P[k] -> Since S is negative, divide by S (negative) flips inequality: r >= (-H - P[k]) / S. But careful: S is negative, so dividing by S: r >= ( -H - P[k] ) / S . Because we have r*S <= some negative number. Write:
Condition: H + rS + P[k] <= 0 => rS <= -H - P[k]. Let RHS = -H - P[k]. Since S < 0, divide both sides by S (negative) reverses inequality: r >= ( -H - P[k] ) / S. But careful: ( -H - P[k] ) may be positive or negative. Actually, because we already know that after first round (r=0) it didn't die for any k, i.e., H + P[k] > 0 for all k, thus -H - P[k] < 0. Meanwhile S < 0, so ( -H - P[k] ) / S would be positive? Let's compute: Suppose H=100, P[k]=-50 -> -H-P[k] = -100 - (-50) = -50. S = -5. Then (-50)/(-5)=10, so r>=10. That's plausible. If -H-P[k] is negative and S negative, the quotient is positive. If -H-P[k] is positive, then quotient is negative and r>= negative number is automatically satisfied for r>=0. But could -H-P[k] be positive? That would mean H + P[k] < 0, i.e., already dead in first round for that k, but we already checked that case and would have answered. So after excluding first-round kills, for all k we have H + P[k] > 0 => -H - P[k] < 0. So RHS negative.
Thus r must be at least ceil( (-H - P[k]) / S ). Since r is integer >=0, the minimal r is max(0, ceil( (-H - P[k]) / S )). Because of negative division, careful: We need the smallest integer r such that rS <= -H - P[k] . Since S < 0, rS is decreasing as r increases. For r=0, we have 0 >? Actually check: Since H+P[k] >0, we have 0 > -H-P[k]? Wait: -H-P[k] is negative, so 0 > -H-P[k]? Example: H=100, P[k]=-50 => -H-P[k] = -50, 0 > -50 (since 0 is larger). So inequality rS <= -50. For r=0, 0 <= -50 is false. So we need r >= something. So rS is negative and decreasing (more negative) as r increases. So we need r large enough so that r*S (which is negative) is <= RHS (also negative). That's equivalent to r >= ceil( (-H-P[k]) / S ). Because dividing by negative flips inequality.
But because both sides negative, we can think: Let A = - (H+P[k]) = -(H+P[k]) >0? Wait H+P[k] >0, so -(H+P[k]) <0. But we can define positive value: Let need = H + P[k] (positive). Condition: H + rS + P[k] <= 0 => rS <= - (H+P[k]) = -need. Since S negative, rS is negative; as r increases, rS becomes more negative. So we need r such that rS is at most -need (more negative). That is r >= ceil( need / (-S) ). Actually rewrite: r(-S) >= need. Because rS <= -need => Multiply both sides by -1: -rS >= need => r * (-S) >= need. Since -S >0, r >= ceil( need / (-S) ). That's simpler: r >= ceil( (H+P[k]) / |S| ). Because need = H + P[k] >0. Yes: condition: H + P[k] + rS <= 0 -> rS <= -(H+P[k]) -> Multiply by -1: -rS >= H+P[k] -> r * (-S) >= H+P[k]. Let D = -S > 0. Then r >= ceil( (H+P[k]) / D ). That is the minimal number of full rounds (after which we start the next round?) Wait: r is number of complete rounds before the current round? Let's define: After r complete rounds (i.e., after rn minutes), the hp is H + rS. Then we apply the first k minutes of the next round. So total minutes = rn + k. So for each k, the minimal r that makes it die during the (r+1)-th round at minute k is the smallest integer r >= 0 such that H + r*S + P[k] <= 0. Using the transformed inequality: r >= ceil( (H+P[k]) / D ). Since H+P[k] >0. So r_min[k] = ceil( (H+P[k]) / D ). Then total minutes candidate = r_min[k] * n + k.
But careful: Is r allowed to be zero? Yes. But we already checked first round (r=0) and no kill; so for each k, r_min[k] will be at least 1? Not necessarily: If H+P[k] >0, then ceil( (H+P[k])/D ) may be 0 if (H+P[k]) <=0? But it's positive, so if D is large enough maybe (H+P[k])/D could be <1? But H+P[k] is positive, D is positive. So ceil of a positive number is at least 1 unless that number is 0 or less? Actually if 0 < x <= 1, ceil(x) = 1. So minimal r is at least 1. So indeed r_min >=1 for all k because we excluded the case where H+P[k] <=0 which would give r_min=0. So all r_min >=1.
Thus answer = min over k=1..n of ( r_min[k] * n + k ), where r_min[k] = ceil( (H+P[k]) / D ), with D = -S. However we must be careful: The condition H + rS + P[k] <= 0 is exactly that. But we also need that during that round, after applying the first k minutes, we kill. But there could be a scenario where earlier within the same round we might kill before minute k. However, we are taking the minimum over all k, so if there is an earlier minute (smaller index) that also satisfies with same or smaller total minutes, it will be captured. But note: For a fixed round r, if the monster dies at minute k, it will not die earlier in that same round? It could die at minute j<k. But when we compute for each k, we consider the possibility that it dies at minute k in some round r. If there is a j < k in the same round r that also dies, then total minutes rn + j would be smaller, and that j would be considered in its own candidate. So taking min over all k works.
But we need to be careful: For a given k, the derived r_min ensures that at minute k of the (r_min)-th round? Wait, we assumed r complete rounds and then a round where we apply k minutes. In our indexing: r is number of complete rounds before the round in which we kill. So if r_min = 2, that means after 2 full rounds (2n minutes), we start the 3rd round and apply k minutes; total minutes = 2n + k. However, note that our inequality derived: H + rS + P[k] <=0. If r=2, then hp at start of round (after 2 full rounds) is H+2S. Then after k minutes of that round, hp = H+2S+P[k]. So r is number of completed rounds. So total minutes = rn + k. So r_min as ceil((H+P[k])/D) corresponds to that r.
But is there any chance that the monster dies during a round earlier than the k that we consider for that r? Let's test: Suppose we compute r_min for k = 5. That yields some r. Could it be that for the same r, at some j < 5 the monster already died? That would mean H + rS + P[j] <=0. If that holds, then our candidate for j with r (the same r) would be rn + j, which is smaller. But is it possible that r for j is not minimal? Possibly the minimal r for j might be larger than r? Actually, for j < k, P[j] is typically larger (since cumulative sums may vary; but d_i could be positive and negative, so P[j] may be less than P[k]? Not necessarily; the prefix sum can go down then up. So it's possible that for j < k, the required r to kill might be larger than for k, because P[j] could be greater (less negative) than P[k]. In such case, the minimal r for j is larger, and it's possible that with the same r (computed for k), the condition for j is not satisfied, but for k it is. However, if at minute k it dies, it must have survived all previous minutes j=1..k-1 in that round. So we require that for all j < k, H + r*S + P[j] > 0. So our assumption that r is the minimal integer such that for that particular k the condition holds ensures that for j<k the condition is not satisfied with that r (if they were, the monster would have died earlier, but we are only considering the first minute of death. But our min over all (r,k) candidates will produce the earliest minute across all possibilities. However, is it possible that with r being minimal for k, but there is some j<k that also satisfies with the same r? Then that would be a smaller minute, and we would have a candidate for j with the same r, which we will also compute when we evaluate j (with r_min_j). But r_min_j might be less than or equal to r? Let's examine.
We need the first minute overall. Suppose for some k, with r_min[k] = r, the monster dies at minute k in round r+1 (since after r rounds). But if for some j<k, it also dies in that same round (i.e., H + rS + P[j] <=0), then the first death would be at minute j, not k. But then the candidate for j with r (the same r) would have total minutes rn + j, which is less. However, our calculation for j would use r_min[j] = smallest r such that H + rS + P[j] <= 0. Since H + rS + P[j] <=0 holds with this r, the minimal r for j is <= r. It could be exactly r or maybe even smaller. If it is smaller, then candidate (r_min[j]n + j) would be even smaller. If it is equal to r, then r_min[j] = r, and candidate = rn + j < rn + k, so it will be considered. If r_min[j] < r, then candidate would be less than rn + j? Actually (r_min[j])*n + j. If r_min[j] < r, then candidate would be smaller. So min over all candidates will include that earlier minute. Therefore, taking min over all k and corresponding minimal r works to find the overall earliest death minute. Because for any death scenario (r, k) where r is the number of complete rounds before death, that scenario yields a candidate total minutes. The earliest death will correspond to some (r,k) with r minimal possible? Not necessarily minimal r overall, but minimal total minutes. However, if there is any scenario (r,k) that causes death, then for that specific k, the minimal r that works is r_min[k] <= r. So the total minutes from that (r_min[k], k) is <= (r,k). Therefore, the earliest death minute will be achieved by using for each k the minimal r that works. So indeed we only need to consider r_min[k] for each k.
Thus algorithm:
- Compute S = sum_{i=1}^{n} d_i.
- Compute prefix sums P[1..n] = cumulative sum after i minutes (so P[i] = sum_{j=1}^{i} d_j).
- For each i from 1 to n, if H + P[i] <= 0, answer i (since first round).
- If S >= 0, answer -1.
- Else S < 0:
- D = -S.
- For each i from 1 to n:
need = H + P[i] ( > 0 )
r = ceil( need / D ) // integer division: r = (need + D - 1) // D
total = r * n + i
take minimum total across i.
But careful: need = H + P[i] might be huge up to 1e12 plus up to n1e6? Actually H up to 1e12, P[i] up to n1e6 = 2e51e6 = 2e11, so need up to about 1.2e12. D = -S up to n1e6? Actually S negative, D up to n*1e6 = 2e11. So need/D could be up to ~6 (1e12 / 2e11 = 5). Actually might be up to 1e12/1? But S could be as small as -1? S can be as low as -10^6 * n? Actually d_i can be as low as -1e6, so S could be as negative as -1e6 * 2e5 = -2e11. So D up to 2e11. So need/D up to ~ 1e12/1? Minimum D could be 1? If S = -1? Actually d_i can be positive and negative, S could be small magnitude, even 0? But we are in S<0 case, so S <= -1? d_i are integers, so S can be -1, -2, etc. So D could be as small as 1. So need/D could be up to 1e12. So r up to 1e12. But that's okay, we can compute using integer arithmetic.
But we have n up to 2e5, so iterating over i and computing r is fine.
Edge Cases: Need to check for overflow when computing total? r * n might be up to 1e12 * 2e5 = 2e17, which exceeds 64-bit? 2e17 is within 2^63-1? 2^63-1 is about 9.22e18, so 2e17 is fine for 64-bit signed integer. Actually 2e17 is less than 9e18, so safe with 64-bit (long long in C++). But careful: H up to 1e12, P up to 2e11, need up to ~1.2e12. D could be 1, r = need, r up to 1.2e12. n = 2e5, r*n up to 2.4e17, fits in 64-bit (9e18). So we can use 64-bit.
Now test with examples.
Example 1:
H=1000, n=6, d = [-100,-200,-300,125,77,-4]
Compute prefix: P1 = -100, P2 = -300, P3 = -600, P4 = -475, P5 = -398, P6 = -402? Let's compute: -100-200=-300; -300-300=-600; -600+125=-475; -475+77=-398; -398-4=-402. S = -402.
Check first round: H+P1=900>0, H+P2=700>0, H+P3=400>0, H+P4=525>0, H+P5=602>0, H+P6=598>0. So none <=0. S = -402 <0. D=402.
For each i:
i=1: need=1000-100=900, r = ceil(900/402)=ceil(2.2388)=3? Actually 900/402 = 2.2388, ceil = 3. But wait compute properly: 4022=804, 4023=1206, need=900, so need>804, need <=1206? Actually condition r402 >= need. So smallest r such that 402r >= 900 is r=3 (since 2402=804<900, 3402=1206>=900). So r=3. total=36+1=18+1=19.
i=2: need=1000-300=700, r = ceil(700/402)=ceil(1.7413)=2 (since 4021=402<700, 2402=804>=700) total=26+2=12+2=14.
i=3: need=1000-600=400, r=ceil(400/402)=1? Because 4021=402>=400, so r=1. total=16+3=6+3=9.
i=4: need=1000-475=525, r=ceil(525/402)=2 (since 1402=402<525, 2402=804>=525) total=26+4=12+4=16.
i=5: need=1000-398=602, r=ceil(602/402)=2? 1402=402<602, 2*402=804>=602 => r=2 total=12+5=17.
i=6: need=1000-402=598, r=ceil(598/402)=2 total=12+6=18.
Minimum total is 9. That matches output. Good.
Example 2:
H=1000000000000, n=5, d = [-1,0,0,0,0]
Prefix: P1=-1, P2=-1, P3=-1, P4=-1, P5=-1? Actually sum: -1+0=-1, etc all -1. So S = -1. D=1.
Check first round: H+P1=999999999999 >0, H+P2 same, etc. So no.
For each i (1..5):
need = H + P[i] = 10^12 -1? Actually H=10^12, so H+P1= 999,999,999,999? Wait 1e12 - 1 = 999,999,999,999? 1,000,000,000,000 -1 = 999,999,999,999. So need = 999,999,999,999. D=1 => r = need (since ceil(need/1)=need). total = rn + i = need5 + i. For i=1: total = 999,999,999,9995 + 1 = 4,999,999,999,995 + 1 = 4,999,999,999,996? Actually 5999,999,999,999 = 4,999,999,999,995. +1 = 4,999,999,999,996. That's the output. For i=2 would be +2 => 4,999,999,999,997, larger. So min is i=1. Output matches.
Example 3:
H=10, n=4, d=[-3,-6,5,4]
Prefix: P1=-3, P2=-9, P3=-4, P4=0? Actually -9+5=-4; -4+4=0. S = 0? Sum: -3-6+5+4=0. So S=0. Check first round: H+P1=7>0, H+P2=1>0, H+P3=6>0, H+P4=10>0. So no kill. S>=0 => -1. Output matches.
So algorithm works.
But is there any nuance? The problem statement: "The battle consists of rounds, each of which lasts exactly n minutes. After a round ends, the next round starts immediately. This is repeated over and over again." And "Each round has the same scenario." So the sequence repeats exactly. So our model is correct.
Potential issues: The monster dies when its hp is less than or equal to 0 after a minute. Could it die during a minute? It says after the i-th minute it changes to h + d_i, then presumably after that change we check. So okay.
Now consider the case where S<0 but the first round already kills? Our algorithm would catch it in the first check and output minute i. That's fine.
But consider case where S<0 and some prefix sum P[i] is positive large such that H+P[i] > H? Actually H+P[i] could be > H if P[i] positive. That is fine; need may be large. But we must also consider that the monster might die during some minute within a later round before we apply all n minutes? The algorithm does that via r and k.
One more nuance: The inequality r >= ceil((H+P[k])/D) yields integer r. But we must ensure that we are not missing the scenario where the monster could die at the same minute but from a round with r smaller than ceil((H+P[k])/D) due to some specific condition? Because we derived condition H + rS + P[k] <= 0. If r is less than ceil((H+P[k])/D), then rD < H+P[k] => r*(-S) < H+P[k] => -rS < H+P[k] => H + rS + P[k] > 0. So it's not dead. So indeed the minimal r is that ceil.
Thus algorithm stands.
But need to be careful about integer overflow when computing need = H + P[i]. H up to 1e12, P[i] can be positive up to maybe 2e11, so sum up to 1.2e12, fits in 64-bit signed. r calculation using (need + D - 1) / D is fine with 64-bit.
But careful: In languages like C++ with signed integers, ensure no overflow in multiplication r*n. r up to ~1e12, n up to 2e5, product up to 2e17, fits in signed 64-bit (max ~9e18). Use long long.
Edge Cases:
- d_i could be zero, positive, negative.
- S might be zero? Already handled.
- H is at least 1.
- If S<0, D positive. But what if D is very small like 1, r = need, product huge but still okay. However total minutes may exceed 2^63? Let's compute worst-case: H=1e12, min P[i] maybe? The most negative P[i] could be as low as -n1e6 = -2e11, so need = H + P[i] ~ 1e12 - 2e11 = 8e11. D = -S could be as low as 1 (if sum = -1). Then r = 8e11, n = 2e5, product = 8e11 * 2e5 = 1.6e17. Add i up to 2e5 => still ~1.6e17. This is less than 2^63-1 (9.22e18). So safe.
But if H is 1e12 and P[i] could be positive, need could be up to 1e12 + 2e11 = 1.2e12, r=1.2e12, product=1.2e122e5=2.4e17. Still safe.
But what about H up to 1e12? Actually problem says 1 ≤ H ≤ 10^{12}, yes.
Ok.
Now, is there any scenario where the monster dies exactly at the end of a round? That is after n minutes, P[n] = S, so condition H + rS + S <=0? Actually at the end of a round, after r complete rounds plus a full next round, we would have rS + S = (r+1)S, but careful: After r complete rounds, then we apply all n minutes of the next round; total minutes = rn + n = (r+1)n. That is covered by k=n with r_min computed as ceil((H+P[n])/D). But P[n]=S, so need = H+S. Condition: H + rS + S <= 0 => H + (r+1)S <= 0 => (r+1) >= ceil((H)/(-S))? Actually need = H+S. But if we think of r as number of complete rounds before this round, then after r complete rounds, hp = H + rS, then applying n minutes adds S, so total hp = H + (r+1)S. So condition H+(r+1)S <=0 => r+1 >= ceil( H / D )? Because H+(r+1)S <=0 => (r+1)(-S) >= H => (r+1) >= ceil( H/D ). That's a different r. However, our formula for k=n yields need = H + P[n] = H+S. Then r_min = ceil( (H+S)/D ). Since D = -S, (H+S)/D = (H+S)/(-S) = -(H+S)/S? Hmm, let's compare. (H+S)/D = (H+S)/(-S) = -(H+S)/S = ( -H/S - 1). Since S<0, -H/S = H/|S|. So (H+S)/D = H/|S| - 1? Actually D = -S = |S|. So (H+S)/D = (H+S)/|S|. Since S = -|S|, H+S = H - |S|. So (H - |S|)/|S| = H/|S| - 1. Then ceil of that might be something. Condition for total rounds (r+1) is ceil( H/|S| )? Because we need (r+1) such that (r+1)*|S| >= H. So minimal (r+1) = ceil( H/|S| ). So r = ceil( H/|S| ) - 1. Now our computed r_min for k=n is ceil( (H+S)/|S| ) = ceil( H/|S| - 1 ). Since H/|S| may not be integer, this might be either ceil(H/|S|) - 1 or ceil(H/|S|) depending? Let's test: Suppose H=10, |S|=3 => H/|S|=3.333, ceil =4. So r = 3. H+S = H - |S| = 10-3=7, (H+S)/|S| = 7/3 ≈ 2.333, ceil =3. So r_min =3, which equals r? r was 3, good. If H=9, |S|=3 => H/|S|=3, ceil=3, r =2. H+S = 9-3=6, 6/3=2, ceil=2 => r_min=2, matches. If H=10, |S|=4 => H/|S|=2.5, ceil=3, r=2. H+S=10-4=6, 6/4=1.5, ceil=2 => r_min=2, matches. If H=8, |S|=4 => H/|S|=2, ceil=2, r=1. H+S=8-4=4, 4/4=1, ceil=1 => r_min=1, matches. So indeed ceil((H+S)/|S|) = ceil(H/|S|) - 1 if H/|S| not integer? Wait, check H=12, |S|=4 => H/|S|=3, ceil=3, r=2. H+S=12-4=8, 8/4=2, ceil=2 => r_min=2 matches. So it seems consistent: ceil((H - |S|)/|S|) = ceil(H/|S|) - 1 if H/|S| not integer? Actually H/|S| integer case: H=12, |S|=4 => H/|S|=3 exactly, ceil(H/|S|)=3, r=2, and (H-|S|)/|S| = 2 exactly, ceil=2, so indeed equals ceil(H/|S|)-1. For non-integer, it's also ceil(H/|S|)-1 because (H-|S|)/|S| = H/|S| - 1, and ceil(x-1) = ceil(x) - 1 for all real x? Not exactly: ceil(x-1) = ceil(x) - 1 because if x = n + f, where n integer, f in (0,1], then x-1 = (n-1)+f, ceil(x-1) = n (since f>0) => = ceil(x) -1? ceil(x)= n+1? Wait, if x = n + f, with 0 < f <= 1, then ceil(x) = n+1. Then x-1 = (n-1)+f, ceil(x-1) = n (since f>0). So ceil(x-1) = n = (n+1)-1 = ceil(x)-1. If f=0 (x integer), x = n, ceil(x)=n, x-1=n-1, ceil(x-1)=n-1 = ceil(x)-1. So indeed ceil(x-1) = ceil(x)-1 always. So with x = H/|S|, we have ceil((H/|S|)-1) = ceil(H/|S|)-1. So r_min = ceil(H/|S|)-1. That's exactly r. So it matches the expected r for killing at end of a round.
Thus our general formula works for k=n as well.
So answer = min_i ( ceil((H+P[i]) / D ) * n + i ) where D = -S, given S<0.
But careful: In case S < 0 but H+P[i] may be <=0 already? We already check that first; if any, we would have answered. So in the loop we assume H+P[i] > 0.
Implementation: Use long long. For each i, compute need = H + prefix[i]. Compute r = (need + D - 1) / D. But need and D are positive, but need may be up to 1e12+2e11, D may be as low as 1. So use (need + D - 1) / D. Or use (need - 1) / D + 1. For safety with large numbers.
Edge Cases: What if D is 0? But we are in S<0 case, so D > 0.
Complexity: O(n).
But careful: We need to compute prefix sums correctly: prefix[i] = sum_{j=1}^{i} d_j. Use 64-bit.
Double-check with random tests.
Consider H=5, n=3, d=[-2, -2, -2]. S=-6, D=6. Prefix: P1=-2, P2=-4, P3=-6. First round: H+P1=3>0, H+P2=1>0, H+P3=-1<=0? Actually H+P3 = -1 <=0, so it dies at minute 3? But after minute 3, hp = -1 <=0. So answer=3. Our algorithm would detect first round with i=3, output 3. Good.
Consider H=5, n=3, d=[-1, -1, 5]. S=3 (>=0). Prefix: P1=-1 (H+(-1)=4>0), P2=-2 (3>0), P3=3 (8>0). S=3 >0, so -1? But could it die later? Since net gain per round, hp increases. No kill. Output -1.
Consider H=10, n=2, d=[-5, -6]. S=-11, D=11. Prefix: P1=-5 (5>0), P2=-11 (-1<=0?) Actually H+P2 = -1 <=0, so dies at minute 2, answer 2. Our algorithm catches first round.
Consider H=10, n=2, d=[-9, -1]. S=-10, D=10. Prefix: P1=-9 (1>0), P2=-10 (0<=0) -> dies at minute2, answer2.
Consider H=10, n=2, d=[-9, 5]. S=-4, D=4. Prefix: P1=-9 (1>0), P2=-4 (6>0). No first round kill. Compute for i=1: need=1, r = ceil(1/4)=1? Actually 1/4=0.25, ceil=1, total=12+1=3. For i=2: need=6, r = ceil(6/4)=2 (since 14=4<6, 24=8>=6), total=22+2=6. Minimum=3. Let's simulate: Round1: after minute1 hp=10-9=1 (alive), minute2 hp=1+5=6 (alive). End round1. After round1: hp=6? Actually after round1, S=-4, so after full round1 hp=10-4=6? Wait sum of round: -9+5 = -4, so after round1 hp=6. Then round2: minute1: hp=6-9=-3 <=0, so dies at minute3 (since round1 had 2 minutes, minute3 is first minute of round2). So answer=3 matches.
Consider case where within the same round, the monster dies at minute k, but the prefix sums later become more negative? Actually we just need first death, so if multiple minutes in first round cause death, we take smallest i. Already handled.
Now consider case where S<0 and D > 0, but some P[i] are positive and large, making need huge. That will yield large r. But maybe we can reduce computation? Not needed.
Potential pitfalls: Overflows when computing need? need = H + P[i] may be as large as 1e12 + 2e11 = 1.2e12, fine. But if P[i] could be negative large? H up to 1e12, P[i] as low as -2e11, so need is still positive (since no first round kill). But if P[i] is very negative but not enough to kill, need is still positive and could be up to ~1e12.
But what about the possibility of overflow when computing r = ceil((H+P[i])/D) where D could be 1, so r up to 1.2e12, and then total = r * n + i, product up to 1.2e12 * 2e5 = 2.4e17, still within 2^63. So 64-bit signed safe.
Now, could the answer exceed 2^63-1? Let's think: H up to 1e12, d_i min -1e6, S negative down to -2e11, D up to 2e11. Minimal D is 1. Maximum need = H + max(P[i])? Actually need = H + P[i] could be as high as H + (max prefix sum). max prefix sum could be up to n*1e6 = 2e11, so need up to 1.2e12. r = ceil(need/D) could be up to 1.2e12 (if D=1). Then total = r * n + i could be up to 1.2e12 * 2e5 = 2.4e17. That's 2.4e17, which is about 2.4e17, less than 9e18, so safe. However, if H is 1e12 and D=1, but maybe need could be larger if H plus some P[i] > H? P[i] positive could be up to 2e11, so need <=1.2e12, as said. So max total ~ 2.4e17. 2^63-1 is 9.22e18, so fine. Use unsigned long long? But signed is fine.
Now, implement.
Steps:
- Read H (long long) and n (int).
- Read array d of size n.
- Compute prefix sums cumulatively: maintain current sum pref = 0 (long long). Also store prefix sums in vector P maybe, or we can compute on the fly without storing? We need to use each P[i] multiple times? Actually we need to compute for each i the prefix sum P[i] to compute need and then candidate. So we can compute on the fly as we read d_i: maintain pref, and for each i (1-indexed) after updating, we can evaluate first round and also store prefix values for later. But for S<0 case, we need to iterate over i again to compute min candidate. So we could store prefix sums in an array, or store them in a vector. n up to 2e5, storing is fine.
Alternatively, we could compute S separately, then recompute prefix sums in a second pass if we read all d's into an array. Simpler: read all d into array, compute S (sum), compute prefix sums array P of length n. Then first check: for i from 0 to n-1 (0-index), if H + P[i] <= 0, output i+1 and return. If none, and S >= 0, output -1. Else D = -S. Then iterate i again: need = H + P[i]; r = (need + D - 1) / D; total = r * n + (i+1); keep min.
But careful: In the first loop, we must check after each minute; if H+P[i] <= 0, output i+1 and return. But what if H is already <= 0? Not possible as H>=1.
Edge case: If S < 0, but after first round it doesn't die, we proceed.
Now, is it possible that the answer is found during the first round but after we already did the first loop? yes.
Now, what about when S < 0 but H+P[i] > 0 for all i, but some need might be zero? Actually need >0, but could be zero? If H+P[i] = 0 exactly, then that would have been caught in first round, so after first round no need zero. So need >0.
Now, consider if S < 0 but D may be large. Then r could be 1 for many i. That's okay.
But there is nuance: The monster's hp could go negative during a round and then later become positive again due to positive d_i? But the monster dies immediately when hp <= 0 after some minute; battle stops. So we only consider the first time it becomes <=0.
Our algorithm looks for the first minute it becomes <=0; it doesn't consider the possibility that hp could be <=0 at some minute but we might have skipped that because later it becomes positive and then later dies? No, the first death ends battle. So it's fine.
Now test with some custom.
Case: H=10, n=3, d=[5, -20, 5]. S=-10, D=10. Prefix: P1=5 (15>0), P2=-15 (-5 <=0)? Actually H+P2 = -5 <=0, so dies at minute 2? After minute1: hp=15, alive. minute2: hp=15-20=-5 <=0, dies. Answer=2. Our first loop will catch i=2 (0-index for P2). Good.
Now, S<0 case with positive d early may cause large need but later negative sum? It's fine.
Edge: When S < 0, but some P[i] is very negative? Already covered.
Now, what about the possibility that the answer could be huge and exceed 64-bit? As argued, it's within safe range. But let's verify the maximum possible answer. The worst-case scenario is when H=1e12, n=1, d=[-1]. Then S=-1, D=1. Prefix: P1=-1. H+P1 = 1e12 - 1 >0? Actually 1e12 -1 = 999,999,999,999 >0, so no first round kill. Then need = 1e12-1, r = ceil((1e12-1)/1) = 1e12-1? Wait need = H+P1 = 1e12 -1, r = need? Since D=1, r = need. total = rn + 1 = (1e12-1)1 + 1 = 1e12. Actually that's the minute after which it dies: After r=need-1? Let's compute: H=1e12, d=-1 each minute, so each minute hp decreases by 1. It will die after 1e12 minutes. So answer=1e12. That's fine. n=1 so total = need1 + 1? Actually careful: need = H+P1 = H -1 = 1e12-1. r = ceil(need/D) = need (since D=1). total = rn + i = need*1 + 1 = (1e12-1) + 1 = 1e12. So answer = 1e12. That's within 1e12? Actually 1e12 is within 1e12, but answer can be up to about 2.4e17 as earlier. But H up to 1e12, but answer could be larger than 1e12 due to n >1. For instance, H=1e12, n=200000, d_i = [0,0,...,0,-1]? Let's design: All zeros except last element -1. Then S = -1, D=1. Prefix sums: For i < n, P[i]=0, need = H = 1e12. For i = n, P[n]=-1, need = 1e12-1. r = ceil(need) = need for i=n, but for i=n-1, need=1e12, r=1e12. So r = 1e12 for i=n-1, total = 1e12 * n + (n-1) ~ 1e12 * 200000 = 2e17. So answer can be up to 2e17. That's within 9e18. So okay.
Thus use 64-bit.
Now, we need to be careful about reading input, loops.
Now, but is there any scenario where the monster can die in the middle of a round after many rounds, but the prefix sums cause an earlier death within the same round? Our min over i covers that.
But what about the possibility that the monster dies exactly at the start of a round (i.e., after completing previous round, hp <=0)? That would be after some full rounds. But after a round ends, we check? The problem statement: "Print the first minute after which the monster dies." The battle consists of rounds, each of which lasts exactly n minutes. After a round ends, the next round starts immediately. So after each minute we check. The moment after a round ends is the same as after minute that ends the round. So if after a full round hp <=0, then the death occurs at the end of that round, i.e., minute = (number of full rounds) * n. That is covered by k=n with r = number_of_full_rounds - 1? Let's define: Suppose after r full rounds, hp <=0. That would be after rn minutes. In our modeling, after r complete rounds, we have applied rn minutes, and hp = H + rS. If that <=0, then death occurs at minute rn. But our approach using (r, k) with k minutes of a round only considers death within a round (k from 1..n). For death exactly at the end of a round (i.e., after n minutes of a round), that corresponds to taking k=n with r being number of complete rounds before that round? Actually after r complete rounds, we start round r+1. If after applying all n minutes of round r+1 we die, that's total minutes = rn + n = (r+1)n. But what about death after r complete rounds, i.e., before starting round r+1? That would be after rn minutes, and hp = H + rS. For that to happen, H + r*S <=0. In our formulation, that's equivalent to having k=n and r-1? Let's see: If we set r' = r-1 (number of complete rounds before the round we just finished) and k=n, then total minutes = (r-1)n + n = rn. So that works if we allow r to be zero? If r=0, after 0 rounds, hp=H>0, so not die. So for r>=1, death after r full rounds corresponds to r' = r-1, k=n. So it's covered by our (r,k) with k=n. But we must ensure that our derived r_min for k=n gives the correct r' (which is number of complete rounds before the killing round). In that case, condition H + r'*S + P[n] <= 0. Since P[n] = S, this becomes H + (r'+1)S <=0, which is exactly condition for death after r'+1 full rounds. So r' = ceil((H+S)/D) - 1? Actually we derived r_min = ceil((H+S)/D). That yields r' such that H + r'*S + S <=0, i.e., death after r'+1 rounds. So if death occurs exactly after r full rounds, then r' = r-1. So the total minutes = r'*n + n = (r-1)n + n = rn. So using our formula yields the correct minute.
Thus our min over i includes k=n, which captures death at end of rounds.
Now, check edge: Suppose H=10, n=2, d=[-5,-5]. S=-10, D=10. Prefix: P1=-5 (5>0), P2=-10 (0<=0) -> dies at minute2. So first round kill. Good.
Suppose H=10, n=2, d=[-4,-6]. S=-10. Prefix: P1=-4 (6>0), P2=-10 (0<=0) -> dies at minute2, first round.
Suppose H=10, n=2, d=[-6,-4]. P1=-6 (4>0? Actually H+P1=4>0), P2=-10 (0<=0) dies at minute2. Good.
Suppose H=10, n=2, d=[-3,-3]. S=-6, D=6. Prefix: P1=-3 (7>0), P2=-6 (4>0) no first round kill. Compute: i=1: need=7, r=ceil(7/6)=2? 6<7, 12>=7 => r=2, total=22+1=5. i=2: need=4, r=ceil(4/6)=1, total=12+2=4. So answer=4. Simulate: round1: after minute1 hp=7, minute2 hp=4. round2: minute3: hp=4-3=1, minute4: hp=1-3=-2 die at minute4. So answer=4 matches.
All good.
Now, consider case where S<0 but there is a prefix with P[i] positive. That increases need, thus requires more rounds. But perhaps the monster could die earlier due to a different prefix with more negative P. Our min over i will capture the minimal total minutes.
But we need to consider that after some rounds, the monster may die not necessarily at the same minute index that gives minimal total according to the formula, because the death minute for a given k is determined by r_min[k] which is the minimal number of full rounds before that minute. However, could there be a scenario where for a particular k, using a non-minimal r (i.e., more rounds than r_min[k]) yields a smaller total minute? That seems impossible because total = r*n + k, and r is multiplied by n, so increasing r by 1 adds n minutes, which is at least 1 (since n>=1). So for fixed k, total minutes is increasing in r. Therefore the minimal total for that k is achieved at the smallest r that satisfies the condition. So it's sufficient to consider r_min[k].
Now, we must ensure that for each i, r_min[i] computed as ceil((H+P[i])/D) indeed yields a death at minute i of round r_min[i] (the round after r_min[i] full rounds). But we must also ensure that the monster is still alive at minutes before that within the same round. However, if it dies at minute i, it means for all j < i, H + r_min[i]*S + P[j] > 0. Is this guaranteed by the fact that r_min[i] is minimal for that i? Not necessarily. It might be that for the same r, some earlier j also satisfies the death condition, which would mean the monster would have died earlier in that round. But if that happens, then the actual death minute would be j, not i. However, the candidate (r_min[i], i) may not be a valid scenario because death would have occurred earlier. But we are taking the min over all (r_min[k], k). If for i, (r_min[i], i) is not a valid death because it would have died earlier at j with the same r, then the actual first death would be at (r_min[i], j) or perhaps at a smaller r for j. And that candidate will be considered when evaluating j (with its own r_min[j] maybe <= r_min[i]). Let's analyze.
We need to confirm that the minimal total minute across all (r_min[k], k) corresponds to a scenario where the monster indeed dies at minute k (i.e., survives previous minutes of that round). If for some k, with r