Problem p03440 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e5 + 10;
int fa[maxn], num[maxn], a[maxn];
bool vis[maxn];
ll ans;
priority_queue<int, vector<int>, greater<int>> q;
int find(int x) {
  if (fa[x] == x)
    return x;
  return fa[x] = find(fa[x]);
}
void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y)
    return;
  if (a[num[x]] < a[num[y]])
    num[y] = num[x];
  fa[x] = y;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("d.in", "r", stdin);
  freopen("d.out", "w", stdout);
#endif
  int n = read(), m = read();
  if ((n - 1 - m) * 2 > n) {
    printf("Impossible\n");
    return 0;
  }
  if (n - 1 == m) {
    printf("0\n");
    return 0;
  }
  int tmp = (n - 1 - m) * 2;
  REP(i, 1, n) a[i] = read(), fa[i] = num[i] = i;
  while (m--) {
    int x = read() + 1, y = read() + 1;
    merge(x, y);
  }
  //	REP(i,1,n) printf("%d %d %d\n",i,fa[i],num[i]);
  REP(i, 1, n) if (fa[i] == i) ans += a[num[i]], vis[num[i]] = 1, --tmp;
  REP(i, 1, n) if (!vis[i]) q.push(a[i]);
  while (!q.empty() && tmp) {
    --tmp;
    int u = q.top();
    q.pop();
    ans += u;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e5 + 10;
int fa[maxn], num[maxn], a[maxn];
bool vis[maxn];
ll ans;
priority_queue<int, vector<int>, greater<int>> q;
int find(int x) {
  if (fa[x] == x)
    return x;
  return fa[x] = find(fa[x]);
}
void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y)
    return;
  if (a[num[x]] < a[num[y]])
    num[y] = num[x];
  fa[x] = y;
}
int main() {
  int n = read(), m = read();
  if ((n - 1 - m) * 2 > n) {
    printf("Impossible\n");
    return 0;
  }
  if (n - 1 == m) {
    printf("0\n");
    return 0;
  }
  int tmp = (n - 1 - m) * 2;
  REP(i, 1, n) a[i] = read(), fa[i] = num[i] = i;
  while (m--) {
    int x = read() + 1, y = read() + 1;
    merge(x, y);
  }
  //	REP(i,1,n) printf("%d %d %d\n",i,fa[i],num[i]);
  REP(i, 1, n) if (fa[i] == i) ans += a[num[i]], vis[num[i]] = 1, --tmp;
  REP(i, 1, n) if (!vis[i]) q.push(a[i]);
  while (!q.empty() && tmp) {
    --tmp;
    int u = q.top();
    q.pop();
    ans += u;
  }
  printf("%lld\n", ans);
  return 0;
}