#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 j = 0; j < snowmen[request_p].balls.size(); j++){
        snowmen[i].balls.push_back(snowmen[request_p].balls[j]);
    }
}*/


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;
        for(int j = 0; j < snowmen[request_p].balls.size(); j++){
            snowmen[i].balls.push_back(snowmen[request_p].balls[j]);
        }
        if(mass > 0){
            snowmen[i].balls.push_back(mass);
        }else{
            snowmen[i].balls.pop_back();
        }
    }
    /*for(int i = 1; i <= n; i++){
        total += snowmen[i].total_mass();
    }*/
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < snowmen[i].balls.size(); j++){
            total += snowmen[i].balls[j];
        }
    }
    cout << total;
    return 0;
}
