It is Christmas Eve, and Santa is delivering presents!
There are houses in the world which he has already delivered presents to. Each house has already been delivered presents. However, he noticed that he gave some houses more presents than others. He wants all the houses to end up with the same number of presents.
Thankfully, Santa has unlimited elves at his disposal. The houses are connected by two-way roads where each house is able to reach every other house through some series of roads. A single elf can carry a present from any house to a house adjacent to it (i.e. the elf can only carry the present across road). Presents may be carried multiple times, and multiple presents can be carried across a single path.
What is the minimum number of elves needed to distribute the presents evenly?
Constraints
For all test cases, , , the sum of all for is divisible by .
Subtask 1 [10%]
Subtask 2 [10%]
Subtask 3 [30%]
Subtask 4 [50%]
No additional constraints.
Input Specification
First line: (the number of houses).
Second line: space-separated integers describing for each (the initial number of presents in each home).
Next lines: Two space-separated integers and () indicating that a road connects houses and .
Output Specification
One integer: The minimum number of elves needed for every house to have the same number of presents.
Sample Input
4
2 1 1 4
1 2
1 3
1 4
Output for Sample Input
4
Explanation for Sample Case
Santa can get elves to carry presents
- From house to house ,
- From house to house ,
- From house to house ,
- From house to house .
Afterwards, each house has exactly presents. It can be proven that this is an optimal solution.
Comments