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

#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

template <typename T> string to_string(const vector<T> &);

template <typename T> string to_string(const vector<T> &vc, int w) {
  if (w + 1 == vc.size())
    return to_string(vc[w]);
  return to_string(vc[w]) + "," + to_string(vc, w + 1);
}

template <typename T> string to_string(const pair<T, T> &x) {
  return "(" + to_string(x.fi) + "," + to_string(x.se) + ")";
}

template <typename T> string to_string(const vector<T> &vc) {
  return "{" + to_string(vc, 0) + "}";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;

int n;

void no() {
  cout << "No\\n";
  exit(0);
}

ii gt(const string &str) {
  int opn = 0, cls = 0;
  for (auto el : str) {
    if (el == '(')
      opn++;
    else if (opn)
      opn--;
    else
      cls++;
  }
  return ii(cls, opn - cls);
}

int solve(vector<vector<int>> vc, int n) {
  int d = 0, opt = -1;
  priority_queue<ii> pq;
  for (int i = 0; i < n; i++) {
    debug(opt, d);
    while (opt < d) {
      opt++;
      if (opt < vc.size())
        for (auto el : vc[opt])
          pq.push(ii(el, opt));
    }
    while (pq.size() && pq.top().se > d)
      pq.pop();
    if (pq.empty())
      return -1;
    d += pq.top().fi;
    pq.pop();
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif

  cin >> n;
  vector<vector<int>> uno, dos;
  int na = 0, nb = 0;
  for (int i = 0; i < n; i++) {
    string inp;
    cin >> inp;
    ii x = gt(inp);
    auto &vc = (x.se >= 0 ? uno : dos);
    auto &sz = (x.se >= 0 ? na : nb);
    sz++;
    if (x.se < 0) {
      x.fi += x.se;
      x.se *= -1;
    }
    while (vc.size() <= x.fi)
      vc.push_back(vector<int>());
    debug(x);
    vc[x.fi].push_back(x.se);
  }

  // debug(uno, dos);
  auto p = solve(uno, na);
  auto q = solve(dos, nb);
  debug(p, q);
  cout << (min(p, q) == -1 || p != q ? "No" : "Yes") << '\\n';
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

template <typename T> string to_string(const vector<T> &);

template <typename T> string to_string(const vector<T> &vc, int w) {
  if (w + 1 == vc.size())
    return to_string(vc[w]);
  return to_string(vc[w]) + "," + to_string(vc, w + 1);
}

template <typename T> string to_string(const pair<T, T> &x) {
  return "(" + to_string(x.fi) + "," + to_string(x.se) + ")";
}

template <typename T> string to_string(const vector<T> &vc) {
  return "{" + to_string(vc, 0) + "}";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;

void no() {
  cout << "No\\n";
  exit(0);
}

ii gt(const string &str) {
  int opn = 0, cls = 0;
  for (auto el : str) {
    if (el == '(')
      opn++;
    else if (opn)
      opn--;
    else
      cls++;
  }
  return ii(cls, opn - cls);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif

  int n;
  cin >> n;
  vector<vector<int>> vc;
  for (int i = 0; i < n; i++) {
    string inp;
    cin >> inp;
    ii x = gt(inp);
    while (vc.size() <= x.fi)
      vc.push_back(vector<int>());
    debug(x);
    vc[x.fi].push_back(x.se);
  }

  int d = 0, opt = -1;
  priority_queue<ii> pq;
  for (int i = 0; i < n; i++) {
    while (opt < d) {
      opt++;
      for (auto el : vc[opt])
        pq.push(ii(el, opt));
    }
    while (pq.size() && pq.top().se > d)
      pq.pop();
    if (pq.empty())
      no();
    d += pq.top().fi;
    pq.pop();
  }

  cout << (d ? "No" : "Yes") << '\\n';
}