#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct node
{
	long long x,y;
};
int n;
long long mn[200010];
vector<pair<int,int> > v[200010];
vector<node> f[200010];
int cnt,head[200010],to[400010],nxt[400010],w[400010];
void add(int x,int y,int z)
{
	cnt++;
	to[cnt]=y;
	nxt[cnt]=head[x];
	head[x]=cnt;
	w[cnt]=z;
}
bool cmpx(node x,node y)
{
	return x.x<y.x;
}
bool cmpy(node x,node y)
{
	return x.y<y.y;
}
void dfs(int x,long long mid)
{
	f[x].clear();
	if (v[x].empty())
	{
		f[x].push_back((node){0LL,0LL});
		return;
	}
	dfs(v[x][0].first,mid);
	dfs(v[x][1].first,mid);
	vector<node> ls,rs;
	int sl,sr;
	ls=f[v[x][0].first];
	sl=ls.size();
	int y=v[x][0].second;
	for (int i=0;i<sl;i++)
	{
		//printf("(%lld,%lld)-->",ls[i].x,ls[i].y);
		//cout<<ls[i].y<<" "<<y<<" ";
		ls[i].x+=y;
		ls[i].y+=y;
		//printf("(%lld,%lld) ",ls[i].x,ls[i].y);
		//cout<<ls[i].x<<" "<<ls[i].y<<" "<<endl;
	}
	//puts("");
	rs=f[v[x][1].first];
	sr=rs.size();
	y=v[x][1].second;
	for (int i=0;i<sr;i++)
	{
		rs[i].x+=y;
		rs[i].y+=y;
	}
	if (!sl || !sr)
	{
		return;
	}
	if (sl>sr)
	{
		/*
		vector<node> tmp;
		tmp.clear();
		for (int i=0;i<sl;i++)
		{
			tmp.push_back(ls[i]);
		}
		ls.clear();
		for (int i=0;i<sr;i++)
		{
			ls.push_back(rs[i]);
		}
		rs.clear();
		for (int i=0;i<sl;i++)
		{
			rs.push_back(tmp[i]);
		}
		*/
		swap(sl,sr);
		swap(ls,rs);
	}
	/*
	cout<<v[x][0].first<<":";
	for (int i=0;i<sl;i++)
	{
		printf(" (%lld,%lld) ",ls[i].x,ls[i].y);
	}
	cout<<"& "<<v[x][1].first<<":";
	for (int i=0;i<sr;i++)
	{
		printf(" (%lld,%lld) ",rs[i].x,rs[i].y);
	}
	printf("--> ");
	*/
	sort(ls.begin(),ls.end(),cmpy);
	sort(rs.begin(),rs.end(),cmpx);
	mn[0]=rs[0].y;
	for (int i=1;i<sr;i++)
	{
		mn[i]=min(mn[i-1],rs[i].y);
	}
	//cout<<x<<" ";
	for (int i=0,j=sr-1;i<sl;i++)
	{
		while (j>=0 && ls[i].y+rs[j].x>mid)
		{
			j--;
		}
		if (j>=0)
		{
			f[x].push_back((node){ls[i].x,mn[j]});
		}
	}
	sort(ls.begin(),ls.end(),cmpx);
	sort(rs.begin(),rs.end(),cmpy);
	mn[0]=rs[0].x;
	for (int i=1;i<sr;i++)
	{
		mn[i]=min(mn[i-1],rs[i].x);
	}
	for (int i=0,j=sr-1;i<sl;i++)
	{
		while (j>=0 && ls[i].x+rs[j].y>mid)
		{
			j--;
		}
		if (j>=0)
		{
			f[x].push_back((node){mn[j],ls[i].y});
		}
	}
	/*
	cout<<x<<":";
	for (int i=0;i<f[x].size();i++)
	{
		printf(" (%lld,%lld) ",f[x][i].x,f[x][i].y);
	}
	puts("");
	system("pause");
	*/
	//cout<<f[x].size()<<endl;
}
int main()
{
	//freopen("7e.in","r",stdin);
	//freopen("7e.out","w",stdout);
	scanf("%d",&n);
	for (int i=2;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].push_back(make_pair(i,y));
	}
	long long l=0,r=1e12,mid;
	//l=19,r=19;
	while (l<=r)
	{
		mid=(l+r)>>1;
		dfs(1,mid);
		//cout<<l<<" "<<r<<" "<<mid<<endl;
		if (!f[1].empty())
		{
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	printf("%lld\n",l);
    return 0;
}
//人世几回伤往事，山形依旧枕寒流。
//——刘禹锡《西塞山怀古》 

Measured memory_bytes: 35692