#include<bits/stdc++.h>
using namespace std;
vector< tuple<int,int,int> > v;
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        if(a==0){
            v.push_back(make_tuple(b,b,a));
        }else{
            if(b!=0){
                v.push_back(make_tuple(get<0>(v[a-1])+b,b,a));
            }else{
                v.push_back(make_tuple(get<0>(v[a-1])-get<1>(v[a-1]),get<1>(v[get<2>(v[a-1])-1]),a));
            }
        }
        //cout<<get<0>(v[v.size()-1])<<" "<<get<1>(v[v.size()-1])<<" "<<get<2>(v[v.size()-1]);
    }
    int s=0;
    for(int i=0;i<v.size();i++){
        s=s+get<0>(v[i]);
    }
    cout<<s;
    /*ios_base::sync_with_stdio(false);
    cin.tie(0);
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    */
return 0;
}
