#include <cstdio>
#include <vector>
#define FOR(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
struct T{
	long long a, b;
	T(long long _a = 0, long long _b = 0){
		a = _a;
		b = _b;
	}
}A[200010], B[200010], C[200010];
vector<T> f[200010];
int cnta, cntb;
int n, x;
int a[200010];
int tr[200010][2];
long long mx, dep[200010];
void pre(int x){
	if (tr[x][0]){
		dep[tr[x][0]] = dep[x] + a[tr[x][0]];
		dep[tr[x][1]] = dep[x] + a[tr[x][1]];
		pre(tr[x][0]);
		pre(tr[x][1]);
	}
}
void dfs(int x){
	if (!tr[x][0]){
		f[x].push_back(T(dep[x], dep[x]));
		return;
	}
	int ls = tr[x][0], rs = tr[x][1], l = 0;
	dfs(ls);
	dfs(rs);
	cnta = 0;
	for (int i = 0; i < f[ls].size(); i++){
		int key = -1;
		while (l < f[rs].size() && f[ls][i].b + f[rs][l].a - 2 * dep[x] <= mx){
			key = l;
			l++;
		}
		if (key != -1) A[++cnta] = T(f[ls][i].a, f[rs][key].b);
	}
	l = 0;
	cntb = 0;
	for (int i = 0; i < f[rs].size(); i++){
		int key = -1;
		while (l < f[ls].size() && f[rs][i].b + f[ls][l].a - 2 * dep[x] <= mx){
			key = l;
			l++;
		}
		if (key != -1) B[++cntb] = T(f[rs][i].a, f[ls][key].b);
	}
	f[ls].clear();
	f[rs].clear();
	int l1 = 1, l2 = 1, cntc = 0;
	FOR(i, 1, cnta + cntb){
		if (l2 > cntb || (l1 <= cnta && l2 <= cntb && A[l1].a < B[l2].a)){
			if (!cntc) C[++cntc] = A[l1++];
			else if (C[cntc].a == A[l1].a){
				C[cntc].b = min(C[cntc].b, A[l1++].b);
			}
			else C[++cntc] = A[l1++];
		}
		else{
			if (!cntc) C[++cntc] = B[l2++];
			else if (C[cntc].a == B[l2].a){
				C[cntc].b = min(C[cntc].b, B[l2++].b);
			}
			else C[++cntc] = B[l2++];
		}
	}
	f[x].clear();
	FOR(i, 1, cntc) f[x].push_back(C[i]);
}
bool judge(long long x){
	mx = x;
	dfs(1);
	if (f[1].size()) return 1;
	return 0;
}
int main(){
	scanf("%d", &n);
	FOR(i, 2, n){
		scanf("%d%d", &x, &a[i]);
		if (tr[x][0]) tr[x][1] = i;
		else tr[x][0] = i;
		mx += a[i];
	}
	pre(1);
	long long l = 0, r = mx, mid, ans = mx;
	while (l <= r){
		mid = (l + r) >> 1;
		if (judge(mid)){
			r = mid - 1;
			ans = mid;
		}
		else l = mid + 1;
	}
	printf("%lld\n", ans);
	return 0;
}