You have been mailed a disassembled Christmas tree. In the box, there were three things: a set of
Each branch can be attached to one other branch. The attachment relationship between two branches can be described as follows: if you attach a branch
The safety instructions state:
- Every branch except the lightest branch should be attached to another branch.
- Start by placing the lightest branch at the top of your tree.
- Add all the remaining branches in a predetermined order, one at a time.
- When adding a branch with a weight of
, find the heaviest branch already added to the assembled tree which is strictly lighter than . Attach the weight branch to the one found. If the found branch already has children (attached branches), place it to the right side of all of its children.
Before you start, you realize that you must add the branches in a specific order so that the resulting tree will match the diagram's tree.
Input Specification
On the first line, read one integer
The following
It is guaranteed that the tree will be rooted at
Please note that for the second section of input, the ordering of children matters. Values of
If you are still confused, please consult the sample input.
Output Specification
Print a permutation of the numbers in the range
If there are multiple possible answers, output any one of them (only one).
If there is no answer, output a single integer
Again, consult the samples for more clarification.
Sample Input 1
3
2 2 3
0
0
Sample Output 1
1 3 2
Explanation for Sample 1
The diagram's tree looks like this:
By starting with the branch with weight
Now add the
You can see that the two trees are structurally equivalent. Remember, numbers don't matter.
In this case, the given answer is the only valid answer. For example, adding the branches in the order
Sample Input 2
7
1 2
2 3 4
1 6
1 7
0
1 5
0
Sample Output 2
1 2 5 6 3 7 4
Explanation for Sample 2
The tree looks like this:
As you can see, it probably works.
Comments
Hi, is the second output right? Mustn't it be 1 2 5 6 7 3 4?
If you believe there are multiple possible answers, print any of them.
thanks for answering, the output2 is the answer of the assembling shown below?
Yes, sample output 2 is correct. It produces the desired tree, as shown.