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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
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;
}
int main() {
  ll n;
  cin >> n;
  vpl left, right;
  rep(i, n) {
    string s;
    cin >> s;
    ll tot = 0, mi = 0;
    rep(j, s.size()) {
      if (s[j] == '(')
        tot++;
      else
        tot--;
      chmin(mi, tot);
    }
    if (tot > 0)
      left.pb({mi, tot});
    else
      right.pb({mi - tot, -tot});
  }
  sort(all(left));
  sort(all(right));
  rev(all(left));
  rev(all(right));
  // for(auto p:right)cout << p.fi <<" " <<p.se <<endl;
  bool check = true;
  ll total = 0;
  rep(i, left.size()) {
    if (total < -left[i].fi)
      check = false;
    total += left[i].se;
  }
  ll whole = 0;
  rep(j, right.size()) {
    if (whole < -right[j].fi)
      check = false;
    whole += right[j].se;
  }
  if (check && total == whole)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
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;
}
int main() {
  ll n;
  cin >> n;
  vector<multiset<ll>> st;
  rep(i, n) {
    string s;
    cin >> s;
    ll tot = 0, mi = 0;
    rep(j, s.size()) {
      if (s[j] == '(')
        tot++;
      else
        tot--;
      chmin(mi, tot);
    }
    st[-mi].ins(tot);
  }
  priority_queue<ll, vl, less<ll>> que;
  ll now = 0, pre = -1;
  bool check = true;
  while (n) {
    rep(i, now - pre) {
      for (auto p : st[i + pre + 1]) {
        que.push(p);
      }
    }
    if (que.empty()) {
      check = false;
      break;
    }
    ll pick = que.top();
    que.pop();
    pre = now;
    now += pick;
    n--;
    if (now < 0) {
      check = false;
      break;
    }
  }
  if (check && now == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}