Language: C++

#include<bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed<<setprecision(x)
using namespace std;
void Freopen(){
	freopen(title".in","r",stdin);
	freopen(title".out","w",stdout);
}
ll read(){
	ll g=0,f=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();}
	return g*f;
}
const ll N=30005;
ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
struct Edge{
	ll to,ne;
}e[N<<1];
struct node{
	ll x,sum,size;
	bool operator<(const node &other)const{
		return sum*other.size<other.sum*size;
	}
};
void add(ll x,ll y){
	e[++cnt]=Edge{y,h[x]};
	h[x]=cnt;
}
void dfs(ll x,ll anc){
	if(a[x])prt[x]=anc,anc=x;else w[anc]++;
	for(ll i=h[x];i;i=e[i].ne)dfs(e[i].to,anc);
}
ll get(ll x){
	return (f[x]==x?x:f[x]=get(f[x]));
}
void solve(ll x){
	Ans=0;priority_queue<node>q;
	size[0]=1;
	for(ll i=1;i<=n;i++)if(~prt[i])f[i]=i,size[i]=1,sum[i]=w[i],q.push(node{i,w[i],1});
	while(true){
		while(!q.empty()&&q.top().size!=size[q.top().x])q.pop();
		if(q.empty())break;
		ll x=q.top().x;q.pop();
		if(prt[x]==-1)continue;
		Ans+=size[get(prt[x])]*sum[x];
		f[x]=get(prt[x]);
		size[get(prt[x])]+=size[x];
		sum[get(prt[x])]+=sum[x];
		q.push(node{get(prt[x]),sum[get(prt[x])],size[get(prt[x])]});
	}ans=Ans;
}
signed main(){
	n=read();add(0,1);
	for(int i=2;i<=n;i++)pr[i]=read(),add(pr[i],i);
	a[0]=1;for(ll i=1;i<=n;i++)a[i]=read();
	memset(prt,-1,sizeof(prt));dfs(0,0);prt[0]=-1;
	return solve(0),cout<<ans,signed();
}

Compiler output:
a.cc: In function 'void solve(long long int)':
a.cc:42:9: error: reference to 'size' is ambiguous
   42 |         size[0]=1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:19:24: note:                 'long long int size [30005]'
   19 | ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
      |                        ^~~~
a.cc:43:47: error: reference to 'size' is ambiguous
   43 |         for(ll i=1;i<=n;i++)if(~prt[i])f[i]=i,size[i]=1,sum[i]=w[i],q.push(node{i,w[i],1});
      |                                               ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:19:24: note:                 'long long int size [30005]'
   19 | ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
      |                        ^~~~
a.cc:45:49: error: reference to 'size' is ambiguous
   45 |                 while(!q.empty()&&q.top().size!=size[q.top().x])q.pop();
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:19:24: note:                 'long long int size [30005]'
   19 | ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
      |                        ^~~~
a.cc:49:22: error: reference to 'size' is ambiguous
   49 |                 Ans+=size[get(prt[x])]*sum[x];
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:19:24: note:                 'long long int size [30005]'
   19 | ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
      |                        ^~~~
a.cc:51:17: error: reference to 'size' is ambiguous
   51 |                 size[get(prt[x])]+=size[x];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:19:24: note:                 'long long int size [30005]'
   19 | ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
      |                        ^~~~
a.cc:51:36: error: reference to 'size' is ambiguous
   51 |                 size[get(prt[x])]+=size[x];
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:19:24: note:                 'long long int size [30005]'
   19 | ll h[N],cnt,pr[N],f[N],size[N],sum[N],Ans,ans,n,w[N],a[N],prt[N];
      |                        ^~~~
a.cc:53:28: error: expected primary-expression before '{' token
   53 |                 q.push(node{get(prt[x]),sum[get(prt[x])],size[get(prt[x])]});
      |                            ^