OMOI
문제
Organization of Meticulous Oil Inspectors (OMOI) is an association of oil inspectors of the city N. All oil inspectors in the organization are numbered successively from to in the order of joining the OMOI. There is a tree-like hierarchy among the OMOI members so that the oil inspector is the head of OMOI, and every other oil inspector has an immediate supervisor who joined OMOI prior to , that is, has a smaller number. An oil inspector is a supervisor of an oil inspector if is present in the chain of immediate supervisors starting at , that is, in the sequence , , and so on. In this case, the oil inspector is a subordinate of the oil inspector .
All members of OMOI are very meticulous, and it is not uncommon for them to argue on business questions. We assume that two oil injectors may argue only if none of them is a supervisor of the other. To resolve their dispute, arguing oil inspectors ask their closest common supervisor for advice, that is, an oil inspector who is their common supervisor with the largest number. Every oil inspector (expect for the head of OMOI) has a perfectionism level that is expressed with an integer . Intensity of an argument between two oil inspectors is the sum of their perfectionism levels. Finally, tension of a working day is the total intensity of all arguments during the day.
At the end of a working day an oil inspector thinks himself an effective manager if for each one of his subordinates he resolved at least one argument involving this subordinate during the day. Formally, that means that for each oil inspector who is a subordinate of there is an oil inspector such that and had an argument during the day, and was the closest common supervisor of and . In particular, every oil inspector who does not have any subordinates thinks himself an effective manager according to this definition.
You are employed at OMOI as a programmer, and know every oil inspector in the organization. At the end of today's workday every oil inspector in the organization told you in confidence that he thought himself an effective manager on this day. You are now interested what could be the smallest tension of today's workday if every oil inspector indeed was an effective manager today.
입력
The first line contains an integer (), the number of oil inspectors in OMOI. The second line contains integers (), where is the number of the immediate supervisor of the oil inspector . The third line contains integers (), where is the perfectionism level of the oil inspector .
It is guaranteed that it is possible that every oil inspector would think himself an effective manager at the end of a working day assuming the given hierarchy.
출력
Print the smallest possible tension of the today's workday.
힌트
Consider the first sample test. The desired value of tension can be achieved if during the day oil inspectors in pairs , , , and have arguments.
- Oil inspectors , , and automatically think themselves effective managers since they have no subordinates.
- The oil inspector thinks himself an effective manager since he helped the oil inspector in his argument with the oil inspector , and helped the oil inspector in his argument with the oil inspector .
- The oil inspector thinks himself an effective manager since he helped oil inspectors , , and in their arguments with the oil inspector , and helped the oil inspector with three different arguments.
Intensity of each argument is , thus tension of this particular day is .
The second sample test only satisfies constraints of the second and fourth test groups. An optimal solution can be achieved with arguments in pairs , , , and . Tension of the day in this scenario is . Note that this collection of pairs is not the only possible way to achieve the smallest value of tension.
예제
예제 1
5 1 2 2 1 1 1 1 1
8
예제 2
6 1 1 1 4 4 1 2 3 4 5
25