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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

const int64_t cc = pow(10, 9) + 7;

int main() {
  int64_t a, b, c, d;
  std::cin >> a;
  std::cin >> b;

  std::string str;
  std::cin >> str;

  std::vector<int64_t> list;
  std::vector<int64_t> zero_list;

  char s = str[0];
  c = 0;

  for (int i = 0; i < str.size(); i++) {
    if (s != str[i]) {
      if (s == '1') {
        list.push_back(c);
      } else {
        list.push_back(c * -1);
        zero_list.push_back(list.size() - 1);
      }

      c = 0;
      s = str[i];
    }

    c += 1;
  }

  if (s == '1') {
    list.push_back(c);
  } else {
    list.push_back(c * -1);
    zero_list.push_back(list.size() - 1);
  }

  int64_t start = 0;
  int64_t finish = 0;
  int64_t answer = 0;

  if (!zero_list.empty()) {
    for (int i = 0; i <= zero_list.size() - b; i++) {
      int instance = 0;

      if (i + b >= zero_list.size()) {
        finish = list.size() - 1;
      } else {
        finish = zero_list[i + b] - 1;
      }
      // std::cout<< start << " " << finish <<"\n";

      for (int r = start; r <= finish; r++) {
        instance += abs(list[r]);
      }

      if (answer < instance) {
        answer = instance;
      }

      start = zero_list[i] + 1;
    }
  } else {
    answer = str.size();
  }

  std::cout << answer;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

const int64_t cc = pow(10, 9) + 7;

int main() {
  int64_t a, b, c, d;
  std::cin >> a;
  std::cin >> b;

  std::string str;
  std::cin >> str;

  std::vector<int64_t> list;
  std::vector<int64_t> zero_list;

  char s = str[0];
  c = 0;

  for (int i = 0; i < str.size(); i++) {
    if (s != str[i]) {
      if (s == '1') {
        list.push_back(c);
      } else {
        list.push_back(c * -1);
        zero_list.push_back(list.size() - 1);
      }

      c = 0;
      s = str[i];
    }

    c += 1;
  }

  if (s == '1') {
    list.push_back(c);
  } else {
    list.push_back(c * -1);
    zero_list.push_back(list.size() - 1);
  }

  int64_t start = 0;
  int64_t finish = 0;
  int64_t answer = 0;

  if (!zero_list.empty() && b < zero_list.size()) {
    for (int i = 0; i <= zero_list.size() - b; i++) {
      int instance = 0;

      if (i + b >= zero_list.size()) {
        finish = list.size() - 1;
      } else {
        finish = zero_list[i + b] - 1;
      }
      // std::cout<< start << " " << finish <<"\n";

      for (int r = start; r <= finish; r++) {
        instance += abs(list[r]);
      }

      if (answer < instance) {
        answer = instance;
      }

      start = zero_list[i] + 1;
    }
  } else {
    answer = str.size();
  }

  std::cout << answer;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, k, z = 0;
  vector<int> a = {0};
  string s;
  cin >> n >> k >> s;
  if (s[0] == '0')
    a.push_back(0);
  int l = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] != s[i - 1]) {
      a.push_back(i - l);
      l = i;
    }
  }
  for (int i = 0; i < 100005; i++)
    a.push_back(0);
  for (int i = 1; i < a.size(); i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= n + 1; i++) {
    if (i % 2) {
      z = max(z, a[i + k + k] - a[i - 1]);
    }
  }
  cout << z;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, k, z = 0;
  vector<int> a = {0};
  string s;
  cin >> n >> k >> s;
  if (s[0] == '0')
    a.push_back(0);
  int l = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] != s[i - 1]) {
      a.push_back(i - l);
      l = i;
    }
  }
  for (int i = 0; i < 300005; i++)
    a.push_back(0);
  for (int i = 1; i < a.size(); i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= n + 1; i++) {
    if (i % 2) {
      z = max(z, a[i + k + k] - a[i - 1]);
    }
  }
  cout << z;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

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

  vector<int> vec;
  int now = 1;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == (char)('0' + now)) {
      cnt++;
    } else {
      vec.push_back(cnt);
      cnt = 1;
      now ^= 1;
    }
  }
  if (cnt > 0) {
    vec.push_back(cnt);
  }

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

  ll ans = 0;
  int right = 0;
  ll sum = 0;
  for (int left = 0; left < n; left += 2) {
    while (right < min(left + 2 * k + 1, (int)vec.size())) {
      sum += vec[right];
      ++right;
    }

    ans = max(ans, sum);

    if (right == left)
      ++right;
    else
      sum -= vec[left] + vec[left + 1];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

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

  vector<int> vec;
  int now = 1;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == (char)('0' + now)) {
      cnt++;
    } else {
      vec.push_back(cnt);
      cnt = 1;
      now ^= 1;
    }
  }
  if (cnt > 0) {
    vec.push_back(cnt);
  }

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

  ll ans = 0;
  int right = 0;
  ll sum = 0;
  for (int left = 0; left < vec.size(); left += 2) {
    while (right < min(left + 2 * k + 1, (int)vec.size())) {
      sum += vec[right];
      ++right;
    }

    ans = max(ans, sum);

    if (right == left)
      ++right;
    else
      sum -= vec[left] + vec[left + 1];
  }
  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> a;
  a.push_back(0);
  for (int i = 0; i < N;) {
    int cnt = 0;
    if (S[i] == '0') {
      while (i < N && S[i] == '0') {
        i++;
        cnt--;
      }
    } else if (S[i] == '1') {
      while (i < N && S[i] == '1') {
        i++;
        cnt++;
      }
    }
    a.push_back(cnt);
  }
  int n = a.size();

  vector<int> nNeg(n, 0);
  for (int i = 0; i < n; i++) {
    if (a[i] < 0)
      nNeg[i]++;
    if (i > 0)
      nNeg[i] += nNeg[i - 1];
  }
  nNeg.push_back(1e9);
  vector<int> suma(n, 0);
  for (int i = 1; i <= n; i++) {
    suma[i] = abs(a[i]);
    suma[i] += suma[i - 1];
  }
  // for(int i=0; i<n; i++){
  //     cout << a[i] << " ";
  // }cout << endl;
  // for(int i=0; i<n; i++){
  //     cout << nNeg[i] << " ";
  // }cout << endl;

  int ans = 0;
  for (int start = 1; start < n; start++) {
    int end = upper_bound(nNeg.begin(), nNeg.end(), K + nNeg[start - 1]) -
              nNeg.begin() - 1;
    ans = max(ans, suma[end] - suma[start - 1]);
    // cout << start << " " << end << " " << suma[end]-suma[start-1] << endl;
  }

  cout << ans << endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> a;
  a.push_back(0);
  for (int i = 0; i < N;) {
    int cnt = 0;
    if (S[i] == '0') {
      while (i < N && S[i] == '0') {
        i++;
        cnt--;
      }
    } else if (S[i] == '1') {
      while (i < N && S[i] == '1') {
        i++;
        cnt++;
      }
    }
    a.push_back(cnt);
  }
  int n = a.size();

  vector<int> nNeg(n, 0);
  for (int i = 0; i < n; i++) {
    if (a[i] < 0)
      nNeg[i]++;
    if (i > 0)
      nNeg[i] += nNeg[i - 1];
  }
  nNeg.push_back(1e9);
  vector<int> suma(n, 0);
  for (int i = 0; i < n; i++) {
    suma[i] = abs(a[i]);
    suma[i] += suma[i - 1];
  }
  // for(int i=0; i<n; i++){
  //     cout << a[i] << " ";
  // }cout << endl;
  // for(int i=0; i<n; i++){
  //     cout << nNeg[i] << " ";
  // }cout << endl;

  int ans = 0;
  for (int start = 1; start < n; start++) {
    int end = upper_bound(nNeg.begin(), nNeg.end(), K + nNeg[start - 1]) -
              nNeg.begin() - 1;
    ans = max(ans, suma[end] - suma[start - 1]);
    // cout << start << " " << end << " " << suma[end]-suma[start-1] << endl;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define PII pair<int, int>
#define N 100005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int main() {
  vector<int> v, bit;
  int n, k, c, sum = 0, ans = 0;
  cin >> n >> k;
  string s;
  cin >> s;
  c = 1;
  for (int i = 1; i < n; ++i) {
    if (s[i] == s[i - 1]) {
      c++;
    } else {
      v.push_back(c);
      bit.push_back(s[i - 1] - '0');
      c = 1;
    }
  }
  if (c > 0) {
    v.push_back(c);
    bit.push_back(s.back() - '0');
  }
  int r = 0, cnt = 0;
  for (int i = 0; i < (int)v.size(); ++i) {
    while (r < n && cnt <= k) {
      if (bit[r] == 0 && cnt == k)
        break;
      if (bit[r] == 0)
        cnt++;
      sum = sum + v[r];
      r++;
    }
    // cout << i << ' ' << sum << "\n";
    ans = max(ans, sum);
    if (bit[i] == 0)
      cnt--;
    sum = sum - v[i];
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define PII pair<int, int>
#define N 100005
#define MOD 1000000007
#define INF 1000000000000000000
using namespace std;

int main() {
  vector<int> v, bit;
  int n, k, c, sum = 0, ans = 0;
  cin >> n >> k;
  string s;
  cin >> s;
  c = 1;
  for (int i = 1; i < n; ++i) {
    if (s[i] == s[i - 1]) {
      c++;
    } else {
      v.push_back(c);
      bit.push_back(s[i - 1] - '0');
      c = 1;
    }
  }
  if (c > 0) {
    v.push_back(c);
    bit.push_back(s.back() - '0');
  }
  int r = 0, cnt = 0;
  for (int i = 0; i < (int)v.size(); ++i) {
    while (r < (int)v.size() && cnt <= k) {
      if (bit[r] == 0 && cnt == k)
        break;
      if (bit[r] == 0)
        cnt++;
      sum = sum + v[r];
      r++;
    }
    // cout << i << ' ' << sum << "\n";
    ans = max(ans, sum);
    if (bit[i] == 0)
      cnt--;
    sum = sum - v[i];
  }
  cout << ans;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007
using namespace std;

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

  // 1-0-1-0-1-0-1が欲しい
  // 1-0-1-0みたいなので終わってたら1を足したい
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  /*for(int i=0;i<n;i++){
      cout << Nums[i];
  }*/

  int Add = 2 * k + 1;

  int ans = 0;
  int tmp = 0;

  // 偶数番目だけ足したい
  for (int i = 0; i < Nums.size(); i += 2) {
    tmp = 0;
    for (int j = i; j < i + Add; j++) {
      tmp += Nums[j];
    }
    ans = max(ans, tmp);
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007
using namespace std;

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

  // 1-0-1-0-1-0-1が欲しい
  // 1-0-1-0みたいなので終わってたら1を足したい
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  /*for(int i=0;i<n;i++){
      cout << Nums[i];
  }*/

  int Add = 2 * k + 1;

  int ans = 0;
  int tmp = 0;

  // 偶数番目だけ足したい
  for (int i = 0; i < Nums.size(); i += 2) {
    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: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
#define repr(i, a, b) for (int i = a; i >= (b); --i)
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  string S;
  cin >> S;

  vector<int> A(0);

  char now = '1';
  int cnt = 0;
  rep(i, S.size()) {
    if (now == S[i])
      cnt++;
    else {
      A.pb(cnt);
      now = S[i];
      cnt = 1;
    }
  }
  if (cnt != 0)
    A.pb(cnt);
  if (S[S.size() - 1] == '0')
    A.pb(0);
  // rep(i, A.size()) { cout << A[i] << endl; }

  vector<int> sum(N + 1);
  rep(i, N + 1) { sum[i + 1] = sum[i] + A[i]; }

  int w = 2 * K + 1;
  int temp = 0;
  int ans = 0;

  for (int i = 0; i < A.size(); i += 2) {
    int l = i;
    int r = min(i + w, (int)A.size());

    temp = sum[r] - sum[l];

    chmax(ans, temp);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
#define repr(i, a, b) for (int i = a; i >= (b); --i)
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  string S;
  cin >> S;

  vector<int> A(0);

  char now = '1';
  int cnt = 0;
  rep(i, S.size()) {
    if (now == S[i])
      cnt++;
    else {
      A.pb(cnt);
      now = S[i];
      cnt = 1;
    }
  }
  if (cnt != 0)
    A.pb(cnt);
  if (S[S.size() - 1] == '0')
    A.pb(0);
  // rep(i, A.size()) { cout << A[i] << endl; }

  vector<int> sum(N + 1);
  rep(i, A.size()) { sum[i + 1] = sum[i] + A[i]; }

  int w = 2 * K + 1;
  int temp = 0;
  int ans = 0;

  for (int i = 0; i < A.size(); i += 2) {
    int l = i;
    int r = min(i + w, (int)A.size());

    temp = sum[r] - sum[l];

    chmax(ans, temp);
  }

  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; // nowがいくつ並んでいるか
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now; // 0と1を切り替える時の計算 now ^= 1;
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);

  // 1-0-1-0-1-0-1って感じの配列が欲しい
  // 1-0-1-0みたいに0で終わってたら、適当に１つ足す
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  int Add = 2 * K + 1;

  // 累積和を作る
  //  0 1 2 3 4 5 6
  //   0 1 2 3 4 5

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

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

    ans = max(tmp, ans);
  }

  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; // nowがいくつ並んでいるか
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now; // 0と1を切り替える時の計算 now ^= 1;
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);

  // 1-0-1-0-1-0-1って感じの配列が欲しい
  // 1-0-1-0みたいに0で終わってたら、適当に１つ足す
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  int Add = 2 * K + 1;

  // 累積和を作る
  //  0 1 2 3 4 5 6
  //   0 1 2 3 4 5

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

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

    ans = max(tmp, ans);
  }

  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; // nowがいくつ並んでいるか
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now; // 0と1を切り替える時の計算 now ^= 1;
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);

  // 1-0-1-0-1-0-1って感じの配列が欲しい
  // 1-0-1-0みたいに0で終わってたら、適当に１つ足す
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  int Add = 2 * K + 1;

  int ans = 0;

  // 1-0-1...の1から始めるので、偶数番目だけ見る
  for (int i = 0; i < Nums.size(); i += 2) {
    int tmp = 0;

    int left = i;
    int right = i + Add;
    for (int j = left; j < right; j++) {
      tmp += Nums[j];
    }
    ans = max(tmp, ans);
  }

  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; // nowがいくつ並んでいるか
  for (int i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now))
      cnt++;
    else {
      Nums.push_back(cnt);
      now = 1 - now; // 0と1を切り替える時の計算 now ^= 1;
      cnt = 1;       // 新しいのをカウントし始める
    }
  }
  if (cnt != 0)
    Nums.push_back(cnt);

  // 1-0-1-0-1-0-1って感じの配列が欲しい
  // 1-0-1-0みたいに0で終わってたら、適当に１つ足す
  if (Nums.size() % 2 == 0)
    Nums.push_back(0);

  int Add = 2 * K + 1;

  int ans = 0;

  // 1-0-1...の1から始めるので、偶数番目だけ見る
  for (int i = 0; i < Nums.size(); i += 2) {
    int tmp = 0;

    int left = i;
    int right = min(i + Add, (int)Nums.size());
    for (int j = left; j < right; j++) {
      tmp += Nums[j];
    }
    ans = max(tmp, ans);
  }

  cout << ans << endl;
}
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> a;
  int b = (s[0] == '0' ? 0 : 1);
  a.push_back(1);
  for (int i = 1; i < n; ++i) {
    if (s[i] == s[i - 1]) {
      ++a.back();
    } else {
      a.push_back(1);
    }
  }
  int m = a.size();

  for (int i = 1; i < n; ++i)
    a[i] += a[i - 1];

  int ans = 0;
  for (int i = 0; i < m; ++i) {
    if (b == 0) {
      ans = max(ans, a[min(m - 1, i + 2 * k - 1)] - ((i > 0) ? a[i - 1] : 0));
    } else {
      ans = max(ans, a[min(m - 1, i + 2 * k)] - ((i > 0) ? a[i - 1] : 0));
    }
    b = 1 - b;
  }

  cout << ans << endl;
}

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> a;
  int b = (s[0] == '0' ? 0 : 1);
  a.push_back(1);
  for (int i = 1; i < n; ++i) {
    if (s[i] == s[i - 1]) {
      ++a.back();
    } else {
      a.push_back(1);
    }
  }
  int m = a.size();

  for (int i = 1; i < m; ++i)
    a[i] += a[i - 1];

  int ans = 0;
  for (int i = 0; i < m; ++i) {
    if (b == 0) {
      ans = max(ans, a[min(m - 1, i + 2 * k - 1)] - ((i > 0) ? a[i - 1] : 0));
    } else {
      ans = max(ans, a[min(m - 1, i + 2 * k)] - ((i > 0) ? a[i - 1] : 0));
    }
    b = 1 - b;
  }

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

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

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

  ll count = 1, ans = 0;
  vector<ll> stand, handstand;
  for (int i = 1; i < N; i++) {
    if (S[i] != S[i - 1]) {
      if (S[i - 1] == '1') {
        handstand.push_back(count);
        if (count > ans)
          ans = count;
      } else {
        stand.push_back(count);
      }
      count = 1;
    } else {
      count++;
    }
  }
  if (S[N - 1] == '1') {
    handstand.push_back(count);
    if (count > ans)
      ans = count;
  } else {
    stand.push_back(count);
  }

  ll HS_size = handstand.size(), S_size = stand.size();
  if (HS_size == 0 || S_size <= K) {
    cout << N << endl;
    return 0;
  }

  ll HS_count;
  for (int i = 0; i < S_size; i++) {
    HS_count = 0;
    for (int j = i; j < i + K && j < S_size; j++) {
      HS_count += stand[j];
      if (j < HS_size)
        HS_count += handstand[j];
      if (S[0] == '0' && j == i && j != 0)
        HS_count += handstand[j - 1];
      if (S[0] == '1' && j == i + K - 1 && j + 1 < HS_size)
        HS_count += handstand[j + 1];
    }

    if (HS_count > ans)
      ans = HS_count;
  }

  cout << ans << endl;
}


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

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

  ll count = 1, ans = 0;
  vector<ll> stand, handstand;
  for (int i = 1; i < N; i++) {
    if (S[i] != S[i - 1]) {
      if (S[i - 1] == '1') {
        handstand.push_back(count);
        if (count > ans)
          ans = count;
      } else {
        stand.push_back(count);
      }
      count = 1;
    } else {
      count++;
    }
  }
  if (S[N - 1] == '1') {
    handstand.push_back(count);
    if (count > ans)
      ans = count;
  } else {
    stand.push_back(count);
  }

  ll HS_size = handstand.size(), S_size = stand.size();
  if (HS_size == 0 || S_size <= K) {
    cout << N << endl;
    return 0;
  }

  ll HS_count;
  for (int i = 0; i < (S_size - K + 1); i++) {
    HS_count = 0;
    for (int j = i; j < i + K && j < S_size; j++) {
      HS_count += stand[j];
      if (j < HS_size)
        HS_count += handstand[j];
      if (S[0] == '0' && j == i && j != 0)
        HS_count += handstand[j - 1];
      if (S[0] == '1' && j == i + K - 1 && j + 1 < HS_size)
        HS_count += handstand[j + 1];
    }

    if (HS_count > ans)
      ans = HS_count;
  }

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

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

