#include<bits/stdc++.h>
using   namespace   std;
#define ll long long
#define llu unsigned    long    long
#define ld  double
#define llu unsigned    long    long
#define rep(i,x,y)      for(ll  i=x;i<y;++i)
#define For(i,x,y)      for(ll  i=x;i<=y;++i)
#define FOr(i,x,y)      for(ll  i=(ll)(x);i>=(ll)(y);--i)
#define pi  acos(-1)    
#define mk  make_pair
#define pa  pair<ll,ll>
#define lf  else    if
#define IL  inline
#define max(x,y)                ((x)<(y)?(y):(x))
#define min(x,y)                ((x)<(y)?(x):(y))
#define sqr(x)                      ((x)*(x))
#define Mul(x,y)                ((x)=1LL*(x)*(y)%mod)
#define Add(x,y)                ((x)=((x)+(y))%mod)
#define Max(x,y)                ((x)=((x)<(y)?(y):(x)))
#define Min(x,y)                ((x)=((x)>(y)?(y):(x)))
#define E(x)                    return  writeln(x),0
#define LL                      (long   long)
#define p(x)                    printf("~%lld~\n",LL(x))
#define pp(x,y)                 printf("~~%lld  %lld~~\n",LL(x),LL(y))
#define ppp(x,y,z)              printf("~~~%lld %lld    %lld~~~\n",LL(x),LL(y),LL(z))
#define pppp(a,b,c,d)           printf("~~~%lld %lld    %lld    %lld\n",LL(a),LL(b),LL(c),LL(d))
#define f_in(x)                 freopen(x".in","r",stdin)
#define f_out(x)                freopen(x".out","w",stdout)
#define open(x)                 f_in(x),f_out(x)
#define fi  first
#define se  second
#define GuYue       puts("\nGuYueNa________________________________________________________________________________")
typedef complex<double>   E;
namespace   SHENZHEBEI{
#ifdef  LOCAL
                struct  _{_(){freopen("cf.in","r",stdin);}}_;
#endif  
#define NEG 1
                inline  char    gc(){   return  getchar();  }
#if NEG
                inline  ll  read(){             ll  x=0,fa=1;   char    ch=gc();            for (;!isdigit(ch);ch=gc()) if  (ch=='-')   fa=-1;      for (;isdigit(ch);ch=gc())      x=x*10-48+ch;           return  x*fa;   }
                inline  void    write(ll    x){             if  (x<0)    putchar('-'),x=-x;      if  (x>=10)          write(x/10);                putchar(x%10+'0');      }
#else
                inline  ll  read(){             ll  x=0;    char    ch=gc();            for (;!isdigit(ch);ch=gc());                for (;isdigit(ch);ch=gc())      x=x*10-48+ch;           return  x;          }
                inline  void    write(ll    x){             if  (x>=10)          write(x/10);                putchar(x%10+'0');      }
#endif
    void    writeln(ll  x){write(x);puts("");}
}using  namespace   SHENZHEBEI;
const ll N=1000010;
ll n,tot,a[N],dfn[N],head[N],nxt[N],vet[N],pre[N],fa[N],q[N],w[N],ans;
struct dt{ll a,b,c;}lbc[N];
bool operator <(dt a,dt b){return a.a*b.b==b.a*a.b?(a.a>b.a):(a.a*b.b>b.a*a.b);}
bool operator !=(dt a,dt b){return (a.a!=b.a)||(a.b!=b.b)||(a.c!=b.c);}
priority_queue<dt>Q;
void link(ll x,ll y){++dfn[y];pre[y]=x;nxt[++tot]=head[x];head[x]=tot;vet[tot]=y;}
ll find(ll x){return x==fa[x]?x:fa[x]=find(fa[x]);}
bool pd(){
    ll he=0,ta=1;
    for(;he^ta;){
        ll x=q[++he];
        for(ll i=head[x];i;i=nxt[i])if (!--dfn[vet[i]])q[++ta]=vet[i];
    }For(i,1,n)if (dfn[i])return 0;
    return 1;
}
int main(){
    n=read();
    For(i,2,n)pre[i]=read();
    For(i,1,n)w[i]=read();
    For(i,1,n)Q.push(lbc[i]=(dt){w[i],1,i}),fa[i]=i;
    for(;!Q.empty();){
        dt x=Q.top();Q.pop();
        if (x!=lbc[find(x.c)])continue;
        ans+=(x.b-x.a)*lbc[find(pre[x.c])].a;
        if (!find(pre[x.c]))fa[x.c]=0,lbc[0].b+=x.b,lbc[0].a+=x.a;
        else{
            fa[x.c]=find(pre[x.c]);
            lbc[fa[x.c]].a+=x.a;
            lbc[fa[x.c]].b+=x.b;
            Q.push(lbc[fa[x.c]]);
        }
    }writeln(ans);
}

Measured memory_bytes: 23532