#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using i64 = int64_t; using i32 = int32_t; template void init_n(vector& v, size_t n, U x) { v = vector(n, x); } template void init_n(vector& v, size_t n) { init_n(v, n, T()); } template void read_n(vector& v, size_t n, size_t o = 0) { v = vector(n+o); for (size_t i=o; i> v[i]; } template void read_n(T a[], size_t n, size_t o = 0) { for (size_t i=o; i> a[i]; } template T gabs(const T& x) { return max(x, -x); } #define abs gabs struct E { i64 v, w; }; i64 n; vector> g; vector ch; i64 ans; void dfs(i64 u, i64 p) { ch[u] = 1; for (auto e : g[u]) { if (e.v != p) { dfs(e.v, u); ch[u] += ch[e.v]; ans += (n - ch[e.v]) * ch[e.v] * e.w; } } } int main() { cin >> n; init_n(g, n + 1); for (i64 i = 0; i < n - 1; ++i) { i64 u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); } init_n(ch, n + 1); dfs(1, -1); cout << 2 * ans << endl; return 0; }