Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define debug(var)                                                             \
  do {                                                                         \
    std::cout << #var << " : ";                                                \
    view(var);                                                                 \
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

class UnionFind {
public:
  vector<ll> par;
  vector<ll> siz;

  UnionFind(ll k) : par(k), siz(k, 1LL) { rep(i, k) par[i] = i; }

  ll root(ll x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = root(par[x]);
  }

  void unite(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;

    if (siz[x] < siz[y])
      swap(x, y);
    par[y] = x;
    siz[x] += siz[y];
    return;
  }

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

  ll size(ll x) { return siz[root(x)]; }
};

const int mxN = 1e5 + 5;
V<> adj[mxN];
bool seen[mxN] = {false};
int gr[mxN] = {0};
int g = 0;

void dfs(int v, int par) {
  for (auto u : adj[v]) {
    if (u == par)
      continue;
    gr[u] = g;
    seen[u] = true;
    dfs(u, v);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  V<> a(n);
  UnionFind uf(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  if (2 * (n - m - 1) > n) {
    cout << "Impossible" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (!seen[i])
      seen[i] = true, gr[i] = g, dfs(i, -1), g++;
  }
  if (g == 1) {
    cout << 0 << endl;
    return 0;
  }
  V<set<ll>> s(g);
  for (int i = 0; i < n; i++) {
    s[gr[i]].insert(a[i]);
  }
  ll ans = 0;
  for (int i = 0; i < g; i++) {
    ans += *s[i].begin();
    s[i].erase(s[i].begin());
  }
  V<ll> p;
  for (int i = 0; i < g; i++) {
    for (auto u : s[i])
      p.push_back(u);
  }
  sort(all(p));
  int t = 2 * (n - m - 1) - g;
  for (int i = 0; i < t; i++) {
    ans += p[i];
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define debug(var)                                                             \
  do {                                                                         \
    std::cout << #var << " : ";                                                \
    view(var);                                                                 \
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

class UnionFind {
public:
  vector<ll> par;
  vector<ll> siz;

  UnionFind(ll k) : par(k), siz(k, 1LL) { rep(i, k) par[i] = i; }

  ll root(ll x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = root(par[x]);
  }

  void unite(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;

    if (siz[x] < siz[y])
      swap(x, y);
    par[y] = x;
    siz[x] += siz[y];
    return;
  }

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

  ll size(ll x) { return siz[root(x)]; }
};

const int mxN = 1e5 + 5;
V<> adj[mxN];
bool seen[mxN] = {false};
int gr[mxN] = {0};
int g = 0;

void dfs(int v, int par) {
  for (auto u : adj[v]) {
    if (u == par)
      continue;
    gr[u] = g;
    seen[u] = true;
    dfs(u, v);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  V<> a(n);
  UnionFind uf(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  if (2 * (n - m - 1) > n) {
    cout << "Impossible" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (!seen[i])
      seen[i] = true, gr[i] = g, dfs(i, -1), g++;
  }
  if (g == 1) {
    cout << 0 << endl;
    return 0;
  }
  V<multiset<ll>> s(g);
  for (int i = 0; i < n; i++) {
    s[gr[i]].insert(a[i]);
  }
  ll ans = 0;
  for (int i = 0; i < g; i++) {
    ans += *s[i].begin();
    s[i].erase(s[i].begin());
  }
  V<ll> p;
  for (int i = 0; i < g; i++) {
    for (auto u : s[i])
      p.push_back(u);
  }
  sort(all(p));
  int t = 2 * (n - m - 1) - g;
  for (int i = 0; i < t; i++) {
    ans += p[i];
  }
  cout << ans << endl;
}