Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

int N;
int K;
char hito[200000];
int renzoku[20000];
int kotae;
int kotae2;

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> hito[i];
  }
  for (int i = 0; i < N; i++) {
    if (hito[i] == '1')
      hito[i] = '0';
    else
      break;
  }
  for (int i = N - 1; i >= 0; i--) {
    if (hito[i] == '1')
      hito[i] = '0';
    else
      break;
  }

  char now = '0';
  int now_num = 0;
  int now_idx = 0;
  for (int i = 0; i < N; i++) {
    if (hito[i] == now) {
      now_num++;
    } else {
      renzoku[now_idx] = now_num;
      now_num = 1;
      now_idx++;
      if (now == '0')
        now = '1';
      else
        now = '0';
    }
  }
  renzoku[now_idx] = now_num;
  if (now_idx < 2 * K - 1) {
    cout << N << endl;
    return 0;
  } else {
    kotae = 0;
    for (int i = 0; i < 2 * K; i++) {
      kotae += renzoku[i];
      kotae2 = max(kotae2, kotae);
    }
    for (int i = 2 * K; i <= now_idx; i = i + 2) {
      if (i == 2 * K) {
        kotae += renzoku[i];
        kotae += renzoku[i + 1];
        kotae -= renzoku[i - 2 * K];
        kotae2 = max(kotae2, kotae);
      } else if (i == now_idx) {
        kotae += renzoku[i];
        kotae -= renzoku[i - 2 * K];
        kotae -= renzoku[i - 2 * K - 1];
        kotae2 = max(kotae2, kotae);
      } else {
        kotae += renzoku[i];
        kotae += renzoku[i + 1];
        kotae -= renzoku[i - 2 * K];
        kotae -= renzoku[i - 2 * K - 1];
        kotae2 = max(kotae2, kotae);
      }
    }
  }
  cout << kotae2 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

int N;
int K;
char hito[200000];
int renzoku[200000];
int kotae;
int kotae2;

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> hito[i];
  }
  for (int i = 0; i < N; i++) {
    if (hito[i] == '1')
      hito[i] = '0';
    else
      break;
  }
  for (int i = N - 1; i >= 0; i--) {
    if (hito[i] == '1')
      hito[i] = '0';
    else
      break;
  }

  char now = '0';
  int now_num = 0;
  int now_idx = 0;
  for (int i = 0; i < N; i++) {
    if (hito[i] == now) {
      now_num++;
    } else {
      renzoku[now_idx] = now_num;
      now_num = 1;
      now_idx++;
      if (now == '0')
        now = '1';
      else
        now = '0';
    }
  }
  renzoku[now_idx] = now_num;
  if (now_idx < 2 * K - 1) {
    cout << N << endl;
    return 0;
  } else {
    kotae = 0;
    for (int i = 0; i < 2 * K; i++) {
      kotae += renzoku[i];
      kotae2 = max(kotae2, kotae);
    }
    for (int i = 2 * K; i <= now_idx; i = i + 2) {
      if (i == 2 * K) {
        kotae += renzoku[i];
        kotae += renzoku[i + 1];
        kotae -= renzoku[i - 2 * K];
        kotae2 = max(kotae2, kotae);
      } else if (i == now_idx) {
        kotae += renzoku[i];
        kotae -= renzoku[i - 2 * K];
        kotae -= renzoku[i - 2 * K - 1];
        kotae2 = max(kotae2, kotae);
      } else {
        kotae += renzoku[i];
        kotae += renzoku[i + 1];
        kotae -= renzoku[i - 2 * K];
        kotae -= renzoku[i - 2 * K - 1];
        kotae2 = max(kotae2, kotae);
      }
    }
  }
  cout << kotae2 << endl;
  return 0;
}