Christmas Balls

Time limit: 0.5s Memory limit: 256MB Input: Output:

Christmas is coming, and Alessandro has to decorate his living room with a Christmas tree.

He heard that in Pordenone a new tree shop has just opened, so he decided to go there. This shop has a huge tree decorated with NN balls, one at each branching point (the root is node 00). Of course the balls are quite spectacular, and he noticed that the balls come in CC different colors in total.

The peculiarity of this shop is that you can buy the whole tree, or cut a branch and buy just a subtree! You can make the cut just below a ball, so that you take that ball and the subtree rooted there.

Alessandro doesn't want just a random tree, he wants the nicest. His taste is in fact quite peculiar, he'd like that the amount of balls of each color to be well distributed. Precisely, let's call xx the number of balls of (one of) the most frequent color in a subtree, he wants to maximize the number of colors with xx balls each.

Help Alessandro finding where to cut the tree by printing the maximum amount of most-frequent colors he can get.

Input data

The first line contains the only integer NN, the number of nodes of the tree.

The second line contains NN integers CiC_i, the colors of each ball.

The third line contains N1N-1 integers: PiP_i is the index of the parent of the ii-th node.

Output data

The output contains a single line with an integer: chosen the optimal subtree, the number of colors with the highest frequency.

Constraints and Clarifications

  • 1N1051 \leq N \leq 10^5;
  • 0Ci<N0 \leq C_i < N;
  • For tests worth 1313 points, the tree is a line.
  • For tests worth 1515 more points, 1N1 0001 \leq N \leq 1 \ 000, 1MAXC21 \leq MAXC \leq 2.
  • For tests worth 2121 more points, 1N1 0001 \leq N \leq 1 \ 000.
  • For tests worth 1717 more points, 1MAXC21 \leq MAXC \leq 2.

Example 1

stdin

8
1 2 0 2 0 0 1 1
0 0 1 1 3 4 4

stdout

3

Explanation

In the first sample case, there are 88 nodes in 33 different colors. The solution is to cut the tree and take the subtree rooted at node 11. This way we are left with 66 nodes: the most frequent color has 22 balls, and there are 33 colors with that many balls.

Example 2

stdin

5
0 1 1 0 0
0 1 2 3

stdout

2

Explanation

If, for example, we have kept the entire tree, the most frequent color appears with 33 balls, but there are only 22 colors with 33 balls, so this is worse than the aforementioned solution.

Log in or sign up to be able to send submissions!