#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
long long fath[1000003], sum[1000003];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long n,i,j, s=0, m, a;

    cin>>n;

    for(i=1;i<=n;i++)
    {
        cin>>a>>m;

        if(m>0)
        {
            fath[i]=a;
            sum[i]=sum[fath[i]]+m;
        }
        else
        {
            sum[i]=sum[fath[a]];
            fath[i]=fath[fath[a]];
        }
        s+=sum[i];

    }
    cout<<s<<endl;


}


