#include <cstdio>
#include <vector>
using namespace std;
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
struct Point{
	long long x, y;
	inline bool operator <(const Point &p) const {
		return x == p.x ? y < p.y : x < p.x;
	}
	inline bool operator ==(const Point &p) const {
		return x == p.x && y == p.y;
	}
};
typedef vector<Point> V;
V f[131103];
V::iterator it;
int lc[131103], rc[131103], tmp;
long long dep[131103], tmpx1[131103], tmpy1[131103], tmpx2[131103], tmpy2[131103], MIDI, maxdep;
void dfs(register int o){
	if (lc[o] == 0) {
		cmax(maxdep, dep[o]);
		f[o].push_back((Point){dep[o], dep[o]});
		return ;
	}
	dep[lc[o]] += dep[o];
	dfs(lc[o]);
	dep[rc[o]] += dep[o];
	dfs(rc[o]);
	return ;
}
int dp(register int o){
	if (lc[o] == 0) return 0;
	//fprintf(stderr, "%d\n", o);
	if (dp(lc[o])) return 1;
	if (dp(rc[o])) return 1;
	register int lcc = lc[o], rcc = rc[o], len, p1, p2, l1, l2, lbd, rbd, mid;
	register long long now = MIDI + (dep[o] << 1), val;
	if (f[lcc].size() > f[rcc].size()) {
		tmp = lcc;
		lcc = rcc;
		rcc = tmp;
	}
	//len = f[lcc].size();
	l1 = l2 = 0;
	for (it = f[lcc].begin(); it != f[lcc].end(); ++it) {
		val = now - it->x;
		if (f[rcc].front().y <= val) {
			++l1;
			tmpx1[l1] = f[rcc].front().x;
			tmpy1[l1] = it->y;
		}
		else if (f[rcc].back().y <= val) {
			lbd = 0, rbd = f[rcc].size() - 1;
			while (rbd - lbd > 1) {
				if (f[rcc][mid = lbd + rbd >> 1].y <= val) rbd = mid;
				else lbd = mid;
			}
			++l1;
			tmpx1[l1] = f[rcc][rbd].x;
			tmpy1[l1] = it->y;
		}
		
		val = now - it->y;
		if (f[rcc].back().x <= val) {
			++l2;
			tmpx2[l2] = it->x;
			tmpy2[l2] = f[rcc].back().y;
		}
		else if (f[rcc].front().x <= val) {
			lbd = 0, rbd = f[rcc].size() - 1;
			while (rbd - lbd > 1) {
				if (f[rcc][mid = lbd + rbd >> 1].x <= val) lbd = mid;
				else rbd = mid;
			}
			++l2;
			tmpx2[l2] = it->x;
			tmpy2[l2] = f[rcc][lbd].y;
		//fprintf(stderr, "GG");
		}

	}
	if (l1 == 0 && l2 == 0) return 1;
	f[o].clear();
	//fprintf(stderr, "%d %d %d\n", o, l1, l2);
	//for (p1 = 1; p1 <= l1; ++p1) fprintf(stderr, "A#%d: (%d, %d)\n", p1, tmpx1[p1], tmpy1[p1]);
	//for (p2 = 1; p2 <= l2; ++p2) fprintf(stderr, "B#%d: (%d, %d)\n", p2, tmpx2[p2], tmpy2[p2]);
	p1 = p2 = 1;
	while (p1 <= l1 && p2 <= l2) {
		if (tmpx1[p1] <= tmpx2[p2]) {
			if (f[o].empty() || tmpy1[p1] < f[o].back().y) f[o].push_back((Point){tmpx1[p1], tmpy1[p1]});
			++p1;
		}
		else {
			if (f[o].empty() || tmpy2[p2] < f[o].back().y) f[o].push_back((Point){tmpx2[p2], tmpy2[p2]});
			++p2;
		}
	}
	if (p1 <= l1) {
		while (p1 <= l1) {
			if (f[o].empty() || tmpy1[p1] < f[o].back().y) f[o].push_back((Point){tmpx1[p1], tmpy1[p1]});
			++p1;
		}
	}
	else {
		while (p2 <= l2) {
			if (f[o].empty() || tmpy2[p2] < f[o].back().y) f[o].push_back((Point){tmpx2[p2], tmpy2[p2]});
			++p2;
		}
	}
	//if (f[o].empty()) return 1;
	return 0;
}
int fastin(){
	register int i; register char ch;
	while (ch = getchar(), ch < '0' || ch > '9');
	i = ch - '0';
	while (ch = getchar(), ch >= '0' && ch <= '9') i = i * 10 + ch - '0';
	return i;
}
int main(){
	register int n, i, a;
	register long long l, r;
	n = fastin();
	for (i = 2; i <= n; ++i) {
		a = fastin(), dep[i] = fastin();
		if (lc[a]) rc[a] = i;
		else lc[a] = i;
	}
	dfs(1);
	l = 0, r = maxdep << 1;
	while (r - l > 1) {
		//fprintf(stderr, "%lld %lld %lld\n", l, r, MIDI);
		MIDI = l + r >> 1;
		if (dp(1)) l = MIDI;
		else r = MIDI;
	}
	printf("%lld", r);
	return 0;
}

Measured memory_bytes: 31144