#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define sz(s) ((int) ((s).size()))

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

typedef long long ll;
typedef long double ld;

const int inf = (int) 1.01e9;
const ld eps = 1e-11;
const ld pi = acos(-1.0L);

mt19937 mrand(random_device{} ());
int rnd(int x) {
  return mrand() % x;
}

const int mod = (int) 1e9 + 7;

void add(int & x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}

int sum(int x, int y) {
  add(x, y);
  return x;
}

int mult(int x, int y) {
  return (ll) x * y % mod;
}

int power(int x, ll p) {
  int res = 1;
  while (p) {
    if (p & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
    p >>= 1;
  }
  return res;
}

void precalc() {
}

const int maxn = 2e5;

int n;
int cost[maxn];
vector<vector<int> > tree;

bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  tree = vector<vector<int> > (n);
  for (int i = 0; i < n - 1; i++) {
    int p, c;
    scanf("%d%d", &p, &c);
    p--;
    tree[p].pb(i + 1);
    cost[i + 1] = c;
  }
  return true;
}

ll atmost;

bool mycomp(const pair<ll, ll> & a, const pair<ll, ll> & b) {
  return a.first < b.first;
}

void solve(int v, ll & toadd, vector<pair<ll, ll> > & f) {
  if (sz(tree[v]) == 0) {
    f.pb(mp((ll) 0, (ll) 0));
  } else if (sz(tree[v]) == 1) {
    solve(tree[v][0], toadd, f);
    toadd += cost[tree[v][0]];
  } else {
    assert(sz(tree[v]) == 2);
    ll toadd1 = 0, toadd2 = 0;
    vector<pair<ll, ll> > v1, v2;
    solve(tree[v][0], toadd1, v1);
    solve(tree[v][1], toadd2, v2);
    toadd1 += cost[tree[v][0]];
    toadd2 += cost[tree[v][1]];
    vector<pair<ll, ll> > ans1, ans2;
    for (int i = 0, j = sz(v2); i < sz(v1); i++) {
      while (j && v2[j - 1].second + toadd2 + v1[i].second + toadd1 <= atmost) {
        j--;
      }
      if (j != sz(v2)) {
        ans1.pb(mp(v1[i].first + toadd1, v2[j].first + toadd2));
        ans2.pb(mp(v2[j].first + toadd2, v1[i].first + toadd1));
      }
    }
    reverse(ans2.begin(), ans2.end());
    vector<pair<ll, ll> > ans(sz(ans1) + sz(ans2));
    merge(ans1.begin(), ans1.end(), ans2.begin(), ans2.end(), ans.begin(), mycomp);
    ll allmn = 1e18;
    for (int i = 0; i < sz(ans); ) {
      int i0 = i;
      ll mnval = 1e18;
      while (i < sz(ans) && ans[i].first == ans[i0].first) {
        mnval = min(mnval, ans[i].second);
        i++;
      }
      if (mnval < allmn) {
        allmn = mnval;
        f.pb(mp(ans[i0].first, mnval));
      }
    }
  }
}

void solve() {
  ll l = 0, r = 1ll << 40; //////////////////////////////////////////////////////////////////////////////////
  while (l < r) {
    ll mid = (l + r) / 2;
    atmost = mid;
    ll toadd = 0;
    vector<pair<ll, ll> > v;
    solve(0, toadd, v);
    if (sz(v)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", l);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen("text.in", "r", stdin));
  assert(freopen("text.out", "w", stdout));
#endif

  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time: %.3f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}



Measured memory_bytes: 22272