#include int ri() { int n; assert(scanf("%d", &n) == 1); return n; } int n; std::vector > > hen; std::vector size; std::vector parent; int dfs(int i, int par) { parent[i] = par; int cur = 1; for (auto &j : hen[i]) { if (j.first == par) continue; cur += dfs(j.first, i); } return size[i] = cur; } int main () { n = ri(); hen.resize(n); for (int i = 0; i + 1 < n; i++) { int a = ri() - 1; int b = ri() - 1; int c = ri(); hen[a].push_back({b, c}); hen[b].push_back({a, c}); } size.resize(n); parent.resize(n); dfs(0, -1); int64_t res = 0; for (int i = 0; i < n; i++) { for (auto &j : hen[i]) { if (parent[j.first] == i) { res += j.second * (int64_t) size[j.first] * (n - size[j.first]); } } } std::cout << res * 2 << std::endl; return 0; }