#include #define Int int64_t using namespace std; void dfs(const vector>>& g, int u, vector& child) { child[u] = 1; for (auto p : g[u]) { int v = p.first; if (child[v] < 0) { dfs(g, v, child); child[u] += child[v]; } } } int main() { Int N; cin >> N; vector>> g(N); for (int i = 0; i < N - 1; ++i) { int u, v, w; cin >> u >> v >> w; --u; --v; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } vector child(N, -1); dfs(g, 0, child); Int ans = 0; deque que; que.push_back(0); vector used(N, false); while (!que.empty()) { int u = que.front(); que.pop_front(); if (used[u]) { continue; } used[u] = true; for (auto p : g[u]) { int v = p.first; if (used[v]) { continue; } ans += child[v] * (N - child[v]) * p.second * 2; que.push_back(v); } } cout << ans << endl; return 0; }