Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define LL long long
#define fo(i, j, k) for (int i = j; i <= k; i++)
#define fd(i, j, k) for (int i = j; i >= k; i--)
#define fr(i, j) for (int i = beg[j]; i; i = nex[i])
using namespace std;
int const mn = 100 + 3, ma = 1e7;
int n, a[mn], tag[ma], ss[ma], b[2][mn], gra, beg[mn], to[mn * mn],
    nex[mn * mn], vis[mn], re[mn];
void ins(int u, int v) {
  to[++gra] = v;
  nex[gra] = beg[u];
  beg[u] = gra;
}
int dfs(int p) {
  vis[p] = 1;
  fr(i, p) if ((!re[to[i]]) || dfs(re[to[i]])) {
    re[to[i]] = p;
    return 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  fo(i, 1, n) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  b[a[1] & 1][++b[a[1] & 1][0]] = a[1];
  b[(a[n] + 1) & 1][++b[(a[n] + 1) & 1][0]] = a[n] + 1;
  fo(i, 2, n) if (a[i - 1] + 1 != a[i])
      b[(a[i - 1] + 1) & 1][++b[(a[i - 1] + 1) & 1][0]] = a[i - 1] + 1,
                                                 b[a[i] & 1][++b[a[i] & 1][0]] =
                                                     a[i];
  tag[1] = 1;
  fo(i, 2, ma) {
    if (!tag[i])
      ss[++ss[0]] = i;
    fo(j, 1, ss[0]) {
      if (1ll * i * ss[j] > ma)
        break;
      tag[i * ss[j]] = 1;
      if (!(i % ss[j]))
        break;
    }
  }
  fo(i, 1, b[0][0]) fo(j, 1, b[1][0]) if (!tag[abs(b[0][i] - b[1][j])])
      ins(i, j);
  int tmp = 0;
  fo(i, 1, b[0][0]) {
    memset(vis, 0, sizeof(vis));
    tmp += dfs(i);
  }
  printf("%d", tmp + 2 * ((b[0][0] - tmp) / 2 + (b[1][0] - tmp) / 2) +
                   3 * ((b[0][0] - tmp) & 1));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define LL long long
#define fo(i, j, k) for (int i = j; i <= k; i++)
#define fd(i, j, k) for (int i = j; i >= k; i--)
#define fr(i, j) for (int i = beg[j]; i; i = nex[i])
using namespace std;
int const mn = 100 + 3, ma = 1e7;
int n, a[mn], tag[ma], ss[ma], b[2][mn], gra, beg[mn], to[mn * mn],
    nex[mn * mn], vis[mn], re[mn];
void ins(int u, int v) {
  to[++gra] = v;
  nex[gra] = beg[u];
  beg[u] = gra;
}
int dfs(int p) {
  vis[p] = 1;
  fr(i, p) if ((!re[to[i]]) || ((!vis[re[to[i]]]) && dfs(re[to[i]]))) {
    re[to[i]] = p;
    return 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  fo(i, 1, n) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  b[a[1] & 1][++b[a[1] & 1][0]] = a[1];
  b[(a[n] + 1) & 1][++b[(a[n] + 1) & 1][0]] = a[n] + 1;
  fo(i, 2, n) if (a[i - 1] + 1 != a[i])
      b[(a[i - 1] + 1) & 1][++b[(a[i - 1] + 1) & 1][0]] = a[i - 1] + 1,
                                                 b[a[i] & 1][++b[a[i] & 1][0]] =
                                                     a[i];
  tag[1] = 1;
  fo(i, 2, ma) {
    if (!tag[i])
      ss[++ss[0]] = i;
    fo(j, 1, ss[0]) {
      if (1ll * i * ss[j] > ma)
        break;
      tag[i * ss[j]] = 1;
      if (!(i % ss[j]))
        break;
    }
  }
  fo(i, 1, b[0][0]) fo(j, 1, b[1][0]) if (!tag[abs(b[0][i] - b[1][j])])
      ins(i, j);
  int tmp = 0;
  fo(i, 1, b[0][0]) {
    memset(vis, 0, sizeof(vis));
    tmp += dfs(i);
  }
  printf("%d", tmp + 2 * ((b[0][0] - tmp) / 2 + (b[1][0] - tmp) / 2) +
                   3 * ((b[0][0] - tmp) & 1));
  return 0;
}