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

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

int findb2(int a,int i)
{
    if(num_balls[a] < num_balls[i])
        return balls[a];
    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(int i=1; i<=n; i++)
    {
        cin>>a>>m;
        if(m==0)
        {
            num_balls[i] = num_balls[a]-1;
            values[i] = values[a]-balls[a];
            balls[i]= balls2[a];
            balls2[i] = findb2(a,i);

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

}
