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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  char a[11111];
  cin >> a;
  int count = 0, n = strlen(a);
  for (int i = 0; i < strlen(a); i++) {
    if (i % 2 == 0 && a[i] == '0')
      count++;
    if (i % 2 == 1 && a[i] == '1')
      count++;
  }
  cout << min(count, n - count) << endl;
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  char a[111111];
  cin >> a;
  int count = 0, n = strlen(a);
  for (int i = 0; i < strlen(a); i++) {
    if (i % 2 == 0 && a[i] == '0')
      count++;
    if (i % 2 == 1 && a[i] == '1')
      count++;
  }
  cout << min(count, n - count) << endl;
  return (0);
}
Problem p03073 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define INF (ll)1e18
#define MOD (ll)1e9 + 7
#define pb push_back

typedef long long ll;
typedef long long int llt;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

char dif = 'A' - 'a';
int mod = 1e9 + 7;
ll inf = 1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  char t[12345], u[12345];
  for (int i = 0; i < n; i++) {
    if (i % 2 == 0) {
      t[i] = '1', u[i] = '0';
    } else
      t[i] = '0', u[i] = '1';
  }
  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] != t[i])
      a++;
    if (s[i] != u[i])
      b++;
  }
  cout << min(a, b) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define INF (ll)1e18
#define MOD (ll)1e9 + 7
#define pb push_back

typedef long long ll;
typedef long long int llt;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

char dif = 'A' - 'a';
int mod = 1e9 + 7;
ll inf = 1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  char t[112345], u[112345];
  for (int i = 0; i < n; i++) {
    if (i % 2 == 0) {
      t[i] = '1', u[i] = '0';
    } else
      t[i] = '0', u[i] = '1';
  }
  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] != t[i])
      a++;
    if (s[i] != u[i])
      b++;
  }
  cout << min(a, b) << endl;
  return 0;
}
Problem p03073 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch[100];
  scanf("%s", &ch);
  int len = strlen(ch), cnt = 0;
  if (len == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < len - 1; i++) {
    if (ch[i] == ch[i + 1]) {
      if (ch[i] == '0') {
        ch[i + 1] = '1';
        ++cnt;
      } else if (ch[i] == '1') {
        ch[i + 1] = '0';
        ++cnt;
      }
    }
  }
  printf("%d\n", cnt);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch[100000];
  scanf("%s", &ch);
  int len = strlen(ch), cnt = 0;
  if (len == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < len - 1; i++) {
    if (ch[i] == ch[i + 1]) {
      if (ch[i] == '0') {
        ch[i + 1] = '1';
        ++cnt;
      } else if (ch[i] == '1') {
        ch[i + 1] = '0';
        ++cnt;
      }
    }
  }
  printf("%d\n", cnt);
}
Problem p03073 (Python)

Buggy submission (status: Runtime Error):
a = list(input())
b = a[::2].index("0") + a[1::2].index("1")
c = min(b, len(a) - b)
print(c)


Fixed submission:
a = list(input())
b = a[::2].count("0") + a[1::2].count("1")
c = min(b, len(a) - b)
print(c)
Problem p03073 (C++)

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

int main() {
  char S[10001];
  scanf("%s", S);
  int len = strlen(S);

  int ans1 = 0, ans2 = 0;
  for (int i = 0; i < len; i++) {
    if (i % 2 == S[i] - '0') {
      // 黒色スタート時の塗り替え数を検証
      ans1++;
    } else {
      // 白色スタート時の塗り替え数を検証
      ans2++;
    }
  }

  printf("%d", min(ans1, ans2));
  return 0;
}

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

int main() {
  char S[100010];
  scanf("%s", S);
  int len = strlen(S);

  int ans1 = 0, ans2 = 0;
  for (int i = 0; i < len; i++) {
    if (i % 2 == S[i] - '0') {
      // 黒色スタート時の塗り替え数を検証
      ans1++;
    } else {
      // 白色スタート時の塗り替え数を検証
      ans2++;
    }
  }

  printf("%d", min(ans1, ans2));
  return 0;
}
Problem p03073 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch[200], temp;
  scanf("%s", &ch);
  int len = strlen(ch), cnt = 0;
  if (len == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < len - 1; i++) {
    if (ch[i] == ch[i + 1]) {
      if (ch[i] == '0') {
        temp = ch[i + 1];
        ch[i + 1] = '1';
        ++cnt;
      } else if (ch[i] == '1') {
        temp = ch[i + 1];
        ch[i + 1] = '0';
        ++cnt;
      }
    }
  }
  printf("%d\n", cnt);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch[100009], temp;
  scanf("%s", &ch);
  int len = strlen(ch), cnt = 0;
  if (len == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < len - 1; i++) {
    if (ch[i] == ch[i + 1]) {
      if (ch[i] == '0') {
        temp = ch[i + 1];
        ch[i + 1] = '1';
        ++cnt;
      } else if (ch[i] == '1') {
        temp = ch[i + 1];
        ch[i + 1] = '0';
        ++cnt;
      }
    }
  }
  printf("%d\n", cnt);
}
Problem p03074 (C++)

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

