// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<LL, LL> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 2e5 + 5;

int N;
int A[MAXN], V[MAXN];
vector<int> G[MAXN];

void input()
{
    read(N);
    for (int i = 2; i <= N; ++i) {
        read(A[i]); read(V[i]);
        G[A[i]].push_back(i);
    }
}

bool check(LL limit)
{
    static vector<Pii> list[MAXN];

    for (int u = N; u >= 1; --u) {
        list[u].clear();
        if (SZ(G[u]) == 0) {
            list[u].push_back(MP(0, 0));
            continue;
        }

        int lc = G[u][0], rc = G[u][1];
        int vl = V[lc], vr = V[rc];
        vector<Pii> &ll = list[lc], &lr = list[rc];
        vector<Pii> L, R, tmp;

        for (int i = 0, j = 0; i < SZ(ll); ++i) {
            while (j < SZ(lr) && ll[i].y + lr[j].x + vl + vr <= limit) {
                ++j;
            }
            if (j > 0)
                L.push_back(MP(ll[i].x + vl, lr[j - 1].y + vr));
        }
        for (int i = 0, j = 0; i < SZ(lr); ++i) {
            while (j < SZ(ll) && lr[i].y + ll[j].x + vl + vr <= limit) {
                ++j;
            }
            if (j > 0)
                R.push_back(MP(lr[i].x + vr, ll[j - 1].y + vl));
        }

        tmp.resize(SZ(L) + SZ(R));
        std::merge(ALL(L), ALL(R), tmp.begin());
        for (int i = 0; i < SZ(tmp); ++i) {
            if (SZ(list[u]) == 0 || list[u].back().y > tmp[i].y)
                list[u].push_back(tmp[i]);
        }

        // printf("u=%d\n", u);
        // printf("%d %d %d %d\n", lc, rc, vl, vr);
        // printf("L: ");
        // for (auto x : L) printf("(%lld,%lld) ", x.x, x.y);
        // puts("");
        // printf("R: ");
        // for (auto x : R) printf("(%lld,%lld) ", x.x, x.y);
        // puts("");
        // printf("U: ");
        // for (auto x : list[u]) printf("(%lld,%lld) ", x.x, x.y);
        // puts("");

        if (SZ(list[u]) == 0)
            return false;
    }

    return true;
}

void solve()
{
    LL l = -1, r = 1ll << 36;
    while (l + 1 < r) {
        LL mid = (l + r) >> 1;
        // printf("(%lld,%lld] checking %lld\n", l, r, mid);
        if (check(mid))
            r = mid;
        else
            l = mid;
    }

    printf("%lld\n", r);
}

int main()
{
#ifdef __DEBUG
    freopen("E.in", "r", stdin);
    freopen("E.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 薄于当世，而荣于后世。
//   --与杨京兆凭书，柳宗元


Measured memory_bytes: 39576