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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n;
  cin >> n;
  vector<int> nest(n), mn(n, 0);
  int l = 0, r = 0;
  REP(i, n) {
    string s;
    cin >> s;
    int now = 0;
    for (char c : s) {
      now += c == '(' ? 1 : -1;
      (c == '(' ? l : r) += 1;
      chmin(mn[i], now);
    }
    nest[i] = now;
  }
  if (l != r) {
    cout << "No\\n";
    return 0;
  }
  vector<int> idx(n);
  iota(ALL(idx), 0);
  sort(ALL(idx), [&](int a, int b) { return mn[a] > mn[b]; });
  int pos = 0, depth = 0;
  priority_queue<pair<int, int>> que;
  vector<int> minus;
  REP(_, n) {
    // cout << depth << '\\n';
    while (pos < n && -mn[idx[pos]] <= depth) {
      if (nest[idx[pos]] >= 0) {
        que.emplace(nest[idx[pos]], idx[pos]);
      } else {
        minus.emplace_back(idx[pos]);
      }
      ++pos;
    }
    if (que.empty() && minus.empty()) {
      cout << "No\\n";
      return 0;
    }
    if (!que.empty()) {
      int id = que.top().second;
      que.pop();
      depth += nest[id];
    } else {
      if (_ + minus.size() < n) {
        cout << "No\\n";
        return 0;
      }
      sort(ALL(minus),
           [&](int a, int b) { return mn[a] - nest[a] < mn[b] - nest[b]; });
      for (int id : minus) {
        if (depth + mn[id] < 0) {
          cout << "No\\n";
          return 0;
        }
        depth += nest[id];
      }
      break;
    }
  }
  cout << "Yes\\n";
  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n;
  cin >> n;
  vector<int> nest(n), mn(n, 0);
  int l = 0, r = 0;
  REP(i, n) {
    string s;
    cin >> s;
    int now = 0;
    for (char c : s) {
      now += c == '(' ? 1 : -1;
      (c == '(' ? l : r) += 1;
      chmin(mn[i], now);
    }
    nest[i] = now;
  }
  if (l != r) {
    cout << "No\\n";
    return 0;
  }
  vector<int> idx(n);
  iota(ALL(idx), 0);
  sort(ALL(idx), [&](int a, int b) { return mn[a] > mn[b]; });
  int pos = 0, depth = 0;
  priority_queue<pair<int, int>> que;
  set<pair<pair<int, int>, int>> st;
  REP(_, n) {
    // cout << depth << '\\n';
    while (pos < n && -mn[idx[pos]] <= depth) {
      if (nest[idx[pos]] >= 0) {
        que.emplace(nest[idx[pos]], idx[pos]);
      } else {
        st.emplace(make_pair(mn[idx[pos]], -nest[idx[pos]]), idx[pos]);
      }
      ++pos;
    }
    if (que.empty() && st.empty()) {
      cout << "No\\n";
      return 0;
    }
    int id;
    if (!que.empty()) {
      id = que.top().second;
      que.pop();
    } else {
      if (_ + st.size() < n) {
        cout << "No\\n";
        return 0;
      }
      id = st.begin()->second;
      st.erase(st.begin());
    }
    // if (depth + mn[id] < 0) {
    //   cout << "No\\n";
    //   return 0;
    // }
    assert(depth + mn[id] >= 0);
    depth += nest[id];
  }
  cout << "Yes\\n";
  return 0;
}