#include <bits/stdc++.h>
#define ednl "\n"
using namespace std;

stack<int>snow[1000004];
int sum[1000004];

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,p,m,otg=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p>>m;
        if(m>0){
            snow[i]=snow[p];
            snow[i].push(m);
            sum[i]=sum[p]+m;
        }else{
            sum[i]=sum[p]-snow[p].top();
            snow[i]=snow[p];
            snow[i].pop();
        }
        otg+=sum[i];
    }
    cout<<otg<<endl;

    return 0;
}

