Problem p03642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 11;
const int M = 7e5 + 11;
const int MAXN = 1e7 + 11;
const int inf = 2147483647;
int n, tot, cnt = 1, t1, t2, mx, S, T, hd[M];
int ans, pri[M], L[N << 1], R[N << 1];
bitset<MAXN> is, a;
struct Edge {
  int nxt, to, val;
} e[M];
void ins(int x, int y, int z) {
  e[++cnt].nxt = hd[x];
  e[cnt].to = y;
  hd[x] = cnt;
  e[cnt].val = z;
}
void prepare() {
  is[0] = is[1] = 1;
  for (int i = 2; i < MAXN; i++) {
    if (!is[i])
      pri[++tot] = i;
    for (int j = 1; j <= tot && i * pri[j] <= MAXN; j++) {
      is[pri[j] * i] = 1;
      if (i % pri[j] == 0)
        break;
    }
  }
}
namespace Network_Flow {
queue<int> p;
int dep[N << 1];
int bfs() {
  memset(dep, 0, sizeof(dep));
  p.push(S);
  dep[S] = 1;
  while (!p.empty()) {
    int x = p.front();
    p.pop();
    for (int i = hd[x]; i; i = e[i].nxt) {
      int y = e[i].to, v = e[i].val;
      if (!dep[y] && v) {
        dep[y] = dep[x] + 1;
        p.push(y);
      }
    }
  }
  if (dep[T])
    return 1;
  return 0;
}
int dfs(int x, int flow) {
  if (x == T || flow <= 0)
    return flow;
  int rest = 0;
  for (int i = hd[x]; i; i = e[i].nxt) {
    int j = e[i].to;
    int v = e[i].val;
    if (dep[j] == dep[x] + 1 && v) {
      int now = dfs(j, min(v, flow));
      e[i].val -= now;
      e[i ^ 1].val += now;
      flow -= now;
      rest += now;
      if (flow <= 0)
        break;
    }
  }
  if (!rest)
    dep[x] = -1;
  return rest;
}
int dinic() {
  int maxflow = 0;
  while (bfs())
    maxflow += dfs(S, inf);
  return maxflow;
}
} // namespace Network_Flow
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
signed main() {
  freopen("oatmeal.in", "r", stdin);
  freopen("oatmeal.out", "w", stdout);
  prepare();
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    a[x] = 1, mx = max(mx, x);
  }
  for (int i = 1; i <= mx + 1; i++)
    if (a[i] != a[i - 1])
      i & 1 ? L[++t1] = i : R[++t2] = i;
  S = 0, T = t1 + t2 + 1;
  for (int i = 1; i <= t1; i++)
    ins(S, i, 1), ins(i, S, 0);
  for (int i = 1; i <= t2; i++)
    ins(i + t1, T, 1), ins(T, i + t1, 0);
  for (int i = 1; i <= t1; i++)
    for (int j = 1; j <= t2; j++)
      if (!is[abs(L[i] - R[j])])
        ins(i, j + t1, 1), ins(j + t1, i, 0);
  int Val = Network_Flow::dinic();
  ans += Val;
  ans += (t1 - Val) / 2 * 2 + (t2 - Val) / 2 * 2;
  ans += ((t1 - Val) & 1) * 3;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 11;
const int M = 7e5 + 11;
const int MAXN = 1e7 + 11;
const int inf = 2147483647;
int n, tot, cnt = 1, t1, t2, mx, S, T, hd[M];
int ans, pri[M], L[N << 1], R[N << 1];
bitset<MAXN> is, a;
struct Edge {
  int nxt, to, val;
} e[M];
void ins(int x, int y, int z) {
  e[++cnt].nxt = hd[x];
  e[cnt].to = y;
  hd[x] = cnt;
  e[cnt].val = z;
}
void prepare() {
  is[0] = is[1] = 1;
  for (int i = 2; i < MAXN; i++) {
    if (!is[i])
      pri[++tot] = i;
    for (int j = 1; j <= tot && i * pri[j] <= MAXN; j++) {
      is[pri[j] * i] = 1;
      if (i % pri[j] == 0)
        break;
    }
  }
}
namespace Network_Flow {
queue<int> p;
int dep[N << 1];
int bfs() {
  memset(dep, 0, sizeof(dep));
  p.push(S);
  dep[S] = 1;
  while (!p.empty()) {
    int x = p.front();
    p.pop();
    for (int i = hd[x]; i; i = e[i].nxt) {
      int y = e[i].to, v = e[i].val;
      if (!dep[y] && v) {
        dep[y] = dep[x] + 1;
        p.push(y);
      }
    }
  }
  if (dep[T])
    return 1;
  return 0;
}
int dfs(int x, int flow) {
  if (x == T || flow <= 0)
    return flow;
  int rest = 0;
  for (int i = hd[x]; i; i = e[i].nxt) {
    int j = e[i].to;
    int v = e[i].val;
    if (dep[j] == dep[x] + 1 && v) {
      int now = dfs(j, min(v, flow));
      e[i].val -= now;
      e[i ^ 1].val += now;
      flow -= now;
      rest += now;
      if (flow <= 0)
        break;
    }
  }
  if (!rest)
    dep[x] = -1;
  return rest;
}
int dinic() {
  int maxflow = 0;
  while (bfs())
    maxflow += dfs(S, inf);
  return maxflow;
}
} // namespace Network_Flow
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
signed main() {
  prepare();
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    a[x] = 1, mx = max(mx, x);
  }
  for (int i = 1; i <= mx + 1; i++)
    if (a[i] != a[i - 1])
      i & 1 ? L[++t1] = i : R[++t2] = i;
  S = 0, T = t1 + t2 + 1;
  for (int i = 1; i <= t1; i++)
    ins(S, i, 1), ins(i, S, 0);
  for (int i = 1; i <= t2; i++)
    ins(i + t1, T, 1), ins(T, i + t1, 0);
  for (int i = 1; i <= t1; i++)
    for (int j = 1; j <= t2; j++)
      if (!is[abs(L[i] - R[j])])
        ins(i, j + t1, 1), ins(j + t1, i, 0);
  int Val = Network_Flow::dinic();
  ans += Val;
  ans += (t1 - Val) / 2 * 2 + (t2 - Val) / 2 * 2;
  ans += ((t1 - Val) & 1) * 3;
  printf("%d\n", ans);
  return 0;
}