#include <iostream>
#include <stack>

#define endl '\n'

using namespace std;

const int MAXN = 1e6 + 3;
int64_t weights[MAXN];
stack<int> snowballs[MAXN];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        int p, m;
        cin >> p >> m;

        snowballs[i] = snowballs[p];
        weights[i] = weights[p];

        if (m == 0) {
            weights[i] -= 1ll * snowballs[i].top();
            snowballs[i].pop();
        } else {
            weights[i] += 1ll * m;
            snowballs[i].push(m);
        }
    }

    int64_t total_weight = 0;
    for (int i = 0; i <= n; ++i) {
        total_weight += weights[i];
    }

    cout << total_weight << endl;
return 0;
}
