#include <bits/stdc++.h>
using namespace std;

int child1[131077];
int child2[131077];
int w1[131077];
int w2[131077];
int n;
long long cur;

vector<pair<int,int> > func(int node){
    if (child1[node]==-1){
        vector<pair<int,int> > ans;
        ans.push_back({0,0});
        return ans;
    }
    else{
        vector<pair<int,int> > v1 = func(child1[node]);
        vector<pair<int,int> > v2 = func(child2[node]);
        vector<pair<int,int> > ans1;
        vector<pair<int,int> > ans2;
        int point = 0;
        for (auto x : v1){
            while (point<v2.size() && v2[point].second+x.first+w1[node]+w2[node]>cur){
                point++;
            }
            if (point!=v2.size())
            ans1.push_back({v2[point].first+w2[node],x.second+w1[node]});
        }
        point = 0;
        for (auto x : v2){
            while (point<v1.size() && v1[point].second+x.first+w1[node]+w2[node]>cur){
                point++;
            }
            if (point!=v1.size())
            ans2.push_back({v1[point].first+w1[node],x.second+w2[node]});
        }
        int a = 0;
        int b = 0;
        vector<pair<int,int> > finalans;
        while (a<ans1.size()||b<ans2.size()){
            int tobe = 0;
            if (a>=ans1.size()){
                tobe = 2;
            }
            else if (b>=ans2.size()){
                tobe = 1;
            }
            else{
                if (ans1[a]<ans2[b]){
                    tobe = 1;
                }
                else{
                    tobe = 2;
                }
            }
            if (tobe==1){
                if (!finalans.empty() && finalans[finalans.size()-1].first==ans1[a].first){
                    finalans[finalans.size()-1].second = min(finalans[finalans.size()-1].second,ans1[a].second);
                }
                else if (!finalans.empty() && finalans[finalans.size()-1].second>ans1[a].second){
                    finalans.push_back(ans1[a]);
                }
                else if (finalans.empty()){
                    finalans.push_back(ans1[a]);
                }
                a++;
            }
            else{
                if (!finalans.empty() && finalans[finalans.size()-1].first==ans2[b].first){
                    finalans[finalans.size()-1].second = min(finalans[finalans.size()-1].second,ans2[b].second);
                }
                else if (!finalans.empty() && finalans[finalans.size()-1].second>ans2[b].second){
                    finalans.push_back(ans2[b]);
                }
                else if (finalans.empty()){
                    finalans.push_back(ans2[b]);
                }
                b++;
            }
        }
        return finalans;

    }
}

int main(){
memset(child1,-1,sizeof(child1));
memset(child2,-1,sizeof(child2));
scanf("%d",&n);
for (int x = 0; x<n-1; x++){
    int a,b;
    scanf("%d%d",&a,&b);
    if (child1[a]==-1){
        child1[a]=x+2;
        w1[a] = b;
    }
    else{
        child2[a] = x+2;
        w2[a] = b;
    }
}
long long a = -1;
long long b = 10000000000000LL;
while (a+1<b){
    cur = (a+b)/2;
    if (func(1).empty()){
        a = cur;
    }
    else{
        b = cur;
    }
}
printf("%lld",b);
}


Measured memory_bytes: 19712