#include <iostream>
#include <set>
#include <map>
#include <array>
#include <cassert>
#include <cstring>
#include <vector>
#include <list>
#include <algorithm>

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define mp make_pair

template <typename T>
bool uax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template <typename T>
bool uin(T& a, T b) { if (b < a) { a = b; return true; } return false; }

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef list<int> li;
typedef vector<li> vli;
typedef vector<vpi> vvpi;
typedef long long i64;

const int inf = 1e9;

i64 merge(vi &a, const vi& b) {
  vpi za(a.size() + 1);
  forn(i, a.size()) if (a[i]) za[i + 1].se += 1; else za[i + 1].fi += 1;
  forn(i, a.size()) { za[i + 1].fi += za[i].fi; za[i + 1].se += za[i].se; }
  vpi zb(b.size() + 1);
  forn(i, b.size()) if (b[i]) zb[i + 1].se += 1; else zb[i + 1].fi += 1;
  forn(i, b.size()) { zb[i + 1].fi += zb[i].fi; zb[i + 1].se += zb[i].se; }
  vvi d(a.size() + 1, vi(b.size() + 1, inf));
  vvi p(a.size() + 1, vi(b.size() + 1, -1));
  d[0][0] = 0;
  fore(i, 0, a.size()) {
    fore(j, 0, b.size()) {
      if (i + 1 <= a.size()) {
        if (uin(d[i + 1][j], d[i][j] + (a[i] ? 0 : zb[j].se))) p[i+1][j] = 0;
      }
      if (j + 1 <= b.size()) {
        if (uin(d[i][j + 1], d[i][j] + (b[j] ? 0 : za[i].se))) p[i][j+1] = 1;
      }
    }
  }
  int i = a.size(), j = b.size();
  vi c;
  i64 ans = d[i][j];
  while (i && j) {
    if (p[i][j] == 1) c.pb(b[--j]);
    else if (p[i][j] == 0) c.pb(a[--i]);
  }
  while (i) {
    c.pb(a[--i]);
  }
  while (j) {
    c.pb(b[--j]);
  }
  reverse(all(c));
  if (0) {
    cerr << "dbg\n";
    cerr << a.size(); for (int x: a) cerr << ' ' << x; cerr << endl;
    cerr << b.size(); for (int x: b) cerr << ' ' << x; cerr << endl;
    cerr << c.size(); for (int x: c) cerr << ' ' << x; cerr << endl;
    cerr << ans << endl;
  }
  a = c;
  return ans;
}

void solve_d0() {
  int n;
  cin >> n;
  vi p(n), w(n);
  vvi g(n, vi{});
  forn(i, n - 1) cin >> p[i + 1];
  forn(i, n) cin >> w[i];
  i64 ans = 0;
  ford(i, n) {
    if (w[i]) {
      for (int x: g[i]) if (!x) ans += 1;
    }
    g[i].insert(g[i].begin(), w[i]);
    if (i) {
      if (g[p[i] - 1].size() < g[i].size()) g[i].swap(g[p[i] - 1]);
      if (!g[i].empty()) {
        ans += merge(g[p[i] - 1], g[i]);
      }
    }
  }
  cout << ans << endl;
}

void solve_d() {
  int n;
  cin >> n;
  vi p(n), w(n);
  forn(i, n - 1) cin >> p[i + 1];
  forn(i, n) cin >> w[i];
  vpi q(n);
  forn(i, n) if (w[i]) q[i].se += 1; else q[i].fi += 1;
  vi pp(n);
  forn(i, n) pp[i] = i;
  auto cmp = [&] (int i, int j) { return mp(q[i].fi * 1ll * q[j].se, i) > mp(q[j].fi * 1ll * q[i].se, j); };
  set<int, decltype(cmp)> S(cmp);
  forn(i, n - 1) S.insert(i + 1);
  i64 ans = 0;
  while (!S.empty()) {
    int x = *S.begin();
    S.erase(S.begin());
    int y = p[x] - 1;
    while (pp[y] != y) y = pp[y];
    int z = p[x] - 1;
    while (z != y) { int nz = pp[z]; pp[z] = y; z = nz; }
    ans += q[y].se * 1ll * q[x].fi;
    if (y) {
      S.erase(y);
    }
    q[y].fi += q[x].fi;
    q[y].se += q[x].se;
    if (y) {
      S.insert(y);
    }
    pp[x] = y;
  }
  cout << ans << endl;
}

