#CF17FINALJ. Tree MST

Tree MST

Score : 19001900 points

Problem Statement

Ringo has a tree with NN vertices. The ii-th of the N1N-1 edges in this tree connects Vertex AiA_i and Vertex BiB_i and has a weight of CiC_i. Additionally, Vertex ii has a weight of XiX_i.

Here, we define f(u,v)f(u,v) as the distance between Vertex uu and Vertex vv, plus Xu+XvX_u + X_v.

We will consider a complete graph GG with NN vertices. The cost of its edge that connects Vertex uu and Vertex vv is f(u,v)f(u,v). Find the minimum spanning tree of GG.

Constraints

  • 2N200,0002 \leq N \leq 200,000
  • 1Xi1091 \leq X_i \leq 10^9
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9
  • The given graph is a tree.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

NN

X1X_1 X2X_2 ...... XNX_N

A1A_1 B1B_1 C1C_1

A2A_2 B2B_2 C2C_2

::

AN1A_{N-1} BN1B_{N-1} CN1C_{N-1}

Output

Print the cost of the minimum spanning tree of GG.

4
1 3 5 1
1 2 1
2 3 2
3 4 3
22

We connect the following pairs: Vertex 11 and 22, Vertex 11 and 44, Vertex 33 and 44. The costs are 55, 88 and 99, respectively, for a total of 2222.

6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15
359
2
1000000000 1000000000
2 1 1000000000
3000000000