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

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

int count_balls(int x, int m){
    if(m>0){
        return x=x+1;
    }else{
        return x=x-1;
    }
}

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

    if(m==0){
        return (array/x)*(x-1);
    }else{
        return array + m;
    }
}

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

    scanf("%d", &N);

    int array_weight[N];
    int array_number_of_balls[N];

    array_weight[0] = 0;
    array_number_of_balls[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_number_of_balls[snowmen.p]);
        array_number_of_balls[i] = count_balls(array_number_of_balls[snowmen.p], snowmen.m);

        weight_sum += array_weight[i];
    }

    printf("%d", weight_sum);
}