#include using namespace std; #include int main() { int n, m; cin >> n >> m; vector> edges; for (int i = 0; i < m; i++) { int a, b, w; cin >> a >> b >> w; a--, b--; edges.push_back({w, a, b}); } sort(begin(edges), end(edges)); atcoder::dsu uf(n); long long ans = 0; for (auto [w, a, b] : edges) { if (uf.same(a, b)) continue; ans += w * uf.size(a) * uf.size(b); uf.merge(a, b); } cout << ans << "\n"; }