using namespace std;

int main() {
  int N, K;
  string S;
  cin >> N >> K;
  cin >> S;
  int a[100002];
  int prev = 0;
  int ans = 0;
  a[0] = 1;
  for (int i = 1; i < N + 2; i++) {
    a[i] = 0;
  }
  int cnt = 0;
  for (int i = 1; i < N; i++) {
    if (S[i] == S[i - 1])
      a[cnt]++;
    else {
      cnt++;
      a[cnt]++;
    }
  }
  if (2 * K > cnt) {
    cout << N << endl;
    return 0;
  }
  if (S[0] == '0') {
    for (int i = 0; i < 2 * K; i++)
      ans += a[i];
    for (int i = 1; i < cnt + 1; i += 2) {
      for (int j = i; j < 2 * K + 1 + i; j++)
        prev += a[j];
      if (prev > ans)
        ans = prev;
      prev = 0;
    }
  } else {
    for (int i = 0; i < cnt + 1; i += 2) {
      for (int j = i; j < 2 * K + 1 + i; j++)
        prev += a[j];
      if (prev > ans)
        ans = prev;
      prev = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int main() {
  int N, K;
  string S;
  cin >> N >> K;
  cin >> S;
  int a[200000];
  int prev = 0;
  int ans = 0;
  a[0] = 1;
  for (int i = 1; i < N + 2; i++) {
    a[i] = 0;
  }
  int cnt = 0;
  for (int i = 1; i < N; i++) {
    if (S[i] == S[i - 1])
      a[cnt]++;
    else {
      cnt++;
      a[cnt]++;
    }
  }
  if (2 * K > cnt) {
    cout << N << endl;
    return 0;
  }
  if (S[0] == '0') {
    for (int i = 0; i < 2 * K; i++)
      ans += a[i];
    for (int i = 1; i < cnt + 1; i += 2) {
      for (int j = i; j < 2 * K + 1 + i; j++)
        prev += a[j];
      if (prev > ans)
        ans = prev;
      prev = 0;
    }
  } else {
    for (int i = 0; i < cnt + 1; i += 2) {
      for (int j = i; j < 2 * K + 1 + i; j++)
        prev += a[j];
      if (prev > ans)
        ans = prev;
      prev = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <vector>

using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define llrep(i, m, n) for (ll i = m; i < n; ++i)
#define inf 1000000000
#define llinf 1000000000000000000
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define all(v) v.begin(), v.end()

vector<int> v0, v1;

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  if (s[0] == '0') {
    v1.push_back(0);
  }
  int hoge = 1;
  rep(i, 1, n) {
    if (s[i] != s[i - 1]) {
      if (s[i - 1] == '0') {
        v0.push_back(hoge);
      } else {
        v1.push_back(hoge);
      }
      hoge = 1;
    } else {
      hoge++;
    }
  }
  if (s[n - 1] == '0') {
    v1.push_back(0);
    v0.push_back(hoge);
  } else {
    v1.push_back(hoge);
  }
  if (v0.size() == 0) {
    cout << n << endl;
    return 0;
  }
  rep(i, 1, v0.size()) { v0[i] += v0[i - 1]; }
  rep(i, 1, v1.size()) { v1[i] += v1[i - 1]; }
  int ans = 0;
  v0.insert(v0.begin(), 0);
  v1.insert(v1.begin(), 0);
  rep(i, 0, v0.size() - k) {
    ans = max(ans, v1[k + i + 1] - v1[i] + v0[k + i] - v0[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <vector>

using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define llrep(i, m, n) for (ll i = m; i < n; ++i)
#define inf 1000000000
#define llinf 1000000000000000000
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define all(v) v.begin(), v.end()

vector<int> v0, v1;

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  if (s[0] == '0') {
    v1.push_back(0);
  }
  int hoge = 1;
  rep(i, 1, n) {
    if (s[i] != s[i - 1]) {
      if (s[i - 1] == '0') {
        v0.push_back(hoge);
      } else {
        v1.push_back(hoge);
      }
      hoge = 1;
    } else {
      hoge++;
    }
  }
  if (s[n - 1] == '0') {
    v1.push_back(0);
    v0.push_back(hoge);
  } else {
    v1.push_back(hoge);
  }
  if (v0.size() < k) {
    cout << n << endl;
    return 0;
  }
  rep(i, 1, v0.size()) { v0[i] += v0[i - 1]; }
  rep(i, 1, v1.size()) { v1[i] += v1[i - 1]; }
  int ans = 0;
  v0.insert(v0.begin(), 0);
  v1.insert(v1.begin(), 0);
  rep(i, 0, v0.size() - k) {
    ans = max(ans, v1[k + i + 1] - v1[i] + v0[k + i] - v0[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  int n, k;
  string s;
  vector<pair<int, int>> v;
  ifstream cin("testcase_01.in");

  cin >> n >> k;
  cin >> s;

  int res = 0;
  int cnt = 0;
  int front = -1, end = -1;
  bool flag = false;

  for (int i = 0; i < n; i++) {
    if (flag == false) {
      if (s[i] == '1') {
        cnt++;
      } else {
        res = max(res, cnt);
        cnt = 0;
        front = i;
        flag = true;
      }
    } else {
      if (s[i] == '1') {
        end = i - 1;
        v.push_back(make_pair(front, end));
        flag = false;
        cnt = 1;
      }
    }
  }

  if (flag == false) {
    res = max(res, cnt);
  } else {
    v.push_back(make_pair(front, n - 1));
  }

  // cout << ' ' << k << ' '<< v.size() << '\n';

  int start = 0;
  for (int i = 0; i < v.size(); i++) {
    int idx = i + k;
    if (idx < v.size()) {
      // cout << i << ' ' << k << ' ' << idx << '\n';
      int end = v[idx].first - 1;
      res = max(res, end - start + 1);
      start = v[i].second + 1;
    } else if (idx == v.size()) {
      int end = n - 1;
      res = max(res, end - start + 1);
    }
  }

  if (k >= v.size()) {
    res = max(res, n);
  }

  cout << res << '\n';

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  int n, k;
  string s;
  vector<pair<int, int>> v;

  cin >> n >> k;
  cin >> s;

  int res = 0;
  int cnt = 0;
  int front = -1, end = -1;
  bool flag = false;

  for (int i = 0; i < n; i++) {
    if (flag == false) {
      if (s[i] == '1') {
        cnt++;
      } else {
        res = max(res, cnt);
        cnt = 0;
        front = i;
        flag = true;
      }
    } else {
      if (s[i] == '1') {
        end = i - 1;
        v.push_back(make_pair(front, end));
        flag = false;
        cnt = 1;
      }
    }
  }

  if (flag == false) {
    res = max(res, cnt);
  } else {
    v.push_back(make_pair(front, n - 1));
  }

  // cout << ' ' << k << ' '<< v.size() << '\n';

  int start = 0;
  for (int i = 0; i < v.size(); i++) {
    int idx = i + k;
    if (idx < v.size()) {
      // cout << i << ' ' << k << ' ' << idx << '\n';
      int end = v[idx].first - 1;
      res = max(res, end - start + 1);
      start = v[i].second + 1;
    } else if (idx == v.size()) {
      int end = n - 1;
      res = max(res, end - start + 1);
    }
  }

  if (k >= v.size()) {
    res = max(res, n);
  }

  cout << res << '\n';

  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
// #define rep(i,a,b) for(int i=a ; i<b ; i++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> v;
  if (s[0] == '0')
    v.pb(0);
  for (int i = 0; i < n;) {
    int j = i;
    while (s[i] == s[j] && j < n) {
      j++;
    }
    v.pb(j - i);
    i = j;
  }
  if (s.back() == '0')
    v.pb(0);
  vector<int> sums((int)v.size() + 1, 0);
  for (int i = 0; i < sums.size(); i++) {
    sums[i + 1] = sums[i] + v[i];
  }
  int res = 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;
    res = max(res, sums[right] - sums[left]);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
// #define rep(i,a,b) for(int i=a ; i<b ; i++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> v;
  if (s[0] == '0')
    v.pb(0);
  for (int i = 0; i < n;) {
    int j = i;
    while (s[i] == s[j] && j < n) {
      j++;
    }
    v.pb(j - i);
    i = j;
  }
  if (s.back() == '0')
    v.pb(0);
  vector<int> sums((int)v.size() + 1, 0);
  for (int i = 0; i < v.size(); i++) {
    sums[i + 1] = sums[i] + v[i];
  }
  int res = 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;
    res = max(res, sums[right] - sums[left]);
  }
  cout << res << endl;
  return 0;
}
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++)

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

  int ans = 0, left_idx = 0, right_idx = 0, count = 0;
  for (int i = 0; i < N - 1; i++) {
    if (i == 0 && S[i] == '0')
      count++;
    else if (S[i] == '1' && S[i + 1] == '0')
      count++;

    if (count > K) {
      right_idx = i;
      break;
    }
  }
  if (right_idx == 0)
    right_idx == N - 1;

  ans = right_idx - left_idx + 1;
  while (right_idx != N - 1) {
    while (!(S[left_idx] == '0' && S[left_idx + 1] == '1')) {
      left_idx++;
      // cout << S[left_idx] << " "<< S[left_idx+1] << " " << (S[left_idx] ==
      // '0' && S[left_idx+1] == '1') << endl;
    }
    left_idx++;

    right_idx++;
    while (!(S[right_idx] == '1' && S[right_idx + 1] == '0') &&
           right_idx != N - 1) {
      right_idx++;
      if (right_idx == N - 1)
        break;
    }
    ans = max(ans, right_idx - left_idx + 1);
  }
  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++)

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

  int ans = 0, left_idx = 0, right_idx = 0, count = 0;
  for (int i = 0; i < N - 1; i++) {
    if (i == 0 && S[i] == '0')
      count++;
    else if (S[i] == '1' && S[i + 1] == '0')
      count++;

    if (count > K) {
      right_idx = i;
      break;
    }
  }
  if (right_idx == 0)
    right_idx = N - 1;

  ans = right_idx - left_idx + 1;
  while (right_idx != N - 1) {
    while (!(S[left_idx] == '0' && S[left_idx + 1] == '1')) {
      left_idx++;
      // cout << S[left_idx] << " "<< S[left_idx+1] << " " << (S[left_idx] ==
      // '0' && S[left_idx+1] == '1') << endl;
    }
    left_idx++;

    right_idx++;
    while (!(S[right_idx] == '1' && S[right_idx + 1] == '0') &&
           right_idx != N - 1) {
      right_idx++;
      if (right_idx == N - 1)
        break;
    }
    ans = max(ans, right_idx - left_idx + 1);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

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

typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;

int n, k;
string s;

int main() {
  cin >> n >> k >> s;
  bool is_zero = false;
  int cnt = 0;
  int start = 0, i = 0;
  int mx = 0;
  while (i < n) {
    while (true) {
      if (s[i] == '0') {
        if (!is_zero)
          cnt++;
        is_zero = true;
      } else {
        is_zero = false;
      }

      // cout << "cnt" << cnt << endl;
      if (cnt > k)
        break;
      i++;
      if (i == n)
        break;
    }

    mx = max(mx, i - start);

    bool flg = false;
    while (true) {
      if (s[start] == '0') {
        flg = true;
      } else {
        if (flg)
          break;
      }
      start++;
    }
    cnt--;
    if (start == i)
      break;
  }
  cout << mx << endl;
}

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

typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;

int n, k;
string s;

int main() {
  cin >> n >> k >> s;
  bool is_zero = false;
  int cnt = 0;
  int start = 0, i = 0;
  int mx = 0;
  while (i < n) {
    while (true) {
      if (s[i] == '0') {
        if (!is_zero)
          cnt++;
        is_zero = true;
      } else {
        is_zero = false;
      }

      // cout << "cnt" << cnt << endl;
      if (cnt > k)
        break;
      i++;
      if (i == n)
        break;
    }

    mx = max(mx, i - start);

    bool flg = false;
    while (true) {
      if (s[start] == '0') {
        flg = true;
      } else {
        if (flg)
          break;
      }
      start++;
      if (start == i)
        break;
    }
    cnt--;
    if (start == i)
      break;
  }
  cout << mx << endl;
}
Problem p03074 (C++)

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

int main(void) {
  int number, manipulate_number;
  cin >> number >> manipulate_number;

  string s;
  cin >> s;

  vector<int> nums;
  int now = 1;
  int cnt = 0;
  for (int i = 0; i < number; i++) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      nums.emplace_back(cnt);
      now ^= 1;
      cnt = 1;
    }
  }
  if (cnt != 0)
    nums.emplace_back(cnt);

  if (nums.size() % 2 == 0)
    nums.emplace_back(0);

  int add_range = 2 * manipulate_number + 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_range, (int)nums.size());
    int tmp = sum[right] - sum[left];

    ans = max(tmp, ans);
  }
  cout << ans << endl;
  return 0;
}

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

int main(void) {
  int number, manipulate_number;
  cin >> number >> manipulate_number;

  string s;
  cin >> s;

  vector<int> nums;
  int now = 1;
  int cnt = 0;
  for (int i = 0; i < number; i++) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      nums.emplace_back(cnt);
      now ^= 1;
      cnt = 1;
    }
  }
  if (cnt != 0)
    nums.emplace_back(cnt);

  if (nums.size() % 2 == 0)
    nums.emplace_back(0);

  int add_range = 2 * manipulate_number + 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_range, (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 <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)
typedef long long ll;
int main() {
  int n, k;
  string s;
  cin >> n >> k >> s;

  vector<int> V;

  int cnt = 1;

  rep(i, n - 1) {
    if (s[i + 1] == s[i]) {
      cnt++;
    } else {
      V.push_back(cnt);
      cnt = 1;
    }
  }
  V.push_back(cnt);
  // rep(i, (int)V.size()) {
  // 	cout << V[i] << " ";
  // }

  int ma = 0;
  if (s[0] == '1') {
    for (int beg = 0; beg < (int)V.size(); beg += 2) {
      int end = min((int)V.size() - 1, beg + 2 * k);
      int tem = 0;
      for (int i = beg; i <= end; i++) {
        tem += V[i];
      }
      ma = max(tem, ma);
    }
  } else {
    for (int i = 0; i <= 2 * k - 1; i++) {
      ma += V[i];
    }

    for (int beg = 1; beg < (int)V.size(); beg += 2) {
      int end = min((int)V.size() - 1, beg + 2 * k);
      int tem = 0;
      for (int i = beg; i <= end; i++) {
        tem += V[i];
      }
      ma = max(tem, ma);
    }
  }

  cout << ma << endl;
}


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)
typedef long long ll;
int main() {
  int n, k;
  string s;
  cin >> n >> k >> s;

  vector<int> V;

  int cnt = 1;

  rep(i, n - 1) {
    if (s[i + 1] == s[i]) {
      cnt++;
    } else {
      V.push_back(cnt);
      cnt = 1;
    }
  }
  V.push_back(cnt);
  // rep(i, (int)V.size()) {
  // 	cout << V[i] << " ";
  // }

  int ma = 0;
  if (s[0] == '1') {
    for (int beg = 0; beg < (int)V.size(); beg += 2) {
      int end = min((int)V.size() - 1, beg + 2 * k);
      int tem = 0;
      for (int i = beg; i <= end; i++) {
        tem += V[i];
      }
      ma = max(tem, ma);
    }
  } else {
    int en = min((int)V.size() - 1, 2 * k - 1);
    for (int i = 0; i <= en; i++) {
      ma += V[i];
    }

    for (int beg = 1; beg < (int)V.size(); beg += 2) {
      int end = min((int)V.size() - 1, beg + 2 * k);
      int tem = 0;
      for (int i = beg; i <= end; i++) {
        tem += V[i];
      }
      ma = max(tem, ma);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
string s;
int n, k;
bool f[10111];
int a[10111], b[10111];
int main() {
  cin >> n >> k;
  cin >> s;
  int ans = 0, ans1 = 0, tt = 1, ttt = 1, zero = 0, one = 0, cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      ans++;
    else {
      if (ans != 0) {
        b[tt++] = ans;
        one += ans;
        ans = 0;
      }
    }
    if (i == n - 1) {
      if (ans != 0) {
        b[tt++] = ans;
        one += ans;
        ans = 0;
      }
    }
    if (s[i] == '0')
      ans1++;
    else {
      if (ans1 != 0) {
        a[ttt++] = ans1;
        zero += ans1;
        ans1 = 0;
      }
    }
    if (i == n - 1) {
      if (ans1 != 0) {
        a[ttt++] = ans1;
        zero += ans1;
        ans1 = 0;
      }
    }
  }
  // for (int i=1;i<=tt;i++) cout<<b[i]<<endl;
  if (k == 0) {
    for (int i = 1; i <= tt - 1; i++)
      if (b[i] > cnt)
        cnt = b[i];
  } else if (k >= zero) {
    cnt = s.size();
  } else if (k > ttt - 1)
    cnt = s.size();
  else {
    int i = 1;
    while (i + k - 1 <= ttt - 1) {
      int q = 0;
      if (s[0] == '1') {
        for (int j = i; j <= i + k - 1; j++)
          q += b[j] + a[j];
        q += b[i + k];
      } else {
        for (int j = i; j <= i + k - 1; j++)
          q += b[j] + a[j];
        q += b[i - 1];
      }
      // cout<<q<<endl;
      if (q > cnt)
        cnt = q;
      i++;
    }
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
string s;
int n, k;
bool f[201011];
int a[201101], b[201101];
int main() {
  cin >> n >> k;
  cin >> s;
  int ans = 0, ans1 = 0, tt = 1, ttt = 1, zero = 0, one = 0, cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      ans++;
    else {
      if (ans != 0) {
        b[tt++] = ans;
        one += ans;
        ans = 0;
      }
    }
    if (i == n - 1) {
      if (ans != 0) {
        b[tt++] = ans;
        one += ans;
        ans = 0;
      }
    }
    if (s[i] == '0')
      ans1++;
    else {
      if (ans1 != 0) {
        a[ttt++] = ans1;
        zero += ans1;
        ans1 = 0;
      }
    }
    if (i == n - 1) {
      if (ans1 != 0) {
        a[ttt++] = ans1;
        zero += ans1;
        ans1 = 0;
      }
    }
  }
  // for (int i=1;i<=tt;i++) cout<<b[i]<<endl;
  if (k == 0) {
    for (int i = 1; i <= tt - 1; i++)
      if (b[i] > cnt)
        cnt = b[i];
  } else if (k >= zero) {
    cnt = s.size();
  } else if (k > ttt - 1)
    cnt = s.size();
  else {
    int i = 1;
    while (i + k - 1 <= ttt - 1) {
      int q = 0;
      if (s[0] == '1') {
        for (int j = i; j <= i + k - 1; j++)
          q += b[j] + a[j];
        q += b[i + k];
      } else {
        for (int j = i; j <= i + k - 1; j++)
          q += b[j] + a[j];
        q += b[i - 1];
      }
      // cout<<q<<endl;
      if (q > cnt)
        cnt = q;
      i++;
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> a;
  rep(i, n) {
    if (i == 0) {
      if (s[i] == '0')
        a.push_back(0);
      a.push_back(1);
    } else {
      if (s[i - 1] == s[i])
        a[a.size() - 1]++;
      else
        a.push_back(1);
    }
    if (i == n - 1 && s[i] == '0')
      a.push_back(0);
  }
  int m = a.size();
  int ans = 0;
  if (m <= 2 * k + 1)
    ans = n;
  else {
    vector<vector<int>> asum(2 * k + 1, vector<int>(m, 0));
    asum[0] = a;
    for (int i = 1; i < 2 * k + 1; i++) {
      rep(j, m) {
        if (i + j >= m)
          continue;
        asum[i][j] = asum[i - 1][j] + a[j + i];
      }
    }
    for (int i = 0; i < m - 2 * k; i += 2) {
      ans = max(ans, asum[2 * k][i]);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> a;
  rep(i, n) {
    if (i == 0) {
      if (s[i] == '0')
        a.push_back(0);
      a.push_back(1);
    } else {
      if (s[i - 1] == s[i])
        a[a.size() - 1]++;
      else
        a.push_back(1);
    }
    if (i == n - 1 && s[i] == '0')
      a.push_back(0);
  }
  int m = a.size();
  int ans = 0;
  if (m <= 2 * k + 1)
    ans = n;
  int b = 0;
  rep(i, m) {
    b += a[i];
    if (i - 2 * k - 1 >= 0)
      b -= a[i - 2 * k - 1];
    if (i % 2 == 0)
      ans = max(ans, b);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vl a(n + 5, 0);
  ll j = 0;
  if (s[0] == '0') {
    j++;
  }
  a[j]++;
  FOR(i, 1, n) {
    if (s[i] == s[i - 1]) {
      a[j]++;
    } else {
      j++;
      a[j]++;
    }
  }
  ll t = 0;
  rep(i, 2 * k + 1) { t += a[i]; }
  ll ans = t;
  for (ll i = 0; 2 * k + i < n; i += 2) {
    t -= a[i] + a[i + 1];
    t += a[2 * k + 1 + i] + a[2 * k + 2 + i];
    ans = max(ans, t);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vl a(n + 2 * k + 5, 0);
  ll j = 0;
  if (s[0] == '0') {
    j++;
  }
  a[j]++;
  FOR(i, 1, n) {
    if (s[i] == s[i - 1]) {
      a[j]++;
    } else {
      j++;
      a[j]++;
    }
  }
  ll t = 0;
  rep(i, 2 * k + 1) { t += a[i]; }
  ll ans = t;
  for (ll i = 0; 2 * k + i < n; i += 2) {
    t -= a[i] + a[i + 1];
    t += a[2 * k + 1 + i] + a[2 * k + 2 + i];
    ans = max(ans, t);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

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

vector<ll> sum;
ll i;
void pre_process(vector<ll> &a, vector<ll> &s) { // 累積和のvector
  ll n = (ll)a.size();
  s.assign(n + 1, 0);
  for (i = 0; i < n; i++) {
    s[i + 1] = s[i] + a[i];
  }
}
ll query(ll i, ll j, vector<ll> &s) { // 区間[i,j]の和
  return s[j] - s[i];
}

int main() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<ll> a;

  if (n == 1) {
    cout << "1" << endl;
    return 0;
  }

  vector<ll> Nums;
  int now = 1; // 今見ている数
  int cnt = 0; // nowがいくつ並んでいるか
  for (i = 0; i < n; i++) {
    if (s[i] == (char)('0' + now)) {
      cnt++;
    } else {
      Nums.push_back(cnt); // 最初に1がなければ0が追加される
      now = 1 - now;       // 0 1の交換
      cnt = 1;             // 新しいのをカウント
    }
  }
  if (cnt != 0) {
    Nums.push_back(cnt);
  }

  // 0で終わっていたらたす
  if (Nums.size() % 2 == 0) {
    Nums.push_back(0);
  }
  // 1で始まり1で終わる

  pre_process(Nums, sum);

  ll add = 2 * k + 1;
  ll ans = -1;
  for (i = 1; i <= sum.size() - add; i += 2) {
    if (i + add - 1 > sum.size()) {
      cout << n << endl;
    }
    ans = max(ans, query(i - 1, i + add - 1, sum));
  }

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

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

vector<ll> sum;
ll i;
void pre_process(vector<ll> &a, vector<ll> &s) { // 累積和のvector
  ll n = (ll)a.size();
  s.assign(n + 1, 0);
  for (i = 0; i < n; i++) {
    s[i + 1] = s[i] + a[i];
  }
}
ll query(ll i, ll j, vector<ll> &s) { // 区間[i,j]の和
  return s[j] - s[i];
}

int main() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<ll> a;

  if (n == 1) {
    cout << "1" << endl;
    return 0;
  }

  vector<ll> Nums;
  int now = 1; // 今見ている数
  int cnt = 0; // nowがいくつ並んでいるか
  for (i = 0; i < n; i++) {
    if (s[i] == (char)('0' + now)) {
      cnt++;
    } else {
      Nums.push_back(cnt); // 最初に1がなければ0が追加される
      now = 1 - now;       // 0 1の交換
      cnt = 1;             // 新しいのをカウント
    }
  }
  if (cnt != 0) {
    Nums.push_back(cnt);
  }

  // 0で終わっていたらたす
  if (Nums.size() % 2 == 0) {
    Nums.push_back(0);
  }
  // 1で始まり1で終わる

  pre_process(Nums, sum);

  ll add = 2 * k + 1;
  ll ans = -1;
  for (i = 1; i <= sum.size() - add; i += 2) {
    if (i + add - 1 > sum.size()) {
      cout << n << endl;
      return 0;
    }
    ans = max(ans, query(i - 1, i + add - 1, sum));
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, k;
char a[N];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int maxx = 0;
  for (int i = 1; i <= n; i++) {
    int o = k, ans = 0, j = i;
    while (o > 0 && j <= n) {
      while (a[j] == '1' && j <= n)
        ans++, j++;
      o--;
      while (a[j] == '0' && j <= n)
        ans++, j++;
    }
    while (a[j] == '1' && j <= n)
      ans++, j++;
    maxx = max(maxx, ans);
  }
  cout << maxx;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, k;
char a[N];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int maxx = 0, h = 1, t = 1, ans = 0;
  for (int i = 1; i <= k; i++) {
    for (; a[t] == '1'; t++)
      ans++;
    for (; a[t] == '0'; t++)
      ans++;
  }
  for (; a[t] == '1'; t++)
    ans++;
  maxx = ans;
  for (; a[h] == '1'; h++)
    ans--;
  for (; a[t] == '1'; t++)
    ans++;
  maxx = max(maxx, ans);
  while (h <= t && h <= n) {
    for (; a[h] == '0'; h++)
      ans--;
    for (; a[t] == '0' && t <= n; t++)
      ans++;
    for (; a[t] == '1' && t <= n; t++)
      ans++;
    maxx = max(ans, maxx);
    for (; a[h] == '1'; h++)
      ans--;
    for (; a[t] == '1' && t <= n; t++)
      ans++;
    maxx = max(maxx, ans);
  }
  cout << maxx;
  return 0;
}
Problem p03074 (C++)

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

using namespace std;

int main() {

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

  vector<int> Nums;
  int cnt = 0;
  int now = 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now;
      cnt = 1;
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  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;
  int temp = 0;
  for (int i = 0; i < Nums.size(); i += 2) {

    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int temp = sum[right] - sum[left];
    ans = max(ans, temp);
  }

  cout << ans << '\n';
}


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

using namespace std;

int main() {

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

  vector<int> Nums;
  int cnt = 0;
  int now = 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now;
      cnt = 1;
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  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;
  int temp = 0;
  for (int i = 0; i < Nums.size(); i += 2) {

    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int temp = sum[right] - sum[left];
    ans = max(ans, temp);
  }

  cout << ans << '\n';
}
Problem p03074 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
#endif

  vector<int> zero, one;
  vector<pii> v;
  int n, k;
  string s;
  cin >> n >> k >> s;

  if (n == 1) {
    cout << 1 << '\n';
    return 0;
  }

  int cnt = 1;
  if (s[0] == '0')
    one.push_back(0);

  for (int i = 1; i < s.length(); i++) {
    if (s[i] != s[i - 1]) {
      if (s[i - 1] == '0')
        zero.push_back(cnt);
      else
        one.push_back(cnt);
      cnt = 0;
    }
    cnt++;
  }

  if (s[s.length() - 1] == '0') {
    one.push_back(0);
    zero.push_back(cnt);
  } else
    one.push_back(cnt);

  for (int i = 0; i < zero.size(); i++) {
    v.push_back(pii(zero[i] + one[i] + one[i + 1], zero[i] + one[i + 1]));
  }

  k = min(k, (int)zero.size());

  int m = -1;
  for (int i = 0; i < v.size() - k + 1; i++) {
    int tmp = v[i].first;
    for (int j = i + 1; j < i + k; j++) {
      tmp += v[j].second;
    }
    m = max(m, tmp);
  }

  cout << m << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
#endif

  vector<int> zero, one;
  vector<pii> v;
  int n, k;
  string s;
  cin >> n >> k >> s;

  if (n == 1) {
    cout << 1 << '\n';
    return 0;
  }

  int cnt = 1;
  if (s[0] == '0')
    one.push_back(0);

  for (int i = 1; i < s.length(); i++) {
    if (s[i] != s[i - 1]) {
      if (s[i - 1] == '0')
        zero.push_back(cnt);
      else
        one.push_back(cnt);
      cnt = 0;
    }
    cnt++;
  }

  if (s[s.length() - 1] == '0') {
    one.push_back(0);
    zero.push_back(cnt);
  } else
    one.push_back(cnt);

  for (int i = 0; i < zero.size(); i++) {
    v.push_back(pii(zero[i] + one[i] + one[i + 1], zero[i] + one[i + 1]));
  }

  k = min(k, (int)v.size());

  if (v.size() == 0) {
    cout << n << '\n';
    return 0;
  }

  int m = -1;
  for (int i = 0; i < v.size() - k + 1; i++) {
    int tmp = v[i].first;
    for (int j = i + 1; j < i + k; j++) {
      tmp += v[j].second;
    }
    m = max(m, tmp);
  }

  cout << m << '\n';

  return 0;
}
Problem p03074 (C++)

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("../../a.in", "r", stdin);
  freopen("../../a.out", "w", stdout);
#endif

  int n, k, ct = 1;
  vector<int> v;
  cin >> n >> k;
  string s;
  cin >> s;

  for (int i = 1; i < n; i++) {
    if (s[i] != s[i - 1]) {
      v.push_back(ct);
      ct = 1;
    } else
      ++ct;
  }
  v.push_back(ct);

  if (s[0] == '0') {
    long long mx = 0;
    for (int i = 0; i < v.size(); i += 2) {
      long long sum = 0;
      for (int j = i, nl = 0; nl < k && j < v.size(); j += 2, nl++) {
        if (j == i && j - 1 >= 0)
          sum += v[j - 1];
        if (j + 1 < v.size())
          sum += v[j] + v[j + 1];
        else
          sum += v[j];
      }
      mx = max(mx, sum);
    }
    cout << mx << "\n";

  } else {
    long long mx = 0;
    for (int i = 1; i < v.size(); i += 2) {
      long long sum = 0;
      for (int j = i, nl = 0; nl < k && j < v.size(); j += 2, nl++) {
        if (j == i && j - 1 >= 0)
          sum += v[j - 1];
        if (j + 1 < v.size())
          sum += v[j] + v[j + 1];
        else
          sum += v[j];
      }
      mx = max(mx, sum);
    }

    if (mx == 0)
      mx = v.front();

    cout << mx << "\n";
  }
}

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    #ifndef ONLINE_JUDGE
  // 	freopen("../../a.in","r",stdin);
  // 	freopen("../../a.out","w",stdout);
  // #endif

  int n, k, ct = 1;
  vector<int> v;
  cin >> n >> k;
  string s;
  cin >> s;

  for (int i = 1; i < n; i++) {
    if (s[i] != s[i - 1]) {
      v.push_back(ct);
      ct = 1;
    } else
      ++ct;
  }
  v.push_back(ct);

  if (s[0] == '0') {
    long long mx = 0;
    for (int i = 0; i < v.size(); i += 2) {
      long long sum = 0;
      for (int j = i, nl = 0; nl < k && j < v.size(); j += 2, nl++) {
        if (j == i && j - 1 >= 0)
          sum += v[j - 1];
        if (j + 1 < v.size())
          sum += v[j] + v[j + 1];
        else
          sum += v[j];
      }
      mx = max(mx, sum);
    }
    cout << mx << "\n";

  } else {
    long long mx = 0;
    for (int i = 1; i < v.size(); i += 2) {
      long long sum = 0;
      for (int j = i, nl = 0; nl < k && j < v.size(); j += 2, nl++) {
        if (j == i && j - 1 >= 0)
          sum += v[j - 1];
        if (j + 1 < v.size())
          sum += v[j] + v[j + 1];
        else
          sum += v[j];
      }
      mx = max(mx, sum);
    }

    if (mx == 0)
      mx = v.front();

    cout << mx << "\n";
  }
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (i = 0; i < n; i++)
#define P pair<int, int>

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

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

  int ans = 0;
  int Add = 2 * k + 1;

  int left = 0;
  int right = 0;
  int tmp = 0; // [left, right)のsum
  for (int i = 0; i < Nums.size(); i += 2) {
    int Nextleft = i;
    int Nextright = min(i + Add, n);

    while (Nextleft > left) {
      tmp -= Nums[left];
      left++;
    }

    while (Nextright > right) {
      tmp += Nums[right];
      right++;
    }

    ans = max(ans, tmp);
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (i = 0; i < n; i++)
#define P pair<int, int>

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

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

  int ans = 0;
  int Add = 2 * k + 1;

  int left = 0;
  int right = 0;
  int tmp = 0; // [left, right)のsum
  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;
}
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;
  for (int i = 0; i < lines.size(); i += 2) {
    int tmp = 0;
    int left = i;
    int right = i + add;
    for (int j = left; j < right; j++) {
      tmp += lines[j];
    }
    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;
  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);
  }
  cout << ans << endl;
  return 0;
}
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 = 1e9 + 7;

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

  string S;
  cin >> S;

  vector<int> nums;
  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);
      now = 1 - now;
      cnt = 1;
    }
  }
  nums.push_back(cnt);

  if (nums.size() % 2 == 0)
    nums.push_back(0);

  // rep(i, nums.size()) {
  //     cout << nums[i] << endl;
  // }
  // return 0;

  int add = 2 * K + 1;
  int ans = 0;
  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];
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;
}

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 = 1e9 + 7;

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

  string S;
  cin >> S;

  vector<int> nums;
  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);
      now = 1 - now;
      cnt = 1;
    }
  }
  nums.push_back(cnt);

  if (nums.size() % 2 == 0)
    nums.push_back(0);

  // rep(i, nums.size()) {
  //     cout << nums[i] << endl;
  // }
  // return 0;

  int add = 2 * K + 1;
  int ans = 0;
  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];
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;
}