int n, k, a[100005];
vector<int> v, av;

int main(void) {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%1d", &a[i]);
  if (a[1] == 0)
    v.push_back(0);
  int len = 0;
  for (int i = 1; i <= n; i++) {
    if (i > 1 && a[i] != a[i - 1]) {
      v.push_back(len);
      len = 1;
    } else
      len++;
  }
  v.push_back(len);
  av.resize(v.size());
  av[0] = 0;
  for (int i = 0; i < v.size(); i++)
    av[i + 1] = av[i] + v[i];
  int ans = 0;
  for (int i = 0; i < av.size(); i += 2) {
    int ss = av[min((int)v.size(), i + 2 * k + 1)] - av[i];
    ans = max(ans, ss);
  }
  printf("%d\n", ans);
}

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

int n, k, a[100005];
vector<int> v, av;

int main(void) {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%1d", &a[i]);
  if (a[1] == 0)
    v.push_back(0);
  int len = 0;
  for (int i = 1; i <= n; i++) {
    if (i > 1 && a[i] != a[i - 1]) {
      v.push_back(len);
      len = 1;
    } else
      len++;
  }
  v.push_back(len);
  av.resize(v.size() + 1);
  av[0] = 0;
  for (int i = 0; i < v.size(); i++)
    av[i + 1] = av[i] + v[i];
  int ans = 0;
  for (int i = 0; i < av.size(); i += 2) {
    int ss = av[min((int)v.size(), i + 2 * k + 1)] - av[i];
    ans = max(ans, ss);
  }
  printf("%d\n", ans);
}
Problem p03074 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> vec;
  // 0,2,4,,番目に連続する1の個数を、1,3,5,,,番目に連続する0の個数をpush_backする
  if (S[0] == '0') {
    vec.push_back(0);
  } // 最初の連続する1の個数は０
  int head = 0;
  while (head < N) {
    // headは0(または１）の連続する個数を数えるためのスタート位置
    int tail = head;
    int cnt = 0;
    while ((head + tail) < N && S[tail] == S[head]) {
      tail++;
      cnt++;
    }
    head = tail;
    vec.push_back(cnt);
  }

  if (S.back() == '0') {
    vec.push_back(0);
  } // 最初と最後は1の個数で終わりたい　
  // このときのvecの長さは必ず奇数

  vector<long long> sum((int)vec.size() + 1, 0);

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

  long long ans = 0;

  for (int start = 0; start < (int)vec.size(); start++) {
    long long res = 0;
    int j = start;
    if (j % 2 == 0) { // vec[start]は１の個数
      int goal = min((int)vec.size() - 1, j + 2 * K);
      res = sum[goal + 1] - sum[j];
    }

    else {
      // vec[start]は0の個数
      int goal = min((int)vec.size() - 1, j + 2 * K - 1);
      res = sum[goal + 1] - sum[j - 1];
    }

    ans = max(ans, res);
  }

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

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

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> vec;
  // 0,2,4,,番目に連続する1の個数を、1,3,5,,,番目に連続する0の個数をpush_backする
  if (S[0] == '0') {
    vec.push_back(0);
  } // 最初の連続する1の個数は０
  int head = 0;
  while (head < N) {
    // headは0(または１）の連続する個数を数えるためのスタート位置
    int tail = head;
    int cnt = 0;
    while (tail < N && S[tail] == S[head]) {
      tail++;
      cnt++;
    }
    head = tail;
    vec.push_back(cnt);
  }

  if (S.back() == '0') {
    vec.push_back(0);
  } // 最初と最後は1の個数で終わりたい　
  // このときのvecの長さは必ず奇数

  vector<long long> sum((int)vec.size() + 1, 0);

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

  long long ans = 0;

  for (int start = 0; start < (int)vec.size(); start++) {
    long long res = 0;
    int j = start;
    if (j % 2 == 0) { // vec[start]は１の個数
      int goal = min((int)vec.size() - 1, j + 2 * K);
      res = sum[goal + 1] - sum[j];
    }

    else {
      // vec[start]は0の個数
      int goal = min((int)vec.size() - 1, j + 2 * K - 1);
      res = sum[goal + 1] - sum[j - 1];
    }

    ans = max(ans, res);
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> enc(1, 1);
  for (int i = 1; i < n; ++i) {
    if (s[i - 1] == s[i])
      enc.back()++;
    else
      enc.emplace_back(1);
  }

  ll ans = 0;
  bool startWithZero = (s[0] == '0');
  for (int i = 0; i < enc.size(); ++i) {
    ll score = enc[i];
    int loop_num = (startWithZero == (i & 1)) ? k / 2 * 2 : (k - (k % 2 == 0));
    // cout << "loop times " << loop_num << endl;
    for (int j = 1; j <= loop_num; ++j) {
      if (i - j >= 0)
        score += enc[i - j];
      if (i + j < enc.size())
        score += enc[i + j];
    }
    ans = max(ans, score);
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  return 0;
}

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> enc(1, 1);
  for (int i = 1; i < n; ++i) {
    if (s[i - 1] == s[i])
      enc.back()++;
    else
      enc.emplace_back(1);
  }

  ll ans = 0;
  bool startWithZero = (s[0] == '0');
  int start_i = startWithZero != (k & 1);

  ll sum = 0;
  if (start_i < enc.size())
    sum += enc[start_i];
  for (int i = 1; i <= k; ++i) {
    if (start_i - i >= 0)
      sum += enc[start_i - i];
    if (start_i + i < enc.size())
      sum += enc[start_i + i];
  }
  ans = max(ans, sum);
  // cout << start_i << " : " << sum << endl;

  for (int i = start_i + 2; i < enc.size(); i += 2) {
    if (i - k - 2 >= 0)
      sum -= enc[i - k - 2];
    if (i - k - 1 >= 0)
      sum -= enc[i - k - 1];
    if (i + k - 1 < enc.size())
      sum += enc[i + k - 1];
    if (i + k - 0 < enc.size())
      sum += enc[i + k];
    // cout << i << " : " << sum << endl;
    ans = max(ans, sum);
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  return 0;
}
Problem p03074 (C++)

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

std::vector<int64_t> calc_seq(std::vector<int64_t> &xs) {
  auto seq = std::vector<int64_t>{};
  auto count = 1;
  for (size_t i = 0; i < xs.size() - 1; i++) {
    if (xs[i] == xs[i + 1]) {
      count++;
    } else {
      seq.push_back(count);
      count = 1;
    }
  }
  seq.push_back(count);
  return seq;
}

std::vector<int64_t> form_seq(std::vector<int64_t> &xs) {
  auto seq = calc_seq(xs);
  if (xs[0] == 0) {
    seq.insert(begin(seq), 0);
  }
  if (seq.size() % 2 == 0) {
    seq.push_back(0);
  }
  return seq;
}

int64_t sum(std::vector<int64_t> xs, int64_t i0, int64_t i1) {
  auto val = 0LL;
  for (auto i = i0; i <= i1; i++) {
    val += xs[i];
  }
  return val;
}

void calc(int64_t n, int64_t k, std::vector<int64_t> &xs) {
  auto seq = form_seq(xs);
  auto m = seq.size();
  auto i = 1LL;
  if (i + 2 * k > m) {
    std::cout << n;
    return;
  }
  auto val = sum(seq, i - 1, i - 1 + 2 * k);
  auto biggest = val;
  i += 2;
  while (i + 2 * k <= m) {
    val += sum(seq, i - 1 + 2 * k - 1, i - 1 + 2 * k) -
           sum(seq, i - 1 - 2, i - 1 - 1);
    if (biggest < val) {
      biggest = val;
    }
    i += 2;
  }
  std::cout << biggest;
}

int main() {
  std::string s1, s2;
  std::cin >> s1 >> s2;
  auto n = stoi(s1);
  auto k = stoi(s2);

  std::string s3;
  std::cin >> s3;
  std::vector<int64_t> xs(n);
  for (size_t i = 0; i < n; i++) {
    xs[i] = stoi(s3.substr(i, 1));
  }
  calc(n, k, xs);
}

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

std::vector<int64_t> calc_seq(std::vector<int64_t> &xs) {
  auto seq = std::vector<int64_t>{};
  auto count = 1;
  for (size_t i = 0; i < xs.size() - 1; i++) {
    if (xs[i] == xs[i + 1]) {
      count++;
    } else {
      seq.push_back(count);
      count = 1;
    }
  }
  seq.push_back(count);
  return seq;
}

std::vector<int64_t> form_seq(std::vector<int64_t> &xs) {
  auto seq = calc_seq(xs);
  if (xs[0] == 0) {
    seq.insert(begin(seq), 0);
  }
  if (seq.size() % 2 == 0) {
    seq.push_back(0);
  }
  return seq;
}

int64_t sum(std::vector<int64_t> xs, int64_t i0, int64_t i1) {
  auto val = 0LL;
  for (auto i = i0; i <= i1; i++) {
    val += xs[i];
  }
  return val;
}

void calc(int64_t n, int64_t k, std::vector<int64_t> &xs) {
  auto seq = form_seq(xs);
  auto m = seq.size();
  auto i = 1LL;
  if (i + 2 * k > m) {
    std::cout << n;
    return;
  }
  auto val = sum(seq, i - 1, i - 1 + 2 * k);
  auto biggest = val;
  i += 2;
  while (i + 2 * k <= m) {
    val += seq[i - 1 + 2 * k - 1] + seq[i - 1 + 2 * k] - seq[i - 1 - 2] -
           seq[i - 1 - 1];
    if (biggest < val) {
      biggest = val;
    }
    i += 2;
  }
  std::cout << biggest;
}

int main() {
  std::string s1, s2;
  std::cin >> s1 >> s2;
  auto n = stoi(s1);
  auto k = stoi(s2);

  std::string s3;
  std::cin >> s3;
  std::vector<int64_t> xs(n);
  for (size_t i = 0; i < n; i++) {
    xs[i] = stoi(s3.substr(i, 1));
  }
  calc(n, k, xs);
}
Problem p03074 (C++)

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

int n, k;
string s;
P p[505];

int main(void) {
  scanf("%d%d", &n, &k);
  cin >> s;
  int prev = (s[0] - '0');
  int cnt = 0;
  int sz = 0;
  for (int i = 0; i < n; i++) {
    if (prev != (s[i] - '0')) {
      p[sz++] = P(prev, cnt);
      cnt = 0;
    }
    cnt++;
    prev = (s[i] - '0');
  }
  p[sz++] = P(prev, cnt);
  int l = 0;
  int zero = 0;
  int sum = 0;
  int ans = 0;
  for (int i = 0; i < sz; i++) {
    // printf("%d %d\n",p[i].first,p[i].second);
    if (p[i].first == 0) {
      zero++;
      while (zero > k) {
        sum -= p[l].second;
        zero -= 1 - p[l].first;
        l++;
      }
    }
    sum += p[i].second;
    ans = max(ans, sum);
  }
  printf("%d\n", ans);
  return 0;
}


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

int n, k;
string s;
P p[100005];

int main(void) {
  scanf("%d%d", &n, &k);
  cin >> s;
  int prev = (s[0] - '0');
  int cnt = 0;
  int sz = 0;
  for (int i = 0; i < n; i++) {
    if (prev != (s[i] - '0')) {
      p[sz++] = P(prev, cnt);
      cnt = 0;
    }
    cnt++;
    prev = (s[i] - '0');
  }
  p[sz++] = P(prev, cnt);
  int l = 0;
  int zero = 0;
  int sum = 0;
  int ans = 0;
  for (int i = 0; i < sz; i++) {
    // printf("%d %d\n",p[i].first,p[i].second);
    if (p[i].first == 0) {
      zero++;
      while (zero > k) {
        sum -= p[l].second;
        zero -= 1 - p[l].first;
        l++;
      }
    }
    sum += p[i].second;
    ans = max(ans, sum);
  }
  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;
  string str;
  cin >> N >> K >> str;

  vector<int> vec;
  if (str[0] == '0')
    vec.push_back(0);
  int cnt = 1;
  for (int i = 1; i < N; i++) {
    if (str[i - 1] == str[i]) {
      cnt++;
    } else {
      vec.push_back(cnt);
      cnt = 1;
    }
  }
  vec.push_back(cnt);
  auto M = (int)vec.size();

  vector<int> S(M, 0);
  for (int i = 0; i < M; i++)
    S[i + 1] = S[i] + vec[i];

  int ret = 0;
  for (int i = 0; i < M; i += 2) {
    ret = max(ret, S[min(M, i + 2 * K + 1)] - S[i]);
  }
  cout << ret << endl;

  return 0;
}

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

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

  vector<int> vec;
  if (str[0] == '0')
    vec.push_back(0);
  int cnt = 1;
  for (int i = 1; i < N; i++) {
    if (str[i - 1] == str[i]) {
      cnt++;
    } else {
      vec.push_back(cnt);
      cnt = 1;
    }
  }
  vec.push_back(cnt);
  auto M = (int)vec.size();

  vector<int> S(M + 1, 0);
  for (int i = 0; i < M; i++)
    S[i + 1] = S[i] + vec[i];

  int ret = 0;
  for (int i = 0; i < M; i += 2) {
    ret = max(ret, S[min(M, i + 2 * K + 1)] - S[i]);
  }
  cout << ret << endl;

  return 0;
}
Problem p03074 (Python)

