Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rg register
using namespace std;
const int MAXN = 1010;
struct gao {
  int u, v, Next;
} Edge[MAXN << 1];
int n, Lo, Le, Seq[10000010], Odd[MAXN], Even[MAXN];
int Ans, Es, Ps, First[MAXN], Match[MAXN], Vis[MAXN];
inline int Read() {
  rg int a = 0, c = 1;
  rg char b = getchar();
  while (b != '-' && (b < '0' || b > '9'))
    b = getchar();
  if (b == '-')
    c = -1, b = getchar();
  while (b >= '0' && b <= '9')
    a = a * 10 + b - 48, b = getchar();
  return a * c;
}
inline void Link(rg int u, rg int v) {
  Edge[++Es] = (gao){u, v, First[u]}, First[u] = Es;
}
inline int Abs(rg int A) { return A < 0 ? -A : A; }
inline bool Judge(rg int Num) {
  if (Num % 2 == 0 || Num <= 2)
    return 0;
  for (rg int i = 3, End = sqrt(Num); i <= End; i += 2)
    if (Num % i == 0)
      return 0;
  return 1;
}
inline bool Couple(rg int Now, rg int Sign) {
  for (rg int i = First[Now]; i != -1; i = Edge[i].Next) {
    rg int v = Edge[i].v;
    if (Vis[v] == Sign)
      continue;
    Vis[v] = Sign;
    if (!Match[v] || Couple(Match[v], Sign))
      return Match[v] = Now, Match[Now] = v, 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  memset(First, -1, sizeof(First));
  for (rg int i = 1; i <= n; i++)
    Seq[Read()] = 1;
  for (rg int i = 1e7 + 1; i >= 1; i--)
    Seq[i] = Seq[i] != Seq[i - 1];
  for (rg int i = 1; i <= 1e7 + 1; i++)
    if (Seq[i])
      i % 2 ? Odd[++Lo] = i : Even[++Le] = i;
  if ((Lo + Le) % 2)
    return puts("0");
  for (rg int i = 1; i <= Lo; i++)
    for (rg int j = 1; j <= Le; j++)
      if (Judge(Abs(Odd[i] - Even[j])))
        Link(i, Lo + j), Link(Lo + j, i);
  for (rg int i = 1; i <= Lo; i++)
    if (!Match[i])
      Ps += Couple(i, i);
  Ans = Ps + (Lo - Ps) / 2 * 2 + (Le - Ps) / 2 * 2 + ((Lo - Ps) % 2) * 3,
  printf("%d\n", Ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rg register
using namespace std;
const int MAXN = 20010;
struct gao {
  int u, v, Next;
} Edge[MAXN << 1];
int n, Lo, Le, Seq[10000010], Odd[MAXN], Even[MAXN];
int Ans, Es, Ps, First[MAXN], Match[MAXN], Vis[MAXN];
inline int Read() {
  rg int a = 0, c = 1;
  rg char b = getchar();
  while (b != '-' && (b < '0' || b > '9'))
    b = getchar();
  if (b == '-')
    c = -1, b = getchar();
  while (b >= '0' && b <= '9')
    a = a * 10 + b - 48, b = getchar();
  return a * c;
}
inline void Link(rg int u, rg int v) {
  Edge[++Es] = (gao){u, v, First[u]}, First[u] = Es;
}
inline int Abs(rg int A) { return A < 0 ? -A : A; }
inline bool Judge(rg int Num) {
  if (Num % 2 == 0 || Num <= 2)
    return 0;
  for (rg int i = 3, End = sqrt(Num); i <= End; i += 2)
    if (Num % i == 0)
      return 0;
  return 1;
}
inline bool Couple(rg int Now, rg int Sign) {
  for (rg int i = First[Now]; i != -1; i = Edge[i].Next) {
    rg int v = Edge[i].v;
    if (Vis[v] == Sign)
      continue;
    Vis[v] = Sign;
    if (!Match[v] || Couple(Match[v], Sign))
      return Match[v] = Now, Match[Now] = v, 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  memset(First, -1, sizeof(First));
  for (rg int i = 1; i <= n; i++)
    Seq[Read()] = 1;
  for (rg int i = 1e7 + 1; i >= 1; i--)
    Seq[i] = Seq[i] != Seq[i - 1];
  for (rg int i = 1; i <= 1e7 + 1; i++)
    if (Seq[i])
      i % 2 ? Odd[++Lo] = i : Even[++Le] = i;
  if ((Lo + Le) % 2)
    return puts("0");
  for (rg int i = 1; i <= Lo; i++)
    for (rg int j = 1; j <= Le; j++)
      if (Judge(Abs(Odd[i] - Even[j])))
        Link(i, Lo + j), Link(Lo + j, i);
  for (rg int i = 1; i <= Lo; i++)
    if (!Match[i])
      Ps += Couple(i, i);
  Ans = Ps + (Lo - Ps) / 2 * 2 + (Le - Ps) / 2 * 2 + ((Lo - Ps) % 2) * 3,
  printf("%d\n", Ans);
}