Tree MST

View as PDF

Submit solution

Points: 1.70
Time limit: 5.0s
Memory limit: 256M

Author:
Problem type
Problem Statement

Ringo has a tree with N vertices. The i -th of the N1 edges in this tree connects Vertex Ai and Vertex Bi and has a weight of Ci . Additionally, Vertex i has a weight of Xi .

Here, we define f(u,v) as the distance between Vertex u and Vertex v , plus Xu+Xv .

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

Constraints
  • 2N200,000
  • 1Xi109
  • 1Ai,BiN
  • 1Ci109
  • The given graph is a tree.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N

X1 X2 ... XN

A1 B1 C1

A2 B2 C2

:

AN1 BN1 CN1

Output

Print the cost of the minimum spanning tree of G .


Sample Input 1
Copy
4
1 3 5 1
1 2 1
2 3 2
3 4 3
Sample Output 1
Copy
22

We connect the following pairs: Vertex 1 and 2 , Vertex 1 and 4 , Vertex 3 and 4 . The costs are 5 , 8 and 9 , respectively, for a total of 22 .


Sample Input 2
Copy
6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15
Sample Output 2
Copy
359

Sample Input 3
Copy
2
1000000000 1000000000
2 1 1000000000
Sample Output 3
Copy
3000000000

Comments

Please read the guidelines before commenting.


There are no comments at the moment.