#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::pair<int, int> sm[MAX_N]; // sum, top
int par[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;
        par[i] = p;
        if( m != 0 ){
            sm[i].first = sm[p].first + m;
            sm[i].second = m;
        }else{
            sm[i].first = sm[p].first - sm[p].second;
            sm[i].second = sm[par[p]].second;
        }
        ans += sm[i].first;
        //std::cerr<<sm[i].first<<" ";
    }
    std::cout<<ans<<"\n";

    return 0;
}
