/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
N 個の村があり、1 から N までの番号が付いています。村の間には N-1 本の双方向の道があり、道 i は村 u_i と村 v_i を結びます。どの 2 つの村の間にも、同じ村を 2 回以上通らない道筋がちょうど 1 つあります。
いくつかの村に救護所を設けます。村 i に救護所を設ける費用は A_i です。各村には高々一つの救護所を設けられます。
救護所を設けない村には、その村と道で直接つながっている村のうち、少なくとも 2 つに救護所がなければなりません。救護所を設ける村には、この条件はありません。
この条件をすべての村で満たすために必要な費用の総和の最小値を求めてください。
制約
- 1\leq N\leq 200000
- 0\leq A_i\leq 10^9 (1\leq i\leq N)
- 1\leq u_i,v_i\leq N (1\leq i\leq N-1)
- 与えられる村と道は木をなす。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
- 1 行目には村の個数 N が与えられる。
- 2 行目には各村に救護所を設ける費用が番号順に与えられる。
- 続く N-1 行には各道が結ぶ村の番号が与えられる。N=1 のとき、これらの行はない。
出力
必要な費用の総和の最小値を整数で出力せよ。
入力例 1
3 2 9 3 1 2 2 3
出力例 1
5
入力例 2
2 4 7 1 2
出力例 2
11
入力例 3
5 100 1 100 1 100 1 2 2 3 3 4 4 5
出力例 3
202
入力例 4
9 8 2 9 1 6 3 4 7 5 1 2 1 3 2 4 2 5 3 6 3 7 6 8 6 9
出力例 4
31
入力例 5
1 0
出力例 5
0
Score : 433 pts
Problem Statement
There are N villages, numbered 1 through N. There are N-1 bidirectional roads between the villages, where road i connects village u_i and village v_i. Between any two villages, there is exactly one path that does not pass through the same village more than once.
Aid stations will be established in some villages. The cost to establish an aid station in village i is A_i. At most one aid station can be established in each village.
For any village where an aid station is not established, at least 2 of the villages directly connected to it by a road must have an aid station. There is no such requirement for villages where an aid station is established.
Find the minimum total cost required to satisfy this condition for all villages.
Constraints
- 1\leq N\leq 200000
- 0\leq A_i\leq 10^9 (1\leq i\leq N)
- 1\leq u_i,v_i\leq N (1\leq i\leq N-1)
- The given villages and roads form a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
- The first line contains the number of villages N.
- The second line contains the cost to establish an aid station in each village, in order of village index.
- The following N-1 lines contain the indices of the villages connected by each road. When N=1, these lines are omitted.
Output
Print the minimum total cost required as an integer.
Sample Input 1
3 2 9 3 1 2 2 3
Sample Output 1
5
Sample Input 2
2 4 7 1 2
Sample Output 2
11
Sample Input 3
5 100 1 100 1 100 1 2 2 3 3 4 4 5
Sample Output 3
202
Sample Input 4
9 8 2 9 1 6 3 4 7 5 1 2 1 3 2 4 2 5 3 6 3 7 6 8 6 9
Sample Output 4
31
Sample Input 5
1 0
Sample Output 5
0