2079.Alyona and a tree

Time Limit: 1s Memory Limit: 256MB

Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v,u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v \neq u) if and only if u is in the subtree of v and dist(v,u) \le au.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v controls u.

Input Format(From the terminal/stdin)

The first line contains single integer n (1 \le n \le 2 \cdot 105).

The second line contains n integers a1,a2,...,an (1 \le ai \le 109)- the integers written in the vertices.

The next (n-1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 \le pi \le n, 1 \le wi \le 109)- the parent of the (i+1)-th vertex in the tree and the number written on the edge between pi and (i+1).

It is guaranteed that the given graph is a tree.

Output Format(To the terminal/stdout)

Print n integers- the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Sample Input 1

Copy
5
2 5 1 4 6
1 7
1 1
3 5
3 6
 \n
 · · · · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 1

Copy
1 0 1 0 0
 · · · · \n

Sample Input 2

Copy
5
9 7 8 6 5
1 1
2 1
3 1
4 1
 \n
 · · · · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 2

Copy
4 3 2 1 0
 · · · · \n

Hints

In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5).

Submit

请先 登录

© 2025 FAQs