Sim-4-15 | Echidistant

This was the problem page during the contest. Access the current page here.
Time limit: 2s Memory limit: 512MB Input: echidistant.in Output: echidistant.out

Iaroslav-Menelaos Trapanache, known as the Thunderbolt of the Lower Danube, or (to his close friends) the Pharaoh, wants to become wiser, and feels like solving a computer science problem about trees that are as equidistant and correct as he is, so he composed the following problem.

Equidistant tree. A rooted tree AA is called equidistant if all its leaves are at the same distance from the root. (Recall that the distance between two nodes is given by the number of edges of the unique elementary path between the two nodes.)

For example, the following tree rooted at node 11 is equidistant:

, whereas the following one (also rooted at node 11) is not equidistant:

, because the distances from node 11 to nodes 44, 55, 66 are not all equal.

Value of a tree. Consider a tree AA with nodes numbered 1,,N1, \ldots, N, where each node i=1,,Ni = 1, \ldots, N has a weight w(i)w(i) (which can also be negative). The value of tree AA, denoted val(A)\text{val}(A), is defined as follows. Consider any way of successively removing leaves of tree AA such that the tree becomes or remains equidistant as a result, while keeping the root node. (We may remove a node that becomes a leaf as a result of removing other nodes.) Let x1,,xkx_1, \ldots, x_k be the nodes remaining after this removal process. The value val(A)\text{val}(A) of AA is the maximum possible sum w(x1)++w(xk)w(x_1) + \ldots + w(x_k) over all ways of removing leaves from AA.

For example, let's consider the second tree above (the one with 66 nodes). Some of the sets of nodes that can remain after removals are {1,2,3,5,6}\{1, 2, 3, 5, 6\} (we remove node 44), {1,2,3,4}\{1, 2, 3, 4\} (we remove nodes 55, 66), {1,2}\{1, 2\} (we remove nodes 44, 55, 66, 33 in this order), and {1}\{1\} (we remove nodes 44, 55, 66, 33, 22, in this order). If we assign this tree the weights w(1),,w(6)w(1), \ldots, w(6), then its value is: max(w(1)+w(2)+w(3)+w(5)+w(6), w(1)+w(2)+w(3)+w(4), w(1)+w(2), w(1))\max(w(1) + w(2) + w(3) + w(5) + w(6),\ w(1) + w(2) + w(3) + w(4),\ w(1) + w(2),\ w(1)).

Requirement

We are given a tree AA with NN nodes rooted at node 11, and with weights w(1),,w(N)w(1), \ldots, w(N). Let AiA_i be the subtree of AA rooted at node ii. Compute val(A1),,val(AN)\text{val}(A_1), \ldots, \text{val}(A_N).

Input Data

On the first line of the input file echidistant.in is the number NN of nodes of tree AA. On the second line are the numbers w(1),,w(N)w(1), \ldots, w(N) in order, separated by spaces. On the third line are N1N - 1 numbers t(2),,t(N)t(2), \ldots, t(N), representing the fact that the edges of the tree are 2t(2)2 - t(2), 3t(3)3 - t(3), \ldots, Nt(N)N - t(N).

Output Data

The single line of the output file echidistant.out contains NN numbers, namely val(A1),,val(AN)\text{val}(A_1), \ldots, \text{val}(A_N) separated by spaces, in this order.

Constraints and Notes

  • 1N1 000 0001 \leq N \leq 1 \ 000 \ 000
  • 1 000 000 000w(i)1 000 000 000−1 \ 000 \ 000 \ 000 \leq w(i) \leq 1 \ 000 \ 000 \ 000 for 1iN1 \leq i \leq N
  • 1t(i)N1 \leq t(i) \leq N for 1iN1 \leq i \leq N
# Score Constraints
1 9 t(i)=i1t(i) = i − 1 for 2iN2 \leq i \leq N
2 12 1N1001 \leq N \leq 100
3 21 1N5 0001 \leq N \leq 5 \ 000
4 44 1N100 0001 \leq N \leq 100 \ 000
5 14 No additional restrictions

Example

echidistant.in

6
0 -10 10 1 -1 -1
1 2 2 3 3

echidistant.out

1 1 10 1 -1 -1

Explanation

This is the second tree from the statement, the one with 66 nodes, where we assign the weights [0,10,10,1,1,1][0, -10, 10, 1, -1, -1] to nodes [1,2,3,4,5,6][1, 2, 3, 4, 5, 6].

Log in or sign up to be able to send submissions!