#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define re(i,a,b) for(int i=(a);i<(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define ergomap(i,a) for(map<int,int>::iterator i=a.begin();i!=a.end();i++)
#define ergoset(i,a) for(set<int>::iterator i=a.begin();i!=a.end();i++)
#define ergo(i,a) for(int i=0;i<a.size();i++)
#define clr(a) memset(a,0,sizeof(a));
#define il inline
#define sz(a) ((int)s.size())
#define pb push_back 
#define w1 first
#define w2 second
#define addm(a,b,c) {a=a+b;if(a>=c)a-=c;}
typedef long long ll;typedef long double ld;typedef unsigned long long ull;
typedef pair<ll,ll> pa;
const int N=5e5+50,M=1e6+5,INF=1e9,mod=1e9+7;
const ll linf=1e18;
const double eps=1e-8l;
il int gmin(int &a,int b){if(a>b)a=b;}il ll gmin(ll &a,ll b){if(a>b)a=b;}il int gmax(int &a,int b){if(a<b)a=b;}il ll gmax(ll &a,ll b){if(a<b)a=b;}
il int max(int a,int b,int c){if(b>a)a=b;if(c>a)a=c;return a;}il int max(int a,int b){if(a>b)return a;return b;}il int min(int a,int b){if(a>b)return b;return a;}
il int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
il void read(ll&x){ll f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}x=t*f;}il ll read(ll&x,ll&y){read(x);read(y);}
il void read(int&x){int f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}x=t*f;}il int read(int&x,int&y){read(x);read(y);}
il void read(int&a,int&b,int&c){read(a);read(b);read(c);}il void read(ll&a,ll&b,ll&c){read(a);read(b);read(c);}
il ll qpow(ll a,ll b,ll p){ll ret=1;for(;b;b>>=1,a=a*a%p)if(b&1)ret=ret*a%p;return ret;}
il ll qmul(ll a,ll b,ll p){ll ret=0;for(;b;b>>=1,a=(a<<1)%p)if(b&1)addm(ret,a,p);return ret;}
il void judge(){
	freopen("travel10.in","r",stdin);
	freopen("my.out","w",stdout);}
il void gen(){freopen("data.in","w",stdout);}
const double pi=acos(-1);
int n,m;
vector<pa>s[N];
int c[N][2],v[N][2];
bool gao(int x,ll mid){
	if(!c[x][0]){s[x].pb(pa(0,0));return 1;}
	int c0=c[x][0],c1=c[x][1];
	if(!gao(c0,mid))return 0;
	if(!gao(c1,mid))return 0;
	re(i,0,s[c0].size())s[c0][i]=pa(s[c0][i].w1+v[x][0],s[c0][i].w2+v[x][0]);
	re(i,0,s[c1].size())s[c1][i]=pa(s[c1][i].w1+v[x][1],s[c1][i].w2+v[x][1]);
	vector<pa>h1,h2,g;
	for(int i=0,j=0;i<s[c0].size();i++){
		while(j<s[c1].size()&&s[c0][i].w2+s[c1][j].w1<=mid)j++;
		if(j&&s[c0][i].w2+s[c1][j-1].w1<=mid)h1.pb(pa(s[c0][i].w1,s[c1][j-1].w2));
	}
	for(int i=0,j=0;i<s[c0].size();i++){
		while(j<s[c1].size()&&s[c1][j].w2+s[c0][i].w1>mid)j++;
		if(j==s[c1].size())break;
		h2.pb(pa(s[c1][j].w1,s[c0][i].w2));
	}
	for(int i=0,j=0;i<h1.size()||j<h2.size();){
		if(i==h1.size())g.pb(h2[j++]);else
		if(j==h2.size())g.pb(h1[i++]);else
		if(h1[i].w1<h2[j].w1)g.pb(h1[i++]);else g.pb(h2[j++]);
	}
	re(i,0,g.size())if(s[x].size()==0||g[i].w2<s[x][s[x].size()-1].w2)s[x].pb(g[i]);
	
	int i=x;/*
		if(i==1)puts("\n");
		printf("%d:",i);
		for(int j=0;j<s[i].size();j++)printf("(%lld %lld)",s[i][j].w1,s[i][j].w2);
		puts("\n");*/
	
	return s[x].size()>0;
}
int main(){//judge();
	read(n);
	rep(i,2,n){
		int x,y;read(x,y);
		int o=0;
		if(c[x][0])o++;
		c[x][o]=i;v[x][o]=y;
	}
	ll l=0,r=1e10,res=1e10;
	while(l<=r){
		ll mid=(l+r)>>1;
		rep(i,1,n)s[i].clear();
		if(gao(1,mid))r=(res=mid)-1;else
			l=mid+1;
	}cout<<res<<endl;
}

Measured memory_bytes: 44292