#P7162. [COCI 2020/2021 #2] Sjekira

[COCI 2020/2021 #2] Sjekira

Description

There is a tree with nn nodes, and each node has a weight. The cost of deleting an edge is the sum of the maximum node weight in each of the two subtrees formed by that edge. Find the minimum total cost to delete all edges of the tree in any order.

Input Format

The first line contains an integer nn, the number of nodes.
The second line contains nn integers t1,t2,,tnt_1, t_2, \ldots , t_n, where the ii-th number is the weight of node ii.
The next n1n-1 lines each contain two integers x,yx, y, meaning there is an edge between xx and yy.

Output Format

Output one number, the minimum total cost.

3
1 2 3
1 2
2 3
8
4
2 2 3 2
1 3
3 2
4 3
15
5
5 2 3 1 4
2 1
3 1
2 4
2 5
26

Hint

Sample Explanation #1

Delete (2,3)(2,3) first, then delete (1,2)(1,2). The cost is 5+3=85+3=8.

Constraints

For 100%100\% of the testdata, 1n100,0001 \leq n \leq 100,000 and 1ti1091 \leq t_i \leq 10^9.

Subtask #1 (1010 pts): n10n \leq 10.
Subtask #2 (1010 pts): There is an edge directly connecting ii and i1i-1.
Subtask #3 (3030 pts): n1000n \leq 1000.
Subtask #4 (5050 pts): No additional constraints.

Notes

Translated from Croatian Open Competition in Informatics 2020 ~ 2021 Round 2 D Sjekira

Translated by ChatGPT 5