#include <iostream>
using namespace std;
struct snow{
    long long kg;
    int prev;
};
snow man[1000010];
int main () {
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    man[0].kg = 0;
    man[0].prev = 0;
    int n;
    cin >> n;
    n++;
    long long sum = 0;
    for (int i = 1; i < n; i++) {
        int p;
        long long m;
        cin >> p >> m;
        if (m == 0) {
            man[i].kg = man[man[p].prev].kg;
            man[i].prev = man[man[p].prev].prev;
        } else {
            man[i].kg = man[p].kg + m;
            man[i].prev = p;
        }
        sum += man[i].kg;
    }
    cout << sum;
    return 0;
}
