Language: C++

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=2e5+10;
int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
long long dep[N];
void ad(int x,int y,int z)
{
	tot++;
	next[tot]=head[x];
	head[x]=tot;
	t[tot]=y;
	cc[tot]=z;
}
void fr(int x,int fa)
{
	int i=head[x],blan=0;
	size[x]=1;
	while (i)
	{
		if (t[i]==fa)
		{
			i=next[i];
			continue;
		}
		fr(t[i],x);
		size[x]=size[x]+size[t[i]];
		blan=max(blan,size[t[i]]);
		i=next[i];
	}
	blan=max(blan,n-size[x]);
	if (blan<sta) 
	{
		root=x;
		sta=blan;
	}
}
void dfs(int x,int fa)
{
	int i=head[x];
	size[x]=1; 
	while (i)
	{
		if (t[i]==fa)
		{
			i=next[i];
			continue;
		} 
		dep[t[i]]=dep[x]+cc[i];
		dfs(t[i],x);
		size[x]=size[x]+size[t[i]];
		i=next[i];
	}
}
int main()
{
	//freopen("test.in","r",stdin);
	scanf("%d",&n);
	for (int i=1;i<n;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		ad(x,y,z);
		ad(y,x,z); 
	}
	sta=1e9+7;
	fr(1,0);
	dfs(root,0);
	long long ans=0;
	for (int i=1;i<=n;i++) ans=ans+dep[i];
	int i=head[root],num=1e9+7;
	while (i)
	{
		if (size[t[i]]==(n+1)/2) num=min(num,cc[i]);
		i=next[i];
	}
	if (num==1e9+7) 
	{
		int i=head[root];
		while (i)
		{
			num=min(num,cc[i]);
			i=next[i];
		}
	}
	ans=ans*2-num;
	printf("%lld\n",ans);
}

Compiler output:
a.cc: In function 'void ad(int, int, int)':
a.cc:11:9: error: reference to 'next' is ambiguous
   11 |         next[tot]=head[x];
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~
a.cc: In function 'void fr(int, int)':
a.cc:24:27: error: reference to 'next' is ambiguous
   24 |                         i=next[i];
      |                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~
a.cc:30:19: error: reference to 'next' is ambiguous
   30 |                 i=next[i];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~
a.cc: In function 'void dfs(int, int)':
a.cc:47:27: error: reference to 'next' is ambiguous
   47 |                         i=next[i];
      |                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~
a.cc:53:19: error: reference to 'next' is ambiguous
   53 |                 i=next[i];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~
a.cc: In function 'int main()':
a.cc:76:19: error: reference to 'next' is ambiguous
   76 |                 i=next[i];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~
a.cc:84:27: error: reference to 'next' is ambiguous
   84 |                         i=next[i];
      |                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:13: note:                 'int next [200010]'
    6 | int head[N],next[N],t[N],cc[N],size[N],tot,n,root,sta;
      |             ^~~~