#include<bits/stdc++.h>
using namespace std;
int n,sb=0,b[10000];
pair<int,int> a[10000];
int main(){
   // cout<<"vlizam\n";
    cin>>n;
    a[0].first=0;
    a[0].second=0;
    for(int i=1;i<=n;i++){
        int p,m;
        cin>>p>>m;
        if(m!=0){
            a[i].first=a[p].first+m;
            a[i].second=m;
            b[i]=p;
            sb+=a[i].first;
        }else{
            a[i].first=a[p].first-a[p].second;
            a[i].second=a[b[p]].second;
            sb+=a[i].first;
        }
    }
    cout<<sb<<"\n";
    return 0;
}
