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

#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "vector"
using namespace std;
#define MAXN 1000006
// #define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define mem(a) memset(a, 0, sizeof a)
typedef long long ll;
int n, m;
char ch[MAXN];
int A[MAXN], a[MAXN], B[MAXN], mn[MAXN], mmn = 0x3f3f3f3f;
bool us[MAXN];
void solve() {
  scanf("%s", ch + 1);
  n = strlen(ch + 1);
  int mx = 0, ps;
  rep(i, 1, n) {
    if (ch[i] == '?' || ch[i] == '0')
      A[i] = A[i - 1] - 1;
    else
      A[i] = A[i - 1] + 1;
    a[i] = A[i];
    mx = max(mx, A[i]);
  }
  rep(i, 0, n) if (A[i] == mx) ps = i;
  B[n] = A[n];
  per(i, n - 1, 1) B[i] = max(A[i], B[i + 1]);
  int t = 0;
  rep(i, ps, n) {
    if (ch[i] == '?' && B[i] + t <= mx - 2)
      t += 2, us[i] = 1;
    A[i] += t;
  }
  mmn = 0;
  rep(i, 1, n) mmn = min(mmn, A[i]);
  int as = mx - mmn, chp = 0;
  mem(us);

  rep(i, 1, n) A[i] = a[i];
  t = 0;
  rep(i, ps, n) {
    if (ch[i] == '?' && B[i] + t <= mx - 1)
      t += 2, us[i] = 1;
    A[i] += t;
  }
  mmn = 0;
  rep(i, 1, n) mmn = min(mmn, A[i]);
  int as1 = mx + 1 - mmn, chp1 = 0;

  printf("%d\\n", min(as1, as));
}

signed main() {
  //    int T;cin >> T;while( T-- ) solve();
  solve();
}

Error: WA

Fixed submission:

#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "vector"
using namespace std;
#define MAXN 1000006
// #define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define mem(a) memset(a, 0, sizeof a)
typedef long long ll;
int n, m;
char ch[MAXN];
int A[MAXN], a[MAXN], B[MAXN], mn[MAXN], mmn = 0x3f3f3f3f;
bool us[MAXN];
void solve() {
  scanf("%s", ch + 1);
  n = strlen(ch + 1);
  int mx = 0, ps;
  rep(i, 1, n) {
    if (ch[i] == '?' || ch[i] == '0')
      A[i] = A[i - 1] - 1;
    else
      A[i] = A[i - 1] + 1;
    a[i] = A[i];
    mx = max(mx, A[i]);
  }
  rep(i, 1, n) if (A[i] == mx) ps = i;
  B[n] = A[n];
  per(i, n - 1, 1) B[i] = max(A[i], B[i + 1]);
  int t = 0;
  rep(i, ps, n) {
    if (ch[i] == '?' && B[i] + t <= mx - 2)
      t += 2, us[i] = 1;
    A[i] += t;
  }
  mmn = 0;
  rep(i, 1, n) mmn = min(mmn, A[i]);
  int as = mx - mmn, chp = 0;
  mem(us);

  rep(i, 1, n) A[i] = a[i];
  t = 0;
  rep(i, ps, n) {
    if (ch[i] == '?' && B[i] + t <= mx - 1)
      t += 2, us[i] = 1;
    A[i] += t;
  }
  mmn = 0;
  rep(i, 1, n) mmn = min(mmn, A[i]);
  int as1 = mx + 1 - mmn, chp1 = 0;

  printf("%d\\n", min(as1, as));
}

signed main() {
  //    int T;cin >> T;while( T-- ) solve();
  solve();
}