// 1000 пъти аз по тебе

#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int maxn = 1e5+10;

int w[maxn], n;
pair < int, int > p[maxn];
int clone_parent[maxn];

void solve() {

    int ans = 0;
    for (int i = 1 ; i <= n ; ++i) {

        if (p[i].second == 0) {

            p[i].first = clone_parent[p[i].first];
            clone_parent[i] = clone_parent[p[i].first];
            w[i] = w[p[i].first];

        } else {

            clone_parent[i] = p[i].first;
            w[i] = w[p[i].first];
            w[i] += p[i].second;

        }

        ans += w[i];

    }

    cout << ans << '\n';

}

void read() {

    cin >> n;
    for (int i = 1 ; i <= n ; ++i)
        cin >> p[i].first >> p[i].second;

}

void fast_io() {

    ios_base :: sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);

}

signed main () {

    fast_io();
    read();
    solve();

    return 0;

}

/*
10
0 1
1 2
2 3
3 4
4 5
5 0
6 0
7 0
8 0
9 0

*/
