#include <iostream>
#include<stdlib.h>

int main()
{
    int n;
    int res=0;
    std::cin>>n;
    int *x=(int*)malloc(n*sizeof(int)),*y=(int*)malloc(n*sizeof(int));
    for(int i=0;i<n;i++)
    {
        std::cin>>x[i]>>y[i];
    }
    int *balls=(int*)malloc(sizeof(int)*(n+1));
    int *weight=(int*)malloc(sizeof(int)*(n+1));
    balls[0]=0;
    weight[0]=0;
    for(int i=1;i<=n;i++)
    {

        if(y[i-1]==0)
        {
            balls[i]=balls[x[i-1]]-1;
            weight[i]=0;
            int j=i;
            for(;y[j-1]==0;j=x[j-1])/*std::cout<<j<<"\n"*/;
            weight[i]=weight[j-1];
        }
        else
        {
            balls[i]=balls[x[i-1]]+1;
            weight[i]=weight[x[i-1]]+y[i-1];
        }
        res+=weight[i];
    }
    std::cout<<res;
    return 0;
}
