Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define INFF 9223372036854775807
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, m, n) for (int i = m; i < n; i++)
#define ALL(N) (N.begin(), N.end())
#define de cout << "debug" << endl;
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> E, Z;
  int es = 0, zs = 0;
  if (S.size() == 1) {
    cout << 1 << endl;
    return 0;
  } else if (S.size() == 2) {
    cout << 2 << endl;
    return 0;
  }

  if (S[0] == '0')
    E.push_back(0);
  REP(i, S.size() - 1) {
    if (S[i] == '1') {
      es++;
      if (S[i + 1] == '0') {
        E.push_back(es);
        es = 0;
      }
    } else if (S[i] == '0') {
      zs++;
      if (S[i + 1] == '1') {
        Z.push_back(zs);
        zs = 0;
      }
    }

    if (i == S.size() - 2) {
      if (S[i + 1] == '1') {
        es++;
        E.push_back(es);
      } else {
        zs++;
        Z.push_back(zs);
      }
    }
  }
  if (S[S.size() - 1] == '0')
    E.push_back(0);
  int sum = 0;
  int maxv = 0;
  REP(i, E.size() - K) {
    REP(k, K) { sum += E[i + k] + Z[i + k]; }
    sum += E[i + K];
    maxv = max(maxv, sum);
    sum = 0;
  }
  cout << maxv << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define INFF 9223372036854775807
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, m, n) for (int i = m; i < n; i++)
#define ALL(N) (N.begin(), N.end())
#define de cout << "debug" << endl;
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> E, Z;
  int es = 0, zs = 0;
  if (S.size() == 1) {
    cout << 1 << endl;
    return 0;
  } else if (S.size() == 2) {
    cout << 2 << endl;
    return 0;
  }

  if (S[0] == '0')
    E.push_back(0);
  REP(i, S.size() - 1) {
    if (S[i] == '1') {
      es++;
      if (S[i + 1] == '0') {
        E.push_back(es);
        es = 0;
      }
    } else if (S[i] == '0') {
      zs++;
      if (S[i + 1] == '1') {
        Z.push_back(zs);
        zs = 0;
      }
    }

    if (i == S.size() - 2) {
      if (S[i + 1] == '1') {
        es++;
        E.push_back(es);
      } else {
        zs++;
        Z.push_back(zs);
      }
    }
  }
  if (S[S.size() - 1] == '0')
    E.push_back(0);
  int sum = 0;
  int maxv = 0;
  if (Z.size() <= K) {
    cout << N << endl;
    return 0;
  }
  REP(i, E.size() - K) {
    REP(k, K) { sum += E[i + k] + Z[i + k]; }
    sum += E[i + K];
    maxv = max(maxv, sum);
    sum = 0;
  }
  cout << maxv << endl;
}