#include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" const long long INF = (long long)1e18; // const long long MOD = 1'000'000'007; string yn(bool f){return f?"Yes":"No";} string YN(bool f){return f?"YES":"NO";} int N, result; vector>> tree; vector con, ans; void dfs1(int per = -1, int now = 1){ con[now]++; for(int i = 0; i < tree[now].size(); i++){ if(tree[now][i].first == per) continue; dfs1(now, tree[now][i].first); con[now] += con[tree[now][i].first]; ans[now] += ans[tree[now][i].first] + con[tree[now][i].first]*tree[now][i].second; } } void dfs2(int per = -1, int now = 1, int sum = ans[1]){ int res = sum; result += res; for(pair next : tree[now]){ if(per == next.first) continue; dfs2(now, next.first, res + (N - con[next.first])*next.second - (con[next.first])*next.second); } } signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout<>N; tree.resize(N+1); con.resize(N+1); ans.resize(N+1); for(int i = 0; i < N-1; i++){ int u, v, w; cin>>u>>v>>w; tree[u].push_back(make_pair(v,w)); tree[v].push_back(make_pair(u,w)); } dfs1(); dfs2(); cout<