#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define x first
#define y second

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}

vector<vector<pll>> E;

vector<pll> solve(int u, ll T) {
    if (E[u].empty()) {
        return {{0LL,0LL}};
    } else {
        auto A = solve(E[u][0].x, T);
        auto B = solve(E[u][1].x, T);
        for (auto &a: A) { a.x += E[u][0].y; a.y += E[u][0].y; }
        for (auto &b: B) { b.x += E[u][1].y; b.y += E[u][1].y; }
        vector<pll> Ans;
        for (int t = 0; t < 4; ++t) {
            int i = 0;
            for (pll a: A) {
                while (i < B.size() && B[i].y + a.x > T) ++i;
                if (i != B.size()) Ans.emplace_back(min(a.y,B[i].x),max(a.y,B[i].x));
            }
            
            if (t == 1) {
                for (pll&a:A) swap(a.x, a.y);
                reverse(A.begin(),A.end());
            } else {
                for (pll&b:B) swap(b.x, b.y);
                reverse(B.begin(),B.end());
            }
        }

        sort(Ans.begin(),Ans.end());
        vector<pll> F;
        for (pll ans: Ans) if (F.empty() || F.back().y > ans.y) F.push_back(ans);
        return F;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    int N; cin >> N;
    E.resize(N);
    for (int i = 1; i < N; ++i) {
        int p, c; cin >> p >> c;
        --p;
        E[p].push_back({i,c});
    }

    cout << bsl(0LL, 18000000000LL, [](ll v) {
        auto s = solve(0, v);
        return !s.empty();
    }) << endl;
}
