#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') q = 0, c = getchar();
  while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
int son[N][2], len[N], dis[N], siz[N];
long long ans, mid;
vector<pair<long long, long long> > v[N];
pair<long long, long long> p1[N], p2[N];
inline void init(int k, long long &pre) {
  if (!len[k])
    ans = max(ans, pre), pre = dis[k], siz[k] = 1;
  else {
    init(son[k][0], pre += dis[son[k][0]]);
    init(son[k][1], pre += dis[son[k][1]]);
    siz[k] = siz[son[k][0]] + siz[son[k][1]];
    pre += dis[k];
  }
}
inline void dfs(int k) {
  v[k].clear();
  if (!len[k]) {
    v[k].push_back(make_pair(dis[k], dis[k]));
    return;
  }
  dfs(son[k][0]);
  dfs(son[k][1]);
  int ls = v[son[k][0]].size(), rs = v[son[k][1]].size(), i, j, l1 = 0, l2 = 0,
      len = -1;
  for (i = 0, j = rs; i < ls; i++) {
    while (j && v[son[k][0]][i].second + v[son[k][1]][j - 1].second <= mid) j--;
    if (j == rs) continue;
    if (!l1 || v[son[k][1]][j].first < p1[l1].second)
      p1[++l1] = make_pair(v[son[k][0]][i].first, v[son[k][1]][j].first);
  }
  swap(ls, rs);
  swap(son[k][0], son[k][1]);
  for (i = 0, j = rs; i < ls; i++) {
    while (j && v[son[k][0]][i].second + v[son[k][1]][j - 1].second <= mid) j--;
    if (j == rs) continue;
    if (!l2 || v[son[k][1]][j].first < p2[l2].second)
      p2[++l2] = make_pair(v[son[k][0]][i].first, v[son[k][1]][j].first);
  }
  for (i = j = 1; i <= l1 || j <= l2;)
    if (i <= l1 && (j > l2 || p1[i].first < p2[j].first)) {
      if (len == -1 || p1[i].second < v[k][len].second)
        v[k].push_back(p1[i]), len++;
      i++;
    } else if (i > l1 || p1[i].first > p2[j].first) {
      if (len == -1 || p2[j].second < v[k][len].second)
        v[k].push_back(p2[j]), len++;
      j++;
    } else
      v[k].push_back(make_pair(p1[i].first, min(p1[i].second, p2[j].second))),
          i++, j++;
  for (i = 0; i <= len; i++) v[k][i].first += dis[k], v[k][i].second += dis[k];
}
int main() {
  int n = gi(), i, k;
  long long t = 0, l, r;
  for (i = 2; i <= n; i++) k = gi(), son[k][len[k]++] = i, dis[i] = gi();
  init(1, t);
  for (l = 0, r = ans; l != r;) {
    mid = (l + r) >> 1;
    dfs(1);
    if (v[1].size())
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", l);
  return 0;
}
