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

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

    return 0;
}
