Lowest Common Ancestor

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Bobo has a rooted tree with nn nodes which are conveniently labeled with 1,2,,n1, 2, \dots, n. Node 11 is the root, and the ii-th node has weight w_iw\_i.

He would like to find out f(2),f(3),,f(n)f(2), f(3), \dots, f(n) where

f(i)=_j=1i1w_LCA(i,j).f(i) = \sum\_{j = 1}^{i - 1} w\_{\mathrm{LCA}(i, j)}\text{.}

입력

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains an integer nn (2n21052 \leq n \leq 2 \cdot 10^5).

The second line contains nn integers w_1,w_2,,w_nw\_1, w\_2, \dots, w\_n (1w_i1041 \leq w\_i \leq 10^4).

The third line contains (n1)(n - 1) integers p_2,p_3,,p_np\_2, p\_3, \dots, p\_n, where p_ip\_i denotes an edge from the p_ip\_i-th node to the ii-th node (1p_in1 \leq p\_i \leq n). The edges form a tree.

It is guaranteed that the sum of nn does not exceed 21052 \cdot 10^5.

출력

For each test case, output (n1)(n - 1) integers: f(2),f(3),,f(n)f(2), f(3), \dots, f(n).