#include <bits/stdc++.h>
using namespace std;
int n;
namespace a {
using ll = long long;
struct ll_it : iterator<random_access_iterator_tag, ll, ll, ll*, ll&> {
  ll x;
  ll_it() {}
  ll_it(const ll xx) : x(xx) {}
  ll_it& operator+=(const ll y) {
    x += y;
    return *this;
  }
  ll operator-(const ll_it& rhs) { return x - rhs.x; }
  ll_it& operator++() {
    ++x;
    return *this;
  }
  ll operator*() { return x; }
};
vector<vector<int>> fii;
vector<int> father;
vector<map<int, ll>> dist;
vector<vector<array<ll, 2>>> posibilitati;
void remove_stupidity(vector<array<ll, 2>>& v) {
  if (v.empty()) return;
  int i0 = 0;
  for (int i = 1; i < (int)v.size(); ++i) {
    if (v[i0][0] < v[i][0] && v[i0][1] > v[i][1]) {
      v[++i0] = v[i];
    } else if (v[i0][0] == v[i][0] && v[i0][1] > v[i][1]) {
      v[i0] = v[i];
    }
  }
  v.erase(begin(v) + i0 + 1, end(v));
}
void dfs(const int cur, const ll maxlen) {
  posibilitati[cur].clear();
  if (fii[cur].empty()) {
    posibilitati[cur] = vector<array<ll, 2>>{array<ll, 2>{0, 0}};
    return;
  }
  dfs(fii[cur][0], maxlen), dfs(fii[cur][1], maxlen);
  if (posibilitati[fii[cur][0]].empty() || posibilitati[fii[cur][1]].empty())
    return;
  const ll ca = dist[cur][fii[cur][0]], cb = dist[cur][fii[cur][1]];
  auto it = --end(posibilitati[fii[cur][1]]);
  vector<array<ll, 2>> tmpa, tmpb;
  for (const auto& pa : posibilitati[fii[cur][0]]) {
    while (it > begin(posibilitati[fii[cur][1]]) &&
           ca + cb + pa[0] + (*it)[0] > maxlen) {
      --it;
    }
    if (ca + cb + pa[0] + (*it)[0] <= maxlen) {
      tmpa.push_back(array<ll, 2>{pa[1] + ca, (*it)[1] + cb});
      if (pa[1] + ca != (*it)[1] + cb)
        tmpb.push_back(array<ll, 2>{(*it)[1] + cb, pa[1] + ca});
    }
  }
  if (!is_sorted(begin(tmpa), end(tmpa))) reverse(begin(tmpa), end(tmpa));
  if (!is_sorted(begin(tmpb), end(tmpb))) reverse(begin(tmpb), end(tmpb));
  merge(begin(tmpa), end(tmpa), begin(tmpb), end(tmpb),
        back_inserter(posibilitati[cur]));
  remove_stupidity(posibilitati[cur]);
}
int main() {
  fii.resize(n);
  father.resize(n);
  dist.resize(n);
  posibilitati.resize(n);
  for (int i = 1, x, y; i < n; ++i) {
    cin >> x >> y;
    --x;
    fii[x].push_back(i);
    father[i] = x;
    dist[x][i] = y;
  }
  cout << *partition_point(ll_it(0), ll_it(131072ll * 131072ll * 2ll),
                           [](const ll val) {
                             dfs(0, val);
                             return posibilitati[0].empty();
                           })
       << endl;
  return 0;
}
}  // namespace a
namespace b {
using ll = long long;
struct ll_it : iterator<random_access_iterator_tag, ll, ll, ll*, ll&> {
  ll x;
  ll_it() {}
  ll_it(const ll xx) : x(xx) {}
  ll_it& operator+=(const ll y) {
    x += y;
    return *this;
  }
  ll operator-(const ll_it& rhs) { return x - rhs.x; }
  ll_it& operator++() {
    ++x;
    return *this;
  }
  ll operator*() { return x; }
};
vector<vector<int>> fii;
vector<int> father;
vector<map<int, ll>> dist;
vector<vector<array<ll, 2>>> posibilitati;
void dfs(const int cur, const ll maxlen) {
  if (fii[cur].empty()) {
    posibilitati[cur].push_back(array<ll, 2>{0, 0});
    return;
  }
  dfs(fii[cur][0], maxlen), dfs(fii[cur][1], maxlen);
  if (posibilitati[fii[cur][0]].empty() || posibilitati[fii[cur][1]].empty()) {
    return;
  }
  const ll ca = dist[cur][fii[cur][0]], cb = dist[cur][fii[cur][1]];
  for (const auto& pa : posibilitati[fii[cur][0]]) {
    for (const auto& pb : posibilitati[fii[cur][1]]) {
      if (ca + cb + pa[0] + pb[0] <= maxlen) {
        posibilitati[cur].push_back(array<ll, 2>{ca + pa[1], cb + pb[1]});
        posibilitati[cur].push_back(array<ll, 2>{cb + pb[1], ca + pa[1]});
      }
    }
  }
  if (posibilitati[cur].empty()) return;
  sort(begin(posibilitati[cur]), end(posibilitati[cur]));
  int i0 = 0;
  for (int i = 1; i < (int)posibilitati[cur].size(); ++i) {
    if (posibilitati[cur][i0][1] > posibilitati[cur][i][1]) {
      posibilitati[cur][++i0] = posibilitati[cur][i];
    }
  }
  posibilitati[cur].erase(begin(posibilitati[cur]) + i0 + 1,
                          end(posibilitati[cur]));
}
int main() {
  fii.resize(n);
  father.resize(n);
  dist.resize(n);
  posibilitati.resize(n);
  for (int i = 1, x, y; i < n; ++i) {
    cin >> x >> y;
    --x;
    fii[x].push_back(i);
    father[i] = x;
    dist[x][i] = y;
  }
  cout << *partition_point(ll_it(0), ll_it(131072ll * 131072ll * 2ll),
                           [](const ll val) {
                             for (auto& x : posibilitati) x.clear();
                             dfs(0, val);
                             return posibilitati[0].empty();
                           })
       << endl;
  return 0;
}
}  // namespace b
int main() {
  cin >> n;
  if (n >= 30000) {
    a::main();
  } else {
    b::main();
  }
  return 0;
}
