#include #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define ALL(x) begin(x), end(x) using ll = long long; using namespace std; int main() { // input int n; cin >> n; vector > > g(n); REP (i, n - 1) { int u, v, w; cin >> u >> v >> w; -- u; -- v; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } // solve ll d = 0; function go = [&](int i, int parent) -> int { vector sizes; for (auto edge : g[i]) { int j, cost; tie(j, cost) = edge; if (j == parent) continue; int size = go(j, i); sizes.push_back(size); d += 2ll * size * (n - size) * cost; } return accumulate(ALL(sizes), 0) + 1; }; go(0, -1); // output cout << d << endl; return 0; }