#include<iostream>
using namespace std;
const int maxn=1000009;
long long n,ans;
pair<int,int> sm[maxn]; /// first = weight, second = last
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    int p,m;
    for(int i=0;i<n;i++){
        cin>>p>>m;
        if(p>0) p--;
        if(m==0){
            sm[i].first=sm[p].first;
            sm[i].first-=sm[p].second;
            /// sm[i].second = ????
        }else{
            sm[i].first=sm[p].first;
            sm[i].first+=m;
            sm[i].second=m;
        }
        ///cout<<i<<" "<<sm[i].first<<"\n";
        ans+=sm[i].first;
    }
    cout<<ans<<"\n";
    return 0;
}
