Language: C++

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=140010;
int val[N];
int son[N][2];
ll dis[N];
void calcdis(int u,ll Dis)
{
	dis[u]=Dis;
	int s1=son[u][0],s2=son[u][1];
	if(!s1) return;
	calcdis(s1,Dis+ll(val[s1]));calcdis(s2,Dis+ll(val[s2]));
}
struct data
{
	int s,t;
	data(){}
	data(int s,int t):s(s),t(t){}
};
vector<data>q[N];
int work(int u,int mx,int s1,int s2,data que[])
{
	int sz1=(int)q[s1].size(),sz2=(int)q[s2].size();
	int l=0,cnt=0;
	for(int i=0;i<sz1;i++)
	{
		bool flag=false;
		while(l<sz2&&dis[q[s1][i].t]+dis[q[s2][l].s]-(dis[u]<<1)<=ll(mx))
		{
			l++;
			flag=true;
		}
		if(!flag) continue;
		que[cnt++]=data(q[s1][i].s,q[s2][l-1].t);
	}
	return cnt;
}
data que1[N],que2[N];
bool solve(int u,int mx)
{
	q[u].clear();
	int s1=son[u][0],s2=son[u][1];
	if(!s1)
	{
		q[u].push_back(data(u,u));
		return true;
	}
	if(!solve(s1,mx)||!solve(s2,mx)) return false;
	int cnt1=work(u,mx,s1,s2,que1),cnt2=work(u,mx,s2,s1,que2);
	if(!cnt1&&!cnt2) return false;
	int p1=0,p2=0;
	while(p1<cnt1||p2<cnt2)
	{
		if(p1==cnt1) q[u].push_back(que2[p2++]);
		else if(p2==cnt2) q[u].push_back(que1[p1++]);
		else{
			if(dis[que1[p1].s]<dis[que2[p2].s])
			{
				q[u].push_back(que1[p1]);
				while(p2<cnt2&&dis[que2[p2].t]>=dis[que1[p1].t]) p2++;
				p1++;
			}
			else{
				q[u].push_back(que2[p2]);
				while(p1<cnt1&&dis[que1[p1].t]>=dis[que2[p2].t]) p1++;
				p2++;
			}
		}
	}
	return true;
}
int main()
{
	int Size = 64 << 20; // 64MB
	char *p = (char*)malloc(Size) + Size;
	__asm__("movl %0, %%esp\n" :: "r"(p));
	int n;scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		int Fa;scanf("%d%d",&Fa,&val[i]);
		if(!son[Fa][0]) son[Fa][0]=i;
		else son[Fa][1]=i;
	}
	calcdis(1,0ll);
	int l=0,r=1,now=2;
	while(now*2<n)
	{
		now*=2;
		r++;
	}
	r*=2000000;
	int ans;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(solve(1,mid)==true)
		{
			ans=mid;
			r=mid-1;
		}
		else l=mid+1;
	}
	printf("%d\n",ans);
	return 0;
}

Compiler output:
a.cc:24:12: error: template argument 1 is invalid
   24 | vector<data>q[N];
      |            ^
a.cc:24:12: error: template argument 2 is invalid
a.cc:25:37: error: reference to 'data' is ambiguous
   25 | int work(int u,int mx,int s1,int s2,data que[])
      |                                     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:8: note:                 'struct data'
   18 | struct data
      |        ^~~~
a.cc:25:37: error: 'data' has not been declared
   25 | int work(int u,int mx,int s1,int s2,data que[])
      |                                     ^~~~
a.cc: In function 'int work(int, int, int, int, int*)':
a.cc:27:28: error: request for member 'size' in 'q[s1]', which is of non-class type 'int'
   27 |         int sz1=(int)q[s1].size(),sz2=(int)q[s2].size();
      |                            ^~~~
