#include using namespace std; long long N, ans = 0; vector>> edge; vector scale; void dfs(int curr, int prev, long long d){ for(auto& e: edge[curr]){ int next = e.first; if(next!=prev) dfs(next,curr,e.second); } if(prev == -1) return; scale[prev] += scale[curr]; ans += d*scale[curr]*(N-scale[curr])*2; } int main() { cin >> N; edge.resize(N); scale.resize(N,1); for(int i = 0; i < N-1; ++i){ int u,v,w; cin >> u >> v >> w; u--,v--; edge[u].push_back({v,w}); edge[v].push_back({u,w}); } dfs(0,-1,-1); cout << ans << endl; return 0; }