Problem:
Score : 1700 points

Problem Statement

Snuke has a rooted tree with N vertices, numbered 1 through N.
Vertex 1 is the root of the tree, and the parent of Vertex i ( 2\leq i \leq N ) is Vertex P_i ( P_i < i ).
There is a number, 0 or 1, written on each vertex. The number written on Vertex i is V_i.

Snuke would like to arrange the vertices of this tree in a horizontal row.
Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex.

After arranging the vertices, let X be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement.
Snuke would like to minimize the inversion number of X.
Find the minimum possible inversion number of X.

Notes

The inversion number of a sequence Z whose length N is the number of pairs of integers i and j ( 1 \leq i < j \leq N ) such that Z_i > Z_j.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq P_i < i ( 2 \leq i \leq N )

0 \leq V_i \leq 1 ( 1 \leq i \leq N )

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_2 P_3 ... P_N
V_1 V_2 ... V_N

Output

Print the minimum possible inversion number of X.

Sample Input 1

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

Sample Output 1

4

When the vertices are arranged in the order 1, 3, 5, 6, 2, 4, X will be (0, 1, 0, 0, 1, 0), whose inversion number is 4.
It is impossible to have fewer inversions, so the answer is 4.

Sample Input 2

1

0

Sample Output 2

0

X = (0), whose inversion number is 0.

Sample Input 3

15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0

Sample Output 3

31

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200005;
vector<int> adjlist[N];
struct ii{
    int x,y;
    ii (){}
    ii (int _x, int _y): x(_x),y(_y){
    }
    bool operator<(const ii &b)const{
        return ((ll)x*b.y < (ll)b.x*y);
    }
    ii operator+(const ii &b){
        return ii(x+b.x,y+b.y);
    }
    bool operator!=(const ii &b) const{
        return (b.x!=x)||(b.y!=y);
    }
};
ii sz[N];
int p[N];
int s[N];
int rt[N];
long long ans = 0;
priority_queue<pair<ii,int> > pq;

int find(int x){
    return x == rt[x] ? x : rt[x] = find(rt[x]);
}
void merge(int x, int y){
    x = find(x);
    ans += (ll)sz[x].y*sz[y].x;
    sz[x] = sz[x]+sz[y];
    pq.push(make_pair(sz[x],x));
    rt[y] = x;
}
int main(){
    int n;
    scanf("%d",&n);
    p[1] = 1;
    iota(rt,rt+N,0);
    for (int i = 2; i <= n; i++){
        scanf("%d",&p[i]);
    }
    for (int i = 1; i <= n; i++){
        scanf("%d",&s[i]);
        if (s[i] == 0) sz[i] = {1,0};
        if (s[i] == 1) sz[i] = {0,1};
        if (i != 1) pq.push({sz[i],i});
    }
    while (pq.size()){
        while (pq.size() && sz[pq.top().second] != pq.top().first){
            //printf("remove %d %d %d\n",pq.top().first.x,pq.top().first.y,pq.top().second);
            pq.pop();
        }
        if (pq.empty()) break;
        auto k = pq.top(); pq.pop();
        //printf("processing %d %d %d\n",k.first.x,k.first.y,k.second);
        if (k.second == 1) continue;
        merge(p[k.second],k.second);
    }
    printf("%lld ",ans);

}