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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

  vector<int> nums;
  int now = 1;
  int cnt = 0;
  for (size_t 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 ans = 0;
  int add = 2 * k + 1;
  for (size_t i = 0; i < nums.size(); i += 2) {
    int tmp = 0;
    int left = i;
    int right = i + add;
    for (size_t j = left; j < right; j++) {
      tmp += nums[j];
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

  vector<int> nums;
  int now = 1;
  int cnt = 0;
  for (size_t 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 ans = 0;
  int add = 2 * k + 1;
  for (size_t i = 0; i < nums.size(); i += 2) {
    int tmp = 0;
    int left = i;
    int right = min(i + add, nums.size());
    for (size_t j = left; j < right; j++) {
      tmp += nums[j];
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

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

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;
    }
  }
  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);
  sum[0] = 0;
  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(ans, tmp);
  }
  cout << ans << endl;
}


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

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;
    }
  }
  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);
  sum[0] = 0;
  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(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

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

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;
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);
  int Add = 2 * K + 1;
  int ans = 0;
  int left = 0;
  int right = 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(ans, tmp);
  }
  cout << ans << endl;
}

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

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;
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);
  int Add = 2 * K + 1;
  int ans = 0;
  int left = 0;
  int right = 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(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
#define inf 1e9

int main() {
  int n, k;
  cin >> n >> k;
  vector<mp> a;
  int p, c = 1;
  string s;
  cin >> s;
  p = s[0] - '0';
  int f = p;
  for (int i = 1; i < n; i++) {
    int b = s[i] - '0';

    if (b != p) {
      a.push_back(mp(c, p));
      c = 0;
    }
    c++;
    p = b;
  }
  a.push_back(mp(c, p));
  //		for(int i=0;i<a.size();i++)cout<<a[i].first<<'
  //'<<a[i].second<<endl;;
  int sum = 0;
  for (int i = 0; i < a.size(); i++) {
    int cnt = 0;
    int res = 0;
    for (int j = 0; i + j < a.size() && cnt <= k; j++) {
      if (a[i + j].second == 0)
        cnt++;
      if (cnt == k + 1)
        break;
      res += a[i + j].first;
    }
    sum = max(sum, res);
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
#define inf 1e9

int main() {
  int n, k;
  cin >> n >> k;
  vector<mp> a;
  int p, c = 1;
  string s;
  cin >> s;
  p = s[0] - '0';
  int f = p;
  for (int i = 1; i < n; i++) {
    int b = s[i] - '0';

    if (b != p) {
      a.push_back(mp(c, p));
      c = 0;
    }
    c++;
    p = b;
  }
  a.push_back(mp(c, p));
  //		for(int i=0;i<a.size();i++)cout<<a[i].first<<'
  //'<<a[i].second<<endl;;
  int sum = 0;
  int cnt = 0;
  int res = 0;
  int last = 0;
  for (int j = 0; j < a.size() && cnt <= k; j++) {
    if (a[j].second == 0)
      cnt++;
    if (cnt == k + 1)
      break;
    res += a[j].first;
    last = j;
  }
  sum = max(sum, res);
  int st = 0;
  if (a[0].second == 0) {
    st++;
    res -= a[0].first;
    if (last + 1 < a.size()) {
      res += a[last + 1].first;
      last++;
    }
    if (last + 1 < a.size()) {
      res += a[last + 1].first;
      last++;
    }
  }
  sum = max(sum, res);
  for (int i = st; i < a.size() && last < a.size(); i += 2) {
    res -= a[i].first;
    res -= a[i + 1].first;
    if (last + 1 < a.size()) {
      res += a[last + 1].first;
      last++;
    }
    if (last + 1 < a.size()) {
      res += a[last + 1].first;
      last++;
    }
    sum = max(sum, res);
  }
  cout << sum << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int n, k, i;
  int ans = 0;
  std::cin >> n >> k;
  string s;
  std::cin >> s;
  int num = 0;
  int a[n];
  if (s[0] == '0') {
    a[0] = 0;
    num++;
  }
  for (i = 1; i < n; i++) {
    if (s[i - 1] == '0' && s[i] == '1') {
      a[num] = i;
      num++;
    }
    if (s[i - 1] == '1' && s[i] == '0') {
      a[num] = i;
      num++;
    }
  }
  if (s[n - 1] == '1') {
    a[num] = n;
    num++;
  }
  if (s[n - 1] == '0') {
    a[num] = n;
    num++;
    a[num] = n;
    num++;
  }
  // for (i=0;i<num;i++){std::cout << a[i] << std::endl;}
  if (num - 1 <= 2 * k)
    std::cout << n << std::endl;
  else {
    for (i = 0; i < num - 2 * k - 1; i++) {
      if (i == 0)
        ans = a[2 * k];
      else {
        if (a[2 * (k + i)] - a[2 * i - 1] > ans)
          ans = a[2 * (k + i)] - a[2 * i - 1];
      }
    }
    std::cout << ans << std::endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int n, k, i;
  int ans = 0;
  std::cin >> n >> k;
  string s;
  std::cin >> s;
  int num = 0;
  int a[n];
  if (s[0] == '0') {
    a[0] = 0;
    num++;
  }
  for (i = 1; i < n; i++) {
    if (s[i - 1] == '0' && s[i] == '1') {
      a[num] = i;
      num++;
    }
    if (s[i - 1] == '1' && s[i] == '0') {
      a[num] = i;
      num++;
    }
  }
  if (s[n - 1] == '1') {
    a[num] = n;
    num++;
  }
  if (s[n - 1] == '0') {
    a[num] = n;
    num++;
    a[num] = n;
    num++;
  }
  // for (i=0;i<num;i++){std::cout << a[i] << std::endl;}
  if (num - 1 <= 2 * k)
    std::cout << n << std::endl;
  else {
    for (i = 0; i < (num - 2 * k + 1) / 2; i++) {
      if (i == 0)
        ans = a[2 * k];
      else {
        if (a[2 * (k + i)] - a[2 * i - 1] > ans)
          ans = a[2 * (k + i)] - a[2 * i - 1];
      }
    }
    std::cout << ans << std::endl;
  }
}
Problem p03074 (C++)

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

using namespace std;

int main() {
  int N, K;
  string S;
  vector<int> v;
  cin >> N >> K >> S;
  char now = '1';
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (now == S[i]) {
      cnt++;
    } else {
      v.push_back(cnt);
      cnt = 1;
      now = S[i];
    }
  }
  if (cnt != 0)
    v.push_back(cnt);
  if (now == '0')
    v.push_back(0);

  int ans = 0;
  if (2 * K + 1 >= v.size()) {
    ans = N;
  } else {
    vector<int> sum(v.size());
    sum[0] = v[0];
    for (int i = 1; i < v.size(); i++) {
      sum[i] = sum[i - 1] + v[i];
    }
    for (int i = 0; 2 * K - i + 1 < v.size(); i += 2) {
      if (i == 0) {
        ans = max(ans, sum[2 * K + i]);
      } else {
        ans = max(ans, sum[2 * K + i] - sum[i - 1]);
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <string.h>
#include <vector>

using namespace std;

int main() {
  int N, K;
  string S;
  vector<int> v;
  cin >> N >> K >> S;
  char now = '1';
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (now == S[i]) {
      cnt++;
    } else {
      v.push_back(cnt);
      cnt = 1;
      now = S[i];
    }
  }
  if (cnt != 0)
    v.push_back(cnt);
  if (now == '0')
    v.push_back(0);

  int ans = 0;
  if (2 * K + 1 >= v.size()) {
    ans = N;
  } else {
    vector<int> sum(v.size());
    sum[0] = v[0];
    for (int i = 1; i < v.size(); i++) {
      sum[i] = sum[i - 1] + v[i];
    }
    for (int i = 0; 2 * K + i < v.size(); i += 2) {
      if (i == 0) {
        ans = max(ans, sum[2 * K + i]);
      } else {
        ans = max(ans, sum[2 * K + i] - sum[i - 1]);
      }
    }
  }
  cout << ans << endl;
}
Problem p03074 (C++)

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

#define reps(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reps(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define PI (acos(-1))

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

  vector<int> Nums;
  int now = 1; // いま見ている数
  int cnt = 0; // nowがいくつ並ぶか
  rep(i, n) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now; // 1と0切り替え
      cnt = 1;
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  // 連続する1の数-連続する0の数-…ってかんじの配列
  // 1-0-1-0-…-0-1のように1でおわりにする
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  int ans = 0;
  for (int i = 0; i < Nums.size(); i += 2) {
    int tmp = 0;

    int left = i;
    int right = i + 2 * k + 1;
    reps(j, left, right) { tmp += Nums[j]; }
    ans = max(tmp, ans);
  }

  cout << ans << endl;
}

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

#define reps(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reps(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define PI (acos(-1))

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

  vector<int> Nums;
  int now = 1; // いま見ている数
  int cnt = 0; // nowがいくつ並ぶか
  rep(i, n) {
    if (s[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now; // 1と0切り替え
      cnt = 1;
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);
  // 連続する1の数-連続する0の数-…ってかんじの配列
  // 1-0-1-0-…-0-1のように1でおわりにする
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  int ans = 0;
  for (int i = 0; i < Nums.size(); i += 2) {
    int tmp = 0;

    int left = i;
    int right = min(i + 2 * k + 1, (int)Nums.size());
    reps(j, left, right) { tmp += Nums[j]; }
    ans = max(tmp, ans);
  }

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

Buggy submission (status: Runtime Error):
// g++ -std=c++14 test.cpp -o test.out
#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

int N, K;
string S;

int ans = 0;

void solve() {
  vector<int>
      segment; // segmentには　1の数、0の数、1の数、0の数......1の数　と記録する
  if (S[0] == '0')
    segment.push_back(0);
  int left_index = 0;
  for (int right_index = 0; right_index < N;) {
    while (right_index < N) {
      if (S[left_index] == S[right_index])
        right_index++;
      else
        break;
    }
    segment.push_back(right_index - left_index);
    left_index = right_index;
  }
  if (S[N - 1] == '0')
    segment.push_back(0);

  // for(int i=0;i<segment.size();i++){
  // 	cout << segment[i] << " ";
  // }
  // cout << endl;

  int sumof1 = segment[0];
  for (int i = 1; i <= K; i++) {
    if (2 * i - 1 >= N) {
      ans = sumof1;
      return;
    }
    sumof1 += (segment[2 * i - 1] + segment[2 * i]);
  }
  ans = sumof1;
  for (int i = 2 * K + 1; i < segment.size(); i += 2) {
    sumof1 += (segment[i] + segment[i + 1]);
    sumof1 -= (segment[i - (2 * K + 1)] + segment[i + 1 - (2 * K + 1)]);
    if (ans < sumof1)
      ans = sumof1;
  }
}

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

  solve();

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


Fixed submission:
// g++ -std=c++14 test.cpp -o test.out
#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

int N, K;
string S;

int ans = 0;

void solve() {
  vector<int>
      segment; // segmentには　1の数、0の数、1の数、0の数......1の数　と記録する
  if (S[0] == '0')
    segment.push_back(0);
  int left_index = 0;
  for (int right_index = 0; right_index < N;) {
    while (right_index < N) {
      if (S[left_index] == S[right_index])
        right_index++;
      else
        break;
    }
    segment.push_back(right_index - left_index);
    left_index = right_index;
  }
  if (S[N - 1] == '0')
    segment.push_back(0);

  // for(int i=0;i<segment.size();i++){
  // 	cout << segment[i] << " ";
  // }
  // cout << endl;

  int sumof1 = segment[0];
  for (int i = 1; i <= K; i++) {
    if (2 * i - 1 >= segment.size()) {
      ans = sumof1;
      return;
    }
    sumof1 += (segment[2 * i - 1] + segment[2 * i]);
  }
  ans = sumof1;
  for (int i = 2 * K + 1; i < segment.size(); i += 2) {
    sumof1 += (segment[i] + segment[i + 1]);
    sumof1 -= (segment[i - (2 * K + 1)] + segment[i + 1 - (2 * K + 1)]);
    if (ans < sumof1)
      ans = sumof1;
  }
}

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

  solve();

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

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

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  char prev = '1';
  vector<int> a;
  int ctr = 0;
  rep(i, s.size()) {
    switch (s[i]) {
    case '0':
      if (prev == '1') {
        a.push_back(ctr);
        ctr = 0;
      }
      prev = '0';
      ctr++;
      break;
    case '1':
      if (prev == '0') {
        a.push_back(ctr);
        ctr = 0;
      }
      ctr++;
      prev = '1';
      break;
    }
  }
  a.push_back(ctr);
  if (s[s.size() - 1] == '0') {
    a.push_back(0);
  }
  vector<int> b(a.size() + 1);
  b[0] = 0;
  rep(i, s.size()) { b[i + 1] = b[i] + a[i]; }
  if (k * 2 + 1 >= b.size()) {
    cout << n << endl;
    exit(0);
  }
  int ans = 0;
  rep(i, 2 * k + 1, b.size()) {
    int p = b[i] - b[i - (2 * k + 1)];
    chmax(ans, p);
    i++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  char prev = '1';
  vector<int> a;
  int ctr = 0;
  rep(i, s.size()) {
    switch (s[i]) {
    case '0':
      if (prev == '1') {
        a.push_back(ctr);
        ctr = 0;
      }
      prev = '0';
      ctr++;
      break;
    case '1':
      if (prev == '0') {
        a.push_back(ctr);
        ctr = 0;
      }
      ctr++;
      prev = '1';
      break;
    }
  }
  a.push_back(ctr);
  if (s[s.size() - 1] == '0') {
    a.push_back(0);
  }
  vector<int> b(a.size() + 1);
  b[0] = 0;
  rep(i, a.size()) { b[i + 1] = b[i] + a[i]; }
  if (k * 2 + 1 >= b.size()) {
    cout << n << endl;
    exit(0);
  }
  int ans = 0;
  rep(i, 2 * k + 1, b.size()) {
    int p = b[i] - b[i - (2 * k + 1)];
    chmax(ans, p);
    i++;
  }
  cout << ans << endl;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <vector>
#define FORi(N) for (int i = 0; i < N; ++i)
#define FORj(N) for (int j = 0; j < N; ++j)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ll gcd(ll m, ll n) {
  if (n == 0)
    return abs(m);
  return (gcd(n, m % n));
}
void putYN(bool b) {
  if (b) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> _01 = {0, 0}; // 累積和
  string S;
  cin >> S;
  bool flag = false;
  ll n = 0;
  // if(S[0]=='1')_01.push_back(0);
  FORi(S.size()) {
    bool b = S[i] == '0';
    if (flag)
      b = !b;
    if (!b) {
      flag = !flag;
      _01.push_back(i);
    }
  }
  _01.push_back(S.size());
  _01.push_back(S.size());
  // 最後が0と仮定というか
  if (_01.size() % 2 == 0)
    _01.push_back(S.size());
  ll ma = 0;
  // ll tsize= K*2+1;
  // if(tsize>_01.size()/2)tsize=_01.size()/2;
  // cout<<_01.size()/2-tsize+1;
  for (int i = 1; i < _01.size() - K * 2; i += 2) {
    // cout<<"a"<<i;
    ll tmp = _01[i + K * 2] - _01[i - 1];
    if (ma < tmp)
      ma = tmp;
  }
  for (auto a : _01) {
    // cout<<a<<endl;
  }
  cout << ma << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <vector>
#define FORi(N) for (int i = 0; i < N; ++i)
#define FORj(N) for (int j = 0; j < N; ++j)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ll gcd(ll m, ll n) {
  if (n == 0)
    return abs(m);
  return (gcd(n, m % n));
}
void putYN(bool b) {
  if (b) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> _01 = {0, 0}; // 累積和
  string S;
  cin >> S;
  bool flag = false;
  ll n = 0;
  // if(S[0]=='1')_01.push_back(0);
  FORi(S.size()) {
    bool b = S[i] == '0';
    if (flag)
      b = !b;
    if (!b) {
      flag = !flag;
      _01.push_back(i);
    }
  }
  _01.push_back(S.size());
  _01.push_back(S.size());
  // 最後が0と仮定というか
  if (_01.size() % 2 == 0)
    _01.push_back(S.size());
  ll ma = 0;
  // ll tsize= K*2+1;
  // if(tsize>_01.size()/2)tsize=_01.size()/2;
  // cout<<-tsize+1;
  if (K * 2 >= _01.size())
    K = _01.size() / 2 - 1;
  for (int i = 1; i < (int)_01.size() - K * 2; i += 2) {
    // cout<<"a"<<i;
    ll tmp = _01[i + K * 2] - _01[i - 1];
    if (ma < tmp)
      ma = tmp;
  }
  for (auto a : _01) {
    // cout<<a<<endl;
  }
  cout << ma << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, K;
  string S;
  cin >> N >> K;
  cin >> S;
  vector<int> l;
  int i = 0, tmp = 0;
  while (i < N) {
    tmp = 0;
    if (S[i] == '1') {
      while (S[i] == '1') {
        tmp++;
        i++;
      }
    } else {
      while (S[i] == '0') {
        tmp++;
        i++;
      }
    }
    l.push_back(tmp);
  }
  int hp;
  int ans = 0;
  for (int i = 0; i < (int)l.size(); i++) {
    tmp = 0;
    hp = K;

    if ((S[0] == '1' && i % 2 == 0) || (S[0] == '0' && i % 2 == 1)) {
      hp = hp * 2 + 1;
      hp = min(hp, (int)l.size() - i);
      for (int j = i; j < i + hp; j++) {
        tmp += l[j];
      }
    } else {
      hp = hp * 2;
      hp = min(hp, (int)l.size() - i);
      for (int j = i; j < i + hp; j++) {
        tmp += l[j];
      }
    }
    ans = max(ans, tmp);
    if (ans == N) {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int main() {
  int N, K;
  string S;
  cin >> N >> K;
  cin >> S;
  vector<int> l;
  int i = 0, tmp = 0;
  while (i < N) {
    tmp = 0;
    if (S[i] == '1') {
      while (S[i] == '1') {
        tmp++;
        i++;
      }
    } else {
      while (S[i] == '0') {
        tmp++;
        i++;
      }
    }
    l.push_back(tmp);
  }
  int hp;
  int ans = 0;
  for (int i = 0; i < (int)l.size(); i++) {
    tmp = 0;
    hp = K;

    if ((S[0] == '1' && i % 2 == 0) || (S[0] == '0' && i % 2 == 1)) {
      hp = hp * 2 + 1;
      hp = min(hp, (int)l.size() - i);
      for (int j = i; j < i + hp; j++) {
        tmp += l[j];
      }
    } else {
      if (i == 0) {
        hp = hp * 2;
        hp = min(hp, (int)l.size() - i);
        for (int j = i; j < i + hp; j++) {
          tmp += l[j];
        }
      }
    }
    ans = max(ans, tmp);
    if (ans == N) {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
//
// Created on 2019-02-09.
//
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PB push_back
#define INF 1e9
#define MOD 1e9 + 7
#define EPS 1e-9

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

  int N, K;
  string s;
  cin >> N >> K >> s;

  vector<ll> v;
  ll num__continuous = 0;
  char count_ch = '1';

  REP(i, s.length()) {
    if (s[i] == count_ch) {
      num__continuous++;
    } else {
      v.emplace_back(num__continuous);
      num__continuous = 1;
      count_ch = s[i];
    }
  }

  if (num__continuous != 0)
    v.push_back(num__continuous);
  if (v.size() % 2 == 0)
    v.push_back(0);

  ll ans = 0;
  int num_cnt = 2 * K + 1;

  for (int i = 0; i < v.size(); i += 2) {
    ll cnt = 0;
    REP(j, min(i + num_cnt, (int)v.size())) cnt += v[i + j];
    ans = max(ans, cnt);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
//
// Created on 2019-02-09.
//
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PB push_back
#define INF 1e9
#define MOD 1e9 + 7
#define EPS 1e-9

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

  int N, K;
  string s;
  cin >> N >> K >> s;

  vector<ll> v;
  ll num__continuous = 0;
  char count_ch = '1';

  REP(i, s.length()) {
    if (s[i] == count_ch) {
      num__continuous++;
    } else {
      v.emplace_back(num__continuous);
      num__continuous = 1;
      count_ch = s[i];
    }
  }

  if (num__continuous != 0)
    v.push_back(num__continuous);
  if (v.size() % 2 == 0)
    v.push_back(0);

  ll ans = 0;
  int num_cnt = 2 * K + 1;

  for (int i = 0; i < v.size(); i += 2) {
    ll cnt = 0;

    int left = i;
    int right = min(i + num_cnt, (int)v.size());
    for (int j = left; j < right; j++) {
      cnt += v[j];
    }
    ans = max(ans, cnt);
  }

  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;
  string s;

  cin >> n >> k >> s;

  vector<int> nums;
  int now = 1;
  int cnt = 0;

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

  if (cnt > 0) {
    nums.push_back(cnt);
  }

  // 1の数 - 0の数 - 1の数 - 0の数 - 1の数 という配列を作りたい
  // 0で終わってたら最後に1のやつを加える

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

  // for (auto a : nums) {
  //     cout << a << " ";
  // }
  // cout << endl;

  // TLEするバージョン
  int ans = nums[0];
  for (int i = 0; i < nums.size() / 2 - k + 1; i++) {
    int left = i * 2;
    int right = min(left + k * 2, (int)nums.size() - 1);

    // cout << left << " " << right << endl;

    int a = 0;
    for (int j = left; j <= right; j++) {
      a += nums[j];
    }
    ans = max(ans, a);
  }

  cout << ans << endl;

  return 0;
}


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

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

  cin >> n >> k >> s;

  vector<int> nums;
  int now = 1;
  int cnt = 0;

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

  if (cnt > 0) {
    nums.push_back(cnt);
  }

  // 1の数 - 0の数 - 1の数 - 0の数 - 1の数 という配列を作りたい
  // 0で終わってたら最後に1のやつを加える

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

  // for (auto a : nums) {
  //     cout << a << " ";
  // }
  // cout << endl;

  // TLEするバージョン
  int ans = nums[0];
  for (int i = 0; i < nums.size(); i += 2) {
    int left = i;
    int right = min(left + k * 2, (int)nums.size() - 1);

    // cout << left << " " << right << endl;

    int a = 0;
    for (int j = left; j <= right; j++) {
      a += nums[j];
    }
    ans = max(ans, a);
  }

  cout << ans << endl;

  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n, k, cnt = 0, mx = 0;
  string s;
  cin >> n >> k >> s;
  char cur = s[0], curr = 0, curl = 0;
  while (curr < n) {
    while (cnt < k && curr < n) {
      curr++;
      if (curr == n)
        break;
      if (s[curr] == '1' && s[curr - 1] == '0') {
        cnt++;
      }
    }
    while (curr < n) {
      curr++;
      if (s[curr] == '0' && s[curr - 1] == '1') {
        break;
      }
    }
    mx = max(mx, curr - curl);
    if (cnt < k)
      break;
    while (cnt >= k && curl < n) {
      curl++;
      if (s[curl] == '1' && s[curl - 1] == '0') {
        cnt--;
      }
    }
  }
  cout << mx << endl;
}

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

using namespace std;

int main() {
  int n, k, cnt = 0, mx = 0;
  string s;
  cin >> n >> k >> s;
  int curr = 0, curl = 0;
  while (curr < n) {
    while (cnt < k && curr < n) {
      curr++;
      if (curr == n)
        break;
      if (s[curr] == '1' && s[curr - 1] == '0') {
        cnt++;
      }
    }
    while (curr < n) {
      curr++;
      if (s[curr] == '0' && s[curr - 1] == '1') {
        break;
      }
    }
    mx = max(mx, curr - curl);
    if (cnt < k)
      break;
    while (cnt >= k && curl < n) {
      curl++;
      if (s[curl] == '1' && s[curl - 1] == '0') {
        cnt--;
      }
    }
  }
  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 n, k, i, bi, hs = 0, max;
  string s;
  vector<int> w;
  cin >> n >> k >> s;
  if (s[0] == '0')
    w.push_back(0);
  i = 0;
  while (i < n) {
    bi = i;
    while (i < n && s[i + 1] == s[i])
      i++;
    w.push_back(i - bi + 1);
    i++;
  }
  if (s[n - 1] == '0')
    w.push_back(0);
  if (w.size() <= k * 2 + 1) {
    cout << n << endl;
    return 0;
  }
  for (i = 0; i < k * 2 + 1; i++)
    hs += w[i];
  max = hs;
  for (i = 0; i + k * 2 + 2 < w.size(); i += 2) {
    hs += w[i + k * 2 + 1] + w[i + k * 2 + 2] - w[i * 2] - w[i * 2 + 1];
    if (max < hs)
      max = hs;
  }
  cout << max << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  int n, k, i, bi, hs = 0, max;
  string s;
  vector<int> w;
  cin >> n >> k >> s;
  if (s[0] == '0')
    w.push_back(0);
  i = 0;
  while (i < n) {
    bi = i;
    while (i < n && s[i + 1] == s[i])
      i++;
    w.push_back(i - bi + 1);
    i++;
  }
  if (s[n - 1] == '0')
    w.push_back(0);
  if (w.size() <= k * 2 + 1) {
    cout << n << endl;
    return 0;
  }
  for (i = 0; i < k * 2 + 1; i++)
    hs += w[i];
  max = hs;
  for (i = 0; i + k * 2 + 2 < w.size(); i += 2) {
    hs += w[i + k * 2 + 1] + w[i + k * 2 + 2] - w[i] - w[i + 1];
    if (max < hs)
      max = hs;
  }
  cout << max << 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;

  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;
    }
  }
  if (cnt != 0) {
    Nums.push_back(cnt);
  }
  if (Nums.size() % 2 == 0) {
    Nums.push_back(0);
  }

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

  // 1 0 1 0 1  0  1
  // 0 1 3 5 8 10 14
  int Add = 2 * k + 1;
  int ans = 0;
  for (int i = 0; i < (int)Nums.size(); i += 2) {
    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int tmp = cum[right] - cum[left];
    ans = max(tmp, ans);
  }
  cout << ans;
  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;

  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;
    }
  }
  if (cnt != 0) {
    Nums.push_back(cnt);
  }
  if (Nums.size() % 2 == 0) {
    Nums.push_back(0);
  }

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

  // 1 0 1 0 1  0  1
  // 0 1 3 5 8 10 14
  int Add = 2 * k + 1;
  int ans = 0;
  for (int i = 0; i < (int)Nums.size(); i += 2) {
    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int tmp = cum[right] - cum[left];
    ans = max(tmp, ans);
  }
  cout << ans;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <vector>

int main(void) {
  int tmp, n, k;
  char s[100001];
  std::vector<int> v0;
  std::vector<int> v1;
  scanf("%d %d\n", &n, &k);
  scanf("%s", s);
  char *tmp1, *tmp2;

  int m = 0;
  tmp1 = s;

  v1.push_back(0);
  while (*tmp1 != '\0') {
    tmp2 = tmp1;
    tmp1 += strspn(tmp1, "0");
    tmp = tmp1 - tmp2;
    v0.push_back(tmp);

    tmp2 = tmp1;
    tmp1 += strspn(tmp1, "1");
    tmp = tmp1 - tmp2;
    v1.push_back(tmp);

    m++;
  }

  tmp = v1[0];
  int i = 0;
  for (; i < k; i++) {
    tmp += v0[i] + v1[i + 1];
  }
  int ans = tmp;
  for (; i < m; i++) {
    tmp = tmp - v0[i - k] - v1[i - k] + v0[i] + v1[i + 1];
    if (tmp > ans)
      ans = tmp;
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <vector>

int main(void) {
  int tmp, n, k;
  char s[100001];
  std::vector<int> v0;
  std::vector<int> v1;
  scanf("%d %d\n", &n, &k);
  scanf("%s", s);
  char *tmp1, *tmp2;

  int m = 0;
  tmp1 = s;

  v1.push_back(0);
  while (*tmp1 != '\0') {
    tmp2 = tmp1;
    tmp1 += strspn(tmp1, "0");
    tmp = tmp1 - tmp2;
    v0.push_back(tmp);

    tmp2 = tmp1;
    tmp1 += strspn(tmp1, "1");
    tmp = tmp1 - tmp2;
    v1.push_back(tmp);

    m++;
  }

  tmp = v1[0];
  int i = 0;
  for (; i < std::min(k, m); i++) {
    tmp += v0[i] + v1[i + 1];
  }
  int ans = tmp;
  for (; i < m; i++) {
    tmp = tmp - v0[i - k] - v1[i - k] + v0[i] + v1[i + 1];
    if (tmp > ans)
      ans = tmp;
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03074 (C++)

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

using namespace std;

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

  string S;
  cin >> S;

  vector<int> cum_sum;
  cum_sum.push_back(0);

  int counter = 0;
  char tmp_char = S[0];
  if (tmp_char == '0')
    cum_sum.push_back(0);

  for (int i = 0; i < N; i++) {
    if (tmp_char == S[i]) {
      counter++;
      if (i == (N - 1))
        cum_sum.push_back(cum_sum.back() + counter);
    } else {
      cum_sum.push_back(cum_sum.back() + counter);
      counter = 1;
      tmp_char = S[i];
      if (i == (N - 1))
        cum_sum.push_back(cum_sum.back() + 1);

      // cout << S[i] << endl;
    }
    // cout << cum_sum.back() << endl;
  }

  if (S[N - 1] == '0')
    cum_sum.push_back(cum_sum.back());

  // for (int i = 0; i != cum_sum.size(); ++i) {
  //     cout << cum_sum[i] << endl;
  // }
  int max_1 = 0;
  counter = 0;

  if (K == 0) {
    for (int i = 0; i < N; i++) {
      if (S[i] == '1')
        counter++;
      else {
        max_1 = max(max_1, counter);
        counter = 0;
      }
    }
    max_1 = max(max_1, counter);
    cout << max_1 << endl;
    return 0;
  }

  int ans = 1;
  for (int i = 0; i + (2 * K + 1) < cum_sum.size(); i = (i + 2)) {
    ans = max(ans, cum_sum[(2 * K + 1) + i] - cum_sum[i]);
  }
  for (int i = cum_sum.size(); i - (2 * K + 1) < 0; i = (i - 2)) {
    ans = max(ans, cum_sum[i] - cum_sum[i - (2 * K + 1)]);
  }

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

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

using namespace std;

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

  string S;
  cin >> S;

  vector<int> cum_sum;
  cum_sum.push_back(0);

  int counter = 0;
  char tmp_char = S[0];
  if (tmp_char == '0')
    cum_sum.push_back(0);

  for (int i = 0; i < N; i++) {
    if (tmp_char == S[i]) {
      counter++;
      if (i == (N - 1))
        cum_sum.push_back(cum_sum.back() + counter);
    } else {
      cum_sum.push_back(cum_sum.back() + counter);
      counter = 1;
      tmp_char = S[i];
      if (i == (N - 1))
        cum_sum.push_back(cum_sum.back() + 1);

      // cout << S[i] << endl;
    }
    // cout << cum_sum.back() << endl;
  }

  if (S[N - 1] == '0')
    cum_sum.push_back(cum_sum.back());

  // for (int i = 0; i != cum_sum.size(); ++i) {
  //     cout << cum_sum[i] << endl;
  // }
  int max_1 = 0;
  counter = 0;

  if (K == 0) {
    for (int i = 0; i < N; i++) {
      if (S[i] == '1')
        counter++;
      else {
        max_1 = max(max_1, counter);
        counter = 0;
      }
    }
    max_1 = max(max_1, counter);
    cout << max_1 << endl;
    return 0;
  }

  int ans = 1;
  int ind = 0;
  for (int i = 0; i < cum_sum.size(); i += 2) {
    ind = min((2 * K + 1) + i, int(cum_sum.size()) - 1);
    ans = max(ans, cum_sum[ind] - cum_sum[i]);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, k;
  int ans = 0;
  int block[100000] = {0};
  string str;

  cin >> n >> k;
  cin >> str;

  int pattern = 1;
  int cnt = 0;
  int index = 0;
  for (int i = 0; i < (int)str.size(); i++) {
    switch (pattern) {
    case 1:
      if (str[i] == '0') {
        block[index] = cnt;
        index++;
        cnt = 1;
        pattern = 0;
      } else {
        cnt++;
      }
      break;
    case 0:
      if (str[i] == '1') {
        block[index] = cnt;
        index++;
        cnt = 1;
        pattern = 1;
      } else {
        cnt++;
      }
      break;
    }
  }
  block[index] = cnt;

  int black = 0;
  int white = 0;

  for (int i = 0; i < k + 1; i++) {
    black += block[i * 2];
  }
  for (int i = 0; i < k; i++) {
    white += block[i * 2 + 1];
  }

  int lblack = 0, rblack = k * 2;
  int lwhite = 1, rwhite = k * 2 - 1;
  ans = max(ans, black + white);

  while (rblack <= index) {
    black += block[rblack + 2];
    black -= block[lblack];
    white += block[rwhite + 2];
    white -= block[lwhite];
    ans = max(ans, black + white);
    rblack += 2;
    lblack += 2;
    rwhite += 2;
    lwhite += 2;
  }

  cout << ans << endl;

  return (0);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, k;
  int ans = 0;
  int block[1000005] = {0};
  string str;

  cin >> n >> k;
  cin >> str;

  int pattern = 1;
  int cnt = 0;
  int index = 0;
  for (int i = 0; i < (int)str.size(); i++) {
    switch (pattern) {
    case 1:
      if (str[i] == '0') {
        block[index] = cnt;
        index++;
        cnt = 1;
        pattern = 0;
      } else {
        cnt++;
      }
      break;
    case 0:
      if (str[i] == '1') {
        block[index] = cnt;
        index++;
        cnt = 1;
        pattern = 1;
      } else {
        cnt++;
      }
      break;
    }
  }
  block[index] = cnt;

  int black = 0;
  int white = 0;

  for (int i = 0; i < k + 1; i++) {
    black += block[i * 2];
  }
  for (int i = 0; i < k; i++) {
    white += block[i * 2 + 1];
  }

  int lblack = 0, rblack = k * 2;
  int lwhite = 1, rwhite = k * 2 - 1;
  ans = max(ans, black + white);

  while (rblack <= index) {
    black += block[rblack + 2];
    black -= block[lblack];
    white += block[rwhite + 2];
    white -= block[lwhite];
    ans = max(ans, black + white);
    rblack += 2;
    lblack += 2;
    rwhite += 2;
    lwhite += 2;
  }

  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> num;
  if (s[0] == '0')
    num.push_back(0);

  for (int left = 0; left < N;) {
    int right = left;
    while (right < N && s[right] == s[left]) {
      right++;
    }
    num.push_back(right - left);
    left = right;
  } // 連続した数を尺取りで記録
  if (s.back() == '1')
    s.push_back('0');

  vector<int> sum(num.size() + 1, 0);
  for (int i = 0; i <= num.size(); i++) {
    sum[i + 1] = sum[i] + num[i];
  } // 累積和

  /*
  for(int i=0;i<sum.size();i++){
      cout << sum[i] << endl;
  }
  */

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

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

int main() {
  int N, K;
  cin >> N >> K;
  string s;
  cin >> s;
  vector<int> num;
  if (s[0] == '0')
    num.push_back(0);

  for (int left = 0; left < N;) {
    int right = left;
    while (right < N && s[right] == s[left]) {
      right++;
    }
    num.push_back(right - left);
    left = right;
  } // 連続した数を尺取りで記録
  if (s.back() == '1')
    s.push_back('0');

  vector<int> sum(num.size() + 1, 0);
  for (int i = 0; i < num.size(); i++) {
    sum[i + 1] = sum[i] + num[i];
  } // 累積和

  /*
  for(int i=0;i<sum.size();i++){
      cout << sum[i] << endl;
  }
  */

  int ans = 0;
  for (int left = 0; left < sum.size(); left += 2) {
    int right = left + 2 * K + 1;
    // cout << right << endl;
    if (right >= sum.size())
      right = sum.size() - 1;
    ans = max(ans, sum[right] - sum[left]);
  }
  cout << ans << endl;
}
Problem p03074 (C++)

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

using namespace std;

static const int N = 100000;

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

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

  vector<int> v(100100);
  int vidx = 2;

  for (int i = 0; i < n; i++) {
    if (s[i] - '0' == vidx % 2) {
      v[vidx]++;
    } else {
      vidx++;
      v[vidx]++;
    }
  }

  int maxlen = -1;
  for (int i = 1; i < vidx + 10; i += 2) {
    int nowlen = 0;
    for (int j = 0; j < k * 2 + 1; j++) {
      nowlen += v[i + j];
    }
    maxlen = max(nowlen, maxlen);
  }

  cout << maxlen << endl;

  return 0;
}


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

using namespace std;

static const int N = 100000;

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

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

  vector<int> v(2 * N);
  int vidx = 2;

  for (int i = 0; i < n; i++) {
    if (s[i] - '0' == vidx % 2) {
      v[vidx]++;
    } else {
      vidx++;
      v[vidx]++;
    }
  }

  int maxlen = -1;
  for (int i = 1; i < vidx + 10; i += 2) {
    int nowlen = 0;
    for (int j = 0; j < k * 2 + 1; j++) {
      nowlen += v[i + j];
    }
    maxlen = max(nowlen, maxlen);
  }

  cout << maxlen << 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<pair<char, int>> HF;
  char now = S[0];
  int count = 1;
  for (int i = 1; i < N; i++) {
    char pre = now;
    now = S[i];
    if (pre == now)
      count++;
    else {
      HF.push_back(make_pair(pre, count));
      count = 1;
    }
  }
  HF.push_back(make_pair(S[N - 1], count));
  //  for(int i = 0; i < HF.size(); i++){
  //    std::cout << i << ": (" << HF.at(i).first << ", " << HF.at(i).second <<
  //    ")"<< std::endl;
  //  }
  // std::cout << "HF.size() " << HF.size() << std::endl;
  // std::cout << "HF.size()-2*K " << HF.size()-2*K << std::endl;

  int ans = 0;
  bool check = true;
  for (int i = 0; i <= HF.size() - 2 * K + 1; i++) {
    //    std::cout << "i: "<< i << std::endl;
    check = false;
    int temp = 0;
    if (HF.at(i).first == '1') {
      temp += HF.at(i).second;
      i++;
      //      std::cout <<"HF.size()-2*K+1 " << HF.size()-2*K+1 << std::endl;
      if (i + 2 * K > HF.size() + 1) {
        ans = max(ans, temp);
        break;
      }
    };
    for (int j = i; j < 2 * K - 1 + i; j++) {
      //      std::cout << "j: " << j << std::endl;
      temp += HF.at(j).second;
    }
    if (2 * K - 1 + i < HF.size())
      temp += HF.at(2 * K - 1 + i).second;
    ans = max(ans, temp);
  }
  if (check) {
    for (int i = 0; i < HF.size(); i++)
      ans += HF.at(i).second;
  }
  std::cout << ans << std::endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<pair<char, int>> HF;
  char now = S[0];
  int count = 1;
  for (int i = 1; i < N; i++) {
    char pre = now;
    now = S[i];
    if (pre == now)
      count++;
    else {
      HF.push_back(make_pair(pre, count));
      count = 1;
    }
  }
  HF.push_back(make_pair(S[N - 1], count));
  //  for(int i = 0; i < HF.size(); i++){
  //    std::cout << i << ": (" << HF.at(i).first << ", " << HF.at(i).second <<
  //    ")"<< std::endl;
  //  }
  // std::cout << "HF.size() " << HF.size() << std::endl;
  // std::cout << "HF.size()-2*K " << HF.size()-2*K << std::endl;

  int ans = 0;
  bool check = true;
  for (int i = 0; i + 2 * K <= HF.size() + 1; i++) {
    //    std::cout << "i: "<< i << std::endl;
    check = false;
    int temp = 0;
    if (HF.at(i).first == '1') {
      temp += HF.at(i).second;
      i++;
      //      std::cout <<"HF.size()-2*K+1 " << HF.size()-2*K+1 << std::endl;
      if (i + 2 * K > HF.size() + 1) {
        ans = max(ans, temp);
        break;
      }
    };
    for (int j = i; j < 2 * K - 1 + i; j++) {
      //      std::cout << "j: " << j << std::endl;
      temp += HF.at(j).second;
    }
    if (2 * K - 1 + i < HF.size())
      temp += HF.at(2 * K - 1 + i).second;
    ans = max(ans, temp);
  }
  if (check) {
    for (int i = 0; i < HF.size(); i++)
      ans += HF.at(i).second;
  }
  std::cout << ans << std::endl;
}