#include<iostream>

using namespace std;

long long snow_man_mas[10003];
int last_snow_ball[10003];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for(int i = 1, p, m;i <= n;i++)
    {
        cin >> p >> m;
        if(m == 0)
        {
            snow_man_mas[i] = snow_man_mas[p];
            snow_man_mas[i] -= last_snow_ball[p];
        }
        else if(m > 0)
        {
            snow_man_mas[i] += m + snow_man_mas[p];
            last_snow_ball[i] = m;
        }
    }
    long long mass = 0;
    for(int i = 1;i < n; i++)
        mass += snow_man_mas[i];

    cout << mass << endl;

    return 0;
}
