#include <bits/stdc++.h>
      
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); i++)
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())

#define pb emplace_back
#define fi first
#define se second

using namespace std;

using ld = long double;
using ll = long long int;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ll MOD = 1e9+7;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> void UNIQUE(vector<T> &a){ a.erase(unique(a.begin(), a.end()), a.end()); }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
  os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
  for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
  for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

ll in(){long long int x; assert(scanf("%lld", &x) == 1); return x;}
ld fin(){double x; assert(scanf("%lf", &x) == 1); return x;}
char yuyushiki[1000010]; string stin(){assert(scanf("%s", yuyushiki) == 1); return yuyushiki;}

// head

using pl = pair<ll, ll>;

ll n, md;

vector<pl> cs[200010];
vector<pl> G[200010];

void dfs(ll x, ll p = -1){
  // cout << x << " " << p << endl;
  ll a = -1, aw, b = -1, bw;
  YYS(w, G[x]){
    if(w.fi == p){
      continue;
    }
    dfs(w.fi, x);
    if(a == -1){
      a = w.fi;
      aw = w.se;
    } else {
      assert(b == -1);
      b = w.fi;
      bw = w.se;
    }
  }
  // cout << x << endl;
  if(a == -1){
    cs[x].pb(0, 0);
    return;
  }
  assert(b != -1);
  vector<pl> ca(0), cb(0);
  ll it = 0;
  YYS(w, cs[a]){
    while(it + 1 < SZ(cs[b]) && w.se + cs[b][it + 1].fi + aw + bw <= md){
      it++;
    }
    if(it < SZ(cs[b]) && w.se + cs[b][it].fi + aw + bw <= md){
      ca.pb(w.fi + aw, cs[b][it].se + bw);
    }
  }
  it = 0;
  YYS(w, cs[b]){
    while(it + 1 < SZ(cs[a]) && w.se + cs[a][it + 1].fi + aw + bw <= md){
      it++;
    }
    if(it < SZ(cs[a]) && w.se + cs[a][it].fi + aw + bw <= md){
      cb.pb(w.fi + bw, cs[a][it].se + aw);
    }
  }
  it = 0;
  YYS(w, ca){
    while(it < SZ(cb) && cb[it].fi <= w.fi){
      if(cs[x].empty() || cs[x].back().se > cb[it].se){
        cs[x].pb(cb[it]);
      }
      it++;
    }
    if(cs[x].empty() || cs[x].back().se > w.se){
      if(!cs[x].empty() && cs[x].back().fi == w.fi){
        cs[x].pop_back();
      }
      cs[x].pb(w);
    }
  }
  while(it < SZ(cb)){
    if(cs[x].empty() || cs[x].back().se > cb[it].se){
      cs[x].pb(cb[it]);
    }
    it++;
  }
  /*
  cout << "# " << x << endl;
  cout << ca << endl;
  cout << cb << endl;
  cout << cs[x] << endl;
  */
}

int main(){

  n = in();
  REP(i, n-1){
    ll a = in() - 1;
    ll v = in();
    G[a].pb(i + 1, v);
    G[i + 1].pb(a, v);
  }
  
  ll lb = -1, ub = INFLL;
  while(ub - lb > 1){
    md = (lb + ub) / 2;
    REP(i, n){
      cs[i].clear();
    }
    dfs(0);
    if(SZ(cs[0]) >= 1){
      ub = md;
    } else {
      lb = md;
    }
  }

  cout << ub << endl;  
  
  return 0;
}


Measured memory_bytes: 41520