#include<iostream>
#include<iomanip>
using namespace std;
struct systoqnie{
    pair<long long,long long> komanda;
    long long weight;
    long long top;
    long long fromwhere_is_top;
};
systoqnie snowmen[1002000];
int main(){
int n;
cin>>n;
snowmen[0].weight=0;
snowmen[0].top=0;
for(int i=1;i<=n;i++){
    cin>>snowmen[i].komanda.first>>snowmen[i].komanda.second;
}
for(int i=1;i<=n;i++){
    pair<int,int> kom=snowmen[i].komanda;
    if(kom.second>0){
        snowmen[i].top=kom.second;
        snowmen[i].weight=snowmen[kom.first].weight+kom.second;
        snowmen[i].fromwhere_is_top=i;
    }else if(kom.second==0){
        long long k=snowmen[kom.first].fromwhere_is_top;
        k=snowmen[k].komanda.first;
        snowmen[i].top=snowmen[k].top;
        snowmen[i].fromwhere_is_top=snowmen[k].fromwhere_is_top;
        snowmen[i].weight=snowmen[kom.first].weight-snowmen[kom.first].top;
    }
}
long long ans=0;
for(int i=1;i<=n;i++){
    ans+=snowmen[i].weight;
}
/*for(int i=1;i<=n;i++){
    cout<<setw(3)<<i;
}
cout<<endl;
for(int i=1;i<=n;i++){
    cout<<setw(3)<<snowmen[i].weight;
}
cout<<endl;
for(int i=1;i<=n;i++){
    cout<<setw(3)<<snowmen[i].top;
}
cout<<endl;
for(int i=1;i<=n;i++){
    cout<<setw(3)<<snowmen[i].fromwhere_is_top;
}*/
cout<<ans;
return 0;
}
/*
8
0 1
1 5
2 4
3 2
4 3
5 5
6 6
1 3
*/
