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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

Int N, K;
vector<Int> A(200010, 0), S(200010, 0);

Int id(Int i) {
  Int res = S[i] - i;
  while (res < 0)
    res += K;
  res %= K;
  return res;
}

int main() {
  cin >> N >> K;
  rep1(i, N) {
    cin >> A[i];
    S[i] = S[i - 1] + A[i];
    S[i] %= K;
  }

  map<Int, Int> M;

  Int ans = 0;
  rep1(j, N) {
    Int R = j - 1, L = j - K;
    M[id(R)]++;
    if (L >= 0) {
      M[id(L)]--;
    }

    ans += M[id(j)];
  }

  cout << ans << endl;
}

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

Int N, K;
vector<Int> A(200010, 0), S(200010, 0);

Int id(Int i) {
  Int res = S[i] - i;
  if (res >= 0) {
    res %= K;
  } else {
    res = (K - (-res) % K) % K;
  }
  return res;
}

int main() {
  cin >> N >> K;
  rep1(i, N) {
    cin >> A[i];
    S[i] = S[i - 1] + A[i];
    S[i] %= K;
  }

  map<Int, Int> M;

  Int ans = 0;
  rep1(j, N) {
    Int R = j - 1, L = j - K;
    M[id(R)]++;
    if (L >= 0) {
      M[id(L)]--;
    }

    ans += M[id(j)];
  }

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

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

typedef long long ll;

const int maxn = 1e5 + 100;

ll s[maxn];

ll a[maxn];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s[i] = (s[i - 1] + a[i]);
  }
  for (int i = 1; i <= n; i++) {
    s[i] = (s[i] - i) % k;
  }

  map<ll, ll> cnt;
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += cnt[s[i]];
    cnt[s[i]]++;
    if (i - k + 1 >= 0)
      cnt[s[i - k + 1]]--;
  }

  printf("%lld\n", ans);

  return 0;
}

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

typedef long long ll;

const int maxn = 2e5 + 100;

ll s[maxn];

ll a[maxn];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s[i] = (s[i - 1] + a[i]);
  }
  for (int i = 1; i <= n; i++) {
    s[i] = (s[i] - i) % k;
  }

  map<ll, ll> cnt;
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += cnt[s[i]];
    cnt[s[i]]++;
    if (i - k + 1 >= 0)
      cnt[s[i - k + 1]]--;
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
ll MOD = 1e9 + 7;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<ll> S(N + 1);
  S[0] = 0;
  rep(i, N) S[i + 1] = S[i] + A[i];
  rep(i, N + 1) S[i] = (S[i] - i + K) % K;

  map<ll, ll> mp;

  ll ans = 0;

  rep(i, K) mp[S[i]]++;

  ans += mp[S[0]] - 1;
  for (ll i = 1; i <= N; ++i) {
    mp[S[i - 1]]--;
    if (i + K - 1 <= N)
      mp[S[i + K - 1]]++;
    ans += mp[S[i]] - 1;
  }

  print(ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
ll MOD = 1e9 + 7;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<ll> S(N + 1);
  S[0] = 0;
  rep(i, N) S[i + 1] = S[i] + A[i];
  rep(i, N + 1) S[i] = (S[i] - i + K) % K;

  map<ll, ll> mp;

  ll ans = 0;

  rep(i, min(K, N + 1)) mp[S[i]]++;

  ans += mp[S[0]] - 1;
  for (ll i = 1; i <= N; ++i) {
    mp[S[i - 1]]--;
    if (i + K - 1 <= N)
      mp[S[i + K - 1]]++;
    ans += mp[S[i]] - 1;
  }

  print(ans);

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  }
}
#define all(v) v.begin(), v.end()
#define inputv(v, n)                                                           \
  vl v;                                                                        \
  rep(i, n) {                                                                  \
    ll x;                                                                      \
    cin >> x;                                                                  \
    v.push_back(x);                                                            \
  }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
vl A, B(MAX_N, 0), dp(MOD, 0);
int main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a;
    a--;
    A.push_back(a);
  }

  rep(i, n) {
    B[i + 1] = B[i];
    B[i + 1] += A[i];
  }

  ans = 0;
  auto ite1 = A.begin(), ite2 = A.begin();
  while (ite2 != A.end()) {
    dp[B[ite2 - A.begin()] % k]++;
    ite2++;
    while (ite2 - ite1 >= k) {
      dp[B[ite1 - A.begin()] % k]--;
      ite1++;
    }
    ans += dp[B[ite2 - A.begin()] % k];
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  }
}
#define all(v) v.begin(), v.end()
#define inputv(v, n)                                                           \
  vl v;                                                                        \
  rep(i, n) {                                                                  \
    ll x;                                                                      \
    cin >> x;                                                                  \
    v.push_back(x);                                                            \
  }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
