P25300 [AtCoder] Tree MST
NOI
题目描述

Problem Description

Ringo has a tree with $ N $ vertices. Among the $ N-1 $ edges in this tree, the $ i $-th edge connects vertex $ A_i $ and vertex $ B_i $ with a weight of $ C_i $. Additionally, vertex $ i $ has a weight of $ X_i $.

Here, $ f(u,v) $ is defined as the sum of "the distance from vertex $ u $ to vertex $ v $" and "$ X_u + X_v $".

Consider a complete graph $ G $ with $ N $ vertices. The cost of the edge connecting vertex $ u $ and vertex $ v $ is $ f(u,v) $. Find the minimum spanning tree of graph $ G $.

Input Format

The input is given from the standard input in the following format:

$ N $ $ X_1 $ $ X_2 $ $ ... $ $ X_N $ $ A_1 $ $ B_1 $ $ C_1 $ $ A_2 $ $ B_2 $ $ C_2 $ $ : $ $ A_{N-1} $ $ B_{N-1} $ $ C_{N-1} $

Output Format

Output the cost of the minimum spanning tree of graph $ G $.

Sample Input and Output

Sample Input #1

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

Sample Output #1

22

Sample Input #2

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

359

Sample Input #3

2
1000000000 1000000000
2 1 1000000000

Sample Output #3

3000000000

Notes/Hints

Constraints

  • $2 \leq N \leq 200,000$
  • $1 \leq X_i \leq 10^9$
  • $1 \leq A_i, B_i \leq N$
  • $1 \leq C_i \leq 10^9$
  • The given graph is a tree.
  • All inputs are integers.

Sample Explanation 1

Connecting vertices $ 1 $ and $ 2 $, vertices $ 1 $ and $ 4 $, and vertices $ 3 $ and $ 4 $ results in costs of $ 5, 8, 9 $ respectively, summing up to $ 22 $.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 1 3 5 1 1 2 1 2 3 2 3 4 3
样例输出
22