#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int n = 0, f = 1;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') c = getchar(), f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - 48;
  return n * f;
}
const int maxn = (1 << 17) + 5;
int k[maxn], ne[maxn], c[maxn], g[maxn], num;
int i, j, n, v, x;
long long m, l, r, inf = 2e10;
struct ar {
  long long a, b;
} o;
vector<ar> p1[maxn], p2[maxn], p;
void add(int x, int y, int z) {
  ne[++num] = k[x], k[x] = num, g[num] = y, c[num] = z;
}
bool cmp1(ar x, ar y) { return x.a == y.a ? x.b < y.b : x.a < y.a; }
bool cmp2(ar x, ar y) { return x.b == y.b ? x.a < y.a : x.b < y.b; }
void dfs(int x) {
  p1[x].clear(), p2[x].clear();
  int i, cnt = 0, son[2];
  long long dis[2], sum;
  for (i = k[x]; i; i = ne[i])
    dfs(g[i]), son[cnt] = g[i], dis[cnt] = c[i], cnt++;
  if (!cnt) {
    p1[x].push_back(o), p2[x].push_back(o);
    return;
  }
  if (p1[son[0]].size() > p1[son[1]].size())
    swap(son[0], son[1]), swap(dis[0], dis[1]);
  sum = m - dis[0] - dis[1];
  int j = 0, n0 = p1[son[0]].size(), n1 = p1[son[1]].size();
  p.clear();
  long long b = inf, a = inf;
  for (i = n0 - 1; i >= 0; i--) {
    for (; j < n1 && p1[son[1]][j].a + p2[son[0]][i].b <= sum; j++)
      b = (b < p1[son[1]][j].b ? b : p1[son[1]][j].b);
    if (b != inf) p.push_back((ar){p2[son[0]][i].a + dis[0], b + dis[1]});
  }
  j = 0;
  for (i = n0 - 1; i >= 0; i--) {
    for (; j < n1 && p2[son[1]][j].b + p1[son[0]][i].a <= sum; j++)
      a = (a < p2[son[1]][j].a ? a : p2[son[1]][j].a);
    if (a != inf) p.push_back((ar){a + dis[1], p1[son[0]][i].b + dis[0]});
  }
  sort(p.begin(), p.end(), cmp1);
  for (i = 0; i < p.size(); i++) p1[x].push_back(p[i]);
  sort(p.begin(), p.end(), cmp2);
  for (i = 0; i < p.size(); i++) p2[x].push_back(p[i]);
}
int main() {
  n = read();
  for (i = 2; i <= n; i++) {
    x = read(), v = read();
    add(x, i, v);
  }
  l = 0, r = inf;
  for (; l < r;) {
    m = (l + r) >> 1;
    dfs(1);
    if (p1[1].size())
      r = m;
    else
      l = m + 1;
  }
  printf("%lld\n", l);
}
