#include<bits/stdc++.h>
using namespace std;
long long n,sb=0,b[1000000];
pair<long long,long long> a[1000000];
int main(){
   // cout<<"vlizam\n";
   ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n;
    a[0].first=0;
    a[0].second=0;
    for(long long i=1;i<=n;i++){
        long long 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;
            b[i]=b[b[p]];
            sb+=a[i].first;
        }
       // cout<<i<<" i "<<a[i].first<<" "<<a[i].second<<"\n";
    }
    cout<<sb<<"\n";
    return 0;
}
