#include<bits/stdc++.h>
#define N ((1<<17)+3)
#define LL long long
using namespace std;
vector<int> son[N];
vector<pair<LL,LL> > dp[N];
LL mid,dis[N];
vector<pair<LL,LL> > temp;
bool DFS(int u,int fr){
    dp[u].clear();
    for(int i:son[u]) 
        if(!DFS(i,u))
            return false;
    if(son[u].size()==0){
        dp[u].push_back(make_pair(0,0));
    }else{
        temp.clear();
        int l=son[u][0],r=son[u][1];LL limit=mid-dis[l]-dis[r];
        if(dp[l].size()>dp[r].size()) swap(l,r);
        for(int i=0,p=0;i<dp[l].size()&&p<dp[r].size();i++){
            while(p+1<dp[r].size() && dp[l][i].second+dp[r][p+1].first <= limit) p++;
            if(dp[l][i].second+dp[r][p].first <= limit) 
                temp.push_back(make_pair( dp[l][i].first+dis[l],dp[r][p].second+dis[r] ));
        }
        for(int i=temp.size()-1;i>=0;i--) 
            temp.push_back(make_pair(temp[i].second,temp[i].first));
        sort(temp.begin(),temp.end());
        for(pair<LL,LL> x:temp){
            if(dp[u].size()==0|| x.second < dp[u][dp[u].size()-1].second )
                dp[u].push_back(x);
        }
    }
    return dp[u].size()!=0;
}

int n,fa[N];

int main(){
    scanf("%d",&n);
    for(int i=2;i<=n;i++) scanf("%d%lld",&fa[i],&dis[i]),son[fa[i]].push_back(i);
    LL l=-1,r=1e12;//(]
    while(l+1<r){
        mid=(l+r)>>1;
        if(!DFS(1,0)) l=mid;
        else r=mid;
    }
    cout<<r<<endl;
    return 0;
}