#include <iostream>
#include <stack>
#include <vector>

#define endl '\n'

using namespace std;

const int MAXN = 1e6 + 3;
int64_t weights[MAXN];
pair<int, int> pos[MAXN];
vector<vector<pair<int, int>>> snowballs;

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

    int n;
    cin >> n;

    weights[0] = 0;
    pos[0] = {-1, -1};

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

        weights[i] = weights[p];

        int seq = pos[p].first;
        int ind = pos[p].second;

        if (m == 0) {
            weights[i] -= 1ll * snowballs[seq][ind].first;
            --ind;

            while (ind == 0) {
                pair<int, int> prev_seq = snowballs[seq][ind];
                seq = prev_seq.first;
                ind = prev_seq.second;
            }
        } else {
            weights[i] += 1ll * m;

            if (seq == -1 or ind + 1 < (int) snowballs[seq].size()) {
                snowballs.push_back({{seq, ind}});
                seq = (int) snowballs.size() - 1;
                ind = 0;
            }

            snowballs[seq].push_back({m, -1});
            ++ind;
        }

        /*cout << i << " -> Element witn index " << ind << " in seq: ";
        if (seq != -1) {
            for (pair<int, int> nz : snowballs[seq]) {
                cout << "(" << nz.first << ", " << nz.second << ") ";
            }
        }
        cout << endl;*/

        pos[i] = {seq, ind};
    }

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

    cout << total_weight << endl;

return 0;
}
