#P11332. Recover the Delivery Sequence
Recover the Delivery Sequence
Recover the Delivery Sequence
Charles is a deliveryman who, on his first day, delivered N packages. Each package has a label number in the range \(1\) to \(N\) (labels may not be unique). At the end of the day, he recorded a sequence \(A = [A_1, A_2, \ldots, A_N]\) where \(A_i\) represents the label of the \(i\)th delivered package.
To save storage space, Charles stored a difference sequence \(D = [D_1, D_2, \ldots, D_{N-1}]\) where each difference is defined as:
\(D_i = A_{i+1} - A_i \quad (1 \le i \le N-1)\)
Your task is to help him recover the original sequence \(A\) given \(D\), under the condition that every \(A_i\) must satisfy:
\(1 \le A_i \le N\)
If there is a unique sequence \(A\) that meets the requirements, output it; otherwise, output -1
.
Hint: Let \(P_i = \sum_{j=1}^{i-1} D_j\) with \(P_1 = 0\). Then \(A_i = x + P_i\) for some integer \(x\). Find the valid range for \(x\) such that every \(A_i\) lies within \([1,N]\). The answer is unique only if there is exactly one valid \(x\).
inputFormat
The first line contains an integer \(N\) representing the number of packages. The second line contains \(N-1\) space-separated integers where the \(i\)th integer is \(D_i\).
outputFormat
If a unique valid sequence \(A\) exists, output its elements separated by spaces. Otherwise, output -1
.
sample
5
2 -1 2 -2
-1