Link to this code: https://cses.fi/paste/f6f8dd9648d7bf2fad20d5/
// https://cses.fi/problemset/task/1675, Prim
#include "bits/stdc++.h"
using namespace std;
using LL = long long;

constexpr LL INF = 1e15;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	int m; cin >> m;
	vector<vector<array<LL, 2>>> adj(n);
	for (int i = 0, a, b, w; i < m; ++i) {
		cin >> a >> b >> w;
		--a, --b;
		adj[a].push_back({b, w});
		adj[b].push_back({a, w});
	}

	vector<LL> d(n, INF);
	vector<bool> proc(n);
	priority_queue<array<LL, 2>> Q;
	d[0] = 0;
	Q.push({0, 0});
	while (!Q.empty()) {
		int v = Q.top()[1];
		Q.pop();
		if (proc[v])
			continue;
		proc[v] = true;
		for (auto [u, w] : adj[v]) {
			if (!proc[u] && d[u] > w) {
				d[u] = w;
				Q.push({-d[u], u});
			}
		}
	}
	if (count(begin(d), end(d), INF) != 0) {
		cout << "IMPOSSIBLE\n";
	} else {
		LL s = 0;
		for (LL x : d)
			s += x;
		cout << s << "\n";
	}
}