#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d", &n);
    int arr[n][2];
    int sum = 0;
    int snowmens[n][2];
    for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &arr[i][0], &arr[i][1]);
            snowmens[i][0] = 0;
            snowmens[i][1] = 0;
        }
    snowmens[0][0] = arr[0][1];
    snowmens[0][1] = arr[0][1];
    sum += snowmens[0][0];
    for(int i = 1; i < n; i++)
        {
            if(arr[i][1] == 0)
                {
                    snowmens[i][0] = snowmens[arr[i][0] - 1][0] - snowmens[arr[i][0] - 1][1];
                    sum += snowmens[i][0];
                    continue;
                }
            snowmens[i][0] = snowmens[arr[i][0] - 1][0] + arr[i][1];
            snowmens[i][1] = arr[i][1];
            sum += snowmens[i][0];
        }
    printf("%d", sum);
    return 0;
}