Buggy submission (status: Runtime Error):
from itertools import groupby, accumulate

# 入力
N, K = map(int, input().split())
S = input()

# 連続する0または1の数に関する累積和
A = list(accumulate(len(list(seq)) for _, seq in groupby(S))) + [N]
m = 1 if S[0] == "0" else 0
# 累積和を用いて解を求める
ans = (
    max(
        A[2 * K - 1],
        A[-1] - A[-(2 * K)],
        max((a - b for a, b in zip(A[(2 * K + m) :: 2], ([0] + A)[m::2])), default=0),
    )
    if m < len(A)
    else A[-1]
)

# 出力
print(ans)


Fixed submission:
from itertools import groupby, accumulate

# 入力
N, K = map(int, input().split())
S = input()

# 連続する0または1の数に関する累積和
A = list(accumulate(len(list(seq)) for _, seq in groupby(S))) + [N]
m = 1 if S[0] == "0" else 0
# 累積和を用いて解を求める
ans = (
    max(
        A[min(2 * K - 1, len(A) - 1)],
        A[-1] - A[max(-(2 * K), -len(A))],
        max((a - b for a, b in zip(A[(2 * K + m) :: 2], ([0] + A)[m::2])), default=0),
    )
    if m < len(A)
    else A[-1]
)

