#include<iostream>
#include<vector>
using namespace std;
vector<int> a[1000001];
int main(){
    long long N,b,sum=0,p,k=0;
    cin>>N;
    for(int i=1;i<=N;i++){
        cin>>p>>b;
        if(b>0){
            for(int j=0;j<a[p].size();j++){
                a[i].push_back(a[p][j]);
                k+=a[p][j];
            }
            a[i].push_back(b);
            sum+=k+b;
        }
        if(b==0){
            for(int j=0;j<a[p].size()-1;j++){
                a[i].push_back(a[p][j]);
                k+=a[p][j];
            }
            sum+=k;
        }
        k=0;
    }
    cout<<sum;
return 0;
}
