func_code_string: #include <bits/stdc++.h>   using namespace std;   static const int INF = 500000000;   template <class T>   void debug(T a, T b) {     for (; a != b; ++a) cerr << *a <<    ;     cerr << endl;   }   int n;   pair<pair<int, int>, int> es[5005];   vector<pair<int, int> > g[5005];   int size[5005];   int cut[5005];   int prep(int v, int p) {     size[v] = 1;     for (int i = 0; i < g[v].size(); ++i) {       int to = g[v][i].first;       if (to == p || cut[to]) continue;       size[v] += prep(to, v);     }     return size[v];   }   int type;   int cost[2], root[2], all[2];   void dfs(int v, int p) {     int maxi = 0;     for (int i = 0; i < g[v].size(); ++i) {       int to = g[v][i].first;       if (to == p || cut[to]) continue;       maxi = max(maxi, size[to]);       dfs(to, v);     }     if (max(maxi, all[type] - size[v]) < cost[type]) {       cost[type] = max(maxi, all[type] - size[v]);       root[type] = v;     }   }   long long int tot;   int dfs2(int v, int p) {     size[v] = 1;     for (int i = 0; i < g[v].size(); ++i) {       int to = g[v][i].first;       if (to == p || cut[to]) continue;       size[v] += dfs2(to, v);       tot += size[to] * (all[type] - size[to]) * (long long int)g[v][i].second;       tot += size[to] * (long long int)all[type ^ 1] * g[v][i].second;     }     return size[v];   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       --a;       --b;       g[a].push_back(make_pair(b, c));       g[b].push_back(make_pair(a, c));       es[i] = make_pair(make_pair(a, b), c);     }     long long int ans = 1e18;     for (int i = 0; i < n - 1; ++i) {       int a = es[i].first.first, b = es[i].first.second;       tot = 0;       cost[0] = cost[1] = INF;       type = 0;       cut[b] = 1;       all[0] = prep(a, b);       all[1] = n - all[0];       dfs(a, b);       dfs2(root[0], -1);       type = 1;       cut[b] = 0;       cut[a] = 1;       all[1] = prep(b, a);       dfs(b, a);       dfs2(root[1], -1);       cut[a] = 0;       tot += all[0] * all[1] * (long long int)es[i].second;       ans = min(ans, tot);     }     cout << ans << endl;     return 0;   }   