Problem p03642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

template <typename T = int> T Hungarian(vector<vector<T>> &A) {
  const T infty = numeric_limits<T>::max();
  const int N = (int)A.size() - 1;
  const int M = (int)A[0].size() - 1;
  vector<int> P(M + 1), way(M + 1);
  vector<T> U(N + 1, 0), V(M + 1, 0), minV;
  vector<bool> used;

  for (int i = 1; i <= N; i++) {
    P[0] = i;
    minV.assign(M + 1, infty);
    used.assign(M + 1, false);
    int j0 = 0;
    while (P[j0] != 0) {
      int i0 = P[j0], j1 = 0;
      used[j0] = true;
      T delta = infty;
      for (int j = 1; j <= M; j++) {
        if (used[j])
          continue;
        T curr = A[i0][j] - U[i0] - V[j];
        if (curr < minV[j])
          minV[j] = curr, way[j] = j0;
        if (minV[j] < delta)
          delta = minV[j], j1 = j;
      }
      for (int j = 0; j <= M; j++) {
        if (used[j])
          U[P[j]] += delta, V[j] -= delta;
        else
          minV[j] -= delta;
      }
      j0 = j1;
    }
    do {
      P[j0] = P[way[j0]];
      j0 = way[j0];
    } while (j0 != 0);
  }
  return (-V[0]);
}

const int LIM = (int)1e7;
bool prime[LIM + 1];

int main() {
  prime[0] = prime[1] = true;
  for (int i = 2; i * i <= LIM; i++) {
    if (!prime[i]) {
      for (int j = i + i; j <= LIM; j += i) {
        prime[j] = true;
      }
    }
  }

  int N;
  cin >> N;
  vector<int> vs;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (vs.empty() || vs.back() + 1 != x) {
      vs.emplace_back(x - 1);
      vs.emplace_back(x);
    } else {
      ++vs.back();
    }
  }

  vector<int> latte, malta;
  for (auto &p : vs) {
    if (p & 1)
      latte.emplace_back(p);
    else
      malta.emplace_back(p);
  }

  int ret = 0;

  if (!latte.empty() && !malta.empty()) {
    vector<vector<int>> g(latte.size() + 1, vector<int>(malta.size() + 1, 0));
    for (int i = 0; i < latte.size(); i++) {
      for (int j = 0; j < malta.size(); j++) {
        if (!prime[abs(latte[i] - malta[j])]) {
          g[i + 1][j + 1] = -1;
        }
      }
    }
    ret = -Hungarian(g);
  }

  int beet1 = latte.size() - ret;
  int beet2 = malta.size() - ret;
  ret += beet1 / 2 * 2;
  ret += beet2 / 2 * 2;
  ret += beet1 % 2 * 3;
  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <typename T = int> T Hungarian(vector<vector<T>> &A) {
  const T infty = numeric_limits<T>::max();
  const int N = (int)A.size() - 1;
  const int M = (int)A[0].size() - 1;
  vector<int> P(M + 1), way(M + 1);
  vector<T> U(N + 1, 0), V(M + 1, 0), minV;
  vector<bool> used;

  for (int i = 1; i <= N; i++) {
    P[0] = i;
    minV.assign(M + 1, infty);
    used.assign(M + 1, false);
    int j0 = 0;
    while (P[j0] != 0) {
      int i0 = P[j0], j1 = 0;
      used[j0] = true;
      T delta = infty;
      for (int j = 1; j <= M; j++) {
        if (used[j])
          continue;
        T curr = A[i0][j] - U[i0] - V[j];
        if (curr < minV[j])
          minV[j] = curr, way[j] = j0;
        if (minV[j] < delta)
          delta = minV[j], j1 = j;
      }
      for (int j = 0; j <= M; j++) {
        if (used[j])
          U[P[j]] += delta, V[j] -= delta;
        else
          minV[j] -= delta;
      }
      j0 = j1;
    }
    do {
      P[j0] = P[way[j0]];
      j0 = way[j0];
    } while (j0 != 0);
  }
  return (-V[0]);
}

const int LIM = (int)1e7;
bool prime[LIM + 1];

int main() {
  prime[0] = prime[1] = true;
  for (int i = 2; i * i <= LIM; i++) {
    if (!prime[i]) {
      for (int j = i + i; j <= LIM; j += i) {
        prime[j] = true;
      }
    }
  }

  int N;
  cin >> N;
  vector<int> vs;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (vs.empty() || vs.back() + 1 != x) {
      vs.emplace_back(x - 1);
      vs.emplace_back(x);
    } else {
      ++vs.back();
    }
  }

  vector<int> latte, malta;
  for (auto &p : vs) {
    if (p & 1)
      latte.emplace_back(p);
    else
      malta.emplace_back(p);
  }

  int ret = 0;

  if (!latte.empty() && !malta.empty()) {
    if (latte.size() > malta.size())
      swap(latte, malta);
    vector<vector<int>> g(latte.size() + 1, vector<int>(malta.size() + 1, 0));
    for (int i = 0; i < latte.size(); i++) {
      for (int j = 0; j < malta.size(); j++) {
        if (!prime[abs(latte[i] - malta[j])]) {
          g[i + 1][j + 1] = -1;
        }
      }
    }
    ret = -Hungarian(g);
  }

  int beet1 = latte.size() - ret;
  int beet2 = malta.size() - ret;
  ret += beet1 / 2 * 2;
  ret += beet2 / 2 * 2;
  ret += beet1 % 2 * 3;
  cout << ret << endl;
}