We need to solve: given n and c, and arrays a[1..n-1] and b[1..n-1]. Starting from floor 1. We can move using stairs or elevator; each move is from floor x to y with cost: stairs: sum of a_i between min(x,y) and max(x,y)-1; elevator: c + sum of b_i similarly.
We can do any number of moves, and we need min total time to reach each floor i (including floor 1, t1=0). This looks like a shortest path problem on a line graph with two types of edges: between i and i+1, we have two possible travel methods? Actually, we can move directly from any floor to any other floor, but the cost is sum of per-segment costs. However, because costs are additive along segments, the shortest path will be equivalent to some dynamic programming: we either climb floor by floor or take some combination. Since we can move directly, the cost from i to j is either stairs_cost(i,j) = sum_{k=min(i,j)}^{max(i,j)-1} a_k, or elevator_cost(i,j) = c + sum_{k=min(i,j)}^{max(i,j)-1} b_k. So the graph is complete with two types of direct edges: for any unordered pair (i,j), there is an edge with stairs cost, and another with elevator cost. But we don't need to consider all; because these costs are additive (sum over segments), it's essentially like we have two modes of travel: stairs mode and elevator mode. However, the elevator has a fixed overhead c per ride? Actually description: "If you are using the elevator, just sum up c and the corresponding values of b_i." So each elevator ride includes one c, regardless of distance? Yes: "c + sum_{i} b_i". So if you take elevator from floor x to y, you pay c + sum of b on the path. You can also mix modes: e.g., stairs to some floor, then elevator to another, etc. The overhead c is incurred each time you start an elevator ride (like waiting for it). The stairs have no overhead.
So the problem is to compute the shortest time from floor 1 to each floor, with these possible moves. This is reminiscent of Codeforces problem "Elevator or Stairs?" (CF Round #...). Indeed it's similar to CF problem 1249E? Actually there's a problem: "Elevator or Stairs?" where you need to go floor by floor, but you have a state (whether you are using stairs or elevator) and you can switch. But that problem: you are at floor 1, you can either take stairs to next floor, or elevator to next floor (with cost c+b_i) but if you continue using elevator from previous floor, you don't pay c again? Wait, typical problem: "Elevator or Stairs?" (CF 1249E) description: There is an elevator that you can take, but if you take it consecutively, you don't pay the waiting time again. Actually, recall problem: you are on floor 1, want to reach floor i. There are stairs: cost a_i to go from i to i+1. Elevator: cost b_i to go from i to i+1, plus if you used stairs before and now take elevator you have to pay an extra c (waiting time). In that problem, you can only move one floor at a time (you can't skip floors). But here the problem says "In one move, you can go from the floor you are staying at x to any floor y (x ≠ y) in two different ways". That suggests you can skip floors. However, the typical solution to this Codeforces problem (perhaps it's from Round #... ) uses DP with two states: min time to reach floor i using stairs (i.e., last move used stairs) and min time to reach floor i using elevator (i.e., last move used elevator). Because you can only move one floor at a time? Actually in that problem, you could go floor by floor; but here you can go directly from any floor to any other. That would change the DP.
Let's read the problem statement carefully: "In one move, you can go from the floor you are staying at x to any floor y (x ≠ y) in two different ways: If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take ∑{i=min(x, y)}^{max(x, y) - 1} a_i time units. If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + ∑{i=min(x, y)}^{max(x, y) - 1} b_i time units."
Thus, you can teleport? Actually you can go directly to any floor in one move, paying either stairs sum or elevator sum plus c. Since stairs sum is additive over segments, it's equivalent to sum of a_i between floors. So if you want to go from 1 to i, using stairs directly costs S_a[1,i] = sum_{k=1}^{i-1} a_k. Using elevator directly costs c + S_b[1,i] = c + sum_{k=1}^{i-1} b_k. But you can also make intermediate stops, maybe to combine modes and reduce cost? For example, you might go from 1 to j using stairs (cost S_a[1,j]), then from j to i using elevator (cost c + S_b[j,i] if j < i? But wait: if j>i, you could go backwards, but floors are ordered; we need to go from floor 1 to floor i, which is higher (since floor numbers increase from bottom to top; we start at bottom floor 1; other floors are above; we probably only need to go up? The problem says "reach it from the first (the bottom) floor." Usually you only go upwards. But the moves allow any x to y, could be downward as well. However, to minimize, we likely only move upward because moving down and then up would only add cost (since all a_i,b_i >0). So we can assume monotonic increasing moves (non-decreasing floor numbers). So we consider only moving upward (x<y). Then the direct costs are as above.
But is there any benefit to mixing? For stairs: The cost from 1 to i via stairs is simply sum a over the path, which is additive: any path that goes through intermediate floors using stairs (i.e., multiple stairs moves) will have total cost equal to sum of a over the total covered segments; if you break it into multiple legs, total is still sum of a over the union of intervals, which is exactly the sum over [1,i-1] if you never go back. So the minimal stairs-only cost is just direct stairs. However, you could mix stairs and elevator: For example, go from 1 to k using stairs (cost sum a from 1 to k-1), then take elevator from k to i (cost c + sum b from k to i-1). Or you could start with elevator and then stairs? That would be elevator from 1 to k (cost c + sum b from 1 to k-1), then stairs from k to i (cost sum a from k to i-1). Or even multiple switches. Since all costs are additive and positive, is it ever optimal to switch more than once? Possibly, because the elevator has a fixed overhead c per ride. So if you use elevator multiple times, you pay c each time, which is not efficient. So the optimal path likely uses at most one elevator ride (or zero). However, consider that you might want to use elevator for a segment because b_i may be smaller than a_i, but paying c might be offset only if the segment is long enough. But if you use elevator in two separate rides, you'd pay c twice, which seems worse than combining them into one longer elevator ride (if feasible). But could there be a situation where you use stairs for a small part, then elevator for a later part, and then stairs again? If the elevator segment is cheap but you want to avoid the overhead c for some part? But if you take elevator from k to i, you pay c + sum b over that segment. If instead you took elevator from k to j (with j < i) and then stairs from j to i, you'd pay c + sum b over [k,j-1] + sum a over [j,i-1]. This could be better than one elevator all the way if sum b over [j,i-1] is large relative to sum a? Actually we need to compare: Option A: stairs 1->k, then elevator k->i. Option B: stairs 1->k, elevator k->j, stairs j->i. Cost(B) = sum a[1,k-1] + (c + sum b[k,j-1]) + sum a[j,i-1]. Option A cost = sum a[1,k-1] + c + sum b[k,i-1]. Difference: Cost(B)-Cost(A) = sum a[j,i-1] - sum b[j,i-1] (since sum b[k,i-1] = sum b[k,j-1] + sum b[j,i-1]). So B is better than A if sum a[j,i-1] < sum b[j,i-1]. But note that a_i and b_i are given, and a_i could be less than b_i for some intervals, and greater for others. If for the segment [j,i-1], stairs are cheaper than elevator (ignoring c), then splitting the elevator ride to avoid using elevator on that segment could be beneficial. However, in that case, why not just take stairs for that segment directly? But you already have to pay c for the elevator ride from k to j. If you then switch to stairs, you avoid paying the elevator cost for that later segment. So mixing multiple switches could be beneficial if the elevator segment is cheaper for some parts but not for others. But you could also just take stairs from k all the way? Then cost = sum a[1,k-1] + sum a[k,i-1] = sum a[1,i-1]. That is another option. So the problem reduces to: find min over all sequences of segments, each segment is either stairs or elevator (with overhead c per elevator segment), total cost = sum over stairs segments of sum a over that segment + sum over elevator segments of (c + sum b over that segment). Segments cover from 1 to i (non-overlapping, contiguous, covering the whole path). Because you can only go upward, you can partition [1,i) into intervals, each assigned to stairs or elevator. The total cost is: for stairs intervals, sum of a over the interval; for elevator intervals, c + sum of b over the interval. The question: what is the minimal cost to reach floor i from floor 1? This is like we have two types of edges between any two floors, but with the twist that elevator edges incur a fixed cost c per edge. Since we can take any number of edges, it's a graph problem: vertices are floors 1..n, edges for all pairs (i<j) with two weights: stairs: S_a(i,j) = prefix sum difference A[j-1]-A[i-1] (if we define A[0]=0, A[k]=sum_{t=1}^k a_t), elevator: c + (B[j-1]-B[i-1]), where B similar. This is a directed graph (upwards) with all edges. We want shortest path from 1 to each vertex.
This is a classic problem: shortest paths in a DAG with O(n^2) edges, but we can optimize using convex hull or DP with some structure. Because the edge weights are of the form: stairs: f(j) - f(i) where f is a prefix sum (of a). Similarly elevator: c + (g(j)-g(i)) with g prefix sum of b.
For stairs: cost = A_{j-1} - A_{i-1}. This is essentially a linear function in terms of A. For a path, if we use only stairs edges, it's just A_{i-1} (starting from 1, A_0=0). But mixing with elevator edges: note that elevator edges have a constant overhead c per edge.
One can think of DP: dp[1]=0. For j>1, dp[j] = min( min_{i<j} (dp[i] + A_{j-1} - A_{i-1}), min_{i<j} (dp[i] + c + B_{j-1} - B_{i-1}) ). Simplify: dp[j] = min( A_{j-1} + min_{i<j} (dp[i] - A_{i-1}), c + B_{j-1} + min_{i<j} (dp[i] - B_{i-1}) ). So we can maintain two values: min_stairs = min_{i<j} (dp[i] - A_{i-1}), min_elev = min_{i<j} (dp[i] - B_{i-1}). Then dp[j] = min( A_{j-1} + min_stairs, c + B_{j-1} + min_elev ). But wait: This DP assumes we can always directly jump from any i to j using either stairs or elevator. But is it allowed to have multiple edges? Yes, because we can combine jumps; the DP recurrence for shortest path on a DAG with all edges from lower to higher is valid. Since all edges go forward (i<j), it's a DAG and shortest path can be computed by DP if we consider all i<j. The recurrence dp[j] = min_{i<j} (dp[i] + cost(i,j)) is correct. And cost(i,j) can be either stairs or elevator. So the DP above is correct.
Now the challenge: n up to 2e5, we need O(n) or O(n log n). The DP as written would be O(n^2) if we compute min over all i for each j. But the expression is separable: dp[i] - A_{i-1} is independent of j, so we can keep a running minimum of that quantity as we iterate j from 1 to n. Similarly for dp[i] - B_{i-1}. However, careful: the minimization is over i < j. So as we compute dp[j], we can use the minima over previously computed dp[i]. Then after computing dp[j], we update the minima: min_stairs = min(min_stairs, dp[j] - A_{j-1}), and min_elev = min(min_elev, dp[j] - B_{j-1}). Since we go in increasing order, this works. So we can compute dp in O(n). That's it! But wait, is there any catch? The DP as described assumes that we can directly go from i to j using either stairs or elevator, which is allowed. So solution is straightforward: dp[1]=0. Compute prefix sums A[0]=0, A[k]=sum_{i=1}^k a_i for k=1..n-1. Similarly B for b. For each floor j from 2 to n: dp[j] = min( A_{j-1} + min_{i<j} (dp[i] - A_{i-1}), c + B_{j-1} + min_{i<j} (dp[i] - B_{i-1}) ). Maintain two variables best_stairs = dp[1] - A_0? For i=1: dp[1]=0, A_0=0 => dp[1]-A_0=0. best_stairs = 0 initially. Similarly best_elev = dp[1] - B_0 = 0 - 0 = 0. Then for j=2: dp[2] = min( A1 + best_stairs, c + B1 + best_elev ) = min( A1, c+B1). That makes sense: either stairs directly from 1 to 2: cost a1, or elevator: c+b1. Works. For j=3: dp[3] = min( A2 + min_{i=1,2} (dp[i]-A_{i-1}), c+B2 + min_{i=1,2} (dp[i]-B_{i-1}) ). This allows possibility: from 1 to 3 directly using stairs (cost a1+a2) or elevator (c+b1+b2), or from 1 to 2 then to 3, which might be cheaper if, say, from 2 to 3 using elevator after stairs? But DP considers all i<j; i=2 corresponds to dp[2] + cost(2,3). For i=2, cost(2,3) could be stairs (a2) or elevator (c+b2). But note: dp[2] already includes optimal way to reach floor 2. So dp[2] + a2 corresponds to going optimal to floor 2 then stairs to 3. Similarly dp[2] + c + b2 corresponds to optimal to floor 2 then elevator to 3. But does this DP include both possibilities? In the min over i<j, we only consider two cost options for each i? Actually, the recurrence dp[j] = min_{i<j} min( dp[i] + stairs_cost(i,j), dp[i] + elevator_cost(i,j) ). That is equivalent to taking min over i of two expressions: dp[i] - A_{i-1} + A_{j-1} and dp[i] - B_{i-1} + B_{j-1} + c. So we can compute two minima separately. This is exactly what we have: min over i of (dp[i] - A_{i-1}) and min over i of (dp[i] - B_{i-1}). So the DP covers all possibilities.
Thus the answer is dp[1..n] = 0, dp[2..n] computed as above.
Is there any scenario where it's better to go down then up? Since all costs are positive, going down would only add extra cost and possibly use elevator differently? Might there be a situation where you go up to some floor, then go down to some lower floor to take elevator from there to a higher floor, because the elevator overhead c is per ride and you want to combine? For example, you go from 1 to 5 using stairs (cost A4), then go down to 3 (cost stairs? that would cost A4 - A2? That would be extra cost), then take elevator from 3 to 6 (c + sum b from 3 to 5). This would be more costly than just taking elevator from 5 to 6 (c + b5) after stairs? Not sure. But could going down to avoid paying c? Suppose you are at floor i and want to go to floor j > i. If you take elevator directly from i to j, cost c + sum_{k=i}^{j-1} b_k. Alternatively, you could go down to some k < i, then take elevator from k to j (c + sum_{k}^{j-1} b_k) and then the cost to go down from i to k (stairs or elevator). Since all costs are positive, the added cost of descending would be at least something positive, while the benefit might be that sum_{k}^{j-1} b_k is less than sum_{i}^{j-1} b_k? Actually sum from k to j-1 includes extra segment from k to i-1 (which are more b's), so it's larger, not smaller. So descending wouldn't reduce the b sum; it would increase it. Could descending allow you to avoid paying c again? If you are at floor i, you could consider taking elevator from i to j, paying c. If you instead go down to k (cost stairs) then take elevator from k to j, you pay c (again) plus the descent cost. So it's worse. Could descending allow you to use stairs for a segment and then elevator without paying c? But you still need to pay c for elevator ride; you could use stairs for some part to avoid paying c? Actually the only way to avoid paying c is to not use elevator at all, or to use elevator only once (c is per ride). But if you already used elevator to get to i, you might have already paid c for that ride. However, if you are currently after having taken stairs (no active elevator ride), and you decide to take elevator from i to j, you pay c. If you had earlier taken an elevator to some floor and then continued with stairs, the c was already paid. Could we combine elevator rides to avoid multiple c? That would be using a single elevator ride covering a larger segment. For that, you would want to start the elevator at the lowest floor you intend to use elevator from, and end at the highest floor, without switching. So it's best to have a single contiguous elevator segment if you use elevator at all. So optimal path likely has at most one elevator segment (and maybe some stairs before and after). However, consider possibility of using elevator, then stairs, then elevator again. That would incur c twice, which seems less efficient than a single longer elevator ride covering the union of intervals, unless the middle stairs segment is so cheap compared to elevator that it outweighs the extra c. But note: if you have two elevator rides separated by stairs, you pay c twice. Suppose you have three segments: stairs from 1 to i, elevator i->j, stairs j->k, elevator k->l. The total cost = stairs(1,i) + (c+b(i,j-1)) + stairs(j,k) + (c+b(k,l-1)). Could it be better than a single elevator from i to l? Single elevator: stairs(1,i) + c + b(i,l-1). Compare: double elevator: plus stairs(j,k) + c + b(k,l-1) vs b(i,l-1). Since b(i,l-1) = b(i,j-1)+b(j,k-1)+b(k,l-1). So double elevator cost = stairs(1,i) + c + b(i,j-1) + stairs(j,k) + c + b(k,l-1). Single elevator cost = stairs(1,i) + c + b(i,j-1) + b(j,k-1) + b(k,l-1). So the difference (double - single) = stairs(j,k) + c - b(j,k-1). If stairs(j,k) + c < b(j,k-1), then double could be better. Since stairs(j,k) = sum_{t=j}^{k-1} a_t, b(j,k-1)= sum_{t=j}^{k-1} b_t. So if a segment has sum a + c < sum b, then using stairs for that segment and paying another c for subsequent elevator might be better than using elevator through that segment. But could you also avoid paying the second c by not using elevator after the stairs? That would be stairs only to l, but maybe that's even worse. But the point is that multiple elevator rides might be optimal if there are segments where stairs are much cheaper than elevator and the overhead c is not too big. However, note that the DP we derived allows arbitrary number of switches, because we consider any i < j and both types. The DP does not restrict the number of elevator rides; it simply finds the shortest path using arbitrary edges. So it should correctly handle multiple switches.
But is there any situation where going backwards could be beneficial? Consider you are at floor i, and you want to go to floor j > i. Instead of going directly, you go down to k < i, then take elevator to j, then maybe go up more? That would add extra distance. Since all costs are positive and the elevator cost is additive along the path, descending would only increase total distance; there's no negative costs. Also the overhead c is per elevator ride, but descending and then taking elevator would still incur c, possibly more than one. So it's not beneficial. Similarly, using elevator and then stairs then elevator: but this is captured by DP with intermediate vertices (i, j, k...). No need to go backwards.
Thus, the DP solution with linear scan should be correct. Let's test with examples.
Example 1:
n=10, c=2
a: 7 6 18 6 16 18 1 17 17
b: 6 9 3 10 9 1 10 1 5
Compute prefixes A (sum a up to index i-1? Let's define: for floor f, the stairs cost from 1 to f is sum_{k=1}^{f-1} a_k. So A[0]=0, A[1]=a1=7, A[2]=7+6=13, A[3]=13+18=31, A[4]=31+6=37, A[5]=37+16=53, A[6]=53+18=71, A[7]=71+1=72, A[8]=72+17=89, A[9]=89+17=106? Wait, there are 9 a's, so floors 1..10. A indexes: A[0]=0 (to floor1), A[1]=a1=7 (to floor2), A[2]=7+6=13 (to floor3), A[3]=13+18=31 (to floor4), A[4]=31+6=37 (to floor5), A[5]=37+16=53 (to floor6), A[6]=53+18=71 (to floor7), A[7]=71+1=72 (to floor8), A[8]=72+17=89 (to floor9), A[9]=89+17=106 (to floor10). But let's compute properly: a1..a9 correspond to floors 1->2 through 9->10. So sum up to floor i: from 1 to i: sum_{k=1}^{i-1} a_k. So for i=2, sum a1=7; i=3 sum a1+a2=13; i=4 sum a1+a2+a3=31; i=5 sum to a4=31+6=37; i=6 sum to a5=37+16=53; i=7 sum to a6=53+18=71; i=8 sum to a7=71+1=72; i=9 sum to a8=72+17=89; i=10 sum to a9=89+17=106.
Similarly for b: b1=6, b2=9, b3=3, b4=10, b5=9, b6=1, b7=10, b8=1, b9=5. So prefix B: B[0]=0; B[1]=6; B[2]=15; B[3]=18; B[4]=28; B[5]=37; B[6]=38; B[7]=48; B[8]=49; B[9]=54.
Now DP:
dp[1]=0.
Initialize min_stairs = dp[1] - A[0] = 0 - 0 = 0.
min_elev = dp[1] - B[0] = 0.
i=2:
dp[2] = min( A[1] + min_stairs =7+0=7, c+B[1]+min_elev=2+6+0=8) => 7. So dp[2]=7.
Update min_stairs = min(0, dp[2]-A[1]) = min(0, 7-7=0) => 0.
min_elev = min(0, dp[2]-B[1]) = min(0, 7-6=1) => 0? Actually min(0,1)=0.
i=3:
dp[3] = min( A[2] + min_stairs =13+0=13, c+B[2]+min_elev=2+15+0=17) =>13.
Update: min_stairs = min(0, dp[3]-A[2]=13-13=0) =>0.
min_elev = min(0, dp[3]-B[2]=13-15=-2) => -2.
i=4:
dp[4] = min( A[3]=31, c+B[3]=2+18=20) => min(31,20)=20? But expected output from example: t4=18? Let's check example output: "0 7 13 18 24 35 36 37 40 45". So expected dp[4]=18. Our computed gives 20. So something is wrong. Let's recalc A[3]= sum a1+a2+a3 =7+6+18=31. B[3]= sum b1+b2+b3 =6+9+3=18. So direct stairs:31, direct elevator: c+18=20, but output says 18. So there is a better way that gives 18. How can we get 18? Possibly from 1->2 by stairs (7), then 2->4 by elevator? Let's compute: 1->2 stairs =7, then 2->4 elevator: c + sum b2+b3? Actually elevator cost from 2 to 4 is c + (b2+b3) = 2+ (9+3)=14, total =21, not 18. 1->3 stairs=13, then 3->4 elevator: c + b3=2+3=5 total 18. Yes! That yields 18. So dp[4] = min( dp[3] + elevator(3,4) =13 + (c+b3)=13+2+3=18). That's indeed cheaper. But in our DP we only considered edges from any i<4 directly. i=3 is included in min_{i<4} of dp[i] + elevator(i,4) = dp[3] + c + (B[3-1]? Wait B[?]) We need to be careful: In our DP formula, we used A_{j-1} and B_{j-1} and dp[i] - A_{i-1} etc. That should capture i=3. Let's recalc min_elev: For i=3, dp[3]=13, B[2]=15? Actually B index: B[0]=0 (floor1), B[1]=b1 (floor2), B[2]=b1+b2 (floor3), B[3]=b1+b2+b3 (floor4). So for j=4, we need min over i<4 of dp[i] - B_{i-1}? Let's derive properly.
We have dp[j] = min( min_{i<j} (dp[i] + A_{j-1} - A_{i-1}), min_{i<j} (dp[i] + c + B_{j-1} - B_{i-1}) ).
Where A_{k} = sum_{t=1}^{k} a_t, for k from 0..n-1, with A_0=0, A_1 = a1, etc. Similarly B.
Thus for j=4, i=3: dp[3]=13, A_{3-1}=A_2=13? Actually A_{i-1}=A_2=13, B_{i-1}=B_2=15? Wait i=3, i-1=2. So dp[i] - A_{i-1}=13 - A_2 =13-13=0. Then dp[3] + A_{j-1} - A_{i-1} = 0 + A_3 =31. That's stairs. For elevator: dp[i] - B_{i-1}=13 - B_2 =13-15=-2. Then dp[3] + c + B_{j-1} - B_{i-1} = (dp[i] - B_{i-1}) + c + B_{j-1} = -2 + 2 + B_3? B_{j-1}=B_3? j=4 => j-1=3, B_3=18. So -2+2+18=18. That matches. So in our earlier calculation, min_elev was -2 after processing i=3. For j=4, we have min_elev = min over i<4 of (dp[i] - B_{i-1}). After processing i=3, min_elev = min(0, dp[2]-B_1=7-6=1, dp[3]-B_2=13-15=-2) = min(0,1,-2) = -2. So indeed min_elev = -2. Then dp[4] = min( A_3 + min_stairs =31+0=31, c + B_3 + min_elev =2+18+(-2)=18 ). So dp[4]=18. But earlier we computed incorrectly as 20 because we used min_elev = 0. Why did we get min_elev=0 earlier? Because we updated min_elev after i=3 incorrectly? Let's re-evaluate step-by-step after i=3.
We had:
i=2: min_stairs=0, min_elev=0.
After i=2, we set min_stairs = min(prev min_stairs, dp2 - A1) = min(0,7-7=0)=0; min_elev = min(0, dp2 - B1 =7-6=1) = min(0,1)=0.
i=3: before computing dp3, min_stairs=0, min_elev=0. Then dp3 = min( A2 + min_stairs =13+0=13, c+B2+min_elev=2+15+0=17) =>13. After dp3, we update: min_stairs = min(0, dp3 - A2 =13-13=0) =>0. min_elev = min(0, dp3 - B2 =13-15=-2) => -2.
Thus after i=3, min_elev = -2. Then i=4 uses that. So my earlier miscalculation for dp4 used min_elev=0, but I must have forgotten to include the update. So correct dp4 = 18. Good.
Continue:
i=5: A4=37? Wait A index: A0=0, A1=7, A2=13, A3=31, A4=37, A5=53, A6=71, A7=72, A8=89, A9=106. B0=0,B1=6,B2=15,B3=18,B4=28,B5=37,B6=38,B7=48,B8=49,B9=54.
Now min_stairs currently =0 (after i=4? we need update after i=4). After i=4: dp4=18. Update min_stairs = min(0, dp4 - A3=18-31=-13) => -13. min_elev = min(-2, dp4 - B3=18-18=0) => min(-2,0) = -2.
So min_stairs = -13, min_elev = -2.
i=5: dp5 = min( A4 + min_stairs =37 + (-13)=24, c+B4 + min_elev =2+28+(-2)=28 ) => min(24,28)=24. matches expected output 24.
Update after i=5: min_stairs = min(-13, dp5 - A4 =24-37=-13) => -13. min_elev = min(-2, dp5 - B4 =24-28=-4) => -4.
i=6: A5=53, B5=37. dp6 = min(53 + (-13)=40, 2+37+(-4)=35) => 35. expected 35.
Update: min_stairs = min(-13, dp6 - A5 =35-53=-18) => -18. min_elev = min(-4, dp6 - B5 =35-37=-2) => -4? Actually min(-4, -2) = -4, because -4 < -2, so min_elev stays -4.
i=7: A6=71, B6=38. dp7 = min(71 + (-18)=53, 2+38+(-4)=36) => 36. expected 36.
Update: min_stairs = min(-18, dp7 - A6 =36-71=-35) => -35. min_elev = min(-4, dp7 - B6 =36-38=-2) => -4? Actually -4 < -2, so stays -4.
i=8: A7=72, B7=48. dp8 = min(72+(-35)=37, 2+48+(-4)=46) => 37. expected 37.
Update: min_stairs = min(-35, dp8 - A7 =37-72=-35) => -35. min_elev = min(-4, dp8 - B7 =37-48=-11) => -11.
i=9: A8=89, B8=49. dp9 = min(89+(-35)=54, 2+49+(-11)=40) => 40. expected 40.
Update: min_stairs = min(-35, dp9 - A8=40-89=-49) => -49. min_elev = min(-11, dp9 - B8=40-49=-9) => -11? Actually -11 < -9, so -11 remains.
i=10: A9=106, B9=54. dp10 = min(106+(-49)=57, 2+54+(-11)=45) => 45. expected 45.
Matches example.
Thus DP works.
Second example: we can test quickly.
n=10, c=1
a: 3 2 3 1 3 3 1 4 1
b: 1 2 3 4 4 1 2 1 3
Compute prefixes:
A: a1=3 -> A1=3; A2=5; A3=8; A4=9; A5=12; A6=15; A7=16; A8=20; A9=21? Wait there are 9 a's, so A9 = sum a1..a9 = let's compute: 3+2=5, +3=8, +1=9, +3=12, +3=15, +1=16, +4=20, +1=21. So A0=0, A1=3, A2=5, A3=8, A4=9, A5=12, A6=15, A7=16, A8=20, A9=21.
B: b1=1 -> B1=1; B2=3; B3=6; B4=10; B5=14; B6=15; B7=17; B8=18; B9=21? 1+2=3, +3=6, +4=10, +4=14, +1=15, +2=17, +1=18, +3=21. So B0=0, B1=1, B2=3, B3=6, B4=10, B5=14, B6=15, B7=17, B8=18, B9=21.
Now DP:
dp1=0, min_stairs=0, min_elev=0.
i=2: A1=3, B1=1 -> dp2 = min(3+0=3, 1+1+0=2) =>2. expected 2. update: min_stairs = min(0,2-3=-1)=-1. min_elev = min(0,2-1=1)=0.
i=3: A2=5, B2=3 -> dp3 = min(5+(-1)=4, 1+3+0=4) =>4. expected 4. update: min_stairs = min(-1,4-5=-1) stays -1. min_elev = min(0,4-3=1) stays 0? Actually min(0,1)=0.
i=4: A3=8, B3=6 -> dp4 = min(8+(-1)=7, 1+6+0=7) =>7. expected 7? Output says 7? Actually output: "0 2 4 7 8 11 13 14 16 17". Yes dp4=7. update: min_stairs = min(-1,7-8=-1) = -1. min_elev = min(0,7-6=1) = 0.
i=5: A4=9, B4=10 -> dp5 = min(9+(-1)=8, 1+10+0=11) =>8. expected 8. update: min_stairs = min(-1,8-9=-1) = -1. min_elev = min(0,8-10=-2) = -2? Actually min(0, -2) = -2.
i=6: A5=12, B5=14 -> dp6 = min(12+(-1)=11, 1+14+(-2)=13) =>11. expected 11. update: min_stairs = min(-1,11-12=-1) = -1. min_elev = min(-2,11-14=-3) = -3.
i=7: A6=15, B6=15 -> dp7 = min(15+(-1)=14, 1+15+(-3)=13) =>13. expected 13. update: min_stairs = min(-1,13-15=-2) = -2. min_elev = min(-3,13-15=-2) = -3? Actually -3 < -2, so stays -3.
i=8: A7=16, B7=17 -> dp8 = min(16+(-2)=14, 1+17+(-3)=15) =>14. expected 14. update: min_stairs = min(-2,14-16=-2) = -2. min_elev = min(-3,14-17=-3) = -3.
i=9: A8=20, B8=18 -> dp9 = min(20+(-2)=18, 1+18+(-3)=16) =>16. expected 16. update: min_stairs = min(-2,16-20=-4) = -4. min_elev = min(-3,16-18=-2) = -3? Actually -3 < -2 so stays -3.
i=10: A9=21, B9=21 -> dp10 = min(21+(-4)=17, 1+21+(-3)=19) =>17. expected 17. matches.
So DP works for both examples.
Thus solution is simple O(n) with prefix sums and two running minima.
But wait: there is nuance: The problem states you can move from any floor to any other floor, not necessarily upward. But we assumed upward moves only. Could moving down ever help? Consider if you go down and then take elevator from lower floor to a higher floor. Suppose you start at floor 1, you could go up to floor i via stairs, then go down to floor k (k < i) via stairs (which costs something), then take elevator from k to j (j > i). This seems wasteful because you could have taken elevator from i to j directly. The elevator cost from i to j is c + sum_{t=i}^{j-1} b_t, while from k to j is c + sum_{t=k}^{j-1} b_t. The difference is extra sum_{t=k}^{i-1} b_t, which is positive. Meanwhile the extra stairs cost to go down from i to k would be sum_{t=k}^{i-1} a_t (if using stairs) or c+sum b (if using elevator). So total extra positive costs. So not beneficial.
Could moving down to avoid paying c for an elevator ride? For instance, if you are currently at floor i and you have never used elevator, you want to go to floor j > i. The direct elevator ride costs c+sum b. Alternatively, you could go down to floor 1 (cost stairs? sum a from 1 to i? Actually you'd have to go down, which costs something) then take elevator from 1 to j (c+sum b). That would be extra cost for the descent. If you had previously used elevator, you might have already paid c, but that's per ride; you cannot avoid paying c for a new ride by going back. So no.
Thus only upward moves matter.
Thus solution: compute dp[1]=0. Initialize min_stairs = dp[1] - A[0] = 0, min_elev = dp[1] - B[0] = 0. For i from 2 to n:
dp[i] = min( A[i-1] + min_stairs, c + B[i-1] + min_elev )
Then update:
min_stairs = min(min_stairs, dp[i] - A[i-1])
min_elev = min(min_elev, dp[i] - B[i-1])
But careful with indices: we need prefix sums for a and b. Let's define arrays of length n (1-indexed floors). Let pref_a[0]=0; for i=1..n-1, pref_a[i] = pref_a[i-1] + a[i]; similarly pref_b. Then for floor j (2-indexed), stairs cost from 1 to j is pref_a[j-1]. For a transition from i to j (i<j), stairs cost = pref_a[j-1] - pref_a[i-1]. So dp[j] = min_i ( dp[i] + pref_a[j-1] - pref_a[i-1] ) and similarly with elevator. That's what we have.
Implementation: Use long long because sums up to (n-1)1000 = 2e51000 = 2e8, but dp values can be slightly larger due to negative minima? But they are within 64-bit.
Edge Cases: n=2 works.
Now check constraints: n up to 2e5, a_i,b_i,c up to 1000, so sums up to 2e8, within int but safe to use long long.
Thus answer is simple.
But wait: is there any nuance about the elevator overhead being paid per ride? In DP we treat each elevator edge as having cost c + sum b. That's correct. However, note that if you take multiple elevator edges, you pay c each time. The DP allows that because each elevator edge includes c separately. That's fine.
But is there any scenario where using an elevator ride starting from a floor where you arrived via elevator and you haven't left the elevator? In the problem description, the elevator ride cost is c + sum b. It doesn't matter if you were already using the elevator; each ride incurs c. So multiple rides mean multiple c's. So DP is accurate.
Thus solution is O(n) time, O(n) memory (or O(1) if we compute prefix sums on the fly). But we need to output t_i = dp[i] (minimum total time). dp[1]=0.
Now we need to confirm with example outputs: They have a trailing space after each? But that's fine.
Now consider if there is any possibility that the optimal path uses a move that goes down and then up, but maybe because of the overhead c, you might want to start an elevator ride from a lower floor to combine multiple segments while avoiding extra c? However, you could start from that lower floor directly without going down first; you can start from floor 1 and go directly to higher floors. Since you start at floor 1, you can always start elevator from 1 if beneficial. So no need to go down.
But could there be a case where you want to go down to avoid an elevator ride? Suppose you are at floor i and you want to go to floor j, and you might consider using stairs from i down to k (k<i) to then take elevator from k to j, because the elevator cost from k to j might be cheaper due to some b values being small? But as argued, the sum of b from k to j includes additional b's from k to i-1, which are positive, so it's not cheaper than from i to j. And you also pay for stairs down (positive). So total more.
What about the possibility of using a combination where you go up then down then up to avoid an extra c? Example: Suppose you want to go from floor 1 to floor n. You might go from 1 to i by stairs, then from i to j by elevator (pay c), then from j to k by stairs, then from k to n by elevator (pay c again). Could this be better than using a single elevator from i to n? That would be dp[i] + c + sum b(i,j-1) + sum a(j,k-1) + c + sum b(k,n-1). Single elevator: dp[i] + c + sum b(i,n-1). The difference is sum a(j,k-1) + c - sum b(j,k-1). So if sum a(j,k-1) + c < sum b(j,k-1), then it's beneficial to interrupt the elevator ride with stairs. That is plausible if a segment is much cheaper on stairs than elevator, and the extra c is small. So multiple switches can be beneficial. Our DP captures that because it considers all possible i<j with both types. For instance, to achieve the path: 1 -> i stairs, i -> j elevator, j -> k stairs, k -> n elevator, the DP would compute dp[n] as min over i' < n of dp[i'] + cost(i',n). For i' = k, dp[k] already accounts for the optimal way to reach k, which may involve an elevator and stairs etc. So the DP with only direct edges from any i to n may not capture a path with multiple switches if the switches are not represented by a single direct edge? But the DP does because dp[k] will be computed as minimal time to reach k, which could include an elevator segment and stairs segment. Then from k to n we take elevator again. So the DP allows any sequence of segments as long as we consider transitions from any previous floor. Since the graph is complete with edges from any lower floor to any higher floor, any path that goes upward can be represented as a sequence of such edges; but it's not necessary that the DP considers intermediate floors because we are allowed direct jumps. In fact, any path that goes through intermediate floors can be reduced to a single edge from start to end, because cost(i,j) is exactly the sum of costs of moving from i to j directly (either stairs or elevator). However, that direct edge might not be the same as taking multiple moves with different modes? Actually, if you take a path that changes mode within the segment i->j, the cost may differ from any single-mode direct edge. For example, from i to j you could take stairs for part and elevator for part. That path's cost is not equal to either direct stairs cost (which would be sum a) nor direct elevator cost (c+ sum b). So such a path could be cheaper than both direct options. In our DP, we are only considering direct edges that are either pure stairs or pure elevator. But we also allow multiple edges: we could have i -> k (stairs) and then k -> j (elevator). That is two edges, not a single direct edge. However, our DP recurrence dp[j] = min_{i<j} (dp[i] + cost(i,j)) only considers a single edge from i to j. If the optimal path to j involves an intermediate stop k, then dp[j] would be computed from dp[k] + cost(k,j). But dp[k] would have been computed from earlier floors. So overall, the DP with all direct edges (where each edge is either pure stairs or pure elevator) captures all possible sequences because you can break the path at the points where the mode changes. For example, the path 1 -> i (stairs), i -> j (elevator), j -> n (stairs) can be represented as: dp[i] computed (maybe via stairs from 1), then from i to j elevator edge, then from j to n stairs edge. But in the DP recurrence dp[n] = min_{k<n} (dp[k] + cost(k,n)). To capture the intermediate j, we would need to have dp[j] computed (which includes the path up to j via i with elevator) and then cost(j,n) as stairs. So if we allow only direct edges (stairs or elevator) between any pair, we can represent the path as a series of edges, and the DP will consider it because we will compute dp[j] from dp[i] and then dp[n] from dp[j]. So indeed the DP with all i<j edges but only two types per edge is sufficient to capture any combination of modes, because any change in mode corresponds to a vertex where we start a new edge. So the DP works.
Thus the solution is as above.
But wait: The DP as we wrote using min_stairs and min_elev essentially considers transitions from any previous floor using stairs or elevator. That seems to capture all possibilities. However, let's double-check: Suppose we have a path: stairs from 1 to i, elevator from i to j, stairs from j to k, elevator from k to l. This corresponds to dp[i] (stairs only), then dp[j] = dp[i] + elevator(i,j), then dp[k] = dp[j] + stairs(j,k), then dp[l] = dp[k] + elevator(k,l). In the DP, when computing dp[l], we consider min_{i<l} of dp[i] + cost(i,l) with either stairs or elevator. Could it be that dp[l] from dp[k] + elevator(k,l) is considered? Yes, because k < l, and we consider elevator edge from k to l. So that's covered.
Now, is there any possibility of using an elevator from i to j but with a different cost structure not captured? No.
Therefore the DP is correct.
But we must be careful: The DP formulation with min_stairs = min_{i<j} (dp[i] - A_{i-1}) and min_elev = min_{i<j} (dp[i] - B_{i-1}) yields dp[j] = min( A_{j-1} + min_stairs, c + B_{j-1} + min_elev ). This works because we are essentially combining the direct edge from some i to j. However, is it possible that the optimal path to j uses multiple edges, but the best direct edge from i to j is not necessarily covering the whole path from i to j? Actually, dp[i] already is the optimal cost to reach i. So dp[i] + cost(i,j) is the cost of a path that goes from 1 to i optimally and then directly to j in one move. That path may involve any number of mode changes before i, and then a single move from i to j (either stairs or elevator). Since the DP recurrence considers all i<j, it will consider the best possible last leg from some i. This is indeed the standard shortest path DP for a DAG where you have edges from every i to every j>i. The optimal path to j will have a last edge from some predecessor i. So the DP is exact.
Thus solution is correct.
Now we need to implement reading inputs, compute prefix sums as we go (since we only need current prefix values). Actually we can compute on the fly without storing full prefix arrays: we can maintain cumulative sums Sa and Sb as we iterate from 1 to n-1. For each floor j (starting from 2), we need A_{j-1} and B_{j-1}. So we can read a_i and b_i sequentially, update cumulative sums, compute dp for floor i+1.
Pseudo:
read n, c
read arrays a[1..n-1], b[1..n-1]
dp[1] = 0
min_stairs = 0 // dp[1] - A0
min_elev = 0 // dp[1] - B0
sa = 0, sb = 0
print dp[1] (with space or newline as per output)
for i from 1 to n-1:
sa += a[i]
sb += b[i]
// now sa = A_i (sum a1..ai) which corresponds to floor i+1
// sb = B_i
dp_next = min(sa + min_stairs, c + sb + min_elev)
print dp_next
// update minima for future
min_stairs = min(min_stairs, dp_next - sa)
min_elev = min(min_elev, dp_next - sb)
We need to store dp values? Only need to output as we compute.
Check for large n, use 64-bit.
Edge Cases: All a_i and b_i are positive. c positive.
Now confirm with the examples.
Implementation straightforward.
But is there any nuance about the possibility of using the elevator without paying c if you're already in the elevator? The problem statement says: "If you are using the elevator, just sum up c and the corresponding values of b_i." So it appears c is added each time you use the elevator, regardless of previous usage. So DP as above is correct.
Thus the solution is trivial O(n).
Wait, but why is this problem considered to be from Codeforces (maybe problem "Elevator or Stairs?"), which often has a DP with states (stairs/elevator) and only moving one floor at a time? That problem is different: you can only move one floor per step, but you can decide to use stairs or elevator each step; if you continue using elevator you don't pay c again; that leads to DP with two states: dp[i][0] (arrived via stairs) and dp[i][1] (arrived via elevator). The recurrence: dp[i][0] = min(dp[i-1][0], dp[i-1][1]) + a_{i-1}; dp[i][1] = min(dp[i-1][0] + c + b_{i-1}, dp[i-1][1] + b_{i-1}). That's a different problem because you can't skip floors; you can only go floor by floor. However, the present problem allows direct jumps, so it's easier? Actually, the DP we derived is even simpler. Let's verify: In the classic "Elevator or Stairs?" problem (CF 1249E), you can only move one floor at a time, but you can choose stairs or elevator each step, with elevator having a waiting time c only when you start using it. That problem yields DP with two states. In our problem, you can jump any number of floors, which seems more general. But interestingly, the DP we derived (with min_stairs and min_elev) yields a solution similar to the DP for the floor-by-floor version? Actually, the floor-by-floor DP also yields an O(n) solution but with two DP values. For our problem, the DP is even simpler because we can directly jump. But is it correct? Let's double-check with some edge cases.
Consider n=3, a=[1000,1], b=[1,1000], c=100. According to intuition: It might be best to take stairs from 1 to 2 (1000) then elevator from 2 to 3 (c+1000=1100) total 2100. Or direct stairs 1->3: 1000+1=1001. Or direct elevator: c+1+1000=1101. Or stairs 1->2 then stairs 2->3: 1000+1=1001 (same as direct stairs). So optimal is 1001. Our DP: dp1=0. min_stairs=0, min_elev=0.
i=1: sa=1000, sb=1 -> dp2 = min(1000+0=1000, 100+1+0=101) => 101? That's weird: elevator from 1 to 2 is c+b1=100+1=101, which is cheaper than stairs. So dp2=101. Then update: min_stairs = min(0, dp2 - sa =101-1000=-899) => -899. min_elev = min(0, dp2 - sb =101-1=100) => 0 (since 0<100).
i=2: sa=1000+1=1001, sb=1+1000=1001 -> dp3 = min(1001 + (-899)=102, 100+1001+0=1101) => 102. So dp3=102. That suggests the optimal time to floor 3 is 102? But earlier we thought 1001. Let's examine: dp2=101 (elevator from 1 to 2). Then from floor 2 to 3, you could take stairs: a2=1, cost 101+1=102. So dp3=102. That is cheaper than direct stairs (1001) or direct elevator (1101) or stairs+ elevator? Indeed, path: 1->2 elevator (101) then stairs 2->3 (1) gives total 102. That's better than direct stairs (1001). So our DP gives 102. That seems correct: The optimal is to take elevator to floor 2 (101) then stairs to floor 3 (1) = 102. That's allowed because you can move directly from 2 to 3 using stairs