#include <bits/stdc++.h>
using namespace std;
const long maxN=1e6+3;
vector<pair<pair<long,long>, long> > a; ///nowsum, last, whfr
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long n,sb=0; cin>>n;
a.push_back({{0,0},-1});
for(long i=1;i<=n;i++){
    long p,m; cin>>p>>m;
    if(m!=0) {
        long q=a[p].first.first;
        q+=m;
        a.push_back({{q,m},i});
        sb+=q;
    }
    else {
        long q,lst,fr=a[p].second;
        tie(q,lst)=a[p].first;
        q-=lst;
        a.push_back({{q,a[fr].first.second},a[fr].second});
        sb+=q;
    }
}
cout<<sb<<'\n';
return 0;
}
