#include using namespace std; using lint = long long; template using V = vector; template using VV = V< V >; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; struct Edge { int to, w; }; VV g(n); for (int _ = 0; _ < n - 1; ++_) { int u, v, w; cin >> u >> v >> w, --u, --v; g[u].emplace_back(Edge{v, w}); g[v].emplace_back(Edge{u, w}); } lint res = 0; V sz(n); auto dfs = [&](const auto& dfs, int v, int p) -> void { sz[v] = 1; for (const auto& e : g[v]) if (e.to != p) { dfs(dfs, e.to, v); res += sz[e.to] * (n - sz[e.to]) * e.w; sz[v] += sz[e.to]; } }; dfs(dfs, 0, -1); cout << 2 * res << '\n'; }