// Never let them see you bleed...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;

const int maxn = 15e4 + 10, mod = 1e9 + 7;
const ll inf = 1e18 + 10;

vector<pii> v[maxn];
set<pll> st[maxn];
ll LIM;

void add(set<pll> &st, pll p){
    if(st.count(p))
	return;
    auto it = st.upper_bound(p);
    if(it != st.begin() && (prev(it)->S) <= p.S)
	return;
    it = st.insert(p).F;
    while(next(it) != st.end() && p.S <= (next(it)->S))
	st.erase(next(it));
}
bool dfs(int u){
    st[u].clear();
    for(pii y : v[u]){
	if(!dfs(y.F))
	    return 0;
    }
    if(sz(v[u]) == 0){
	st[u].insert({0,0});
	return 1;
    }
    if(sz(st[v[u][0].F]) > sz(st[v[u][1].F]))
	swap(v[u][0], v[u][1]);
    pii A = v[u][0], B = v[u][1];
    for(pll p : st[A.F]){
	auto it = st[B.F].upper_bound({LIM - p.S - A.S - B.S, inf});
	if(it != st[B.F].begin()){
	    --it;
	    add(st[u], {p.F + A.S, it->S + B.S});
	    add(st[u], {it->S + B.S, p.F + A.S});
	}
    }
    if(st[u].empty())
	return 0;
    return 1;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();

    int n;
    cin >> n;
    for(int i = 2; i <= n; i++){
	int p, c;
	cin >> p >> c;
	v[p].PB({i, c});
    }
    ll l = -1, r = 1ll * maxn * maxn;
    while(r-l > 1){
	ll mid = (l+r) >> 1;
	LIM = mid;
	if(dfs(1))
	    r = mid;
	else
	    l = mid;	
    }
    return cout << r << endl, 0;
}
