#include #define REP(i, start, end) for (int i=start, i##Len=(end); i < i##Len; ++i) using ll = int64_t; using namespace std; int N; vector>> tree; int counts[200010] = {}; ll ans = 0; int rec(int v, int parent) { for (auto p: tree[v]) { if (p.first == parent) continue; counts[v] += rec(p.first, v); } return counts[v]+1; } ll solve(int v, int parent) { ll total = 0; for (auto p : tree[v]) { if (p.first == parent) continue; total += p.second * (counts[p.first]+1) * (N-counts[p.first]-1) * 2 + solve(p.first, v); } return total; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cin >> N; tree = vector>>(N+1); int u, v, w; REP(i, 0, N-1) { cin >> u >> v >> w; tree[u].emplace_back(v, w); tree[v].emplace_back(u, w); } rec(1, 0); cout << solve(1, 0) << endl; return 0; }