#include <bits/stdc++.h>
using namespace std;

long long n, p, m;
long long a[1000002], stem[1000002], sum;

int main () {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

cin >> n;

for (int i=1; i <= n; i++) {
    cin >> p >> m;
    if (m > 0) {
        a[i] = a[p] + m;
        stem[i] = p;
    }
    else {
        a[i] = a[stem[p]];
        stem[i] = stem[stem[p]];
    }
    sum += a[i];
}

//for (int i=0; i <= n; i++) {cout << i << " " << a[i] << " " << stem[i] << endl;}
cout << sum << endl;

return 0;
}