Language: C++

#include <bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
const int Maxn=2000000;
struct edge{
	int v,nxt,c;
}e[Maxn<<1|5];
int p[Maxn+5],eid;
void init(){
	memset(p,-1,sizeof p);
	eid=0;
}
void insert(int u,int v,int c){
	e[eid].v=v;
	e[eid].c=c;
	e[eid].nxt=p[u];
	p[u]=eid++;
}
int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
void dfs(int u,int fa){
	msize[u]=0;
	a[++r]=u;
	size[u]=1;
	for(int i=p[u];i+1;i=e[i].nxt){
		int v=e[i].v,c=e[i].c;
		if(vis[v]||v==fa){
			continue;
		}
		dfs(v,u);
		size[u]+=size[v];
		if(size[v]>msize[u]){
			msize[u]=size[v];
		}
	}
}
void dfss(int u,int fa,ll sum){
	dis[u]=sum; 
	for(int i=p[u];i+1;i=e[i].nxt){
		int v=e[i].v,c=e[i].c;
		if(v==fa||vis[v]){
			continue;
		}
		dfss(v,u,sum+c);
	}
}
struct AA{
	int u,v;
	ll c;
}ee[Maxn<<3|5];
void solve(int u){
	l=1;
	r=0;
	dfs(u,u);
	int sz=size[u];
	for(int i=1;i<=r;i++){
		int v=a[i];
		if(max(msize[v],sz-size[v])<max(msize[u],sz-size[u])){
			u=v;
		}
	}
	dis[u]=0;
	vis[u]=1;
	for(int i=p[u];i+1;i=e[i].nxt){
		int v=e[i].v;
		if(vis[v]){
			continue;
		}
		dfss(v,u,e[i].c);
	}
	int ma=u;
	for(int i=l;i<=r;i++){
		int v=a[i];
		if(dis[v]+w[v]<dis[ma]+w[ma]){
			ma=v;
		}		
	}
	for(int i=l;i<=r;i++){
		int v=a[i];
		if(v==ma){
			continue;
		}
		ee[++szz]={ma,v,w[ma]+w[v]+dis[v]+dis[ma]};
	}
	for(int i=p[u];i+1;i=e[i].nxt){
		int v=e[i].v;
		if(vis[v]){
			continue;
		}
		solve(v);
	}
}
int cmp(AA x,AA y){
	return x.c<y.c;
}
int fa[Maxn+5],n;
int get(int x){
	return fa[x]==x?x:fa[x]=get(fa[x]);
}
signed main(){
	init();
	scanf("%lld", &n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&w[i]);
		fa[i]=i;
	}
	for(int i=2;i<=n;i++){
		int u,v,c;
		scanf("%lld%lld%lld",&u,&v,&c);
		insert(u,v,c);
		insert(v,u,c);
	}
	solve(1); 
	sort(ee+1,ee+1+szz,cmp);
	ll ans=0;
	for(int i=1;i<=szz;i++){
		int u=ee[i].u,v=ee[i].v;
		ll c=ee[i].c;
		u=get(u);
		v=get(v);
		if(u==v){
			continue;
		}
		fa[u]=v;
		ans+=c;
	}
	printf("%lld\n",ans);
	return 0;
}


Compiler output:
a.cc: In function 'void dfs(long long int, long long int)':
a.cc:24:9: error: reference to 'size' is ambiguous
   24 |         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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc:31:17: error: reference to 'size' is ambiguous
   31 |                 size[u]+=size[v];
      |                 ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc:31:26: error: reference to 'size' is ambiguous
   31 |                 size[u]+=size[v];
      |                          ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc:32:20: error: reference to 'size' is ambiguous
   32 |                 if(size[v]>msize[u]){
      |                    ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc:33:34: error: reference to 'size' is ambiguous
   33 |                         msize[u]=size[v];
      |                                  ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc: In function 'void solve(long long int)':
a.cc:55:16: error: reference to 'size' is ambiguous
   55 |         int sz=size[u];
      |                ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc:58:36: error: reference to 'size' is ambiguous
   58 |                 if(max(msize[v],sz-size[v])<max(msize[u],sz-size[u])){
      |                                    ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~
a.cc:58:61: error: reference to 'size' is ambiguous
   58 |                 if(max(msize[v],sz-size[v])<max(msize[u],sz-size[u])){
      |                                                             ^~~~
/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:20:19: note:                 'long long int size [2000005]'
   20 | int l,r,a[Maxn+5],size[Maxn+5],msize[Maxn+5],vis[Maxn+5],w[Maxn+5],dis[Maxn+5],szz;
      |                   ^~~~