vl A, B(MAX_N, 0);
map<ll, ll> dp;
int main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a;
    a--;
    A.push_back(a);
  }

  rep(i, n) {
    B[i + 1] = B[i];
    B[i + 1] += A[i];
  }

  ans = 0;
  auto ite1 = A.begin(), ite2 = A.begin();
  while (ite2 != A.end()) {
    dp[B[ite2 - A.begin()] % k]++;
    ite2++;
    while (ite2 - ite1 >= k) {
      dp[B[ite1 - A.begin()] % k]--;
      ite1++;
    }
    ans += dp[B[ite2 - A.begin()] % k];
  }
  cout << ans << endl;
}
Problem p02852 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N, M;
  cin >> N >> M;
  string S;
  cin >> S;
  vector<ll> ans;
  ll now = N;
  while (now != 0) {
    ll t = -1;
    rrep(i, now - 1, now - M) {
      if (S[i] == '0') {
        t = i;
      }
    }
    if (t == -1) {
      cout << -1 << '\n';
      return;
    } else {
      ans.push_back(now - t);
      now = t;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, ans.size()) { cout << ans[i] << ' '; }
  cout << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N, M;
  cin >> N >> M;
  string S;
  cin >> S;
  vector<ll> ans;
  ll now = N;
  while (now != 0) {
    ll t = -1;
    rrep(i, now - 1, max(0LL, now - M)) {
      if (S[i] == '0') {
        t = i;
      }
    }
    if (t == -1) {
      cout << -1 << '\n';
      return;
    } else {
      ans.push_back(now - t);
      now = t;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, ans.size()) { cout << ans[i] << ' '; }
  cout << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

int main() {
  int N, M;
  string S, T;
  cin >> N >> M >> T;
  rep(i, N + 1) S[i] = T[N - i];
  vi s(N + 2);
  rep(i, N + 1) s[i + 1] = s[i] + (int)(S[i] - '0');

  int now = 0;
  vi ans;
  bool flag = true;
  while (now < N) {
    if (now + M >= N) {
      ans.push_back(N - now);
      break;
    }
    if (S[now + M] == 0) {
      ans.push_back(M);
      continue;
    }
    int l = now - 1, r = now + M;
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (s[now + M + 1] - s[m + 1] == now + M - m)
        r = m;
      else
        l = m;
    }
    if (r == now) {
      flag = false;
      break;
    }
    ans.push_back(r - now);
    now = r;
  }

  if (flag) {
    reverse(ans.begin(), ans.end());
    for (int x : ans)
      cout << x << ' ';
    cout << endl;
  } else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

int main() {
  int N, M;
  string S;
  cin >> N >> M >> S;
  reverse(S.begin(), S.end());
  vi s(N + 2);
  rep(i, N + 1) s[i + 1] = s[i] + (int)(S[i] - '0');

  int now = 0;
  vi ans;
  bool flag = true;
  while (now < N) {
    if (now + M >= N) {
      ans.push_back(N - now);
      break;
    }
    if (S[now + M] == 0) {
      ans.push_back(M);
      continue;
    }
    int l = now - 1, r = now + M;
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (s[now + M + 1] - s[m + 1] == now + M - m)
        r = m;
      else
        l = m;
    }
    if (r == now) {
      flag = false;
      break;
    }
    ans.push_back(r - now);
    now = r;
  }

  if (flag) {
    reverse(ans.begin(), ans.end());
    for (int x : ans)
      cout << x << ' ';
    cout << endl;
  } else
    cout << -1 << endl;
}
Problem p02852 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n + 1; i++) {
    if (s[i] == '1') {
      cnt++;
    } else {
      cnt = 0;
    }
    if (cnt > m) {
      cout << -1 << endl;
      return 0;
    }
  }
  reverse(s.begin(), s.end());
  int t = 0;
  vector<int> ans;
  int r = 0;
  while (t < n) {
    int num = t;
    for (int i = num + 1; i <= num + m; i++) {
      if (s[i] == '0') {
        t = i;
      }
    }
    ans.push_back(t - num);
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i] << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n + 1; i++) {
    if (s[i] == '1') {
      cnt++;
    } else {
      cnt = 0;
    }
    if (cnt >= m) {
      cout << -1 << endl;
      return 0;
    }
  }
  reverse(s.begin(), s.end());
  int t = 0;
  vector<int> ans;
  int r = 0;
  while (t < n) {
    int num = t;
    for (int i = num + 1; i <= num + m; i++) {
      if (s[i] == '0') {
        t = i;
      }
    }
    ans.push_back(t - num);
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i] << " ";
  }
  cout << endl;
}
Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int n;
pair<int, int> seg[maxn], ans[maxn];
void upd(int p, int v) {
  for (seg[p += n] = {v, p}; p > 1; p >>= 1)
    seg[p >> 1] = min(seg[p], seg[p ^ 1]);
}
pair<int, int> qry(int l, int r) {
  pair<int, int> ans = {1 << 30, 1 << 30};
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      ans = min(ans, seg[l++]);
    if (r & 1)
      ans = min(ans, seg[--r]);
  }
  return ans;
}
int main() {
  int m;
  cin >> n >> m;
  string s;
  cin >> s;
  n++;
  upd(n - 1, 0);
  for (int i = n - 2; i >= 0; i--) {
    pair<int, int> b = {1 << 30, 1 << 30};
    if (s[i] == '0') {
      b = qry(i + 1, min(i + m + 1, n));
    }
    b.first++;
    upd(i, b.first);
    ans[i] = b;
  }
  if (ans[0].first >= (1 << 30))
    cout << -1 << endl;
  else {
    int p = 0;
    while (p != n - 1) {
      cout << ans[p].second - p << " ";
      p = ans[p].second;
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300100;
int n;
pair<int, int> seg[maxn], ans[maxn];
void upd(int p, int v) {
  for (seg[p += n] = {v, p}; p > 1; p >>= 1)
    seg[p >> 1] = min(seg[p], seg[p ^ 1]);
}
pair<int, int> qry(int l, int r) {
  pair<int, int> ans = {1 << 30, 1 << 30};
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      ans = min(ans, seg[l++]);
    if (r & 1)
      ans = min(ans, seg[--r]);
  }
  return ans;
}
int main() {
  int m;
  cin >> n >> m;
  string s;
  cin >> s;
  n++;
  upd(n - 1, 0);
  for (int i = n - 2; i >= 0; i--) {
    pair<int, int> b = {1 << 30, 1 << 30};
    if (s[i] == '0') {
      b = qry(i + 1, min(i + m + 1, n));
    }
    b.first++;
    upd(i, b.first);
    ans[i] = b;
  }
  if (ans[0].first >= (1 << 30))
    cout << -1 << endl;
  else {
    int p = 0;
    while (p != n - 1) {
      cout << ans[p].second - p << " ";
      p = ans[p].second;
    }
    cout << endl;
  }
  return 0;
}
Problem p02852 (C++)

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

using namespace std;

#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second

priority_queue<pii> Q;
int ans[100010];

int main() {

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

  // cout << n << ' ' << m << ' ' << s << endl;

  fill(ans, ans + n + 1, -1);

  for (int i = n - 1, j = 1; i >= n - m; i--, j++) {
    if (s[i] == '1')
      continue;
    Q.push({-1, -i});
    ans[i] = j;
  }

  for (int i = n - m - 1; i >= 0; i--) {
    if (s[i] == '1')
      continue;

    while (!Q.empty()) {
      pii u = Q.top();

      // cout << i << ' ' << u.ff << ' ' << u.ss << endl;

      if (i + m < -u.ss) {
        Q.pop();
        continue;
      } else {
        Q.push({u.ff - 1, -i});
        ans[i] = -u.ss - i;
        break;
      }
    }
  }

  if (ans[0] == -1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < n; i += ans[i]) {
    cout << ans[i] << ' ';
  }

  return 0;
}

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

using namespace std;

#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second

priority_queue<pii> Q;
int ans[100010];

int main() {

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

  // cout << n << ' ' << m << ' ' << s << endl;

  fill(ans, ans + n + 1, -1);

  for (int i = n - 1, j = 1; i >= n - m; i--, j++) {
    if (i < 0)
      break;
    if (s[i] == '1')
      continue;
    Q.push({-1, -i});
    ans[i] = j;
  }

  for (int i = n - m - 1; i >= 0; i--) {
    if (s[i] == '1')
      continue;

    while (!Q.empty()) {
      pii u = Q.top();

      // cout << i << ' ' << u.ff << ' ' << u.ss << endl;

      if (i + m < -u.ss) {
        Q.pop();
        continue;
      } else {
        Q.push({u.ff - 1, -i});
        ans[i] = -u.ss - i;
        break;
      }
    }
  }

  if (ans[0] == -1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < n; i += ans[i]) {
    cout << ans[i] << ' ';
  }

  return 0;
}
Problem p02852 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int n, m;
string s;

vector<int> v;
bool func(int now) {
  if (now == 0) {
    return true;
  }
  if (now < 0) {
    return false;
  }
  for (int i = m; i >= 1; i--) {
    int next = now - i;
    if (s[next] == '0') {
      v.push_back(next);
      if (func(next))
        return true;
      v.pop_back();
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  cin >> s;

  if (!func(n)) {
    cout << -1 << endl;
    return 0;
  }
  reverse(v.begin(), v.end());
  for (int i = 1; i <= v.size(); i++) {
    if (i == v.size())
      cout << n - v[i - 1] << " ";
    else
      cout << v[i] - v[i - 1] << " ";
  }
  cout << endl;
  return 0;
}


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

int n, m;
string s;

vector<int> v;
bool func(int now) {
  if (now == 0) {
    return true;
  }
  if (now < 0) {
    return false;
  }
  for (int i = m; i >= 1; i--) {
    int next = now - i;
    if (s[next] == '0') {
      v.push_back(next);
      if (func(next))
        return true;
      v.pop_back();
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  cin >> s;
  int now = n;
  while (now > 0) {
    bool ok = false;
    for (int i = m; i >= 1; i--) {
      int next = now - i;
      if (next < 0)
        continue;
      if (s[next] == '1')
        continue;
      v.push_back(next);
      now = next;
      ok = true;
      break;
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
  }
  reverse(v.begin(), v.end());
  for (int i = 1; i <= v.size(); i++) {
    if (i == v.size())
      cout << n - v[i - 1] << " ";
    else
      cout << v[i] - v[i - 1] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02852 (C++)

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

const int INF = 1 << 30;

int main() {
  int n, m;
  string s;
  cin >> n >> m >> s;
  vector<int> dp(n + 1, INF);
  dp[n] = 0;
  queue<int> q;
  q.push(0);
  for (int i = n - 1; i >= 0; --i) {
    while (1) {
      if (q.size() == 0) {
        puts("-1");
        return 0;
      }
      if (q.front() != INF && q.size() <= m)
        break;
      q.pop();
    }
    if (s[i] == '0')
      dp[i] = q.front() + 1;
    q.push(dp[i]);
  }

  vector<int> ans;
  int x = 0;
  int rest = dp[0];
  while (x < n) {
    --rest;
    int i = 1;
    while (dp[x + i] != rest)
      ++i;
    ans.push_back(i);
    x += i;
  }
  rep(i, n) { printf("%d%c", ans[i], i == n - 1 ? '\n' : ' '); }
  return 0;
}

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

const int INF = 1 << 30;

int main() {
  int n, m;
  string s;
  cin >> n >> m >> s;
  vector<int> dp(n + 1, INF);
  dp[n] = 0;
  queue<int> q;
  q.push(0);
  for (int i = n - 1; i >= 0; --i) {
    while (1) {
      if (q.size() == 0) {
        puts("-1");
        return 0;
      }
      if (q.front() != INF && q.size() <= m)
        break;
      q.pop();
    }
    if (s[i] == '0')
      dp[i] = q.front() + 1;
    q.push(dp[i]);
  }

  vector<int> ans;
  int x = 0;
  int rest = dp[0];
  while (x < n) {
    --rest;
    int i = 1;
    while (dp[x + i] != rest)
      ++i;
    ans.push_back(i);
    x += i;
  }
  rep(i, ans.size()) {
    printf("%d%c", ans[i], i == int(ans.size()) - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p02852 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  reverse(ALL(s));
  vector<bool> dp(n + m + 10, false);
  dp[n] = true;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      dp[i] = false;
      continue;
    } else {
      int sup = min(n - i, m);
      rep1(j, sup) { dp[i] = dp[i] || dp[i + j]; }
    }
  }
  if (!dp[0]) {
    cout << -1 << "\n";
  } else {
    vector<int> res;
    rep(i, n) {
      for (int j = m; j > 0; --j) {
        if (dp[i + j]) {
          i += j - 1;
          res.push_back(j);
          break;
        }
      }
    }
    int num = res.size();
    rep(i, num) { cout << res[num - i - 1] << " "; }
    cout << endl;
  }

  return 0;
}


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

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  reverse(ALL(s));
  vector<bool> dp(n + m + 10, false);
  dp[n] = true;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      dp[i] = false;
      continue;
    } else {
      int sup = min(n - i, m);
      rep1(j, sup) {
        dp[i] = dp[i] || dp[i + j];
        if (dp[i])
          break;
      }
    }
  }
  if (!dp[0]) {
    cout << -1 << "\n";
  } else {
    vector<int> res;
    rep(i, n) {
      for (int j = m; j > 0; --j) {
        if (dp[i + j]) {
          i += j - 1;
          res.push_back(j);
          break;
        }
      }
    }
    int num = res.size();
    rep(i, num) { cout << res[num - i - 1] << " "; }
    cout << endl;
  }

  return 0;
}
Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  int now = n;
  vector<int> ans;
  while (now != 0) {
    bool end = true;
    for (int j = m; j >= 1; j--) {
      if (s[now - j] == '0') {
        now -= j;
        end = false;
        ans.push_back(j);
        break;
      }
    }
    if (end) {
      cout << -1 << endl;
      return 0;
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto p : ans)
    cout << p << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  int now = n;
  vector<int> ans;
  while (now != 0) {
    bool end = true;
    for (int j = m; j >= 1; j--) {
      if (now < j)
        continue;
      if (s[now - j] == '0') {
        now -= j;
        end = false;
        ans.push_back(j);
        break;
      }
    }
    if (end) {
      cout << -1 << endl;
      return 0;
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto p : ans)
    cout << p << " ";
  cout << endl;
  return 0;
}
Problem p02852 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fix20 cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000007

int main() {
  int n, m;
  string s;
  cin >> n >> m >> s;
  vector<int> step(n + 1, 1e9);
  step.at(n) = 0;
  int pos = n;
  int d = 1;
  while (pos > 0) {
    int tmp = pos;
    rep(i, m) {
      tmp--;
      if (s.at(tmp) == '1')
        continue;
      step.at(tmp) = d;
      pos = tmp;
      if (tmp == 0) {
        pos = 0;
        break;
      }
    }
    d++;
  }
  // vector<int> path(1,0);
  int p = step.at(0);
  int id = 0;
  for (int i = 1; i <= n; i++) {
    if (s.at(i) == '1')
      continue;
    if (step.at(i) == p)
      continue;
    cout << i - id << " ";
    p = step.at(i);
    id = i;
  }
  cout << endl;
  // rep(i,n+1) cout << step.at(i) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fix20 cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000007

int main() {
  int n, m;
  string s;
  cin >> n >> m >> s;
  int cnt = 0;
  rep(i, n + 1) {
    if (s.at(i) == '1')
      cnt++;
    else
      cnt = 0;
    if (cnt == m) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<int> step(n + 1, 1e9);
  step.at(n) = 0;
  int pos = n;
  int d = 1;
  while (pos > 0) {
    int tmp = pos;
    rep(i, m) {
      tmp--;
      if (s.at(tmp) == '1')
        continue;
      step.at(tmp) = d;
      pos = tmp;
      if (tmp == 0) {
        pos = 0;
        break;
      }
    }
    d++;
  }
  // vector<int> path(1,0);
  int p = step.at(0);
  int id = 0;
  for (int i = 1; i <= n; i++) {
    if (s.at(i) == '1')
      continue;
    if (step.at(i) == p)
      continue;
    cout << i - id << " ";
    p = step.at(i);
    id = i;
  }
  cout << endl;
  // rep(i,n+1) cout << step.at(i) << endl;
}
Problem p02852 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mid (l + r) / 2
#define L (x << 1)
#define R (L + 1)
pair<int, int> tree[1000000];
string s;
void build(int x, int l, int r) {
  if (l == r) {
    tree[x] = {1e9, l};
  } else {
    build(L, l, mid);
    build(R, mid + 1, r);
    tree[x] = min(tree[L], tree[R]);
  }
}
void upd(int x, int l, int r, int v, int val) {
  if (v > r || v < l)
    return;
  if (l == r) {
    tree[x] = {val, l};
  } else {
    upd(L, l, mid, v, val);
    upd(R, mid + 1, r, v, val);
    tree[x] = min(tree[L], tree[R]);
  }
}
pair<int, int> query(int x, int l, int r, int s, int e) {
  if (l > e || r < s)
    return {1e9, 0};
  if (l >= s && r <= e)
    return tree[x];
  return min(query(L, l, mid, s, e), query(R, mid + 1, r, s, e));
}
int dp[1000000];
int back[1000000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  cin >> s;
  if (s[n] == '1') {
    cout << -1 << endl;
    return 0;
  }
  build(1, 0, n);
  dp[n] = 0;
  upd(1, 0, n, n, 0);
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = 1e9;
    if (s[i] == '1')
      continue;
    int r = min(n, i + m);
    auto q = query(1, 0, n, i + 1, r);
    dp[i] = q.first + 1;
    back[i] = q.second;
    upd(1, 0, n, i, dp[i]);
  }
  if (dp[0] == 1e9) {
    cout << -1 << endl;
  } else {
    int v = 0;
    vector<int> path;
    while (v != n) {
      path.push_back(back[v] - v);
      v = back[v];
    }
    for (auto x : path)
      cout << x << ' ';
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mid (l + r) / 2
#define L (x << 1)
#define R (L + 1)
pair<int, int> tree[1000000];
string s;
void build(int x, int l, int r) {
  if (l == r) {
    tree[x] = {1e9, l};
  } else {
    build(L, l, mid);
    build(R, mid + 1, r);
    tree[x] = min(tree[L], tree[R]);
  }
}
void upd(int x, int l, int r, int v, int val) {
  if (v > r || v < l)
    return;
  if (l == r) {
    tree[x] = {val, l};
  } else {
    upd(L, l, mid, v, val);
    upd(R, mid + 1, r, v, val);
    tree[x] = min(tree[L], tree[R]);
  }
}
pair<int, int> query(int x, int l, int r, int s, int e) {
  if (l > e || r < s)
    return {1e9, 0};
  if (l >= s && r <= e)
    return tree[x];
  return min(query(L, l, mid, s, e), query(R, mid + 1, r, s, e));
}
int dp[1000000];
int back[1000000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  cin >> s;
  if (s[n] == '1') {
    cout << -1 << endl;
    return 0;
  }
  build(1, 0, n);
  dp[n] = 0;
  upd(1, 0, n, n, 0);
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = 1e9;
    if (s[i] == '1')
      continue;
    int r = min(n, i + m);
    auto q = query(1, 0, n, i + 1, r);
    dp[i] = q.first + 1;
    back[i] = q.second;
    upd(1, 0, n, i, dp[i]);
  }
  if (dp[0] >= 1e9) {
    cout << -1 << endl;
  } else {
    int v = 0;
    vector<int> path;
    while (v != n) {
      path.push_back(back[v] - v);
      v = back[v];
    }
    for (auto x : path)
      cout << x << ' ';
    cout << endl;
  }
}
Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

  int N, M;
  cin >> N >> M;
  string S;
  cin >> S;

  int dp[100002] = {};
  int mae = 0;
  int kazu = 0;
  while (mae <= N) {
    int tugi = mae + M;
    chmin(tugi, N);
    kazu++;
    int saigo = mae;
    for (int i = mae + 1; i <= tugi; i++) {
      if (S[i] == '0') {
        dp[i] = kazu;
        saigo = i;
      }
    }
    if (mae == saigo)
      break;
    mae = saigo;
  }

  if (dp[N] == 0) {
    co(-1);
    return 0;
  }
  vector<int> kotae;
  for (int i = N; i > 0;) {
    int k = dp[i];
    for (int j = i - M; j <= i; j++) {
      if (dp[j] == k - 1) {
        kotae.pb(i - j);
        i = j;
        break;
      }
    }
  }

  for (auto itr = kotae.rbegin(); itr != kotae.rend(); itr++) {
    cosp(*itr);
  }

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

  int N, M;
  cin >> N >> M;
  string S;
  cin >> S;

  int dp[100002] = {};
  int mae = 0;
  int kazu = 0;
  while (mae <= N) {
    int tugi = mae + M;
    chmin(tugi, N);
    kazu++;
    int saigo = mae;
    for (int i = mae + 1; i <= tugi; i++) {
      if (S[i] == '0') {
        dp[i] = kazu;
        saigo = i;
      }
    }
    if (mae == saigo)
      break;
    mae = saigo;
  }

  if (dp[N] == 0) {
    co(-1);
    return 0;
  }
  vector<int> kotae;
  for (int i = N; i > 0;) {
    int k = dp[i];
    for (int j = max(0, i - M); j <= i; j++) {
      if (dp[j] == k - 1) {
        kotae.pb(i - j);
        i = j;
        break;
      }
    }
  }

  for (auto itr = kotae.rbegin(); itr != kotae.rend(); itr++) {
    cosp(*itr);
  }

  Would you please return 0;
}
Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; ++i)
#define all(a) (a).begin(), (a).end()

int N, M;
string S;

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

  stack<int> m;
  int i = N;
  while (i != 0) {
    bool found = false;
    for (int j = i - M; j < i; ++j) {
      if (S[j] == '0') {
        m.push(i - j);
        i = j;
        found = true;
        break;
      }
    }
    if (!found)
      break;
  }

  if (i != 0) {
    cout << -1 << endl;
    return 0;
  }

  cout << m.top();
  m.pop();
  while (!m.empty()) {
    cout << " " << m.top();
    m.pop();
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; ++i)
#define all(a) (a).begin(), (a).end()

int N, M;
string S;

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

  stack<int> m;
  int i = N;
  while (i != 0) {
    bool found = false;
    for (int j = i - M; j < i; ++j) {
      if (j >= 0 && S[j] == '0') {
        m.push(i - j);
        i = j;
        found = true;
        break;
      }
    }
    if (!found)
      break;
  }

  if (i != 0) {
    cout << -1 << endl;
    return 0;
  }

  cout << m.top();
  m.pop();
  while (!m.empty()) {
    cout << " " << m.top();
    m.pop();
  }
  cout << endl;

  return 0;
}
Problem p02852 (C++)

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

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)

typedef pair<int, int> pint;

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front

#define MOD (1000 * 1000 * 1000 + 7)

signed main() {
  int n, m;
  string s;

  cin >> n >> m;
  cin >> s;

  vector<int> ok_list;
  {
    int one_max = 0;
    int now = 0;
    REP(i, n + 1) {
      if (s[i] == '1') {
        now++;
        one_max = max(one_max, now);
      } else {
        now = 0;
        ok_list.pb(i);
      }
    }

    if (one_max >= m) {
      cout << -1 << endl;
      return -1;
    }
  }

  int now = n;
  vector<int> pos;
  pos.pb(now);
  while (now != 0) {
    decltype(ok_list)::iterator it =
        std::upper_bound(ok_list.begin(), ok_list.end(), now - m - 1);
    now = *it;
    pos.pb(now);
  }

  int _size = SZ(pos);
  RREP(i, _size - 1) {
    cout << (pos[i] - pos[i + 1]);
    if (i != 0)
      cout << " ";
  }
  cout << endl;

  return 0;
}

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

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)

typedef pair<int, int> pint;

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front

#define MOD (1000 * 1000 * 1000 + 7)

signed main() {
  int n, m;
  string s;

  cin >> n >> m;
  cin >> s;

  vector<int> ok_list;
  {
    int one_max = 0;
    int now = 0;
    REP(i, n + 1) {
      if (s[i] == '1') {
        now++;
        one_max = max(one_max, now);
      } else {
        now = 0;
        ok_list.pb(i);
      }
    }

    if (one_max >= m) {
      cout << -1 << endl;
      return 0;
    }
  }

  int now = n;
  vector<int> pos;
  pos.pb(now);
  while (now != 0) {
    decltype(ok_list)::iterator it =
        std::upper_bound(ok_list.begin(), ok_list.end(), now - m - 1);
    now = *it;
    pos.pb(now);
  }

  int _size = SZ(pos);
  RREP(i, _size - 1) {
    cout << (pos[i] - pos[i + 1]);
    if (i != 0)
      cout << " ";
  }
  cout << endl;

  return 0;
}
Problem p02852 (C++)

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

using namespace std;

#define int long long
#define endl '\n'
#define MOD 1000000007
#define maxn 100010

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  string s;
  cin >> s;

  int now = n;

  vector<int> ans;
  while (now > 0) {
    bool fl = false;
    for (int i = now - m; i < now; i++) {
      if (s[i] == '0') {
        ans.push_back(now - i);
        now = i;
        fl = true;
        break;
      }
    }
    if (!fl)
      break;
  }
  if (now)
    cout << -1 << endl;
  else
    for (int i = ans.size() - 1; i >= 0; i--)
      cout << ans[i] << " ";
}

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

using namespace std;

#define int long long
#define endl '\n'
#define MOD 1000000007
#define maxn 100010

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  string s;
  cin >> s;

  int now = n;

  vector<int> ans;
  while (now > 0) {
    bool fl = false;
    for (int i = max(0ll, now - m); i < now; i++) {
      if (s[i] == '0') {
        ans.push_back(now - i);
        now = i;
        fl = true;
        break;
      }
    }
    if (!fl)
      break;
  }
  if (now)
    cout << -1 << endl;
  else
    for (int i = ans.size() - 1; i >= 0; i--)
      cout << ans[i] << " ";
}
Problem p02852 (Python)

Buggy submission (status: Time Limit Exceeded):
n, m = map(int, input().split())
s = input()
s = s[::-1]
if s[0] == "1":
    print(-1)
    exit()
pre = [-1] * (n + 1)
stack = [0]
while stack:
    now = stack.pop()
    if now == n:
        break
    for i in range(1, m + 1):
        tsugi = now + i
        if tsugi > n:
            continue
        if s[tsugi] == "1":
            continue
        pre[tsugi] = now
        stack.append(tsugi)
else:
    print(-1)
    exit()
ato = n
mae = pre[n]
ans = []
while mae >= 0:
    ans.append(ato - mae)
    ato = mae
    mae = pre[ato]
print(*ans, sep=" ")


Fixed submission:
n, m = map(int, input().split())
s = input()
out_s = "1" * (m)
if out_s in s:
    print(-1)
    exit()
s = s[::-1]
if s[0] == "1":
    print(-1)
    exit()
pre = [-1] * (n + 1)
stack = [0]
while stack:
    now = stack.pop()
    if now == n:
        break
    for i in range(1, m + 1):
        tsugi = now + i
        if tsugi > n:
            continue
        if s[tsugi] == "1":
            continue
        pre[tsugi] = now
        stack.append(tsugi)
else:
    print(-1)
    exit()
ato = n
mae = pre[n]
ans = []
while mae >= 0:
    ans.append(ato - mae)
    ato = mae
    mae = pre[ato]
print(*ans, sep=" ")
Problem p02852 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**9)

n, m = map(int, input().split())
s = input()


def solve(t):
    if len(t) <= m + 1:
        return [len(t) - 1]
    for i in reversed(range(1, m + 1)):
        if t[len(t) - 1 - i] == "0":
            cand = solve(t[:-i])
            if len(cand) != 0:
                cand.append(i)
                return cand

    return []


cnt1_continuous = 0
cnt = 0
for i in range(n + 1):
    if s[i] == "1":
        cnt += 1
        if i == n:
            cnt1_continuous = max(cnt1_continuous, cnt)
    else:
        cnt1_continuous = max(cnt1_continuous, cnt)
        cnt = 0

if cnt1_continuous >= m:
    print(-1)
else:
    print(*solve(s))


Fixed submission:
import sys

sys.setrecursionlimit(10**9)

n, m = map(int, input().split())
s = input()


def solve(t):
    if len(t) <= m + 1:
        return [len(t) - 1]
    for i in reversed(range(1, m + 1)):
        if t[len(t) - 1 - i] == "0":
            cand = solve(t[:-i])
            if len(cand) != 0:
                cand.append(i)
                return cand

    return []


cnt1_continuous = 0
cnt = 0
for i in range(n + 1):
    if s[i] == "1":
        cnt += 1
        if i == n:
            cnt1_continuous = max(cnt1_continuous, cnt)
    else:
        cnt1_continuous = max(cnt1_continuous, cnt)
        cnt = 0

if cnt1_continuous >= m:
    print(-1)
else:
    if m == 1:
        ans = [1] * n
    else:
        ans = solve(s)
    print(*ans)
Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, n)                                                            \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i]; }                             \
  cout << endl;
