#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(){
	int N;
	scanf("%d", &N);
	std::vector<int> input(N);
	std::vector<int> m(N);
	int result = 0;
	for(int i = 0; i < N; i++){
		int p;
        scanf("%d %d", &p, &m[i]);
		if(p > 0) p--;
		if(m[i] > 0){
			input[i] = input[p] + m[i];
		}else if(m[i] == 0){
			input[i] = input[p] - get_last_ball(m, p);
		}
		printf("snowman #%d = %d\n", i+1, input[i]);
		result += input[i];
	}
	printf("%d", result);
	//system("pause");
	return 0;
}
