#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int MAX_N = 1e6;
const int INF = 1e9;

std::vector <int> v[MAX_N];

int main (){

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n;
    std::cin>>n;

    ll ans = 0;
    for( int i=1 ; i<=n ; i++ ){
        int p, m;
        std::cin>>p>>m;
        if( m != 0 ){
            for( auto j : v[p] ){ v[i].push_back( j ); ans += j;}
            v[i].push_back( m );
            ans += m;
        }else{
            for( int j=0 ; j<v[p].size()-1 ; j++ ){ v[i].push_back( v[p][j] ); ans += v[p][j];}
        }
    }
    std::cout<<ans<<"\n";

    return 0;
}
