Alicia has an array, a_1, a_2, …, a_n, of non-negative integers. For each 1 ≤ i ≤ n, she has found a non-negative integer x_i = max(0, a_1, …, a_{i-1}). Note that for i=1, x_i = 0.
For example, if Alicia had the array a = {0, 1, 2, 0, 3}, then x = {0, 0, 1, 2, 2}.
Then, she calculated an array, b_1, b_2, …, b_n: b_i = a_i - x_i.
For example, if Alicia had the array a = {0, 1, 2, 0, 3}, b = {0-0, 1-0, 2-1, 0-2, 3-2} = {0, 1, 1, -2, 1}.
Alicia gives you the values b_1, b_2, …, b_n and asks you to restore the values a_1, a_2, …, a_n. Can you help her solve the problem?
Input
The first line contains one integer n (3 ≤ n ≤ 200 000) – the number of elements in Alicia's array.
The next line contains n integers, b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that for the given array b there is a solution a_1, a_2, …, a_n, for all elements of which the following is true: 0 ≤ a_i ≤ 10^9.
Output
Print n integers, a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9), such that if you calculate x according to the statement, b_1 will be equal to a_1 - x_1, b_2 will be equal to a_2 - x_2, ..., and b_n will be equal to a_n - x_n.
It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique.
Examples
Input
5
0 1 1 -2 1
Output
0 1 2 0 3
Input
3
1000 999999000 -1000000000
Output
1000 1000000000 0
Input
5
2 1 2 2 3
Output
2 3 5 7 10
Note
The first test was described in the problem statement.
In the second test, if Alicia had an array a = {1000, 1000000000, 0}, then x = {0, 1000, 1000000000} and b = {1000-0, 1000000000-1000, 0-1000000000} = {1000, 999999000, -1000000000}.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to reconstruct a_i from b_i. Given b_i = a_i - x_i, where x_i = max(0, a_1, …, a_{i-1}) (and x_1 = 0). Also given constraints that a_i ∈ [0, 10^9], and existence and uniqueness guaranteed.
We need to compute a_i sequentially. Let's define current maximum M_i = max(0, a_1, ..., a_i). Then x_i = M_{i-1} (with M_0 = 0). So b_i = a_i - M_{i-1}. Then a_i = b_i + M_{i-1}. But we also need to ensure that a_i >= 0 (given it's non-negative) and also M_i = max(M_{i-1}, a_i). This is consistent if we compute sequentially.
Start with M = 0.
For i = 1 to n:
a_i = b_i + M
if a_i < 0? but guarantee says solution exists and a_i within [0,1e9], so it should be fine. But careful: b_i could be negative, but a_i must be >=0, and we trust input.
Then update M = max(M, a_i).
But is this always correct? Let's test with examples:
Seems straightforward. But is the solution unique? Yes, because M_{i-1} is determined from earlier a's, which are determined sequentially uniquely. So the algorithm is simply:
Initialize max_so_far = 0.
For i from 1 to n:
a_i = b_i + max_so_far
Print a_i
if a_i > max_so_far: max_so_far = a_i
This works given the constraints. However, need to verify that it's always consistent with x_i = max(0, a_1,...,a_{i-1}) = M_{i-1}. Since we compute M_{i-1} as the maximum up to i-1, that's correct. So indeed b_i = a_i - M_{i-1} gives a_i = b_i + M_{i-1}. Then after computing a_i, we update M_i. Since M_i is non-decreasing (never goes down) because we take max. So the algorithm is O(n).
Edge cases: n up to 200k, values up to 1e9 for a_i, but b_i can be -1e9 to 1e9. The sum could be up to 2e9? Actually a_i = b_i + M_{i-1} and M_{i-1} could be up to 1e9, and b_i up to 1e9, so a_i could be up to 2e9? But problem says a_i ≤ 10^9. So inputs are such that a_i computed will be ≤ 1e9. But our code doesn't need to validate; we just output.
But wait: Since b_i can be negative, a_i might become negative if M_{i-1} is not large enough. However, guarantee ensures a valid a exists, so it won't be negative. But we could add an assert or ignore.
Thus solution is trivial? But maybe there is nuance: x_i = max(0, a_1,...,a_{i-1}). For i=1, x_1 = 0. That's consistent.
Thus answer: read n, array b, compute sequentially.
But I must confirm: Is there any situation where the computed a_i might exceed 1e9? Possibly, but input guarantees existence of a solution with a_i ≤ 1e9, so our computed a_i must be that solution. So it's fine.
Double-check with some custom tests:
If b = [0, 0, 0]. Then a1=0, M=0; a2=0, M=0; a3=0, M=0 => a = [0,0,0]. Works.
If b = [5, -5, 10]. Then a1=5, M=5; a2 = -5+5 =0, M=5; a3=10+5=15 => a3=15 which >1e9? Not necessarily, but 15 is okay. So works.
If b = [1000000000, 0, ...] then a1=1e9, M=1e9; a2=0+1e9=1e9, M=1e9; then fine.
Edge: b = [-1000000000, 1000000000]. Then a1 = -1e9+0 = -1e9, invalid. But guarantee says there exists a solution, so such b wouldn't be given.
Thus algorithm is simple. However, problem might be from some contest, and the catch could be that we need to ensure x_i = max(0, a_1,...,a_{i-1}) not just previous maximum, but it's exactly the maximum of all previous values (including zero). Since we start with M=0, that's correct. And we never decrease M because we keep max. So yes.
Thus the solution is to implement that.
Now time complexity O(n) and memory O(1) aside from input.
Implementation details:
Read n (int)
Read list b (list of ints) of length n
Initialize max_sofar = 0
Initialize result list a = []
For i from 0 to n-1:
a_i = b[i] + max_sofar
append a_i
if a_i > max_sofar: max_sofar = a_i
Print the a list separated by spaces.
Check constraints: n up to 200k, fine.
One more nuance: Since a_i can be up to 1e9, but b_i can be up to 1e9 and M up to 1e9, sum can be 2e9, but Python int can handle big numbers. However final output must be within [0,1e9] per guarantee.
Test with given examples.
We should also consider the possibility that the problem wants us to output the unique solution, but is the approach always correct? Let's prove:
Let M_0 = 0. For i=1..n, define a_i = b_i + M_{i-1}, and then M_i = max(M_{i-1}, a_i). Then by construction, M_{i-1} = max(0, a_1, ..., a_{i-1}) (by induction: base i=1: M_0=0, works; assume for i-1, M_{i-1} = max(0, a_1,...,a_{i-1}). Then after computing a_i, M_i = max(M_{i-1}, a_i) = max(0, a_1,...,a_i). So holds). Then b_i = a_i - M_{i-1}. So it satisfies the equation. Since any solution must satisfy these recurrences (given that M_{i-1} is determined by previous a's), this yields the unique solution.
Thus solution is correct.
Thus final code is straightforward.
But the problem statement might be tricky: It says "It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique." So indeed.
Now we can produce answer.
But wait, could there be any alternative interpretation? Let's double-check: x_i = max(0, a_1, …, a_{i-1}). Note that max includes zero explicitly. So if all previous a_j are negative? But a_j are non-negative, so they are always >=0, but max includes zero ensures that when i=1, x1=0, and for i>1, if all previous a_j are zero, max is 0. So it's consistent.
Thus the algorithm is correct.
Therefore answer: implement as described.
But is there any issue about integer overflow in languages like C++? But we are writing in Python, no issue.
Thus final code.
But the problem might be from Codeforces? Then maybe there is a catch: The b_i values could be large, and we must compute a_i, but the straightforward method could produce a_i > 1e9? But guarantee says solution exists, so the computed a_i will be within [0,1e9]. However, if we compute with the algorithm, could we get an a_i that is > 1e9 but still the actual solution is different? Let's test: Suppose b = [1000000000, 1]. Our algorithm: a1=1e9, M=1e9; a2=1+1e9=1000000001 > 1e9, which violates constraint a_i <=1e9. However, is there any alternative solution? Could we have chosen a different M_{i-1}? But M_{i-1} is forced by previous a's, which are forced because a1 must satisfy b1 = a1 - 0, so a1 = b1 = 1e9. Then M1=1e9. Then a2 must satisfy b2 = a2 - M1 => a2 = b2 + 1e9 = 1 + 1e9 = 1000000001, which is >1e9. But the problem says it's guaranteed that there exists a solution with a_i in [0,1e9]. So this b array would not be given. So it's fine.
Thus no issue.
Implementation:
python
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
b = list(map(int, data[1:1+n]))
max_sofar = 0
a = []
for val in b:
ai = val + max_sofar
a.append(ai)
if ai > max_sofar:
max_sofar = ai
print(' '.join(map(str, a)))
Edge: n = 3, b length 3, reading works.
Let's run mental check with examples.
Thus final answer.
But wait, the problem says n≥3, but that's irrelevant.
Hence solution.
But is there any possibility that the definition of x_i includes zero as a default? Yes, max(0, a1,...,a_{i-1}) includes zero, meaning x_i is at least 0. So it's covered.
Thus solution is done.
Now produce final output.
But the problem might be from an "array restoration" style, and some participants might think of constructing the maximum prefix, but it's straightforward.
Thus I'm ready to answer.
But maybe there's an alternative approach? No.
Thus final message: Provide code.
However, the user asked: "Alicia gives you the values b_1, b_2, …, b_n and asks you to restore the values a_1, a_2, …, a_n. Can you help her solve the problem?" So we should output the solution.
Thus final answer: Provide explanation and code.
But the question is likely from a coding competition, and they want the code. Since we are ChatGPT, we can provide a solution.
But the conversation says: "You are ChatGPT, large language model... Provide answers in the following format: final." So we need to produce final answer.
Thus I'll produce final answer: Explanation and code.
But per instructions, we should produce just the code? The typical answer on such platforms: provide solution description and code. Since it's not a specific judge format, I'll provide explanation then code.
But the user might be expecting code only. But they said "Print n integers", so they want the output. I can give code that solves.
I'll present the solution in Python.
Thus final output: The code.
But to be safe, I'll include comments.
Thus answer. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
n = int(data[0])
b = list(map(int, data[1:1 + n]))
max_sofar = 0
a = []
for val in b:
ai = val + max_sofar
a.append(ai)
if ai > max_sofar:
max_sofar = ai
sys.stdout.write(' '.join(map(str, a)))