Restore the Expense Indentation

Time limit1sMemory limit1024 MB

Summary
Given pre-order amounts where each parent equals the sum of its immediate children, recover the lexicographically smallest 0-based indentation depth for every line.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Tree, Implementation
Solved
No attempts yet

Problem

Juku keeps track of his expenses in a text file with the following format. The hierarchy is not always exactly three levels deep.

March expenses - 1000
   Food - 500
      Curd snacks - 250
      Meat - 250
   Fun - 400
      Party - 200
      Cinema - 200
   Health - 100

While re-saving the file, his text editor somehow lost all of the indentation, and now the file looks like this.

March expenses - 1000
Food - 500
Curd snacks - 250
Meat - 250
Fun - 400
Party - 200
Cinema - 200
Health - 100

Given that the first line of the file is the total of all expenses, write a program that helps Juku restore the original hierarchy.

Formally, the amount on each line equals the sum of the amounts of the items nested exactly one level directly beneath it (its immediate children). The amounts are given in file order (a pre-order traversal of the hierarchy), and you must recover the indentation depth of each line.

Input

The first line contains the number of lines NN (1≤N≤201 \le N \le 20). Each of the next NN lines contains one amount AiA_i (1≤Ai≤1091 \le A_i \le 10^9), in file order.

Output

Print exactly NN lines. On line ii, print the indentation depth of the ii-th amount from the input (depth is 0-based). The first line's depth is always 00, and when N≥2N \ge 2 the second line's depth is always 11.

A depth assignment is valid when it forms a single hierarchy rooted at the first line and, for every line that is split into sub-items, the amounts of its immediate children sum exactly to that line's amount.

Because several valid indentations may exist, print the lexicographically smallest valid sequence of depths d1,d2,…,dNd_1, d_2, \dots, d_N (compare the sequences element by element and choose the one whose first differing position holds the smaller value).

Examples1

  1. Example 1

    Input
    6
    1000
    500
    250
    250
    500
    500
    
    Expected output
    0
    1
    2
    2
    1
    2