#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define maxn 140010
#define inf 999999999999999
using namespace std;
typedef long long ll;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int ch[maxn][2],val[maxn][2],leaf[maxn];
struct P{
    ll a,b;
};
bool cmp1(P x,P y)
{
    return x.a<y.a;
}
bool cmp2(P x,P y)
{
    return x.b<y.b;
}
vector<P>e[maxn];
ll res;
void solve(int x,ll mid)
{
    e[x].clear();
    if(leaf[x])
    {
        e[x].push_back((P){0,0});
        return;
    }
    solve(ch[x][0],mid);
    solve(ch[x][1],mid);

    int l=ch[x][0],r=ch[x][1],v1=val[x][0],v2=val[x][1];
    if(e[l].size()>e[r].size())  swap(l,r),swap(v1,v2);
    int l1=e[l].size(),l2=e[r].size();

    sort(e[l].begin(),e[l].end(),cmp2);
    sort(e[r].begin(),e[r].end(),cmp1);
    int pos=-1;
    ll mn=inf;
    for(int i=l1-1;i>=0;i--)
    {
        while(e[l][i].b+e[r][pos+1].a<=mid-v1-v2&&pos<l2-1)  pos++,mn=min(mn,e[r][pos].b);
        if(pos!=-1)  e[x].push_back((P){e[l][i].a+v1,mn+v2});
    }

    sort(e[l].begin(),e[l].end(),cmp1);
    sort(e[r].begin(),e[r].end(),cmp2);
    pos=-1;
    mn=inf;
    for(int i=l1-1;i>=0;i--)
    {
        while(e[l][i].a+e[r][pos+1].b<=mid-v1-v2&&pos<l2-1)  pos++,mn=min(mn,e[r][pos].a);
        if(pos!=-1)  e[x].push_back((P){mn+v2,e[l][i].b+v1});
    }
}
int main()
{
    n=read();
    for(int i=2;i<=n;i++)
    {
        int x=read(),y=read();
        if(!ch[x][0])  ch[x][0]=i,val[x][0]=y;
        else           ch[x][1]=i,val[x][1]=y;
    }
    for(int i=1;i<=n;i++)
      if(!ch[i][0])  leaf[i]=1;
    ll l=0,r=1000000000000ll;
    while(l<=r)
    {
        int mid=(l+r)/2;
        solve(1,mid);
        if(e[1].size()!=0)
        {
            res=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    cout<<res<<endl;
    return 0;
}