Language: C++

//============================================================================
// Author       : Sun YaoFeng
//============================================================================

//#pragma 	comment(linker, "/STACK:100240000,100240000")
//#include	<cstdio>
//#include	<cstdlib>
//#include	<cstring>
//#include	<algorithm>

#include	<bits/stdc++.h>

using	namespace	std;

#define DB		double
#define	lf		else if
#define I64		long long
#define	Rd()	(rand()<<15|rand())
#define For(i,a,b)	for(int i=a,lim=b;i<=lim;i++)
#define Rep(i,a,b)	for(int i=a,lim=b;i>=lim;i--)

#define	fi	first
#define se	second
#define MK	make_pair
#define PA	pair<I64, I64>

//#define	min(a,b)	((a)<(b)?(a):(b))
//#define	max(a,b)	((a)<(b)?(b):(a))

#define	CH	(ch=getchar())
int		IN()	{
		int x= 0, f= 0, ch;
		for	(; CH < '0' || ch > '9';)	f= (ch == '-');
		for	(; ch >= '0' && ch <= '9'; CH)	x= x*10 + ch -'0';
		return	f? -x : x;
}

#define	n	150005

int		N, A[n][2], B[n][2];

vector<PA>	S[n];

bool	DFS(int i, I64 Mid)	{
		if	(! A[i][0])	{
			S[i].push_back(MK(0, 0));
			return	1;
		}
		
		int j= A[i][0], k= A[i][1];
		if	(! DFS(j, Mid))	return	0;
		if	(! DFS(k, Mid))	return	0;
		
		For(u, 0, S[j].size()-1)	S[j][u]= MK(S[j][u].fi + B[i][0], S[j][u].se + B[i][0]);
		For(v, 0, S[k].size()-1)	S[k][v]= MK(S[k][v].fi + B[i][1], S[k][v].se + B[i][1]);
		
		static	vector<PA>	H1, H2, G;
		H1.clear();
		H2.clear();
		G.clear();
		
		for (int u= 0, v= 0; u < S[j].size(); u++)	{
			for (; v < S[k].size() && S[j][u].se + S[k][v].fi <= Mid; v++);
			if	(v && S[j][u].se + S[k][v-1].fi <= Mid)	H1.push_back(MK(S[j][u].fi, S[k][v-1].se));
		}
		
		for (int u= 0, v= 0; u < S[j].size(); u++)	{
			for (; v < S[k].size() && S[k][v].se + S[j][u].fi > Mid; v++);
			if	(v == S[k].size())	break;
			H2.push_back(MK(S[k][v].fi, S[j][u].se));
		}
		
		for (int u= 0, v= 0; u < H1.size() || v < H2.size(); )	{
			if	(u == H1.size())	G.push_back(H2[v++]);
			lf	(v == H2.size())	G.push_back(H1[u++]);
			lf	(H1[u].fi < H2[v].fi)	G.push_back(H1[u++]);
				else	G.push_back(H2[v++]);
		}
		
		for (int u= 0; u < G.size(); u++)
			if	(S[i].size() == 0 || G[u].se < S[i][S[i].size()-1].se)	S[i].push_back(G[u]);
		
		/* puts("------");
		printf("%d\n", i);
		For(t, 1, H1.size())	printf("(%d , %d) ", H1[t-1].fi, H1[t-1].se);
		puts("");
		
		For(t, 1, H2.size())	printf("(%d , %d) ", H2[t-1].fi, H2[t-1].se);
		puts(""); */
		
		return	S[i].size() > 0;
}

int		main(int argc, char* argv[]){
		N= IN();
		For(i, 2, N)	{
			int j= IN(), v= IN();
			
			if	(! A[j][0])	A[j][0]= i, B[j][0]= v;
				else	A[j][1]= i, B[j][1]= v;
		}
		
	 	I64 l= 1-, r= 1ll << 35;
		for (; l+1 < r;)	{
			I64	Mid= (l + r) >> 1;
			For(i, 1, N)	S[i].clear();
			
			if	(DFS(1, Mid))	r= Mid;
				else	l= Mid;
		}
		
		cout << r << endl;
	
	// 	printf("%d\n", DFS(1, 6));
	 
		return	0;
}

Compiler output:
a.cc: In function 'int main(int, char**)':
a.cc:103:26: error: expected primary-expression before ',' token
  103 |                 I64 l= 1-, r= 1ll << 35;
      |                          ^
a.cc:104:30: error: 'r' was not declared in this scope
  104 |                 for (; l+1 < r;)        {
      |                              ^
a.cc:112:25: error: 'r' was not declared in this scope
  112 |                 cout << r << endl;
      |                         ^