Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, a, b) for (long long(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i)                                                               \
  std::cerr << "debug "                                                        \
            << "LINE:" << __LINE__ << "  " << #i << ":" << i << std::endl

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

long long pow_mod(long long a, long long b, long long mod = -1) {
  if ((a == 0) || (mod != -1 && a % mod == 0)) {
    return 0;
  }

  long long x = 1;

  while (b > 0) {
    if (b & 1) {
      x = (mod != -1) ? (x * a) % mod : x * a;
    }
    a = (mod != -1) ? (a * a) % mod : a * a;
    b >>= 1;
  }
  return x;
}

// const long long MOD = 998244353;
const long long MOD = 1e9 + 7;

using ll = long long;
using P = std::pair<ll, ll>;

template <typename T> class SWAG {
private:
  std::function<T(T, T)> fn;

  std::stack<T> front, back;
  std::vector<T> front_fold, back_fold;

  T id;

public:
  SWAG(T id_, std::function<T(T, T)> fn_)
      : id(id_), fn(fn_), front_fold(1, id_), back_fold(1, id_) {}

  void push(T val) {
    back.push(val);
    back_fold.push_back(fn(back_fold.back(), val));
  }

  void pop() {
    if (front.empty()) {
      assert(front_fold.size() == 1);
      while (!back.empty()) {
        T temp = back.top();
        back.pop();
        back_fold.pop_back();

        front.push(temp);
        front_fold.push_back(fn(temp, front_fold.back()));
      }
    }

    assert(!front.empty());

    front.pop();
    front_fold.pop_back();
  }

  T fold_all() { return fn(front_fold.back(), back_fold.back()); }
};

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

  ll n, m;
  std::cin >> n >> m;

  std::string s;
  std::cin >> s;

  SWAG<P> swag({MOD, MOD}, [](P a, P b) { return std::min<P>(a, b); });

  std::vector<P> dp(n + 1, {MOD, MOD});

  dp[n] = {0, 0};
  swag.push({0, n});

  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      P temp = swag.fold_all();
      dp[i] = {temp.first + 1, temp.second - i};
    }

    swag.push({dp[i].first, i});
    if (i <= n - m)
      swag.pop();
  }

  if (dp[0].first == MOD) {
    std::cout << -1 << "\n";
    return 0;
  }

  ll now = 0;

  while (now != n) {
    std::cout << dp[now].second;
    now += dp[now].second;
    if (now != n)
      std::cout << " ";
  }

  std::cout << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, a, b) for (long long(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i)                                                               \
  std::cerr << "debug "                                                        \
            << "LINE:" << __LINE__ << "  " << #i << ":" << i << std::endl

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

long long pow_mod(long long a, long long b, long long mod = -1) {
  if ((a == 0) || (mod != -1 && a % mod == 0)) {
    return 0;
  }

  long long x = 1;

  while (b > 0) {
    if (b & 1) {
      x = (mod != -1) ? (x * a) % mod : x * a;
    }
    a = (mod != -1) ? (a * a) % mod : a * a;
    b >>= 1;
  }
  return x;
}

// const long long MOD = 998244353;
const long long MOD = 1e9 + 7;

using ll = long long;
using P = std::pair<ll, ll>;

template <typename T> class SWAG {
private:
  std::function<T(T, T)> fn;

  std::stack<T> front, back;
  std::vector<T> front_fold, back_fold;

  T id;

public:
  SWAG(T id_, std::function<T(T, T)> fn_)
      : id(id_), fn(fn_), front_fold(1, id_), back_fold(1, id_) {}

  void push(T val) {
    back.push(val);
    back_fold.push_back(fn(back_fold.back(), val));
  }

  void pop() {
    if (front.empty()) {
      assert(front_fold.size() == 1);
      while (!back.empty()) {
        T temp = back.top();
        back.pop();
        back_fold.pop_back();

        front.push(temp);
        front_fold.push_back(fn(temp, front_fold.back()));
      }
    }

    assert(!front.empty());

    front.pop();
    front_fold.pop_back();
  }

  T fold_all() { return fn(front_fold.back(), back_fold.back()); }
};

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

  ll n, m;
  std::cin >> n >> m;

  std::string s;
  std::cin >> s;

  SWAG<P> swag({MOD, MOD}, [](P a, P b) { return std::min<P>(a, b); });

  std::vector<P> dp(n + 1, {MOD, MOD});

  dp[n] = {0, 0};
  swag.push({0, n});

  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      P temp = swag.fold_all();
      dp[i] = {temp.first + 1, temp.second - i};
    }

    swag.push({dp[i].first, i});
    if (i <= n - m)
      swag.pop();
  }

  if (dp[0].first >= MOD) {
    std::cout << -1 << "\n";
    return 0;
  }

  ll now = 0;

  while (now != n) {
    std::cout << dp[now].second;
    now += dp[now].second;
    if (now != n)
      std::cout << " ";
  }

  std::cout << "\n";

  return 0;
}