#include<iostream>
#include<vector>
using namespace std;

class snowman
{
    public:
        vector<int> balls;
        int total_mass();
        void claim(snowman snmn);

        snowman();
        virtual ~snowman();

    protected:

    private:
};

snowman::snowman()
{
    //ctor
}

snowman::~snowman()
{
    //dtor
}

int snowman::total_mass(){
    int t_mass = 0;
    for(int i = 0; i < balls.size(); i++){
        t_mass += balls[i];
    }
    return t_mass;
}

void snowman::claim(snowman snmn){
    for(int i = 0; i < snmn.balls.size(); i++){
        balls.push_back(snmn.balls[i]);
    }
}


int main(){
    int n, total = 0;
    cin >> n;
    snowman snowmen[n + 1];
    snowmen[0].balls.push_back(0);
    for(int i = 1; i <= n; i++){
        int request_p, mass;
        cin >> request_p >> mass;
        snowmen[i].claim(snowmen[request_p]);
        if(mass > 0){
            snowmen[i].balls.push_back(mass);
        }else{
            snowmen[i].balls.pop_back();
        }
        total += snowmen[i].total_mass();
    }
    cout << total;
    return 0;
}
