#include <bits/stdc++.h>
#define endl '\n'

int n, p, m, ans;
std::stack<int> snowmen[1000001];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;
    for (int i = 0; i < n; ++ i) {
        std::cin >> p >> m;
        snowmen[i + 1] = snowmen[p];
        if (m == 0) {
            snowmen[i + 1].pop();
        } else {
            snowmen[i + 1].push(m);
        }
    }
    for (int i = 0; i <= n; ++ i) {
        while (!snowmen[i].empty()) {
            ans += snowmen[i].top();
            snowmen[i].pop();
        }
    }
    std::cout << ans << endl;
    return 0;
}
