/* be name khoda */

// #define stream_enable
#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;

#ifdef stream_enable
#define cin sss
#endif
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

const ll MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

#define forifrom(i, s, n) for(ll i = (s); i < (n); ++i)
#define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 132000;

ll n;
ll chn[maxn];
pii subt[maxn][2];
vpii dp[maxn];
ll K;

void dfs(ll x) {
    dp[x].clear();
    if (chn[x] == 0) {
        dp[x].eb(0, 0);
        return;
    }
    fori (i, 2) dfs(subt[x][i].F);
    ll L = subt[x][0].F, R = subt[x][1].F;
    ll Ln = dp[L].size(), Rn = dp[R].size();
    ll Le = subt[x][0].S, Re = subt[x][1].S;
    if (Ln > Rn) swap(L, R), swap(Ln, Rn), swap(Le, Re);
    ll ptr = 0;
    vpii A, B;
    fori (i, Ln) {
        while (ptr < Rn && dp[L][i].S + Le + Re + dp[R][ptr].F <= K) ++ptr;
        if (ptr) A.eb(dp[L][i].F + Le, dp[R][ptr - 1].S + Re);
    }
    ptr = 0;
    fori (i, Ln) {
        while (ptr < Rn && dp[R][ptr].S + Re + Le + dp[L][i].F > K) ++ptr;
        if (ptr < Rn) B.eb(dp[R][ptr].F + Re, dp[L][i].S + Le);
    }
    ll p1 = 0, p2 = 0;
    while (p1 < (ll) A.size() && p2 < (ll) B.size()) {
        if (A[p1].F < B[p2].F) {
            if (A[p1].S <= B[p2].S) ++p2;
            else dp[x].pb(A[p1++]);
        } else if (A[p1].F > B[p2].F) {
            if (A[p1].S >= B[p2].S) ++p1;
            else dp[x].pb(B[p2++]);
        } else {
            if (A[p1].S < B[p2].S) ++p2;
            else ++p1;
        }
    }
    while (p1 < (ll) A.size()) dp[x].pb(A[p1++]);
    while (p2 < (ll) B.size()) dp[x].pb(B[p2++]);
}

bool check() {
    dfs(0);
    if (dp[0].empty()) return false;
    return true;
}

void MAIN() {

    cin >> n;
    fori (i, n - 1) {
        ll a, v; cin >> a >> v, --a;
        subt[a][chn[a]++] = {i + 1, v};
    }

    ll l = -1, r = maxn * maxn;
    while (r - l > 1) {
        K = (l + r) / 2;
        if (check()) r = K;
        else l = K;
    }
    cout << r << '\n';

}

// -----------------------------------------------------------------------

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
9
1 2
1 2
3 2
3 2
5 2
5 2
7 2
7 2
    )";
    MAIN();
    return 0;
}



Measured memory_bytes: 31100