#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
	using namespace std;
	#define pll pair<ll,ll>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 202002
	typedef long long ll;
	typedef double db;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(register int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifdef NTFOrz
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		cout<<(clock()-t)/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;
struct hh{int t;ll w;int nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t,ll w)
{
	edge[++ecnt]=(hh){t,w,head[f]};
	head[f]=ecnt;
	edge[++ecnt]=(hh){f,w,head[t]};
	head[t]=ecnt;
}

ll W;
vector<pll>V[sz];
void merge(vector<pll> a,vector<pll> b,vector<pll> &c)
{
	if (!a.size()||!b.size()) return;
	int p=0;
	rep(i,0,(int)a.size()-1)
	{
		while (p<(int)b.size()-1&&a[i].sec+b[p+1].fir<=W) ++p;
		if (a[i].sec+b[p].fir<=W) c.push_back(MP(a[i].fir,b[p].sec));
	}
	p=0;
	rep(i,0,(int)b.size()-1)
	{
		while (p<(int)a.size()-1&&b[i].sec+a[p+1].fir<=W) ++p;
		if (b[i].sec+a[p].fir<=W) c.push_back(MP(b[i].fir,a[p].sec));
	}
	sort(c.begin(),c.end());
	stack<pll>s;
	for (auto x:c)
		if (s.empty()||x.sec<s.top().sec)
			s.push(x);
	c.clear();
	while (!s.empty()) c.push_back(s.top()),s.pop();
	reverse(c.begin(),c.end());
}
void dfs(int x,int fa)
{
	int ls=0,rs=0;
	#define v edge[i].t
	go(x) if (v!=fa)
	{
		dfs(v,x);
		if (!ls) ls=v;
		else rs=v;
		rep(k,0,(int)V[v].size()-1) V[v][k].fir+=edge[i].w,V[v][k].sec+=edge[i].w;
	}
	#undef v
	if (!ls) return (void)(V[x].push_back(MP(0,0)));
	merge(V[ls],V[rs],V[x]);
}

int main()
{
//	file();
	read(n);
	int x,y;
	rep(i,2,n) read(x,y),make_edge(x,i,y);
	ll l=0,r=1e17,ans=r;
	while (l<=r)
	{
		rep(i,1,n) V[i].clear();
		W=(l+r)>>1;dfs(1,0);
		if (V[1].size()) ans=W,r=W-1;
		else l=W+1;
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 38168