#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long i64;
typedef pair<i64, i64> pii;

const int MAX_N = 140000;
const i64 INF = 0x3f3f3f3f3f3f3f3fLL;

int val[MAX_N], ch[MAX_N][2], num[MAX_N];
bool flag;
i64 LIM;

inline void chmin(i64 &x, i64 y) {
  if (x > y) x = y;
}

void merge(vector<pii> &a, vector<pii> &b, vector<pii> &c) {
  static vector<pii> t1, t2;
  static pii st[MAX_N];
  
  int top = 0;
  t1.clear(), t2.clear();
  for (int i = b.size() - 1, l = 0, r = c.size() - 1; i >= 0; --i) {
    while (l < c.size() && c[l].second + b[i].second > LIM) l++;
    while (r >= 0 && c[r].first + b[i].second > LIM) r--;
    i64 temp = INF;
    if (l < c.size()) chmin(temp, c[l].first);
    if (r >= 0) chmin(temp, c[r].second);
    if (temp != INF) t1.push_back(make_pair(b[i].first, temp));
  }
  // reverse(t1.begin(), t1.end());
  for (int i = 0, l = 0, r = c.size() - 1; i < b.size(); ++i) {
    while (l < c.size() && c[l].second + b[i].first > LIM) l++;
    while (r >= 0 && c[r].first + b[i].first > LIM) r--;
    i64 temp = INF;
    if (l < c.size()) chmin(temp, c[l].first);
    if (r >= 0) chmin(temp, c[r].second);
    if (temp != INF) t2.push_back(make_pair(temp, b[i].second));
  }
  
  // sort(t1.begin(), t1.end());
  // sort(t2.begin(), t2.end());
  
  // int l1 = 0, l2 = 0;
  // while (l1 < t1.size() && l2 < t2.size()) {
  //   if (l1 == t1.size()) top++, st[top] = t2[l2++];
  //   else if (l2 == t2.size()) top++, st[top] = t1[l1++];
  //   else if (t1[l1] < t2[l2]) top++, st[top] = t1[l1++];
  //   else top++, st[top] = t2[l2++];
  // }
  // for (int i = 0; i < t1.size(); ++i) st[++top] = t1[i];
  // for (int i = 0; i < t2.size(); ++i) st[++top] = t2[i];
  
  // sort(st + 1, st + top + 1);
  
  // for (int i = 1; i <= top; ++i) {
  //   if (i != 1 && a.back().second <= st[i].second) continue;
  //   a.push_back(st[i]);
  // }

  // for (int i = 0; i < t1.size(); ++i) st[++top] = t1[i];
  // for (int i = 0; i < t2.size(); ++i) st[++top] = t2[i];
  // sort(st + 1, st + top + 1);
  // for (int i = 2; i <= top; ++i) 
  //   if (st[i].second >= st[i - 1].second) st[i] = st[i - 1];
  
  // top = unique(st + 1, st + top + 1) - st - 1;
  // for (int i = 1; i <= top; ++i)
  //   a.push_back(st[i]);
  
  for (int i = 0; i < t1.size(); ++i) a.push_back(t1[i]);
  for (int i = 0; i < t2.size(); ++i) a.push_back(t2[i]);
  sort(a.begin(), a.end());
  for (int i = 1; i < a.size(); ++i) {
    if (a[i].second >= a[i - 1].second) a[i] = a[i - 1];
  }
  
  int X = unique(a.begin(), a.end()) - a.begin();
  while (a.size() > X) a.pop_back();
}

vector<pii> vec[MAX_N];

void dfs(int u) {
  vec[u].clear();
  if (!num[u]) {
    vec[u].push_back(make_pair(0, 0));
    return;
  }
  
  int v1 = ch[u][0], v2 = ch[u][1];
  dfs(ch[u][0]), dfs(ch[u][1]);
  for (int i = 0; i < vec[v1].size(); ++i)
    vec[v1][i].first += val[v1], vec[v1][i].second += val[v1];
  for (int i = 0; i < vec[v2].size(); ++i)
    vec[v2][i].first += val[v2], vec[v2][i].second += val[v2];
  
  if (vec[v1].size() > vec[v2].size()) swap(v1, v2);
  
  if (flag) merge(vec[u], vec[v1], vec[v2]);
  if (!vec[u].size()) flag = 0;
}

bool check(i64 mid) {
  LIM = mid, flag = 1;
  dfs(1);
  return flag;
}

int N;

int main() {
  scanf("%d", &N);
  for (int i = 2, u; i <= N; ++i) {
    scanf("%d%d", &u, &val[i]);
    ch[u][num[u]++] = i;
  }

  i64 l = 0, r = 20000000000LL, res = 0;

  while (l <= r) {
    i64 mid = (l + r) >> 1;
    if (check(mid)) res = mid, r = mid - 1;
    else l = mid + 1;
  }

  printf("%lld\n", res);
  return 0;
}


Measured memory_bytes: 28908