#P5290. [十二省联考 2019] 春节十二响

[十二省联考 2019] 春节十二响

Background

“During Qingming, the rain falls in torrents; on the road, travelers are heartbroken.”

In the year 20752075, 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 4.224.22 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 nn subprograms. The memory required by the ii-th subprogram is MiM_i. The call relationships among these nn subprograms form a tree rooted at subprogram 11, where the parent of subprogram ii in the call tree is subprogram fif_i.

Because memory is tight, the power-control chip provides a memory segmentation mechanism. You can divide memory into several segments S1S_1, S2S_2, ..., SkS_k, 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 aa and bb are not in an ancestor–descendant relationship in the call tree, aa and bb 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 nn representing the number of subprograms, where n2×105n \leqslant 2 \times 10^5.

The second line contains nn positive integers M1M_1, M2M_2, ..., MnM_n separated by spaces, where MiM_i denotes the memory required by the ii-th subprogram.

The third line contains n1n - 1 positive integers f2f_2, f3f_3, ..., fnf_n separated by spaces, satisfying fi<if_i < i, meaning that the parent of the ii-th subprogram in the call tree is subprogram fif_i.

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 11.

In the optimal plan, the memory is divided into three segments of sizes 1010, 2020, and 3030. Subprogram 11 is assigned to the first segment, subprograms 22 and 33 are assigned to the second segment, and subprograms 44 and 55 are assigned to the third segment. It can be proven that no better plan exists.

Subtasks

img

Note: In test points 1010, 1111, and 1212, subprogram 11 is not necessarily an endpoint of the chain.

Here, MM is the maximum memory requirement among all subprograms, i.e., max{Mi}\max\left\{M_i\right\}.

For all data, 1n2×1051 \leqslant n \leqslant2 \times 10^5, 1M1091 \leqslant M \leqslant 10^9.

After carefully reading the statement and analyzing the Constraints, Elephant begins to write a program to solve this problem.

\texttt{\$ login Elephant}

password: ********\texttt{password: ********}

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}

Spring Accepted. Score: 100/100.\texttt{Spring Accepted. Score: 100/100.}

Translated by ChatGPT 5