#include <iostream>
using namespace std;
#define endl '\n'
#define int long long
#define MAXN 1000005
int arr[MAXN], remget[MAXN];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int br; cin >> br;
    int sum = 0;
    for(int i = 1; i <= br; ++i){
        int p, m; cin >> p >> m;
        if(m > 0){
            arr[i] = arr[p] + m;
            remget[i] = p;
        }
        else{
            arr[i] = arr[remget[p]];
            remget[i] = remget[remget[p]];
        }
        sum += arr[i];
    }
    cout << sum << endl;
    return 0;
}
