#include #define int long long using namespace std; #define rep(i, n) for(int i=0;i<(n);++i) typedef pair pii; const int INF = 1l << 60; #define u_b upper_bound #define l_b lower_bound int N; vector G[200200]; int Ans; int dfs(int v, int p) { int children = 0; for (pii edge:G[v]) { if (edge.first == p)continue; int ch = dfs(edge.first, v) + 1; Ans += edge.second * (N - ch) * ch; children += ch; } return children; } signed main() { cin >> 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); } dfs(0, -1); cout << 2 * Ans << endl; return 0; }