Tree Permutations#11281

Tree Permutations

1 second
128 megabytes
Very hard

Once upon a time, Mr. Cool created a tree (an undirected graph without cycles) of vertices, by assigning to each vertex i > 1 two numbers: p_i < i - the direct ancestor of vertex and w_i - the weight of the edge between vertex and p_i. Vertex is the root, so it does not have any ancestors.

You wanted to know what tree did Mr. Cool build, but Mr. Cool refused to tell this, but he gave you a tip:

He wrote all these numbers in one line. That's how he got array of length 2 \cdot n - 2.

b = [p_2, w_2, p_3, w_3, \ldots, p_{n-1}, w_{n-1}, p_n, w_n]

Then he randomly shuffled it. That's how he got array a, and Mr. Cool presented you with it. Since it is impossible to restore the tree knowing only values of array a, you decided to solve a different problem.

Let's call a tree -long, if there are exactly edges on the path between vertex and .

Let's call a tree -perfect, if it is -long and the sum of the weights of the edges on the path between vertex and vertex is maximal among all possible -long trees that Mr. Cool could build.

Your task is to print the sum of the weights of the edges on the path between vertex and vertex for all possible -perfect trees or print if a certain -long tree could not be built by Mr. Cool.

Input

The first line contains one integer (2 \le n \le 10^5) - the number of the vertices in the tree.

The second line contains 2 \cdot n - 2 integers a_1, a_2, \ldots, a_{2n-2} (1 \le a_i \le n - 1) - the elements of array .

Output

In one line, print n - 1 integers w_1, w_2, w_3, \ldots, w_{n-1}, where w_k is the sum of the weights of the edges on the path between vertex and vertex in a -perfect tree. If there is no -long tree, then w_i should be equal to .

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Example 3

Loading
Loading

Note

In the first example, the -perfect tree is defined by array [1, 2, 1, 2] (i.e. p_2 = 1, w_2 = 2, p_3 = 1, w_3 = 2).

The -perfect tree is defined by array [1, 2, 2, 1] (i.e p_2 = 1, w_2 = 2, p_3 = 2, w_3 = 1). Here are illustrations of the -perfect tree and the -perfect tree respectively (path from vertex to vertex is drawn with bold lines):

prb11281.gif

In the second example, there are no -perfect trees, that can be obtained by permuting array .

In the third example, only -perfect tree and -perfect tree can be obtained. These are defined by arrays [1, 4, 2, 4, 3, 4, 4, 4, 4, 5] and [1, 4, 2, 4, 3, 4, 4, 4, 5, 4] respectively. Here are illustrations of them:

prb11281_1.gif