B. Choosing a Vertex To Remove
time limit per test
1 second
memory limit per test
128 mebibytes
input
standard input
output
standard output

You are given an undirected tree with $$$n$$$ vertices. Each vertex $$$v$$$ has a value $$$a_v$$$. The cost of any subtree $$$T$$$ is defined as: $$$$$$\displaystyle \mathrm{cost}(T) = \mathrm{size}(T) \cdot \sum_{v \in T} a_v\text{,}$$$$$$ where $$$\mathrm{size}(T)$$$ is the size of subtree $$$T$$$.

A vertex $$$u$$$ is removed from the original tree along with all its incident edges. Thus the graph transforms into multiple trees.

Your task is to find the maximum possible total cost of the resulting trees, given that you can choose the vertex $$$u$$$ yourself.

Input

The first line of input contains an integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$).

The second line of input contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-2 \cdot 10^5 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ corresponds to the value for vertex $$$i$$$.

The third line of input contains $$$n - 1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \le p_i \lt i$$$). They indicate that there is an edge in the tree between $$$2$$$ and $$$p_2$$$, an edge between $$$3$$$ and $$$p_3$$$, ..., an edge between $$$n$$$ and $$$p_n$$$.

It is guaranteed that the given edges form a tree.

Output

Output a single integer: the maximum possible total cost of the resulting trees if you can choose any vertex as the vertex $$$u$$$.

Examples
Input
2
1 2
1
Output
2
Input
3
-1 -1 2
1 1
Output
2
Input
4
1 -1 -1 -1
1 1 1
Output
-3
Input
4
1 -1 -1 -1
1 2 3
Output
-1