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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b)                                                           \\
  for (int i = a; a < b; i++)                                                  \\
    ;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << __LINE__ << ": " << #x << " -> " << x << endl
#define debug_vec(v)                                                           \\
  cerr << __LINE__ << ": " << #v << " ->";                                     \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << endl
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

struct Paren {
  int idx;
  int r;
  int l;
};

void solve() {
  int n;
  cin >> n;
  vector<Paren> X(n);
  vector<string> S(n);
  rep(i, n) {
    int r = 0, l = 0;
    string s;
    cin >> s;
    rep(j, s.size()) {
      if (s[j] == '(')
        r++;
      else
        r--;
      if (r < 0) {
        l++;
        r = 0;
      }
    }
    S[i] = s;
    X[i] = {i, r, l};
  }
  auto compare = [&](Paren a, Paren b) {
    if (a.r - a.l >= 0 && b.r - b.l < 0)
      return true;
    else if (a.r - a.l >= 0 && b.r - b.l >= 0)
      return a.l < b.l;
    else if (a.r - a.l < 0 && b.r - b.l < 0)
      return a.r > b.r;
    else
      return false;
  };
  sort(all(X), compare);
  string s = "";
  for (auto &x : X) {
    int i = x.idx;
    s += S[i];
  }
  int r = 0;
  rep(i, s.size()) {
    if (s[i] == '(')
      r++;
    else
      r--;
    if (r < 0) {
      cout << "No" << endl;
      return;
    }
  }
  if (r == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b)                                                           \\
  for (int i = a; a < b; i++)                                                  \\
    ;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << __LINE__ << ": " << #x << " -> " << x << endl
#define debug_vec(v)                                                           \\
  cerr << __LINE__ << ": " << #v << " ->";                                     \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << endl
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

struct Paren {
  int idx;
  int r;
  int l;
};

void solve() {
  int n;
  cin >> n;
  vector<Paren> X(n);
  vector<string> S(n);
  rep(i, n) {
    int r = 0, l = 0;
    string s;
    cin >> s;
    if (s[i] == '(')
      r++;
    else
      r--;
    if (r < 0) {
      l++;
      r = 0;
    }
    S[i] = s;
    X[i] = {i, r, l};
  }
  auto compare = [&](Paren a, Paren b) { return a.r - a.l > b.r - b.l; };
  sort(all(X), compare);
  string s = "";
  for (auto &x : X) {
    int i = x.idx;
    s += S[i];
  }
  int r = 0;
  rep(i, s.size()) {
    if (s[i] == '(')
      r++;
    else
      r--;
    if (r < 0) {
      cout << "No" << endl;
      return;
    }
  }
  if (r == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}