#include<cstdio>
#include<algorithm>
#include<vector>
using std::vector;
#define ff(x) for(int x=0;x<2;x++)
using std::swap;
const int N=200011;
typedef long long lint;
lint min(lint a,lint b){return a<b?a:b;}
lint max(lint a,lint b){return a>b?a:b;}
int n;

int fa[N];

int son[N][2];
lint el[N][2];
lint esp[N];

struct Patchouli
{
	lint a,b;
	Patchouli(){}
	Patchouli(lint x,lint y){a=x;b=y;}
	bool friend operator < (Patchouli x,Patchouli y){return (x.a<y.a)||(x.a==y.a&&x.b<y.b);}
};

Patchouli p1[N],p2[N],p[N];
vector<Patchouli> ve[N];
int cnt[N];

#define pat Patchouli
lint k;
void fuck(int x)
{
	if(!son[x][0]) return;
	ff(i)
	{
		esp[son[x][i]]=esp[x]+el[x][i];
		fuck(son[x][i]);
	}
}

void dfs(int x)
{
	if(!son[x][0])
	{
		ve[x].push_back(pat(esp[x],esp[x]));
		cnt[x]=1;
		return;
	}
	int lson=son[x][0],rson=son[x][1];
	dfs(lson),dfs(rson);
	if(cnt[lson]>cnt[rson]) swap(lson,rson);
	int ar1=0,ar2=0,ar=0;
	for(int i=0,aa=-1;i<cnt[lson];i++)
	{
		while(aa<cnt[rson]-1&&ve[rson][aa+1].a+ve[lson][i].b-2ll*esp[x]<=k) aa++;
		if(aa>=0)
		{
			p1[++ar1]=pat(ve[lson][i].a,ve[rson][aa].b);
			p2[++ar2]=pat(ve[rson][aa].b,ve[lson][i].a);
		}
	}
	int a1=1,a2=ar2;
	while(a1<=ar1&&a2)
	{
		if(p1[a1].a<p2[a2].a) p[++ar]=p1[a1++];
		else p[++ar]=p2[a2--];
	}
	while(a1<=ar1)
	{
		p[++ar]=p1[a1++];
	}
	while(a2)
	{
		p[++ar]=p2[a2--];
	}
	for(int i=1;i<=ar;i++)
	{
		if(i==1||p[i].b<p[i-1].b) ve[x].push_back(p[i]),cnt[x]++;
	}
}

bool check(lint lim)
{
	k=lim;
	for(int i=1;i<=n;i++) ve[i].clear(),cnt[i]=0;
	dfs(1);
	return ve[1].size()?1:0;
}


lint r=0x3f3f3f3f3f3f3f3f,l,mid;

int main()
{
//	freopen("travel.in","r",stdin);
//	freopen("travel.out","w",stdout);
	scanf("%d",&n);
	lint vin;
	for(int i=2;i<=n;i++)
	{
		scanf("%d%lld",&fa[i],&vin);
		if(!son[fa[i]][0]) son[fa[i]][0]=i,el[fa[i]][0]=vin;
		else son[fa[i]][1]=i,el[fa[i]][1]=vin;
	}
	fuck(1);
	while(l<r)
	{
		mid=(l+r)>>1;
		if(check(mid)) r=mid;
		else l=mid+1;
	}
	printf("%lld\n",l);
	return 0;
}
