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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  vector<pair<int, int>> plq;
  vector<pair<int, int>> miq;
  vector<pair<int, int>> equ;

  rep(i, n) {
    int lowmax = 0;
    int tot = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        tot++;
      else
        tot--;
      lowmax = min(lowmax, tot);
    }
    if (tot > 0)
      plq.push_back({lowmax, tot});
    else if (tot == 0)
      equ.push_back({lowmax, tot});
    else
      miq.push_back({lowmax, tot});
  }

  sort(miq.begin(), miq.end(),
       [](pair<int, int> const &lef, pair<int, int> const &rig) {
         return lef.second + rig.first > rig.second + lef.first;
       });
  sort(equ.rbegin(), equ.rend());
  sort(plq.rbegin(), plq.rend());
  ll ans = 0;
  rep(j, plq.size()) {
    if (ans + plq[j].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    ans += plq[j].second;
  }
  rep(j, equ.size()) {
    if (ans + equ[j].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    ans += equ[j].second;
  }
  rep(j, miq.size()) {
    // cout << miq[j].first <<" " <<  miq[j].second << endl;
    if (ans + miq[j].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    ans += miq[j].second;
  }
  if (ans == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  // cout << endl << endl;
  // cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  vector<pair<int, int>> plq;
  vector<pair<int, int>> miq;
  vector<pair<int, int>> equ;

  rep(i, n) {
    int lowmax = 0;
    int tot = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        tot++;
      else
        tot--;
      lowmax = min(lowmax, tot);
    }
    if (tot > 0)
      plq.push_back({lowmax, tot});
    else if (tot == 0)
      equ.pb({lowmax, tot});
    else
      miq.push_back({lowmax, tot});
  }

  sort(miq.begin(), miq.end(), [](pair<int, int> lef, pair<int, int> rig) {
    return lef.first + lef.second - rig.first >
           rig.first + rig.second + lef.first;
  });
  sort(equ.rbegin(), equ.rend());
  sort(plq.rbegin(), plq.rend());
  ll ans = 0;
  rep(j, plq.size()) {
    if (ans + plq[j].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    ans += plq[j].second;
  }
  rep(j, equ.size()) {
    if (ans + equ[j].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    ans += equ[j].second;
  }
  rep(j, miq.size()) {
    // cout << miq[j].first <<" " <<  miq[j].second << endl;
    if (ans + miq[j].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    ans += miq[j].second;
  }
  if (ans == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  // cout << endl << endl;
  // cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
}