Language: C++

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
long long ans=0;
struct Edge
{
	int to,next,v;
}w[200005];
void add(int x,int y,int z)
{
	w[++cnt]=Edge{y,h[x],z};
	h[x]=cnt;
}
void dfs(int x,int fa,int v)
{
	size[x]=1;
	int s=0;
	for(int i=h[x];i;i=w[i].next)
	{
		int to=w[i].to;
		if(to==fa) continue;
		dfs(to,x,w[i].v);
		size[x]+=size[to];s=max(s,size[to]);
	}
	if(size[x]*2==n) vs=v;
	ans+=1ll*min(size[x],n-size[x])*v;
	s=max(s,n-size[x]);
	if(s<maxx) maxx=s,rt=x;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z),add(y,x,z);
	}
	dfs(1,0,0);
	if(!vs)
	{
		vs=0x3f3f3f3f;
		for(int i=h[rt];i;i=w[i].next) vs=min(vs,w[i].v);
	}
	printf("%lld\n",ans*2-vs);
}

Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:22:9: error: reference to 'size' is ambiguous
   22 |         size[x]=1;
      |         ^~~~
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: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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:29:17: error: reference to 'size' is ambiguous
   29 |                 size[x]+=size[to];s=max(s,size[to]);
      |                 ^~~~
/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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:29:26: error: reference to 'size' is ambiguous
   29 |                 size[x]+=size[to];s=max(s,size[to]);
      |                          ^~~~
/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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:29:43: error: reference to 'size' is ambiguous
   29 |                 size[x]+=size[to];s=max(s,size[to]);
      |                                           ^~~~
/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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:31:12: error: reference to 'size' is ambiguous
   31 |         if(size[x]*2==n) vs=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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:32:22: error: reference to 'size' is ambiguous
   32 |         ans+=1ll*min(size[x],n-size[x])*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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:32:32: error: reference to 'size' is ambiguous
   32 |         ans+=1ll*min(size[x],n-size[x])*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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~
a.cc:33:19: error: reference to 'size' is ambiguous
   33 |         s=max(s,n-size[x]);
      |                   ^~~~
/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:9:7: note:                 'int size [100005]'
    9 | int n,size[100005],rt,maxx=0x3f3f3f3f,vs=0,h[100005],cnt;
      |       ^~~~