Problem p03642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
#define N 806
#define ll long long
#define mp make_pair
ll read() {
  ll f = 1, x = 0;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
int t, n, x[N], st[N], dep[N], top, size, head[N], S, T, pre[10000020], cnt;
bool isp[10000020];
struct ndoe {
  int next, to, vlu;
} mem[N * N * 4];
void add(int u, int v, int vlu) {
  mem[++size].to = v;
  mem[size].next = head[u];
  head[u] = size;
  mem[size].vlu = vlu;
  mem[++size].to = u;
  mem[size].next = head[v];
  head[v] = size;
  mem[size].vlu = 0;
}
void init() {
  int top = 1e7;
  isp[1] = 1;
  for (int i = 2; i <= top; ++i) {
    if (!isp[i])
      pre[++cnt] = i;
    for (int j = 1; j <= cnt && pre[j] * i <= top; ++j) {
      isp[pre[j] * i] = 1;
      if (i % pre[j] == 0)
        break;
    }
  }
}
queue<int> q;
bool bfs() {
  memset(dep, 0, sizeof(dep));
  dep[S] = 1;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = mem[i].next) {
      int v = mem[i].to;
      if (mem[i].vlu && !dep[v]) {
        dep[v] = dep[x] + 1;
        q.push(v);
      }
    }
  }
  return dep[T];
}
int dfs(int x, int flow) {
  if (x == T)
    return flow;
  int now, used = 0;
  for (int i = head[x]; i; i = mem[i].next) {
    int v = mem[i].to;
    if (mem[i].vlu && dep[v] == dep[x] + 1) {
      now = dfs(v, min(mem[i].vlu, flow - used));
      used += now;
      mem[i].vlu -= now;
      mem[i ^ 1].vlu += now;
      if (used == flow)
        break;
    }
  }
  if (used != flow)
    dep[x] = -1;
  return used;
}
int main() {
  //	freopen("light3.in","r",stdin);
  //	freopen("B.out","w",stdout);
  init();
  t = read();
  while (t--) {
    top = 0;
    size = 1;
    memset(head, 0, sizeof(head));
    n = read();
    for (int i = 1; i <= n; ++i)
      x[i] = read();
    for (int i = 1; i <= n; ++i) {
      st[++top] = x[i] - 1;
      int now = x[i], r = i;
      while (x[r] == now && r <= n)
        now++, r++;
      i = r - 1;
      st[++top] = x[r - 1];
    }
    //	for(int i=1;i<=top;++i) cout<<st[i]<<" ";cout<<'\n';
    S = top + 1, T = S + 1;
    int s1 = 0, s2 = 0;
    for (int i = 1; i <= top; ++i) {
      if (st[i] & 1)
        add(S, i, 1), s1++;
      else
        add(i, T, 1), s2++;
      for (int j = i + 1; j <= top; ++j)
        if (st[j] - st[i] != 2 && !isp[st[j] - st[i]])
          if (st[i] & 1)
            add(i, j, 1);
          else
            add(j, i, 1);
    }
    int k = 0;

    while (bfs())
      k += dfs(S, 1e9);
    //	cout<<"k"<<k<<" "<<s1<<" "<<s2<<'\n';
    cout << k + 2 * ((s1 - k) / 2 + (s2 - k) / 2) + 3 * ((s1 - k) % 2) << '\n';
  }
  return 0;
}
/*
1
1
6
*/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
#define N 806
#define ll long long
#define mp make_pair
ll read() {
  ll f = 1, x = 0;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
int t, n, x[N], st[N], dep[N], top, size, head[N], S, T, pre[10000020], cnt;
bool isp[10000020];
struct ndoe {
  int next, to, vlu;
} mem[N * N * 4];
void add(int u, int v, int vlu) {
  mem[++size].to = v;
  mem[size].next = head[u];
  head[u] = size;
  mem[size].vlu = vlu;
  mem[++size].to = u;
  mem[size].next = head[v];
  head[v] = size;
  mem[size].vlu = 0;
}
void init() {
  int top = 1e7;
  isp[1] = 1;
  for (int i = 2; i <= top; ++i) {
    if (!isp[i])
      pre[++cnt] = i;
    for (int j = 1; j <= cnt && pre[j] * i <= top; ++j) {
      isp[pre[j] * i] = 1;
      if (i % pre[j] == 0)
        break;
    }
  }
}
queue<int> q;
bool bfs() {
  memset(dep, 0, sizeof(dep));
  dep[S] = 1;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = mem[i].next) {
      int v = mem[i].to;
      if (mem[i].vlu && !dep[v]) {
        dep[v] = dep[x] + 1;
        q.push(v);
      }
    }
  }
  return dep[T];
}
int dfs(int x, int flow) {
  if (x == T)
    return flow;
  int now, used = 0;
  for (int i = head[x]; i; i = mem[i].next) {
    int v = mem[i].to;
    if (mem[i].vlu && dep[v] == dep[x] + 1) {
      now = dfs(v, min(mem[i].vlu, flow - used));
      used += now;
      mem[i].vlu -= now;
      mem[i ^ 1].vlu += now;
      if (used == flow)
        break;
    }
  }
  if (used != flow)
    dep[x] = -1;
  return used;
}
int main() {
  //	freopen("light3.in","r",stdin);
  //	freopen("B.out","w",stdout);
  init();
  t = 1;
  while (t--) {
    top = 0;
    size = 1;
    memset(head, 0, sizeof(head));
    n = read();
    for (int i = 1; i <= n; ++i)
      x[i] = read();
    for (int i = 1; i <= n; ++i) {
      st[++top] = x[i] - 1;
      int now = x[i], r = i;
      while (x[r] == now && r <= n)
        now++, r++;
      i = r - 1;
      st[++top] = x[r - 1];
    }
    //	for(int i=1;i<=top;++i) cout<<st[i]<<" ";cout<<'\n';
    S = top + 1, T = S + 1;
    int s1 = 0, s2 = 0;
    for (int i = 1; i <= top; ++i) {
      if (st[i] & 1)
        add(S, i, 1), s1++;
      else
        add(i, T, 1), s2++;
      for (int j = i + 1; j <= top; ++j)
        if (st[j] - st[i] != 2 && !isp[st[j] - st[i]])
          if (st[i] & 1)
            add(i, j, 1);
          else
            add(j, i, 1);
    }
    int k = 0;

    while (bfs())
      k += dfs(S, 1e9);
    //	cout<<"k"<<k<<" "<<s1<<" "<<s2<<'\n';
    cout << k + 2 * ((s1 - k) / 2 + (s2 - k) / 2) + 3 * ((s1 - k) % 2) << '\n';
  }
  return 0;
}
/*
1
1
6
*/