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

#include <bits/stdc++.h>
#define my_max(a, b) ((a) > (b) ? (a) : (b))
#define my_min(a, b) ((a) < (b) ? (a) : (b))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++)
#define rev(i, t, s) for (int i = (int)(t); i >= (int)(s); i--)
#define lson rt << 1
#define rson rt << 1 | 1
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double lb;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<int> VI;
typedef std::vector<ll> VL;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    rbtree;
ll gcd(ll x, ll y) { return y % x == 0 ? x : gcd(y % x, x); }
template <class T> T my_abs(T a) {
  if (a < 0)
    a = -a;
  return a;
}
inline ll read() {
  ll ret = 0, sign = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * sign;
}
inline void write(ll x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

// void expand_stack_space(){
//     int size = 256 << 20; // 256MB
//     char *p = (char*)malloc(size) + size;
//     __asm__("movl %0, %%esp\\n" :: "r"(p));
// }
const int maxn = 1e6 + 7;
char s[maxn];
int n;
pii a[maxn];
int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    a[i].fi = a[i].se = 0;
    for (int j = 1; j <= len; ++j) {
      if (s[j] == ')' && a[i].se > 0)
        --a[i].se;
      if (s[j] == '(')
        ++a[i].se;
    }
    for (int j = len; j >= 1; --j) {
      if (s[j] == '(' && a[i].fi > 0)
        --a[i].fi;
      if (s[j] == ')')
        ++a[i].fi;
    }

    // printf("l r --- %d %d\\n", a[i].fi, a[i].se);
  }
  sort(a + 1, a + n + 1, [](pii &x, pii &y) {
    if (x.se >= x.fi && y.se < y.fi)
      return true;
    if (x.se < x.fi && y.se >= y.fi)
      return false;
    if (x.se >= x.fi && y.se >= y.fi)
      return x.fi < y.fi;
    if (x.fi >= x.se && y.fi >= y.se)
      return x.se > y.se;
  });
  if (a[1].fi != 0)
    return !printf("No");
  int st = a[1].se;
  for (int i = 2; i <= n; ++i) {
    st -= a[i].fi;
    if (st < 0)
      return !printf("No");
    st += a[i].se;
  }
  if (st > 0)
    return !printf("No");
  puts("Yes");
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define my_max(a, b) ((a) > (b) ? (a) : (b))
#define my_min(a, b) ((a) < (b) ? (a) : (b))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++)
#define rev(i, t, s) for (int i = (int)(t); i >= (int)(s); i--)
#define lson rt << 1
#define rson rt << 1 | 1
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double lb;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<int> VI;
typedef std::vector<ll> VL;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    rbtree;
ll gcd(ll x, ll y) { return y % x == 0 ? x : gcd(y % x, x); }
template <class T> T my_abs(T a) {
  if (a < 0)
    a = -a;
  return a;
}
inline ll read() {
  ll ret = 0, sign = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * sign;
}
inline void write(ll x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

// void expand_stack_space(){
//     int size = 256 << 20; // 256MB
//     char *p = (char*)malloc(size) + size;
//     __asm__("movl %0, %%esp\\n" :: "r"(p));
// }
const int maxn = 1e6 + 7;
char s[maxn];
int n;
pii a[maxn];
int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    a[i].fi = a[i].se = 0;
    for (int j = 1; j <= len; ++j) {
      if (s[j] == ')' && a[i].se > 0)
        --a[i].se;
      if (s[j] == '(')
        ++a[i].se;
    }
    for (int j = len; j >= 1; --j) {
      if (s[j] == '(' && a[i].fi > 0)
        --a[i].fi;
      if (s[j] == ')')
        ++a[i].fi;
    }
    // printf("l r --- %d %d\\n", a[i].fi, a[i].se);
  }
  sort(a + 1, a + n + 1, [](pii &x, pii &y) {
    if (x.fi == 0)
      return y.fi > x.fi;
    if (x.se == 0)
      return y.se < x.se;
    int d1 = x.se - x.fi, d2 = y.se - y.fi;
    if (d1 == d2)
      return x.se > y.se;
    return d1 > d2;
  });
  if (a[1].fi != 0)
    return !printf("No");
  int st = a[1].se;
  for (int i = 2; i <= n; ++i) {
    st -= a[i].fi;
    if (st < 0)
      return !printf("No");
    st += a[i].se;
  }
  if (st > 0)
    return !printf("No");
  puts("Yes");
  return 0;
}