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

stack<int>snow[1000004];

int main(){

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

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

    return 0;
}

