#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>

namespace IO {
    template <typename _T>
    inline bool read (_T& x) {
        x = 0;
        _T y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return false;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        x *= y;
        return true;
    }

    template <typename _T>
    inline _T input () {
        _T x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return 0;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        x *= y;
        return x;
    }
};
using namespace IO;

namespace modular {
    const int MOD = 1000000007;

    inline int add (int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }

    inline void inc (int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }

    inline int mul (int x, int y) { return 1LL * x * y % MOD; }

    inline int qpow (int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = mul(x, x)) if (y & 1) ans = mul(ans, x);
        return ans;
    }
};

#define reg register
#define MAX_N 200007
#define ls (c[x][0])
#define rs (c[x][1])
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for(int i = l; i <= r; ++i)
#define lep(i, l, r) for(int i = l; i < r; ++i)
#define irep(i, r, l) for(int i = r; i >= l; --i)
#define ilep(i, r, l) for(int i = r; i > l; --i)
typedef long long ll;

int N;
int c[MAX_N][2];
ll d[MAX_N][2], mn[MAX_N];

struct node { ll in, out; };
typedef std::vector<node> vcn;
vcn vc[MAX_N];

inline bool cmp1 (node x, node y) { return x.out < y.out; }

inline bool cmp2 (node x, node y) { return x.in == y.in ? x.out < y.out : x.in < y.in; }

inline void merge (vcn& a, vcn& b, vcn& ans, ll mx) {
    // if (a.size() > b.size()) std::swap(a, b);
    std::sort(a.begin(), a.end(), cmp1);
    std::sort(b.begin(), b.end(), cmp2);
    mn[0] = b[0].out;
    lep (i, 1, b.size()) mn[i] = std::min(mn[i - 1], b[i].out);
    int cur = b.size() - 1;
    lep (i, 0, a.size()) {
        while ((~cur) && b[cur].in + a[i].out > mx) cur--;
        if (cur < 0) break;
        ans.push_back((node){a[i].in, mn[cur]});
    }
}

node tmp[MAX_N];
void dfs (int x, ll mx) {
    vc[x].clear();
    if (!ls) return vc[x].push_back((node){0, 0}), void();
    dfs(ls, mx), dfs(rs, mx);
    if (vc[ls].empty() || vc[rs].empty()) return;
    for (node& t: vc[ls]) t.in += d[x][0], t.out += d[x][0];
    for (node& t: vc[rs]) t.in += d[x][1], t.out += d[x][1];
    merge(vc[ls], vc[rs], vc[x], mx);
    // for (node& t: vc[ls]) std::swap(t.in, t.out);
    // for (node& t: vc[rs]) std::swap(t.in, t.out);
    merge(vc[rs], vc[ls], vc[x], mx);
    vc[ls].clear(), vc[rs].clear();
    if (vc[x].size() <= 1) return;
    std::sort(vc[x].begin(), vc[x].end(), cmp2);
    int top = 0;
    tmp[++top] = vc[x][0];
    lep (i, 1, vc[x].size()) {
        // printf("%d %d\n", vc[x][i].in, vc[x][i].out);
        if (vc[x][i].out < tmp[top].out) tmp[++top] = vc[x][i];
    }
    // printf("%d:\n", x);
    // for (node t: vc[x]) printf("%d %d\n", t.in, t.out);
    vc[x].clear();
    rep (i, 1, top) vc[x].push_back(tmp[i]);
}

inline bool check (ll x) {
    dfs(1, x);
    if (!vc[1].empty()) return true;
    return false;
}

inline void solve () {
    ll l = 0, r = 0, mid, ans = -1;
    rep (i, 1, N) r += d[i][0] + d[i][1];
    while (l <= r) {
        mid = l + r >> 1LL;
        if (check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    printf("%lld\n", ans);
}

int main () {
#ifdef LOCAL
    FIN("044.in");
#endif
    read(N);
    int u, v;
    rep (i, 2, N) {
        read(u), read(v);
        if (c[u][0]) c[u][1] = i, d[u][1] = v;
        else c[u][0] = i, d[u][0] = v;
    }
    solve();
    return 0;
}

Measured memory_bytes: 35952