prithivMLmods/Coder-Stat huggingface dataset:
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;
  int c = 1;
  int acc = 0;
  vector<int> r;

  for (int i = 0; i < N; ++i) {
    if (s[i] == '0' + c) {
      ++acc;
      if (i == N - 1)
        r.push_back(acc);
    } else {
      c = 1 - c;
      r.push_back(acc);
      acc = 1;
    }
  }
  if (r.size() % 2 == 0)
    r.push_back(0);

  int count = 0;
  for (int i = 0; i < K; i++) {
    count += r[i * 2] + r[i * 2 + 1];
  }
  count += r[2 * K];

  int m = count;
  for (int i = 0; 2 * (i + K) < r.size() - 2; ++i) {
    count = count + r[2 * (i + K) + 1] + r[2 * (i + K) + 2] - r[2 * i] -
            r[2 * i + 1];
    if (count > m)
      m = count;
  }
  cout << m << endl;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;
  int c = 1;
  int acc = 0;
  vector<int> r;

  for (int i = 0; i < N; ++i) {
    if (s[i] == '0' + c) {
      ++acc;
      if (i == N - 1)
        r.push_back(acc);
    } else {
      c = 1 - c;
      r.push_back(acc);
      acc = 1;
    }
  }
  if (r.size() % 2 == 0)
    r.push_back(0);

  if (K >= (r.size() - 1) / 2) {
    int count = 0;
    for (auto e : r) {
      count += e;
    }
    cout << count << endl;
    return 0;
  }

  int count = 0;
  for (int i = 0; i < K; i++) {
    count += r[i * 2] + r[i * 2 + 1];
  }
  count += r[2 * K];

  int m = count;
  for (int i = 0; 2 * (i + K) < r.size() - 2; ++i) {
    count = count + r[2 * (i + K) + 1] + r[2 * (i + K) + 2] - r[2 * i] -
            r[2 * i + 1];
    if (count > m)
      m = count;
  }
  cout << m << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  vi v;
  // 1, 0, ... , 1の数を記録する
  if (s[0] == '0')
    v.push_back(0);
  REP(i, s.size()) {
    int j = i;
    while (j < s.size() && s[i] == s[j])
      ++j; // 同じ数を圧縮するため数える
    v.push_back(j - i);
    i = j - 1;
  }
  if (s.back() == '0')
    v.push_back(0);

  vi sums((int)v.size() + 1, 0);
  REP(i, v.size() + 1)
  sums[i + 1] = sums[i] + v[i]; // sum[0] が 0 だと端を特別扱いしなくて良い．

  int ans = 0;
  for (int left = 0; left < sums.size(); left += 2) {
    int right = left + 2 * k + 1;
    if (right >= sums.size())
      right = (int)sums.size() - 1;
    ans = max(ans, sums[right] - sums[left]);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  vi v;
  // 1, 0, ... , 1の数を記録する
  if (s[0] == '0')
    v.push_back(0);
  REP(i, s.size()) {
    int j = i;
    while (j < s.size() && s[i] == s[j])
      ++j; // 同じ数を圧縮するため数える
    v.push_back(j - i);
    i = j - 1;
  }
  if (s.back() == '0')
    v.push_back(0);

  vi sums((int)v.size() + 1, 0);
  REP(i, v.size())
  sums[i + 1] = sums[i] + v[i]; // sum[0] が 0 だと端を特別扱いしなくて良い．

  int ans = 0;
  for (int left = 0; left < sums.size(); left += 2) {
    int right = left + 2 * k + 1;
    if (right >= sums.size())
      right = (int)sums.size() - 1;
    ans = max(ans, sums[right] - sums[left]);
  }
  cout << ans << endl;

  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

char s[300000];
int t[300000];

int main(void) {
  int n, k;
  scanf("%d%d", &n, &k);
  k = 2 * k + 1;
  scanf("%s", s);

  char last = '0';
  int j = 2;
  for (int i = 0; i < n; i++) {
    if (s[i] == last) {
      t[j]++;
    } else {
      t[++j] = 1;
      last = s[i];
    }
  }
  ++j;
  j += 2 * k + 10;

  for (int i = 0; i < j; i++) {
    /* printf("%d ", t[i]); */
    t[i + 1] += t[i];
  }
  /* printf("\n"); */

  int m = 0;
  for (int i = 0; i + k < j; i += 2) {
    if (m < t[i + k] - t[i])
      m = t[i + k] - t[i];
  }
  printf("%d\n", m);
}


Fixed submission:
#include <cstdio>

char s[200000];
int t[500000];

int main(void) {
  int n, k;
  scanf("%d%d", &n, &k);
  k = 2 * k + 1;
  scanf("%s", s);

  char last = '0';
  int j = 2;
  for (int i = 0; i < n; i++) {
    if (s[i] == last) {
      t[j]++;
    } else {
      t[++j] = 1;
      last = s[i];
    }
  }
  ++j;
  j += 2 * k + 10;

  for (int i = 0; i < j; i++) {
    /* printf("%d ", t[i]); */
    t[i + 1] += t[i];
  }
  /* printf("\n"); */

  int m = 0;
  for (int i = 0; i + k < j; i += 2) {
    if (m < t[i + k] - t[i])
      m = t[i + k] - t[i];
  }
  printf("%d\n", m);
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;

  string s;
  cin >> s;

  vector<int> nums;
  int now = 1;
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == ('0' + now))
      count++;
    else {
      nums.push_back(count);
      now = 1 - now;
      count = 1;
    }
  }
  if (count != 0)
    nums.push_back(count);
  if (nums.size() % 2 == 0)
    nums.push_back(0);

  int add = 2 * k + 1;

  vector<int> sum(nums.size() + 1);
  for (int i = 0; i < sum.size(); i++) {
    sum[i + 1] = sum[i] + nums[i];
  }

  int ans = 0;

  for (int i = 0; i < nums.size(); i += 2) {
    int left = i;
    int right = min(i + add, (int)nums.size());
    int tmp = sum[right] - sum[left];
    ans = max(tmp, ans);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;

  string s;
  cin >> s;

  vector<int> nums;
  int now = 1;
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == ('0' + now))
      count++;
    else {
      nums.push_back(count);
      now = 1 - now;
      count = 1;
    }
  }
  if (count != 0)
    nums.push_back(count);
  if (nums.size() % 2 == 0)
    nums.push_back(0);

  int add = 2 * k + 1;

  vector<int> sum(nums.size() + 1);
  for (int i = 0; i < nums.size(); i++) {
    sum[i + 1] = sum[i] + nums[i];
  }

  int ans = 0;

  for (int i = 0; i < nums.size(); i += 2) {
    int left = i;
    int right = min(i + add, (int)nums.size());
    int tmp = sum[right] - sum[left];
    ans = max(tmp, ans);
  }

  cout << ans << endl;

  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  vector<int> size(n + k + 1, 0);
  int c = 0;
  int group_num = 0;
  bool is_zero_first = s[0] == '0';
  bool is_zero_group = is_zero_first;

  for (int i = 0; i < n; i++) {
    if ((is_zero_group && s[i] == '1') || (!is_zero_group && s[i] == '0')) {
      size[group_num] = c;
      is_zero_group = !is_zero_group;
      c = 0;
      group_num++;
    }
    c++;
    if (i == (n - 1)) {
      size[group_num] = c;
      group_num++;
    }
  }

  int max_sum = 0;
  for (int i = 0; i < group_num; i += 2) {
    int sum = 0;
    int init;
    if (is_zero_first) {
      init = i - 1;
    } else {
      init = i;
    }

    for (int j = init; j < (init + 2 * k + 1); j++) {
      if (j < 0) {
        continue;
      }
      sum += size[j];
    }
    if (sum > max_sum) {
      max_sum = sum;
    }
  }

  cout << max_sum << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  vector<int> size(n + 2 * k + 1, 0);
  int c = 0;
  int group_num = 0;
  bool is_zero_first = s[0] == '0';
  bool is_zero_group = is_zero_first;

  for (int i = 0; i < n; i++) {
    if ((is_zero_group && s[i] == '1') || (!is_zero_group && s[i] == '0')) {
      size[group_num] = c;
      is_zero_group = !is_zero_group;
      c = 0;
      group_num++;
    }
    c++;
    if (i == (n - 1)) {
      size[group_num] = c;
      group_num++;
    }
  }

  int max_sum = 0;
  for (int i = 0; i < group_num; i += 2) {
    int sum = 0;
    int init;
    if (is_zero_first) {
      init = i - 1;
    } else {
      init = i;
    }

    for (int j = init; j < (init + 2 * k + 1); j++) {
      if (j < 0) {
        continue;
      }
      sum += size[j];
    }
    if (sum > max_sum) {
      max_sum = sum;
    }
  }

  cout << max_sum << endl;

  return 0;
}
Problem p03074 (C++)

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

