#include <bits/stdc++.h>
using namespace std;

long long const maxn = 1000005;
long long values[maxn];
int balls[maxn];
int balls2[maxn];
int p[maxn];
long long num_balls[maxn];
long long n;
int a,m;

int findb2(int a,int i)
{
    if(num_balls[a] == num_balls[i])
        return balls2[a];
    return findb2(p[a],i);
}

int main()
{
    values[0] = 0;
    balls[0] = 0;
    balls2[0] = 0;
    cin>>n;
    for(long long i=1; i<=n; i++)
    {
        cin>>a>>m;
        if(m==0)
        {
            p[i] = a;
            num_balls[i] = num_balls[a]-1;
            values[i] = values[a]-balls[a];
            balls[i]= balls2[a];
            balls2[i] = findb2(a,i);

        }
        else
        {
            p[i] = a;
            num_balls[i] = num_balls[a]+1;
            values[i] = values[a]+m;
            balls[i] = m;
            balls2[i]=balls[a];
        }
    }
    long long sum=0;
    for (long long i=1;i<=n;i++)
    {
        sum+=values[i];
    }
    cout<<sum;
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

}
