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

// This program is written by Brian Peng.
#pragma GCC optimize("Ofast", "inline", "no-stack-protector")
#include <bits/stdc++.h>
using namespace std;
#define Rd(a) (a = rd())
#define Gc(a) (a = getchar())
#define Pc(a) putchar(a)
int rd() {
  int x;
  char c(getchar());
  bool k;
  while (!isdigit(c) && c ^ '-')
    if (Gc(c) == EOF)
      exit(0);
  c ^ '-' ? (k = 1, x = c & 15) : k = x = 0;
  while (isdigit(Gc(c)))
    x = x * 10 + (c & 15);
  return k ? x : -x;
}
void wr(int a) {
  if (a < 0)
    Pc('-'), a = -a;
  if (a <= 9)
    Pc(a | '0');
  else
    wr(a / 10), Pc((a % 10) | '0');
}
signed const INF(0x3f3f3f3f), NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL), LNINF(0xc3c3c3c3c3c3c3c3LL);
#define Ps Pc(' ')
#define Pe Pc('\\n')
#define Frn0(i, a, b) for (int i(a); i < (b); ++i)
#define Frn1(i, a, b) for (int i(a); i <= (b); ++i)
#define Frn_(i, a, b) for (int i(a); i >= (b); --i)
#define Mst(a, b) memset(a, b, sizeof(a))
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
#define N (1000010)
int n, a[N], b[N], c[N], p[N], m, sb;
char s[N];
signed main() {
  Rd(n), Mst(c, 0x3f);
  Frn1(i, 1, n) {
    cin >> s + 1, m = strlen(s + 1);
    Frn1(j, 1, m) {
      if (s[j] == '(')
        ++a[i];
      else if (a[i] > 0)
        --a[i];
      else
        ++b[i];
      c[i] = min(c[i], a[i] - b[i]);
    }
    p[i] = i;
  }
  sort(p + 1, p + n + 1, [](int x, int y) {
    if (!b[x] && !b[y])
      return a[x] > a[y];
    if (b[x] && b[y]) {
      if (a[x] > b[x] && a[y] > b[y])
        return b[x] < b[y];
      if (a[x] > b[x])
        return true;
      if (a[y] > b[y])
        return false;
      return a[x] > a[y];
    }
    return !b[x];
  });
  Frn1(i, 1, n) {
    if (sb + c[p[i]] < 0)
      printf("No"), exit(0);
    sb += a[p[i]] - b[p[i]];
  }
  printf(sb ? "No" : "Yes"), exit(0);
}


Error: WA

Fixed submission:

// This program is written by Brian Peng.
#pragma GCC optimize("Ofast", "inline", "no-stack-protector")
#include <bits/stdc++.h>
using namespace std;
#define Rd(a) (a = rd())
#define Gc(a) (a = getchar())
#define Pc(a) putchar(a)
int rd() {
  int x;
  char c(getchar());
  bool k;
  while (!isdigit(c) && c ^ '-')
    if (Gc(c) == EOF)
      exit(0);
  c ^ '-' ? (k = 1, x = c & 15) : k = x = 0;
  while (isdigit(Gc(c)))
    x = x * 10 + (c & 15);
  return k ? x : -x;
}
void wr(int a) {
  if (a < 0)
    Pc('-'), a = -a;
  if (a <= 9)
    Pc(a | '0');
  else
    wr(a / 10), Pc((a % 10) | '0');
}
signed const INF(0x3f3f3f3f), NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL), LNINF(0xc3c3c3c3c3c3c3c3LL);
#define Ps Pc(' ')
#define Pe Pc('\\n')
#define Frn0(i, a, b) for (int i(a); i < (b); ++i)
#define Frn1(i, a, b) for (int i(a); i <= (b); ++i)
#define Frn_(i, a, b) for (int i(a); i >= (b); --i)
#define Mst(a, b) memset(a, b, sizeof(a))
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
#define N (1000010)
int n, a[N], b[N], c[N], p[N], l, r, m, sb;
char s[N];
signed main() {
  Rd(n), Mst(c, 0x3f);
  Frn1(i, 1, n) {
    cin >> s + 1, m = strlen(s + 1);
    Frn1(j, 1, m) {
      if (s[j] == '(')
        ++a[i];
      else if (a[i] > 0)
        --a[i];
      else
        ++b[i];
      c[i] = min(c[i], a[i] - b[i]);
    }
    p[i] = i;
  }
  sort(p + 1, p + n + 1, [](int x, int y) {
    if (!b[x] && !b[y])
      return a[x] > a[y];
    if (b[x] && b[y]) {
      if (a[x] > b[x] && a[y] > b[y])
        return b[x] < b[y];
      if (a[x] > b[x])
        return true;
      if (a[y] > b[y])
        return true;
      return a[x] > a[y];
    }
    return !b[x];
  });
  printf(sb ? "No" : "Yes"), exit(0);
}