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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i > -1; --i)
#define ALL(a) (a).begin(), (a).end()
#define FILL(a, n, x)                                                          \\
  ;                                                                            \\
  REP(i, (n)) { (a)[i] = (x); }
#define CINA(a, n)                                                             \\
  ;                                                                            \\
  REP(i, (n)) { cin >> (a)[i]; }
#define FILL2(a, n, m, x)                                                      \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { (a)[i][j] = (x); }                                           \\
  }
#define CINA2(a, n, m)                                                         \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { cin >> (a)[i][j]; }                                          \\
  }
#define Liny "Yes\\n"
#define Linn "No\\n"
#define LINY "YES\\n"
#define LINN "NO\\n"

// cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x) {
  if (x < 10) {
    return 1;
  } else {
    return keta(x / 10) + 1;
  }
}
int keta_wa(ll x) {
  if (x < 10) {
    return x;
  } else {
    return keta_wa(x / 10) + x % 10;
  }
}
int ctoi(char c) { return ((c >= '0' && c <= '9') ? c - '0' : 0); }
int __stoi(string s) { return atoi(s.c_str()); }
ll sum(ll a[], ll N) { return accumulate(a, a + N, 0LL); }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  return a / gcd(a, b) * b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
const ll MOD = 998244353;
#define pii pair<int, int>
#define pll pair<ll, ll>

bool func(pii a, pii b) { return a.first > b.first; }

bool check(vector<pii> v) {
  bool ok = true;
  int h = 0;
  for (pii p : v) {
    int b = h + p.first;
    if (b < 0) {
      ok = false;
      return false;
    }
    h += p.second;
  }
  return ok;
}

int main() {
  ll N;
  cin >> N;
  vector<pii> ls, rs;
  int x = 0;
  REP(i, N) {
    string s;
    cin >> s;
    int h = 0;
    int b = 0;
    for (char c : s) {
      if (c == '(') {
        ++h;
      } else {
        --h;
      }
      chmin(b, h);
    }
    if (h >= 0) {
      ls.push_back(make_pair(b, h));
    } else {
      rs.push_back(make_pair(b - h, -h));
    }
    x += h;
  }
  sort(ALL(ls), func);
  sort(ALL(rs), func);

  bool ok = (x == 0 && check(ls) && check(rs));
  if (ok) {
    cout << Liny;
  } else {
    cout << Linn;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i > -1; --i)
#define ALL(a) (a).begin(), (a).end()
#define FILL(a, n, x)                                                          \\
  ;                                                                            \\
  REP(i, (n)) { (a)[i] = (x); }
#define CINA(a, n)                                                             \\
  ;                                                                            \\
  REP(i, (n)) { cin >> (a)[i]; }
#define FILL2(a, n, m, x)                                                      \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { (a)[i][j] = (x); }                                           \\
  }
#define CINA2(a, n, m)                                                         \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { cin >> (a)[i][j]; }                                          \\
  }
#define Liny "Yes\\n"
#define Linn "No\\n"
#define LINY "YES\\n"
#define LINN "NO\\n"

// cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x) {
  if (x < 10) {
    return 1;
  } else {
    return keta(x / 10) + 1;
  }
}
int keta_wa(ll x) {
  if (x < 10) {
    return x;
  } else {
    return keta_wa(x / 10) + x % 10;
  }
}
int ctoi(char c) { return ((c >= '0' && c <= '9') ? c - '0' : 0); }
int __stoi(string s) { return atoi(s.c_str()); }
ll sum(ll a[], ll N) { return accumulate(a, a + N, 0LL); }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  return a / gcd(a, b) * b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
const ll MOD = 998244353;
#define pii pair<int, int>
#define pll pair<ll, ll>

string v[1000000];

bool func(string a, string b) {
  if (a[(a.size() + 1) / 2] == '(') {
    return true;
  } else if (b[(b.size() + 1) / 2] == '(') {
    return false;
  } else
    return true;
}

int main() {
  ll N;
  cin >> N;
  REP(i, N) {
    string s;
    cin >> s;
    int hidari = 0;
    int migi = 0;
    for (char c : s) {
      if (c == '(') {
        ++migi;
      } else {
        if (migi > 0) {
          --migi;
        } else {
          ++hidari;
        }
      }
    }
    v[i] = string(hidari, ')') + string(migi, '(');
  }
  sort(v, v + N, func);
  ll x = 0;
  REP(i, N) {
    for (char c : v[i]) {
      if (c == '(') {
        ++x;
      } else {
        --x;
      }
      if (x < 0) {
        break;
      }
    }
    if (x < 0) {
      break;
    }
  }
  if (x == 0) {
    cout << Liny;
  } else {
    cout << Linn;
  }
}