Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define inf 1000000007
#define mod 1000000007
#define x first
#define y second
#define pi acos(-1.0)
#define DBG(x) cerr << (#x) << "=" << x << "\n";
// #define dprintf(...)
#define hash _hash
// #define dprintf(...) fprintf(outFile,__VA_ARGS__)

#define FOREACH(it, x)                                                         \
  for (__typeof(x.begin()) it = x.begin(); it != x.end(); it++)
#define ull unsigned long long
#define ll long long
#define N 100010

template <class T, class U> inline void Max(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> inline void Min(T &a, U b) {
  if (a > b)
    a = b;
}

// FILE* outFile;

inline void add(int &a, int b) {
  a += b;
  while (a >= mod)
    a -= mod;
}

int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * (ll)a % mod;
    a = (ll)a * a % mod;
    b >>= 1;
  }
  return ans;
}

int f[N], sz[N], a[N];
int find(int a) { return a == f[a] ? a : f[a] = find(f[a]); }
void merge(int a, int b) {
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  f[b] = a;
}
vi v[N];
int main() {
  freopen("read.in", "r", stdin);
  // freopen("atlarge.in","r",stdin);
  // freopen("atlarge.out","w",stdout);
  // cout<<setprecision(9)<<fixed;
  int T, j, i, k, ca = 0, K = 0, n, m;
  scanf("%d%d", &n, &m);
  rep(i, 0, n) scanf("%d", &a[i]);
  rep(i, 0, n) f[i] = i, sz[i] = 1;
  ll s = 0;
  while (m--) {
    scanf("%d%d", &i, &j);
    i = find(i), j = find(j);
    if (i != j)
      merge(i, j);
  }
  m = 0;
  rep(i, 0, n) if (find(i) == i) {
    if (sz[i] == 1)
      K++, s += a[i];
    else
      v[i].pb(a[i]), m++;
  }
  else {
    v[find(i)].pb(a[i]);
  }
  if (K > n - K - 2 * (m - 1)) {
    puts("Impossible");
    return 0;
  }
  if (m + K == 1) {
    puts("0");
    return 0;
  }
  int rem = K + 2 * (m - 1);
  vi e;
  rep(i, 0, n) if (f[i] == i && sz[i] > 1) {
    sort(all(v[i]));
    s += v[i][0];
    rem--;
    rep(j, 1, SZ(v[i])) e.pb(v[i][j]);
  }
  if (rem) {
    sort(all(e));
    rep(i, 0, rem) s += e[i];
  }
  printf("%lld\n", s);
  // cerr<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
  return 0;
}


Fixed submission:
#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define inf 1000000007
#define mod 1000000007
#define x first
#define y second
#define pi acos(-1.0)
#define DBG(x) cerr << (#x) << "=" << x << "\n";
// #define dprintf(...)
#define hash _hash
// #define dprintf(...) fprintf(outFile,__VA_ARGS__)

#define FOREACH(it, x)                                                         \
  for (__typeof(x.begin()) it = x.begin(); it != x.end(); it++)
#define ull unsigned long long
#define ll long long
#define N 100010

template <class T, class U> inline void Max(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> inline void Min(T &a, U b) {
  if (a > b)
    a = b;
}

// FILE* outFile;

inline void add(int &a, int b) {
  a += b;
  while (a >= mod)
    a -= mod;
}

int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * (ll)a % mod;
    a = (ll)a * a % mod;
    b >>= 1;
  }
  return ans;
}

int f[N], sz[N], a[N];
int find(int a) { return a == f[a] ? a : f[a] = find(f[a]); }
void merge(int a, int b) {
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  f[b] = a;
}
vi v[N];
int main() {
  // freopen("read.in","r",stdin);
  // freopen("atlarge.in","r",stdin);
  // freopen("atlarge.out","w",stdout);
  // cout<<setprecision(9)<<fixed;
  int T, j, i, k, ca = 0, K = 0, n, m;
  scanf("%d%d", &n, &m);
  rep(i, 0, n) scanf("%d", &a[i]);
  rep(i, 0, n) f[i] = i, sz[i] = 1;
  ll s = 0;
  while (m--) {
    scanf("%d%d", &i, &j);
    i = find(i), j = find(j);
    if (i != j)
      merge(i, j);
  }
  m = 0;
  rep(i, 0, n) if (find(i) == i) {
    if (sz[i] == 1)
      K++, s += a[i];
    else
      v[i].pb(a[i]), m++;
  }
  else {
    v[find(i)].pb(a[i]);
  }
  if (K > n - K - 2 * (m - 1)) {
    puts("Impossible");
    return 0;
  }
  if (m + K == 1) {
    puts("0");
    return 0;
  }
  int rem = K + 2 * (m - 1);
  vi e;
  rep(i, 0, n) if (f[i] == i && sz[i] > 1) {
    sort(all(v[i]));
    s += v[i][0];
    rem--;
    rep(j, 1, SZ(v[i])) e.pb(v[i][j]);
  }
  if (rem) {
    sort(all(e));
    rep(i, 0, rem) s += e[i];
  }
  printf("%lld\n", s);
  // cerr<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
  return 0;
}