Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 1e5 + 1;
static const ll MOD = 1e9 + 7;

namespace Printer {
void print(vector<int> v) {
  rep(i, v.size()) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}

void print(vector<vector<int>> vv) {
  rep(i, vv.size()) {
    rep(j, vv[i].size()) {
      if (j)
        cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}

void print(pair<int, int> p) {
  cout << "(" << p.first << ", " << p.second << ")" << endl;
}
}; // namespace Printer

//--global--//

//----------//

vector<pair<char, int>> Ranlength(string str) {
  vector<pair<char, int>> ranlength;

  pair<char, int> f;
  char lastChar;
  int cnt = 0;
  rep(i, str.size()) {
    if (lastChar != str[i] || i == str.size()) {
      if (i != 0) {
        f = make_pair(lastChar, cnt);
        ranlength.push_back(f);
      }
      lastChar = str[i];
      cnt = 1;
    } else {
      cnt++;
    }
  }
  f = make_pair(lastChar, cnt);
  ranlength.push_back(f);

  // rep(i, ranlength.size()){ // for debug
  //     cout << ranlength[i].first << ranlength[i].second << endl;
  // }
  return ranlength;
}

int main(int argc, const char *argv[]) {
  // 提出時、消す----//
  //--------------//

  int n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  vector<pair<char, int>> v;
  v = Ranlength(str);
  if (str[0] != '1') {
    v.insert(v.begin(), make_pair(1, 0));
  }
  if (*str.end() != '1') {
    v.insert(v.end(), make_pair(1, 0));
  }

  int zeroNum = 0;
  for (pair<char, int> f : v) {
    char ch = f.first;
    if (ch == '0') {
      zeroNum++;
    }
  }

  int cnt = max(0, zeroNum - k) + 1;

  // cout << cnt << endl;

  int ans = 0;
  rep(i, cnt) {
    int start = i * 2;
    int end = k * 2 + 1;
    int tmp = 0;
    rep(j, end) { tmp += v[j + start].second; }
    ans = max(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 1e5 + 1;
static const ll MOD = 1e9 + 7;

namespace Printer {
void print(vector<int> v) {
  rep(i, v.size()) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}

void print(vector<vector<int>> vv) {
  rep(i, vv.size()) {
    rep(j, vv[i].size()) {
      if (j)
        cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}

void print(pair<int, int> p) {
  cout << "(" << p.first << ", " << p.second << ")" << endl;
}
}; // namespace Printer

//--global--//

//----------//

vector<pair<char, int>> Ranlength(string str) {
  vector<pair<char, int>> ranlength;

  pair<char, int> f;
  char lastChar;
  int cnt = 0;
  rep(i, str.size()) {
    if (lastChar != str[i] || i == str.size()) {
      if (i != 0) {
        f = make_pair(lastChar, cnt);
        ranlength.push_back(f);
      }
      lastChar = str[i];
      cnt = 1;
    } else {
      cnt++;
    }
  }
  f = make_pair(lastChar, cnt);
  ranlength.push_back(f);

  // rep(i, ranlength.size()){ // for debug
  //     cout << ranlength[i].first << ranlength[i].second << endl;
  // }
  return ranlength;
}

int main(int argc, const char *argv[]) {
  // 提出時、消す----//
  //--------------//

  int n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  vector<pair<char, int>> v;
  v = Ranlength(str);
  if (str[0] != '1') {
    v.insert(v.begin(), make_pair(1, 0));
  }
  if (*str.end() != '1') {
    v.insert(v.end(), make_pair(1, 0));
  }

  int zeroNum = 0;
  for (pair<char, int> f : v) {
    char ch = f.first;
    if (ch == '0') {
      zeroNum++;
    }
  }

  int cnt = max(0, zeroNum - k) + 1;

  // cout << cnt << endl;

  int ans = 0;
  rep(i, cnt) {
    int start = i * 2;
    int end = min(k, zeroNum) * 2 + 1;
    int tmp = 0;
    rep(j, end) { tmp += v[j + start].second; }
    ans = max(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}