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

    vector<snowman> number;
    stack<int> q;
    number.push_back(snowman(0, q));
    int n;
    cin >> n;
    int result = 0;
    for(int g = 0; g < n; g++) {
        int m, p;
        cin >> p >> m;
        if(m > 0) {
            number.push_back(snowman(number.at(p).mass + m, number.at(p).balls));
            number.at(g + 1).balls.push(m);
        } else {
            number.push_back(snowman(number.at(p).mass - number.at(p).balls.top(), number.at(p).balls));
            number.at(g + 1).balls.pop();
        }
        result += number.at(g + 1).mass;
    }
    cout << result << endl;



    return 0;
}
