#include <bits/stdc++.h>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 131072 + 5;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int n, w[N], ch[2][N], c[N];
long long d[N], l = INF, r, mid, ans, pre[N];
vector<pair<long long, long long> > f[N];
inline bool cmpx(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first < b.first;
}
inline bool cmpy(pair<long long, long long> a, pair<long long, long long> b) {
  return a.second < b.second;
}
inline void insert(vector<pair<long long, long long> > &ls,
                   vector<pair<long long, long long> > &rs, int x) {
  int sy = ls.size(), sz = rs.size();
  sort(ls.begin(), ls.end(), cmpy);
  sort(rs.begin(), rs.end(), cmpx);
  pre[0] = rs[0].second;
  for (int i = 1; i < sz; ++i) pre[i] = min(pre[i - 1], rs[i].second);
  for (int i = 0, j = sz - 1; i < sy; ++i) {
    while (~j && ls[i].second + rs[j].first - d[x] * 2 > mid) --j;
    if (~j) f[x].push_back(make_pair(ls[i].first, pre[j]));
  }
}
bool valid(int x) {
  f[x].clear();
  if (!c[x]) return f[x].push_back(make_pair(d[x], d[x])), f[x].size();
  int y = ch[0][x], z = ch[1][x];
  if (!valid(y) || !valid(z)) return false;
  insert(f[y], f[z], x), insert(f[z], f[y], x);
  return f[x].size();
}
void dfs(int x) {
  d[x] += w[x];
  if (!c[x]) return l = min(l, d[x]), void(r = max(r, d[x] << 1));
  d[ch[0][x]] = d[ch[1][x]] = d[x];
  dfs(ch[0][x]), dfs(ch[1][x]);
}
int main() {
  n = read();
  for (int i = 2; i <= n; ++i) {
    int x = read();
    w[i] = read();
    ch[c[x]++][x] = i;
  }
  dfs(1);
  while (l <= r) {
    mid = (l + r) >> 1;
    if (valid(1))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
