Language: C++

#include<bits/stdc++.h>

#define N 131075
#define INF 0x7fffffff

using namespace std;

struct edge
{
	int a,b;
};

bool operator < (edge a,edge b)
{
	if(a.a==b.a)return a.b<b.b;
	return a.a<b.a;
}

int n,sum;
int cnt,head[N],to[N<<1],w[N<<1],nxt[N<<1];
int ch[N][2],pre[N],size[N];

vector<edge>g[N],o;

void adde(int u,int v,int wi)
{
	to[++cnt]=v;
	w[cnt]=wi;
	nxt[cnt]=head[u];
	head[u]=cnt;
}

void dfs(int u,int fa)
{
	int tot=0;
	size[u]=1;
	for(int i=head[u];i;i=nxt[i])
	{
		if(to[i]!=fa)
		{
			ch[u][tot++]=to[i];
			pre[to[i]]=w[i];
			dfs(to[i],u);
			size[u]+=size[to[i]];
		}
	}
}

bool get(int a,int b,int x,int b1,int &a2,int &b2)
{
	int l=0,r=g[b].size()-1;
	while(l<r-1)
	{
		int mid=(l+r)>>1;
		a2=g[b][mid].a,b2=g[b][mid].b;
		if(b1+pre[a]+pre[b]+a2<=x)l=mid;
		else r
		.0=mid;
	}
	a2=g[b][r].a,b2=g[b][r].b;
	if(b1+pre[a]+pre[b]+a2<=x)return true;
	a2=g[b][l].a,b2=g[b][l].b;
	if(b1+pre[a]+pre[b]+a2<=x)return true;
	return false;
}

void merge(int u,int x)
{
	if(ch[u][0]) 
	{
		int a=ch[u][0],b=ch[u][1];
		merge(a,x),merge(b,x);
		o.clear();
		if(size[a]>size[b])swap(a,b);
		if(!g[a].size()||!g[b].size())
			return;
		for(int i=0;i<g[a].size();i++)
		{
			int a1=g[a][i].a,b1=g[a][i].b,a2,b2;
			if(!get(a,b,x,b1,a2,b2))
				return;
			o.push_back((edge){a1+pre[a],b2+pre[b]});
			o.push_back((edge){b2+pre[b],a1+pre[a]});
		}
		sort(o.begin(),o.end());
		int minn=INF;
		for(int i=0;i<o.size();i++)
		{
			if(o[i].b<minn)
			{
				minn=o[i].b;
				g[u].push_back(o[i]);
			}
		}
	}
	else g[u].push_back((edge){0,0});
}

bool check(int x)
{
	for(int i=1;i<=n;i++)
		g[i].clear();
	merge(1,x);
	return g[1].size()>0;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int a,v;
		scanf("%d%d",&a,&v);
		adde(i+1,a,v),adde(a,i+1,v);
		sum+=v;
	}
	dfs(1,0);
	int l=0,r=sum,ans;
	while(l<r-1)
	{
		int mid=(l+r)>>1;
		if(check(mid))r=mid;
		else l=mid;
	}
	if(check(l)) ans=l;
	else ans=r;
	printf("%d\n",ans);
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:36:9: error: reference to 'size' is ambiguous
   36 |         size[u]=1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:21: note:                 'int size [131075]'
   21 | int ch[N][2],pre[N],size[N];
      |                     ^~~~
a.cc:44:25: error: reference to 'size' is ambiguous
   44 |                         size[u]+=size[to[i]];
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:21: note:                 'int size [131075]'
   21 | int ch[N][2],pre[N],size[N];
      |                     ^~~~
a.cc:44:34: error: reference to 'size' is ambiguous
   44 |                         size[u]+=size[to[i]];
      |                                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:21: note:                 'int size [131075]'
   21 | int ch[N][2],pre[N],size[N];
      |                     ^~~~
a.cc: In function 'bool get(int, int, int, int, int&, int&)':
a.cc:57:23: error: expected ';' before numeric constant
   57 |                 else r
      |                       ^
      |                       ;
   58 |                 .0=mid;
      |                 ~~     
a.cc: In function 'void merge(int, int)':
a.cc:74:20: error: reference to 'size' is ambiguous
   74 |                 if(size[a]>size[b])swap(a,b);
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:21: note:                 'int size [131075]'
   21 | int ch[N][2],pre[N],size[N];
      |                     ^~~~
a.cc:74:28: error: reference to 'size' is ambiguous
   74 |                 if(size[a]>size[b])swap(a,b);
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:21: note:                 'int size [131075]'
   21 | int ch[N][2],pre[N],size[N];
      |                     ^~~~