Range Reconstruction

Bessie has an array a1​,…,aN​, where 1≤N≤300 and 0≤ai​≤109 for all i. She won't tell you a itself, but she will tell you the range of each subarray of a. That is, for each pair of indices i≤j, Bessie tells you ri,j​=maxa[i…j]−mina[i…j]. Given these values of r, please construct an array that could have been Bessie's original array. The values in your array should be in the range [−109,109].

For example, r1,3​=maxa[1…3]−mina[1…3]=3−1=2.

This example satisfies the constraints for subtask 1.

This example satisfies the constraints for subtask 2.

SCORING:

  • Test 5 satisfies r1,N​≤1.
  • Tests 6-8 satisfy ri,i+1​=1 for all 1≤i<N.
  • Tests 9-14 satisfy no additional constraints.

Problem credits: Danny Mittal

Source: USA Computing Olympiad.

Input Format

The first line contains N.

Another N lines follow. The ith of these lines contains the integers ri,i​,ri,i+1​,…,ri,N​.

It is guaranteed that there is some array a with values in the range [0,109] such that for all i≤j, ri,j​=maxa[i…j]−mina[i…j].

Output Format

Output one line containing N integers b1​,b2​,…,bN​ in the range [−109,109] representing your array. They must satisfy ri,j​=maxb[i…j]−minb[i…j] for all i≤j.

Sample Input 1

3 0 2 2 0 1 0

Sample Output 1

1 3 2

Sample Input 2

3 0 1 1 0 0 0

Sample Output 2

0 1 1

Sample Input 3

4 0 1 2 2 0 1 1 0 1 0

Sample Output 3

1 2 3 2

Sample Input 4

4 0 1 1 2 0 0 2 0 2 0

Sample Output 4

1 2 2 0

Submit