#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

using namespace std;

struct node {
    int masa;
    int last;
    int predshestvenik;
};

struct node* init(int masa, int last, int predshestvenik){
    struct node* sm = (struct node*)malloc(sizeof(struct node));
    sm->masa = masa;
    sm->last = masa;
    sm->predshestvenik = predshestvenik;
    return sm;
}

int main()
{
    int n;
    cin >> n;
    vector<struct node*> arr;

    for(int i = 0; i < n; i++){
        int index, masa;
        cin >> index >> masa;
        //printf("%d", index);

        if (index == 0){
            arr.push_back(init(masa, masa, 0));
        }
        else if (masa == 0) {
            int pra_index = arr[index]->predshestvenik;
            arr.push_back(init(arr[index]->masa - arr[index]->last, arr[pra_index]->last, index));
        }
        else arr.push_back(init(arr[index]->masa+masa, masa, index));
    }

    int sum = 0;
    for(int i = 0; i < arr.size(); i++){
        sum += arr[i]->masa;
    }

    printf("%d", sum);
}
