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

#include <bits/stdc++.h>
#define int long long
#define rep(i, a, n) for (int i = (int)(a); (i) < (int)(n); ++(i))
#define repe(i, a, n) for (int i = (int)(a); (i) <= (int)(n); ++(i))
#define repif(i, a, x) for (int i = (int)(a); (x); ++(i))
#define repr(i, a, n) for (int i = ((int)(a)-1); (i) >= (int)(n); --(i))
#define reper(i, a, n) for (int i = (int)(a); (i) >= (int)(n); --(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x) / gcd(x, y) * (y))
using namespace std;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<pair<int, int>>;
using VTIII = vector<tuple<int, int, int>>;
using Complex = complex<double>;
using LD = long double;
template <typename T> inline bool chmax(T &a, const T &b, bool t = false) {
  if (a < b or (t and a == b)) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, const T &b, bool t = false) {
  if (a > b or (t and a == b)) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool isin(const T &x, const T &l, const T &r) {
  return l <= x and x < r;
}
inline ostream &fcout(int n = 10) { return cout << fixed << setprecision(n); }
inline ostream &rcout(int n = 2) { return cout << right << setw(n); }
constexpr int64_t INF = numeric_limits<int64_t>::max();
constexpr int32_t INF32 = numeric_limits<int32_t>::max();
constexpr int64_t MOD = 1000000007LL;
constexpr long double PI = 3.14159265358979323846264338327950288;
constexpr double EPS = 1e-10;

int N;
string S[1000010];

int32_t main() {
  cin >> N;
  rep(i, 0, N) cin >> S[i];

  vector<PII> v;
  rep(i, 0, N) {
    stack<char> s;
    rep(j, 0, SZ(S[i])) {
      char t = ' ';
      if (!s.empty())
        t = s.top();
      if (t == '(' and S[i][j] == ')')
        s.pop();
      else
        s.push(S[i][j]);
    }
    if (s.empty())
      continue;
    string t;
    while (!s.empty())
      t.push_back(s.top()), s.pop();
    reverse(ALL(t));
    int a = count(ALL(t), '(');
    int b = count(ALL(t), ')');
    if (b == 0)
      v.emplace_back(-INF, i);
    else if (a == 0)
      v.emplace_back(INF, i);
    else
      v.emplace_back(b - a, i);
  }

  sort(ALL(v));

  stack<char> s;
  rep(i, 0, SZ(v)) {
    int u = v[i].second;
    rep(j, 0, SZ(S[u])) {
      char t = ' ';
      if (!s.empty())
        t = s.top();
      if (t == '(' and S[u][j] == ')')
        s.pop();
      else
        s.push(S[u][j]);
    }
  }

  if (s.empty())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define rep(i, a, n) for (int i = (int)(a); (i) < (int)(n); ++(i))
#define repe(i, a, n) for (int i = (int)(a); (i) <= (int)(n); ++(i))
#define repif(i, a, x) for (int i = (int)(a); (x); ++(i))
#define repr(i, a, n) for (int i = ((int)(a)-1); (i) >= (int)(n); --(i))
#define reper(i, a, n) for (int i = (int)(a); (i) >= (int)(n); --(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x) / gcd(x, y) * (y))
using namespace std;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<pair<int, int>>;
using VTIII = vector<tuple<int, int, int>>;
using Complex = complex<double>;
using LD = long double;
template <typename T> inline bool chmax(T &a, const T &b, bool t = false) {
  if (a < b or (t and a == b)) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, const T &b, bool t = false) {
  if (a > b or (t and a == b)) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool isin(const T &x, const T &l, const T &r) {
  return l <= x and x < r;
}
inline ostream &fcout(int n = 10) { return cout << fixed << setprecision(n); }
inline ostream &rcout(int n = 2) { return cout << right << setw(n); }
constexpr int64_t INF = numeric_limits<int64_t>::max();
constexpr int32_t INF32 = numeric_limits<int32_t>::max();
constexpr int64_t MOD = 1000000007LL;
constexpr long double PI = 3.14159265358979323846264338327950288;
constexpr double EPS = 1e-10;

int N;
string S[1000010];

int32_t main() {
  cin >> N;
  rep(i, 0, N) cin >> S[i];

  vector<tuple<int, int, string>> v;
  rep(i, 0, N) {
    stack<char> s;
    rep(j, 0, SZ(S[i])) {
      char t = ' ';
      if (!s.empty())
        t = s.top();
      if (t == '(' and S[i][j] == ')')
        s.pop();
      else
        s.push(S[i][j]);
    }
    string t;
    while (!s.empty())
      t.push_back(s.top()), s.pop();
    reverse(ALL(t));
    int a = count(ALL(t), '(');
    int b = count(ALL(t), ')');
    v.emplace_back(a, b, t);
  }

  sort(ALL(v), [](const auto &e1, const auto &e2) {
    int a, b;
    tie(a, b, ignore) = e1;
    if (b == 0)
      return true;
    if (a == 0)
      return false;
    int c, d;
    tie(c, d, ignore) = e2;
    if (d == 0)
      return false;
    if (c == 0)
      return true;
    return b < d;
  });

  stack<char> s;
  rep(i, 0, SZ(v)) {
    string u = get<2>(v[i]);
    rep(j, 0, SZ(u)) {
      char t = ' ';
      if (!s.empty())
        t = s.top();
      if (t == '(' and u[j] == ')')
        s.pop();
      else
        s.push(u[j]);
    }
  }

  if (s.empty())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}