#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ls(x) (son[x][0])
#define rs(x) (son[x][1])
#define lv(x) (val[x][0])
#define rv(x) (val[x][1])
#define pll pair<ll,ll>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
const int N=1e6+5;
int son[N][2],val[N][2],fa[N],n;
ll pre[N],mv;
vector<pll >f[N];
bool cmp(pll a,pll b){return a.se<b.se;}
void dfs(int u)
{
    f[u].clear();int l=ls(u),r=rs(u);
    if(!l){f[u].PB(MP(0,0));return;}
    dfs(l),dfs(r);
    for(int i=0;i<f[l].size();i++)f[l][i].fi+=lv(u),f[l][i].se+=lv(u);
    for(int i=0;i<f[r].size();i++)f[r][i].fi+=rv(u),f[r][i].se+=rv(u);
    if(f[l].size()>f[r].size())swap(f[l],f[r]);
    sort(f[l].begin(),f[l].end(),cmp),sort(f[r].begin(),f[r].end());
    pre[0]=f[r][0].se;for(int i=1;i<f[r].size();i++)pre[i]=min(pre[i-1],f[r][i].se);
    for(int l1=0,r1=f[r].size()-1;l1<f[l].size();l1++)
    {
        while(r1>=0&&f[r][r1].fi+f[l][l1].se>mv)r1--;
        if(r1<0)break;f[u].PB(MP(f[l][l1].fi,pre[r1]));
    }
    sort(f[l].begin(),f[l].end()),sort(f[r].begin(),f[r].end(),cmp);
    pre[0]=f[r][0].fi;for(int i=1;i<f[r].size();i++)pre[i]=min(pre[i-1],f[r][i].fi);
    for(int l1=0,r1=f[r].size()-1;l1<f[l].size();l1++)
    {
        while(r1>=0&&f[r][r1].se+f[l][l1].fi>mv)r1--;
        if(r1<0)break;f[u].PB(MP(pre[r1],f[l][l1].se));
    }
}
bool cal(ll md){mv=md;dfs(1);return f[1].size()!=0;}
int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        ls(x)?(rs(x)=i,rv(x)=y):(ls(x)=i,lv(x)=y);
    }
    ll l=0,r=1e10+1,mid,ans;
    while(l<=r){mid=l+r>>1;cal(mid)?(r=mid-1,ans=mid):l=mid+1;}
    printf("%lld\n",ans);
}
