Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.endl(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
vec2int b(2, vecint(3, 0));
vec2int c(3, vecint(2, 0));
vec2int G(3, vecint(3, -1));
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n, k;
  string s;
  cin >> n >> k >> s;
  vecint rec;
  if (s[0] == '0')
    rec.push_back(0);
  REP(i, n) {
    char c = s[i];
    int tmp = 1;
    // cout << i << endl;
    while (i + 1 < n && c == s[i + 1]) {
      tmp++;
      i++;
    }
    rec.push_back(tmp);
  }
  if (s.back() == '0')
    rec.push_back(0);
  // REPe(e, rec) { cout << e << endl; }
  int m = rec.size();
  vecint sum(m + 1, 0);
  REP(i, m) { sum[i + 1] = sum[i] + rec[i]; }
  if (k >= m / 2) {
    cout << n << endl;
    return 0;
  }
  int ans = 0;
  REP(i, m / 2) {
    int l = 2 * i;
    int r = l + 2 * k;
    ans = max(ans, sum[r + 1] - sum[l]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.endl(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
vec2int b(2, vecint(3, 0));
vec2int c(3, vecint(2, 0));
vec2int G(3, vecint(3, -1));
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n, k;
  string s;
  cin >> n >> k >> s;
  vecint rec;
  if (s[0] == '0')
    rec.push_back(0);
  REP(i, n) {
    char c = s[i];
    int tmp = 1;
    // cout << i << endl;
    while (i + 1 < n && c == s[i + 1]) {
      tmp++;
      i++;
    }
    rec.push_back(tmp);
  }
  if (s.back() == '0')
    rec.push_back(0);
  // REPe(e, rec) { cout << e << endl; }
  int m = rec.size();
  vecint sum(m + 1, 0);
  REP(i, m) { sum[i + 1] = sum[i] + rec[i]; }
  if (k >= m / 2) {
    cout << n << endl;
    return 0;
  }
  int ans = 0;
  REP(i, m / 2) {
    int l = 2 * i;
    int r = l + 2 * k;
    if (r >= m)
      break;
    ans = max(ans, sum[r + 1] - sum[l]);
  }
  cout << ans << endl;
}