#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<bitset>
#include<set>
#define N (140010)
#define P ()
#define M (N<<1)
#define inf (0x7f7f7f7f)
#define rg register int
#define mkp make_pair
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1{
	inline char read(){
		static const int IN_LEN=1000000;
		static char buf[IN_LEN],*s,*t;
		return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
	}
	template<class T>
	inline void read(T &x){
		static bool iosig;
		static char c;
		for(iosig=false,c=read();!isdigit(c);c=read()){
			if(c=='-')iosig=true;
			if(c==-1)return;
		}
		for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
		if(iosig)x=-x;
	}
	inline char readc(char &c){
		for(c=read();!isalpha(c)&&!isdigit(c);c=read())
		if(c==-1)return 0;
	}
	const int OUT_LEN = 10000000;
	char obuf[OUT_LEN],*ooh=obuf;
	inline void print(char c) {
		if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
		*ooh++=c;
	}
	template<class T>
	inline void print(T x){
		static int buf[30],cnt;
		if(x==0)print('0');
		else{
			if(x<0)print('-'),x=-x;
			for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
			while(cnt)print((char)buf[cnt--]);
		}
	}
	inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
}
namespace fastIO2{
	template<class T>
	inline void read(T &x){
		static bool iosig;
		static char c;
		for(iosig=false,c=getchar();!isdigit(c);c=getchar()){
			if(c=='-')iosig=true;
			if(c==-1)return;
		}
		for(x=0;isdigit(c);c=getchar())x=((x+(x<<2))<<1)+(c^'0');
		if(iosig)x=-x;
	}
}
using namespace fastIO1;
LL sum,mid; vector<pair<int,int> >S[N];
pair<int,int>q1[N],q2[N]; int cnt1,cnt2;
int n,m,e[M][2],c[M][2]; bool FL=0;
void add(int x,int y,int z){
	if(!e[x][0])e[x][0]=y,c[x][0]=z;
	else e[x][1]=y,c[x][1]=z;
}
void dfs(int u){
	S[u].clear(); 
	if(!e[u][0]){S[u].push_back(mkp(0,0));return;} 
	int ls=e[u][0],rs=e[u][1],lv=c[u][0],rv=c[u][1];
	dfs(e[u][0]),dfs(e[u][1]),cnt1=cnt2=0; if(FL)return;
//	cout<<u<<" "<<FL<<endl;
	for(int i=0,j=0;i<S[ls].size();i++){
//		if(S[ls][i].first+lv>mid)break;
		while(j<S[rs].size()&&S[rs][j].first+S[ls][i].second+lv+rv<=mid)j++; if(j>0)j--;
		if(S[rs][j].first+S[ls][i].second+lv+rv>mid)continue;
//		cout<<S[ls][i].first+lv<<" "<<S[rs][j].second+rv<<" vvv"<<endl;
		q1[++cnt1]=mkp(S[ls][i].first+lv,S[rs][j].second+rv);
		if(j==S[rs].size()-1)break;
	}
	for(int i=0,j=0;i<S[rs].size();i++){
//		if(S[rs][i].first+lv>mid)break;
		while(j<S[ls].size()&&S[ls][j].first+S[rs][i].second+lv+rv<=mid)j++; if(j>0)j--;
		if(S[ls][j].first+S[rs][i].second+lv+rv>mid)continue;
//		cout<<S[rs][i].first+rv<<" "<<S[ls][j].second+lv<<" uuu"<<endl;
		q2[++cnt2]=mkp(S[rs][i].first+rv,S[ls][j].second+lv);
		if(j==S[ls].size()-1)break;
	}
	int i=1,j=1;
//	cout<<cnt1<<" "<<cnt2<<" faq"<<endl;
	for(;i<=cnt1&&j<=cnt2;){
		if(q1[i].first<q2[j].first)S[u].push_back(q1[i]),i++;
		else if(q1[i].first>q2[j].first)S[u].push_back(q2[j]),j++;
		else S[u].push_back(min(q1[i],q2[j])),i++,j++;
//		cout<<q1[i].first<<" "<<q1[i].second<<" "<<q2[j].first<<" "<<q2[j].second<<endl;
	}
	for(;i<=cnt1;i++)S[u].push_back(q1[i]);
	for(;j<=cnt2;j++)S[u].push_back(q2[j]);
//	for(int i=0;i<S[u].size();i++)cout<<S[u][i].first<<" "<<S[u][i].second<<endl; puts("");
	if(S[u].size()==0)FL=1; 
}
int main(){
	read(n);
	for(int i=2;i<=n;i++){
		int x,y;
		read(x),read(y);
		add(x,i,y),sum+=y; 
	}
	LL L=0,R=sum,ans=sum;
	while(L<=R){
		mid=(L+R)>>1,FL=0,dfs(1);
//		cout<<mid<<endl;
//		cout<<"-----------------------"<<endl;
		if(S[1].size()>0)R=mid-1,ans=mid;else L=mid+1;
	}
	cout<<ans;
}

Measured memory_bytes: 24960