We are given S (initial weight), T (target weight, stop when weight <= T), D (cycle length), and D integers w_i (weight changes per day, but note: if weight on i-th day is x, then next day it becomes x + w_{i % D}. So the w_i are like daily changes, could be negative or positive). We need to determine if we ever reach weight <= T. If yes, output the day number d (the day when weight becomes <= T, i.e., after how many steps? The definition: On day 0 weight is S. On day i weight is something. "If his weight successfully gets less than or equal to T, he will stop slimming immediately." So we need the smallest i >= 1? Actually, check: "If his weight on the i-th day of the plan is x, it will be x + w_{i%D} on the (i+1)-th day. Note that i%D is the remainder obtained by dividing i by D. If his weight successfully gets less than or equal to T, he will stop slimming immediately." So we start at day 0 with weight S. Then we compute day 1 weight = S + w_0. If that weight <= T, stop, and answer is 1? The problem says "how many days it takes if it ends." Example 1: S=65, T=60, D=3, w=[-2,3,-4]. Let's simulate: day0:65; day1:65-2=63>60; day2:63+3=66; day3:66-4=62; day4:62+(-2? Wait day4 uses w_{4%3=1} =3? Actually careful: On day i, we compute next day weight. Day i (starting 0) weight x, then day i+1 = x + w_{i % D}. So:
- i=0: x0=S=65; next weight = 65 + w0 = 63. day1=63.
- i=1: weight 63; next = 63 + w1 = 66. day2=66.
- i=2: weight 66; next = 66 + w2 = 62. day3=62.
- i=3: weight 62; next = 62 + w0 = 60. day4=60 <= T. So stops after day4? The answer is 4. So they output the day when weight becomes <= T, i.e., the day index after the step? They output d=4. So d is the day number when weight <= T. That is the smallest positive integer d such that weight after d steps (i.e., on day d) <= T. So we need to find minimal d >= 1 such that S + sum_{k=0}^{d-1} w_{k % D} <= T. If never, output -1.
Thus it's like we have a sequence of increments w_0, w_1, w_2, ... repeated with period D. Starting from S, we add them sequentially, and we want to know when cumulative sum + S <= T.
So define prefix sums over cycles. Let total sum over one cycle: W = sum_{i=0}^{D-1} w_i. After n cycles (i.e., after nD days), the total addition is nW. But we might stop in the middle of a cycle.
So we have S + P_k <= T, where P_k = sum_{i=0}^{k-1} w_{i % D}, for k >= 1. Need minimal k.
If any w_i can be negative, the cumulative sum may not be monotonic.
We have constraints: D up to 100k, and w_i between -100k and 100k, S, T up to 100k. The answer could be large as seen in example 3: S=100000, T=1, D=1, w=[-1]; answer = 99999. That's within linear range. But if W is negative and |W| is small, answer could be huge? For example, S=100000, T=0, D=1, w=-1 -> answer 100000. That's okay. But if w sum over cycle is zero or positive, it might never go down enough; we need to detect infinite.
But there is possibility that W (total per cycle) is negative, so eventually after many cycles, weight will go down arbitrarily, so we will eventually hit <= T. But need to find minimal days. That could be large: e.g., S=10^5, T=0, D=1, w=-1, answer 10^5. That's manageable. But what if W is -1, D=100k, then after each cycle net decrease is -1, to go from S=100k to T=0 would need ~100k cycles = 10^10 days -> too large to simulate directly. We need an efficient method.
We need to compute the minimal k such that S + sum_{i=0}^{k-1} w_{i%D} <= T. Since D can be up to 100k, and k could be huge (up to maybe ~10^10 or more), we need to use cycle analysis and prefix sums.
Observation: The process is periodic with period D, but the cumulative sum after m cycles is mW + prefix within cycle. For each offset r (0 <= r < D), the sequence of days that are at position r in the cycle appears every D steps: days when day index mod D = r? Actually we add w_i in order. To compute the sum after k steps, we can write k = qD + r, where 0 <= r < D. Then P_k = qW + pref[r], where pref[r] = sum_{i=0}^{r-1} w_i (with pref[0] = 0). But careful: if r = 0, then q = k/D, and no extra steps? Actually if k is multiple of D, then r=0, P_k = qW + pref[0] = q*W. That matches because after full cycles we have sum of all cycles.
Thus condition: S + qW + pref[r] <= T, for some q >= 0, 0 <= r < D, with k = qD + r, r>=0? But k must be >0. For r=0, k = q*D, q>=1? Actually if q=0, r=0 gives k=0, but we need k>=1. So we consider q >= 0, r from 1 to D-1 for k>0, and also r=0 corresponds to q>=1.
We need the minimal k.
If W < 0, then over cycles the cumulative sum decreases, so eventually we can satisfy. If W = 0, then per cycle net change 0, so only within first cycle we might get below T; after that, the cumulative sums repeat values each cycle offset, but if after first full cycle we never reached T, then we will never because the sums after each cycle are just the same set of values plus multiples of W=0 i.e., same values. Actually if W = 0, then after full cycles, the weight returns to the same as at the end of previous cycles? Wait: With W=0, after each full cycle, the net change is 0, but the weight at the start of each cycle is the same? Let's see: start weight S. After first cycle (D days), weight = S + W = S. So it returns to S. However, within the cycle there might be values lower than T? If none within first cycle, then after that, because the pattern repeats, the weights at same positions in each cycle are exactly the same as in the first cycle (since the starting weight is same). So if no day in the first cycle (including days 1..D) had weight <= T, then it will never happen, because the weight at day i + mD = S + pref[i] + mW = S + pref[i]. Since W=0, it's constant. So answer is -1.
If W > 0, then per cycle net increase, so if we haven't hit T early (which is lower than S, S > T), then the weight may increase, making it harder to reach T. But it might still be possible if there are negative steps that bring it down below T within a cycle, even if net W is positive. However, if after some cycles the weight never goes below T, because W > 0 might cause the weight at corresponding positions to increase each cycle, it could potentially cross above T and keep increasing, so no later cycle will bring it down? Actually consider: weight at day k = S + P_k. For a fixed offset r (0<r<=D), the sequence for k = qD + r is weight = S + qW + pref[r]. As q increases, weight increases (since W>0). So for each offset r, the weight is monotonic increasing in q. So the minimal weight over all days occurs at the smallest q for each r (i.e., q=0 for r in 1..D, and q=1 for r=0? Actually for r>0, the smallest k for that offset is when q=0, i.e., first occurrence in first cycle). For r=0, the first occurrence is when q=1 (k=D). So overall, the minimal weights across all days are achieved during the first D days (or perhaps at day D if r=0). So if we don't reach T in the first D days (or up to D? Let's check: days 1 to D correspond to r=1..D? Actually for k from 1 to D: if k = r, where 1 <= r <= D, then q=0 and r = k, pref[r] = sum_{i=0}^{r-1} w_i. For k=D, r=D, pref[D] = sum_{i=0}^{D-1} w_i = W. So the weight after D days is S + W. That's included. So days 1..D cover all r>0 with q=0. Also day 0 is S, but we don't consider. So if none of those weights <= T, then for any later day (q>=1) the weight for that offset r will be S + qW + pref[r] >= S + pref[r] (since W>0) > T (if S+pref[r] > T). But careful: for r=0 (k multiple of D), the weight is S + qW (since pref[0]=0). For q>=1, that's >= S+W if q=1, > S+W, etc. So if S+W > T (and we already have S+pref[r] > T for all r>0) then never. But what if S+W <= T? Then after D days, we stop, but that's included in first D days. So indeed, if we don't stop within the first D days, and W >= 0, then we never stop. Because for any later day, the weight is at least as large as the weight at that offset in the first cycle (if W>0, larger; if W=0, equal). So the only chance is within first D days. Thus for W >= 0, answer is either some k <= D, or -1.
Now consider W < 0. Then weight decreases over cycles. So even if we don't hit T in the first cycle, later cycles might bring weight down eventually. However, note that within each cycle, the pattern of increments repeats, but starting weight decreases each cycle by |W|. So we can consider the problem as: find the smallest k = qD + r (q>=0, 0<=r<D, but k>0) such that S + qW + pref[r] <= T.
Since W < 0, as q increases, S + qW decreases. For each offset r, the condition becomes qW <= T - S - pref[r]. Since W is negative, we can solve for minimal q satisfying that inequality, but we need integer q >= 0.
Specifically, for a given r (0<=r<D), we need q >= ceil( (T - S - pref[r]) / W )? Since W is negative, dividing by negative flips inequality. Let's derive:
We require S + qW + pref[r] <= T -> qW <= T - S - pref[r]. Since W < 0, dividing both sides by W (negative) reverses inequality: q >= (T - S - pref[r]) / W. But because W is negative, the right-hand side is generally not an integer. Actually careful: Since W < 0, we can rewrite: q >= ceil( (T - S - pref[r]) / W )? But (T - S - pref[r]) may be positive or negative. Let's do proper.
We want the smallest integer q >= 0 such that S + q*W + pref[r] <= T.
Treat: qW <= T - S - pref[r]. Since W < 0, we can multiply both sides by -1: -qW >= -(T - S - pref[r]) => q * (-W) >= (S + pref[r] - T). Let A_r = S + pref[r] - T. Note that A_r is the amount by which weight at offset r in first cycle exceeds T (if positive). Actually S + pref[r] is weight after r steps in first cycle (for r>0) or for r=0, pref[0]=0, weight at start of cycle? But we consider k = qD + r, with r=0 => after q full cycles, weight = S + qW. So that's included.
We need q * (-W) >= A_r, i.e., q >= ceil(A_r / (-W)). Since q is integer >=0.
But note: For r such that S + pref[r] <= T (i.e., A_r <= 0), then q can be 0 (since condition already satisfied at q=0 for that r). In that case, the minimal k is simply r (if r>0) or if r=0 and A_0 <=0 (meaning S <= T but S > T given, so not). So those would be caught in first cycle; we only care about r where A_r > 0, i.e., first occurrence not enough.
Thus, for each r, we compute the minimal q_r = max(0, ceil(A_r / (-W))) such that condition holds. Then the day count would be k_r = q_r * D + r, with the nuance that if r=0, then q_r must be at least 1? Actually for r=0, the formula still works: k = q*D + 0. But if q=0, then k=0, not allowed (initial day). Since we need day >0, and if S > T, q=0 for r=0 gives weight S, which is > T, so A_0 > 0. So q will be >=1 anyway. So okay.
Then answer is min over r of k_r.
But is it enough to consider only these? Yes, because for each offset r, the sequence of possible days is arithmetic progression: r, D+r, 2D+r, ...; and weight at each such day is S + pref[r] + q*W. Since W is negative, weight decreases linearly with q. The first time it becomes <= T for that residue is exactly at q = ceil(A_r/(-W)). So minimal day for that residue is that. Then global minimum over residues gives the first day overall.
But we must ensure that the day found is indeed the first time overall. Because it's possible that some other residue might yield a smaller day even if its q is larger? Actually k = qD + r, so it's a combination. Taking min over r of (q_r D + r) should give the global minimal day, as we consider all possibilities. However, careful: The formula q_r = ceil(A_r / (-W)) gives the minimal q such that condition holds for that residue. But could there be a scenario where for some residue, a smaller k (with a different q) does not satisfy the condition, but another residue with a larger q but smaller r gives smaller k? Yes, that's covered because we compute min over all residues of the minimal k for that residue. So that's correct.
Now we need to compute efficiently for D up to 100k. For each r (0..D-1), compute pref[r] (prefix sum). Then compute A_r = S + pref[r] - T. If A_r <= 0, then k = r (if r>0) or if r=0 then k=0? But since S > T, for r=0, A_0 = S - T > 0, so A_r <= 0 may occur for some r>0. Then answer candidate = r (with q=0). So we need to consider those as well.
If W >= 0, we only need to check first cycle; if no candidate, answer -1.
If W < 0, we need to compute for each r the minimal q_r = ceil(A_r / (-W)) and then k_r = q_r * D + r. But careful: For r where A_r <= 0, q_r = 0, k = r. However, if r = 0, A_0 > 0 always, so q_0 >= 1. But for r>0 with A_r <= 0, k = r already, which is <= D. So the answer may be within D. So we could first check first cycle (q=0) anyway; if any, answer is minimal r with A_r <=0 (r>=1). So we can compute that as candidate.
But also need to handle case where for some residue, A_r may be huge positive, but -W may be small, leading to q_r large; k_r might be huge. We need to compute min over these possibly large numbers. However, the answer could be large but within 64-bit? Let's bound: S,T up to 100k, w_i up to +-100k, D up to 100k, -W can be up to 100k * 100k? Actually W = sum w_i. Each w_i between -100k and 100k, D up to 100k, so W can be as low as -1e5 * 1e5 = -1e10, i.e., -10^10. Actually -100000 per w_i times D=100000 gives -1e10. So -W can be up to 1e10. But A_r = S + pref[r] - T, pref[r] is sum of up to D terms each up to 1e5, so magnitude up to 1e10 as well. So A_r / (-W) can be around 1. So q_r could be up to about 1e10 / 1? Actually worst-case: Suppose S=100000, T=1, D=1, w=-1. Then -W=1, A_0 = 100000-1 = 99999, q=99999, k=99999. That's fine. Could be larger: If D=100000, each w_i = -100000, then W = -1e10, -W=1e10. Then A_r maybe also up to ~1e10, so q maybe about 1. So not huge. But what if -W is small, say -W=1, and A_r is large, like 1e10? But can A_r be 1e10? S up to 1e5, pref[r] up to maybe 1e10 (since sum of up to 1e5 numbers each 1e5 gives 1e10). Yes, pref[r] could be 1e10 (if all w_i positive). But if W is negative, then sum of w_i is negative, but pref[r] could be large positive because early w_i could be positive, later negative making total negative. So A_r could be as high as 1e10 + 1e5 - 1 = about 1e10. So with -W = 1 (i.e., net per cycle -1), q_r = ceil(1e10 / 1) = 1e10, k_r = 1e10 * D + r, which could be 1e10 * 1e5 = 1e15. That's huge but still within 64-bit? 1e15 fits in 64-bit (2^63 ~9e18). But we need to output answer as integer, possibly up to 1e15. That's fine; we can use 64-bit (long long). However, note constraints: D up to 100000, but k could be up to maybe 10^15; that's okay.
But is such a scenario possible? Let's try: S=100000, T=1, D=100000, w_i: first 99999 are huge positive, last one negative large to make total sum -1. For example, w_i = 100000 for i=0..99998, and w_99999 = -(10000099999 + 1) so sum = -1? Actually 10000099999 = 9,999,900,000? Let's compute: 100000 * 99999 = 9,999,900,000. To have sum = -1, we need w_last = - (9,999,900,000 + 1) = -9,999,900,001. But w_i must be within [-100000, 100000]. So that's impossible because each w_i is bounded by 100k. So with constraints, max magnitude per w_i is 100k. So W is sum of D numbers each in [-100000, 100000]. The maximum absolute value of W is at most 100000 * 100000 = 1e10. Yes that's possible. But to have -W=1, we need sum = -1. With each w_i bounded by 100k, it's possible to have sum -1? For example D=2, w0=100000, w1=-100001? But -100001 is outside allowed range. The max negative is -100000. So to get sum -1, we need some combination of numbers within [-100000,100000] that sum to -1. For D=2, you could have w0=100000, w1=-100001, not allowed. For D=3, could be 100000, 100000, -200001? Not allowed. Since each w_i is integer between -100000 and 100000, the sum can be any integer between -100000D and 100000D. So yes, -1 is possible: e.g., D=2, w0=0, w1=-1; that's allowed? w_i can be -100000 to 100000, includes -1 and 0. So yes, -1 is possible. And pref[r] can be large positive if many positive numbers early. But pref[r] is sum of first r w_i. Since w_i are bounded, pref[r] maximum is if all w_i are 100000, so pref[r] max = 100000 * r. With r up to D=100000, max pref = 1e10. So A_r = S + pref[r] - T can be up to 1e5 + 1e10 - 1 = about 1e10. So q_r = ceil(A_r / 1) = up to 1e10. Then k_r = q_r * D + r, up to 1e10 * 1e5 = 1e15. That's within 64-bit. So answer may be as high as ~10^15. That's acceptable.
But we also need to consider if W = 0? Already handled.
If W < 0, we need to compute for each r, the minimal q_r = ceil( (S + pref[r] - T) / (-W) )? Actually as derived: q_r = max(0, ceil( (S + pref[r] - T) / (-W) )). Since A_r = S + pref[r] - T. For r=0, pref[0]=0. For r>0, pref[r] as defined.
Now we need to compute ceil division for positive numerator and positive denominator. Since A_r may be negative or zero. So we can compute:
if A_r <= 0: q=0
else: q = (A_r + (-W) - 1) / (-W) (integer ceiling)
Then k = q * D + r.
But careful: for r=0, we need to ensure k>0. If A_0>0, q will be >=1, so k>=D. That's fine.
Now we take min over r of k. However, we must also consider that for some residues, the condition might never be satisfied? With W<0, eventually q large enough will satisfy because -W >0, and as q increases, q*(-W) grows unbounded, so for any finite A_r, eventually q*(-W) >= A_r. So always satisfied. So all residues eventually achieve <=T. So we always have some answer (finite) when W<0. But is there any scenario where W<0 but some residues might never achieve because pref[r] might be such that S+pref[r] is very high and -W is positive, but still eventually will? Yes, because q can be arbitrarily large, so condition will be satisfied. So answer always finite for W<0.
But what about W=0? We already said answer is either within first D days or -1. So overall, only cases where answer is -1 are when W >= 0 and no day in first D days meets condition.
But wait, is it possible that W > 0 but still after many cycles, due to fluctuations within a cycle, we might go below T? Consider: weight at day k = S + qW + pref[r]. For a fixed r, as q increases, weight increases. So for each r, weight is monotonic increasing with q. So if at q=0 (first occurrence) weight > T, then at q>=1 weight will be even larger, so > T. So that residue never yields. So if for all residues r (including r=0 as q>=1) weight > T at their first occurrence, then it never happens. So indeed only first cycle matters. However, is it possible that the first occurrence for r=0 is at q=1 (k=D), which is part of first D days? Actually first D days include k=1..D, so r from 1..D. r=0 corresponds to k=D? Let's check: k = D gives r = D % D = 0? Actually for k=D, write k = qD + r with 0 <= r < D. If we set q=1, r=0? But then k=1*D+0 = D. But we can also write q=0, r=D? But in standard division, remainder is 0..D-1. So when k is multiple of D, r=0, q = k/D. So for k=D, r=0, q=1. That day is not included if we only consider k=1..D with r>0? Actually k=D is in the set of days 1..D, yes because D is within 1..D. But our representation with q=0, r = D is not allowed because r must be < D. So the unique representation is q=1, r=0. So the day D corresponds to r=0 with q=1. So it's not covered by q=0 for any r. So when we talk about first D days (k=1..D), it includes r values: for k=1 to D-1, r = k (since q=0); for k=D, r=0 (q=1). So if we only check q=0 for r>0, we miss k=D (r=0). So we must also consider k=D separately? But it's part of first D days anyway. So we can simply simulate or check all k from 1 to D? That would be O(D) time which is okay (100k). So to determine if there is a solution when W >= 0, we can just iterate k=1..D and compute cumulative sum and check if <= T. That's O(D). If none, answer -1.
But careful: Could there be a scenario where W > 0 but the first occurrence for some residue r>0 is > T, but later for the same residue, weight might go down? No, because weight for a fixed r is S + pref[r] + qW. Since W>0, it's increasing in q. So the minimum for that residue is at q=0 (first occurrence). So if that minimum > T, then later values > T. For r=0, minimum is at q=1? Actually for r=0, the sequence is days: D, 2D, 3D,... weight = S + qW, with q>=1. The minimum is at q=1 (since W>0, increasing). So we need to check k=D as well. So the first D days cover all minima: k=1..D-1 (r>0, q=0) and k=D (r=0, q=1). So indeed checking first D days suffices.
Thus algorithm:
-
Read S, T, D, array w[0..D-1].
-
Compute prefix sums pref[i] for i=0..D, where pref[0]=0, pref[i+1]=pref[i]+w[i] (so pref[i] sum of first i elements, for i from 0 to D). For convenience, pref[r] for r in 0..D where r corresponds to number of steps: after r steps (r from 0 to D), weight = S + pref[r]. But note: r=0 corresponds to day0, not considered. So for steps r=1..D, weight = S+pref[r]. Also day D steps: weight = S+pref[D] (since pref[D] = W). So that's fine.
-
Compute W = pref[D].
-
If W >= 0:
- For r=1..D: if S + pref[r] <= T, then answer = r (immediately return). (Because we need smallest day; we can check in increasing order and break at first found).
- If none, answer = -1.
-
Else (W < 0):
- We need to compute minimal k over residues r=0..D-1? But careful: r should correspond to remainder after division by D, i.e., r in 0..D-1. However, our pref array is defined for steps r (0..D). For a given remainder r (0 <= r < D), the weight after qD + r steps (with q>=0) is S + qW + pref[r] if r>0? Wait careful: For k = qD + r, if r > 0, then the sum of first k steps is qW + pref[r] (since the first q full cycles sum to qW, then the first r steps of the next cycle sum to pref[r]). If r = 0, then k = qD, and sum = qW + pref[0] = qW (since pref[0]=0). However, note that for r=0, the representation uses q>=1 for k>0. That matches: q can be 0 only if k=0. So for r=0, we consider q>=1. So we need to consider q>=0 for r>0, and q>=1 for r=0? But our formula q_r = ceil(A_r/(-W)) might give q=0 for r>0 if A_r<=0. For r=0, A_0>0 always, so q>=1 automatically. However, if we use formula q = ceil(A_r/(-W)), with A_r = S + pref[r] - T, we need to ensure that for r=0, we don't allow q=0. But since A_0 > 0, q will be at least 1. So okay. Also note that for r>0, if A_r <=0, q=0 gives k=r, which is correct.
But we must define pref[r] for r from 0 to D-1 (remainders). For r>0, pref[r] is sum of first r steps (w0..w_{r-1}). That's fine. For r=0, pref[0]=0. So we can iterate r from 0 to D-1.
But careful: What about r = D? That's not a remainder; remainder 0 covers that. So we don't need to separately consider r=D. So our loop for residues: r = 0,1,...,D-1.
For each r:
- let add = pref[r] (pref computed for r, using same array of length D, with pref[0]=0, pref[i] for i>=1).
- let A = S + add - T.
- if A <= 0: candidate = r if r>0? But if r=0 and A<=0 cannot happen. Actually if A <= 0 and r>0, candidate = r. If r=0 and A<=0, that would mean S <= T, but S > T given, so ignore.
- else: q = ceil(A / (-W)). Since -W > 0. candidate = q * D + r.
Take the minimum candidate.
But we must be cautious: For r=0, candidate = qD + 0 = qD. But if q=0 (impossible) would be 0. So fine.
Now we need to compute pref array of length D+1? Actually for r up to D-1, pref[r] is sum of first r elements, which we can compute with prefix sums.
However, note that for r = D? Not needed. But we also need to consider day D? Actually day D corresponds to r=0, q=1. Our residue r=0 with q>=1 covers that. So minimal candidate for r=0 might be q=1 if A_0 <= -W? Let's check: A_0 = S - T. Since W<0, -W>0. q = ceil((S - T)/(-W)). For example, if S - T = 5, -W = 10, then ceil(5/10) = 1? Actually ceil(0.5) = 1. So q=1, candidate = D. That's correct: after one full cycle, weight = S+W, and if that is still >T, we may need more cycles.
Now does this method always give the minimal day? Consider scenario where for some r>0, the minimal q_r might be 0 if A_r <=0, giving candidate r (within first cycle). That's fine. But also consider scenario where for some r>0, A_r > 0, but the computed q_r might be, say, 3, giving candidate 3D + r. Could there be a scenario where a different residue with a larger q but smaller r yields a smaller overall k? Yes, that's why we take min over all r. So it's correct.
But we must also consider that the process stops immediately when weight <= T. So the first day when condition holds is the minimal k. Since the set of all days is exactly {q*D + r: q>=0, r=0..D-1, with (q,r) not both zero} (though (q=0,r=0) is day 0). So enumerating all residues and computing the earliest q for each gives the earliest day for that residue. Taking the global min gives answer.
Now complexity O(D) for prefix sums and O(D) for loop, which is fine.
But need to be careful about integer overflow: A can be up to about 1e10, -W up to 1e10, product q*D may be up to 1e15, which fits in 64-bit signed. Use 64-bit (long long in C++). Also note that prefix sums may be up to 1e10, so use 64-bit for pref as well.
Edge Cases:
- D=1: Then residues r=0 only. W = w0. If w0 >= 0: check day1 (r=0? Actually day1 corresponds to k=1, which is q=1, r=0 because D=1. Our loop for r=0 will compute candidate with q = ceil((S - T)/(-W)) if W<0. That's fine. If W>=0: we need to check day1 (k=1) which is S + w0. That's covered by loop r=1..D? For D=1, r=1 would be out-of-range if we define pref of length D? Actually in the simple method for W>=0 we can just simulate days 1..D: for day i from 1 to D compute cumulative sum cum += w_{i-1}? We'll do simpler: maintain current weight, iterate day=1..D: weight = weight + w[(day-1)%D] and check. That is O(D). That is simpler and avoids confusion.
But note: for D=1, r=1 is not a remainder; the remainder set is {0}. The day1 is r=0 with q=1. So to handle W>=0 uniformly, we can just iterate k=1..D and check if S + prefix[k] <= T, where prefix[k] is sum of first k w_i. That works because for k=D, prefix[D]=W. So we can define prefix array up to D inclusive. So we can just check for k=1..D: if S+pref[k] <= T, answer = k; else if none, answer = -1. That's correct for any D. So for W>=0, we can do that.
For W<0, we use residues r=0..D-1 as described. However, note that for k=1..D, those correspond to (q=0, r=k) for k<D, and (q=1, r=0) for k=D. In our residue method, for r>0 we consider q=0, and for r=0 we consider q>=1. So indeed our candidate set includes all days k>=1. But we must also consider that for r>0, the q could be 0 only? Actually for r>0, the smallest q is 0, but we also have q>0 possibilities for same r. Our formula picks the smallest q for which condition holds. That gives the earliest day for that residue. So it includes the possibility that condition may hold for q=0 (first cycle) or later cycles. So that covers day D? Day D is r=0, q=1, not covered by r>0. So it's included as r=0. So we need to include r=0. So loop r=0..D-1.
But careful: For r>0, the first occurrence is at q=0 (k=r). Our formula with A_r <= 0 yields q=0; else compute q>0. That is correct.
Now is there any day that is not represented? Days > D: For example, day D+1 corresponds to q=1, r=1. That's r=1 with q=1. Yes included.
Thus algorithm:
-
Read S, T, D, w[0..D-1].
-
Compute prefix array pref of length D+1: pref[0]=0; for i from 1 to D: pref[i] = pref[i-1] + w[i-1].
So pref[i] = sum of first i w's.
Then W = pref[D].
-
If W >= 0:
For day = 1 to D:
if S + pref[day] <= T: output day; return.
Output -1.
-
Else (W < 0):
Let negW = -W (positive). Initialize answer = INF (some large number, e.g., 9e18).
For r = 0 to D-1:
add = pref[r] (pref[0]=0)
A = S + add - T
if A <= 0:
if r == 0: // can't happen since S > T, but still for safety skip
// but if S <= T would be 0, but not here
continue
else:
cand = r // because q=0
else:
q = (A + negW - 1) / negW // ceil division
cand = q * (long long)D + r
if cand < ans: ans = cand
Output ans.
But wait: For r=0, add=0, A = S - T > 0, so else branch, compute q, cand = q*D. That's correct.
Now we need to verify with examples.
Example1: S=65 T=60 D=3 w=[-2,3,-4]. W = -3 (<0). negW=3.
pref: [0, -2, 1, -3]? Actually compute: pref[0]=0, pref[1]=-2, pref[2]= -2+3=1, pref[3]=1-4=-3. So for r=0: add=0, A=65-60=5, q = ceil(5/3)=2, cand=23+0=6. But answer should be 4. Let's check other residues:
r=1: add=-2, A = 65-2-60=3, q = ceil(3/3)=1, cand = 13+1=4.
r=2: add=1, A = 65+1-60=6, q = ceil(6/3)=2, cand = 23+2=8? Actually 23+2=8. So min cand = 4. So ans=4. Good.
Example2: S=65 T=60 D=3 w=[-2,10,-3]. W = 5 (>=0). Check days: day1:65-2=63>60; day2:63+10=73>60; day3:73-3=70>60. None <=60, so -1. Output -1. Good.
Example3: S=100000 T=1 D=1 w=[-1]. W=-1 (<0). negW=1. pref[0]=0. r=0: add=0, A=100000-1=99999, q=ceil(99999/1)=99999, cand=99999*1+0=99999. ans=99999. Output 99999. Good.
Example4: S=60 T=59 D=1 w=[-123]. W=-123 (<0). negW=123. r=0: A=60-59=1, q=ceil(1/123)=1? Actually 1/123 = 0.008..., ceil =1. cand=1*1+0=1. So output 1. Good.
Now test edge cases: W negative but A <=0 for some r>0: e.g., S=100 T=50 D=2 w=[-60,10] => W=-50 (<0). pref: [0, -60, -50]. For r=1: add=-60, A=100-60-50=-10 <=0 -> cand = r=1. So answer=1, which is correct: day1 weight = 40 <= 50. Good.
What about scenario where A>0 for all r, but some r yields very small q? Good.
But we must consider the possibility that for some r, the computed q might be 0? Actually if A <=0, we set q=0. For r>0, we then set cand=r. But what if r=0 and A<=0? That's impossible given S>T.
Now we must also consider the possibility that answer could be extremely large, but we handle with 64-bit.
Potential pitfalls: For W<0, the method assumes that weight is S + q*W + pref[r]. However, we must ensure that we are not missing the scenario where the condition is satisfied during a cycle before completing the full q cycles? That's exactly captured by q cycles then r steps. Because after q full cycles, we start a new cycle and take r steps. But what about the situation where we stop in the middle of the first cycle? That's q=0, r steps. So it's covered. So good.
One more nuance: For r=0, q cycles then 0 steps means we stop exactly at the end of q cycles. That is day q*D. That's included.
Thus algorithm is correct.
But wait, is there any scenario where W<0 but the minimal day occurs at a residue r, but the computed q for that r using ceil division may not actually be the smallest q such that condition holds? Let's verify: Condition: S + qW + pref[r] <= T. Since W < 0, the left side is decreasing in q (since adding negative). So as q increases, left side decreases. For integer q, we want the smallest q such that inequality holds. Because left side is monotonic decreasing, we can binary search or use ceil division. The condition can be rearranged: q >= (T - S - pref[r]) / W? But careful: dividing by negative. However, using A = S+pref[r]-T > 0, we have: S+pref[r] + qW <= T => qW <= T - S - pref[r] = -A. Since W < 0, multiply both sides by -1: -qW >= A => q*(-W) >= A. Since -W > 0, q >= ceil(A / (-W)). Yes, because q must be integer. And because left side increases with q, the smallest q satisfying is indeed ceil(A/(-W)). So correct.
Now we need to consider whether the condition can be satisfied at q=0? That would be when A <= 0. So yes.
So algorithm is sound.
Now check for W = 0. In that case, our method for W>=0 would simply iterate days 1..D and check. If none, -1. That's correct.
Thus implementation is straightforward.
But we must be cautious: D can be up to 100k, which is fine.
Now about potential overflow in prefix sums: Each w_i up to 1e5, D up to 1e5, so sum up to 1e10, fits in 64-bit. But we must use long long for pref.
Similarly, A can be up to S + pref[r] - T, up to ~1e10 + 1e5, still fits 64-bit.
NegW = -W up to 1e10, fits.
q up to about (1e10) / 1 = 1e10, times D up to 1e5 gives 1e15, fits in 64-bit (2^63-1 ~9.22e18). So we can use unsigned long long or just signed long long. Use long long (signed). But ensure that (A + negW - 1) might overflow beyond 2^63-1? A and negW up to 1e10, so sum up to 2e10, safe. qD could be up to 1e101e5 = 1e15, still safe. So ok.
Now we also need to account for the fact that for r=0, we may get cand = q*D, which may be same as some other r? fine.
Edge case: W negative but very small magnitude (e.g., -1). For some r, A could be huge, q = A (since negW=1). Then cand = A*D + r. That's huge but valid.
One more nuance: Could it be that for some r, the minimal q yields a day that is not actually reachable because the process would have stopped earlier at a different day? But we are computing the earliest day for that residue, but the overall earliest day across residues is the global minimum. Since we take the min across residues, that yields the earliest day overall, because if any day occurs earlier, it would correspond to some residue with some q, and we compute that day as a candidate. But need to ensure that for each residue, we compute the minimal day for that residue. Yes. So the global min is correct.
But is there any possibility that the earliest day overall occurs for a residue r, but the q we compute using ceil(A/(-W)) is not actually the minimal q because the condition might be satisfied earlier due to the fact that during the r steps, the weight might have dropped below T before completing all r steps? Wait, our representation captures day as q full cycles plus r steps. But what about stopping at some intermediate step within those r steps? That would be a day with the same q but smaller r'. For a given q, the sequence of days after q full cycles is: day qD + 1, qD + 2, ..., qD + r, ... The weight after each step is S + qW + pref[step] where step from 1 to D. If at some step s < r, the weight already <= T, then the process would have stopped at day qD + s, not at day qD + r. So for a given q, the first time within that cycle that weight <= T would be the smallest s such that S + qW + pref[s] <= T. So if we only consider for each residue r the day when we have taken exactly r steps after q cycles, we might miss a day where condition holds at an earlier step within the same cycle. However, since we are taking the minimum over all (q,r), we will consider all possible (q,r) pairs, not just the specific r for a given q. But we are computing for each residue r, the minimal q such that S + qW + pref[r] <= T. But for that q, it's possible that for some smaller r' (< r) also satisfies the condition at the same q? But if that were true, then for residue r', the minimal q' might be <= q, and the day qD + r' could be less than qD + r. So the global minimum would be captured by that r' with its own minimal q'. However, is it possible that for a given residue r, the minimal q that makes pref[r] condition true is q0, but for some smaller r' (< r) the condition becomes true at the same q0, but maybe at an even earlier day? But since we compute for all residues r', we will also compute q' for r' (maybe equal to q0 or smaller). So min will include that. But the concern is: Could there be a scenario where for some residue r, the first time the condition is satisfied is at some q, but within that same cycle, the condition was satisfied at an earlier step but for a different q? Actually if for a given q, condition holds at step r, it might also hold at step s > r? Not relevant. The earliest step in that cycle where condition holds would be the smallest s such that condition holds. That s will be some residue. And we compute for that residue s the minimal q (which could be the same q if condition holds for s at q, or maybe a smaller q if it already held earlier). So our method will capture that.
But is it possible that the condition first holds at a step s, but for that residue s, the minimal q required is not the same as the q we are considering because maybe at a smaller q, condition does not hold for s, but at the current q it does. However, could it be that at the same q, condition holds for s, but also for a smaller step t < s, condition does NOT hold? Actually if weight decreases (or increases) monotonically within a cycle? The weight within a cycle is not necessarily monotonic because w_i can be positive or negative. So it's possible that at a given q, weight after s steps is <= T, but after fewer steps (t < s) weight > T. That is possible if there are positive increments early and negative later. Example: w = [10, -20]. Starting S=100, T=90. After q=0 cycle: step1: 110 >90, step2: 90 <=90. So condition holds at step2 but not step1. That's fine. Our method for residue s=2 will compute q=0 (since A_2 = S+pref[2]-T = 100+(-10)-90=0? Actually pref[2]=10-20=-10, S+pref2=90 <= T so A=0 => q=0, cand=2). For residue s=1, A= S+pref1 - T = 100+10-90=20 >0, so q would be ceil(20/(-W))? W = -10, -W=10, q= ceil(20/10)=2? Actually W = 10-20 = -10, negW=10. q = ceil(20/10)=2, cand=2*2+1=5. So min is 2. So correct.
Now consider scenario where the first time condition holds is at q=1, step s, but maybe at q=0, condition did not hold for any step. For residue s, we compute q_s = ceil(A_s/(-W)). That yields q=1. That's correct. But what if for the same q=1, condition holds at step s, but also holds at a later step? Not important.
What if condition holds at q=1, step s, but at the same q, step t < s also holds? Then the first step in that cycle would be t, not s. So we would have computed for residue t its own q_t which might be <=1? If at q=1 condition holds for t, then either at q=0 condition also holds for t (if A_t <=0) or else q_t might be 1. So min would be t with q=1 or 0. So we will capture.
Thus our method of enumerating residues and computing minimal q works.
But we must also consider that for residue r=0 (end of full cycles), the condition is S + q*W <= T. This may become true at some q, but maybe within that cycle (before completing the full cycle) condition becomes true earlier. Our min across residues will catch that because for some r>0, the condition may hold at the same q or earlier.
Thus correct.
Now complexity O(D). Memory O(D) for w and pref.
Implementation details:
-
Use long long for S, T, D? S, T up to 100k, but use long long to be safe for calculations.
-
Input D up to 100k, so store w in vector maybe, but we need prefix sums in long long.
-
Compute prefix sums: vector pref(D+1, 0). for i in 0..D-1: pref[i+1] = pref[i] + w[i].
-
Compute W = pref[D].
-
If W >= 0:
for day=1..D:
if S + pref[day] <= T: print day; return.
print -1.
-
Else: (W < 0)
negW = -W (long long)
ans = LLONG_MAX (or something huge)
for r=0..D-1:
add = pref[r]
A = S + add - T
if A <= 0:
// r must be >0 because A>0 for r=0 (S>T)
if r == 0: continue; // not possible but safe
cand = r
else:
q = (A + negW - 1) / negW // ceil division
cand = q * D + r
if cand < ans: ans = cand
print ans
But need to ensure for r=0, we compute cand = q*D, which is correct.
Now test some more cases.
Case: S=10, T=5, D=2, w=[-1, -1]. W=-2 (<0). pref: [0,-1,-2]. For r=0: add=0, A=10-5=5, negW=2, q=ceil(5/2)=3, cand=32+0=6. r=1: add=-1, A=10-1-5=4, q=ceil(4/2)=2, cand=22+1=5. r=2? Not included; r max 1. So ans=5. Simulate: day1:9; day2:8; day3:7; day4:6; day5:5 <=5. So answer 5 correct.
Case: S=10, T=5, D=2, w=[-2, 1]. W=-1. pref: [0,-2,-1]. r=0: A=5, negW=1, q=5, cand=52=10. r=1: add=-2, A=10-2-5=3, q=ceil(3/1)=3, cand=32+1=7. So ans=7. Simulate: day1:8; day2:9 (since 8+1=9); day3:7 (9-2=7); day4:8 (7+1=8); day5:6 (8-2=6); day6:7 (6+1=7); day7:5 (7-2=5) => day7. So correct.
Case: S=100, T=1, D=3, w=[-10, -10, -10] (W=-30). pref: [0,-10,-20,-30]. r=0: A=99, negW=30, q=ceil(99/30)=4 (since 330=90<99, 430=120>=99), cand=43=12. r=1: add=-10, A=100-10-1=89, q=ceil(89/30)=3 (330=90>=89), cand=33+1=10. r=2: add=-20, A=100-20-1=79, q=ceil(79/30)=3 (90>=79), cand=33+2=11. So ans=10. Simulate: day1:90; day2:80; day3:70; day4:60; day5:50; day6:40; day7:30; day8:20; day9:10; day10:0? Actually day9: after 9 steps: start100 + (-10)*3? Let's simulate properly:
day0:100
day1:90
day2:80
day3:70
day4:60 (w0)
day5:50 (w1)
day6:40 (w2)
day7:30 (w0)
day8:20 (w1)
day9:10 (w2)
day10:0 (w0) -> <=1? Actually 0 <=1, so stop day10. Our answer 10 matches. Good.
Now consider scenario where the answer is found in first cycle for some r, but we compute for W<0 also. That's fine.
Now consider W positive but answer found in first cycle: we handle with the W>=0 branch.
One more nuance: Could there be a scenario where W = 0, but answer occurs at day D (which is end of first cycle) with S+W <= T? That's covered because day D = D, we check day D in loop. So good.
Now also need to consider that for W<0, we might compute ans that is extremely large but we should output as integer. That's fine.
Now, think about potential pitfalls with large numbers and integer division: (A + negW - 1) may overflow if A and negW are near 2^63? But they are at most about 1e10, so safe.
Now, any edge case where A is negative but r=0? Not possible.
Now consider that D can be 1 and W<0. Already tested.
Now consider that D can be 100000 and w_i all zero except one negative? That's fine.
Now test a case with W negative but some residues have huge A leading to q and then cand > 2^63? Let's check maximum possible A: S max 100000, pref[r] max positive: if all w_i are max 100000, then pref[r] = 100000 * r, with r up to D-1 (99999) gives 100000*99999 = 9,999,900,000. So A max ~10^10. -W could be as small as 1. So q max ~10^10. D max = 100000. So cand max = 10^10 * 100000 + 99999 = 10^15 + 1e5 approx. That's about 1e15, which fits in 64-bit (less than 9e18). So safe.
Now verify that for W negative, we are not missing any days from the first D days because we include r>0 with q=0 and r=0 with q=1? Actually r=0 with q=1 corresponds to day D. But we include r=0 with q computed as ceil(A/(-W)). For first cycle, if the weight after D days (S+W) is <= T, then A_0 = S-T, but wait that condition is about S+W <= T, not directly A_0. Let's check: For day D, condition is S+W <= T. Our method for r=0 gives q = ceil((S - T)/(-W)). If S+W <= T, then S - T <= -W => (S-T) <= (-W). So ceil((S-T)/(-W)) <= 1? Actually if S-T <= -W, then (S-T)/(-W) <= 1. Since (S-T)>0, division yields a positive number <=1, so ceil gives 1. So cand = 1*D = D. Good. So we capture day D as candidate. So our method for W<0 includes day D via r=0 and q=1 if appropriate.
But note: Could day D be reached earlier? No, it's exactly day D.
Now what about day 0? Not considered.
Thus algorithm appears correct.
But we must also consider the possibility of W < 0 but the process may stop before we even complete the first full cycle, i.e., at some r>0 with A <= 0, cand = r. That's included.
Now what about the scenario where W < 0 but during the first cycle, we never go <= T, but later at some cycle, we might go <= T at a step before completing the full cycle, which is covered.
Now, check for correctness of the formula for A_r for r>0: add = pref[r]. This is sum of first r w_i. For day = qD + r, the weight = S + qW + pref[r]. Yes, because after q full cycles, weight = S + q*W. Then we add the first r w_i of the next cycle. So correct.
Now, we need to be careful about integer division with negative numbers? We are using A>0 and negW>0, so fine.
Now, double-check the derivation for q when A > 0:
We need smallest integer q >= 0 such that q * (-W) >= A.
Thus q = ceil(A / (-W)). Since both positive, we can compute as (A + (-W) - 1) / (-W). Good.
Now, ensure that for r=0, this q may be 0? If A <= 0, q = 0, but that would mean S <= T, not happening. So for r=0, we always have A > 0, so q >= 1.
Now, also note that when W < 0, we might have some r where pref[r] is very negative, causing A to be negative, then candidate = r. That's fine.
Now, is there any scenario where the answer is so large that it exceeds 2^63 - 1? Let's bound more precisely: A max = S + max(pref[r]) - T. max(pref[r]) is when we take the maximum prefix sum over r=0..D-1. Since w_i are bounded by 100,000, the maximum prefix sum could be up to 100,000 * 100,000 = 1e10. S up to 1e5, T min 1? Actually T could be 1. So A max approx 1e10 + 1e5 - 1 = about 1.00001e10. -W min? Since W can be as low as -1e10, -W max = 1e10. But -W could be as small as 1? Yes if W = -1. So q max = ceil(1.00001e10 / 1) ≈ 1.00001e10. D max 1e5. So k max ≈ 1.00001e10 * 1e5 + 1e5 = about 1.00001e15, which is < 9e18. So safe.
Now, is there any scenario where A may be negative but we still compute q? No, we handle separately.
Now, maybe we can also solve using binary search on days? But O(D) is sufficient.
Now, we should also consider the possibility of early termination in the W>=0 branch: we can break at first found since days increase. So O(D) worst-case.
Now, we need to pay attention to the fact that D can be as large as 100k, but the w_i may be negative, positive. Our algorithm uses O(D) time, which is fine.
Now, consider edge case: D = 100000, but w_i all negative, W negative large. Then we loop over D and compute candidates, each with simple arithmetic. That's fine.
Now, we must ensure that we use 64-bit integers for q*D. So cand = q * D + r; D is up to 100000, but q is long long; product may overflow if we use int. So cast: (long long) q * D + r, all long long.
Now, we need to be careful about reading input: w_i could be negative.
Now, test additional tricky cases:
Case: W=0 but within first cycle we have weight <= T. Example: S=10, T=5, D=2, w=[-10, 10]. W=0. pref: [0,-10,0]. Day1:0? Actually S+pref[1]=10-10=0 <=5 -> answer 1. Our W>=0 branch checks day1..2 and will output 1. Good.
Case: W=0 but no day <= T. Example: S=10, T=5, D=2, w=[10, -10]. Day1:20>5, Day2:10>5. So -1. Good.
Now, W negative but with some pref[r] very high positive, may cause A huge. Already considered.
Now, what about scenario where S and T are large but difference small, and W negative but large magnitude, answer could be day D? Eg: S=1000, T=999, D=5, w = [-1000, 0,0,0,0]? Wait w_i within range. w0 = -1000 (allowed if -100000?), S=1000 -> day1:0 <=999? Actually 0 <=999, so answer 1. That's fine.
Now, let's think about correctness regarding the earliest day. For W<0, we compute for each residue the minimal q. However, is it possible that the minimal day occurs at a residue r, but with a q that is not the minimal q for that residue because maybe the condition is satisfied at a larger q but also at a smaller day due to smaller r? No, because for a given residue, days are strictly increasing with q. So the minimal day for that residue is indeed with minimal q satisfying condition.
Now, but there is a subtle point: When we compute q for residue r using ceil(A/(-W)), we assume that the condition is S + qW + pref[r] <= T. However, what if for that q, during the r steps, the weight might have dropped below T earlier within that cycle? But that would mean that for some r' < r, the condition holds at the same q. In that case, for that r', the minimal q' might be <= q. So the global min would be at most qD + r' which is less than qD + r. Therefore, our min across residues will capture the earlier day. However, there is a scenario where for a given residue r, the minimal q computed is q0, but for some smaller residue r' (< r), the condition might become true at the same q0 but at a step after r? Actually r' < r, so step r' is earlier in that cycle. So if condition holds at r' with same q0, then the day q0D + r' < q0D + r. So we would have computed for r' its own q, which could be <= q0 (maybe exactly q0 if it doesn't hold earlier). But could it be that for r', the minimal q' is actually > q0? That would mean that condition does not hold at q0 for r', but does hold at q0 for r. That is possible if the weight at step r' is higher than T, but at step r it becomes lower due to later negative increments. So the first time in that cycle (q0) that condition becomes true is at step r. So that's the earliest day in that cycle. So then q0D + r is the earliest day in that cycle. But could there be a later cycle (q < q0) where condition holds at some step? No, because q0 is minimal for that residue r, but maybe for some other residue s, the minimal q is less than q0. That's why we take min across residues.
Thus algorithm yields correct.
Now, can we optimize by only considering residues where pref[r] is minimal or something? Not necessary.
Now, verify with more examples.
Let's do a random test: S=100, T=80, D=3, w = [5, -20, -10]. W = -25. pref: [0,5,-15,-25]. Compute candidates:
r=0: A=20, negW=25, q=ceil(20/25)=1, cand=3.
r=1: add=5, A=100+5-80=25, q=ceil(25/25)=1, cand=13+1=4.
r=2: add=-15, A=100-15-80=5, q=ceil(5/25)=1, cand=13+2=5.
So ans = 3? But is day3 weight? Simulate: day1:105; day2:85; day3:75 -> day3 weight 75 <=80? Actually 75 <=80, so answer should be 3. Our candidate from r=0 is day3. That matches. However, note: day3 is after 3 steps, which is q=1, r=0? Because 3 = 1*3+0? Actually q=1, r=0 corresponds to day 3, yes. So r=0 yields candidate 3. So correct.
Now test: S=100, T=80, D=3, w=[5, -10, -20]. W=-25 (same total). pref: [0,5,-5,-25]. r=0: A=20, q=1, cand=3. r=1: add=5, A=25, q=1, cand=4. r=2: add=-5, A=100-5-80=15, q=ceil(15/25)=1, cand=5. ans=3. Simulate: day1:105, day2:95, day3:75 -> day3 75 <=80. So ok.
Now test: S=100, T=90, D=2, w=[-5, 3]. W=-2. pref: [0,-5,-2]. r=0: A=10, negW=2, q=ceil(10/2)=5, cand=52=10. r=1: add=-5, A=100-5-90=5, q=ceil(5/2)=3, cand=32+1=7. So ans=7. Simulate:
day1:95, day2:98, day3:93, day4:96, day5:91, day6:94, day7:89 <=90? Actually day7 weight: start 100, after steps:
1:95,2:98,3:93,4:96,5:91,6:94,7:89 -> yes 89 <=90, stop day7. So answer 7 matches.
Now test: W>0 but maybe answer lies at day D? Already covered.
Now consider scenario where W=0 but answer is at day D? Example: S=10, T=5, D=2, w=[-10,10]. Day1:0 <=5, answer 1, not D. Another: S=10, T=5, D=2, w=[5, -5] -> day1:15>5, day2:10>5 => -1. So fine.
Now, we must also consider the possibility of integer overflow in prefix sums when computing pref array if we use int. Use long long.
Implementation steps:
- Read S, T, D.
- Create vector w(D). Read them.
- Compute vector pref(D+1); pref[0]=0; for i=0..D-1: pref[i+1]=pref[i]+w[i].
- long long W = pref[D].
- If W >= 0:
for (int day=1; day<=D; ++day) {
if (S + pref[day] <= T) { cout << day << endl; return 0; }
}
cout << -1 << endl; return 0;
- Else:
long long negW = -W;
long long ans = LLONG_MAX; // from or use 1e18
for (int r=0; r<D; ++r) {
long long add = pref[r];
long long A = S + add - T;
long long cand;
if (A <= 0) {
// Since S > T, r cannot be 0 when A <= 0, but for safety we skip r==0.
if (r == 0) continue; // actually A <=0 for r=0 would mean S <= T, not possible.
cand = r;
} else {
long long q = (A + negW - 1) / negW; // ceil division
cand = q * D + r;
}
if (cand < ans) ans = cand;
}
cout << ans << endl;
But need to handle the case where r=0 and A <=0? Not possible, but if it happened due to some weirdness, we would output 0? That's invalid because we need day >0. So skip.
Also note that for r>0 and A <=0, cand = r. That's correct.
Now check for r=0 and A <=0: if S <= T, then answer should be 0? But problem states S > T, so not needed.
Now, we must ensure that for W<0, we don't miss the case where the answer might be exactly day D (r=0, q=1). That's covered.
Now, think about possible off-by-one errors: The prefix array: pref[0] = 0, pref[1] = w0, pref[2] = w0+w1, etc. So after r steps, weight = S + pref[r]. That's correct for day r.
Now, for day = qD + r, with r in [0, D-1], weight = S + qW + pref[r] (if r>0) or S + qW (if r=0). Because after q full cycles, weight = S + qW. Then we apply the first r steps of the next cycle, adding pref[r]. Yes.
Now, what about day = D? That's q=1, r=0. So weight = S + 1*W + pref[0] = S+W. Good.
Now, for day = 2D? That's q=2, r=0, weight = S+2W.
Now, for day = 2D+1, that's q=2, r=1, weight = S+2W + pref[1].
Thus formula correct.
Now, what about day = 0? Not considered.
Now, we need to confirm that the answer for W<0 cannot be 0. Since S > T, we need at least 1 day.
Now, we should also test a case where pref[r] is extremely negative, making A negative, but r is large; cand = r may be large but still maybe larger than some other candidate with q>0. Example: S=1000, T=1, D=3, w = [-1000, 1000, -1000]. W = -1000? Actually -1000+1000-1000 = -1000. pref: [0, -1000, 0, -1000]. For r=1: add=-1000, A=1000-1000-1=-1 <=0, cand=1. So answer 1 because day1 weight = 0 <=1. Good.
Now, what about case where multiple residues produce same candidate? Not an issue.
Now, consider potential overflow in q*D: q up to 1e10, D up to 1e5, product 1e15. Use long long.
Now, also check for type of D: D is int up to 100k, but when multiplied by q (long long), we need to cast to long long. In C++, q * D will be computed using the types: q is long long, D is int, result is long long. So fine.
Now, confirm with example 1: we already did.
Now, let's test with a case where W<0 but answer is not found in first D days and we need to compute across residues.
Consider S=100, T=1, D=2, w=[99, -100]. W=-1. pref: [0,99,-1]. r=0: A=99, negW=1, q=99, cand=992=198. r=1: add=99, A=100+99-1=198, q=ceil(198/1)=198, cand=1982+1=397. So ans=198. Let's simulate to see if day 198 is correct? Starting weight 100. Net change per 2 days is -1. After 198 days = 99 full cycles (since 198/2=99). After 99 cycles weight = 100 + 99*(-1) = 1. That's day 198? Actually after 99 cycles (198 days) weight = 1, so day198 weight = 1 <= T, stop. So answer 198 matches. Could there be earlier? Maybe day 197? After 98 cycles (196 days) weight = 100+98*(-1)=2, then day197 = weight + w0 = 2+99=101 >1, not <=; day198 = 2+99-100? Actually careful: after 98 cycles: weight = 2 at day 196. Then day197: add w0 (99) -> 101 >1; day198: add w1 (-100) -> 1 <=1. So yes first is day198. So algorithm works.
Now, consider w where pref[r] is negative for some r, but still A positive, but the minimal day might be with r=0. Fine.
Now, test where multiple residues have same minimal day? Not relevant.
Now, confirm for W=0 but answer -1: Already done.
Now, need to consider potential pitfalls with large answer: The answer may exceed 32-bit, so output must be as long long (in C++, use cout with long long).
Now, think about the scenario where W < 0 but the weight at day 1 is already <= T: then r=1 A = S + w0 - T <= 0, so candidate = 1, and ans = 1. Good.
Now, verify that the algorithm doesn't incorrectly compute cand for r=0 when q computed is 0? As argued, A>0, so q >=1. So cand >= D. That's fine.
Now, consider when D=0? Not possible because D >=1.
Now, final check: For W < 0, the computed answer could be extremely large (up to 1e15). The problem output expects an integer, so fine.
Now, what about scenario where W < 0 but S + pref[r] is huge positive and -W is also huge? q will be small.
Now, implement.
But before finalizing, there might be a nuance: For r=0, we compute add = pref[0]=0. But is it always the case that after q cycles, weight = S + qW, and then we take r=0 steps (i.e., no further steps) meaning we stop exactly at the end of the q-th cycle. That's day qD. That's correct.
Now, we must also consider that the process might stop in the middle of a cycle even before completing the full r steps for a given q. But as argued, that would be captured by a smaller r for the same q. Since we compute for all r, that day will be considered.
Now, double-check the formula for q: q = ceil(A / (-W)). Since A and -W are positive. Suppose A=0? That would mean S+pref[r] = T exactly. Then A <= 0, we would go to the branch A<=0 and set cand = r (with q=0). That's correct because we stop at that step in the first cycle. So we don't need to compute q.
Now, what about the case where A > 0 and -W divides exactly A? Then q = A/(-W). That's integer, and condition holds exactly at that q: S + q*W + pref[r] = T. So that's correct.
Now, is there any situation where the weight might become <= T at some day that is not of the form q*D + r? That's exactly all days.
Now, consider if D is 1: then residues r=0 only. So loop runs once, r=0. add=0, A=S-T>0, q = ceil(A/(-W)). Since -W = -w0 (positive). cand = q*1+0 = q. That's the answer. Good.
Now, check the potential for precision errors: No, it's integer.
Now, we can also think about optimizing further but not needed.
Now, we should also consider the scenario where W < 0 but the answer is found in the first cycle but not at step r but at step r' that is less than some r we compute. That's fine.
Now, we should also consider scenario where W < 0 but we might have to consider that the process could stop before completing the current day's addition? No, the process updates at the end of the day. So we check after the addition.
Now, all good.
Now, there is an alternative approach: we can simulate day by day until we detect a cycle if weight ever repeats? But we have analytic solution.
Now, check if there is any subtlety when W < 0 but the weight might overshoot T and then later become > T again? That doesn't matter; we stop at first <= T.
Now, final step: Implement.
But before final code, let's test some random small cases by brute force to ensure our algorithm matches.
I'll mentally test some random small D and values.
Case: S=10, T=5, D=3. Let's enumerate possible w's.
We'll write a small brute mentally for a couple.
Test 1: w = [1, -2, 0]. W = -1. pref: [0,1,-1,-1]. Compute:
r=0: A=5, negW=1, q=5, cand=53=15.
r=1: add=1, A=10+1-5=6, q=6, cand=63+1=19.
r=2: add=-1, A=10-1-5=4, q=4, cand=4*3+2=14.
So ans = 14? But let's simulate:
Day1:11
Day2:9
Day3:9 (since 9+0=9)
Day4:10 (9+1=10)
Day5:8 (10-2=8)
Day6:8 (8+0=8)
Day7:9 (8+1=9)
Day8:7 (9-2=7)
Day9:7 (7+0=7)
Day10:8 (7+1=8)
Day11:6 (8-2=6)
Day12:6 (6+0=6)
Day13:7 (6+1=7)
Day14:5 (7-2=5) -> day14 <=5. So answer 14. Matches.
Now test w = [2, -5, 1]. W = -2. pref: [0,2,-3,-2]. r=0: A=5, negW=2, q=ceil(5/2)=3, cand=33=9.
r=1: add=2, A=10+2-5=7, q=ceil(7/2)=4, cand=43+1=13.
r=2: add=-3, A=10-3-5=2, q=ceil(2/2)=1, cand=1*3+2=5.
ans=5? Let's simulate:
Day1:12
Day2:7 (12-5=7)
Day3:8 (7+1=8)
Day4:10 (8+2=10)
Day5:5 (10-5=5) -> day5 <=5. So answer 5. Our algorithm gave candidate for r=2 as 5, correct.
Now test w = [2, -1, -1] => W=0? 2-1-1=0. Actually W=0. Check with our W>=0 branch: pref: [0,2,1,0]. Days: day1:12>5, day2:11>5, day3:10>5 => no answer, -1. Simulate: day1:12, day2:11, day3:10, then day4:12 (since cycle repeats), never below 5, so -1. Good.
Now test w = [2, -10, 5] => W=-3. pref: [0,2,-8,-3]. r=0: A=5, negW=3, q=ceil(5/3)=2, cand=6.
r=1: add=2, A=7, q=ceil(7/3)=3, cand=3*3+1=10.
r=2: add=-8, A=10-8-5=-3 <=0 => cand=2. So ans=2? But day2 weight? Simulate: day1:12, day2:2 (12-10=2) <=5, so answer 2. Yes.
Now test w = [2, -1, -2] => W=-1. pref: [0,2,1,-1]. r=0: A=5, negW=1, q=5, cand=15.
r=1: add=2, A=7, q=7, cand=22.
r=2: add=1, A=6, q=6, cand=20.
But also check r? That's all. ans=15. Simulate: day1:12, day2:11, day3:9, day4:11 (9+2), day5:10 (11-1), day6:8 (10-2), day7:10 (8+2), day8:9 (10-1), day9:7 (9-2), day10:9 (7+2), day11:8 (9-1), day12:6 (8-2), day13:8 (6+2), day14:7 (8-1), day15:5 (7-2) -> day15 =5 <=5. So answer 15. Good.
Now test W positive but answer within first cycle: w = [ -5, 3, -1] => W=-3? Actually -5+3-1=-3 negative. Not that. Let's do w = [ -5, 10, -2] => W=3 (>0). pref: [0,-5,5,3]. S=10, T=5? Actually S=10 T=5, day1:5 <=5 answer 1. Our W>=0 branch: day1: S+pref1 =10-5=5 <=5 -> output 1. Good.
Now test W positive, answer -1: w = [5, -1, -1] => W=3. S=10 T=5. day1:15>5, day2:14>5, day3:13>5 -> none. So -1. Good.
Now test W=0, answer within first cycle but not day D: w = [ -10, 5, 5] => W=0. pref: [0,-10,-5,0]. S=10 T=5? day1:0 <=5 answer 1. Good.
Now test W=0, answer at day D: w = [ 5, -5, 0]? Actually sum=0. S=10 T=5. day1:15>5, day2:10>5, day3:10>5 -> none. So -1. Not an example. To have answer at day D, need S+W <= T but since W=0, S <= T needed, but S > T. So cannot.
Now test W negative but answer at day D: S=10, T=5, w = [ 5, -10 ] => W=-5. pref: [0,5,-5]. r=0: A=5, negW=5, q=1, cand=2. So answer 2. That's day D (since D=2). Simulate: day1:15>5, day2:5<=5 -> yes day2.
Now test with huge numbers: S=100000, T=1, D=100000, w[0]=-1, rest 0. Then W=-1. pref[r] = -1 for r>=1? Actually w0=-1, others 0. So pref[0]=0, pref[1]=-1, pref[2]=-1,... pref[100000]=-1. For r=0: A=99999, negW=1, q=99999, cand=99999100000 = 9,999,900,000? Actually 99999100000 = 9,999,900,000. For r=1: add=-1, A=100000-1-1=99998, q=99998, cand=99998100000+1 = 9,999,800,001. So min is about 9,999,800,001? But maybe r=1 yields earlier? Actually day1? Let's simulate: day1 weight = 99999 >1, not yet. Since each full cycle decreases by 1, after many cycles we eventually reach 1. The earliest might be at some day? Let's compute: after q cycles (qD days) weight = 100000 + qW = 100000 - q. To get <=1, need q >= 99999. So after 99999 cycles, weight = 1 at day 99999100000 = 9,999,900,000. But could we get earlier within a cycle? After q cycles, weight = 100000 - q. Then day qD+1 weight = (100000 - q) + w0 = 100000 - q -1 = 99999 - q? Actually careful: after q cycles, weight = S + qW = 100000 - q. Then next day (first step of next cycle) add w0=-1 => weight = 100000 - q - 1 = 99999 - q. So if at start of cycle weight = 100000 - q, then after day qD+1 weight = 99999 - q. To be <=1, need 99999 - q <=1 => q >= 99998. So for q=99998, after q cycles weight = 2, day = 99998100000 = 9,999,800,000, weight = 2 >1; then day+1 = 9,999,800,001 weight = 2 + (-1) = 1 <=1. So day 9,999,800,001 is earlier than 9,999,900,000. So answer should be 9,999,800,001. That matches our calculation for r=1: q=99998? Let's compute A for r=1: add = pref[1] = -1. A = S + add - T = 100000 -1 -1 = 99998. negW=1, q = 99998. cand = 99998*100000 + 1 = 9,999,800,001. So ans=9,999,800,001. Good.
Now, what about r=2? add = pref[2] = -1, A=99998, q=99998, cand = 99998*100000+2 = 9,999,800,002, which is larger. So min is r=1.
Thus algorithm yields correct huge number.
Now, we must ensure that the answer is within 64-bit. 9,999,800,001 < 2^63-1, good.
Now, potential pitfalls:
- Using int for pref may overflow if we use int. So use long long.
- In the W>=0 branch, we need to use long long for S+pref[day] as well, but S and pref are long long.
Now, confirm that for W>=0, we might also have situation where answer is day D and we break. That's fine.
Now, we need to ensure that the loop for W>=0 stops at the first day found because we need minimal d. That's done.
Now, final nuance: The problem statement says "If his slimming plan takes too many days or even does not end forever, he should reconsider it." So we output -1 if never ends. That's consistent.
Now, also note that the weight may become <= T on day 0? No, because S > T, so not.
Now, one more edge case: D could be 100000, w_i all zero? Then W=0, and S > T, so no day will have weight <= T because weight stays S. So -1. Our algorithm: W>=0, check days 1..D: S+0 = S > T, so none, output -1. Good.
Now, what about w_i all positive? W>0, weight increases, so never <=T. So -1. Good.
Now, what about w_i mix but net negative but some early huge positive may cause A huge? Already handled.
Now, we must consider integer overflow when computing A + negW - 1: A up to ~1e10, negW up to 1e10, sum up to 2e10, fits in 64-bit.
Now, the answer may be 0? Not possible, but just in case, we should ensure we output a positive integer.
Now, think about the possibility that the weight never becomes <= T even if W < 0? For example, if the target T is very low, but weight decreases gradually, it will eventually reach. Since W < 0, the weight tends to -infinity as days increase, so eventually will be <= T. Unless there is some weird cycle where the weight never goes below a certain bound? But if W < 0, after many cycles weight = S + qW + something, as q increases, the term qW dominates and goes to -inf, so definitely will go below any finite T. So always ends.
Thus, only -1 cases are when W >= 0 and no hit in first D days.
But is there a scenario where W = 0 and weight might become <= T after many cycles? No, because weight repeats pattern, so if not in first cycle, never.
Thus algorithm stands.
Now, we can consider writing code.
Implementation:
Potential improvement: For W<0, we might compute ans using min over residues but we could also do a more efficient approach by considering the minimum of (ceil((S+pref[r]-T)/(-W)) * D + r). That's what we have.
Now, ensure that for r=0, we treat as above.
Now, test with provided examples.
Example1: Output 4. Good.
Example2: -1.
Example3: 99999.
Example4: 1.
Now, we can write code.
But wait, there is a subtlety: In the condition for W<0, we compute A = S + pref[r] - T. But what about the scenario where r=0 and we have S + q*W <= T. That's handled.
Now, I think it's correct.
Now, before finalizing, I want to consider if there is any scenario where our method for W<0 could produce a candidate that is less than the actual first day because we assume that at day qD + r, the weight is exactly S + qW + pref[r]. But what about the possibility that during those q cycles, the weight might have already dropped below T before completing the r steps? If that happened, the actual stop day would be earlier than qD + r, so our candidate would be larger than the true answer. However, since we compute for all residues, we would have a candidate for that earlier day from some residue with same q but smaller r, or possibly with smaller q. So the true answer would be captured by some other (q', r') pair. But if our computed candidate for a given (q,r) is not the true first day for that residue because condition would have been satisfied earlier in that same cycle, then that means there exists some r' < r such that at the same q, condition holds. So for that r', we would compute a candidate (maybe with the same q, but if A_r' <= 0? Actually we need to compute the minimal q for r'. Let's examine: If for a given q, condition holds at both r' and r with r' < r, then the minimal day for residue r' would be at most qD + r', which is less than qD + r. However, our computed q for r' might be less than or equal to q. If at q-1 cycles, condition does not hold for r', but at q it does, then the minimal q for r' is exactly q. So candidate for r' would be qD + r', which is less than q*D + r. So ans would be that. Therefore, we don't need to worry about overestimation for r, because the correct smaller day will be captured by a different residue. So our approach of taking min across residues of the earliest day for each residue yields the correct global earliest day.
But is it possible that the earliest day overall occurs at a residue r, but the condition for that residue at q is satisfied, but for all residues r' < r, the condition is not satisfied at that q (i.e., weight only becomes <= T at step r, not before). Then the earliest day for that residue is indeed q*D + r, and for residues with smaller r, the minimal q may be larger, giving a larger day. So that candidate is the true global minimum. So fine.
Thus algorithm correct.
Now, consider potential integer division edge when A + negW - 1 may overflow if both are near 2^63? But as reasoned, max around 2e10, safe.
Now, implement.
But we can also simplify: For W<0, we could compute for each r: if pref[r] <= T - S then candidate = r else q = ceil((S + pref[r] - T) / (-W)) and cand = q*D + r. Since S,T are up to 1e5, but we have to use long long.
Now, check that we don't need to consider r = D? Not needed.
Now, maybe we can reduce memory by not storing all pref but computing on the fly? But D up to 100k, memory fine.
Now, finalize.
Edge Cases:
-
If W < 0 and for some r, add = pref[r] is such that A <= 0, but r == 0? Not possible as S > T. But if S == T? Problem says S > T, so no.
-
If W < 0 and D = 1, then r=0 only, as we saw.
-
If D is large and answer huge, we should use long long for ans.
Now, one more check: In the W>=0 branch, we used day from 1 to D inclusive. This includes day D. That's correct. But note: For W>=0, day D is the only day where r=0 with q=1 (if D>1). It's included.
Now, confirm with a scenario where W > 0 and answer occurs at day D: Example: S=10, T=9, D=2, w=[-5, 4] => W=-1? Actually -5+4=-1, not >0. Let's construct: w = [-2, -1]? That's negative sum. Need positive sum: w=[-1, 2] => W=1. S=10, T=9: day1:9 <=9, answer 1, not D. To have answer at day D, we need day1> T, day2 <= T. Example: S=10, T=9, w=[2, -3]? W=-1, negative. Hmm, positive sum and day2 <= T but day1 > T: w=[5, -6]? W=-1 again. For positive sum, if day2 <= T, then since W>0, day2 = S+W > S? Actually S+W > S because W>0, so if S > T, S+W > S > T, can't be <= T. So with W>0, S+W > S > T, so day D cannot be <=T. So day D can't be answer if W>0. But for W=0, S+W = S > T, so day D also not answer. So day D is answer only when W<0 or maybe W=0 and S <= T? But S > T. So day D as answer only occurs when W<0. So for W>=0 branch, we don't need to worry about r=0, but our loop day 1..D includes day D anyway.
Thus good.
Now, consider potential pitfall with reading input: The second line has D integers, may be separated by spaces and newline.
Now, produce code.
But before final, let's think about an alternative scenario: What if during the process, weight becomes <= T exactly at day 0? Not possible.
Now, I'll produce a solution in C++.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll S, T;
int D;
cin >> S >> T >> D;
vector w(D);
for (int i=0; i<D; ++i) cin >> w[i];
vector pref(D+1, 0);
for (int i=0; i<D; ++i) pref[i+1] = pref[i] + w[i];
ll W = pref[D];
if (W >= 0) {
for (int day=1; day<=D; ++day) {
if (S + pref[day] <= T) {
cout << day << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} else {
ll negW = -W;
ll ans = LLONG_MAX;
for (int r=0; r<D; ++r) {
ll add = pref[r];
ll A = S + add - T;
ll cand;
if (A <= 0) {
// r must be >0 because S > T implies A>0 when add=0.
if (r == 0) continue; // not possible, but safe.
cand = r;
} else {
ll q = (A + negW - 1) / negW; // ceil
cand = q * D + r;
}
if (cand < ans) ans = cand;
}
cout << ans << endl;
}
return 0;
}
Now, check for r=0 when A <= 0, we skip. That's safe.
But there is a nuance: For r>0 and A <= 0, we set cand = r. However, is it guaranteed that the condition holds at day r? Since A <= 0 means S + pref[r] <= T, so yes weight at day r (which is S+pref[r]) is <= T, so stopping day is r. But could it be that the process would have stopped earlier at a day < r? If yes, then the actual answer would be < r, and we would have that from some other residue with smaller r and A <= 0. So our min will capture that. So it's fine to set cand = r even if there might be earlier stop, because the min across residues will be smaller. For example, if for r=3, A<=0, but for r=2 also A<=0, then cand=2 is less, and will be taken as min. So okay.
Now, we must ensure we handle r from 0 to D-1. But note: For r>0, we consider cand = r when A <= 0, but that cand might be less than some other computed. Good.
Now, consider the case where W<0 and there is a residue r such that A <= 0, but the minimal answer might be for a different residue with a later day? min will pick the smallest.
Now, what about r=0? A always >0, so cand = q*D, which is >= D. So min will be at most the smallest among those, which could be less than D if some r>0 with A <=0 exists. So overall, ans will be the correct minimal day.
Now, we should also check that for r>0, A could be negative but that day r might be 0? No, r>0 => at least 1.
Now, confirm that when W < 0, we might have A <= 0 for some r, and ans will be <= D. That's correct.
Now, what about the scenario where W < 0 but no A <= 0? Then answer > D. That's fine.
Now, test with random small D and brute force to be absolutely sure.
I'll write a small brute in mind for a few random.
Case: S=20, T=10, D=4. Random w: [3, -5, 2, -6]. Compute W = -6. pref: [0,3,-2,0,-6].
Simulate brute:
day0:20
day1:23
day2:18
day3:20
day4:14
day5:17
day6:12
day7:14
day8:8? Let's compute: day4=14, day5=14+3=17, day6=17-5=12, day7=12+2=14, day8=14-6=8 <=10. So answer=8.
Now our algorithm:
W = -6, negW=6.
r=0: add=0, A=20-10=10, q=ceil(10/6)=2, cand=24+0=8.
r=1: add=3, A=20+3-10=13, q=ceil(13/6)=3, cand=34+1=13.
r=2: add=-2, A=20-2-10=8, q=ceil(8/6)=2, cand=24+2=10.
r=3: add=0, A=20+0-10=10, q=ceil(10/6)=2, cand=24+3=11.
ans=8. Good.
Now test case where answer is within first cycle: S=20, T=15, D=4, w=[-10, 5, 3, -2]. W = -4. pref: [0,-10,-5,-2,-4]. r=1: add=-10, A=20-10-15=-5 <=0 => cand=1. So ans=1. Simulate: day1=10 <=15, stop. Good.
Now test case where answer is exactly day D (r=0 with q=1) and no earlier: S=20, T=15, D=3, w=[5, -5, -5] => W=-5. pref: [0,5,0,-5]. r=0: A=5, negW=5, q=ceil(5/5)=1, cand=3. r=1: add=5, A=10, q=ceil(10/5)=2, cand=23+1=7. r=2: add=0, A=5, q=ceil(5/5)=1, cand=13+2=5. ans=3. Simulate: day1:25>15, day2:20>15, day3:15<=15. So answer 3. Good.
Now test case where W<0 and answer at day r where r>0 with q>0: we already did.
Now, consider a case where multiple residues give same q but different r; min picks smallest r.
Now, test with large numbers? Already did.
Now, potential integer division bug: For A>0, (A + negW - 1) / negW works. Since both positive, ok.
Now, note: If negW is 0? That would mean W=0, but we are in else branch only when W<0, so negW>0.
Now, ensure that the answer may be extremely large but still fit. Use LLONG_MAX as initial ans, but we could also use a large constant like 1e18.
Now, implement.
But one more nuance: In the W>=0 branch, we output -1 if no day <= T. However, is it possible that weight becomes <= T exactly at day 0? No.
Thus final.
Now, double-check the problem constraints: S, T, D up to 100,000, but w_i up to -100,000..100,000. So we used long long for sums.
Now, produce final answer.
But I want to consider if there is any scenario where W > 0 but the weight becomes <= T on a day > D due to the pattern of increments? Let's think carefully: For a given offset r (0<=r<D), the weight on days with that offset is S + pref[r] + q*W. Since W>0, this is increasing in q. The smallest q for offset r is q = 0 for r>0, and q=1 for r=0. So the smallest weight for each offset is as described. So if none of those minimal weights are <= T, then for any larger q, weight will be larger, so never <= T. So indeed no day > D can satisfy if the first D days do not.
But wait: Could it be that for some offset r, the minimal weight is achieved not at the smallest q? Because the weight formula is linear in q with slope W. If W>0, increasing q increases weight, so minimal is at smallest q. If W<0, minimal is at largest q, but we want earliest day. For W>0, we only care about earliest day, which would be at smallest q. So correct.
Thus algorithm stands.
Now, we should also verify that the answer d is the number of days taken, i.e., the day index when weight <= T. In our simulation, we used day numbers starting from 1. That matches.
Now, consider possible off-by-one if we mistakenly treat day index 0. But we have consistent.
Now, finalize code.
But we need to be careful: In the loop for r in 0..D-1 for W<0, we set add = pref[r]. For r=0, add=0. That's fine. But for r=0, the day candidate is q*D. However, note that if q=0 (which doesn't happen), that would be day 0. But we only compute for A>0, so q>=1. So candidate >= D.
Now, for r>0 with A <=0, we set cand = r. That is day r. But what about the scenario where r=0 and A <=0? That would be day 0, but not allowed, so we skip.
Thus fine.
Now, implement and test with given examples.
Now, I'll produce final answer.
However, before final, I want to test a scenario with W<0 where multiple residues produce candidate and the minimal might be for r=0 with q=1 but there is also r=some with q=0 but A>0? That can't happen because if A>0 then q>0 for that r? Actually if A>0, q>0, so candidate > r. But if there is some r with A <=0, then candidate = r, which is <= D-1, so that would be smaller than any candidate with q>=1. So min would be that r. So fine.
Now, I'll produce final solution.
But wait: There's a nuance about the definition of r in the representation k = q*D + r. Usually remainder r is in 0..D-1. But when we compute pref[r] for r>0, that's sum of first r w's. That matches weight after r steps from the start of a cycle. However, is it correct that after q full cycles, we are at the start of a new cycle, and then we take r steps? Yes.
But what about the day numbering? Suppose k = qD + r with r > 0. Then the steps taken: first q full cycles (each D steps), then r steps of the next cycle. So total steps = qD + r. The weight after these steps is S + qW + sum_{i=0}^{r-1} w_i = S + qW + pref[r]. So correct.
For r = 0, it's exactly after q full cycles, weight = S + q*W. That's consistent.
Thus correct.
Now, final code.
But we might be able to compute ans without storing all pref by iterating and maintaining running prefix sum. However, storing is fine.
Now, I will write the solution in C++.
Potential improvements: Use fast I/O.
Now, produce final answer.
Before final, we should test with a custom case where answer is -1: S=5, T=4, D=2, w=[1,2]. W=3>=0. Check days: 5+1=6>4, 5+1+2=8>4. So -1. Our algorithm outputs -1.
Now, test with S=5, T=4, D=2, w=[-1, 2] W=1>=0? -1+2=1>0. Day1:4<=4 -> answer 1. Good.
Now, test with W=0, answer -1: S=5,T=4,D=2,w=[1,-1]? sum=0. Day1:6>4, day2:5>4, so -1. Good.
Now, test with W=0, answer 1: S=5,T=4,D=2,w=[-1,1] sum=0. Day1:4<=4 -> answer 1.
Now, test with W negative and answer > D. Already done.
Now, confirm that for W negative, we always get a finite answer. Yes.
Now, think about the possibility that answer may be larger than 2^63-1? As argued, max around 1e15, safe.
Now, all good.
Now, I'll produce final answer.
But wait: There's a nuance: The input size D up to 100k, but the w_i can be as high as 100k in absolute value, and we compute prefix sums that might exceed 2^31, so we need 64-bit. Already considered.
Now, also note that we used vector for w, but we could store as long long. Not needed.
Now, code.
Potential micro-optimizations: Use arrays instead of vector? Not needed.
Now, I'll write final solution.
But before concluding, I want to double-check with an example that could be tricky: Suppose W < 0, and for some r, pref[r] is extremely large positive, making A huge. However, maybe for a different r with smaller pref, q is smaller, but the product q*D + r might be larger or smaller. Our min will capture.
Now, consider S=100, T=0, D=2, w=[100000, -100001] but w_i range? -100001 not allowed. So limit.
Let's test within range: S=100, T=50, D=3, w=[100000, 100000, -200000]? -200000 not allowed. So can't exceed.
Anyway.
Now, one more subtlety: The condition for stop is "If his weight successfully gets less than or equal to T, he will stop slimming immediately." That means that after computing the weight for the next day, if it is <= T, we stop before proceeding further. Our simulation of day d corresponds to after d steps. So correct.
Now, finalize.
But I recall a potential trap: The weight change is applied as x + w_{i%D} where i is the current day index. That is, on day i (starting from 0), we compute next day's weight. So the sequence of weights: day0 = S, day1 = S + w0, day2 = day1 + w1, etc. That's what we used.
Thus final.
Now, before writing final answer, I'll test with some random brute force for small D by mental enumeration? Already did some.
To be thorough, maybe we should consider scenario where the answer is extremely large and we need to ensure that our algorithm doesn't overflow when computing q*D. As argued, it's safe.
Now, I'll provide the solution.
But the problem might have additional constraints: S, T up to 100,000, D up to 100,000, w_i between -100,000 and 100,000. So our approach is O(D) time and O(D) memory, which is fine.
Now, let's produce final answer.
I will write in C++.
Potential minor optimization: In W<0 branch, we could compute ans = min over r of (ceil((S+pref[r]-T)/(-W)) * D + r). That's exactly what we have.
Now, final code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll S, T;
int D;
cin >> S >> T >> D;
vector<int> w(D);
for (int i=0; i<D; ++i) cin >> w[i];
vector<ll> pref(D+1, 0);
for (int i=0; i<D; ++i) pref[i+1] = pref[i] + w[i];
ll W = pref[D];
if (W >= 0) {
for (int day=1; day<=D; ++day) {
if (S + pref[day] <= T) {
cout << day << '\n';
return 0;
}
}
cout << -1 << '\n';
} else {
ll negW = -W;
ll ans = LLONG_MAX;
for (int r=0; r<D; ++r) {
ll add = pref[r];
ll A = S + add - T;
if (A <= 0) {
if (r == 0) continue; // impossible but safe
ans = min(ans, (ll)r);
} else {
ll q = (A + negW - 1) / negW;
ll cand = q * D + r;
if (cand < ans) ans = cand;
}
}
cout << ans << '\n';
}
return 0;
}
But wait: In the A<=0 branch, we used ans = min(ans, (ll)r). But what if r=0? We skip. For r>0, that's fine. However, note that if A <=0 for multiple r, the smallest r will be the answer (since days are increasing). So we can just set ans = min(ans, r). But also we might have already some candidate from other r that is smaller. So min works.
But careful: Could ans be updated later with a candidate that is smaller than some already found? Yes, min handles.
Now, we initialized ans = LLONG_MAX. That's fine.
But note: For r>0 and A <=0, cand = r. However, what if the answer is 0? Not possible. So fine.
Now, I'll test with an example where multiple A<=0: S=100, T=90, D=4, w=[-5, -5, -5, -5]. pref: [0,-5,-10,-15,-20]. A for r=1: 100-5-90=5>0? Actually 100-5-90=5>0, so not <=0. r=2: 100-10-90=0 <=0, cand=2. r=3: 100-15-90=-5 <=0, cand=3. r=4? Actually r up to 3. So min ans = 2. Simulate: day1:95>90, day2:90<=90 -> stop at day2. So ans=2 correct.
Now, all good.
Now, check the loop for r from 0 to D-1 includes r=0 but A>0, so compute q. That candidate may be large, but min will pick smaller if any.
Now, potential improvement: If we find a candidate with r=1 (day1) we could break early? But we need to compute min across all residues, but we can't guarantee that a smaller day exists beyond r=1 because days increase with r? Actually days for q=0 are r (r from 1 to D-1) and for r=0 it's at least D. So the smallest possible day overall is 1. So if we ever find that A <= 0 for r=1, answer is 1, which is the global minimum possible. So we could break early if we find r such that A <= 0 and r=1. But not necessary.
But we can't break after r=1 if A>0, because maybe r=2 with A <=0 gives day2 which is >1 but still could be smaller than other candidates? But we need the minimal; if r=1 not <= T, then the answer is at least 2. So we could keep checking.
But not needed.
Now, edge case: D might be 0? No.
Now, we should also ensure that we treat all integers as long long where necessary.
Now, final code ready.
But I'll test the code with example input.
Example1:
Input:
65 60 3
-2 3 -4
pref: [0,-2,1,-3]; W=-3; negW=3; loop:
r=0: add=0, A=5>0 -> q=(5+3-1)/3= (7)/3=2, cand=23+0=6
r=1: add=-2, A=65-2-60=3>0 -> q=(3+3-1)/3= (5)/3=1, cand=13+1=4
r=2: add=1, A=65+1-60=6>0 -> q=(6+3-1)/3=8/3=2, cand=2*3+2=8
ans=4 => output 4.
Example2:
65 60 3
-2 10 -3
pref: [0,-2,8,5]; W=5>=0; check day1:63>60, day2:73>60, day3:70>60 => none => -1.
Example3:
100000 1 1
-1
pref: [0,-1]; W=-1<0; negW=1; r=0: add=0, A=100000-1=99999>0 -> q=(99999+1-1)/1=99999, cand=99999*1+0=99999; ans=99999.
Example4:
60 59 1
-123
pref: [0,-123]; W=-123<0; negW=123; r=0: A=60-59=1>0 -> q=(1+123-1)/123=123/123=1, cand=1*1+0=1; ans=1.
Works.
Now, finalize.
One more check: Could there be scenario where ans remains LLONG_MAX? That would happen if we never compute any candidate, which would be if for all r, A>0 and we computed candidates, but we always compute candidates for r=0..D-1. So ans will be set. Unless D=0? Not possible. So ans will be set.
Now, everything fine.
Thus solution.
Now, I'll produce final answer with explanation.
But the problem might be from some contest; they expect output as integer, possibly large. So we output as long long.
Now, final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll S, T;
int D;
cin >> S >> T >> D;
vector<int> w(D);
for (int i = 0; i < D; ++i) cin >> w[i];
vector<ll> pref(D + 1, 0);
for (int i = 0; i < D; ++i)
pref[i + 1] = pref[i] + w[i];
ll W = pref[D];
// case 1: net change per cycle is non‑negative
if (W >= 0) {
for (int day = 1; day <= D; ++day) {
if (S + pref[day] <= T) {
cout << day << '\n';
return 0;
}
}
cout << -1 << '\n';
return 0;
}
// case 2: net change per cycle is negative
ll negW = -W; // positive
ll ans = LLONG_MAX;
for (int r = 0; r < D; ++r) {
ll add = pref[r]; // sum of first r w_i
ll A = S + add - T; // how far above T we are at this offset
if (A <= 0) {
// already ≤ T during the first occurrence of this offset
if (r == 0) continue; // S > T makes this impossible, but safe
ans = min(ans, (ll)r); // day = r
} else {
// smallest q such that q * (-W) >= A
ll q = (A + negW - 1) / negW; // ceil division
ll cand = q * D + r;
if (cand < ans) ans = cand;
}
}
cout << ans << '\n';
return 0;
}