There is a vertical mine with N
chambers, where each chamber has exactly one vertical tunnel that goes into it (from some other chamber which is closer to the surface). Chamber number one is connected directly to the outside. Formally, the mine is a tree, rooted in vertex number one.
Every tunnel has some score associated with it (which depends on a lot of factors, but in this problem we’re given these scores directly). Unfortunately, sometimes going through a tunnel is risky, so these scores can also be negative.
Currently we have some number of miners in each chamber. We want to create a mining assignment, which should include some of the miners (possibly none), and assign to each of them a vertical path. A vertical path can only go deeper - that is if a miner is in some chamber, he can only go to chambers which are further away from the surface. More formally, from a vertex in the tree, we can only go to its children. We define the score of such a path, as the sum of the scores in the tunnels we passed through. Similarly, the score of the assignment is the sum of the scores of the paths in it (if no miner was assigned a path, we consider the score being 0
).
However, we also have some additional constraints! We don’t want to have “crowded” chambers, that is, for each chamber we have a constraint on the maximal number of miners that can end their path there. The miners who weren’t assigned a path will just leave the mine and won’t be counted towards these constraints.
We’re interested in the largest score of some mining assignment. Given the structure of the mine, the initial number of miners in each chamber, and the maximal number of miners that can end in each chamber, you should write a program which computes this value.
Input
From the first line of the standard input, your program should read a single integer N
- the number of chambers (we consider chamber 1
as being connected to the outside). The second line lines contains - the initial number of miners for each chamber. The third line lines contains - the maximal number of miners that can end in each chamber. Finally, the last N-1
lines contain a description of the tunnel: the i
th of these lines contains and , which means that there is a vertical tunnel from chamber number to chamber number with a score of .
Output
On a single line, your program should print the largest possible score of some mining assignment.
Constraints
- , for all
1 ≤ i ≤ N
- , for all
2 ≤ i ≤ N
- , for all
2 ≤ i ≤ N
Subtask 1 (6 points)
N ≤ 8
Subtask 2 (12 points)
N ≤ 100
Subtask 3 (14 points)
N ≤ 2000
Subtask 4 (18 points)
- The tree forms a line, that is, for each
2 ≤ u ≤ N
its parent . - for all
1 ≤ u ≤ N
Subtask 5 (4 points)
- The tree forms a line, that is, for each
2 ≤ u ≤ N
its parent .
Subtask 6 (20 points)
Subtask 7 (26 points)
Example
stdin
5
5 1 0 0 0
100 1 1 2 4
1 6
1 1
2 2
2 -1
stdout
32
Explanation
One possible solution is:
1 -> 2 -> 4
with a score of8
1 -> 2 -> 4
with a score of8
1 -> 2
with a score of6
1 -> 2 -> 5
with a score of5
1 -> 2 -> 5
with a score of5