func_code_string: #include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 5000;   int n, fir[maxn + 10], tmpN;   int nedge, to[2 * maxn + 10], nex[2 * maxn + 10], dis[2 * maxn + 10];   bool no[2 * maxn + 10];   long long ans, dp[maxn + 10], num[maxn + 10], allDis[maxn + 10];   int p[2];   inline void add_edge(int x, int y, int w) {     to[nedge] = y;     dis[nedge] = w;     nex[nedge] = fir[x];     fir[x] = nedge++;   }   void dfs(int x, int fa) {     num[x] = 0;     dp[x] = 0;     for (int i = fir[x]; ~i; i = nex[i]) {       int y = to[i];       if (y == fa) continue;       int w = dis[i];       dfs(y, x);       num[x] += num[y];       dp[x] += dp[y] + (long long)num[y] * w;     }     num[x]++;   }   void update(int x, int k) {     if (p[k] < 0 || allDis[x] < allDis[p[k]]) p[k] = x;   }   void dfs2(int x, int fa, int k) {     for (int i = fir[x]; ~i; i = nex[i]) {       int y = to[i];       if (y == fa) continue;       int w = dis[i];       allDis[y] =           allDis[x] - (long long)w * num[y] + (long long)(tmpN - num[y]) * w;       update(y, k);       dfs2(y, x, k);     }   }   void solve() {     ans = -1;     int N[2];     for (int i = 0; i < 2 * (n - 1); i += 2) {       memset(p, -1, sizeof p);       int x = to[i], y = to[i ^ 1], w = dis[i];       dfs(x, y);       tmpN = N[0] = num[x];       allDis[x] = dp[x];       update(x, 0);       dfs2(x, y, 0);       dfs(y, x);       tmpN = N[1] = num[y];       allDis[y] = dp[y];       update(y, 1);       dfs2(y, x, 1);       long long ret =           N[1] * (allDis[p[0]] + (long long)N[0] * w) + N[0] * allDis[p[1]];       long long ret2 = 0;       for (int(j) = 1; (j) <= (n); (j)++) ret2 += allDis[j];       ret2 /= 2;       ret += ret2;       if (ans < 0 || ret < ans) ans = ret;     }     cout << ans << endl;   }   int main() {     std::ios::sync_with_stdio(false);     int x, y, w;     while (cin >> n) {       memset(fir, -1, (n + 1) * sizeof fir[0]);       nedge = 0;       for (int(i) = 0; (i) < (n - 1); (i)++) {         cin >> x >> y >> w;         add_edge(x, y, w);         add_edge(y, x, w);       }       solve();     }     return 0;   }   