#P6223. [CHCI 2009 Final Exam #1] PODJELA
[CHCI 2009 Final Exam #1] PODJELA
Description
There are farmers living in different villages. These villages form a tree. Initially, each farmer has dollars.
In one operation, a farmer may take any amount of money from their own money and give it to a farmer in an adjacent village.
For each farmer, a value is given. Find the minimum number of operations needed so that in the end every farmer has money the given value.
Input Format
The first line contains an integer , the number of farmers.
The second line contains an integer , the amount of money each farmer initially has.
The third line contains integers, where the -th number is .
The next lines each contain two integers, representing an edge of the tree.
Output Format
Output one line with one integer, the minimum number of operations required.
6
15
10 20 18 16 6 16
1 4
4 5
4 6
6 2
5 3
5
Hint
Constraints
For of the testdata, , , .
Notes
Translated from Croatian Highschool Competitions In Informatics 2009 Final Exam #1 T2 PODJELA。
Translated by ChatGPT 5
京公网安备 11011102002149号