1734 - LL && Wine Trade In WUST
Time Limit : 2 Second
Memory Limit : 64 MB
Submission: 0
Solved: 0
- Description
WUST is a city consists of houses numbered from 1 to, and every inhabitant of the city is a wine salesman. You wonder how this economy works? Simple enough: everyone buys wine from other inhabitants of the city. Every day each inhabitant decides how much wine he wants to buy or sell. Interestingly, demand and supply is always the same, so that each inhabitant gets what he wants. There is one problem, however: Transporting wine from one house to another results in work. Since all wines are equally good, the inhabitants of WUST don’t care which persons they are doing trade with, they are only interested in selling or buying a specific amount of wine.
They are clever enough to figure out a way of trading so that the overall amount of work needed for transports is minimized. In this problem you are asked to reconstruct the trading during one day in WUST. For simplicity we will assume that the houses make up a tree with equal distance between adjacent houses. Transporting one bottle of wine from one house to an adjacent house(there exists a road connecting them directly) results in one unit of work.
- Input
The input consists of several test cases. Each test case starts with the number of inhabitants n(1≤n≤100000). The following line contains n integers a_i (-10^8≤a_i≤10^8) . If a_i≥0 , it means that the inhabitant living in the i-th house wants to buy a_i bottles of wine, otherwise if a_i<0 , he wants to sell -a_i bottles of wine. You may assume that the numbers a_i sum up to 0. The following line contains n-1 integers, describing the road of WUST. the i-th integer f_(i+1) (1≤f_(i+1)≤10^5) — means there exits an road between the (i+1)-th house and the f_(i+1)-th house.
- Output
For each test case print the minimum amount of work units needed so that every inhabitant has his demand fulfilled.
- sample input
-
2 50 -50 1 3 50 -20 -30 1 1
- sample output
-
50 50
- hint
- source
- WUST