Problem p03074 (C++)

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

typedef long long ll;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define ANS(n) cout << (n) << endl

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)

#define ALL(obj) (obj).begin(), (obj).end()

#define pii pair<int, int>

#define pb(a) push_back(a)
#define mp make_pair

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> cnt;
  char last = s[0];
  int tmp = 0;
  FOR(i, 0, n) {
    if (s[i] == last) {
      tmp++;
    } else {
      cnt.pb(tmp);
      tmp = 1;
    }
    last = s[i];
  }
  cnt.pb(tmp);
  vector<int> cntRuiseki;
  cntRuiseki.pb(0);
  FOR(i, 0, n) { cntRuiseki.pb(cnt[i] + cntRuiseki[i]); }
  int i;
  if (s[0] == '0') {
    i = 1;
  } else {
    i = 0;
  }
  int ans = 0;
  while (i < (int)cnt.size()) {
    tmp = cntRuiseki[min(i + k * 2 + 1, (int)cntRuiseki.size() - 1)] -
          cntRuiseki[i];
    /*FOR(j,0,k*2+1){
            if(i+j >= (int)cnt.size())break;
            tmp += cnt[i+j];
    }*/
    ans = max(ans, tmp);
    i += 2;
  }
  if (s[0] == '0') {
    i = 0;
  } else {
    i = 1;
  }
  while (i < (int)cnt.size()) {
    tmp =
        cntRuiseki[min(i + k * 2, (int)cntRuiseki.size() - 1)] - cntRuiseki[i];
    /*		FOR(j,0,k*2){
                            if(i+j >= (int)cnt.size())break;
                            tmp += cnt[i+j];
                    }*/
    ans = max(ans, tmp);
    i += 2;
  }
  // FOR(i,0,(int)cnt.size())ANS(cnt[i]);
  ANS(ans);
  return 0;
}


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

typedef long long ll;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define ANS(n) cout << (n) << endl

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)

#define ALL(obj) (obj).begin(), (obj).end()

#define pii pair<int, int>

#define pb(a) push_back(a)
#define mp make_pair

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> cnt;
  char last = s[0];
  int tmp = 0;
  FOR(i, 0, n) {
    if (s[i] == last) {
      tmp++;
    } else {
      cnt.pb(tmp);
      tmp = 1;
    }
    last = s[i];
  }
  cnt.pb(tmp);
  vector<int> cntRuiseki;
  cntRuiseki.pb(0);
  FOR(i, 0, (int)cnt.size()) { cntRuiseki.pb(cnt[i] + cntRuiseki[i]); }
  int i;
  if (s[0] == '0') {
    i = 1;
  } else {
    i = 0;
  }
  int ans = 0;
  while (i < (int)cnt.size()) {
    tmp = cntRuiseki[min(i + k * 2 + 1, (int)cntRuiseki.size() - 1)] -
          cntRuiseki[i];
    /*FOR(j,0,k*2+1){
            if(i+j >= (int)cnt.size())break;
            tmp += cnt[i+j];
    }*/
    ans = max(ans, tmp);
    i += 2;
  }
  if (s[0] == '0') {
    i = 0;
  } else {
    i = 1;
  }
  while (i < (int)cnt.size()) {
    tmp =
        cntRuiseki[min(i + k * 2, (int)cntRuiseki.size() - 1)] - cntRuiseki[i];
    /*		FOR(j,0,k*2){
                            if(i+j >= (int)cnt.size())break;
                            tmp += cnt[i+j];
                    }*/
    ans = max(ans, tmp);
    i += 2;
  }
  // FOR(i,0,(int)cnt.size())ANS(cnt[i]);
  ANS(ans);
  return 0;
}