Problem:
Score : 1700 points

Problem Statement

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.

Notes

The inversion number of a sequence Z whose length N is the number of pairs of integers i and j ( 1 \leq i < j \leq N ) such that Z_i > Z_j.

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.

Sample Input 1

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

Sample Output 1

4

When the vertices are arranged in the order 1, 3, 5, 6, 2, 4, X will be (0, 1, 0, 0, 1, 0), whose inversion number is 4.
It is impossible to have fewer inversions, so the answer is 4.

Sample Input 2

1

0

Sample Output 2

0

X = (0), whose inversion number is 0.

Sample Input 3

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

Sample Output 3

31

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool smin(T &a, const T &b)   { return a > b ? a = b : a;    }
template<typename T> inline bool smax(T &a, const T &b)   { return a < b ? a = b : a;    }

typedef long long LL;
const int N = (int) 1e6 + 6, mod = (int) 0;
int sub1[N], p[N], val[N], par[N], cnt0[N], sub0[N];
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
void unite(int u, int v) {
	u = find(u);
	v = find(v);
	if (u == v) return;
	if (u < v) swap(u, v);
	par[u] = v;
	cnt0[v] += cnt0[u];
}
#define int long long
vector<int> adj[N];
int32_t main() {
	int n;
	cin >> n;
	for (int j = 1; j < n; ++j) cin >> p[j], --p[j];
	for (int j = 0; j < n; ++j) cin >> val[j], par[j] = j, cnt0[j] = (val[j] == 0);
	int sum0 = 0;
	for (int j = 0; j < n; ++j) if (val[j] == 0) unite(j, p[j]), ++sum0;
	long long res = 0;
	for (int v = 0; v < n; ++v)
		if (val[v] == 1 && v != 0)
			adj[find(p[v])].pb(find(v));
	set<int> st;
	st.insert(find(0));
	if (val[0] == 1) {
		res = sum0;	
	}
	for (int v = n - 1; v >= 1; --v)
		if (par[v] == v) {
			sub0[v] += cnt0[v];
			sub1[v]++;
			int pr = find(p[v]);
			sub0[pr] += sub0[v];
			sub1[pr] += sub1[v];
		}
	sub0[0] += cnt0[0];
	sub1[0] += (val[0] == 1);
	int sum1 = n - sum0;	
	while ((int) st.size()) {
		int mx = -1e9, mxv = -1;
		for (int v : st) if ((LL) cnt0[v] > mx) mx = (LL) cnt0[v], mxv = v;
		sum0 -= cnt0[mxv];
//		cout << " hi " << mxv << ' ' << sub0[mxv] << ' ' << cnt0[mxv] << ' ' << sum0 << ' ' << mx << endl;
		res += sum0;
		st.erase(mxv);
		sum1 -= (val[mxv] == 1);
		for (int u : adj[mxv]) st.insert(u);
	}
	cout << res << endl;
}