#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;

typedef long long ll;
const int maxn = 150010;
int n;
vector<pair<int, int>> G[maxn];
vector<pair<ll, ll>> f[maxn];

int main() {
    scanf("%d", &n);
    for (int i = 2, f, w; i <= n; i++) {
        scanf("%d %d", &f, &w), G[f].push_back({i, w});
    }
    ll l = 0, r = 150000LL * n, ans;
    while (l <= r) {
        ll mid = (l + r) >> 1;
        function<void(int)> dfs = [&](int v) {
            if (G[v].empty()) { f[v].push_back({0, 0}); return; }
            for (auto e : G[v]) dfs(e.x);
            auto& ls = f[G[v][0].x], rs = f[G[v][1].x];
            for (auto& p : ls) p.x += G[v][0].y, p.y += G[v][0].y;
            for (auto& p : rs) p.x += G[v][1].y, p.y += G[v][1].y;
            if (ls.size() > rs.size()) swap(ls, rs);
            vector<pair<ll, ll>> V;
            for (int i = 0, j = rs.size() - 1; i < ls.size(); i++) {
                while (~j && ls[i].x + rs[j].x > mid) j--;
                if (j < 0) break;
                V.push_back({ls[i].y, rs[j].y}), V.push_back({rs[j].y, ls[i].y});
            }
            sort(V.begin(), V.end());
            ll mn = LLONG_MAX;
            for (auto p : V) {
                if (mn > p.y) f[v].push_back(p);
                mn = min(mn, p.y);
            }
        };
        for (int i = 1; i <= n; i++) f[i].clear();
        dfs(1);
        !f[1].empty() ? r = (ans = mid) - 1 : l = mid + 1;
    }
    printf("%d\n", ans);
    return 0;
}