#include<bits/stdc++.h>
using namespace std;

const int maxn=1000003;
int n;
unsigned long long ans=0;
pair <int, int> t[maxn];
int parent[maxn];

int main(){
cin>>n;
t[0]={0, 0};
parent[0]=0;
for(int i=1; i<=n; i++){
    int p, m;
    cin>>p>>m;
    int curr_t;
    if(m==0){
        t[i].first=t[p].first-t[p].second;
        t[i].second=t[parent[p]].second;
        parent[i]=i;
    }else{
        t[i].first=t[p].first+m;
        t[i].second=m;
        parent[i]=p;
    }
    //cerr<<"i="<<i<<" "<<t[i].first<<" "<<t[i].second<<" "<<parent[i]<<endl;
    ans+=t[i].first;
}
cout<<ans;
return 0;
}
