#P6748. 『MdOI R3』Fallen Lord
『MdOI R3』Fallen Lord
Background
Ruling the world, ruling loneliness.
Problem Description
Country L has cities, and there are roads between them, forming a tree structure.
King L sends some troops to guard these roads. The combat power of the troops guarding each road can be measured as an integer in .
Each city has a city lord. The -th city lord has a tolerance value . If the median of the combat powers of the troops stationed on all roads connected to the -th city exceeds the city lord's tolerance, then the city lord will think the king does not trust him and will want to rebel.
Of course, King L does not want anyone to rebel, but he also wants to make the total combat power of the troops guarding the roads as large as possible to ensure national defense. Now he has found the strongest OIer in Country L — you — to help him solve this problem.
If no matter how the troops are arranged there will be someone who wants to rebel, output -1.
Note: For any numbers, their median is the -th number after sorting these numbers in nondecreasing order.
Input Format
The first line contains two positive integers, representing .
The second line contains integers. The -th number represents .
The next lines each contain two numbers , representing a road directly connecting city and city .
The input is guaranteed to form a tree.
Output Format
One line with one integer, representing the maximum total combat power.
7 100
50 25 25 12 12 12 12
1 2
1 3
2 4
2 5
3 6
3 7
148
Hint
For more samples, please get them here.
For all testdata, , , .
| Subtask ID | Other properties | Score | ||
|---|---|---|---|---|
| 1 | None | |||
| 2 | ||||
| 3 | The tree is a chain | |||
| 4 | There exists a node with degree | |||
| 5 | Each node has degree | |||
| 6 | None | |||
| 7 | ||||
Blank entries mean they are the same as the Constraints.
Sample Explanation

As shown in the figure, the combat powers of the troops guarding the roads (in the input order) are , respectively.
Translated by ChatGPT 5