#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

struct nodes
{
    int mass;
    vector <int> balls;
};
struct nodes *init(struct nodes *a)
{
    struct nodes *new_node = new struct nodes;

    new_node->mass = 0;

    return new_node;
}

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

    int inputs_steps[n];
    long long result = 0;

    struct nodes *a[n];
    for(int i=0; i<n;i++)
    {
        a[i] = 0;
    }
    for(int i=0; i<n;i++)
    {
        int p,m;
        cin>>p>>m;

        inputs_steps[p] = i;

        if(m == 0)
        {
            if(a[i] == 0)
                a[i] = init(a[i]);

            if(a[p-1]!=0)
            {
                a[i]->balls = a[p-1]->balls;
                a[i]->mass = a[p-1]->mass;
            }
            a[i]->mass -= a[i]->balls.back();
            a[i]->balls.pop_back();
        }
        else
        {
            if(a[i] == 0)
                a[i] = init(a[i]);

            if(p > 0 && a[p-1]!=0)
            {
                a[i]->balls = a[p-1]->balls;
                a[i]->mass = a[p-1]->mass;
                a[i]->balls.push_back(m);
                a[i]->mass += m;
            }
            else
                a[i]->balls.push_back(m), a[i]->mass += m;;
        }
       result += a[i]->mass;
    }

    cout<<result;
    return 0;
}
