Wells
Time limit2sMemory limit256 MB
On a tree where placing wells at a vertex counts for it and its direct neighbors, find the minimum number of wells so every village's demand is met.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Greedy, DFS
- Solved
- No attempts yet
Problem
Hyunsung wants to install wells in villages for African children who cannot drink as much water as they need.
Each village needs a different number of wells. Installing one well in village A counts as one well for village A and for every village connected to A directly by a road. You may install several wells in the same village.
For example, suppose village A is connected to B and to C, and villages A, B, and C need 5, 10, and 7 wells respectively. If you install 5 wells in village A, villages B and C also have 5 wells each counted toward their needs.
Hyunsung donates a lot, so he does not have much money. There are villages, village needs at least wells (), and there are roads between villages. Find the minimum total number of wells to install so that every village's requirement is met.
Input
The first line contains the number of villages () and the number of roads ().
The second line contains the minimum number of wells each village needs, ().
Each of the next lines describes a road with two village numbers and that the road connects. Villages are numbered from to , and there is exactly one path between any two villages.
Output
Print the minimum number of wells to install on the first line.