#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> 
#define N 200005
using namespace std;
typedef long long ll;
const ll inf=1e12;
struct P{
	ll a,b;
	P(){}
	P(ll x,ll y){a=x;b=y;}
}a[N],b[N];
vector<P> v[N];
int n,son[N][2];
ll w[N][2],mid,g[N];
void dfs(int k){
	v[k].clear();
	if(!son[k][0]){
		v[k].push_back(P(0,0));
		return;
	}
	int l=son[k][0],r=son[k][1];
	ll lim=mid-w[k][0]-w[k][1],wl=w[k][0],wr=w[k][1];
	if(lim<0) return;
	dfs(l);
	dfs(r);
	if(v[l].size()>v[r].size()) swap(l,r),swap(wl,wr);
	int vl=v[l].size(),vr=v[r].size();
	ll mn=inf;int tota=0,totb=0;
	for(int i=0,pos=0;i<vl;i++){
		while(pos<vr&&v[l][i].b+mn>lim) {mn=min(mn,v[r][pos].a);pos++;}
		if(v[l][i].b+mn>lim) break;
		a[++tota]=P(v[l][i].a+wl,v[r][pos-1].b+wr);
	}
	mn=inf;
	for(int i=0;i<vl;i++) g[i]=inf; 
	for(int i=0,pos=0;i<vr;i++){
		while(pos<vl&&v[r][i].b+mn>lim){mn=min(mn,v[l][pos].a);pos++;}
		if(v[r][i].b+mn>lim) break;
		g[pos-1]=min(g[pos-1],v[r][i].a+wr);
	}
	for(int i=0;i<vl;i++) if(g[i]!=inf) b[++totb]=P(g[i],v[l][i].b+wl);
	int L=1,R=1;
	while(L<=tota||R<=totb){
		if(R>totb||(L<=tota&&a[L].b<b[R].b)){
			v[k].push_back(a[L]);L++;
		}else v[k].push_back(b[R]),R++;
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1,x,val;i<n;i++){
		scanf("%d%d",&x,&val);
		if(!son[x][0]) son[x][0]=i+1,w[x][0]=val;
		else son[x][1]=i+1,w[x][1]=val;
	}
	ll l=0,r=inf,ans;
	while(l<=r){
		mid=l+r>>1;
		dfs(1);
		if(v[1].size()) ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
} 