Problem: 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.

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.

Examples

Input

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


Output

4


Input

1

0


Output

0


Input

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


Output

31

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;

#define Re return
#define In inline
#define Op operator
#define St static
#define Rg register
#define Ct continue
#define inc(l, i, r) for(Rg int i=l; i<r; ++i)
#define dec(l, i, r) for(Rg int i=r; i>l; --i)
#define fev for(Rg int o=u[h], v; (v=o[e].v)>0; o=o[e].o)
#define mP make_pair
#define fst first
#define snd second

typedef long long ll;
typedef pair<int, int> pInt;
typedef double db;

const int mxN=1<<18;

struct Nod
{
    int s0, s1, id;
    In Nod(int s0=0, int s1=0, int id=0) :
    s0(s0), s1(s1), id(id) {}
    In bool Op <(const Nod& b) const {Re (ll)s0*b.s1<(ll)s1*b.s0;}
} a[mxN];

int bel[mxN];

int get(int x) {Re bel[x]? bel[x]=get(bel[x]): x;}

int main()
{
    St int n;
    scanf("%d", &n);
    St int f[mxN];
    inc(2, i, n+1)
        scanf("%d", f+i);
    inc(1, i, n+1)
    {
        St int k;
        scanf("%d", &k);
        a[i]=Nod(!k, k, i);
    }
    St priority_queue<Nod> q;
    inc(2, i, n+1)
        q.push(a[i]);
    St ll ans[mxN];
    for(;!q.empty();)
    {
        Nod u=q.top();
        q.pop();
        if(a[u.id].s0!=u.s0 || a[u.id].s1!=u.s1) Ct;
        Rg int v=get(f[u.id]);
        bel[u.id]=v;
        ans[v]+=ans[u.id]+(ll)a[v].s1*u.s0;
        a[v]=Nod(a[v].s0+u.s0, a[v].s1+u.s1, v);
        if(v>1) q.push(a[v]);
    }
    printf("%lld\n", ans[1]);
    Re 0;
}