#P5499. [2019省队联测]春节十二响
[2019省队联测]春节十二响
Background
"It rains heavily during the Qingming Festival, and people on the road feel like breaking their souls."
There is no spring rain on the Qingming Festival. Under the cloud of flying snow, walking through the ice field, only the snowmobile carrying the meager hope of mankind.
The journey of light years, for the lonely traveler - earth, I am afraid it is also extremely lonely.
Description
Sulawesi was only a hundred kilometers away, and the air inside was colder than outside. Four pairs of eyes were glued to the screen in front of Alefin, the key program that controls the planetary engine: twelve rings of spring. He needs to deploy it into a chip in the power control system.
"Spring Festival twelve rings" is composed of subroutines, and the memory space required by the first -th subroutine is . The call relationships between the subroutines form a tree rooted in the -st subroutine, where the subroutine's father on the call tree is the -th subroutine.
Due to the shortage of memory, a memory segmentation mechanism is provided on the power control chip. You can divide the memory into segments , and preassigns each program to a fixed segment. If two subroutines have no direct or indirect calling relationship, they can be assigned to the same segment, but not vice versa. In other words, and can be assigned to the same segment if and only if and are not ancestor-descendant relationships on the call tree.
The size of a segment should be the maximum memory size required by all subprograms allocated to this segment, and the sum of all segment sizes cannot exceed the memory size of the system.
Now Alefin wants to know how much memory the power control chip needs to have in order for it to work correctly. That is, what is the minimum amount of memory required to divide the memory into segments and then allocate each subprogram to a segment so that there is no ancestor-descendant relationship between all subprograms allocated in each segment.
Format
Input
The first line contains a positive integer for the number of subroutines.
The second line has positive integers separated by Spaces , indicates the memory space required by the -th subroutine.
The third line contains positive integers separated by Spaces , meets , indicating that the father of the -th subroutine in the call tree is the -th subroutine.
Output
Only one integer, representing the minimum memory requirements.
Samples
5
10 20 20 30 30
1 1 2 2
60
In the optimal scenario, the memory is divided into three segments of the size , , and , where the -st subroutine is allocated in the -st segment, the -nd and -rd subroutines are allocated in the -nd segment, and the -th and -th subroutines are allocated in the -rd segment. Arguably, there is no better solution.
10
2 1 1 1 1 2 1 1 1 2
1 1 1 4 5 3 3 4 3
6
15
10 1 10 10 2 6 9 6 8 6 3 4 4 5 5
1 2 2 1 5 4 4 1 2 10 1 9 6 1
31
Limitations
Testcase ID | Is a chain? | ||
---|---|---|---|
No | |||
Yes | |||
No | |||
Note: In the -th, -th, -th testcases, the -st subroutine is not necessarily an end point of the chain.
Where is the maximum of all child memory requirements, i.e. .
For all data, , .
After reading the problem carefully and analyzing the range of data, Alefin began writing a program to solve the problem.
Alefin, senior programmer. The third engineering group of Yuyang City reminds you:
-
There are a lot of rules solving problems, and reading the statement is the first;
-
If you don't program normatively, you will get no points and tears drop from your eyes.