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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, N - 1, 0)
#define RREPS(i, n) RFOR(i, N, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;
const int MOD = 1e9 + 7;

signed main() {
  int N;
  cin >> N;
  int lsum = 0, rsum = 0;
  string S[N];
  REP(i, N) cin >> S[i];
  int ls[N], rs[N];
  REP(i, N) ls[i] = rs[i] = 0;
  int sumc = 0;
  bool ok = true;
  REP(i, N) {
    int D = S[i].size();
    REP(j, D) {
      if (S[i][j] == ')') {
        if (rs[i] == 0)
          ls[i]++;
        else
          rs[i]--;
      } else {
        rs[i]++;
      }
    }
    sumc += ls[i] - rs[i];
  }
  if (sumc != 0)
    ok = false;
  vector<pair<int, int>> lsc;
  vector<pair<int, int>> rsc;
  REP(i, N) {
    if (rs[i] >= ls[i]) {
      lsc.pb({ls[i], rs[i] - ls[i]});
    } else
      rsc.pb({rs[i], ls[i] - rs[i]});
  }
  sort(lsc.begin(), lsc.end());
  sort(rsc.begin(), rsc.end());
  int sum = 0;
  REP(i, lsc.size()) {
    if (lsc[i].first > sum) {
      ok = false;
      break;
    }
    sum += lsc[i].second;
  }
  sum = 0;
  REP(i, rsc.size()) {
    if (rsc[i].first > sum) {
      ok = false;
      break;
    }
    sum += rsc[i].second;
  }
  cout << (ok ? "Yes" : "No") << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, N - 1, 0)
#define RREPS(i, n) RFOR(i, N, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;
const int MOD = 1e9 + 7;

signed main() {
  int N;
  cin >> N;
  int lsum = 0, rsum = 0;
  string S[N];
  REP(i, N) cin >> S[i];
  int ls[N], rs[N];
  REP(i, N) ls[i] = rs[i] = 0;
  REP(i, N) {
    int D = S[i].size();
    REP(j, D) {
      if (S[i][j] == ')') {
        if (rs[i] == 0)
          ls[i]++;
        else
          rs[i]--;
      } else {
        rs[i]++;
      }
    }
  }
  vector<pair<int, int>> piyo(N);
  int rssum = 0;
  int count = N;
  vector<priority_queue<int>> pi(1e4 + 1);
  REP(i, N) {
    if (ls[i] == 0) {
      rssum += rs[i];
      count--;
      continue;
    }
    pi[ls[i]].push(rs[i] - ls[i]);
  }
  N = count;
  bool ok = true;
  REP(i, N) {
    int nowtuyo = -INF;
    int nowban = INF;
    for (int k = rssum; k >= 0; k--) {
      if (pi[k].empty())
        continue;
      else {
        if (nowtuyo < pi[k].top()) {
          nowtuyo = pi[k].top();
          nowban = k;
        }
      }
    }
    rssum += nowtuyo;
    if (nowban == INF) {
      ok = false;
      break;
    }
    pi[nowban].pop();
  }
  if (rssum != 0)
    ok = false;
  cout << (ok ? "Yes" : "No") << endl;
}