a.cc:32:25: error: 'sz2' was not declared in this scope; did you mean 'sz1'?
   32 |                 while(l<sz2&&dis[q[s1][i].t]+dis[q[s2][l].s]-(dis[u]<<1)<=ll(mx))
      |                         ^~~
      |                         sz1
a.cc:32:39: error: invalid types 'int[int]' for array subscript
   32 |                 while(l<sz2&&dis[q[s1][i].t]+dis[q[s2][l].s]-(dis[u]<<1)<=ll(mx))
      |                                       ^
a.cc:32:55: error: invalid types 'int[int]' for array subscript
   32 |                 while(l<sz2&&dis[q[s1][i].t]+dis[q[s2][l].s]-(dis[u]<<1)<=ll(mx))
      |                                                       ^
a.cc:38:28: error: reference to 'data' is ambiguous
   38 |                 que[cnt++]=data(q[s1][i].s,q[s2][l-1].t);
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:8: note:                 'struct data'
   18 | struct data
      |        ^~~~
a.cc:38:38: error: invalid types 'int[int]' for array subscript
   38 |                 que[cnt++]=data(q[s1][i].s,q[s2][l-1].t);
      |                                      ^
a.cc:38:49: error: invalid types 'int[int]' for array subscript
   38 |                 que[cnt++]=data(q[s1][i].s,q[s2][l-1].t);
      |                                                 ^
a.cc: At global scope:
a.cc:42:1: error: reference to 'data' is ambiguous
   42 | data que1[N],que2[N];
      | ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:8: note:                 'struct data'
   18 | struct data
      |        ^~~~
a.cc: In function 'bool solve(int, int)':
a.cc:45:14: error: request for member 'clear' in 'q[u]', which is of non-class type 'int'
   45 |         q[u].clear();
      |              ^~~~~
a.cc:49:22: error: request for member 'push_back' in 'q[u]', which is of non-class type 'int'
   49 |                 q[u].push_back(data(u,u));
      |                      ^~~~~~~~~
a.cc:49:32: error: reference to 'data' is ambiguous
   49 |                 q[u].push_back(data(u,u));
      |                                ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:8: note:                 'struct data'
   18 | struct data
      |        ^~~~
a.cc:53:34: error: 'que1' was not declared in this scope
   53 |         int cnt1=work(u,mx,s1,s2,que1),cnt2=work(u,mx,s2,s1,que2);
      |                                  ^~~~
a.cc:54:20: error: 'cnt2' was not declared in this scope; did you mean 'cnt1'?
   54 |         if(!cnt1&&!cnt2) return false;
      |                    ^~~~
      |                    cnt1
a.cc:56:27: error: 'cnt2' was not declared in this scope; did you mean 'cnt1'?
   56 |         while(p1<cnt1||p2<cnt2)
      |                           ^~~~
      |                           cnt1
a.cc:58:35: error: request for member 'push_back' in 'q[u]', which is of non-class type 'int'
   58 |                 if(p1==cnt1) q[u].push_back(que2[p2++]);
      |                                   ^~~~~~~~~
a.cc:58:45: error: 'que2' was not declared in this scope
   58 |                 if(p1==cnt1) q[u].push_back(que2[p2++]);
      |                                             ^~~~
a.cc:59:40: error: request for member 'push_back' in 'q[u]', which is of non-class type 'int'
   59 |                 else if(p2==cnt2) q[u].push_back(que1[p1++]);
      |                                        ^~~~~~~~~
a.cc:61:48: error: 'que2' was not declared in this scope
   61 |                         if(dis[que1[p1].s]<dis[que2[p2].s])
      |                                                ^~~~
a.cc:63:38: error: request for member 'push_back' in 'q[u]', which is of non-class type 'int'
   63 |                                 q[u].push_back(que1[p1]);
      |                                      ^~~~~~~~~
a.cc:68:38: error: request for member 'push_back' in 'q[u]', which is of non-class type 'int'
   68 |                                 q[u].push_back(que2[p2]);
      |                                      ^~~~~~~~~