#include using namespace std; const int INF = 1000000000; const long long INF2 = 1000000000000000; pair comp(pair P, pair Q){ return make_pair(P.first + Q.first, max(P.second + Q.first, Q.second)); } struct dual_segment_tree{ int N; vector> ST; dual_segment_tree(){ } dual_segment_tree(int n){ N = 1; while (N < n){ N *= 2; } ST = vector>(N * 2 - 1, make_pair(0, 0)); } long long operator [](int k){ k += N - 1; pair ans = ST[k]; while (k > 0){ k = (k - 1) / 2; ans = comp(ans, ST[k]); } return max(ans.first, ans.second); } void range_apply(int L, int R, pair P, int i, int l, int r){ if (r <= L || R <= l){ return; } else if (L <= l && r <= R){ ST[i] = comp(ST[i], P); } else { ST[i * 2 + 1] = comp(ST[i * 2 + 1], ST[i]); ST[i * 2 + 2] = comp(ST[i * 2 + 2], ST[i]); ST[i] = make_pair(0, 0); int m = (l + r) / 2; range_apply(L, R, P, i * 2 + 1, l, m); range_apply(L, R, P, i * 2 + 2, m, r); } } void range_chmax(int L, int R, long long x){ range_apply(L, R, make_pair(0, x), 0, 0, N); } void range_add(int L, int R, long long x){ range_apply(L, R, make_pair(x, 0), 0, 0, N); } }; int main(){ int N; cin >> N; vector A(N), H(N), C(N); A[0] = 0; for (int i = 1; i < N; i++){ cin >> A[i]; A[i]--; } for (int i = 0; i < N; i++){ cin >> H[i]; } for (int i = 0; i < N; i++){ cin >> C[i]; } vector> c(N); for (int i = 1; i < N; i++){ c[A[i]].push_back(i); } vector sz(N, 1); for (int i = N - 1; i >= 1; i--){ sz[A[i]] += sz[i]; } for (int i = 0; i < N; i++){ for (int &j : c[i]){ if (sz[j] > sz[c[i][0]]){ swap(j, c[i][0]); } } } vector next(N, 0); for (int i = 1; i < N; i++){ if (i == c[A[i]][0]){ next[i] = next[A[i]]; } else { next[i] = i; } } vector> r(N); for (int i = 0; i < N; i++){ if (next[i] == i){ queue Q; Q.push(i); while (!Q.empty()){ int v = Q.front(); Q.pop(); r[i].push_back(H[v]); for (int w : c[v]){ Q.push(w); } } r[i].push_back(INF); sort(r[i].begin(), r[i].end()); r[i].erase(unique(r[i].begin(), r[i].end()), r[i].end()); } } vector dp(N); for (int i = 0; i < N; i++){ if (!r[i].empty()){ dp[i] = dual_segment_tree(r[i].size()); } } for (int i = N - 1; i >= 0; i--){ int d = c[i].size(); for (int j = 1; j < d; j++){ int p = next[i]; int v = c[i][j]; int cnt = r[v].size(); for (int k = 0; k < cnt; k++){ int L, R; if (k == 0){ L = 0; } else { L = lower_bound(r[p].begin(), r[p].end(), r[v][k - 1]) - r[p].begin() + 1; } R = lower_bound(r[p].begin(), r[p].end(), r[v][k]) - r[p].begin(); dp[p].range_add(L, R + 1, dp[v][k]); } } int id = lower_bound(r[next[i]].begin(), r[next[i]].end(), H[i]) - r[next[i]].begin(); dp[next[i]].range_chmax(0, id + 1, dp[next[i]][id] + C[i]); } cout << dp[0][0] << endl; }