#P7162. [COCI 2020/2021 #2] Sjekira
[COCI 2020/2021 #2] Sjekira
Description
There is a tree with 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 , the number of nodes.
The second line contains integers , where the -th number is the weight of node .
The next lines each contain two integers , meaning there is an edge between and .
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 first, then delete . The cost is .
Constraints
For of the testdata, and .
Subtask #1 ( pts): .
Subtask #2 ( pts): There is an edge directly connecting and .
Subtask #3 ( pts): .
Subtask #4 ( pts): No additional constraints.
Notes
Translated from Croatian Open Competition in Informatics 2020 ~ 2021 Round 2 D Sjekira。
Translated by ChatGPT 5
京公网安备 11011102002149号