//I play to win#include<bits/stdc++.h>#define ll long long#define ull unsigned ll#define pi pair<int,int>#define ld long double#define vi vector<int>#define all(x) begin(x),end(x)usingnamespacestd;inlinellread(){llx=0,f=1;charc=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);returnx*f;}inlinevoidout(llx){if(x>9)out(x/10);putchar(x%10^48);}inlinevoidprint(llx,charc='\n'){if(x<0)putchar('-'),x=-x;out(x),putchar(c);}constintN=5e5+10;intfa[N],dfn[N],w[N],siz[N],dep[N],id[N];vector<int>v[N];structBIT{intc[N];voidadd(intx,intv){for(;x<N;x+=x&-x)c[x]+=v;}intask(intx){intr=0;for(;x;x&=x-1)r+=c[x];returnr;}}T[2][2];voiddfs(intx){siz[x]=1,dep[x]=dep[fa[x]]+1,dfn[x]=++dfn[0];for(inty:v[x])dfs(y),siz[x]+=siz[y];}intmain(){intn=read();llans=0;for(inti=1;i<=n;i++)w[i]=read(),id[i]=i;for(inti=2;i<=n;i++)v[fa[i]=read()].push_back(i);dfs(1),sort(id+1,id+n+1,[](intx,inty){returnw[x]+dep[x]-siz[x]<w[y]+dep[y]-siz[y];});for(inti=1;i<=n;i++){intk=i&1,x=id[i],l=dfn[x],r=dfn[x]+siz[x]-1;if(k)ans-=T[k^1][0].ask(l),ans+=T[k^1][1].ask(r)-T[k^1][1].ask(l-1),ans-=w[x];//先手elseans+=T[k^1][0].ask(l),ans-=T[k^1][1].ask(r)-T[k^1][1].ask(l-1);T[k][0].add(l,1),T[k][0].add(r+1,-1),T[k][1].add(l,1);// cout<<x<<" "<<l<<" "<<r<<" "<<ans<<endl;}print(ans);}
//I play to win#include<bits/stdc++.h>#define ll long long#define ull unsigned ll#define pi pair<int,int>#define ld long double#define vi vector<int>#define all(x) begin(x),end(x)usingnamespacestd;inlinellread(){llx=0,f=1;charc=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);returnx*f;}inlinevoidout(llx){if(x>9)out(x/10);putchar(x%10^48);}inlinevoidprint(llx,charc='\n'){if(x<0)putchar('-'),x=-x;out(x),putchar(c);}constintN=5e5+10;lla[N],b[N],s[N];intmain(){intn=read();lluse=0,ans=0;for(inti=1;i<=n;i++)a[i]=b[i]=read();sort(b+1,b+n+1);for(inti=1;i<=n;i++)s[i]=b[i]+s[i-1];for(inti=n;i;i--){if(a[i]<use)continue;intl=upper_bound(b+1,b+n+1,use)-b,r=upper_bound(b+1,b+n+1,a[i])-b-1;ans+=i*(s[r]-s[l-1]-use*(r-l+1)+(a[i]-use)*(n-r)),use=a[i];// cout<<l<<" "<<r<<" "<<ans<<endl;}print(ans);}
//I play to win#include<bits/stdc++.h>#define ll long long#define ull unsigned ll#define pi pair<int,int>#define ld long double#define vi vector<int>#define all(x) begin(x),end(x)usingnamespacestd;inlinellread(){llx=0,f=1;charc=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);returnx*f;}inlinevoidout(llx){if(x>9)out(x/10);putchar(x%10^48);}inlinevoidprint(llx,charc='\n'){if(x<0)putchar('-'),x=-x;out(x),putchar(c);}constintN=1e5+10;intmain(){puts("China");}