Problem p02652 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define llinf ((long long)1e18)
#define inf int(1e9 + 1e6)
#define maxn (1000006)
ll n, psum[maxn];
string A;
int main() {
  FAST cin >> A;
  n = A.size();
  A = '*' + A;
  FOR(i, 1, n) { psum[i] = psum[i - 1] + (A[i] == '1' ? 1 : -1); }
  ll Z = *max_element(psum, psum + n + 1);
  auto f = [&](ll x) {
    mmst(psum, 0);
    FOR(i, 1, n) { psum[i] = psum[i - 1] + (A[i] == '1' ? 1 : -1); }
    vector<ll> suf_max(n + 2, -llinf);
    DEC(i, n, 1) suf_max[i] = max(suf_max[i + 1], psum[i]);
    ll change = 0;
    FOR(i, 1, n) {
      if (A[i] == '?') {
        if (suf_max[i] + 2 * (change + 1) <= x)
          ++change;
      }
      psum[i] += 2 * change;
    }
    return *min_element(psum, psum + n + 1);
  };
  cout << min(Z - f(Z), Z + 1 - f(Z + 1)) << '\\n';
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define llinf ((long long)1e18)
#define inf int(1e9 + 1e6)
#define maxn (300006)
ll n, psum[maxn];
string A;
int main() {
  FAST cin >> A;
  n = A.size();
  A = '*' + A;
  FOR(i, 1, n) { psum[i] = psum[i - 1] + (A[i] == '1' ? 1 : -1); }
  ll Z = *max_element(psum, psum + n + 1);
  auto f = [&](ll x) {
    mmst(psum, 0);
    FOR(i, 1, n) { psum[i] = psum[i - 1] + (A[i] == '1' ? 1 : -1); }
    vector<ll> suf_max(n + 2, -llinf);
    DEC(i, n, 1) suf_max[i] = max(suf_max[i + 1], psum[i]);
    ll change = 0;
    FOR(i, 1, n) {
      if (A[i] == '?') {
        if (suf_max[i] + 2 * (change + 1) <= x)
          ++change;
      }
      psum[i] += 2 * change;
    }
    return *min_element(psum, psum + n + 1);
  };
  cout << min(Z - f(Z), Z + 1 - f(Z + 1)) << '\\n';
}