Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

int64 calc(const vector<int32> &v) {
  vector<int64> dp[3];
  REP(i, 3) dp[i].resize(v.size(), -INF_LL);
  int32 n = v.size();
  assert(*min_element(all(v)) != 0);
  dp[2][0] = 0;
  REP(i, n - 1) {
    REP(j, 3) {
      REP(k, 3) {
        int64 num = 0;
        if (k == 0)
          num = v[i];
        else if (k == 1)
          num = v[i + 1];
        if (v[i] == 1 && j != 2)
          num = 0;
        if (j == 1 && k == 0)
          num = 0;
        chmax(dp[k][i + 1], dp[j][i] + num - (k == j && j != 2 ? 1 : 0));
      }
    }
  }
  int64 res = 0;
  /*
  REP(i, n){
          REP(j, 3){
                  cout << dp[j][i] << " ";
          }
          cout << endl;
  }
  */
  REP(i, 3) { res = max(res, dp[i][n - 1]); }
  return res;
}

int main(void) {
  int32 n;
  string s;
  cin >> n >> s;
  vector<int32> v;
  int32 cnt = 0;
  int64 res = 0;
  REP(i, s.size()) {
    if (s[i] == '1')
      cnt++;
    else {
      if (i > 0 && s[i - 1] == '0') {
        if (v.size())
          res += calc(v);
        v.clear();
      } else {
        v.push_back(cnt);
        cnt = 0;
      }
    }
  }
  if (cnt > 0)
    v.push_back(cnt);
  if (v.size())
    res += calc(v);
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

int64 calc(const vector<int32> &v) {
  vector<int64> dp[3];
  REP(i, 3) dp[i].resize(v.size(), -INF_LL);
  int32 n = v.size();
  assert(*min_element(all(v)) != 0);
  dp[2][0] = 0;
  REP(i, n - 1) {
    REP(j, 3) {
      REP(k, 3) {
        int64 num = 0;
        if (k == 0)
          num = v[i];
        else if (k == 1)
          num = v[i + 1];
        if (v[i] == 1 && j != 2)
          num = 0;
        if (j == 1 && k == 0)
          num = 0;
        chmax(dp[k][i + 1], dp[j][i] + num - (k == j && j != 2 ? 1 : 0));
      }
    }
  }
  int64 res = 0;
  /*
  REP(i, n){
          REP(j, 3){
                  cout << dp[j][i] << " ";
          }
          cout << endl;
  }
  */
  REP(i, 3) { res = max(res, dp[i][n - 1]); }
  return res;
}

int main(void) {
  int32 n;
  string s;
  cin >> n >> s;
  vector<int32> v;
  int32 cnt = 0;
  int64 res = 0;
  REP(i, s.size()) {
    if (s[i] == '1')
      cnt++;
    else {
      if (i > 0 && s[i - 1] == '0') {
        if (v.size())
          res += calc(v);
        v.clear();
      } else {
        if (cnt)
          v.push_back(cnt);
        cnt = 0;
      }
    }
  }
  if (cnt > 0)
    v.push_back(cnt);
  if (v.size())
    res += calc(v);
  cout << res << endl;
}