#include <bits/stdc++.h>

#define ln                 '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 2e5;
const num inf = 0x3f3f3f3f3f3f3f3f;
int n, a[2 * nsz + 5];
num sum, ans;

int main() {
	scanf("%d", &n);
	cont (i, 2 * n) scanf("%d", &a[i]), sum += a[i];
	priority_queue<int, vector<int>, greater<int>> q;
	cont (i, n) q.push(a[n + 1 - i]), q.push(a[n + i]), ans += q.top(), q.pop();
	ans = sum - ans;
	printf("%lld\n", ans);
}
#include <bits/stdc++.h>
using namespace std;
#define SZ(x) (int)(x.size())
#define int long long

int n;
vector<int> edges[100001];

int fh[100001], sh[100001];
bool change[100001];

bool cmp(int p, int q){
    int s1,s2;
    if(change[p])s1 = fh[p]-sh[p];
    else if(fh[p]-sh[p] >= 0)s1 = 1e18;
    else s1 = -1e18;
    if(change[q])s2 = fh[q]-sh[q];
    else if(fh[q]-sh[q] >= 0)s2 = 1e18;
    else s2 = -1e18;
    return s1 < s2;
}

void dfs(int u){
    for(int i = 0; i < SZ(edges[u]); ++i){
        int nxt = edges[u][i];
        dfs(nxt);
    }
    sort(edges[u].begin(), edges[u].end(), cmp);
    fh[u] = 1;
    sh[u] = 0;
    change[u] = 1;
    for(int i = 0, s = 1; i < SZ(edges[u]); ++i){
        int nxt = edges[u][i];
        change[u] ^= change[nxt];
        if(s == 1){
            fh[u] += fh[nxt];
            sh[u] += sh[nxt];
        }else{
            fh[u] += sh[nxt];
            sh[u] += fh[nxt];
        }
        if(change[nxt]){
            s = !s;
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 2; i <= n; ++i){
        int p;
        cin >> p;
        edges[p].emplace_back(i);
    }
    dfs(1);
    cout << fh[1] << '\n';
    //for(int i = 1; i <= n; ++i){
    //    cout << fh[i] << ' ' << sh[i] << ' ' << change[i] << '\n';
    //}
    return 0;
}