Problem p03074 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define ll long long
#define d_2 100000
#define d9_7 1000000007
int main(void) {
  int k, n;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> accum(n, 0);
  accum[0] = s[0] - '0';
  int num0 = 0;
  int num1 = 0;
  int nnn = 0;
  rep(i, n) {
    if (s[i] == '0') {
      num0++;
    } else {
      num1++;
    }
    if (i < n - 1) {
      if (s[i] == '0' && s[i + 1] == '1') {
        nnn++;
      }
    }
  }
  // cout<<"a";
  if (s[n - 1] == '0')
    nnn++;
  rep(i, n) {
    if (s[i + 1] == '1') {
      accum[i + 1] = accum[i] + 1;
    } else {
      accum[i + 1] = accum[i];
    }
  }
  // kやたら多い場合
  if (nnn <= k) {
    cout << n << endl;
    return 0;
  }
  // cout<<"b";
  vector<int> accum1;
  int cnt = 1;
  rep(i, n - 1) {
    if (s[i] == s[i + 1]) {
      cnt++;
    } else {
      accum1.push_back(cnt);
      cnt = 1;
    }
  }
  // cout<<"c";
  accum1.push_back(cnt);

  int ini = s[0] - '0';
  int size = accum1.size();

  /*rep(i,size){
    cout<<accum1[i]<<" ";
  }
  cout<<"ini "<<ini<<endl;*/
  k--;

  vector<int> accum2(size);
  accum2[0] = accum1[0];
  rep(i, size - 1) { accum2[i + 1] = accum1[i + 1] + accum2[i]; }
  int rmax = accum2[min(ini + k * 2 + 1, size - 1)];
  for (int now = ini + 2; now + k * 2 < size; now += 2) {
    // cout<<rmax<<" r ";
    rmax = max(rmax, accum2[min(now + k * 2 + 1, size - 1)] - accum2[now - 2]);
  }
  // cout<<endl;
  cout << rmax << endl;
  return 0;
}

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define ll long long
#define d_2 100000
#define d9_7 1000000007
int main(void) {
  int k, n;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> accum(n, 0);
  accum[0] = s[0] - '0';
  int num0 = 0;
  int num1 = 0;
  int nnn = 0;
  rep(i, n) {
    if (s[i] == '0') {
      num0++;
    } else {
      num1++;
    }
    if (i < n - 1) {
      if (s[i] == '0' && s[i + 1] == '1') {
        nnn++;
      }
    }
  }
  // cout<<"a";
  if (s[n - 1] == '0')
    nnn++;
  // kやたら多い場合
  if (nnn <= k) {
    cout << n << endl;
    return 0;
  }
  // cout<<"b";
  vector<int> accum1;
  int cnt = 1;
  rep(i, n - 1) {
    if (s[i] == s[i + 1]) {
      cnt++;
    } else {
      accum1.push_back(cnt);
      cnt = 1;
    }
  }
  // cout<<"c";
  accum1.push_back(cnt);

  int ini = s[0] - '0';
  int size = accum1.size();

  /*rep(i,size){
    cout<<accum1[i]<<" ";
  }
  cout<<"ini "<<ini<<endl;*/
  k--;

  vector<int> accum2(size);
  accum2[0] = accum1[0];
  rep(i, size - 1) { accum2[i + 1] = accum1[i + 1] + accum2[i]; }
  int rmax = accum2[min(ini + k * 2 + 1, size - 1)];
  for (int now = ini + 2; now + k * 2 < size; now += 2) {
    // cout<<rmax<<" r ";
    rmax = max(rmax, accum2[min(now + k * 2 + 1, size - 1)] - accum2[now - 2]);
  }
  // cout<<endl;
  cout << rmax << endl;
  return 0;
}