#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define fi first
#define se second
#define ls son[x][0]
#define rs son[x][1]
using namespace std;

const int N = 131075;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<pll> vp;
ll n, dis[N], l = 0, r = 1ll * N * N, lim, ans;
int son[N][3], num[N];
vp f[N];

vp dp(vp a, vp b) {
    vp ret; ret.clear();
    int x = a.size(), y = b.size(), i = 0, j = 0;
    if (!y) return ret;
    while (i != x) {
        while (j != y && a[i].se + b[j].fi <= lim) ++j;
        if (j) --j;
        if (a[i].se + b[j].fi <= lim) ret.push_back(make_pair(a[i].fi, b[j].se));
        ++i;
    } return ret;
}

void insert(vp &a, pll b) {
    if (!a.size()) a.push_back(b);
    else if (a.back().fi == b.fi) a.back().se = min(a.back().se, b.se);
    else if (b.se < a.back().se) a.push_back(b);
}

vp merge(vp a, vp b) {
    vp ret; ret.clear();
    int x = a.size(), y = b.size(), i = 0, j = 0;
    while (i != x && j != y) {
        if (a[i].fi < b[j].fi) insert(ret, a[i++]);
        else insert(ret, b[j++]);
    }
    while (i != x) insert(ret, a[i++]);
    while (j != y) insert(ret, b[j++]);
    return ret;
}

void dfs(int x) {
    if (!num[x]) {
        f[x].clear();
        f[x].push_back(make_pair(dis[x], dis[x]));
        return;
    }
    dfs(ls), dfs(rs);
    lim += dis[x] * 2;
    vp t1 = dp(f[ls], f[rs]);
    vp t2 = dp(f[rs], f[ls]);
    f[x] = merge(t1, t2);
    lim -= dis[x] * 2;
}

bool chk() {
    dfs(1);
    if (f[1].size()) return 1;
    else return 0;
}

int main() {
    cin >> n;
    rep(i, 2, n) {
        int fa, v; scanf("%d%d", &fa, &v);
        dis[i] = dis[fa] + v;
        son[fa][num[fa]++] = i;
    }
    while (l <= r) {
        lim = (l + r) >> 1;
        if (chk()) ans = lim, r = lim - 1;
        else l = lim + 1;
    }
    printf("%lld\n", ans);
    return 0;
}
