#include <stdio.h>
#include <vector>
//#include <windows.h>

#define ll long long

int get_last_ball(std::vector<int> m, int index){
	for(int i = index; i >= 0; i--){
		if(m[i] != 0) return m[i];
	}
	return 0;
}

int main(){
	ll N;
	scanf("%lld", &N);
	std::vector<ll> input[N + 1];
	input[0] = std::vector<ll>(1, 0);
	ll result = 0;
	for(ll i = 1; i <= N; i++){
		ll p, m;
        scanf("%lld %lld", &p, &m);
		//if(p > 0) p--;
		if(i != 0) input[i] = std::vector<ll>(input[p].begin(), input[p].end());
		if(m > 0){
			input[i][0] = input[i][0] + m;
			input[i].push_back(m);
		}else if(m == 0){
			input[i][0] = input[i][0] - input[i].back();
			input[i].pop_back();
		}
		//printf("snowman #%d = %d\n", i+1, input[i][0]);
		result += input[i][0];
	}
	/*
	for(int i = 0; i < N; i++){
		for(int j = 0; j < input[i].size(); j++){
			printf("%lld ", input[i][j]);
		}
		printf("\n");
	}
	*/
	printf("%lld", result);
	//system("pause");
	return 0;
}
