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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

struct RL {
  int R = 0, L = 0;
  pair<int, int> cost;

  bool operator<(const RL &a) { return cost < a.cost; }
};

void solve(long long N, std::vector<std::string> S) {
  rep(i, 0, N) {
    vector<int> left;
    rep(j, 0, S[i].size()) {
      if (S[i][j] == '(') {
        left.push_back(j);
      } else if (!left.empty() && S[i][j] == ')') {
        S[i][left.back()] = 'o';
        S[i][j] = 'o';
        left.pop_back();
      }
    }
    left.clear();
  }
  vector<RL> rl(N);
  rep(i, 0, N) {
    rep(j, 0, S[i].size()) {
      if (S[i][j] == ')') {
        assert(rl[i].L == 0);
        rl[i].R++;
      } else if (S[i][j] == '(') {
        rl[i].L++;
      }
    }
  }
  rep(i, 0, N) {
    if (rl[i].L - rl[i].R >= 0) {
      rl[i].cost.first = 0;
      rl[i].cost.second = rl[i].R;
    } else {
      rl[i].cost.first = 1;
      rl[i].cost.second = -rl[i].L;
    }
  }
  sort(all(rl));
  bool isValid = true;
  int current = 0;
  rep(i, 0, N) {
    current -= rl[i].R;
    if (current < 0) {
      isValid = false;
      break;
    }
    current += rl[i].L;
  }
  if (current != 0)
    isValid = false;
  cout << (isValid ? "Yes" : "No") << endl;
}

signed main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  long long N;
  cin >> N;
  std::vector<std::string> S(N);
  for (int i = 0; i < N; i++) {
    std::cin >> S[i];
  }
  solve(N, std::move(S));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long N, std::vector<std::string> S) {
  //    N = 1e5+5e4;
  //    vector<string> S(N);
  //    rep(i,0,5e4) S[i] = "()";
  //    rep(i,5e4,1e5) S[i] = "((";
  //    rep(i,1e5,N) S[i] = "))";
  rep(i, 0, N) {
    stack<int> left;
    rep(j, 0, S[i].size()) {
      if (S[i][j] == '(') {
        left.push(j);
      } else if (!left.empty() && S[i][j] == ')') {
        S[i][left.top()] = 'o';
        S[i][j] = 'o';
        left.pop();
      }
    }
  }
  vector<pair<int, int>> RL(N, {0, 0});
  rep(i, 0, N) {
    rep(j, 0, S[i].size()) {
      if (S[i][j] == ')') {
        assert(RL[i].second == 0);
        RL[i].first++;
      } else if (S[i][j] == '(') {
        RL[i].second++;
      }
    }
  }
  sort(all(RL), [](const auto &a, const auto &b) {
    if ((a.second - a.first) ^ (b.second - b.first)) {
      return (a.second - a.first) >= (b.second - b.first);
    }
    if (a.second - a.first >= 0) {
      return a.first <= b.first;
    } else {
      return a.second >= b.second;
    }
  });
  bool isValid = true;
  int current = 0;
  rep(i, 0, N) {
    current -= RL[i].first;
    if (current < 0) {
      isValid = false;
      break;
    }
    current += RL[i].second;
  }
  if (current != 0)
    isValid = false;
  cout << (isValid ? "Yes" : "No") << endl;
}

signed main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  long long N;
  cin >> N;
  std::vector<std::string> S(N);
  for (int i = 0; i < N; i++) {
    std::cin >> S[i];
  }
  solve(N, std::move(S));
  return 0;
}