#include<iostream>
#include<vector>
using namespace std;
const int maxn=1000009;
long long n,ans;
pair<int,vector<int>> sm[maxn]; /// first = weight, second = ne samo last
void f(int a,int b){ /// kopira vector b v a
    for(auto to:sm[b].second) sm[a].second.push_back(to);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    int p,m;
    for(int i=1;i<=n;i++){
        cin>>p>>m;
        if(m==0){
            sm[i].first=sm[p].first;
            sm[i].first-=sm[p].second[sm[p].second.size()-1];
            f(i,p);
            sm[i].second.erase(sm[i].second.end()-1,sm[i].second.end());
        }else{
            sm[i].first=sm[p].first;
            sm[i].first+=m;
            f(i,p);
            sm[i].second.push_back(m);
        }
        ///cout<<i<<" "<<sm[i].first<<"\n";
        ans+=sm[i].first;
    }
    cout<<ans<<"\n";
    return 0;
}
