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

long long snow_man_mas[1000003];
vector <int> last_snow_ball[1000003];

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].front();
            last_snow_ball[p].pop_back();
        }
        else if(m > 0)
        {
            snow_man_mas[i] += m + snow_man_mas[p];
            last_snow_ball[i].push_back(m);

            for(int j = 0;j < last_snow_ball[p].size();j++)
                last_snow_ball[i].push_back(last_snow_ball[p][j]);
        }
    }
    long long mass = 0;
    for(int i = 1;i <= n; i++)
        mass += snow_man_mas[i];

    cout << mass;

    return 0;
}
