Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v)                                                                \
  REP(aa, (v).size()) {                                                        \
    cout << v[aa];                                                             \
    if (aa != v.size() - 1)                                                    \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
struct UnionFind {
  vector<int> par;
  vector<int> sizes;

  UnionFind(int n) {
    par.resize(n);
    sizes.resize(n);
    REP(i, n) {
      par[i] = i;
      sizes[i] = 1;
    }
  }

  int root(int a) { return par[a] == a ? a : par[a] = root(par[a]); }

  bool same(int x, int y) { return root(x) == root(y); }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (sizes[x] < sizes[y])
      swap(x, y);

    par[y] = x;
    sizes[x] += sizes[y];
  }
  int size(int x) { return sizes[root(x)]; }
};

using pq = priority_queue<ll, vl, less<ll>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vl a(n);
  REP(i, n) cin >> a[i];
  map<int, vl> mp;
  UnionFind uf(n);
  REP(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    uf.unite(x, y);
  }
  REP(i, n) { mp[uf.root(i)].push_back(a[i]); }
  ll ans = 0;
  vl tmp;
  for (auto b : mp) {
    sort(ALL(b.second), greater<int>());
    ans += b.second.back();
    b.second.pop_back();
    REP(i, b.second.size()) { tmp.push_back(b.second[i]); }
  }
  if (2 * (mp.size() - 1) > n) {
    cout << "Impossible" << endl;
    return 0;
  }
  sort(ALL(tmp));
  if (m == n - 1)
    cout << 0 << endl;
  else {
    REP(i, mp.size() - 2) { ans += tmp[i]; }
    cout << ans << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v)                                                                \
  REP(aa, (v).size()) {                                                        \
    cout << v[aa];                                                             \
    if (aa != v.size() - 1)                                                    \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
struct UnionFind {
  vector<int> par;
  vector<int> sizes;

  UnionFind(int n) {
    par.resize(n);
    sizes.resize(n);
    REP(i, n) {
      par[i] = i;
      sizes[i] = 1;
    }
  }

  int root(int a) { return par[a] == a ? a : par[a] = root(par[a]); }

  bool same(int x, int y) { return root(x) == root(y); }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (sizes[x] < sizes[y])
      swap(x, y);

    par[y] = x;
    sizes[x] += sizes[y];
  }
  int size(int x) { return sizes[root(x)]; }
};

using pq = priority_queue<ll, vl, less<ll>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vl a(n);
  REP(i, n) cin >> a[i];
  map<int, vl> mp;
  UnionFind uf(n);
  REP(i, m) {
    int x, y;
    cin >> x >> y;

    uf.unite(x, y);
  }
  REP(i, n) { mp[uf.root(i)].push_back(a[i]); }
  ll ans = 0;
  vl tmp;
  for (auto b : mp) {
    sort(ALL(b.second), greater<int>());
    ans += b.second.back();
    b.second.pop_back();
    REP(i, b.second.size()) { tmp.push_back(b.second[i]); }
  }
  if (2 * (mp.size() - 1) > n) {
    cout << "Impossible" << endl;
    return 0;
  }
  sort(ALL(tmp));
  if (m == n - 1)
    cout << 0 << endl;
  else {
    REP(i, mp.size() - 2) { ans += tmp[i]; }
    cout << ans << endl;
  }
}