#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<ll, ll>
#define pb push_back
#define int long long

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 15 * 1e4;

vector<pii> adj[N], s[N], t1, t2;
int n;

void dfs(int v, int p, ll lim){
	int u1 = -1, u2 = -1 , w1 = -1, w2 = -1;
	for(auto U : adj[v]){
		int u = U.F, w = U.S;
		if(u == p)continue;
		dfs(u, v, lim);
		if(u1 == -1)u1 = u, w1 = w;
		else u2 = u, w2 = w;
	}

	if(u1 == -1){
		s[v].pb({0, 0});
		return ;
	}

	int sz1 = s[u1].size(), sz2 = s[u2].size();

	t1.clear(); t2.clear();

	for(int ptr = 0,i=0; i<sz1; i++){
		int b = -1;
		while(ptr < sz2 && s[u1][i].S + s[u2][ptr].F + w1 + w2 <= lim){
			b = ptr; ptr++;
		}
		if(b == -1)continue;
		t1.pb({s[u1][i].F + w1, s[u2][b].S + w2});
	}

	for(int ptr = 0, i=0; i<sz2; i++){
		int b = -1;
		while(ptr < sz1 && s[u2][i].S + s[u1][ptr].F + w1 + w2 <= lim){
			b = ptr; ptr++;
		}
		if(b == -1)continue;
		t2.pb({s[u2][i].F + w2, s[u1][b].S + w1});
	}

	s[v].resize((int)t1.size() + (int)t2.size());

	merge(t1.begin(), t1.end(), t2.begin(), t2.end(), s[v].begin());

//	sort(s[v].begin(), s[v].end());
//	s[v].resize(unique(s[v].begin(), s[v].end()) - s[v].begin());

	return ;
}

bool check(ll lim){
	for(int i=1; i<=n; i++)
		s[i].clear();

	dfs(1, -1, lim);

	if((int)s[1].size() > 0)return true;
	return false;
}

int32_t main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;

	for(int i=1; i<n; i++){
		int a, v; cin >> a >> v;
		adj[i + 1].pb({a, v}); adj[a].pb({i + 1, v});
	}

	ll lo = -1, hi = 1LL * n * N;

	while(hi - lo > 1){
		ll mid = (lo + hi)/2;
		if(check(mid))hi = mid;
		else lo = mid;
	}


	cout << hi << endl;
	return 0;
}