#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr ll INF=1e18;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

int NextPow2(int x)
{
	x--;
	for(int i=1;i<32;i*=2) x|=x>>i;
	return x+1;
}

template<typename T>
struct SegmentTree{
	const T I=numeric_limits<T>::max();
	T F(T a,T b){return min(a,b);}

	int size;
	vector<T> data;
	SegmentTree(int n):size(NextPow2(n)),data(size*2,I){}
	SegmentTree(const vector<T>& a):size(NextPow2(a.size())),data(size*2,I){
		copy(all(a),begin(data)+size);
		peri(i,1,size) data[i]=F(data[i*2],data[i*2+1]);
	}
	T Get(int i){
		return data[size+i];
	}
	void Update(int i,T x){
		data[size+i]=x;
		for(i+=size;i/=2;) data[i]=F(data[i*2],data[i*2+1]);
	}
	T Query(int a,int b,int i,int l,int r){
		if(b<=l || r<=a) return I;
		if(a<=l && r<=b) return data[i];
		return F(Query(a,b,i*2,l,(l+r)/2),Query(a,b,i*2+1,(l+r)/2,r));
	}
	T Query(int a,int b){
		return Query(a,b,1,0,size);
	}
};

struct Edge{
	int u,v,w;
	Edge(){}
	Edge(int u,int v,int w):u(u),v(v),w(w){}
};
using Tree=vector<vector<Edge>>;

vector<tuple<ll,ll>> dfs(const Tree& t,ll x,int p)
{
	if(t[p].empty()) return {mt(0,0)};	// leaf
	auto e0=t[p][0],e1=t[p][1];
	auto s0=dfs(t,x,e0.v),s1=dfs(t,x,e1.v);

	vector<tuple<ll,ll>> res;
	rep(_,2){
		for(int i=0,j=0;i<s0.size();i++){
			ll a,b; tie(a,b)=s0[i];
			for(;j<s1.size();j++){
				ll c,d; tie(c,d)=s1[j];
				if(e0.w+b+c+e1.w>x) break;
			}
			if(j>0){
				ll d=get<1>(s1[j-1]);
				res.emplace_back(e0.w+a,d+e1.w);
			}
		}
		swap(e0,e1); swap(s0,s1);
	}

	sort(all(res),[](auto a,auto b){return a<b;});
	{
		vector<tuple<ll,ll>> tmp;
		ll cur=INF;
		for(int i=0,j;i<res.size();i=j){
			for(j=i;j<res.size();j++)
				if(get<1>(res[j])<cur)
					break;
			if(j<res.size()){
				cur=get<1>(res[j]);
				tmp.push_back(res[j]);
				j++;
			}
		}
		res=move(tmp);
	}
	return res;
}

bool ok(const Tree& t,ll x)
{
	return dfs(t,x,0).size();
}

int main()
{
	for(int n;cin>>n&&n;){
		Tree t(n);
		rep(i,n-1){
			int u,v=i+1,w; cin>>u>>w; u--;
			t[u].emplace_back(u,v,w);
		}
		ll lo=-1,hi=INF;
		while(hi-lo>1){
			ll mi=(lo+hi)/2;
			(ok(t,mi)?hi:lo)=mi;
		}
		cout<<hi<<endl;
	}
}


Measured memory_bytes: 21760