Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct edge {
  int k, next;
} e[2005];
int n, a[1005], x[1005], m, home[1005], cnt = -1, flag[1005], y[1005];
bool vis[10000005];
void add(int x, int y) {
  cnt++;
  e[cnt].k = y;
  e[cnt].next = home[x];
  home[x] = cnt;
}
int prime[10000005], cc = -1;
bool isprime[10000005];
void init() {
  isprime[1] = 1;
  for (int i = 2; i <= 10000002; i++) {
    if (!isprime[i]) {
      prime[++cc] = i;
    }
    for (int p = 1; p <= cc && prime[p] * i <= 10000002; p++) {
      isprime[prime[p] * i] = 1;
      if (i % prime[p] == 0)
        break;
    }
  }
}
int Abs(int a) { return a < 0 ? -a : a; }
bool used[2005];
bool dfs(int k) {
  for (int i = home[k]; ~i; i = e[i].next)
    if (!used[e[i].k]) {
      used[e[i].k] = 1;
      if (!flag[e[i].k] || dfs(flag[e[i].k])) {
        flag[e[i].k] = k;
        return 1;
      }
    }
  return 0;
}
int main() {
  init();
  memset(home, -1, sizeof(home));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), vis[a[i]] = 1;
  int o = a[n] + 1;
  n = 0;
  for (int i = 1; i <= o; i++)
    if (vis[i] != vis[i - 1]) {
      if (i & 1)
        x[++n] = i;
      else
        y[++m] = i;
    }
  for (int i = 1; i <= n; i++) {
    for (int p = 1; p <= m; p++)
      if (!isprime[Abs(x[i] - y[p])])
        add(i, p);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    memset(used, 0, sizeof(used));
    if (dfs(i))
      ans++;
  }
  ans += (n - ans) / 2 * 2 + (m - ans) / 2 * 2;
  n = (n - ans) % 2;
  m = (m - ans) % 2;
  if (n && m)
    ans += 3;
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct edge {
  int k, next;
} e[1000005];
int n, a[1005], x[1005], m, home[1005], cnt = -1, flag[1005], y[1005];
bool vis[10000005];
void add(int x, int y) {
  cnt++;
  e[cnt].k = y;
  e[cnt].next = home[x];
  home[x] = cnt;
}
int prime[10000005], cc = -1;
bool isprime[10000005];
void init() {
  isprime[1] = 1;
  for (int i = 2; i <= 10000002; i++) {
    if (!isprime[i]) {
      prime[++cc] = i;
    }
    for (int p = 1; p <= cc && prime[p] * i <= 10000002; p++) {
      isprime[prime[p] * i] = 1;
      if (i % prime[p] == 0)
        break;
    }
  }
}
int Abs(int a) { return a < 0 ? -a : a; }
bool used[2005];
bool dfs(int k) {
  for (int i = home[k]; ~i; i = e[i].next)
    if (!used[e[i].k]) {
      used[e[i].k] = 1;
      if (!flag[e[i].k] || dfs(flag[e[i].k])) {
        flag[e[i].k] = k;
        return 1;
      }
    }
  return 0;
}
int main() {
  init();
  memset(home, -1, sizeof(home));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), vis[a[i]] = 1;
  int o = a[n] + 1;
  n = 0;
  for (int i = 1; i <= o; i++)
    if (vis[i] != vis[i - 1]) {
      if (i & 1)
        x[++n] = i;
      else
        y[++m] = i;
    }
  for (int i = 1; i <= n; i++) {
    for (int p = 1; p <= m; p++)
      if (!isprime[Abs(x[i] - y[p])])
        add(i, p);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    memset(used, 0, sizeof(used));
    if (dfs(i))
      ans++;
  }
  ans += (n - ans) / 2 * 2 + (m - ans) / 2 * 2;
  n = (n - ans) % 2;
  m = (m - ans) % 2;
  if (n && m)
    ans += 3;
  printf("%d", ans);
  return 0;
}