#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 4.224.22 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 nn subroutines, and the memory space required by the first ii-th subroutine is MiM_i. The call relationships between the nn subroutines form a tree rooted in the 11-st subroutine, where the ii subroutine's father on the call tree is the fif_i-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 S1,S2,,SkS_1, S_2,\cdots , S_k, 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, aa and bb can be assigned to the same segment if and only if aa and bb 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 nn for the number of subroutines.

The second line has nn positive integers separated by Spaces M1,M2,,MnM_1,M_2,\cdots, M_n, MiM_i indicates the memory space required by the ii-th subroutine.

The third line contains n1n-1 positive integers separated by Spaces f2,f3,,fnf_2, f_3,\cdots, f_n, meets fi<if_i < i, indicating that the father of the ii-th subroutine in the call tree is the fif_i-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 1010, 2020, and 3030, where the 11-st subroutine is allocated in the 11-st segment, the 22-nd and 33-rd subroutines are allocated in the 22-nd segment, and the 44-th and 55-th subroutines are allocated in the 33-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 nn MM Is a chain?
1,21,2 5\leq 5 10\leq 10 No
3,43,4 10\leq 10 2\leq 2
5,6,7,8,95,6,7,8,9 16\leq 16 109\leq 10^9
10,11,1210,11,12 2×105\leq 2\times 10^5 Yes
13,14,1513,14,15 2000\leq 2000 No
16,17,18,19,2016,17,18,19,20 2×105\leq 2\times 10^5

Note: In the 1010-th, 1111-th, 1212-th testcases, the 11-st subroutine is not necessarily an end point of the chain.

Where MM is the maximum of all child memory requirements, i.e. max{Mi}\max\left\{M_i\right\}.

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

After reading the problem carefully and analyzing the range of data, Alefin began writing a program to solve the problem.

$ login Elephant\texttt{\$ login Elephant}

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

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.

$ cd spring\texttt{\$ cd spring}

$ ac spring\texttt{\$ ac spring}

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