char input[10010];

int main() {
  input[0] = '1';
  int n, k;
  cin >> n >> k;
  cin >> &input[1];

  char *start, *cur;
  start = cur = input;
  int max = 0;

  while (*cur) {
    while (*cur && *cur == '1')
      cur++;
    if (!k)
      break;
    while (*cur && *cur == '0')
      cur++;
    k--;
  }
  max = cur - start - 1;
  while (*cur) {
    while (*start == '1')
      start++;
    while (*start == '0')
      start++;
    while (*cur && *cur == '0')
      cur++;
    while (*cur && *cur == '1')
      cur++;
    if (max < cur - start)
      max = cur - start;
  }

  cout << max << endl;
}

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

char input[100010];

int main() {
  input[0] = '1';
  int n, k;
  cin >> n >> k;
  cin >> &input[1];

  char *start, *cur;
  start = cur = input;
  int max = 0;

  while (*cur) {
    while (*cur && *cur == '1')
      cur++;
    if (!k)
      break;
    while (*cur && *cur == '0')
      cur++;
    k--;
  }
  max = cur - start - 1;
  while (*cur) {
    while (*start == '1')
      start++;
    while (*start == '0')
      start++;
    while (*cur && *cur == '0')
      cur++;
    while (*cur && *cur == '1')
      cur++;
    if (max < cur - start)
      max = cur - start;
  }

  cout << max << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  vector<int> on, ze;
  char m = s[0];
  int num_m = 0;
  if (s[0] == '0')
    on.push_back(0);
  for (int i = 0; i < n; i++) {
    if (s[i] == m) {
      num_m++;
    } else {
      if (m == '0')
        ze.push_back(num_m);
      else
        on.push_back(num_m);
      m = s[i];
      num_m = 1;
    }
    if (i == n - 1) {
      if (m == '0')
        ze.push_back(num_m);
      else
        on.push_back(num_m);
    }
  }
  if (ze.at(ze.size() - 1) == 1)
    on.push_back(0);
  int ans = 0;
  if (on.size() <= k + 1)
    k = on.size() - 1;
  for (int i = 0; i < on.size() - k; i++) {
    int ansd = 0;
    for (int j = 0; j < k + 1; j++) {
      ansd += on.at(i + j);
    }
    for (int j = 0; j < k; j++) {
      ansd += ze.at(i + j);
    }
    ans = max(ansd, ans);
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  vector<int> on, ze;
  char m = s[0];
  int num_m = 0;
  if (s[0] == '0')
    on.push_back(0);
  for (int i = 0; i < n; i++) {
    if (s[i] == m) {
      num_m++;
    } else {
      if (m == '0')
        ze.push_back(num_m);
      else
        on.push_back(num_m);
      m = s[i];
      num_m = 1;
    }
    if (i == n - 1) {
      if (m == '0')
        ze.push_back(num_m);
      else
        on.push_back(num_m);
    }
  }
  if (on.size() == ze.size())
    on.push_back(0);
  int ans = 0;
  if (on.size() <= k + 1)
    k = on.size() - 1;
  for (int i = 0; i < on.size() - k; i++) {
    int ansd = 0;
    for (int j = 0; j < k + 1; j++) {
      ansd += on.at(i + j);
    }
    for (int j = 0; j < k; j++) {
      ansd += ze.at(i + j);
    }
    ans = max(ansd, ans);
  }

  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  string s;
  cin >> s;

  vector<int> s_array;
  s_array.push_back(0);
  char num = '1';
  for (int i = 0; i < n; i++) {
    if ((char)s[i] != num) {
      s_array.push_back(1);
      num = (num == '0' ? '1' : '0');
    } else {
      s_array.back()++;
    }
  }
  if (num == '0')
    s_array.push_back(0);
  int result = 0, sum = 0;
  for (int i = 0; i < n; i += 2) {
    if (i == 0) {
      sum += s_array[i];
    } else if (i <= k * 2) {
      sum += (s_array[i - 1] + s_array[i]);
    } else {
      sum -= (s_array[i - k * 2 - 2] + s_array[i - k * 2 - 1]);
      sum += (s_array[i - 1] + s_array[i]);
    }
    result = max(result, sum);
  }
  cout << result << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  string s;
  cin >> s;

  vector<int> s_array;
  s_array.push_back(0);
  char num = '1';
  for (int i = 0; i < n; i++) {
    if ((char)s[i] != num) {
      s_array.push_back(1);
      num = (num == '0' ? '1' : '0');
    } else {
      s_array.back()++;
    }
  }
  if (num == '0')
    s_array.push_back(0);
  int result = 0, sum = 0;
  for (int i = 0; i < s_array.size(); i += 2) {
    if (i == 0) {
      sum += s_array[i];
    } else if (i <= k * 2) {
      sum += (s_array[i - 1] + s_array[i]);
    } else {
      sum -= (s_array[i - k * 2 - 2] + s_array[i - k * 2 - 1]);
      sum += (s_array[i - 1] + s_array[i]);
    }
    result = max(result, sum);
  }
  cout << result << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;

  vector<int> S;
  char current = s[0];
  if (current == '0') {
    S.push_back(0);
  }
  int num = 0;
  REP(i, N) {
    if (current == s[i]) {
      num++;
    } else {
      S.push_back(num);
      num = 1;
      current = s[i];
    }
  }
  S.push_back(num);
  if (S.size() % 2 == 0) {
    S.push_back(0);
  }

  if (S.size() <= 2 * K + 1) {
    cout << N << endl;
  } else {
    int ans = 0;
    for (int i = 0; i < S.size(); i += 2) {
      int cur = 0;
      FOR(j, i, i + 2 * K + 1) { cur += S[j]; }
      ans = max(ans, cur);
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;

  vector<int> S;
  char current = s[0];
  if (current == '0') {
    S.push_back(0);
  }
  int num = 0;
  REP(i, N) {
    if (current == s[i]) {
      num++;
    } else {
      S.push_back(num);
      num = 1;
      current = s[i];
    }
  }
  S.push_back(num);
  if (S.size() % 2 == 0) {
    S.push_back(0);
  }

  if (S.size() <= 2 * K + 1) {
    cout << N << endl;
  } else {
    int ans = 0;
    for (int i = 0; i < S.size() - 2 * K; i += 2) {
      int cur = 0;
      FOR(j, i, i + 2 * K + 1) { cur += S[j]; }
      ans = max(ans, cur);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  string s;
  int n, k, c = 0;
  vector<int> a;
  cin >> n >> k >> s;
  rep(i, n) {
    int j = i;
    while (s[j + 1] == s[j] && j + 1 < n)
      j++;
    a.push_back(j - i + 1);
    i = j;
  }
  reverse(a.begin(), a.end());
  if (s[0] == '0')
    a.push_back(0);
  reverse(a.begin(), a.end());
  if (s[n - 1] == '0')
    a.push_back(0);
  vector<int> b(a.size() - 1);
  rep(i, a.size()) b[i + 1] = b[i] + a[i];
  for (int i = 0; i < a.size(); i += 2) {
    int left = i;
    int right = min(i + 2 * k + 1, (int)a.size()); // 累積和としての左右
    c = max(c, b[right] - b[left]);
  }
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  string s;
  int n, k, c = 0;
  vector<int> a;
  cin >> n >> k >> s;
  rep(i, n) {
    int j = i;
    while (s[j + 1] == s[j] && j + 1 < n)
      j++;
    a.push_back(j - i + 1);
    i = j;
  }
  reverse(a.begin(), a.end());
  if (s[0] == '0')
    a.push_back(0);
  reverse(a.begin(), a.end());
  if (s[n - 1] == '0')
    a.push_back(0);
  vector<int> b(a.size() + 1);
  rep(i, a.size()) b[i + 1] = b[i] + a[i];
  for (int i = 0; i < a.size(); i += 2) {
    int left = i;
    int right = min(i + 2 * k + 1, (int)a.size()); // 累積和としての左右
    c = max(c, b[right] - b[left]);
  }
  cout << c << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < n; i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPEACH(i, k) for (auto &i : k)
#define all(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

signed main() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> a(n);
  int now = 0;
  bool is_one = false;
  int max = 0;
  REP(i, n) {
    if (i == 0 && s[i] == '0') {
      is_one = false;
      now++;
      a[0] = i;
    } else if (s[i] == '1' && !is_one) {
      a[now] = i;
      is_one = true;
    } else if (s[i] == '1') {
      continue;
    } else if (s[i] == '0' && is_one) {
      if (k >= now) {
        if (i - a[now - k] > max) {
          max = i - a[now - k];
        }
      }
      now++;
      is_one = false;
    }
  }
  if (now <= k) {
    cout << s.size() << endl;
    return 0;
  }
  if (s.size() - a[now - k] > max) {
    max = s.size() - a[now - k];
  }
  cout << max << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < n; i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPEACH(i, k) for (auto &i : k)
#define all(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

signed main() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> a(n);
  int now = 0;
  bool is_one = false;
  int max = 0;
  REP(i, n) {
    if (i == 0 && s[i] == '0') {
      is_one = false;
      now++;
      a[0] = i;
    } else if (s[i] == '1' && !is_one) {
      a[now] = i;
      is_one = true;
    } else if (s[i] == '1') {
      continue;
    } else if (s[i] == '0' && is_one) {
      if (k <= now) {
        if (i - a[now - k] > max) {
          max = i - a[now - k];
        }
      }
      now++;
      is_one = false;
    }
  }
  if (now <= k) {
    cout << s.size() << endl;
    return 0;
  }
  if (s.size() - a[now - k] > max) {
    max = s.size() - a[now - k];
  }
  cout << max << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, k;
  cin >> n >> k;
  string t;
  cin >> t;
  vector<int> a(0);
  int cnt = 0;
  rep(i, n) {
    if (t[i] == t[i + 1])
      cnt++;
    else {
      a.push_back(cnt + 1);
      cnt = 0;
    }
  }
  int l = a.size();
  vector<ll> s(l + 1);
  s[0] = 0;
  rep(i, l) s[i + 1] = s[i] + a[i];
  if (t[0] == '1') {
    ll ans = 0;
    rep(i, l) {
      if (i % 2 == 0) {
        if (i + 2 * k + 1 <= l)
          chmax(ans, s[i + 2 * k + 1] - s[i]);
        else
          chmax(ans, s[l] - s[i]);
      }
    }
    cout << ans << endl;
  } else {
    ll ans = s[2 * k] - s[0];
    rep(i, l) {
      if (i % 2 == 1) {
        if (i + 2 * k + 1 <= l)
          chmax(ans, s[i + 2 * k + 1] - s[i]);
        else
          chmax(ans, s[l] - s[i]);
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, k;
  cin >> n >> k;
  string t;
  cin >> t;
  vector<int> a(0);
  int cnt = 0;
  rep(i, n) {
    if (t[i] == t[i + 1])
      cnt++;
    else {
      a.push_back(cnt + 1);
      cnt = 0;
    }
  }
  int l = a.size();
  vector<ll> s(l + 1);
  s[0] = 0;
  rep(i, l) s[i + 1] = s[i] + a[i];
  if (t[0] == '1') {
    ll ans = 0;
    rep(i, l) {
      if (i % 2 == 0) {
        if (i + 2 * k + 1 <= l)
          chmax(ans, s[i + 2 * k + 1] - s[i]);
        else
          chmax(ans, s[l] - s[i]);
      }
    }
    cout << ans << endl;
  } else {
    ll ans = 2 * k <= l ? s[2 * k] - s[0] : s[l] - s[0];
    rep(i, l) {
      if (i % 2 == 1) {
        if (i + 2 * k + 1 <= l)
          chmax(ans, s[i + 2 * k + 1] - s[i]);
        else
          chmax(ans, s[l] - s[i]);
      }
    }
    cout << ans << endl;
  }
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)1e9 + 7
#define eb emplace_back

template <typename T> void out(vector<T> v) {
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> Nums;
  int now = 1; // 今見ているかず
  int cnt = 0; // nowがいくつ並んでいるか
  for (int i = 0; i < n; ++i) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.eb(cnt);
      now = 1 - now; // 0, 1 切り替え
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  // 1 - 0 - 1 - 0 - 1
  // 0で終わってたら
  if (Nums.size() % 2 == 0)
    Nums.eb(0);
  int Add = 2 * k + 1;
  int ans = 0;
  // 累積和を作る

  vector<int> sum(Nums.size() + 1);
  for (int i = 0; i < sum.size(); ++i) {
    sum[i + 1] = sum[i] + Nums[i];
  }
  // 1 - 0 - 1...の1から始めるので偶数番目だけ見る
  for (int i = 0; i < Nums.size(); i += 2) {
    // 次のleft, rightを計算する[left, right)
    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int tmp = sum[right] - sum[left];
    chmax(ans, tmp);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)1e9 + 7
#define eb emplace_back

template <typename T> void out(vector<T> v) {
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> Nums;
  int now = 1; // 今見ているかず
  int cnt = 0; // nowがいくつ並んでいるか
  for (int i = 0; i < n; ++i) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.eb(cnt);
      now = 1 - now; // 0, 1 切り替え
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  // 1 - 0 - 1 - 0 - 1
  // 0で終わってたら
  if (Nums.size() % 2 == 0)
    Nums.eb(0);
  int Add = 2 * k + 1;
  int ans = 0;
  // 累積和を作る

  vector<int> sum(Nums.size() + 1);
  for (int i = 0; i < Nums.size(); ++i) {
    sum[i + 1] = sum[i] + Nums[i];
  }
  // 1 - 0 - 1...の1から始めるので偶数番目だけ見る
  for (int i = 0; i < Nums.size(); i += 2) {
    // 次のleft, rightを計算する[left, right)
    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int tmp = sum[right] - sum[left];
    chmax(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)1e9 + 7
#define eb emplace_back

template <typename T> void out(vector<T> v) {
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> Nums;
  int now = 1; // 今見ているかず
  int cnt = 0; // nowがいくつ並んでいるか
  for (int i = 0; i < n; ++i) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.eb(cnt);
      now = 1 - now; // 0, 1 切り替え
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  // 1 - 0 - 1 - 0 - 1
  // 0で終わってたら
  if (Nums.size() % 2 == 0)
    Nums.eb(0);
  int Add = 2 * k + 1;
  int ans = 0;
  // 1 - 0 - 1...の1から始めるので偶数番目だけ見る
  for (int i = 0; i < Nums.size(); i += 2) {
    int tmp = 0;
    int left = i;
    int right = i + Add;
    for (int j = left; j < right; ++j) {
      tmp += Nums[j];
    }
    chmax(ans, tmp);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)1e9 + 7
#define eb emplace_back

template <typename T> void out(vector<T> v) {
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> Nums;
  int now = 1; // 今見ているかず
  int cnt = 0; // nowがいくつ並んでいるか
  for (int i = 0; i < n; ++i) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.eb(cnt);
      now = 1 - now; // 0, 1 切り替え
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  // 1 - 0 - 1 - 0 - 1
  // 0で終わってたら
  if (Nums.size() % 2 == 0)
    Nums.eb(0);
  int Add = 2 * k + 1;
  int ans = 0;
  // 1 - 0 - 1...の1から始めるので偶数番目だけ見る
  for (int i = 0; i < Nums.size(); i += 2) {
    int tmp = 0;
    int left = i;
    int right = min(i + Add, (int)Nums.size());
    for (int j = left; j < right; ++j) {
      tmp += Nums[j];
    }
    chmax(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)1e9 + 7
#define eb emplace_back

template <typename T> void out(vector<T> v) {
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<ll> v;
  v.push_back(0);
  // k = 1 -> 2個跨げる
  // k = 2 -> 4個跨げる
  ll c = 0;
  int now = 0;
  while (1) {
    while (s[now] == '0') {
      c++;
      now++;
      if (now == s.size())
        break;
    }
    v.eb(c);
    if (now == s.size())
      break;
    c = 0;
    while (s[now] == '1') {
      c++;
      now++;
      if (now == s.size())
        break;
    }
    v.eb(c);
    if (now == s.size())
      break;
    c = 0;
  }
  if (s[s.size() - 1] == '0')
    v.eb(0);
  // out(v);
  ll ans = v[0];
  rrep(i, 1, k * 2 + 1) ans += v[i];
  c = 0;
  int f = k * 2 + 1, b = 0;
  if (f >= v.size()) {
    cout << ans << endl;
    return 0;
  }
  ll max_tmp = 0;
  ll tmp = 0;
  while (1) {
    tmp += v[f];
    f++;
    if (f == v.size())
      break;
    tmp += v[f];
    f++;
    tmp -= v[b];
    b++;
    tmp -= v[b];
    b++;
    chmax(max_tmp, tmp);
    // cout << tmp << endl;
    if (f == v.size())
      break;
  }
  cout << ans + max_tmp << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)1e9 + 7
#define eb emplace_back

template <typename T> void out(vector<T> v) {
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<ll> v;
  v.push_back(0);
  // k = 1 -> 2個跨げる
  // k = 2 -> 4個跨げる
  ll c = 0;
  int now = 0;
  while (1) {
    while (s[now] == '0') {
      c++;
      now++;
      if (now == s.size())
        break;
    }
    v.eb(c);
    if (now == s.size())
      break;
    c = 0;
    while (s[now] == '1') {
      c++;
      now++;
      if (now == s.size())
        break;
    }
    v.eb(c);
    if (now == s.size())
      break;
    c = 0;
  }
  if (s[s.size() - 1] == '0')
    v.eb(0);
  // out(v);
  ll ans = v[0];
  rrep(i, 1, k * 2 + 1) {
    ans += v[i];
    if (i == v.size() - 1)
      break;
  }
  c = 0;
  int f = k * 2 + 1, b = 0;
  if (f >= v.size()) {
    cout << ans << endl;
    return 0;
  }
  ll max_tmp = 0;
  ll tmp = 0;
  while (1) {
    tmp += v[f];
    f++;
    if (f == v.size())
      break;
    tmp += v[f];
    f++;
    tmp -= v[b];
    b++;
    tmp -= v[b];
    b++;
    chmax(max_tmp, tmp);
    // cout << tmp << endl;
    if (f == v.size())
      break;
  }
  cout << ans + max_tmp << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  bool one_start = (S.at(0) == '1');
  vector<int> arr(100000);
  int idx = 0;
  char pre = '1';
  if (S.at(0) == '0') { // 0スタートだった
    idx++;
    arr.at(0) = 0; // 1が0個あることにする
    pre = '0';
  }
  for (int i = 0; i < S.size(); i++) {
    if (pre == S.at(i)) {
      arr.at(idx)++;
    } else {
      idx++;
      arr.at(idx)++;
    }
    pre = S.at(i);
  }

  int max_lensum = 0;
  for (int i = 0; i < arr.size(); i = i + 2) {
    int lensum = accumulate(arr.begin() + i, arr.begin() + i + 2 * K + 1, 0);
    if (lensum > max_lensum)
      max_lensum = lensum;
    if (i != 0 && arr.at(i) == 0)
      break;
  }
  cout << max_lensum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  bool one_start = (S.at(0) == '1');
  vector<int> arr(1000000);
  int idx = 0;
  char pre = '1';
  if (S.at(0) == '0') { // 0スタートだった
    idx++;
    arr.at(0) = 0; // 1が0個あることにする
    pre = '0';
  }
  for (int i = 0; i < S.size(); i++) {
    if (pre == S.at(i)) {
      arr.at(idx)++;
    } else {
      idx++;
      arr.at(idx)++;
    }
    pre = S.at(i);
  }

  int max_lensum = 0;
  for (int i = 0; i < arr.size(); i = i + 2) {
    int lensum = accumulate(arr.begin() + i, arr.begin() + i + 2 * K + 1, 0);
    if (lensum > max_lensum)
      max_lensum = lensum;
    if (i != 0 && arr.at(i) == 0)
      break;
  }
  cout << max_lensum << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

int main() {
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());

  ll N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  vector<int> numbers;

  ll now = 1;
  ll count = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now)) {
      count++;
    } else {
      numbers.push_back(count);
      now = 1 - now;
      count = 1;
    }
  }
  if (count != 0) {
    numbers.push_back(count);
  }

  // 1-0-1-0-1
  if (numbers.size() % 2 == 0) {
    numbers.push_back(0);
  }

  ll ans = 0;
  ll add = 2 * K + 1;
  for (ll i = 0; i < numbers.size(); i += 2) {
    ll sum = 0;
    ll left = i;
    ll right = min(i + add, (ll)numbers.size());
    for (ll j = left; j < right; j++) {
      sum += numbers[j];
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

int main() {
  ll N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  vector<int> numbers;

  ll now = 1;
  ll count = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now)) {
      count++;
    } else {
      numbers.push_back(count);
      now = 1 - now;
      count = 1;
    }
  }
  if (count != 0) {
    numbers.push_back(count);
  }

  // 1-0-1-0-1
  if (numbers.size() % 2 == 0) {
    numbers.push_back(0);
  }

  ll ans = 0;
  ll add = 2 * K + 1;
  for (ll i = 0; i < numbers.size(); i += 2) {
    ll sum = 0;
    ll left = i;
    ll right = min(i + add, (ll)numbers.size());
    for (ll j = left; j < right; j++) {
      sum += numbers[j];
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const ll MOD = 1e9 + 7;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {
  int n, k, count = 0, sum = 0, ans = -1;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> l, r;
  s += 'a';

  rep(i, n) {
    if (s[i] == '1')
      count++;
    else {
      if (l.size() != 0)
        r.push_back(l[l.size() - 1] + count);
      while (s[i + 1] == '0') {
        count++;
        i++;
      }
      l.push_back(count + 1);
      count = 0;
    }
  }
  r.push_back(l[l.size() - 1] + count);

  /*
  rep(i,l.size()) cout << l[i] << " " ;
  cout << endl;
  rep(i,r.size()) cout << r[i] << " ";
  cout << endl;
  */
  if (r.size() == 0 || r.size() <= k) {
    cout << n;
    return 0;
  }

  for (int i = r.size() - 1; i >= k - 1; i--) {
    sum = 0;
    rep(j, k) {
      if (j == 0)
        sum += r[i];
      else
        sum += l[i - j];
    }
    ans = max(ans, sum);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const ll MOD = 1e9 + 7;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {
  int n, k, count = 0, sum = 0, ans = -1;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> l, r;
  s += 'a';

  rep(i, n) {
    if (s[i] == '1')
      count++;
    else {
      if (l.size() != 0)
        r.push_back(l[l.size() - 1] + count);
      while (s[i + 1] == '0') {
        count++;
        i++;
      }
      l.push_back(count + 1);
      count = 0;
    }
  }
  if (count == n) {
    cout << n;
    return 0;
  }
  r.push_back(l[l.size() - 1] + count);

  /*
  rep(i,l.size()) cout << l[i] << " " ;
  cout << endl;
  rep(i,r.size()) cout << r[i] << " ";
  cout << endl;
  */
  if (r.size() == 0 || r.size() <= k) {
    cout << n;
    return 0;
  }

  for (int i = r.size() - 1; i >= k - 1; i--) {
    sum = 0;
    rep(j, k) {
      if (j == 0)
        sum += r[i];
      else
        sum += l[i - j];
    }
    ans = max(ans, sum);
  }
  cout << ans;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e5 + 5, M = 1e9 + 7;
const ull base = 13331;
const double Pi = acos(-1.0);
const ll C = 299792458;
char a[N];
struct node {
  int x, v;
} h[N];
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int tot = 0;
  int l = 1, r = 1;
  for (l = 1, r = 1; r <= n && l <= n; r++) {
    if (a[r] == a[l])
      continue;
    else {
      h[++tot].x = a[l] - '0';
      h[tot].v = r - l;
      l = r;
    }
  }
  h[++tot].x = a[l] - '0';
  h[tot].v = r - l;
  int sum = 0;
  int ans = 0;
  for (int i = 1; i <= 2 * k; i++) {
    sum += h[i].v;
  }
  if (h[2 * k].x == 0)
    ans = max(ans, sum + h[2 * k + 1].v);
  else
    ans = max(ans, sum);
  for (l = 2, r = 2 * k + 1; r + 1 <= n; l++, r++) {
    sum -= h[l - 1].v;
    sum += h[r].v;
    if (h[r].x == 0) {
      ans = max(ans, sum + h[r + 1].v);
    } else
      ans = max(ans, sum);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e6 + 5, M = 1e9 + 7;
const ull base = 13331;
const double Pi = acos(-1.0);
const ll C = 299792458;
char a[N];
struct node {
  int x, v;
} h[N];
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int tot = 0;
  int l = 1, r = 1;
  for (l = 1, r = 1; r <= n && l <= n; r++) {
    if (a[r] == a[l])
      continue;
    else {
      h[++tot].x = a[l] - '0';
      h[tot].v = r - l;
      l = r;
    }
  }
  h[++tot].x = a[l] - '0';
  h[tot].v = r - l;
  int sum = 0;
  int ans = 0;
  for (int i = 1; i <= 2 * k; i++) {
    sum += h[i].v;
  }
  if (h[2 * k].x == 0)
    ans = max(ans, sum + h[2 * k + 1].v);
  else
    ans = max(ans, sum);
  for (l = 2, r = 2 * k + 1; r + 1 <= n; l++, r++) {
    sum -= h[l - 1].v;
    sum += h[r].v;
    if (h[r].x == 0) {
      ans = max(ans, sum + h[r + 1].v);
    } else
      ans = max(ans, sum);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  vector<int> t;
  int now = 1;
  int cnt = 0;
  rep(i, 0, n) {
    if (s[i] == (char)(now + '0'))
      cnt++;
    else {
      t.push_back(cnt);
      now = 1 - now;
      cnt = 1;
    }
  }
  t.push_back(cnt);
  if ((int)t.size() % 2 == 0)
    t.push_back(0);

  int ans = 0;
  int len = t.size();
  for (int i = 0; i < len; i += 2) {
    int left = i;
    int right = left + 2 * k;
    int tmp = 0;
    for (int j = left; j <= right; j++) {
      tmp += t[j];
    }
    ans = max(ans, tmp);
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  vector<int> t;
  int now = 1;
  int cnt = 0;
  rep(i, 0, n) {
    if (s[i] == (char)(now + '0'))
      cnt++;
    else {
      t.push_back(cnt);
      now = 1 - now;
      cnt = 1;
    }
  }
  t.push_back(cnt);
  if ((int)t.size() % 2 == 0)
    t.push_back(0);

  int ans = 0;
  int len = t.size();
  for (int i = 0; i < len; i += 2) {
    int left = i;
    int right = min(left + 2 * k, len - 1);
    int tmp = 0;
    for (int j = left; j <= right; j++) {
      tmp += t[j];
    }
    ans = max(ans, tmp);
  }

  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  string S;
  cin >> S;

  vector<int> nums;
  int tmp = 1;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + tmp)) {
      cnt++;
    } else {
      nums.push_back(cnt);
      cnt = 1;
      tmp = 1 - tmp;
    }
  }
  if (cnt != 0)
    nums.push_back(cnt);
  if ((int)nums.size() % 2 == 0)
    nums.push_back(0);

  int maxNum = 0;
  for (int i = 0; i < (int)nums.size(); i += 2) {
    int tmp = 0;
    for (int j = 0; j < K * 2 + 1; j++) {
      tmp += (int)nums[i + j];
    }
    maxNum = max(maxNum, tmp);
  }

  cout << maxNum << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  string S;
  cin >> S;

  vector<int> nums;
  int tmp = 1;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + tmp)) {
      cnt++;
    } else {
      nums.push_back(cnt);
      cnt = 1;
      tmp = 1 - tmp;
    }
  }
  if (cnt != 0)
    nums.push_back(cnt);
  if ((int)nums.size() % 2 == 0)
    nums.push_back(0);

  int maxNum = 0;
  for (int i = 0; i < (int)nums.size(); i += 2) {
    int tmp = 0;
    for (int j = 0; (j < K * 2 + 1) && (i + j < (int)nums.size()); j++) {
      tmp += (int)nums[i + j];
    }
    maxNum = max(maxNum, tmp);
  }

  cout << maxNum << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int MOD = 1000000007;

int main() {
  int n, k, sum = 0;
  int ans = 0;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> a;
  int count = 1;
  if (s[0] == '0')
    a.push_back(0);
  rep(i, n - 1) {
    if (s[i] == s[i + 1])
      count++;
    else {
      a.push_back(count);
      count = 1;
    }
  }
  a.push_back(count);
  if (s[n - 1] == '0')
    a.push_back(0);
  if (a.size() == 1) {
    cout << a[0] << endl;
    return 0;
  }

  k = k * 2 + 1;
  rep(i, k) sum += a[i];
  ans = sum;
  int l = 0;
  int r = k;
  rep(i, (a.size() - k) / 2) {
    sum -= a[l];
    l++;
    sum -= a[l];
    l++;
    sum += a[r];
    r++;
    sum += a[r];
    r++;
    ans = max(sum, ans);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int MOD = 1000000007;

int main() {
  int n, k, sum = 0;
  int ans = 0;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> a;
  int count = 1;
  if (s[0] == '0')
    a.push_back(0);
  rep(i, n - 1) {
    if (s[i] == s[i + 1])
      count++;
    else {
      a.push_back(count);
      count = 1;
    }
  }
  a.push_back(count);
  if (s[n - 1] == '0')
    a.push_back(0);
  if (a.size() == 1) {
    cout << a[0] << endl;
    return 0;
  }

  k = k * 2 + 1;
  if (k >= a.size()) {
    rep(i, a.size()) sum += a[i];
    cout << sum << endl;
    return 0;
  }
  rep(i, k) sum += a[i];
  ans = sum;
  int l = 0;
  int r = k;
  rep(i, (a.size() - k) / 2) {
    sum -= a[l];
    l++;
    sum -= a[l];
    l++;
    sum += a[r];
    r++;
    sum += a[r];
    r++;
    ans = max(sum, ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  vector<int> Nums;

  // 1-0-1-0-1-0を作りたい。
  int now = 1;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      // ０と1を切り替える。
      now = 1 - now;
      cnt = 1;
    }
  }
  // 最後が加えられてなかったら加える
  if (cnt != 0)
    Nums.push_back(cnt);

  // 1-0-1-0のようにするには偶数にならないといけない。偶数じゃなかったら最後の0が足りてないってことだから0を足してあげる。
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  // 今回の見る範囲
  int Add = 2 * K + 1;

  // 事前に変化していく変数を用意する。
  int left = 0;
  int right = 0;
  int tmp = 0; // leftからrightを含めないまでの合計

  int ans = 0;

  for (int i = 0; i < Nums.size(); i += 2) {
    // 移動していくゴール地点を用意。
    int Nextleft = i;
    int Nextright = i + Add;

    // ゴール地点に行くまで、左は減らしてみぎは足していく。
    while (Nextleft > left) {
      tmp -= Nums[left];
      left++;
    }
    while (Nextright > right) {
      tmp += Nums[right];
      right++;
    }

    ans = max(ans, tmp);
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  vector<int> Nums;

  // 1-0-1-0-1-0を作りたい。
  int now = 1;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      // ０と1を切り替える。
      now = 1 - now;
      cnt = 1;
    }
  }
  // 最後が加えられてなかったら加える
  if (cnt != 0)
    Nums.push_back(cnt);

  // 1-0-1-0のようにするには偶数にならないといけない。偶数じゃなかったら最後の0が足りてないってことだから0を足してあげる。
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  // 今回の見る範囲
  int Add = 2 * K + 1;

  // 事前に変化していく変数を用意する。
  int left = 0;
  int right = 0;
  int tmp = 0; // leftからrightを含めないまでの合計

  int ans = 0;

  for (int i = 0; i < Nums.size(); i += 2) {
    // 移動していくゴール地点を用意。
    int Nextleft = i;
    int Nextright = min(i + Add, (int)Nums.size());

    // ゴール地点に行くまで、左は減らしてみぎは足していく。
    while (Nextleft > left) {
      tmp -= Nums[left];
      left++;
    }
    while (Nextright > right) {
      tmp += Nums[right];
      right++;
    }

    ans = max(ans, tmp);
  }

  cout << ans << endl;
  return 0;
}