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