Language: C++

#include <bits/stdc++.h>
#define int long long
using namespace std;

int n,c[200010],p[200010],dsu[200010],sz[200010],sum[200010];
vector<int> v[200010];

inline int find_set(int x)
{
	return dsu[x]==x?x:dsu[x]=find_set(dsu[x]);
}

class cmp{
	public:
		bool operator()(const pair<pair<int,int>,int> x,const pair<pair<int,int>,int> y){
			int val1=x.first.first*y.first.second,val2=y.first.first*x.first.second;
			if(val1!=val2) return val1<val2;
			else return x.second<y.second;
		}
};

signed main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++) dsu[i]=i,sz[i]=1;
	for(int i=2;i<=n;i++) cin>>p[i];
	for(int i=1;i<=n;i++) cin>>c[i];
	for(int i=1;i<=n;i++) sum[i]=c[i];
	set<pair<pair<int,int>,int>,cmp> s;
	for(int i=1;i<=n;i++) s.insert(make_pair(make_pair(sum[i],sz[i]),i));
	int ans=0;
	while(!s.empty()){
		int t=(*s.begin()).second;
		pair<int,int> val=(*s.begin()).first;
		s.erase(s.begin());
		if(val.first!=sum[t]||val.second!=sz[t]) continue;
		if(t==1) continue;
		dsu[t]=p[t];
		int root=find_set(t);
		ans+=sum[root]*(sz[t]-sum[t]);
		sz[root]+=sz[t];sum[root]+=sum[t];
		s.insert(make_pair(make_pair(sum[root],sz[root]),root));
	}
	cout<<ans<<'\n';
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<std::pair<long long int, long long int>, long long int>; _Val = std::pair<std::pair<long long int, long long int>, long long int>; _KeyOfValue = std::_Identity<std::pair<std::pair<long long int, long long int>, long long int> >; _Compare = cmp; _Alloc = std::allocator<std::pair<std::pair<long long int, long long int>, long long int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<std::pair<long long int, long long int>, long long int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<std::pair<long long int, long long int>, long long int>; _Val = std::pair<std::pair<long long int, long long int>, long long int>; _KeyOfValue = std::_Identity<std::pair<std::pair<long long int, long long int>, long long int> >; _Compare = cmp; _Alloc = std::allocator<std::pair<std::pair<long long int, long long int>, long long int> >; key_type = std::pair<std::pair<long long int, long long int>, long long int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = std::pair<std::pair<long long int, long long int>, long long int>; _Key = std::pair<std::pair<long long int, long long int>, long long int>; _Val = std::pair<std::pair<long long int, long long int>, long long int>; _KeyOfValue = std::_Identity<std::pair<std::pair<long long int, long long int>, long long int> >; _Compare = cmp; _Alloc = std::allocator<std::pair<std::pair<long long int, long long int>, long long int> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:523:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<std::pair<long long int, long long int>, long long int>; _Compare = cmp; _Alloc = std::allocator<std::pair<std::pair<long long int, long long int>, long long int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<std::pair<long long int, long long int>, long long int>, std::pair<std::pair<long long int, long long int>, long long int>, std::_Identity<std::pair<std::pair<long long int, long long int>, long long int> >, cmp, std::allocator<std::pair<std::pair<long long int, long long int>, long long int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<std::pair<long long int, long long int>, long long int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<std::pair<long long int, long long int>, long long int> >, std::pair<std::pair<long long int, long long int>, long long int> >::rebind<std::pair<std::pair<long long int, long long int>, long long int> >; typename _Alloc::value_type = std::pair<std::pair<long long int, long long int>, long long int>; value_type = std::pair<std::pair<long long int, long long int>, long long int>]'
  523 |           _M_t._M_insert_unique(std::move(__x));
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:31:32:   required from here
   31 |         for(int i=1;i<=n;i++) s.insert(make_pair(make_pair(sum[i],sz[i]),i));
      |                               ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const cmp&, const std::pair<std::pair<long long int, long long int>, long long int>&, const std::pair<std::pair<long long int, long long int>, long long int>&>' evaluates to false