#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector>> g(n); for (int i = 0; i < n - 1; i++) { int x, y, z; cin >> x >> y >> z; x -= 1, y -= 1; g[x].push_back({y, z}); g[y].push_back({x, z}); } ll res = 0; vector sz(n); auto dfs = [&](auto dfs, int s, int p) -> void { sz[s] = 1; for (auto to : g[s]) { int t = to.first; if (t == p) continue; dfs(dfs, t, s); res += to.second * (sz[t]) * (n - sz[t]); sz[s] += sz[t]; } }; dfs(dfs, 0, -1); cout << res*2 << endl; }