#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

const ll Size=1e6+10;

bool is_down[Size];
ll p[Size],m[Size],top[Size],weight[10];
map<ll,ll> parent,weight_below;

void solve(){
    ll n,ans=0;
    cin>>n;
    parent[0]=-1;
    for(ll i=1;i<=n;i++){
        cin>>p[i]>>m[i];
        parent[i]=p[i];
        if(m[i]!=0){
            top[i]=m[i];
            weight[i]=weight[parent[i]]+m[i];
            weight_below[i]=parent[i];
        }else{
            top[i]=top[weight_below[parent[i]]];
            weight_below[i]=weight_below[weight_below[parent[i]]];
            weight[i]=weight[parent[i]]-top[parent[i]];
        }
    }
    for(ll i=0;i<=n;i++){
        ans+=weight[i];
    }
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t=1;
    while(t--){
        solve();
    }
    return 0;
}