eolymp
bolt
Try our new interface for solving problems
Problems

Promotion Counting

Promotion Counting

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

The cows, conveniently numbered 1 .. n, organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow i has a distinct proficiency rating p(i), which describes how good she is at her job. If cow i is an ancestor (e.g., a manager of a manager of a manager) of cow j, then we say j is a subordinate of i.

Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow i in the company, please count the number of subordinates j where p(j) > p(i).

Input

The first line contains n (1n105). The next n lines contain the proficiency ratings p(1) .. p(n) for the cows. Each is a distinct integer in the range 1 .. 109.

The next n1 lines describe the manager (parent) for cows 2 .. n. Recall that cow 1 has no manager, being the president.

Output

Print n lines. The i-th line should tell the number of subordinates of cow i with higher proficiency than cow i.

Time limit 1 second
Memory limit 128 MiB
Input example #1
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Output example #1
2
0
1
0
0
Source 2017 USACO January, Platinum