Count Nodes Greater Than ParentEasy

Count Nodes Greater Than Parent

EasyPractice library · +10 XPtreesparent array

You are given a tree where nodes are numbered from 0 to N-1. The tree structure is described using a parent array P of size N. For each node i (from 1 to N-1), P[i] is the parent of node i. Node 0 is the root and has no parent (represented by P[0] = -1). You are also given an array V of size N, where V[i] is the value of node i. Your task is to count how many nodes in the tree have a value strictly greater than their parent's value. The root node (node 0) has no parent, so it cannot be counted.

Input

The first line contains an integer N. The second line contains N integers representing the parent array P, where P[0] is always -1. The third line contains N integers representing the value array V.

Output

Print a single integer: the count of nodes whose value is strictly greater than their parent's value.

Constraints

1 <= N <= 1000 -1 <= P[i] < i for 1 <= i < N P[0] = -1 0 <= V[i] <= 10^9

Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests

Example 1

Input
5
-1 0 0 1 1
10 5 15 7 12
Output
3

Node 2 (value 15) > parent 0 (value 10). Node 4 (value 12) > parent 1 (value 5). Count = 2.

Example 2

Input
3
-1 0 0
5 8 3
Output
1

Node 1 (value 8) > parent 0 (value 5). Node 2 (value 3) is not > parent 0 (value 5). Count = 1.

Hints