#include #include #include #include #include #include #include #include #include #include #include #include #define vll vector #define vvvl vector #define vvl vector> #define VV(a, b, c, d) vector>(a, vector(b, c)) #define VVV(a, b, c, d) vector(a, vvl(b, vll (c, d))); #define re(c, b) for(ll c=0;c> G(200001, vector()); vll dist(200001, 0); vll c(200001, 0); vll ans(200001, 0); map mp; void f(ll now, ll from){ vvl p; for(auto e:G[now]){ if(e.to==from) continue; f(e.to, now); //子の数だけ p.push_back({dist[e.to] + c[e.to]*e.w, c[e.to]}); ans[now] += dist[e.to] + c[e.to]*e.w; dist[now] += dist[e.to] + c[e.to]*e.w; c[now] += c[e.to]; } for(int i=0;i> n; re(i, n-1){ ll a, b, c;std::cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } f(1, -1); ll s = 0; for(int i=1;i<=n;i++) s += ans[i]; std::cout << s*2 << '\n'; }