#include<bits/stdc++.h>
using namespace std;
pair<long long,long long> mas[1000001];
int main(){
    long long N,a,b,ans=0;
    cin>>N;
    for(long long i=1;i<=N;i++){
        cin>>a>>b;
        if(b==0){
            mas[i].first=mas[mas[a].second].first;
            mas[i].second=mas[mas[a].second].second;
        }else{
            mas[i].first=mas[a].first+b;
            mas[i].second=a;
        }
        ans+=mas[i].first;
    }
    cout<<ans<<endl;
    return 0;
}
