Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (int i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, v) for (int i = 0; i < v.size(); i++)
#define RREPALL(i, v) for (int i = v.size() - 1; i >= 0; i--)
#define SORT(v) sort(v.begin(), v.end())
#define MIN_ELEMENT(v) min_element(v.begin(), v.end())
#define MAX_ELEMENT(v) max_element(v.begin(), v.end())
#define COUNT(v, n) count(v.begin(), v.end(), n);
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

vector<int> states;
// 偶数番目(0,2,,)は直立、奇数番目(1,3,,)は逆立ち

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  bool is_stand = true;
  int idx = 1;
  states.push_back(0);
  states.push_back(0);
  REP(i, n) {
    if (s[i] == '0') {
      if (is_stand) {
        states[idx]++;
      } else {
        states.push_back(1);
        idx++;
        is_stand = true;
      }
    } else {
      if (is_stand) {
        states.push_back(1);
        idx++;
        is_stand = false;
      } else {
        states[idx]++;
      }
    }
  }
  REP(i, 100) states.push_back(0);

  vector<int> scc;
  int fscc = 0;
  REP(i, k * 2 + 1) { fscc += states[i]; }
  scc.push_back(fscc);
  int size = states.size();
  idx = 0;
  for (int i = k * 2 + 1; i < size; i += 2, idx++) {
    scc.push_back(scc[idx] - states[i - k * 2 - 1] - states[i - k * 2 - 0] +
                  states[i] + states[i + 1]);
  }
  cout << *MAX_ELEMENT(scc) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (int i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, v) for (int i = 0; i < v.size(); i++)
#define RREPALL(i, v) for (int i = v.size() - 1; i >= 0; i--)
#define SORT(v) sort(v.begin(), v.end())
#define MIN_ELEMENT(v) min_element(v.begin(), v.end())
#define MAX_ELEMENT(v) max_element(v.begin(), v.end())
#define COUNT(v, n) count(v.begin(), v.end(), n);
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

vector<int> states;
// 偶数番目(0,2,,)は直立、奇数番目(1,3,,)は逆立ち

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  bool is_stand = true;
  int idx = 1;
  states.push_back(0);
  states.push_back(0);
  REP(i, n) {
    if (s[i] == '0') {
      if (is_stand) {
        states[idx]++;
      } else {
        states.push_back(1);
        idx++;
        is_stand = true;
      }
    } else {
      if (is_stand) {
        states.push_back(1);
        idx++;
        is_stand = false;
      } else {
        states[idx]++;
      }
    }
  }
  REP(i, k * 2 + 2) states.push_back(0);

  vector<int> scc;
  int fscc = 0;
  REP(i, k * 2 + 1) { fscc += states[i]; }
  scc.push_back(fscc);
  int size = states.size();
  idx = 0;
  for (int i = k * 2 + 1; i < size; i += 2, idx++) {
    scc.push_back(scc[idx] - states[i - k * 2 - 1] - states[i - k * 2 - 0] +
                  states[i] + states[i + 1]);
  }
  cout << *MAX_ELEMENT(scc) << endl;

  return 0;
}