#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
using std::cin; using std::cout;
using std::endl;
using std::bitset; using std::map;
using std::queue; using std::priority_queue;
using std::set; using std::string;
using std::stringstream; using std::vector;
using std::pair; using std::make_pair;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<ULL, ULL> puu;
#ifdef DEBUG
using std::cerr;
#define pass cerr << "[" << __FUNCTION__ << "] : line = " << __LINE__ << endl;
#define display(x) cerr << #x << " = " << x << endl;
#define displaya(a, st, n)                      \
	{                                           \
		cerr << #a << " = {";                   \
		for (int qwq = (st); qwq <= (n); ++qwq) \
			if (qwq == (st))                    \
				cerr << a[qwq];                 \
			else                                \
				cerr << ", " << a[qwq];         \
		cerr << "}" << endl;                    \
	}
#define displayv(a) displaya(a, 0, (int)(a.size() - 1))
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#include <ctime>
class MyTimer {
	clock_t st;
public:
	MyTimer() { cerr << std::fixed << std::setprecision(0); reset(); }
	~MyTimer() { report(); }
	void reset() { st = clock_t(); }
	void report() {  cerr << "Time consumed: " << (clock() - st) * \
		1e3 / CLOCKS_PER_SEC << "ms" << endl; }
} myTimer;
#else
#define cerr if(false) std::cout
#define pass ;
#define display(x) ;
#define displaya(a, st, n) ;
#define displayv(a) ;
#define eprintf(...) if(0) fprintf(stderr, __VA_ARGS__)
class MyTimer {
public: void reset() {} void report() {}
} myTimer;
#endif

template<typename A, typename B>
std::ostream& operator << (std::ostream &cout, const pair<A, B> &x) {
	return cout << "(" << x.first << ", " << x.second << ")";
}
template<typename T1, typename T2>
inline bool chmin(T1 &a, const T2 &b) { return a > b ? a = b, true : false; }
template<typename T1, typename T2>
inline bool chmax(T1 &a, const T2 &b) { return a < b ? a = b, true : false; }

const int maxN = 131072 + 233;
const LL maxA = (LL)maxN * maxN;
int n, p[maxN];
LL a[maxN];
vector<int> G[maxN];
vector<pll> lst[maxN];

void initialize() {
	std::ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 2; i <= n; ++ i) {
		cin >> p[i] >> a[i];
//		p[i] = i / 2; a[i] = 131072;
		G[p[i]].push_back(i);
	}
}

/*
7
1 0
1 0
2 1
2 2
3 4
3 2

*/

LL thres;
bool check() {
	for(int u = 1; u <= n; ++ u) lst[u].clear();
	vector<pll> s;
	for(int u = n; u >= 1; -- u) {
		if(G[u].empty()) {
			lst[u].emplace_back(0, 0);
		} else {
			assert(G[u].size() == 2u);
			int x = G[u][0], y = G[u][1];
			for(auto &p : lst[x]) p.first += a[x], p.second += a[x];
			for(auto &p : lst[y]) p.first += a[y], p.second += a[y];
			if(lst[x].size() > lst[y].size()) lst[x].swap(lst[y]);
			if(lst[x].empty() || lst[y].empty()) return false;
			auto i = lst[y].begin(); s.clear();
			for(const auto &p : lst[x]) {
				while((i + 1) != lst[y].end() && p.second + (i + 1)->first <= thres) ++ i;
				if(p.second + i->first <= thres) s.emplace_back(p.first, i->second);
			}
			auto insert = [&](LL x, LL y) -> bool {
				if(lst[u].size() && y >= lst[u].back().second) return false;
				if(lst[u].size() && x == lst[u].back().first) {
					if(y <= lst[u].back().second) lst[u].pop_back();
				}
				lst[u].emplace_back(x, y);
				return true;
			};
			i = s.begin();
			auto j = s.rbegin();
			while(i != s.end() && j != s.rend()) {
				if(i->first < j->second) insert(i->first, i->second), ++ i;
				else insert(j->second, j->first), ++ j;
			}
			while(i != s.end()) insert(i->first, i->second), ++ i;
			while(j != s.rend()) insert(j->second, j->first), ++ j;
			
//			auto p = lst[u].begin();
//			auto q = lst[u].rbegin();
//			display(u); displayv(lst[x]); displayv(lst[y]); displayv(s); displayv(lst[u]);
//			for(; p != lst[u].end(); ++p, ++q) assert(*p == pll(q->second, q->first));
		}
	}
	return lst[1].size() > 0;
}

int main() {
	initialize();
	LL L = 0, R = (LL)maxN * n;
	while(L < R) {
		thres = (L + R) / 2;
		if(check()) R = thres;
		else L = thres + 1;
	}
	cout << L << endl;
	return 0;
}


Measured memory_bytes: 31364