func_code_string: #include <bits/stdc++.h>   #pragma GCC optimize( inline , 2)   using namespace std;   const long long N = 5005;   const long long inf = 1e18;   long long n, hd[N], nxt[N << 1], to[N << 1], w[N << 1], tot = 1;   long long sz[N << 1];   long long aa[N << 1], eg[N << 1];   void add(long long a, long long b, long long c) {     nxt[++tot] = hd[a], to[tot] = b, w[tot] = c;     hd[a] = tot;   }   long long dfs1(long long u, long long fe, long long d) {     long long re = d;     for (long long i = hd[u]; i; i = nxt[i])       if (i != fe) re += dfs1(to[i], i ^ 1, d + w[i]);     return eg[fe] = re;   }   long long dfs2(long long u, long long fe) {     long long re = 1;     for (long long i = hd[u]; i; i = nxt[i])       if (i != fe) re += dfs2(to[i], i ^ 1);     sz[fe ^ 1] = n - re;     return sz[fe] = re;   }   void solve(long long rt) {     memset(eg, 0, sizeof eg);     long long sum = dfs1(rt, 0, 0);     for (long long i = 2; i <= tot; ++i) aa[i] = min(aa[i], sum - eg[i]);   }   long long zz[N];   long long wi[N], wo[N], bb[N << 1];   void dfs3(long long u, long long fe) {     zz[u] = 1;     wi[u] = 0, wo[u] = 0;     for (long long i = hd[u]; i; i = nxt[i])       if (i != fe) {         dfs3(to[i], i ^ 1);         zz[u] += zz[to[i]];         wi[u] += wi[to[i]];         wo[u] += wo[to[i]] + zz[to[i]] * w[i];         wi[u] -= (wo[to[i]] + zz[to[i]] * w[i]) * zz[to[i]];       }     wi[u] += wo[u] * zz[u];     bb[fe] = wi[u];   }   signed main() {     memset(aa, 0x3f, sizeof aa);     cin >> n;     for (long long i = 1; i <= n - 1; ++i) {       long long x, y, z;       cin >> x >> y >> z;       add(x, y, z);       add(y, x, z);     }     for (long long i = 1; i <= n; ++i) dfs3(i, 0);     for (long long i = 1; i <= n; ++i) solve(i);     dfs2(1, 0);     long long ans = 1e18;     for (long long i = 1; i <= n - 1; ++i) {       long long r = i << 1, s = i << 1 | 1;       ans = min(ans, 1LL * w[r] * sz[r] * sz[s] + aa[r] * sz[r] + aa[s] * sz[s] +                          bb[r] + bb[s]);     }     cout << ans << endl;   }   