miners

Time limit: 1s Memory limit: 256MB Input: Output:

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 s1,...,sNs_1, ..., s_N - the initial number of miners for each chamber. The third line lines contains e1,...,eNe_1, ..., e_N - the maximal number of miners that can end in each chamber. Finally, the last N-1 lines contain a description of the tunnel: the ith of these lines contains pi+1p_{i+1} and wi+1w_{i+1}, which means that there is a vertical tunnel from chamber number pi+1p_{i+1} to chamber number i+1i+1 with a score of wi+1w_{i+1}.

Output

On a single line, your program should print the largest possible score of some mining assignment.

Constraints

  • 2N5105 2 ≤ N ≤ 5 \cdot 10^5
  • 0si,ei20000 ≤ s_i, e_i ≤ 2000, for all 1 ≤ i ≤ N
  • 1pi<i1 ≤ p_i < i, for all 2 ≤ i ≤ N
  • wi2000|w_i| ≤ 2000, 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)

  • N105N ≤ 10^5
  • The tree forms a line, that is, for each 2 ≤ u ≤ N its parent pu=u1p_u = u-1.
  • su=eu=1s_u = e_u = 1 for all 1 ≤ u ≤ N

Subtask 5 (4 points)

  • N105N ≤ 10^5
  • The tree forms a line, that is, for each 2 ≤ u ≤ N its parent pu=u1p_u = u-1.

Subtask 6 (20 points)

  • N105N ≤ 10^5

Subtask 7 (26 points)

  • N5105N ≤ 5 \cdot 10^5

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. 1 -> 2 -> 4 with a score of 8
  2. 1 -> 2 -> 4 with a score of 8
  3. 1 -> 2 with a score of 6
  4. 1 -> 2 -> 5 with a score of 5
  5. 1 -> 2 -> 5 with a score of 5

Log in or sign up to be able to send submissions!