#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] = {}; int rec(int v, int parent) { counts[v] = 1; for (auto p: tree[v]) { if (p.first == parent) continue; counts[v] += rec(p.first, v); } return counts[v]; } 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] * (N-counts[p.first]) * 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); ll 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; }