void solve_c() {
  int n; cin >> n; ++n;
  vi f(n), l(n), r(n);
  f[0] = r[0] = 1; l[0] = 0;
  forn(i, n) if (i) {
    l[i] += l[i-1];
    fore(j, 1, i-2) l[i] += l[j];
  }
}

void solve_c0() {
  // l_n 0,2,10,60,396,2976
  // f_n 1,3,14,74,468,3396
  // r_n 1,4,16,84,516,3696
  int n;
  cin >> n;
  vi a(n - 1), b(n);
  forn(i, n - 1) a[i] = i;
  i64 ans = 0; // vi ans(n, 0);
  do {
    forn(i, n) b[i] = 0;
    int c = 0;
    forn(i, n - 1) {
      int x = a[i];
      if (!b[x]) ++c; b[x] = 1;
      if (!b[x + 1]) ++c; b[x + 1] = 1;
      if (c == n) {
        ans += i + 1; // ans[i] += 1;
        break;
      }
    }
  } while(next_permutation(all(a)));
  // for (int x: ans) cout << x << endl;
  cout << ans << endl;
}

void solve_b() {
  int n;
  cin >> n;
  vector<string> a(n*2);
  forn(i, n) {
    cin >> a[i]; a[i] += a[i];
  }
  forn(i, n) a[i + n] = a[i];
  vvi b(n*2, vi(n*2, 0));
  int ans = 0;
  fore(d, -n+1, n-1) {
    forn(i, 2 * n) forn(j, 2 * n) b[i][j] = 0;
    forn(i, 2 * n) forn(j, 2 * n) {
      int p = j + d, q = i - d;
      if (0 <= p && p < 2 * n && 0 <= q && q < 2 * n) {
        if (a[p][q] != a[i][j]) b[i][j] = 1;
      }
    }
    if (0) {
      cerr << d << endl;
      forn(i, 2 * n) forn(j, 2 * n) cerr << b[i][j] << " \n"[j + 1 == 2 * n];
    }
    forn(i, 2 * n) {
      forn(j, 2 * n) {
        if (i) b[i][j] += b[i - 1][j];
        if (j) b[i][j] += b[i][j - 1];
        if (i && j) b[i][j] -= b[i - 1][j - 1];
      }
    }
    if (0) {
      cerr << d << endl;
      forn(i, 2 * n) forn(j, 2 * n) cerr << b[i][j] << " \n"[j + 1 == 2 * n];
    }
    fore(i, n - 1, 2 * n - 2) {
      fore(j, n - 1, 2 * n - 2) {
        if (i - j != d) continue;
        int s = b[i][j];
        if (i >= n) s -= b[i - n][j];
        if (j >= n) s -= b[i][j - n];
        if (i >= n && j >= n) s += b[i - n][j - n];
        if (s == 0) {
          // cerr << d << ' ' << i << ' ' << j << endl;
          ans += 1;
        }
      }
    }
  }
  cout << ans << endl;
}

void solve_a() {
  int n;
  cin >> n;
  map<i64, int> M;
  M[0] = 1;
  i64 ans = 0, s = 0;
  forn(i, n) {
    int x;
    cin >> x;
    ans += M[s += x] += 1;
    ans -= 1;
  }
  cout << ans << endl;
}

int main() {
#ifdef HOME
  freopen("input.txt", "rt", stdin);
#endif
  solve_d();
  return 0;
}


Measured memory_bytes: 13568