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

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, LL> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL) INF;

const int MAX = 200200;

vector<PII> g[MAX];
vector<PII> A[MAX];

vector<PII> s, t;

void add(vector<PII>& c, PII val)
{
	if (c.empty() || c.back().second > val.second) c.PB(val);
}

void merge(vector<PII>& a, vector<PII>& b, vector<PII>& c)
{
	c.clear();
	int ind = 0;
	FOR (i, 0, SZ(a))
	{
		while(ind < SZ(b) && b[ind] < a[i])
		{
			add(c, b[ind]);
			ind++;
		}

		add(c, a[i]);
	}
}

void go(int x, LL m)
{
	A[x].clear();
	if (SZ(g[x]) == 0)
	{
		A[x].PB(MP(0, 0));
		return;
	}

	go(g[x][0].first, m);
	go(g[x][1].first, m);

	int w1 = g[x][0].second;
	int w2 = g[x][1].second;

	vector<PII>& a = A[g[x][0].first];
	vector<PII>& b = A[g[x][1].first];

	s.clear();
	t.clear();

	int ind = 0;

	FOR (i, 0, SZ(a))
	{
		LL x = a[i].first;
		LL y = a[i].second;

		LL mn = LINF;
		if (ind < 0) ind = 0;

		while(ind < SZ(b))
		{
			LL u = b[ind].first;
			LL v = b[ind].second;

			if (y + u + w1 + w2 <= m)
			{
				mn = min(mn, v);
				ind++;
			}
			else break;
		}
		ind--;

		if (mn != LINF)
		{
			s.PB(MP(x + w1, mn + w2));
			t.PB(MP(mn + w2, x + w1));
		}
	}

	merge(s, t, A[x]);

	sort(ALL(A[x]));
}

bool ok(LL m)
{
	go(0, m);

	if (SZ(A[0])) return true;
	return false;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//ios::sync_with_stdio(false); cin.tie(0);


	int n;
	scanf("%d", &n);
	FOR (i, 1, n)
	{
		int p, a;
		scanf("%d%d", &p, &a);
		p--;

		g[p].PB(MP(i, a));
	}

/*	cout<<ok(15)<<endl;

	FOR (i, 0, n)
	{
		cout<<i + 1<<": ";
		FOR (j, 0, SZ(A[i]))
		{
			cout<<"("<<A[i][j].first<<", "<<A[i][j].second<<") ";
		}
		cout<<endl;
	}

	return 0;*/

	LL l = -1, r = MAX * (LL)MAX;
	while(r - l > 1)
	{
		LL m = (l + r) / 2;

	//	cout<<l<<' '<<r<<' '<<m<<endl;

		if (ok(m)) r = m;
		else l = m;
	}

	cout<<r<<endl;

}

