#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,lc[N],rc[N],fa[N],size[N];
vector<int> e[N];
ll mid,up[N];
typedef pair<ll,ll> pll;
vector<pll> v[N];
bool out;
bool check(int x){
	size[x]=1;v[x].clear();
	if (!lc[x]){
		if (up[x]>mid) return 0;
		v[x].push_back(pll(up[x],up[x]));
		return 1;
	}
	if (!check(lc[x])||!check(rc[x])) return 0;
	size[x]=size[lc[x]]+size[rc[x]]+1;
	if (v[lc[x]].size()>v[rc[x]].size()) swap(lc[x],rc[x]);
	vector<pll> &a=v[lc[x]],&b=v[rc[x]];
	vector<pll>::iterator i=a.begin(),p=b.end();--p;
	static vector<pll> A;
	A.clear();
	for (;i!=a.end();++i){
		while (p!=b.begin()&&i->first+p->first>mid) --p;
		if (i->first+p->first>mid) continue;
		A.push_back(pll(i->second+up[x],p->second+up[x]));
		A.push_back(pll(p->second+up[x],i->second+up[x]));
	}
	sort(A.begin(),A.end());
	ll Min=1e16;
	for (i=A.begin();i!=A.end();++i)
		if (i->second<Min) v[x].push_back(*i),Min=i->second;
	return !v[x].empty();
}
int main()
{
	scanf("%d",&n);
	for (int i=2;i<=n;i++){
		scanf("%d%lld",&fa[i],&up[i]);
		(lc[fa[i]]?rc:lc)[fa[i]]=i;
	}
	up[1]=-1e16;
	ll l=0,r=2e10;
	while (l<r){
		mid=(l+r)>>1;
		if (check(1)) r=mid;else l=mid+1;
	}
	printf("%lld\n",l);
	return 0;
}