#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; cin >> N; vector>> G(N); rep(i,N-1) { int u,v,w; cin >> u >> v >> w; u--; v--; G[u].push_back({v, w}); G[v].push_back({u, w}); } vector sz(N, 1); function get_sz = [&](int v, int p) -> void { for(auto [to, w] : G[v]) { if(to != p) { get_sz(to, v); sz[v] += sz[to]; } } }; get_sz(0, -1); ll ans = 0; function dfs = [&](int v, int p) -> void { for(auto [to, w] : G[v]) { if(to != p) { ans += 1LL * w * sz[to] * (N - sz[to]); dfs(to, v); } } }; dfs(0, -1); cout << ans * 2 << endl; }