#include <bits/stdc++.h>
using namespace std;
class snowman {
public:
    long long mass;
    long long parent;
    long long last;
    snowman(long long mass, long long last, long long parent) {
        this->mass = mass;
        this->last = last;
        this->parent = parent;
    }
};
int main() {

    long long n;
    cin >> n;
    vector<snowman> arr;
    arr.push_back(snowman(0, 0, 0));
    long long res = 0;
    for(long long g = 1; g <= n; g++)
    {
        long long p, m;
        cin >> p >> m;
        if(m > 0) {
            arr.push_back(snowman(arr[p].mass + m, m, p));
        } else {
            arr.push_back(arr[arr[p].parent]);
        }
        res += arr[g].mass;
    }
    cout << res << endl;

    return 0;
}
