Language: C++

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
const int N = 1e5 + 5;

int n;
struct edge { int to, len; };
vector<edge> G[N];

long long ans;
int tmp;
int maxp[N], size[N];
int centr;

void solve(int x, int f) {
    size[x] = 1, maxp[x] = 0;
    for (auto y : G[x]) {
        if (y.to == f) continue;
        solve(y.to, x);
        ans += y.len * 2ll * min(size[y.to], n - size[y.to]);
        size[x] += size[y.to];
        maxp[x] = max(maxp[x], size[y.to]);
    }
    maxp[x] = max(maxp[x], n - size[x]);
    if (maxp[x] < maxp[centr]) centr = x;
}

signed main() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v, w; cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }

    maxp[centr = 0] = 2e9;
    solve(1, 0);
    tmp = 2e9;
    for (auto i : G[centr]) {
        int x = i.to, w = i.len;
        if (maxp[centr] == maxp[x]) {
            printf("%lld\n", ans - w);
            return 0;
        }
        tmp = min(tmp, w);
    }
 //   printf("%d %d %d\n", ans, tmp, centr);
    printf("%lld\n", ans - tmp);
    return 0;
}

Compiler output:
a.cc: In function 'void solve(int, int)':
a.cc:18:5: error: reference to 'size' is ambiguous
   18 |     size[x] = 1, maxp[x] = 0;
      |     ^~~~
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:2:
/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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~
a.cc:22:34: error: reference to 'size' is ambiguous
   22 |         ans += y.len * 2ll * min(size[y.to], n - size[y.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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~
a.cc:22:50: error: reference to 'size' is ambiguous
   22 |         ans += y.len * 2ll * min(size[y.to], n - size[y.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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~
a.cc:23:9: error: reference to 'size' is ambiguous
   23 |         size[x] += size[y.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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~
a.cc:23:20: error: reference to 'size' is ambiguous
   23 |         size[x] += size[y.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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~
a.cc:24:32: error: reference to 'size' is ambiguous
   24 |         maxp[x] = max(maxp[x], size[y.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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~
a.cc:26:32: error: reference to 'size' is ambiguous
   26 |     maxp[x] = max(maxp[x], 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:14:14: note:                 'int size [100005]'
   14 | int maxp[N], size[N];
      |              ^~~~