#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn 200005
typedef long long LL;
#define pi pair<LL,LL> 
#define fir first
#define sec second
#define mp make_pair
vector<pi> A[maxn];
int father[maxn],son[maxn][2],a[maxn],n;
LL ans;
bool flag[maxn];
pi t1[maxn],t2[maxn];

inline int read(void) 
{
	int x=0;
	char ch=getchar();
	while (ch>'9'||ch<'0') ch=getchar();
	while (ch>='0'&&ch<='9') 
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}

void insert(int u,pi tmp) 
{
	int siz=A[u].size();
	if (siz==0) {A[u].push_back(tmp);return;}
	if (A[u][siz-1].sec<=tmp.sec) return;
	A[u].push_back(tmp);
}

bool dfs(int u,LL limit) 
{
	if (!flag[u]) 
	{
		A[u].push_back(mp(0,0));
		return 1;
	}
	int s1=son[u][0],s2=son[u][1];
	if (!dfs(s1,limit)) return 0;
	if (!dfs(s2,limit)) return 0;
	int tot1=0,tot2=0;	
	int siz1=A[s1].size();
	int siz2=A[s2].size();
	for (int i=0;i<siz1;i++) A[s1][i].fir+=a[s1],A[s1][i].sec+=a[s1];
	for (int i=0;i<siz2;i++) A[s2][i].fir+=a[s2],A[s2][i].sec+=a[s2];
	int p;
	p=siz2-1;
	for (int i=siz1-1;i>=0;i--) 
	{
		while (p>=0&&A[s1][i].sec+A[s2][p].fir>limit) p--;
		if (p<0) break;
		t1[++tot1]=mp(A[s1][i].fir,A[s2][p].sec);
	}
	p=siz1-1;
	for (int i=siz2-1;i>=0;i--) 
	{
		while (p>=0&&A[s2][i].sec+A[s1][p].fir>limit) p--;
		if (p<0) break;
		t2[++tot2]=mp(A[s2][i].fir,A[s1][p].sec);
	}
	reverse(t1+1,t1+tot1+1);
	reverse(t2+1,t2+tot2+1);
	int p1=1,p2=1;
	while (p1<=tot1&&p2<=tot2) 
		if (t1[p1]<=t2[p2]) insert(u,t1[p1]),p1++;
		else insert(u,t2[p2]),p2++;
	while (p1<=tot1) insert(u,t1[p1]),p1++;
	while (p2<=tot2) insert(u,t2[p2]),p2++;
	return A[u].size()>0;
}

int main()
{
	n=read();
	for (int i=2;i<=n;i++) 
	{
		father[i]=read();
		flag[father[i]]?son[father[i]][1]=i:son[father[i]][0]=i;
		flag[father[i]]=1;
		a[i]=read();
	}
	LL L=0,R=1e10;
	while (L<=R) 
	{
		LL mid=(L+R)>>1;
		for (int i=1;i<=n;i++) A[i].clear();
		if (dfs(1,mid)) 
		{
			ans=mid;
			R=mid-1;
		}
		else L=mid+1;
	}
	printf("%lld\n",ans);
	return 0;
}