Problem: Snuke has a rooted tree with N vertices, numbered 1 through N. Vertex 1 is the root of the tree, and the parent of Vertex i ( 2\leq i \leq N ) is Vertex P_i ( P_i < i ). There is a number, 0 or 1, written on each vertex. The number written on Vertex i is V_i.

Snuke would like to arrange the vertices of this tree in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex.

After arranging the vertices, let X be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement. Snuke would like to minimize the inversion number of X. Find the minimum possible inversion number of X.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq P_i < i ( 2 \leq i \leq N )
* 0 \leq V_i \leq 1 ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
P_2 P_3 ... P_N
V_1 V_2 ... V_N


Output

Print the minimum possible inversion number of X.

Examples

Input

6
1 1 2 3 3
0 1 1 0 0 0


Output

4


Input

1

0


Output

0


Input

15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0


Output

31

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define rrep(i,a,b) for(int i = (b); i --> (a);)
#define all(v) (v).begin(),(v).end()
#define trav(x,v) for(auto &x : v)
#define sz(v) int(v.size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;

struct UF {
	vi t;
	UF( int n) {
		t.resize(n, -1);
	}

	int find(int a){
		return t[a]<0 ? a : t[a] = find(t[a]);
	}

	void uni(int a, int b){
		a = find(a), b = find(b);
		if( a == b) return;
		t[a] = b;
	}
};

int main(){
	int n;
	cin >> n;
	vi p(n);
	vector<pii> v01(n);
	rep(i,1,n){
		cin >> p[i];
		--p[i];
	}
	rep(i,0,n){
		int v;
		cin >> v;
		if(v == 0) v01[i].first++;
		else       v01[i].second++;
	}

	function<ll(pii, pii)> invs = [&](pii a, pii b){
		return ll(a.second) * b.first;
	};

	function<bool(int, int)> compare = [&](int a, int b){
		ll res1 = invs(v01[a],v01[b]), res2 = invs(v01[b],v01[a]);
		if(res1 < res2) return true;
		if(res2 < res1) return false;
		return a < b;
	}; // a should come before b

	UF uf(n);

	set< int, function<bool(int, int)> > s( compare );

	rep(i,1,n) s.insert(i);

	ll ans = 0;

	while(sz(s)){
		int i = *s.begin();
		s.erase(s.begin());
		int par = uf.find(p[i]);
		uf.uni(i, par);
		ans += invs( v01[par], v01[i] );
		
		if(par) s.erase(par);
		v01[par].first += v01[i].first;
		v01[par].second += v01[i].second;
		if(par) s.insert(par);
		
	}

	cout << ans << endl;

}