Dario's company is facing a particularly hard task. To tackle it, Dario plans to select a task force of people in the best way possible. The choice will be influenced by the company's highly hierarchical tree-like structure.
There are employees, numbered from to . For each , employee has a , where . Dario, who is employee number , has no direct manager. We say that employee is a of employee if either or is the manager of a supervisor of . Note that this is a recursive definition. The of a pair of employees and is their lowest common ancestor in the company's hierarchy tree, i.e. the employee with the highest identifying number among those that supervise both and . This is well defined because Dario supervises everyone in the company.
Task
Moreover, each employee has a skill level . The of a subset of employees is defined as the sum of the skill level of the coordinator of and over all pairs .
What is the maximum cumulative skill level that a task force of people can obtain?
Input data
The input file consists of:
- a line containing integers .
- a line containing the integers .
- a line containing the integers .
Output data
The output file must contain a single line consisting of 64-bit integer .
Constraints and clarifications
- .
- .
- for each .
- for each .
| # | Score | Constraints |
|---|---|---|
| 1 | 0 | Examples |
| 2 | 18 | , for |
| 3 | 27 | |
| 4 | 24 | for each |
| 5 | 31 | No additional restrictions |
Example 1
stdin
5 3
4 2 5 1 3
0 0 1 1
stdout
12
Explanation
In the first sample case, the maximum cumulative skill level obtainable with a task force of people is .
One possible way to achieve that is to choose employees , and . By doing this:
- Employee and 's coordinator is employee , with a skill level of .
- Employee and 's coordinator is employee , with a skill level of .
- Employee and 's coordinator is employee , with a skill level of .
Example 2
stdin
10 5
5 9 4 4 8 9 9 8 7 3
0 0 1 3 4 0 4 0 7
stdout
84