#define _USE_MATH_DEFINES #include using namespace std; long long ans = 0; int n; vector> g[202020]; int dfs (int cur, int par) { int res = 0; for (auto nxt : g[cur]) { if (nxt.first == par) continue; int x = dfs(nxt.first, cur); res += x; ans += 1LL * x * (n - x) * nxt.second; } return res + 1; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { 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 << ans * 2 << endl; return 0; }