luogu#P9947. [USACO20JAN] Photoshoot B

[USACO20JAN] Photoshoot B

Problem Description

Farmer John is lining up NN cows numbered 1…N1\ldots N to take a photo (2≤N≤1032\le N\le 10^3). FJ originally planned that the cow with number aia_i would stand in the ii-th position from left to right, so he wrote down the permutation a1,a2,…,aNa_1,a_2,\ldots,a_N on a sheet of paper. Unfortunately, this sheet of paper has just been stolen by Farmer Nhoj!

Luckily, FJ still has a chance to recover the permutation he wrote earlier. Before the paper was stolen, Bessie recorded a sequence b1,b2,…,bN−1b_1,b_2,\ldots,b_{N-1}, where for each 1≤i<N1\le i<N, we have bi=ai+ai+1b_i=a_i+a_{i+1}.

Based on Bessie's information, help FJ recover the lexicographically smallest permutation aa that can produce the sequence bb. A permutation xx is lexicographically smaller than a permutation yy if there exists some jj such that for all i<ji<j, xi=yix_i=y_i, and xj<yjx_j<y_j (in other words, the two permutations are the same up to some position, and at that position xx is smaller than yy). It is guaranteed that at least one valid aa exists.

Input Format

The first line contains an integer NN.

The second line contains N−1N-1 space-separated integers b1,b2,…,bN−1b_1,b_2,\ldots,b_{N-1}.

Output Format

Output one line containing NN space-separated integers a1,a2,…,aNa_1,a_2,\ldots,a_N.

5
4 6 7 6
3 1 5 2 4

Hint

Sample Explanation 1

The sequence aa can produce bb because 3+1=43+1=4, 1+5=61+5=6, 5+2=75+2=7, 2+4=62+4=6.

Test Point Properties

  • Test points 2−42-4 satisfy N≤8N\le 8.
  • Test points 5−105-10 have no additional constraints.

Translated by ChatGPT 5