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

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

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  using P = pair<ll, ll>;
  vector<P> X(n);
  vector<P> pl, mi;

  rep(i, n) {
    ll minval, tot;
    minval = 0;
    tot = 0;
    for (auto c : s[i]) {
      if (c == '(')
        tot++;
      else
        tot--;
      chmin(minval, tot);
    }
    X[i] = mp(minval, tot);
    if (tot >= 0)
      pl.emplace_back(minval, tot);
    else
      mi.emplace_back(minval, tot);
  }

  // sort(ALL(X), greater<P>());
  sort(ALL(pl), greater<P>());
  sort(ALL(mi),
       [](P a, P b) { return a.second - a.first > b.second - b.first; });
  // sort(ALL(mi));

  // for(auto p: X)cout<<p.first<<" "<<p.second<<endl;
  ll now = 0;
  // minが大きい順
  for (auto p : pl) {
    if (now + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += p.second;
  }

  // minが大きい順
  for (auto p : mi) {
    if (now + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += p.second;
  }

  if (now == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}


Error: -11

Fixed submission:

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

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  using P = pair<ll, ll>;
  vector<P> X(n);
  vector<P> pl, mi;
  vector<ll> mid;

  rep(i, n) {
    ll minval, tot;
    minval = 0;
    tot = 0;
    for (auto c : s[i]) {
      if (c == '(')
        tot++;
      else
        tot--;
      chmin(minval, tot);
    }
    X[i] = mp(minval, tot);
    if (tot > 0)
      pl.emplace_back(minval, tot);
    else if (tot == 0)
      mid.emplace_back(minval);
    else
      mi.emplace_back(minval, tot);
  }

  // sort(ALL(X), greater<P>());
  sort(ALL(pl), greater<P>());
  // sort(ALL(mi), greater<P>());
  // sort(ALL(mi));

  // for(auto p: X)cout<<p.first<<" "<<p.second<<endl;
  ll now = 0;
  // minが大きい順
  for (auto p : pl) {
    if (now + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += p.second;
  }
  if (*min_element(ALL(mid)) > now) {
    cout << "No" << endl;
    return 0;
  }

  for (auto p : mi) {
    now += p.second;
  }

  if (now == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}