#include <stdio.h>
#include <stdlib.h>
int N[1000000][2] = {{0, 0}};
long long mass = 0;
int getMass(int p, int m, int n)
{
    if(m)
    {
        N[n][0] = N[p][0] + m;
        N[n][1] = m;
        mass += N[n][0];
        return N[n][0];
    }
    else
    {
        N[n][0] = N[p][0] - N[p][1];
        N[n][1] = N[p][0] - N[p][1];
        mass += N[n][0];
        return N[n][0];
    }
}
int main()
{
    int p, m, n;
    scanf("%d", &n);
    for(int i = 1;i <= n;i++)
    {
        scanf("%d%d", &p, &m);
        getMass(p, m, i);
    }
    printf("%lld",mass);
    return 0;
}
