#include using namespace std; #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") int main() { cin.tie(nullptr); ios::sync_with_stdio(false); 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); __int128 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); } assert(ans <= 1e18); cout << (long long) ans << "\n"; }