#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
#define endl '\n'
using namespace std;
long long a[2000003], 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;

    cin>>n;

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

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


    }
    cout<<s<<endl;


}


