#include<cstring> #include<iostream> #include<cstdio> #include<queue> #include<algorithm> #define maxn 200200 using namespace std; typedef long long ll; int par[maxn]; ll num[maxn]; int find(int x) { if (par[x] == -1) return x; return par[x] = find(par[x]); } int n, m; struct Node { int be, en; ll len; }que[maxn]; bool bml(Node a, Node b) { return a.len < b.len; } int main() { memset(par, -1, sizeof(par)); scanf("%d", &n); for (int i = 0; i <= n; i++) num[i] = 1; for (int i = 0; i < n - 1; i++) { scanf("%d %d %lld", &que[i].be, &que[i].en,&que[i].len); } sort(que, que + n - 1,bml); long long ans = 0; for (int i = 0; i < n - 1; i++) { int a = find(que[i].be); int b = find(que[i].en); if (a != b) { par[a] = b; ans += (num[a] * num[b] - 1)*(que[i].len + 1); ans += que[i].len; num[b] += num[a]; } } printf("%lld ", ans); return 0; }