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

const int maxn = 1000000 + 3;
int n;

int gl_obshto = 0;
struct Snowman{
    int otkoi;
    int top;
    int lastuveltop;
    int res;
    //bool m;
};

Snowman snowmen[maxn];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    snowmen[0].otkoi = -1;
    snowmen[0].top = 0;
    snowmen[0].lastuveltop = -1;
    snowmen[0].res = 0;
    //snowmen[0].m = true;

    int p, m;
    for(int i=1;i<=n;i++){
        cin >> p >> m;

        //snowmen[i].m = (m != 0);
        snowmen[i].res = snowmen[p].res;
        gl_obshto += snowmen[i].res;
        snowmen[i].otkoi = p;

        if(m == 0){
            snowmen[i].lastuveltop = snowmen[p].lastuveltop;
            snowmen[i].top = snowmen[snowmen[i].lastuveltop].top;
            snowmen[i].res -= snowmen[p].top;
            gl_obshto -= snowmen[i].top;
        }else{
            snowmen[i].lastuveltop = i;
            snowmen[i].top = m;
            snowmen[i].res += snowmen[i].top;
            gl_obshto += snowmen[i].top;
        }

    }

    cout << gl_obshto;

    return 0;
}