Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename ARR, typename U> void fill_all(ARR &arr, const U &v) {
  for (auto &i : arr)
    fill_all(i, v);
}

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const unordered_set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const unordered_map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}
#define DUMP(x) cout << #x << " = " << (x) << endl

struct edge {
  int to, cost;
};

const int INF = 1LL << 60;
const int MOD = 1e9 + 7;

void dfs(const vector<vector<int>> &G, vector<int> &group, int v, int k) {
  group[v] = k;
  for (int i = 0; i < G[i].size(); ++i) {
    int u = G[v][i];
    if (group[u] == -1) {
      dfs(G, group, u, k);
    }
  }
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<vector<int>> G(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  int k = 0;
  vector<int> group(n, -1);
  for (int i = 0; i < n; ++i) {
    if (group[i] == -1) {
      dfs(G, group, i, k++);
    }
  }

  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<vector<int>> as(k);
  for (int i = 0; i < n; ++i) {
    as[group[i]].push_back(a[i]);
  }

  for (int i = 0; i < k; ++i) {
    sort(as[i].begin(), as[i].end());
  }

  int sum = 0;
  vector<int> others;
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < as[i].size(); ++j) {
      if (j == 0) {
        sum += as[i][j];
      } else {
        others.push_back(as[i][j]);
      }
    }
  }
  sort(others.begin(), others.end());

  if ((int)others.size() < k - 2) {
    cout << "Impossible" << endl;
    return 0;
  }

  for (int i = 0; i < k - 2; ++i) {
    sum += others[i];
  }
  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long int
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename ARR, typename U> void fill_all(ARR &arr, const U &v) {
  for (auto &i : arr)
    fill_all(i, v);
}

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const unordered_set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const unordered_map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}
#define DUMP(x) cout << #x << " = " << (x) << endl

struct edge {
  int to, cost;
};

const int INF = 1LL << 60;
const int MOD = 1e9 + 7;

void dfs(const vector<vector<int>> &G, vector<int> &group, int v, int k) {
  group[v] = k;
  for (int i = 0; i < G[v].size(); ++i) {
    int u = G[v][i];
    if (group[u] == -1) {
      dfs(G, group, u, k);
    }
  }
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<vector<int>> G(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  int k = 0;
  vector<int> group(n, -1);
  for (int i = 0; i < n; ++i) {
    if (group[i] == -1) {
      dfs(G, group, i, k++);
    }
  }

  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<vector<int>> as(k);
  for (int i = 0; i < n; ++i) {
    as[group[i]].push_back(a[i]);
  }

  for (int i = 0; i < k; ++i) {
    sort(as[i].begin(), as[i].end());
  }

  int sum = 0;
  vector<int> others;
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < as[i].size(); ++j) {
      if (j == 0) {
        sum += as[i][j];
      } else {
        others.push_back(as[i][j]);
      }
    }
  }
  sort(others.begin(), others.end());

  if ((int)others.size() < k - 2) {
    cout << "Impossible" << endl;
    return 0;
  }

  for (int i = 0; i < k - 2; ++i) {
    sum += others[i];
  }
  cout << sum << endl;

  return 0;
}