Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL << (n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) (s.substr(f - 1, t - f + 1))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
struct Grid {
  ll x, y, t;
};
struct Edge {
  ll to, cost;
};
struct Graph {
  vector<vector<Edge>> E;
  int V;
  const ll Inf = llINF;
  const int MAX_V = 201010;
  vector<ll> d;
  Graph(int n) : E(n) {
    d.resize(MAX_V);
    E.resize(n);
    V = n;
  }
  void init() {
    for (int i = 0; i < MAX_V; i++)
      d[i] = Inf;
  }
  void add_edge(ll from, ll to, ll cost) { E[from].pb({to, cost}); }
};
ll L;
vi A(210000);
ll dp[210000][5];
ll cost(ll sta, ll i) {
  if (sta == 0 || sta == 4) {
    return A[i];
  } else if (sta == 1 || sta == 3) {
    if (A[i] % 2 == 0 && A[i] != 0)
      return 0;
    else if (A[i] == 0)
      return 2;
    else
      return 1;
  } else if (sta == 2) {
    return (A[i] % 2 == 0) ? 1 : 0;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> L;
  for (int i = 0; i < L; i++)
    cin >> A[i];
  for (int i = 0; i < 210000; i++)
    for (int j = 0; j < 5; j++)
      dp[i][j] = llINF;
  dp[0][0] = 0;
  for (int i = 0; i < 210000; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = j; k < 5; k++)
        dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + cost(k, i));
    }
  }
  ll ans = llINF;
  for (int i = 0; i < 5; i++)
    ans = min(ans, dp[L][i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL << (n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) (s.substr(f - 1, t - f + 1))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
struct Grid {
  ll x, y, t;
};
struct Edge {
  ll to, cost;
};
struct Graph {
  vector<vector<Edge>> E;
  int V;
  const ll Inf = llINF;
  const int MAX_V = 201010;
  vector<ll> d;
  Graph(int n) : E(n) {
    d.resize(MAX_V);
    E.resize(n);
    V = n;
  }
  void init() {
    for (int i = 0; i < MAX_V; i++)
      d[i] = Inf;
  }
  void add_edge(ll from, ll to, ll cost) { E[from].pb({to, cost}); }
};
ll L;
vi A(210000);
ll dp[210000][5];
ll cost(ll sta, ll i) {
  if (sta == 0 || sta == 4) {
    return A[i];
  } else if (sta == 1 || sta == 3) {
    if (A[i] % 2 == 0 && A[i] != 0)
      return 0;
    else if (A[i] == 0)
      return 2;
    else
      return 1;
  } else if (sta == 2) {
    return (A[i] % 2 == 0) ? 1 : 0;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> L;
  for (int i = 0; i < L; i++)
    cin >> A[i];
  for (int i = 0; i < 210000; i++)
    for (int j = 0; j < 5; j++)
      dp[i][j] = llINF;
  dp[0][0] = 0;
  for (int i = 0; i < L; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = j; k < 5; k++)
        dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + cost(k, i));
    }
  }
  ll ans = llINF;
  for (int i = 0; i < 5; i++)
    ans = min(ans, dp[L][i]);
  cout << ans << endl;
  return 0;
}