atcoder#ABC259F. [ABC259F] Select Edges

[ABC259F] Select Edges

Score : 500500 points

Problem Statement

You are given a tree with NN vertices. For each i=1,2,,N1i = 1, 2, \ldots, N-1, the ii-th edge connects Vertex uiu_i and Vertex viv_i and has a weight wiw_i.

Consider choosing some of the N1N-1 edges (possibly none or all). Here, for each i=1,2,,Ni = 1, 2, \ldots, N, one may choose at most did_i edges incident to Vertex ii. Find the maximum possible total weight of the chosen edges.

Constraints

  • 2N3×1052 \leq N \leq 3 \times 10^5
  • 1ui,viN1 \leq u_i, v_i \leq N
  • 109wi109-10^9 \leq w_i \leq 10^9
  • did_i is a non-negative integer not exceeding the degree of Vertex ii.
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

d1d_1 d2d_2 \ldots dNd_N

u1u_1 v1v_1 w1w_1

u2u_2 v2v_2 w2w_2

\vdots

uN1u_{N-1} vN1v_{N-1} wN1w_{N-1}

Output

Print the answer.

7
1 2 1 0 2 1 1
1 2 8
2 3 9
2 4 10
2 5 -3
5 6 8
5 7 3
28

If you choose the 11-st, 22-nd, 55-th, and 66-th edges, the total weight of those edges is 8+9+8+3=288 + 9 + 8 + 3 = 28. This is the maximum possible.

20
0 2 0 1 2 1 0 0 3 0 1 1 1 1 0 0 3 0 1 2
4 9 583
4 6 -431
5 9 325
17 6 131
17 2 -520
2 16 696
5 7 662
17 15 845
7 8 307
13 7 849
9 19 242
20 6 909
7 11 -775
17 18 557
14 20 95
18 10 646
4 3 -168
1 3 -917
11 12 30
2184