// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



inline int read()

{

    int x=0;char ch=getchar();

    while(ch<'0'|| ch>'9')ch=getchar();

    while('0'<=ch && ch<='9')x=x*10+(ch^48),ch=getchar();

    return x;

}



typedef long long ll;

typedef pair<ll,ll> pr;

typedef vector<pr> vec;

#define x first

#define y second

const int N=131100;



int n,fa[N];

vec g[N],f[N];

ll mid,mv[N];



inline bool cmpx(pr a,pr b){return a.x<b.x;}

inline bool cmpy(pr a,pr b){return a.y<b.y;}



inline void work(vec &ls,vec &rs,vec &f)

{

    if(ls.size()>rs.size())swap(ls,rs);

    int el=ls.size(),er=rs.size();



    sort(ls.begin(),ls.end(),cmpy);

    sort(rs.begin(),rs.end(),cmpx);



    mv[0]=rs[0].y;

    for(int i=1;i<er;i++)

        mv[i]=min(mv[i-1],rs[i].y);



    for(int i=0,j=er-1;i<el;i++)

    {

        while(j>=0 && ls[i].y+rs[j].x>mid)j--;

        if(j>=0)f.push_back(pr(ls[i].x,mv[j]));

    }



    sort(ls.begin(),ls.end(),cmpx);

    sort(rs.begin(),rs.end(),cmpy);

    mv[0]=rs[0].x;

    for(int i=1;i<er;i++)

        mv[i]=min(mv[i-1],rs[i].x);



    for(int i=0,j=er-1;i<el;i++)

    {

        while(j>=0 && ls[i].x+rs[j].y>mid)j--;

        if(j>=0)f.push_back(pr(mv[j],ls[i].y));

    }

}



inline void dfs(int u)

{

    if(!f[u].empty())f[u].clear();

    if(g[u].empty())

    {

        f[u].push_back(pr(0,0));

        return;

    }



    for(int i=0,e=g[u].size();i<e;i++)

        dfs(g[u][i].x);



    vec &ls=f[g[u][0].x];

    for(int i=0,e=ls.size(),c=g[u][0].y;i<e;i++)

        ls[i].x+=c,ls[i].y+=c;

    vec &rs=f[g[u][1].x];

    for(int i=0,e=rs.size(),c=g[u][1].y;i<e;i++)

        rs[i].x+=c,rs[i].y+=c;



    work(ls,rs,f[u]);

    ls.clear();

    rs.clear();

}



int main()

{

    n=read();

    for(int i=2,a;i<=n;i++)

    {

        fa[i]=read();

        g[fa[i]].push_back(pr(i,read()));

    }



    ll l=0,r=1e10,ans=1e10;

    while(l<=r)

    {

        mid=l+r>>1;

        dfs(1);

        if(f[1].empty())

            l=mid+1;

        else

            r=mid-1,ans=mid;

    }

    printf("%lld\n",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.9443092027301307x):
#include <cctype>

#include <cstdio>

#include <algorithm>

#include <vector>

using namespace std;

typedef long long int64;

inline int read(int f = 1, int x = 0, char ch = ' ')

{

    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;

    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();

    return f*x; 

}

const int N = 131072+5;

int n, w[N], ch[2][N], c[N];

int64 d[N], l = 0, r = 1e10, mid, ans, pre[N], dfn[N];

vector<pair<int64, int64> > f[N];

inline bool cmpx(pair<int64, int64> a, pair<int64, int64> b) { return a.first < b.first; }

inline bool cmpy(pair<int64, int64> a, pair<int64, int64> b) { return a.second < b.second; }

void insert(vector<pair<int64, int64> > &ls, vector<pair<int64, int64> > &rs, vector<pair<int64, int64> > &s)

{

    int sy = ls.size(), sz = rs.size();

    if(sy > sz) swap(sy, sz), swap(ls, rs);

    sort(ls.begin(), ls.end(), cmpy);

    sort(rs.begin(), rs.end(), cmpx);

    pre[0] = rs[0].second;

    for(int i = 1; i < sz; ++i) pre[i] = min(pre[i-1], rs[i].second);

    for(int i = 0, j = sz-1; i < sy; ++i)

    {

        while(j >= 0&&ls[i].second+rs[j].first > mid) --j;

        if(j >= 0) s.push_back(make_pair(ls[i].first, pre[j]));

    }

    sort(ls.begin(), ls.end(), cmpx);

    sort(rs.begin(), rs.end(), cmpy);

    pre[0] = rs[0].first;

    for(int i = 1; i < sz; ++i) pre[i] = min(pre[i-1], rs[i].first);

    for(int i = 0, j = sz-1; i < sy; ++i)

    {

        while(j >= 0&&ls[i].first+rs[j].second > mid) --j;

        if(j >= 0) s.push_back(make_pair(pre[j], ls[i].second));

    }

}

bool valid()

{

    for(int i = n; i; --i)

    {

        int x = dfn[i]; if(f[x].size()) f[x].clear();

        if(!c[x]) f[x].push_back(make_pair(d[x], d[x]));

        else

        {

            int y = ch[0][x], z = ch[1][x];

            mid += d[x]<<1;

            insert(f[y], f[z], f[x]);

            mid -= d[x]<<1; f[y].clear(), f[z].clear();

        }       

        if(f[x].empty()) return false; 

    }

    return true;

}

void dfs(int x)

{

    d[x] += w[x], dfn[++n] = x;

    if(!c[x]) return;

    d[ch[0][x]] = d[ch[1][x]] = d[x];

    dfs(ch[0][x]), dfs(ch[1][x]);

}

int main()

{

    n = read(); 

    for(int i = 2; i <= n; ++i)

    {

        int x = read(); w[i] = read();

        ch[c[x]++][x] = i;

    }

    n = 0, dfs(1);

    while(l <= r)

    {

        mid = (l+r)>>1;

        if(valid()) ans = mid, r = mid-1;

        else l = mid+1;

    }

    printf("%lld\n", ans);

    return 0;

}