#define pprint(A, m, n)                                                        \
  rep(j, 0, m) { print(A[j], n); }
const long mod = 1e9 + 7;
const int size = 1e5;
const long inf = 1e9;
int main() {
  int N, M;
  cin >> N >> M;
  string s;
  cin >> s;
  int t[N + 1];
  t[0] = 1;
  for (int i = N - 1; i >= 0; i--) {
    t[N - i] = t[N - i - 1] + (s[i] == '1' ? 0 : 1);
  }
  // print(t,N+1);
  deque<int> dq;
  int i = 0;
  int j;
  while (i < N) {
    if (t[i + M] == t[i]) {
      cout << -1 << endl;
      return 0;
    }
    j = upper_bound(t + i + 1, t + min(i + M + 1, N + 1),
                    t[min(i + M, N)] - 1) -
        t;
    dq.push_back(j - i);
    i = j;
  }
  reverse(dq.begin(), dq.end());
  print(dq, dq.size());
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, n)                                                            \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i]; }                             \
  cout << endl;
#define pprint(A, m, n)                                                        \
  rep(j, 0, m) { print(A[j], n); }
const long mod = 1e9 + 7;
const int size = 1e5;
const long inf = 1e9;
int main() {
  int N, M;
  cin >> N >> M;
  string s;
  cin >> s;
  int t[N + 1];
  t[0] = 1;
  for (int i = N - 1; i >= 0; i--) {
    t[N - i] = t[N - i - 1] + (s[i] == '1' ? 0 : 1);
  }
  // print(t,N+1);
  deque<int> dq;
  int i = 0;
  int j;
  while (i < N) {
    if (t[min(i + M, N)] == t[i]) {
      cout << -1 << endl;
      return 0;
    }
    j = upper_bound(t + i + 1, t + min(i + M + 1, N + 1),
                    t[min(i + M, N)] - 1) -
        t;
    dq.push_back(j - i);
    i = j;
  }
  reverse(dq.begin(), dq.end());
  print(dq, dq.size());
}
Problem p02852 (C++)

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

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

  if (S.front() != '0') {
    cout << -1 << endl;
  }
  if (S.back() != '0') {
    cout << -1 << endl;
  }

  reverse(S.begin(), S.end());
  vector<bool> over(S.size());
  for (int i = 0; i < over.size(); i++) {
    over[i] = S[i] == '0';
  }

  bool now = true;
  long long count = 0;
  vector<pair<bool, long long>> comp;
  for (int i = 0; i < over.size(); i++) {
    if (now != over[i]) {
      comp.push_back(make_pair(now, count));
      now = over[i];
      count = 1;
    } else {
      count++;
    }
  }
  if (count > 0) {
    comp.push_back(make_pair(now, count));
  }

  vector<long long> ans;
  long long current = 0;
  long long idx = 0;
  long long remain = comp[0].second - 1;
  while (N > 0) {
    long long m = M;
    long long sum = 0;
    while (m > 0) {
      if (remain == 0) {
        if (comp[idx + 1].second < m) {
          m -= comp[idx + 1].second + 1;
          sum += comp[idx + 1].second + 1;
          idx += 2;
          remain = comp[idx].second - 1;
        } else {
          m = 0;
        }
      } else {
        long long d = min(remain, m);
        sum += d;
        m -= d;
        remain -= d;
      }
    }
    if (sum == 0) {
      cout << -1 << endl;
      return 0;
    }
    ans.push_back(min(N, sum));
    N -= sum;
  }
  reverse(ans.begin(), ans.end());
  for (long long a : ans) {
    cout << a << " ";
  }
  cout << endl;
}


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

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

  if (S.front() != '0') {
    cout << -1 << endl;
  }
  if (S.back() != '0') {
    cout << -1 << endl;
  }

  reverse(S.begin(), S.end());
  vector<bool> over(S.size());
  for (int i = 0; i < over.size(); i++) {
    over[i] = S[i] == '0';
  }

  bool now = true;
  long long count = 0;
  vector<pair<bool, long long>> comp;
  for (int i = 0; i < over.size(); i++) {
    if (now != over[i]) {
      comp.push_back(make_pair(now, count));
      now = over[i];
      count = 1;
    } else {
      count++;
    }
  }
  if (count > 0) {
    comp.push_back(make_pair(now, count));
  }

  vector<long long> ans;
  long long current = 0;
  long long idx = 0;
  long long remain = comp[0].second - 1;
  while (N > 0) {
    long long m = M;
    long long sum = 0;
    while (m > 0) {
      if (remain == 0) {
        if ((idx + 1) < comp.size() && comp[idx + 1].second < m) {
          m -= comp[idx + 1].second + 1;
          sum += comp[idx + 1].second + 1;
          idx += 2;
          remain = comp[idx].second - 1;
        } else {
          m = 0;
        }
      } else {
        long long d = min(remain, m);
        sum += d;
        m -= d;
        remain -= d;
      }
    }
    if (sum == 0) {
      cout << -1 << endl;
      return 0;
    }
    ans.push_back(min(N, sum));
    N -= sum;
  }
  reverse(ans.begin(), ans.end());
  for (long long a : ans) {
    cout << a << " ";
  }
  cout << endl;
}