传统题 1000ms 256MiB

Frahc Finds Forgotten Spell

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Description

As a great wizard, Frahc will one day find the ultimate forgotten great spell. At present, Frahc has been informed that the spell lies in a rooted binary tree, where the node with index 11 is the root.

There is a spell written on each node. The spell on node ii is wiw_i. Obviously, since these spells have been written, they must not be the ultimate forgotten great spell. However, Frahc has seen through everything: the forgotten spell — just like its epithet — is the mex\mathrm{mex} of the spells. The mex\mathrm{mex} of a set of non-negative integers is defined as the minimal non-negative integer that is not in the set. Specifically, Frahc can explore one path in the tree, if so, he will learn the forgotten spell of the path.

Frahc believes the smaller the forgotten spell is, the more primitive the spell is, then the more likely the spell is the ultimate forgotten great spell. Also, Frahc likes longer paths. So he defines the priority of a path is the length of the path minus the forgotten spell of the path.

Frahc wants to find the maximum priority among all the paths, for each subtree. Since Frahc is a great wizard, not a great programmer, he leaves the problem to you.

Format

Input

The first line contains one integer n(1n3×105)n\,(1\leq n\leq 3\times10^5), the size of the binary tree.

The second line contains nn numbers w1,w2,,wn(0wi<n)w_1,w_2,\ldots,w_n\,(0\leq w_i<n).

The third line contains n1n-1 numbers fa2,fa3,,fan(1fai<i)fa_2,fa_3,\ldots,fa_n(1\leq fa_i<i), where faifa_i is the parent node of node ii.

Output

One line with nn numbers. The ii-th number is the maximum path weight in node ii's subtree.

Samples

7
2 1 0 0 1 1 0
1 1 2 3 2 6
3 2 1 0 1 1 0

Hello ACM 2025

未参加
状态
已结束
规则
ACM/ICPC
题目
11
开始于
2025-8-14 13:00
结束于
2025-8-14 18:00
持续时间
5 小时
主持人
参赛人数
46