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

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

    return 0;
}
