Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double

using namespace std;
const int N = 1e7 + 10, M = 200 + 10, inf = 1 << 30;
int rd() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * w;
}
int to[M * M], nt[M * M], c[M * M], hd[M], tot = 1;
void add(int x, int y, int z) {
  ++tot, to[tot] = y, nt[tot] = hd[x], c[tot] = z, hd[x] = tot;
  ++tot, to[tot] = x, nt[tot] = hd[y], c[tot] = 0, hd[y] = tot;
}
int ps, pt, nhd[M], lv[M];
bool bfs() {
  queue<int> q;
  for (int i = 1; i <= pt; ++i)
    lv[i] = 0;
  lv[ps] = 1, q.push(ps);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = hd[x]; i; i = nt[i]) {
      int y = to[i];
      if (c[i] > 0 && !lv[y])
        lv[y] = lv[x] + 1, q.push(y);
    }
  }
  return lv[pt];
}
int dfs(int x, int fw) {
  if (x == pt)
    return fw;
  int an = 0;
  for (int &i = nhd[x]; i; i = nt[i]) {
    int y = to[i];
    if (c[i] > 0 && lv[y] == lv[x] + 1) {
      int dt = dfs(y, min(fw, c[i]));
      c[i] -= dt, c[i ^ 1] += dt;
      fw -= dt, an += dt;
      if (!fw)
        break;
    }
  }
  return an;
}
int dinic() {
  int an = 0, dt;
  while (bfs()) {
    memcpy(nhd, hd, sizeof(int) * (pt + 1));
    while ((dt = dfs(ps, inf)))
      an += dt;
  }
  return an;
}
int prm[M * M], tt;
bool v[N];
int n, a[M], id[M], b[N], ans;

int main() {
  v[1] = 1;
  for (int i = 2; i <= 10000001; ++i) {
    if (!v[i])
      prm[++tt] = i;
    for (int j = 1; i * prm[j] <= 10000001; ++j) {
      v[i * prm[j]] = 1;
      if (i % prm[j] == 0)
        break;
    }
  }
  v[0] = v[2] = 1;
  n = rd();
  for (int i = 1; i <= n; ++i) {
    int x = rd();
    b[x] ^= 1, b[x + 1] ^= 1;
  }
  n = 0;
  for (int i = 1; i <= N - 5; ++i)
    if (b[i])
      a[++n] = i;
  ps = 0, pt = n + 1;
  for (int i = 1; i <= n; ++i) {
    if (a[i] & 1) {
      add(ps, i, 1), id[i] = tot;
      for (int j = 1; j <= n; ++j)
        if (!v[abs(a[i] - a[j])])
          add(i, j, 1);
    } else
      add(i, pt, 1), id[i] = tot;
  }
  ans = dinic();
  for (int i = 1; i <= n; ++i)
    if (c[id[i]])
      a[i] = N + 1;
  sort(a + 1, a + n + 1);
  while (a[n] > N)
    --n;
  for (int i = 1; i <= n; ++i) {
    if (a[i] > N)
      continue;
    for (int j = 1; j <= n; ++j)
      if (i != j && a[j] < N && !((a[i] - a[j]) & 1)) {
        ++ans, ++ans, a[i] = a[j] = N + 1;
        break;
      }
  }
  sort(a + 1, a + n + 1);
  while (a[n] > N)
    --n;
  ans += n / 2 * 3;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double

using namespace std;
const int N = 1e7 + 10, M = 200 + 10, inf = 1 << 30;
int rd() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * w;
}
int to[M * M], nt[M * M], c[M * M], hd[M], tot = 1;
void add(int x, int y, int z) {
  ++tot, to[tot] = y, nt[tot] = hd[x], c[tot] = z, hd[x] = tot;
  ++tot, to[tot] = x, nt[tot] = hd[y], c[tot] = 0, hd[y] = tot;
}
int ps, pt, nhd[M], lv[M];
bool bfs() {
  queue<int> q;
  for (int i = 1; i <= pt; ++i)
    lv[i] = 0;
  lv[ps] = 1, q.push(ps);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = hd[x]; i; i = nt[i]) {
      int y = to[i];
      if (c[i] > 0 && !lv[y])
        lv[y] = lv[x] + 1, q.push(y);
    }
  }
  return lv[pt];
}
int dfs(int x, int fw) {
  if (x == pt)
    return fw;
  int an = 0;
  for (int &i = nhd[x]; i; i = nt[i]) {
    int y = to[i];
    if (c[i] > 0 && lv[y] == lv[x] + 1) {
      int dt = dfs(y, min(fw, c[i]));
      c[i] -= dt, c[i ^ 1] += dt;
      fw -= dt, an += dt;
      if (!fw)
        break;
    }
  }
  return an;
}
int dinic() {
  int an = 0, dt;
  while (bfs()) {
    memcpy(nhd, hd, sizeof(int) * (pt + 1));
    while ((dt = dfs(ps, inf)))
      an += dt;
  }
  return an;
}
int prm[N], tt;
bool v[N];
int n, a[M], id[M], b[N], ans;

int main() {
  v[1] = 1;
  for (int i = 2; i <= 10000001; ++i) {
    if (!v[i])
      prm[++tt] = i;
    for (int j = 1; i * prm[j] <= 10000001; ++j) {
      v[i * prm[j]] = 1;
      if (i % prm[j] == 0)
        break;
    }
  }
  v[0] = v[2] = 1;
  n = rd();
  for (int i = 1; i <= n; ++i) {
    int x = rd();
    b[x] ^= 1, b[x + 1] ^= 1;
  }
  n = 0;
  for (int i = 1; i <= N - 5; ++i)
    if (b[i])
      a[++n] = i;
  ps = 0, pt = n + 1;
  for (int i = 1; i <= n; ++i) {
    if (a[i] & 1) {
      add(ps, i, 1), id[i] = tot;
      for (int j = 1; j <= n; ++j)
        if (!v[abs(a[i] - a[j])])
          add(i, j, 1);
    } else
      add(i, pt, 1), id[i] = tot;
  }
  ans = dinic();
  for (int i = 1; i <= n; ++i)
    if (c[id[i]])
      a[i] = N + 1;
  sort(a + 1, a + n + 1);
  while (a[n] > N)
    --n;
  for (int i = 1; i <= n; ++i) {
    if (a[i] > N)
      continue;
    for (int j = 1; j <= n; ++j)
      if (i != j && a[j] < N && !((a[i] - a[j]) & 1)) {
        ++ans, ++ans, a[i] = a[j] = N + 1;
        break;
      }
  }
  sort(a + 1, a + n + 1);
  while (a[n] > N)
    --n;
  ans += n / 2 * 3;
  printf("%d\n", ans);
  return 0;
}