We need to interpret problem. Given array a of length n. We need to construct two sequences b (non-decreasing) and c (non-increasing) such that b_i + c_i = a_i for all i. We want to minimize the maximum value among all b_i and c_i. Actually minimize max( max_i b_i, max_i c_i )? But statement: "minimize max(b_i,c_i)". It could be interpreted as minimize the maximum over i of max(b_i, c_i)? Or minimize the maximum number in sequences b and c (the largest value among all entries in b and c). That is essentially minimize M such that we can have b_i, c_i with constraints and all b_i ≤ M, c_i ≤ M? Actually "minimize the maximum number in sequences b and c." So we want the smallest M such that we can assign b_i,c_i satisfying b_i+c_i = a_i, b non-decreasing, c non-increasing, and max_i b_i ≤ M and max_i c_i ≤ M? But careful: Minimize max(b_i,c_i) might mean minimize the maximum value among all b_i and c_i, i.e., minimize the overall maximum. So we want smallest M such that there exist b,c with constraints and all b_i ≤ M and all c_i ≤ M. Since we can shift b up and c down? Actually if we decrease b_i, c_i would increase to keep sum? But both need to be bounded by M.
Alternatively, we could think in terms of choosing a threshold M, and ask if there exists sequences b,c such that each b_i ≤ M, c_i ≤ M, and b_i + c_i = a_i, b non-decreasing, c non-increasing. Then minimize M.
This is reminiscent of typical problems where we need to split each a_i into two parts with monotonic constraints and minimize maximum absolute value or something. Here it's minimize max.
Observe that b is non-decreasing, c is non-increasing. Sum a_i = b_i + c_i. So b_i = a_i - c_i. Since c is non-increasing, c_i ≤ c_{i-1}. Then b_i = a_i - c_i, and b_i ≥ b_{i-1} => a_i - c_i ≥ a_{i-1} - c_{i-1} => (a_i - a_{i-1}) ≥ c_i - c_{i-1}. But c_i - c_{i-1} ≤ 0 because c non-increasing. So condition is a_i - a_{i-1} ≥ c_i - c_{i-1} which is always true? Actually c_i - c_{i-1} ≤ 0, so right side is ≤0, left side may be positive or negative. Not necessarily always true. Let's do proper constraints.
Let b_i = p_i, c_i = q_i. Conditions:
p_i + q_i = a_i.
p_i ≥ p_{i-1} (for i>1).
q_i ≤ q_{i-1} (for i>1).
We want to minimize max( max_i p_i, max_i q_i ).
Idea: For given M, can we construct such sequences with all p_i ≤ M and all q_i ≤ M? Since we can also have values below M (maybe negative allowed? a_i can be negative, so b_i and c_i can be negative. No explicit lower bound; just we want to minimize max, so we care about upper bound only. Lower values can be arbitrarily negative. So the condition is that there exists sequences with p_i ≤ M, q_i ≤ M, and monotonic constraints.
Now think: If we fix M, then the constraints are: p_i = a_i - q_i, so p_i ≤ M => a_i - q_i ≤ M => q_i ≥ a_i - M. Also q_i ≤ M. And p_i ≥ p_{i-1} => a_i - q_i ≥ a_{i-1} - q_{i-1} => q_i - q_{i-1} ≤ a_i - a_{i-1}. And q_i ≤ q_{i-1} (since non-increasing). So we have two constraints on q_i relative to q_{i-1}:
(1) q_i ≤ q_{i-1} (non-increasing).
(2) q_i ≥ q_{i-1} + (a_i - a_{i-1})? Actually from p_i ≥ p_{i-1}: a_i - q_i ≥ a_{i-1} - q_{i-1} => -(q_i - q_{i-1}) ≥ a_{i-1} - a_i => q_i - q_{i-1} ≤ a_i - a_{i-1}. Wait rearrange: a_i - q_i ≥ a_{i-1} - q_{i-1} => a_i - a_{i-1} ≥ q_i - q_{i-1}. So q_i - q_{i-1} ≤ a_i - a_{i-1}. This is a constraint: The increase in q (since could be positive or negative) is bounded above by the difference in a.
Combine with non-increasing: q_i ≤ q_{i-1}. So q_i - q_{i-1} ≤ 0 always. So the condition q_i - q_{i-1} ≤ a_i - a_{i-1} may be automatically satisfied if a_i - a_{i-1} is non-negative? But if a_i - a_{i-1} is negative, then the bound is negative, and we have q_i - q_{i-1} ≤ negative number. Since we also have q_i ≤ q_{i-1}, i.e., q_i - q_{i-1} ≤ 0, the stricter bound is the smaller: min(0, a_i - a_{i-1})? Actually we need both: q_i - q_{i-1} ≤ 0 and q_i - q_{i-1} ≤ a_i - a_{i-1}. So overall upper bound is q_i - q_{i-1} ≤ min(0, a_i - a_{i-1}). There is no lower bound on q_i - q_{i-1} except from the fact that q_i can be arbitrarily negative? Actually from the condition that p_i = a_i - q_i must be ≤ M yields q_i ≥ a_i - M. And also p_i must be such that p_i is non-decreasing; we already used that to get the inequality. But also p_i ≥ p_{i-1} gave that inequality. It does not give a lower bound on q_i - q_{i-1}. So the only constraints are: q_i ≤ q_{i-1}, and q_i - q_{i-1} ≤ a_i - a_{i-1} (which is automatically weaker than ≤0 when a_i - a_{i-1} >=0, but when a_i - a_{i-1} <0, it's stricter, meaning q must drop by at least something). Also we have lower bound from M: q_i ≥ a_i - M, and also q_i ≤ M (obvious). Additionally, we have that p_i ≤ M => a_i - q_i ≤ M => q_i ≥ a_i - M (already). Also p_i has no explicit lower bound? But we might need also p_i to be non-decreasing, we used. Also p_i must satisfy p_i = a_i - q_i, and we don't have lower bound, so negative allowed.
Thus the existence of q satisfying:
- q_i is non-increasing.
- For each i≥2: q_i ≤ q_{i-1} and also q_i - q_{i-1} ≤ a_i - a_{i-1}? Actually both must hold. But note that the inequality q_i - q_{i-1} ≤ a_i - a_{i-1} is derived from p_i ≥ p_{i-1}. It's necessary. However, if we enforce non-increasing and also the bound from M, is that sufficient? Possibly we also need p_i = a_i - q_i to be non-decreasing; that's equivalent to the inequality. So we must enforce both.
But we also have constraints from M: For all i: a_i - M ≤ q_i ≤ M.
Thus we need a sequence q_i that is non-increasing, within the interval [a_i - M, M] for each i, and also satisfy for i≥2: q_i ≤ q_{i-1} + (a_i - a_{i-1})? Wait the inequality is q_i - q_{i-1} ≤ a_i - a_{i-1}. That's an upper bound on the decrease? Actually if a_i - a_{i-1} is negative, then q_i - q_{i-1} ≤ negative, meaning q_i must be less than q_{i-1} by at least |a_i - a_{i-1}|? Let's rearrange: q_i ≤ q_{i-1} + (a_i - a_{i-1}). Since q_i must also be ≤ q_{i-1} (non-increasing), the condition q_i ≤ q_{i-1} + (a_i - a_{i-1}) may be stricter if (a_i - a_{i-1}) < 0: then q_i ≤ q_{i-1} + negative, so q_i must be even smaller. If (a_i - a_{i-1}) >=0, then q_i ≤ q_{i-1} + non-negative, which is automatically satisfied by non-increasing? Actually non-increasing says q_i ≤ q_{i-1}. If (a_i - a_{i-1}) >=0, then q_i ≤ q_{i-1} + positive is weaker (allows q_i up to q_{i-1}+positive). But since we also have q_i ≤ q_{i-1}, the combination still yields q_i ≤ q_{i-1}. So the condition effectively becomes: q_i ≤ q_{i-1} and additionally, when a_i - a_{i-1} < 0, we require q_i ≤ q_{i-1} + (a_i - a_{i-1}) which is stricter than q_i ≤ q_{i-1}. So the constraint can be written as:
q_i ≤ min(q_{i-1}, q_{i-1} + (a_i - a_{i-1})) = q_{i-1} + min(0, a_i - a_{i-1}) = q_{i-1} + (a_i - a_{i-1})? Not exactly: min(q_{i-1}, q_{i-1}+d) = q_{i-1} + min(0,d). So we can say q_i ≤ q_{i-1} + min(0, a_i - a_{i-1}). But since we already require non-increasing (q_i ≤ q_{i-1}), we only need to enforce the stricter one: q_i ≤ q_{i-1} + (a_i - a_{i-1}) when (a_i - a_{i-1}) < 0? Actually it's q_i ≤ q_{i-1} + (a_i - a_{i-1}) always from the inequality. But if (a_i - a_{i-1}) is positive, then q_i ≤ q_{i-1} + positive is a looser upper bound than q_i ≤ q_{i-1}. So the tightest bound is min(q_{i-1}, q_{i-1}+d) = q_{i-1} if d>=0, else q_{i-1}+d. So effectively the constraint is:
q_i ≤ q_{i-1} + min(0, a_i - a_{i-1}).
But note that the inequality from p also imposes something like a lower bound? Let's check if there is any lower bound on q_i relative to q_{i-1} from p non-decreasing? We derived q_i - q_{i-1} ≤ a_i - a_{i-1}. That's only an upper bound. There's no lower bound: q_i could be arbitrarily much smaller than q_{i-1}? But then p_i = a_i - q_i would be larger than p_{i-1}? Actually if q_i decreases a lot, then p_i = a_i - q_i increases, which is fine for non-decreasing? Wait p non-decreasing means p_i ≥ p_{i-1}. If q_i is much smaller, then p_i becomes larger, which still satisfies p_i ≥ p_{i-1}. So indeed q_i can drop arbitrarily. So only an upper bound on q_i relative to q_{i-1}. So q is not only non-increasing but also has an additional upper bound on how much it can increase (or decrease? Actually the bound is on increase: q_i - q_{i-1} ≤ d; if d negative, it forces a minimum drop). So it's a constraint that q_i cannot be too high relative to q_{i-1}; it must be low enough if a drops.
So the condition for q_i (given q_{i-1}) is:
- q_i ≤ q_{i-1} (non-increasing)
- q_i ≤ q_{i-1} + (a_i - a_{i-1}) (from p monotonic)
Actually these are both upper bounds. The stricter is q_i ≤ q_{i-1} + min(0, a_i - a_{i-1}). But we also have lower bounds: q_i ≥ L_i = a_i - M (since p_i ≤ M), and q_i ≤ U_i = M.
Thus for each i, q_i must lie in [L_i, U_i] and also q_i ≤ f(q_{i-1}) where f(q_{i-1}) = q_{i-1} + min(0, a_i - a_{i-1})? Wait careful: The two upper bounds depend on q_{i-1}. The first: q_i ≤ q_{i-1}. The second: q_i ≤ q_{i-1} + (a_i - a_{i-1}). The effective maximum allowed for q_i based on previous is:
upper_i(q_{i-1}) = min( q_{i-1}, q_{i-1} + (a_i - a_{i-1}) ) = q_{i-1} + min(0, a_i - a_{i-1}).
Thus we have: q_i ≤ q_{i-1} + min(0, a_i - a_{i-1}).
Also since q_i must be ≥ L_i and ≤ U_i.
Additionally, q_i must be non-increasing, but this condition is already incorporated because q_i ≤ q_{i-1} is part of that min; but if (a_i - a_{i-1})>0, then min(0,..)=0 so upper_i(q_{i-1})=q_{i-1}, so q_i ≤ q_{i-1} (non-increasing) remains. If (a_i - a_{i-1})<0, then upper_i(q_{i-1}) = q_{i-1} + (a_i - a_{i-1}) which is < q_{i-1}, so q_i must be even lower. So the condition subsumes non-increasing.
Thus the only constraint from monotonicity is: q_i ≤ q_{i-1} + d_i where d_i = min(0, a_i - a_{i-1})? But is that fully equivalent to original? Let's re-derive:
Original: p_i = a_i - q_i, with p_i ≥ p_{i-1}. So a_i - q_i ≥ a_{i-1} - q_{i-1} => q_i ≤ q_{i-1} + (a_i - a_{i-1}). That's the only inequality. There's no explicit q_i ≤ q_{i-1} condition from original; that came from c non-increasing: q_i ≤ q_{i-1}. So we have both: q_i ≤ q_{i-1} (c monotone) and q_i ≤ q_{i-1} + (a_i - a_{i-1}) (p monotone). So indeed two upper bounds. So our combined constraint is q_i ≤ min(q_{i-1}, q_{i-1}+(a_i - a_{i-1})). That's what we wrote.
Now, is there any lower bound from p non-decreasing? Actually p_i ≥ p_{i-1} gave an upper bound on q_i. There's no lower bound from that. So lower bound only from the M condition and maybe from something else? Also we need q_i such that p_i = a_i - q_i is non-decreasing, but that condition only involves q_i relative to q_{i-1} as we used. No lower bound on q_i relative to q_{i-1}.
Thus, for existence of q with given M, we need to find a sequence q_i satisfying:
(1) For each i: L_i ≤ q_i ≤ U_i, where L_i = a_i - M, U_i = M.
(2) For i≥2: q_i ≤ q_{i-1} + d_i, where d_i = min(0, a_i - a_{i-1})? Actually more precisely: q_i ≤ q_{i-1} (which is same as d_i' =0) and q_i ≤ q_{i-1} + (a_i - a_{i-1}). So overall q_i ≤ q_{i-1} + delta_i where delta_i = (a_i - a_{i-1}) if that is negative? No, because when a_i - a_{i-1} positive, the condition q_i ≤ q_{i-1} is tighter, which is equivalent to q_i ≤ q_{i-1} + 0. So we can set delta_i = 0 if a_i - a_{i-1} > 0, else delta_i = a_i - a_{i-1} (which is negative). So yes, delta_i = min(0, a_i - a_{i-1}).
Thus q_i ≤ q_{i-1} + delta_i, where delta_i = min(0, a_i - a_{i-1}).
Also note that there is no condition linking q_i from the left, i.e., we don't have a lower bound on q_i from previous. So we can always choose q_i as low as we want subject to lower bound L_i. But we also have the upper bound from previous and the global U_i.
Thus to check feasibility, we can attempt to construct q from left to right: start with q_1 must be within [L_1, U_1] and we can choose any. Then for each i, given q_{i-1}, the allowed q_i is: lower bound = L_i, upper bound = min( U_i, q_{i-1}+delta_i ). Since we want existence, we can try to pick q_i as high as possible to keep future options? Actually since only upper bounds propagate, and lower bounds are independent, we need to ensure that at each step, the interval [L_i, min(U_i, q_{i-1}+delta_i)] is non-empty. Also, after choosing q_i, we proceed.
But we also need to consider that we could pick q_1 arbitrarily within its interval. So we can try to see if we can choose q_1 to make it possible. So the feasibility condition is: there exists q_1 in [L_1, U_1] such that we can assign subsequent q_i satisfying constraints.
We can treat this as a range of possible q_i values at each step. Initially for i=1, possible values are [L_1, U_1]. For each step, given that q_{i-1} is in some interval S_{i-1}, the possible q_i values are: all values v such that v ∈ [L_i, U_i] and v ≤ q_{i-1}+delta_i for some q_{i-1} ∈ S_{i-1}. Since the condition is v ≤ q_{i-1}+delta_i, given v we need existence of q_{i-1} ∈ S_{i-1} with q_{i-1} ≥ v - delta_i? Actually it's v ≤ q_{i-1}+delta_i => q_{i-1} ≥ v - delta_i. So for each v, we need v - delta_i ≤ some q_{i-1} in S_{i-1}. Since we can choose q_{i-1} freely from its possible set, the condition for v to be feasible is that there exists q_{i-1} in S_{i-1} such that q_{i-1} ≥ v - delta_i. Since S_{i-1} is an interval (maybe), we need v - delta_i ≤ max(S_{i-1}). Also we need v ≥ L_i and v ≤ U_i.
Thus the set of possible v is: v ∈ [L_i, U_i] and v ≤ max(S_{i-1}) + delta_i? Actually careful: The condition q_{i-1} ≥ v - delta_i. Since q_{i-1} can be as high as R_{i-1} = max(S_{i-1}), we can always pick q_{i-1} = R_{i-1} if R_{i-1} ≥ v - delta_i. So condition becomes v ≤ R_{i-1} + delta_i. Also we need q_{i-1} to be within S_{i-1} and at least some lower bound? Actually we also need existence of q_{i-1} meeting the lower bound; but if v - delta_i is less than or equal to max(S_{i-1}), we can choose q_{i-1} = max(S_{i-1}) (or any value in between). But we must also ensure that the chosen q_{i-1} satisfies its own constraints; but if we are constructing forward, we only need that there is some q_{i-1} in S_{i-1}. Since we consider the set S_{i-1} as all values that can be achieved at step i-1, we just need to check existence. So the condition for v is v ≤ R_{i-1} + delta_i. However, we also need to consider the lower bound of S_{i-1}? Because if v - delta_i is larger than the maximum? Actually if v - delta_i is > max(S_{i-1}), then no q_{i-1} exists. So the condition is v - delta_i ≤ max(S_{i-1}). That's equivalent to v ≤ max(S_{i-1}) + delta_i. So indeed it's an upper bound. There is no lower bound from previous besides v≥ L_i. So the set S_i becomes [L_i, min(U_i, R_{i-1}+delta_i)]? But careful: what about the minimum possible q_i? Could we get q_i lower than L_i? No. So lower bound is L_i. But what about lower bound from previous? Actually the condition v ≥ something from previous? There is none; we only have upper bound from previous. So the set S_i of possible q_i values is indeed all v such that v ∈ [L_i, U_i] and v ≤ R_{i-1}+delta_i. That's an interval from L_i to min(U_i, R_{i-1}+delta_i). However, is it always contiguous? Yes. But we must also consider that we might need to keep track of possible lower values for future? For existence, we just need that this interval is non-empty at each step. And we can always pick q_i as the maximum possible (i.e., min(U_i, R_{i-1}+delta_i)) to maintain largest possible R_i for future, because future constraints only depend on the maximum possible q value from the set? Let's examine: The condition for next step i+1: v ≤ max(S_i) + delta_{i+1}. So the higher the max(S_i), the looser the upper bound for next v. The lower bound doesn't affect the ability to achieve high v later. So a greedy strategy: keep q_i as high as possible (i.e., the maximum feasible value) to maximize flexibility. This yields the maximum possible R_i = min(U_i, R_{i-1}+delta_i). Starting with R_1 = U_1? Actually we need to choose q_1 within [L_1, U_1]. To maximize future, we would set q_1 = U_1 (since higher q_1 gives larger R for next steps). However, is there any case where picking a lower q_1 could allow a later step that would otherwise be impossible because of lower bound? Since later steps only have an upper bound from previous and a lower bound from L_i, the only potential issue is if the upper bound becomes less than L_i. Picking a lower q_1 would reduce R_1, which might reduce R_2 = min(U_2, R_1+delta_2). Since we want to keep the upper bound as large as possible, picking the maximum q_1 is optimal. So indeed feasibility can be determined by starting with q_1 = U_1 (the maximum allowed) and then recursively define R_i = min(U_i, R_{i-1}+delta_i) for i≥2. At each step we need to check that R_i ≥ L_i. If at any point R_i < L_i, then it's impossible for that M (since even with the highest possible previous q, the upper bound is below the mandatory lower bound). Conversely, if this holds for all i, then there exists a feasible assignment (by setting each q_i = R_i). But we also need to consider that q_1 might need to be lower to satisfy something else? Let's test: Suppose we set q_1 = U_1, then compute R_2 = min(U_2, U_1+delta_2). If R_2 < L_2, is it possible to adjust q_1 lower to make R_2 ≥ L_2? Since R_2 = min(U_2, q_1+delta_2). Lowering q_1 could reduce q_1+delta_2 further, making R_2 even smaller (since min with U_2 could be unchanged if U_2 is larger; but if the constraint is from q_1+delta_2 being smaller than U_2, then reducing q_1 reduces the bound, making it worse). So if R_2 < L_2 due to q_1+delta_2 being too low, lowering q_1 makes it even lower, not helping. If R_2 is limited by U_2 (i.e., U_2 < q_1+delta_2), then R_2 = U_2. Lowering q_1 could potentially keep R_2 = U_2 if q_1+delta_2 remains >= U_2. But if q_1+delta_2 drops below U_2, then R_2 becomes smaller. So the maximum possible R_2 given we can choose q_1 is: max over q_1 in [L_1, U_1] of min(U_2, q_1+delta_2). This maximum is achieved at the maximum q_1, i.e., U_1. Because min(U_2, q_1+delta_2) is non-decreasing in q_1 (since q_1+delta_2 increases with q_1). So indeed the best R_2 is attained with maximum q_1. So if even with maximum q_1 we cannot achieve R_2 ≥ L_2, then no choice works. So the greedy strategy is correct.
Thus feasibility reduces to:
Initialize R = M (since U_1 = M). Actually U_1 = M, L_1 = a_1 - M.
Check that R >= L_1, i.e., M >= a_1 - M => 2M >= a_1? That's not required yet; we need that there exists q_1 in [a_1-M, M]. That requires a_1-M ≤ M => 2M ≥ a_1. So initial condition: 2M ≥ a_1. But also we must be able to choose q_1 such that it's within that interval; we will set q_1 = M, but that is only valid if M ≥ a_1 - M, i.e., 2M ≥ a_1. So if 2M < a_1, then no q_1 exists because the interval is empty. So indeed the first condition is 2M ≥ a_1.
Now for i from 2 to n:
R = min(M, R + delta_i) where delta_i = min(0, a_i - a_{i-1}).
Then check if R ≥ a_i - M. If at any step fails, M is infeasible.
Thus the condition can be turned into a formula involving M.
We need to find the minimal M such that this process succeeds.
Now the problem also has q updates: range add x. Need to recompute minimal M after each update.
Thus we need to compute a function f(M) = whether feasible, and find minimal M. Since M appears linearly in the conditions? Let's examine.
Given M, we have U_i = M (for all i). L_i = a_i - M. And delta_i depends only on differences of a, independent of M. So the recurrence:
R_1 = M (since we set q_1 = M, the maximum possible). Actually we could also consider that we can choose q_1 lower, but we argued that maximum gives best. So we compute:
R_1 = M.
For i=2..n:
R_i = min( M, R_{i-1} + d_i ) where d_i = min(0, a_i - a_{i-1}).
Then check that for each i: R_i >= a_i - M.
Now note R_i is a function of M, and it's essentially the maximum possible q_i we can achieve while keeping all constraints satisfied, assuming we always choose the maximum feasible q at each step. Since we want to check feasibility, we just need to ensure that for each i, the interval [a_i - M, min(M, ...)] is non-empty. The maximum feasible q_i is indeed R_i as defined.
But careful: At step i, we also have the lower bound L_i; we need existence, i.e., L_i ≤ max feasible q_i (R_i). That is the condition.
Thus feasibility reduces to:
For all i, R_i >= a_i - M.
Now R_i can be expressed as: starting with R_0? Actually we can compute R_i recursively: R_i = min( M, R_{i-1} + d_i ). And R_1 = min( M, ??? Actually we set q_1 = M is feasible if M >= a_1 - M, i.e., 2M >= a_1. But R_1 is defined as maximum possible q_1, which is M if interval non-empty, else no feasible. So condition 1: 2M >= a_1.
But note: For i=1, the condition R_1 >= a_1 - M is equivalent to M >= a_1 - M => 2M >= a_1, same as above.
So we can include i=1 in the condition if we define R_0 perhaps? Let's unify: define R_0 = +∞? Not needed. Let's treat i=1 separately.
Now R_i is the min of M and something like R_{i-1} + d_i. Since d_i ≤ 0, R_{i-1}+d_i ≤ R_{i-1}. So R_i is either M if M is lower than the other term? Actually min(M, something). Since something = R_{i-1}+d_i ≤ R_{i-1} ≤ M? Wait R_{i-1} might be less than M; it's the maximum feasible q_{i-1}. Since we take min with M, R_i ≤ M. Also R_{i-1}+d_i might be less than M, so R_i = R_{i-1}+d_i in that case. So essentially R_i is a kind of capped decreasing sequence? Because d_i ≤ 0, so R_{i-1}+d_i ≤ R_{i-1}. So the recurrence reduces the value unless it goes below something? But the min with M means that if at some point R_{i-1}+d_i is > M, then R_i = M? But since R_{i-1} ≤ M and d_i ≤ 0, R_{i-1}+d_i ≤ M, so it's never > M. Actually R_{i-1} ≤ M, and d_i ≤ 0, so sum ≤ M. Thus min(M, sum) = sum always because sum ≤ M. Wait is it always true that R_{i-1} ≤ M? Initially R_1 = M if feasible, else not. So R_1 = M. Then R_2 = min(M, R_1 + d_2) = min(M, M + d_2). Since d_2 ≤ 0, M + d_2 ≤ M, so min = M + d_2. So R_2 = M + d_2. Then R_3 = min(M, R_2 + d_3) = min(M, M + d_2 + d_3). Since d_2 + d_3 ≤ d_2 ≤ 0? Actually each d_i ≤ 0, so cumulative sum ≤ M, so min = cumulative sum. So indeed, as long as we start with R_1 = M, we have R_i = M + sum_{k=2}^{i} d_k (if we define d_1 = 0?). Let's formalize:
Let d_1 = 0? Actually for i=1, R_1 = M. For i>=2, R_i = M + sum_{j=2}^{i} d_j, because d_j = min(0, a_j - a_{j-1}). But careful: We need to verify that at each step, R_{i-1}+d_i = M + sum_{j=2}^{i-1} d_j + d_i = M + sum_{j=2}^{i} d_j. Since d_j ≤ 0, this sum is ≤ M, so min with M is that sum. So indeed R_i = M + S_i, where S_i = sum_{j=2}^{i} d_j, with S_1 = 0? Actually define S_1 = 0. Then for i≥1, R_i = M + S_i, where S_i = sum_{j=2}^{i} d_j (with empty sum = 0 for i=1). Let's verify: For i=1, sum over j=2..1 empty => 0, so R_1 = M = M + 0 ok. For i=2, S_2 = d_2, R_2 = M + d_2, correct. So R_i = M + prefix sum of d's from 2 to i.
Thus the feasibility condition becomes: For each i, M + S_i ≥ a_i - M, i.e., 2M ≥ a_i - S_i.
Define T_i = a_i - S_i. Then condition: 2M ≥ max_i T_i. Also need the initial condition that interval [a_i - M, M] non-empty which is already covered: For i=1, T_1 = a_1 - 0 = a_1, so 2M ≥ a_1. So condition is simply 2M ≥ max_i T_i.
But is that sufficient? Let's double-check: We also need that at each step, the chosen q_i = R_i is actually ≥ a_i - M. That's exactly 2M ≥ a_i - S_i. Also need that q_i ≤ M? Since we set q_i = R_i = M + S_i, but S_i ≤ 0, so R_i ≤ M, automatically satisfied. And q_i ≥ a_i - M? That's the condition. Also need that q_i is within [a_i-M, M]; lower bound is the condition, upper bound automatically as R_i ≤ M. Also need to ensure that q_i is indeed feasible given q_{i-1}=R_{i-1}. Since we derived R_i from recurrence, it satisfies q_i ≤ q_{i-1} + d_i? Actually we constructed it as the maximum feasible, so it satisfies constraints. So condition reduces to 2M ≥ max_i T_i.
But wait, we also need to check that the lower bound for q_i (a_i - M) is not greater than the maximum possible q_i (R_i). This is exactly 2M ≥ a_i - S_i. So the minimal M is half of the maximum of T_i. But M must also be at least? There's no other constraints? Could M be negative? The problem does not restrict b_i, c_i to be non-negative. So M can be negative if T_i negative. But is there any other constraint like the intervals for q_i must be non-empty also require a_i - M ≤ M, i.e., 2M ≥ a_i, which is included because T_i may be larger than a_i? Let's see: T_i = a_i - S_i. S_i is sum of some non-positive numbers (d_j ≤ 0), so -S_i ≥ 0, thus T_i ≥ a_i. Actually S_i ≤ 0, so subtracting S_i (which is negative or zero) yields a_i - S_i = a_i + (-S_i) ≥ a_i. So T_i >= a_i. So max_i T_i >= max_i a_i? Not necessarily, but S_i could be zero, so at least a_i. So the condition 2M ≥ max_i T_i automatically implies 2M ≥ a_i for each i? Not necessarily all i, but if max_i T_i is some value, then 2M >= that, which is >= a_i for that i, but for other i, a_i could be larger than max_i T_i? Actually T_i >= a_i, so max_i T_i >= max_i a_i. So 2M >= max_i T_i implies 2M >= max_i a_i, hence for any i, a_i ≤ max_i a_i ≤ max_i T_i ≤ 2M, so 2M >= a_i holds. So the interval condition is satisfied for all i automatically if condition holds. So indeed feasibility is exactly 2M >= max_i T_i.
Thus minimal M = ceil( max_i T_i / 2 )? But M must be a real number? Since b_i and c_i are integers? The problem states integers for b and c. However, input a_i are integers, changes x are integers. So we likely need integer sequences b,c. Therefore M must be such that we can assign integer b_i,c_i? But the minimization of max(b_i,c_i) over integers? The answer might be integer? The examples show integers. But we need to confirm: If max_i T_i is odd, can we achieve M = (max_i T_i)/2? If max_i T_i is odd, then 2M = odd, so M is half-integer. But b_i,c_i need to be integers, so perhaps the minimal integer M might be ceil(max_i T_i / 2). Let's test: Example 1 initial a = [2, -1, 7, 3]. Compute d_i: d2 = min(0, -1-2) = min(0, -3) = -3; d3 = min(0, 7 - (-1)) = min(0,8)=0; d4 = min(0, 3-7) = min(0, -4)= -4. S1=0, S2=-3, S3=-3, S4=-7. T_i = a_i - S_i: i1:2-0=2; i2:-1-(-3)=2; i3:7-(-3)=10; i4:3-(-7)=10. Max T_i = 10. Then M_min = ceil(10/2)=5. Output is 5. matches.
After first change: a becomes (2, -4, 4, 0). Compute d2 = min(0, -4-2)= -6; d3 = min(0,4-(-4)=8 =>0); d4 = min(0,0-4= -4 => -4). S: [0, -6, -6, -10]. T: i1:2; i2:-4-(-6)=2; i3:4-(-6)=10; i4:0-(-10)=10. max=10 => M=5. Output 5.
After second change: a becomes (2, -4, 6, 2). d2 = -6; d3 = min(0,6-(-4)=10=>0); d4 = min(0,2-6=-4=>-4). S: [0,-6,-6,-10]. T: i1:2; i2:-4-(-6)=2; i3:6-(-6)=12; i4:2-(-10)=12. max=12 => M=6. Output 6. matches.
Second example: a = [-9,-10,-9,-6,-5,4]. Compute d_i:
i2: a2-a1 = -10-(-9)=-1 => min(0,-1) = -1
i3: a3-a2 = -9-(-10)=1 => min(0,1)=0
i4: a4-a3 = -6-(-9)=3 =>0
i5: a5-a4 = -5-(-6)=1=>0
i6: a6-a5 = 4-(-5)=9=>0.
S: S1=0, S2=-1, S3=-1, S4=-1, S5=-1, S6=-1.
T_i = a_i - S_i:
i1: -9-0=-9
i2: -10-(-1)=-9
i3: -9-(-1)=-8
i4: -6-(-1)=-5
i5: -5-(-1)=-4
i6: 4-(-1)=5
max = 5 => M_min = ceil(5/2)=3? Actually 5/2=2.5, ceil=3. Output 3. matches.
After changes we need to compute similarly; final answer 1 after 3 changes. So formula seems correct.
Third example: n=1, a=[0]. d none. T1 = a1 = 0. max=0 => M=0. Output 0. After changes: a becomes -1 => T1=-1 => max=-1 => M = ceil(-1/2) = -0.5? Ceil of -0.5 is 0? Actually integer M? The answer after two changes is -1. Let's compute: After first change: a=[-1]. T1=-1. max=-1. Need smallest integer M such that 2M >= -1. That gives M >= -0.5, so integer M can be 0? But output is 0 after first change? Actually sample output for third test:
1
0
2
1 1 -1
1 1 -1
Output:
0
0
-1
Wait lines: first line answer for initial (0 changes): 0. After first change: 0. After second change: -1. So after first change, a = -1, they answer 0. Our computed T_max = -1, 2M >= -1 => M >= -0.5, smallest integer M satisfying is 0, so answer 0. Good. After second change, a becomes -2? Actually they apply same change again: add -1 to a[1] so a becomes -2. T_max = -2, 2M >= -2 => M >= -1, so M = -1 is integer and minimal. Output -1. matches.
Thus the minimal integer M is ceil( max_i T_i / 2 ). But careful: if max_i T_i is integer and even, M = max_i T_i / 2; if odd, M = (max_i T_i+1)/2. However, consider max_i T_i negative even: e.g., -2 => ceil(-1) = -1? Actually ceil(-2/2) = ceil(-1) = -1? But -2/2 = -1 exactly, ceil(-1)= -1. That matches M >= -1? Condition 2M >= -2 => M >= -1, so M can be -1, -0.5? integer minimal is -1. So ceil works: ceil(-1) = -1. For -3 => -3/2 = -1.5, ceil(-1.5) = -1, because ceil rounds up to greater integer. Condition: 2M >= -3 => M >= -1.5, smallest integer >= -1.5 is -1, correct. So M = ceil( max_i T_i / 2 ). Since we need integer M (since answer must be integer, sample outputs are integer), we can compute that.
Therefore the problem reduces to: For the given array a, compute T_i = a_i - S_i, where S_i = sum_{j=2}^{i} d_j, d_j = min(0, a_j - a_{j-1}). Then answer = ceil( max_i T_i / 2 ). After each range addition (add x to a[l..r]), we need to recompute the new max_i T_i efficiently.
Now we need to understand the structure of T_i. Let's define some prefix sums.
Let’s define an array A (a_i). Let’s define d_i = min(0, a_i - a_{i-1}) for i>=2. Then S_i = sum_{j=2}^{i} d_j. So T_i = a_i - S_i.
We can try to express T_i in terms of original a and maybe some cumulative mins.
Observe that d_j is the negative part of the difference: d_j = (a_j - a_{j-1}) if a_j < a_{j-1}, else 0. So S_i is sum of decreases in a from previous element, i.e., total drop from a_1 to a_i only counting drops, not increases. More precisely, define a function f(i) = a_i + total cumulative drops? Let's compute:
Let’s define cumulative drop D_i = sum_{j=2}^{i} min(0, a_j - a_{j-1}). Then T_i = a_i - D_i.
We can also compute T_i recursively: T_1 = a_1.
For i>=2: T_i = a_i - D_i. But D_i = D_{i-1} + min(0, a_i - a_{i-1}).
Now consider T_i - T_{i-1} = (a_i - D_i) - (a_{i-1} - D_{i-1}) = (a_i - a_{i-1}) - (D_i - D_{i-1}) = (a_i - a_{i-1}) - min(0, a_i - a_{i-1}) = max(0, a_i - a_{i-1}). Because subtract the negative part leaves the positive part. Indeed, if a_i - a_{i-1} >= 0, min=0, so T_i - T_{i-1} = a_i - a_{i-1} (positive). If a_i - a_{i-1} < 0, min = a_i - a_{i-1} (negative), so difference = (a_i - a_{i-1}) - (a_i - a_{i-1}) = 0. So T_i - T_{i-1} = max(0, a_i - a_{i-1}) = (a_i - a_{i-1})_+.
Thus T_i is a non-decreasing sequence! Because increments are non-negative. Indeed, T_1 = a_1. For i>1, T_i = T_{i-1} + max(0, a_i - a_{i-1}). So T_i = a_1 + sum_{j=2}^{i} max(0, a_j - a_{j-1}). That's a well-known "upside" accumulation: T_i is the maximum of a_1..a_i? Wait not exactly; it's the total increase from start: If we define a running max? Actually consider the sequence of cumulative positive differences: Starting from a_1, each time a_i > a_{i-1}, we add the increase; if a_i ≤ a_{i-1}, we add 0. So T_i = a_1 + sum_{j=2}^{i} (a_j - a_{j-1})+ = the "rising water" level? It's essentially the smallest non-decreasing sequence that is ≥ a_i? Let's test: Suppose a = [2, -1, 7, 3]. Compute T: T1=2. i2: a2-a1 = -3 => max0 =0 => T2=2. i3: a3-a2=8 => T3=2+8=10. i4: a4-a3=-4 =>0 => T4=10. So T = [2,2,10,10]. Indeed max T_i = 10. That matches earlier T. So T_i is basically the maximum value encountered so far if we start from a1 and only increase when we see a higher value? Actually if a sequence has ups and downs, T_i is the cumulative sum of positive differences, which results in T_i being the maximum of a_1..a_i? Let's check: maximum of [2, -1, 7, 3] is 7, but T_i = 10, not 7. So it's not the max. It's something else: Actually sum of positive differences from the start: At i=2, a2 is lower, so no increase; at i=3, a3 is 7, difference from -1 to 7 is 8, so T3 = 2+8=10, which is 2 + (7 - (-1)) = 2+8=10. That is a1 + (a3 - a1) if we ignore intermediate dips? It's like reconstructing a non-decreasing sequence that is the "upper envelope" of a? More precisely, T_i = a_1 + Σ{j=2}^{i} max(0, a_j - a_{j-1}) = a_i + Σ_{j=2}^{i} max(0, a_{j-1} - a_j)? Not sure.
But we have a nice property: T_i is non-decreasing, and T_i = max_{k ≤ i} something? Actually note that T_i can be expressed as: T_i = max_{1≤k≤i} ( a_k + sum_{j=k+1}^{i} max(0, a_j - a_{j-1}) )? Not simple.
But importantly, T_i = a_i + total negative drops? Actually earlier T_i = a_i - D_i, where D_i is total drops. Since D_i = sum of decreases, T_i = a_i - (sum of drops) = a_i + total magnitude of drops? Since drops are negative, D_i ≤ 0, so T_i = a_i - D_i = a_i + (-D_i). -D_i is sum of positive parts of - (a_j - a_{j-1})? Actually -min(0, a_j - a_{j-1}) = max(0, a_{j-1} - a_j). So -D_i = sum_{j=2}^{i} max(0, a_{j-1} - a_j). So T_i = a_i + sum_{j=2}^{i} max(0, a_{j-1} - a_j). That is a_i plus total absolute drops (i.e., cumulative positive backward differences). That is different.
Anyway, the key is: T_i is non-decreasing, so max_i T_i = T_n (the last element) if the sequence ends at the maximum? Since it's non-decreasing, the maximum is the last one. Wait T_i is non-decreasing: T_{i} = T_{i-1} + max(0, a_i - a_{i-1}) ≥ T_{i-1}. So yes, T_i is non-decreasing. Therefore the maximum over i of T_i is simply T_n. Let's verify with examples: In example 1, T = [2,2,10,10]; T_n = 10, max = 10. Example 2: a = [-9,-10,-9,-6,-5,4]. Compute T: T1 = -9; i2: diff = -1 => max0=0 => T2=-9; i3: diff = 1 => max0=1 => T3 = -8; i4: diff = 3 => max0=3 => T4 = -5; i5: diff = 1 => max0=1 => T5 = -4; i6: diff = 9 => max0=9 => T6 = 5. So T_n=5, max=5. Indeed T is non-decreasing. So max_i T_i = T_n.
But is it always true that T_i is non-decreasing? Yes, because increments are non-negative. So T_n is the maximum. Then M_min = ceil( T_n / 2 ). So answer reduces to computing T_n = a_1 + Σ_{i=2}^n max(0, a_i - a_{i-1}). That is simply: starting from first element, whenever a_i > a_{i-1}, add the increase; else do nothing. So T_n = a_1 + sum of positive differences = a_1 + Σ_{i=2}^n (a_i - a_{i-1})_+. This is essentially the total "rise" from the start.
Let's denote F(a) = a_1 + Σ_{i=2}^n max(0, a_i - a_{i-1}). Then answer = ceil(F(a)/2). For n=1, F = a_1.
Check: Example 1 initial: a_1=2; positive diffs: (-1-2) negative, (7-(-1)=8) positive, (3-7) negative. So F=2+8=10 => answer ceil(10/2)=5. After first change: a = [2, -4, 4, 0]; diffs: ( -4-2=-6) neg, (4-(-4)=8) pos, (0-4=-4) neg => F=2+8=10 => ans5. After second: a = [2, -4, 6, 2]; diffs: -6 neg, (6-(-4)=10) pos, (2-6=-4) neg => F=2+10=12 => ans6. Works.
Example 2: a1=-9; diffs: -1 neg, 1 pos -> add1 => -8, 3 pos => add3 => -5, 1 pos => add1 => -4, 9 pos => add9 => 5. So F=5 => ans3.
Example 3: n=1, F = a1 = 0 => ans0; after change: a1=-1 => F=-1 => ceil(-0.5)=0? Actually ceil(-0.5)=0; but answer 0. After second: a1=-2 => F=-2 => ceil(-1)=-1 => ans -1. Works.
So the answer is simply ceil( (a_1 + sum_{i=2}^n max(0, a_i - a_{i-1}) ) / 2 ). This is neat.
Now we need to handle range add updates. After each update (add x to a[l..r]), we need to recompute F = a_1 + Σ_{i=2}^n max(0, a_i - a_{i-1}) efficiently.
This is a classic problem: maintain an array a, support range add, and query the value of a_1 + sum of positive adjacent differences. This can be done with a data structure like a segment tree that tracks the sum of positive differences. Since updates are range add, the differences only change at boundaries (l-1, l, r, r+1) except for the interior where adding constant to a contiguous segment doesn't change differences between elements inside the segment? Let's see: For i from l+1 to r, a_i and a_{i-1} both increase by x, so difference a_i - a_{i-1} remains unchanged. For i = l, if l > 1, the difference d_l = a_l - a_{l-1} changes by +x (since a_l increases by x, a_{l-1} unchanged). For i = r+1, if r+1 ≤ n, the difference d_{r+1} = a_{r+1} - a_r changes by -x (since a_r increases by x, a_{r+1} unchanged). Also a_1 itself may change if l==1, then a_1 increases by x, affecting F directly via a_1 term and also d_2 (if n>=2) because d_2 = a_2 - a_1 changes by -x (if l=