#include using namespace std; long long N; vector>> edge; vector dist,sum,scale; int idx = 0; void dfs1(int from, int prev = -1){ for(int i = 0; i < edge[from].size(); ++i){ int to = edge[from][i].first; if(to==prev) continue; dist[to] = dist[from] + edge[from][i].second; dfs1(to,from); scale[from] += scale[to]; } } void dfs2(int from, int prev = -1){ for(int i = 0; i < edge[from].size(); ++i){ int to = edge[from][i].first; if(to==prev) continue; sum[to] = sum[from] + (N - 2*scale[to])*edge[from][i].second; dfs2(to,from); } } //Union Find Tree class Union_Find_Tree_Size { public: vector parent; Union_Find_Tree_Size(int N = 1) : parent(N,-1){ } int root(int n) { return (parent[n]<0?n:parent[n] = root(parent[n])); } bool same(int n, int m) { return root(n) == root(m); } void unite(int n, int m) { n = root(n); m = root(m); if (n == m) return; if(parent[n]>parent[m]) swap(n, m); parent[n] += parent[m]; parent[m] = n; } int size(int n){ return (-parent[root(n)]); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; assert(2<=N && N <= 200000); edge.resize(N); dist.resize(N); sum.resize(N); scale.resize(N,1); Union_Find_Tree_Size uf(N); for(int i = 0; i < N-1; ++i){ int u,v; long long w; cin >> u >> v >> w; assert(1<= u && u <= N); assert(1<= v && v <= N); assert(1<= w && w <= 100); u--,v--; edge[u].push_back({v,w}); edge[v].push_back({u,w}); uf.unite(u,v); } for(int i = 0; i < N; ++i) assert(uf.size(i)==N); dist[0] = 0; dfs1(0); sum[0] = accumulate(dist.begin(),dist.end(),0LL); dfs2(0); cout << accumulate(sum.begin(),sum.end(),0LL) << endl; return 0; }