#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 200005;
typedef long long ll;
vector<pair<ll,ll> >s[MAXN],a,b;
struct Edge{
	int to,next,w;
}e[MAXN<<1];
int n,head[MAXN],cnt;
inline void insert(int u,int v,int w)
{
	e[++cnt].to=v,e[cnt].next=head[u],head[u]=cnt,e[cnt].w=w;
	e[++cnt].to=u,e[cnt].next=head[v],head[v]=cnt,e[cnt].w=w;
}
inline void add(vector<pair<ll,ll> >&x,pair<ll,ll>a)
{
	if(x.size() && a.second>=x.back().second)return;
	while(x.size() && x.back().first==a.first && x.back().second>=a.second)x.pop_back();
	x.push_back(a);
}
int dfs(int x,int fa,ll mid)
{
	int v1=0,v2=0,w1=0,w2=0;
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==fa)continue;
		if(!v1)v1=v,w1=e[i].w;
		else v2=v,w2=e[i].w;
		if(!dfs(v,x,mid))return 0;
	}
	s[x].clear();
	if(v1){
		if(!s[v1].size() || !s[v2].size())return 0;
		if(s[v1].size()>s[v2].size())swap(v1,v2),swap(w1,w2);
		a.clear(),b.clear();
		int p=0;
		for(int i=0;i<s[v1].size();++i){
			while(p<s[v2].size() && s[v1][i].first+s[v2][p].second+w1+w2>mid)++p;
			if(p>=s[v2].size())break;
			else{
				a.push_back(make_pair(s[v1][i].second+w1,s[v2][p].first+w2));
			}
		}
		p=0;
		for(int i=0;i<s[v1].size();++i){
			while(p+1<s[v2].size() && s[v1][i].second+s[v2][p+1].first+w1+w2<=mid)++p;
			if(s[v1][i].second+s[v2][p].first+w1+w2<=mid){
				b.push_back(make_pair(s[v2][p].second+w2,s[v1][i].first+w1));
			}
		}
		int p1=a.size()-1,p2=b.size()-1;
		while(p1>=0 && p2>=0){
			if(a[p1].first<=b[p2].first)add(s[x],a[p1]),--p1;
			else add(s[x],b[p2]),--p2;
		}
		while(p1>=0)add(s[x],a[p1]),--p1;
		while(p2>=0)add(s[x],b[p2]),--p2;
	}else{
		s[x].push_back(make_pair(0,0));
	}
	return s[x].size();
}
ll sum=0;
int main()
{
	scanf("%d",&n);
	for(int i=2;i<=n;++i){
		int f,w;
		scanf("%d%d",&f,&w),sum+=w;
		insert(f,i,w);
	}
	ll l=0,r=sum,mid,ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(dfs(1,0,mid))r=mid-1,ans=mid;
		else l=mid+1;
	}
	printf("%lld\n",ans);
	return 0;
}
