#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;
}
