#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const ll inf=0x3f3f3f3f3f3f3f3fll;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
//const int mo=0;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}

int n;
int pre[222222],wei[222222];
vi chd[222222];

struct node{ll i,o;}buf[14222222];
int operator<(const node&a,const node&b){
	if(a.i==b.i)return a.o>b.o;
	else return a.i<b.i;
}
node *ptr=buf;

struct lis{
	node* a;
	int cnt;  //a[0]..a[cnt-1]
};

lis f[222222];


node tmp[422222],sec[422222],ge[444444];

inline ll putin(const lis &a,ll in){
	int l=0,r=a.cnt-1;
	while(l<=r){
		int mid=l+r>>1;
		if(a.a[mid].i>=in)r=mid-1;
		else l=mid+1;
	}
	if(l==a.cnt)return inf;
	return a.a[l].o;
}
inline ll maxin(const lis &a,ll ou){
	int l=0,r=a.cnt-1;
	while(l<=r){
		int mid=l+r>>1;
		if(a.a[mid].o<=ou)l=mid+1;
		else r=mid-1;
	}
	if(r==-1)return -inf;
	return a.a[r].i;
}
lis merg(lis a,int wa,lis b,int wb,ll lim){
	if(a.cnt>b.cnt){
		swap(a,b);
		swap(wa,wb);
	}
	int top=0;

	rep(i,0,a.cnt){
		ll in=a.a[i].i-wa;
		ll ou=putin(b,a.a[i].o+wa+wb)+wb;
		if(ou>=inf)continue;
		if(in<=-inf)continue;
		tmp[top++]=(node){in,ou};
	}

	int t2=0;
	rep(i,0,a.cnt){
		ll ou=a.a[i].o+wa;
		ll in=a.a[i].i-wa-wb;
		ll canin=maxin(b,in)-wb;

		if(canin<=-inf)continue;
		sec[t2++]=(node){canin,ou};
	}
	node* cur=ptr;

	int to=0;
	int sop=0,s2=0;
	int xxx=0;
	while(sop<top || s2<t2){
		if(s2==t2 || sop!=top && tmp[sop]<sec[s2])
			ge[xxx++]=tmp[sop++];
		else ge[xxx++]=sec[s2++];
	}
	per(i,0,xxx-1)upmin(ge[i].o,ge[i+1].o);
	top=0;
	rep(i,0,xxx)if(i==xxx-1 || ge[i].o!=ge[i+1].o){
		cur[top++]=ge[i];
	}
	ptr=cur+top;

	return (lis){cur,top};
}
void dfs(int u,ll lim){
	if(siz(chd[u])==0){
		node *cur=ptr;
		*ptr++=(node){lim,0};///
		f[u]=(lis){cur,1};
	}else{
		dfs(chd[u][0],lim);
		dfs(chd[u][1],lim);
		f[u]=merg(f[chd[u][0]],wei[chd[u][0]],f[chd[u][1]],wei[chd[u][1]],lim);
	}
}
bool check(ll lim){
	ptr=buf;
	dfs(1,lim);
	rep(i,0,f[1].cnt)if(f[1].a[i].o<inf)return 1;
	return 0;
}
int main()
{
#ifdef JCVB
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int _time_jc=clock();
#endif
	gn(n);
	rep(i,2,n+1){
		gn(pre[i]),gn(wei[i]);
		chd[pre[i]].pb(i);
	}
	ll l=0,r=1ll<<36;
	while(l<=r){
		ll mid=l+r>>1;
		if(check(mid))r=mid-1;
		else l=mid+1;
	}
	printf(lld"\n",l);
#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}




Measured memory_bytes: 30080