#P5290. [十二省联考 2019] 春节十二响
[十二省联考 2019] 春节十二响
Background
“During Qingming, the rain falls in torrents; on the road, travelers are heartbroken.”
In the year , there was no spring rain during Qingming. Under the cover of snow filling the sky, the only thing moving through the icy wasteland was a snow vehicle carrying humanity’s faint hope.
A journey of light-years ahead, for Earth, this lonely traveler, is probably extremely lonely too.
Problem Description
Only one hundred kilometers remain to Sulawesi, and the air inside the vehicle is even colder than outside the window. Four pairs of eyes stare at the screen in front of Elephant, which shows the key program that controls the planetary engine: Twelve Firecrackers for the Spring Festival. He needs to deploy it onto a chip in the power control system.
“Twelve Firecrackers for the Spring Festival” consists of subprograms. The memory required by the -th subprogram is . The call relationships among these subprograms form a tree rooted at subprogram , where the parent of subprogram in the call tree is subprogram .
Because memory is tight, the power-control chip provides a memory segmentation mechanism. You can divide memory into several segments , , ..., , and pre-assign each program to a fixed segment. If two subprograms have neither a direct nor an indirect call relationship, then they can be assigned to the same segment; otherwise, they cannot. In other words, if and only if and are not in an ancestor–descendant relationship in the call tree, and may be assigned to the same segment.
The size of a segment should be the maximum memory requirement among all subprograms assigned to that segment. The sum of the sizes of all segments must not exceed the total memory size of the system.
Now Elephant wants to know the minimum memory the power-control chip must have to guarantee the correct running of Twelve Firecrackers for the Spring Festival. That is: what is the minimum memory needed such that, by first dividing memory into several segments, and then assigning each subprogram to one segment so that no two subprograms within the same segment have an ancestor–descendant relationship, the program can run correctly.
Input Format
The first line contains a positive integer representing the number of subprograms, where .
The second line contains positive integers , , ..., separated by spaces, where denotes the memory required by the -th subprogram.
The third line contains positive integers , , ..., separated by spaces, satisfying , meaning that the parent of the -th subprogram in the call tree is subprogram .
Output Format
Only one integer, representing the minimum required memory.
5
10 20 20 30 30
1 1 2 2
60
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
Hint
Explanation of Sample .
In the optimal plan, the memory is divided into three segments of sizes , , and . Subprogram is assigned to the first segment, subprograms and are assigned to the second segment, and subprograms and are assigned to the third segment. It can be proven that no better plan exists.
Subtasks

Note: In test points , , and , subprogram is not necessarily an endpoint of the chain.
Here, is the maximum memory requirement among all subprograms, i.e., .
For all data, , .
After carefully reading the statement and analyzing the Constraints, Elephant begins to write a program to solve this problem.
\texttt{\$ login Elephant}
Elephant, senior programmer. The third engineering team of Yushi City reminds you:
-
There are a thousand rules for solving problems, but reading the statement is the first rule;
-
Non-standard coding leads to zero points and tears.
\texttt{\$ cd spring}
\texttt{\$ ac spring}
Translated by ChatGPT 5