Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

int main() {
  int N, K;
  string line;
  cin >> N >> K;
  cin >> line;
  int now = 1;
  int cnt = 0;
  vector<int> lines;
  for (int i = 0; i < N; i++) {
    if (line[i] == (char)('0' + now)) {
      cnt++;
    } else {
      lines.push_back(cnt);
      now = 1 - now;
      cnt = 1;
    }
  }
  if (cnt != 0) {
    lines.push_back(cnt);
  }
  if (lines.size() % 2 == 0) {
    lines.push_back(0); // 1-0-1-0-1-...-0-1
  }
  int add = 2 * K + 1;
  int ans = 0;

  /// normal
  // 1-0-1... start from even number
  /*for (int i = 0; i < lines.size(); i+=2) {
    int tmp = 0;
    int left = i;
    int right = min(i + add, (int)lines.size());
    for (int j = left; j < right; j++) {
      tmp += lines[j];
    }
    ans = max(tmp, ans);
  }*/

  /// syakutori-method
  /*
   * int left = 0, right = 0;
   * int tmp = 0; // sum of [left, right)
   * for (int i = 0; i < lines.size(); i+=2) {
   *   int next_left = i;
   *   int next_right = min(i+add, (int)lines.size());
   *
   *   while (next_left > left) {
   *     tmp -= lines[left];
   *     left++;
   *   }
   *   while (next_right > right) {
   *     tmp += lines[right];
   *     right++;
   *   }
   *   ans = max(tmp, ans);
   * }
   */

  /// ruisekiwa
  // 0 1 2 3 4 5 6
  //  0 1 2 3 4 5
  vector<int> sum(lines.size() + 1);
  for (int i = 0; i < sum.size(); i++) {
    sum[i + 1] = sum[i] + lines[i];
  }
  for (int i = 0; i < lines.size(); i += 2) {
    int left = i;
    int right = min(i + add, (int)lines.size());
    int tmp = sum[right] - sum[left];
    ans = max(tmp, ans);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

int main() {
  int N, K;
  string line;
  cin >> N >> K;
  cin >> line;
  int now = 1;
  int cnt = 0;
  vector<int> lines;
  for (int i = 0; i < N; i++) {
    if (line[i] == (char)('0' + now)) {
      cnt++;
    } else {
      lines.push_back(cnt);
      now = 1 - now;
      cnt = 1;
    }
  }
  if (cnt != 0) {
    lines.push_back(cnt);
  }
  if (lines.size() % 2 == 0) {
    lines.push_back(0); // 1-0-1-0-1-...-0-1
  }
  int add = 2 * K + 1;
  int ans = 0;

  /// normal
  // 1-0-1... start from even number
  /*for (int i = 0; i < lines.size(); i+=2) {
    int tmp = 0;
    int left = i;
    int right = min(i + add, (int)lines.size());
    for (int j = left; j < right; j++) {
      tmp += lines[j];
    }
    ans = max(tmp, ans);
  }*/

  /// syakutori-method
  /*
   * int left = 0, right = 0;
   * int tmp = 0; // sum of [left, right)
   * for (int i = 0; i < lines.size(); i+=2) {
   *   int next_left = i;
   *   int next_right = min(i+add, (int)lines.size());
   *
   *   while (next_left > left) {
   *     tmp -= lines[left];
   *     left++;
   *   }
   *   while (next_right > right) {
   *     tmp += lines[right];
   *     right++;
   *   }
   *   ans = max(tmp, ans);
   * }
   */

  /// ruisekiwa
  // 0 1 2 3 4 5 6
  //  0 1 2 3 4 5
  vector<int> sum(lines.size() + 1);
  for (int i = 0; i < lines.size(); i++) {
    sum[i + 1] = sum[i] + lines[i];
  }
  for (int i = 0; i < lines.size(); i += 2) {
    int left = i;
    int right = min(i + add, (int)lines.size());
    int tmp = sum[right] - sum[left];
    ans = max(tmp, ans);
  }
  cout << ans << endl;
  return 0;
}