# 出力
print(ans)
Problem p03074 (C++)

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

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

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

  rep(i, N) {
    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;

  vector<int> sum(Nums.size() + 1);
  rep(i, sum.size()) { sum[i + 1] = sum[i] + Nums[i]; }

  for (int i = 0; i < (int)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;

  return 0;
}

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

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

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

  rep(i, N) {
    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;

  vector<int> sum(Nums.size() + 1);
  rep(i, Nums.size()) { sum[i + 1] = sum[i] + Nums[i]; }

  for (int i = 0; i < (int)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;

  return 0;
}
Problem p03074 (C++)

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

using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define INF (1e9)

int N, K;

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

  string S;
  cin >> S;

  vi change(0);
  if (S[0] == '0')
    change.push_back(0);
  int last = 'x';
  for (int i = 0; i < N; i++) {
    if (S[i] == last)
      continue;
    change.push_back(i);
    last = S[i];
  }
  change.push_back(N);

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

  if (change.size() == 2)
    ans = N;

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


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

using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define INF (1e9)

int N, K;

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

  string S;
  cin >> S;

  vi change(0);
  if (S[0] == '0')
    change.push_back(0);
  int last = 'x';
  for (int i = 0; i < N; i++) {
    if (S[i] == last)
      continue;
    change.push_back(i);
    last = S[i];
  }
  change.push_back(N);

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

  if (change.size() == 2)
    ans = N;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> Nums;
  int now = 1;
  int cnt = 0;
  rep(i, n) {
    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;
  for (int i = 0; i < Nums.size(); i += 2) {

    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int tmp = sum[right] - sum[left];

    ans = max(tmp, ans);
  }

  cout << ans;

  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> Nums;
  int now = 1;
  int cnt = 0;
  rep(i, n) {
    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;
  for (int i = 0; i < Nums.size(); i += 2) {

    int left = i;
    int right = min(i + Add, (int)Nums.size());
    int tmp = sum[right] - sum[left];

    ans = max(tmp, ans);
  }

  cout << ans;

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

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, K;
  cin >> n >> K;
  string s;
  cin >> s;
  vector<P> v;
  for (char c : s) {
    int tar = (c - '0');
    if (v.empty()) {
      v.emplace_back(tar, 1);
      continue;
    }

    if (v.back().first == tar) {
      v.back().second++;
    } else {
      v.emplace_back(tar, 1);
    }
  }

  int sum = 0;
  int cnt0 = 0;
  int idx = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i].first == 0) {
      cnt0++;
      if (cnt0 > K) {
        sum -= v[idx].second;
        idx++;
        if (v[idx - 1].first == 1) {
          sum -= v[idx].second;
          idx++;
        }
        cnt0--;
      }
    }
    sum += v[i].second;
    ans = max(ans, sum);
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, K;
  cin >> n >> K;
  string s;
  cin >> s;
  vector<P> v;
  for (char c : s) {
    int tar = (c - '0');
    if (v.empty()) {
      v.emplace_back(tar, 1);
      continue;
    }

    if (v.back().first == tar) {
      v.back().second++;
    } else {
      v.emplace_back(tar, 1);
    }
  }

  int sum = 0;
  int cnt0 = 0;
  int idx = 0;
  int ans = 0;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].first == 0) {
      cnt0++;
      if (cnt0 > K) {
        sum -= v[idx].second;
        idx++;
        if (v[idx - 1].first == 1) {
          sum -= v[idx].second;
          idx++;
        }
        cnt0--;
      }
    }
    sum += v[i].second;
    ans = max(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
int main() {
  int n, k;
  std::cin >> n >> k;
  std::string s;
  std::cin >> s;
  s += 'E';
  std::vector<int> a;
  int i = 0;
  while (i < n) {
    int x = 0;
    while (s[i] == '1') {
      x++;
      i++;
    }
    a.push_back(x);
    int y = 0;
    while (s[i] == '0') {
      y++;
      i++;
    }
    a.push_back(y);
  }
  a.push_back(0);
  k = std::min(n, k * 2 + 1);
  int ans = 0;
  for (i = 0; i <= n - k; i += 2) {
    int x = std::accumulate(&a[i], &a[i + k], 0);
    ans = std::max(ans, x);
  }
  std::cout << ans;
}

Fixed submission:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
int main() {
  int n, k;
  std::cin >> n >> k;
  std::string s;
  std::cin >> s;
  s += 'E';
  std::vector<int> a;
  int i = 0;
  while (i < n) {
    int x = 0;
    while (s[i] == '1') {
      x++;
      i++;
    }
    a.push_back(x);
    int y = 0;
    while (s[i] == '0') {
      y++;
      i++;
    }
    a.push_back(y);
  }
  a.push_back(0);
  n = a.size();
  k = std::min(n, k * 2 + 1);
  int ans = 0;
  for (i = 0; i <= n - k; i += 2) {
    int x = std::accumulate(&a[i], &a[i + k], 0);
    ans = std::max(ans, x);
  }
  std::cout << ans;
}