E - Closure Separating Three Lines Ahead Editorial /

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 つの施設について、使える通路を高々 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

4
8 2 9 7
1 2
2 3
3 4

出力例 1

2

入力例 2

5
5 8 3 9 4
1 2
1 3
1 4
1 5

出力例 2

0

入力例 3

7
5 3 4 8 8 8 8
1 2
1 3
2 4
2 5
3 6
3 7

出力例 3

3

入力例 4

10
9 1 7 8 4 2 6 3 5 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

出力例 4

6

入力例 5

1
0

出力例 5

0

Score : 433 pts

Problem Statement

There are N facilities, numbered 1 to N. There are N-1 bidirectional pathways between the facilities, and pathway i connects facility u_i and facility v_i. Between any two facilities, there is exactly one path that does not visit the same facility more than once.

You will close some facilities. The cost to close facility i is A_i. When a facility is closed, that facility and all pathways connected to it become unusable.

After the closures, for any two facilities that can reach each other by following pathways, you want it to be possible to travel between them by traversing at most 2 usable pathways. It is acceptable if no facilities remain unclosed.

Find the minimum total closure cost required to satisfy this condition.

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 facilities and pathways 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 facilities, N.
  • The second line contains the closure cost of each facility in order of their numbers.
  • The following N-1 lines contain the numbers of the facilities connected by each pathway. If N=1, these lines are absent.

Output

Print the minimum total closure cost as an integer.


Sample Input 1

4
8 2 9 7
1 2
2 3
3 4

Sample Output 1

2

Sample Input 2

5
5 8 3 9 4
1 2
1 3
1 4
1 5

Sample Output 2

0

Sample Input 3

7
5 3 4 8 8 8 8
1 2
1 3
2 4
2 5
3 6
3 7

Sample Output 3

3

Sample Input 4

10
9 1 7 8 4 2 6 3 5 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Sample Output 4

6

Sample Input 5

1
0

Sample Output 5

0