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

struct snm
{
    int mass;
    vector<int> balls;
};

int main()
{
    int n;
    cin >> n;

    struct snm sm[n+1];
    sm[0].mass=0;

    int p,m,res=0;
    //vinagi v nachaloto p shte e 0


    for(int i = 1; i<=n; i++)
    {
        cin >> p >> m;
        if(p==0)
        {
            sm[i].mass=m;
            sm[i].balls.push_back(m);
        }
        if(m>0)
        {
            sm[i].balls=sm[p].balls;
            sm[i].balls.push_back(m);

            sm[i].mass=sm[p].mass+m;
        }
        else //m==0
        {
            sm[i].balls=sm[p-1].balls;
            sm[i].balls.pop_back();

            sm[i].mass=sm[p].mass-sm[p].balls.back();
        }
        res+=sm[i].mass;
    }

    cout << res <<endl;
    return 0;
}
