#include <bits/stdc++.h>
using namespace std;
const int maxN=1e6+3;
vector<pair<pair<int,int>, int> > a; ///nowsum, last, whfr

int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n,sb=0; cin>>n;
a.push_back({{0,0},-1});
for(int i=1;i<=n;i++){
    int p,m; cin>>p>>m;
    if(m!=0) {
        int q=a[p].first.first;
        q+=m;
        a.push_back({{q,m},i});
        sb+=q;
    }
    else {
        int 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;
}
