#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LD long double
#define SC(t,x) static_cast<t>(x)
#define AR(t) vector < t >
#define PII pair < int, int >
#define PLL pair < LL, LL >
#define PIL pair < int, LL >
#define PLI pair < LL, int >
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define FF first
#define SS second
#define INIT(ar,val) memset ( ar, val, sizeof ( ar ) )
#define lp(loop,start,end) for ( int loop = start; loop < end; ++loop )
#define lpd(loop,start,end) for ( int loop = start; loop > end; --loop )
#define lpi(loop,start,end) for ( int loop = start; loop <= end; ++loop )
#define lpdi(loop,start,end) for ( int loop = start; loop >= end; --loop )
#define qmax(a,b) (((a)>(b))?(a):(b))
#define qmin(a,b) (((a)<(b))?(a):(b))
#define qabs(a) (((a)>=0)?(a):(-(a)))

const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 150007;
const LL MAXA = SC ( LL, 1e10 );

int n;
int ch[MAXN][2], kch[MAXN];
int fa[MAXN], w[MAXN];
PLL t1[MAXN], t2[MAXN], ta[MAXN];
int k1, k2, ka;
LL ntst;

void init ();
void input ();
void work ();

bool check ( LL tst );

void dfs ( int now, AR(PLL) &ar );

void uni ( AR(PLL) &a1, AR(PLL) &a2, PLL ans[], int &ka );



int main()
{
	init();
	input();
	work();
}



void init ()
{
	// Init Everything Here
	
	ios::sync_with_stdio ( false );
}

void input ()
{
	// input method
	
	scanf ( "%d", &n );
	lpi ( i, 2, n ){
		scanf ( "%d%d", &fa[i], &w[i] );
		ch[fa[i]][kch[fa[i]]++] = i;
	}
}

void work ()
{
	// main work
	
	LL l = 0, r = MAXA, mid;
	while ( l < r ){
		mid = ( l + r ) >> 1;
//		cerr << l << " " << r << " " << mid << endl;
		if ( check ( mid ) ) r = mid;
		else l = mid + 1;
	}
	
	cout << l << endl;
}



bool check ( LL tst )
{
	ntst = tst;
	AR(PLL) ans;
	dfs ( 1, ans );
	return !ans.empty ();
}



void dfs ( int now, AR(PLL) &ar )
{
	if ( !kch[now] ){
		ar.PB ( MP ( w[now], w[now] ) );
		return;
	}
	
	AR(PLL) vs[2];
	lp ( i, 0, 2 ) dfs ( ch[now][i], vs[i] );
	uni ( vs[0], vs[1], t1, k1 );
	lp ( i, 0, 2 ){
		for ( auto &x : vs[i] ) swap ( x.FF, x.SS );
		reverse ( vs[i].begin (), vs[i].end () );
	}
	uni ( vs[0], vs[1], t2, k2 );
	lpi ( i, 1, k2 ) swap ( t2[i].FF, t2[i].SS );
	reverse ( t2+1, t2+1+k2 );
	
	t1[k1+1] = MP ( LINF, 0 );
	t2[k2+1] = MP ( LINF, 0 );
	int i1 = 1, i2 = 1;
	ka = 0;
	while ( i1 <= k1 || i2 <= k2 ){
		if ( t1[i1].FF <= t2[i2].FF ){
			if ( !ka || t1[i1].SS < ta[ka].SS ) ta[++ka] = t1[i1];
			++i1;
		}else{
			if ( !ka || t2[i2].SS < ta[ka].SS ) ta[++ka] = t2[i2];
			++i2;
		}
	}
	ar.resize ( ka );
	lpi ( i, 1, ka ) ar[i-1] = MP ( ta[i].FF + w[now], ta[i].SS + w[now] );
//	cerr << now << " : "; lp ( i, 0, ka ) cerr << "(" << ar[i].FF << "," << ar[i].SS << ") "; cerr << endl;
//	cerr << "t1 : "; lpi ( i, 1, k1 ) cerr << "(" << t1[i].FF << "," << t1[i].SS << ") "; cerr << endl;
//	cerr << "t2 : "; lpi ( i, 1, k2 ) cerr << "(" << t2[i].FF << "," << t2[i].SS << ") "; cerr << endl;	
}



void uni ( AR(PLL) &a1, AR(PLL) &a2, PLL ans[], int &ka )
{
	if ( a1.size () > a2.size () ){
		uni ( a2, a1, ans, ka );
		return;
	}
	
	int i2 = -1, s2 = a2.size ();
	ka = 0;
	for ( auto x : a1 ){
		while ( i2 + 1 < s2 && x.SS + a2[i2+1].FF <= ntst ) ++i2;
		if ( ~i2 ){
			if ( !ka || a2[i2].SS < ans[ka].SS ) ans[++ka] = MP ( x.FF, a2[i2].SS );
		}
	}
}

Measured memory_bytes: 16128