Problem p03132 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int l;
  cin >> l;
  vector<lint> a(l), odd(l), even(l), cum(l);
  for (int i = 0; i < l; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      odd[i] = 1;
      even[i] = 2;
    } else {
      odd[i] = (a[i] % 2 == 0) ? 1 : 0;
      even[i] = (a[i] % 2 == 0) ? 0 : 1;
    }
    cum[i] = a[i];
    if (i != 0) {
      cum[i] += cum[i - 1];
    }
  }
  const lint INF = INT64_MAX - 10;
  vector2d<lint> dp(l, vector<lint>(5, INF));
  dp[0][0] = a[0];
  dp[0][1] = even[0];
  dp[0][2] = odd[0];
  for (int i = 1; i < l; i++) {
    dp[i][0] = dp[i - 1][0] + a[i];
    dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + even[i];
    dp[i][2] = min({dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]}) + odd[i];
    dp[i][3] = min(dp[i - 1][2], dp[i - 1][3]) + even[i];
  }
  lint result = min(
      {dp[l - 1][0], dp[l - 1][1], dp[l - 1][2], dp[l - 1][3], dp[l - 1][4]});
  for (int i = 1; i < l; i++) {
    for (int j = 0; j < l; j++) {
      if (dp[i - 1][j] == INF)
        continue;
      lint tmp_result = dp[i - 1][j] + cum[l - 1] - cum[i - 1];
      UpdateMin(result, tmp_result);
    }
  }
  cout << result << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int l;
  cin >> l;
  vector<lint> a(l), odd(l), even(l), cum(l);
  for (int i = 0; i < l; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      odd[i] = 1;
      even[i] = 2;
    } else {
      odd[i] = (a[i] % 2 == 0) ? 1 : 0;
      even[i] = (a[i] % 2 == 0) ? 0 : 1;
    }
    cum[i] = a[i];
    if (i != 0) {
      cum[i] += cum[i - 1];
    }
  }
  const lint INF = INT64_MAX - 10;
  vector2d<lint> dp(l, vector<lint>(5, INF));
  dp[0][0] = a[0];
  dp[0][1] = even[0];
  dp[0][2] = odd[0];
  for (int i = 1; i < l; i++) {
    dp[i][0] = dp[i - 1][0] + a[i];
    dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + even[i];
    dp[i][2] = min({dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]}) + odd[i];
    dp[i][3] = min(dp[i - 1][2], dp[i - 1][3]) + even[i];
  }
  lint result = min(
      {dp[l - 1][0], dp[l - 1][1], dp[l - 1][2], dp[l - 1][3], dp[l - 1][4]});
  for (int i = 1; i < l; i++) {
    for (int j = 0; j < 4; j++) {
      if (dp[i - 1][j] == INF)
        continue;
      lint tmp_result = dp[i - 1][j] + cum[l - 1] - cum[i - 1];
      UpdateMin(result, tmp_result);
    }
  }
  cout << result << endl;

  return 0;
}