#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 7;
set<pair<long long, long long> > ad[N];
vector<pair<pair<long long, long long>, long long> > ads;
long long n, res, sz[N], dp1[N], szrt;
void dfs(long long v, long long d) {
  sz[v] = 1;
  for (auto p : ad[v]) {
    long long u = p.first;
    long long w = p.second;
    if (u == d) continue;
    dfs(u, v);
    sz[v] += sz[u];
    dp1[v] += dp1[u] + sz[u] * w;
  }
}
long long findmn(long long v, long long d) {
  long long ans = dp1[v];
  for (auto p : ad[v]) {
    long long u = p.first;
    long long w = p.second;
    if (u == d) continue;
    dp1[u] = dp1[v] - sz[u] * w + (szrt - sz[u]) * w;
    ans = min(findmn(u, v), ans);
  }
  return ans;
}
long long findsm(long long v, long long d) {
  long long ans = dp1[v];
  for (auto p : ad[v]) {
    long long u = p.first;
    long long w = p.second;
    if (u == d) continue;
    ans += findsm(u, v);
  }
  return ans;
}
int32_t main() {
  res = 1e18;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long v, u, w;
    cin >> v >> u >> w;
    ad[v].insert({u, w});
    ad[u].insert({v, w});
    ads.push_back({{v, u}, w});
  }
  for (auto p : ads) {
    long long v = p.first.first;
    long long u = p.first.second;
    long long w = p.second;
    ad[v].erase({u, w});
    ad[u].erase({v, w});
    memset(sz, 0, sizeof sz);
    memset(dp1, 0, sizeof dp1);
    vector<long long> szcom, mncom, smcom;
    for (long long i = 1; i <= n; i++) {
      if (sz[i]) continue;
      dfs(i, 0);
      szrt = sz[i];
      szcom.push_back(sz[i]);
      mncom.push_back(findmn(i, 0));
      smcom.push_back(findsm(i, 0) / 2);
    }
    res = min(res, smcom[0] + smcom[1] + szcom[0] * szcom[1] * w +
                       szcom[0] * mncom[1] + szcom[1] * mncom[0]);
    ad[v].insert({u, w});
    ad[u].insert({v, w});
  }
  cout << res;
  return 0;
}

### Prompt

Your task is to create a Cpp solution to the following problem:

The great Shaass is the new king of the Drakht empire. The empire has n cities which are connected by n - 1 bidirectional roads. Each road has an specific length and connects a pair of cities. There's a unique simple path connecting each pair of cities.

His majesty the great Shaass has decided to tear down one of the roads and build another road with the same length between some pair of cities. He should build such road that it's still possible to travel from each city to any other city. He might build the same road again.

You as his advisor should help him to find a way to make the described action. You should find the way that minimize the total sum of pairwise distances between cities after the action. So calculate the minimum sum.

Input

The first line of the input contains an integer n denoting the number of cities in the empire, (2 ≤ n ≤ 5000). The next n - 1 lines each contains three integers ai, bi and wi showing that two cities ai and bi are connected using a road of length wi, (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ wi ≤ 106).

Output

On the only line of the output print the minimum pairwise sum of distances between the cities.

Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

Examples

Input

3
1 2 2
1 3 4


Output

12


Input

6
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1


Output

29


Input

6
1 3 1
2 3 1
3 4 100
4 5 2
4 6 1


Output

825

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 7;
set<pair<long long, long long> > ad[N];
vector<pair<pair<long long, long long>, long long> > ads;
long long n, res, sz[N], dp1[N], szrt;
void dfs(long long v, long long d) {
  sz[v] = 1;
  for (auto p : ad[v]) {
    long long u = p.first;
    long long w = p.second;
    if (u == d) continue;
    dfs(u, v);
    sz[v] += sz[u];
    dp1[v] += dp1[u] + sz[u] * w;
  }
}
long long findmn(long long v, long long d) {
  long long ans = dp1[v];
  for (auto p : ad[v]) {
    long long u = p.first;
    long long w = p.second;
    if (u == d) continue;
    dp1[u] = dp1[v] - sz[u] * w + (szrt - sz[u]) * w;
    ans = min(findmn(u, v), ans);
  }
  return ans;
}
long long findsm(long long v, long long d) {
  long long ans = dp1[v];
  for (auto p : ad[v]) {
    long long u = p.first;
    long long w = p.second;
    if (u == d) continue;
    ans += findsm(u, v);
  }
  return ans;
}
int32_t main() {
  res = 1e18;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long v, u, w;
    cin >> v >> u >> w;
    ad[v].insert({u, w});
    ad[u].insert({v, w});
    ads.push_back({{v, u}, w});
  }
  for (auto p : ads) {
    long long v = p.first.first;
    long long u = p.first.second;
    long long w = p.second;
    ad[v].erase({u, w});
    ad[u].erase({v, w});
    memset(sz, 0, sizeof sz);
    memset(dp1, 0, sizeof dp1);
    vector<long long> szcom, mncom, smcom;
    for (long long i = 1; i <= n; i++) {
      if (sz[i]) continue;
      dfs(i, 0);
      szrt = sz[i];
      szcom.push_back(sz[i]);
      mncom.push_back(findmn(i, 0));
      smcom.push_back(findsm(i, 0) / 2);
    }
    res = min(res, smcom[0] + smcom[1] + szcom[0] * szcom[1] * w +
                       szcom[0] * mncom[1] + szcom[1] * mncom[0]);
    ad[v].insert({u, w});
    ad[u].insert({v, w});
  }
  cout << res;
  return 0;
}

```