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

struct snowmen{
    int p;
    int m;
}snowmen;

int count_weight(int p, int m, int array, int last_added){
    if(p==0){
        return m;
    }

    if(m==0){
        return array - last_added;
    }else{
        return array + m;
    }
}

int main(){
    int N;
    int weight_sum = 0;

    scanf("%d", &N);

    int array_weight[N];
    int array_last_added[N];

    array_weight[0] = 0;
    array_last_added[0] = 0;

    for(int i=1; i<=N; i++){
        scanf("%d", &snowmen.p);
        scanf("%d", &snowmen.m);

        array_weight[i] = count_weight(snowmen.p, snowmen.m, array_weight[snowmen.p], array_last_added[snowmen.p]);
        array_last_added[i] = snowmen.m;

        weight_sum += array_weight[i];
    }

    printf("%d", weight_sum);
    return 0;
}
