#include<iostream>
#define endl '\n'
using namespace std;
long long p[1000005],m[1000005],link[1000005],t[1000005],n,rez=0;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;

    for(long long i=1;i<=n;i++)
    {
        cin>>p[i]>>m[i];
        if(m[i]>0)
        {
            link[i]=p[i];
            t[i]=t[link[i]]+m[i];
        }
        else
        {
            link[i]=p[i];
            long long in=p[i];
            while(in>0 && m[in]==0)
            {
                in=link[in];
            }
            if(in>0) in=link[in];
            link[i]=in;

            t[i]=t[link[i]];
        }
        rez+=t[i];
    }
    cout<<rez<<endl;

}

