PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000025. Alyona và một cây (Alyona and a Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Alyona và một cây (Alyona and a Tree)

Alyona and a Tree

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Alyona has a tree of nn vertices rooted at vertex 11. Each vertex uu has a nonnegative integer aua_u. Every parent-child edge has a positive length.

For each vertex uu, count vertices vv that are strict descendants of uu (veuv e u and the root-to-vv path passes through uu) such that the weighted distance from uu to vv is at most ava_v.

The limit is the descendant vertex's value ava_v, not aua_u.

Input

  • The first line contains nn.
  • The second line contains nn values a1,…,ana_1,\ldots,a_n.
  • For each i=2,…,ni=2,\ldots,n, one line contains pi,wip_i,w_i, the parent of ii and the length of its parent edge.

Output

Print nn integers; value uu counts strict descendants vv satisfying the distance limit.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; 1≤n≤2⋅1051\le n\le2\cdot10^5; 0≤ai≤1090\le a_i\le10^9; 1≤pi≤n1\le p_i\le n (các cạnh tạo thành cây gốc 11); 1≤wi≤1091\le w_i\le10^9.
  • Subtask 2 (30 points): n≤1000n\le1000; 1≤n≤2⋅1051\le n\le2\cdot10^5; 0≤ai≤1090\le a_i\le10^9; 1≤pi≤n1\le p_i\le n (các cạnh tạo thành cây gốc 11); 1≤wi≤1091\le w_i\le10^9.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; 0≤ai≤1090\le a_i\le10^9; 1≤pi≤n1\le p_i\le n (các cạnh tạo thành cây gốc 11); 1≤wi≤1091\le w_i\le10^9.

Examples

Input

5
2 5 1 4 6
1 7
1 1
3 5
3 6

Output

1 0 1 0 0

Explanation

Vertex 11 has exactly one strict descendant meeting that descendant’s own distance limit. Vertex 33 also has one such descendant; the other vertices have none.