/*
Author: QAQ-Automaton
LANG: C++
PROG: e.cpp
Mail: cnyalilk@vip.qq.com
*/
#include<bits/stdc++.h>
#define int ll
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const ll inf=0x3f3f3f3f3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	inline void read (signed &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}

	inline void read (long long &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}
	inline void read (char &x) {
		x=gc();
	}
	inline void read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r');
		while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
		*x=0;
	}
	template<typename A,typename ...B>
	inline void read(A &x,B &...y){
		read(x);read(y...);
	}
	// print a signed integer
	inline void write (signed x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}

	inline void write (long long x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	inline void write (char x) {
		putc(x);
	}
	inline void write(const char *x){
		while(*x){putc(*x);++x;}
	}
	inline void write(char *x){
		while(*x){putc(*x);++x;}
	}
	template<typename A,typename ...B>
	inline void write(A x,B ...y){
		write(x);write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
vector<int> son[131075];
int fa[131075],ans,w[131075],rg;
vector<pii> okp[131075],qaq;
void dfs(int x){
	okp[x].clear();
	if(!son[x].size()){
		okp[x].push_back(make_pair(w[x],w[x]));
	}	
	else{
		dfs(son[x][0]);
		dfs(son[x][1]);
		int l=son[x][0],r=son[x][1];
		if(okp[l].size()>okp[r].size())swap(l,r);
		int mny=inf;
		for(auto i=okp[r].begin();i!=okp[r].end();++i){
			chkmin(mny,i->y);
			i->y=mny;
		}
		int s=okp[r].size(),j=s-1;
		for(auto i:okp[l]){
			while(~j &&okp[r][j].x+i.x>rg)--j;
			if(!~j)break;
			okp[x].push_back(make_pair(i.y+w[x],okp[r][j].y+w[x]));
			okp[x].push_back(make_pair(okp[r][j].y+w[x],i.y+w[x]));
		}
		sort(all(okp[x]));
		okp[x].erase(unique(all(okp[x])),okp[x].end());
		
	}
}
signed main(){
#ifdef QAQAutoMaton 
	freopen("e.in","r",stdin);
	freopen("e.out","w",stdout);
#endif
	int n;	
	read(n);
	for(int i=2;i<=n;++i){
		read(fa[i],w[i]);
		son[fa[i]].push_back(i);
	}
	int l=0,r=1LL<<34;	
	while(l<=r){
		rg=(l+r)>>1;
		dfs(1);
		if(okp[1].size())r=rg-1;
		else l=rg+1;
	}
	write(r+1,'\n');
	return 0;
}



Measured memory_bytes: 36848