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

#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (int)(e); --i)

#define sim template <class n
sim, class s > ostream &operator<<(ostream &p, pair<n, s> x) {
  return p << "<" << x.f << ", " << x.s << ">";
}
sim > auto operator<<(ostream &p, n y) ->
      typename enable_if<!is_same<n, string>::value,
                         decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++)
      p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << endl; }
sim, class... s > void dor(n p, s... y) {
  cerr << p << " ";
  dor(y...);
}
sim, class s > void mini(n &p, s y) {
  if (p > y)
    p = y;
}
sim, class s > void maxi(n &p, s y) {
  if (p < y)
    p = y;
}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "
const int MXN = 1e6 + 6;
string s[MXN];
int mi[MXN];
int mi_rev[MXN];
int sum[MXN];

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  VI all;
  REP(i, n) {
    cin >> s[i];
    for (auto j : s[i]) {
      sum[i] += (j == '(') ? 1 : -1;
      mini(mi[i], sum[i]);
    }
    all.PB(i);

    reverse(ALL(s[i]));
    int ss = 0;
    for (auto j : s[i]) {
      ss += j == ')' ? 1 : -1;
      mini(mi_rev[i], ss);
    }
    //    debug(mi_rev[i], sum[i], mi[i]);
    //    assert(abs(mi_rev[i]) == sum[i] - mi[i]);
  }
  sort(ALL(all), [&](int a, int b) {
    if ((sum[a] >= 0) != (sum[b] >= 0))
      return (sum[a] >= 0) > (sum[b] >= 0);
    if (sum[a] >= 0) {
      return mi[a] > mi[b];
    } else {
      //      return mi_rev[a] < mi_rev[b];
      return sum[b] - mi[b] < sum[a] - mi[a];
    }
  });

  int s = 0;
  for (auto i : all) {
    if (s + mi[i] < 0) {
      puts("No");
      return 0;
    }
    s += sum[i];
  }
  if (s == 0)
    puts("Yes");
  else
    puts("No");
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (int)(e); --i)

#define sim template <class n
sim, class s > ostream &operator<<(ostream &p, pair<n, s> x) {
  return p << "<" << x.f << ", " << x.s << ">";
}
sim > auto operator<<(ostream &p, n y) ->
      typename enable_if<!is_same<n, string>::value,
                         decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++)
      p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << endl; }
sim, class... s > void dor(n p, s... y) {
  cerr << p << " ";
  dor(y...);
}
sim, class s > void mini(n &p, s y) {
  if (p > y)
    p = y;
}
sim, class s > void maxi(n &p, s y) {
  if (p < y)
    p = y;
}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "
const int MXN = 1e6 + 6;
string s[MXN];
int mi[MXN];
int mi_rev[MXN];
int sum[MXN];

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  VI all;
  REP(i, n) {
    cin >> s[i];
    for (auto j : s[i]) {
      sum[i] += (j == '(') ? 1 : -1;
      mini(mi[i], sum[i]);
    }
    all.PB(i);

    reverse(ALL(s[i]));
    int ss = 0;
    for (auto j : s[i]) {
      ss += j == ')' ? 1 : -1;
      mini(mi_rev[i], ss);
    }
    debug(mi_rev[i], sum[i], mi[i]);
    assert(abs(mi_rev[i]) == sum[i] - mi[i]);
  }
  sort(ALL(all), [&](int a, int b) {
    if ((sum[a] >= 0) != (sum[b] >= 0))
      return (sum[a] >= 0) > (sum[b] >= 0);
    if (sum[a] >= 0) {
      return mi[a] > mi[b];
    } else {
      //      return mi_rev[a] < mi_rev[b];
      return sum[b] - mi[b] > sum[a] + mi[a];
    }
  });

  int s = 0;
  for (auto i : all) {
    if (s + mi[i] < 0) {
      puts("No");
      return 0;
    }
    s += sum[i];
  }
  if (s == 0)
    puts("Yes");
  else
    puts("No");
}