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

#include <algorithm>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define MOD 1000000007
#define EPS 1e-10
#define ll long long
#define ld long double
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rmsame(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define rmvector(a, b)                                                         \\
  rep(i, a.size()) rep(j, b.size()) if (a[i] == b[j]) {                        \\
    a.erase(a.begin() + i);                                                    \\
    i--;                                                                       \\
    break;                                                                     \\
  }
#define first_more_idx(ve, num)                                                \\
  upper_bound(all(ve), num) - ve.begin() // idx = first_more_idx(ve,num); if(idx
                                         // = ve.size()) , then not exist
#define first_lessequal_idx(ve, num) lower_bound(all(ve), num) - ve.begin()
#define pq_pair_tB                                                             \\
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
#define pq_pair_ts priority_queue<pair<ll, ll>> // only sort first param
template <typename X> bool exist(vector<X> vec, X item) {
  return find(all(vec), item) != vec.end();
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll x, ll n) {
  if (n == 0)
    return 1;
  return (n % 2) ? x * power(x, n - 1) % MOD : power(x * x % MOD, n / 2);
}
ll dx[] = {0, 0, -1, 1};
ll dy[] = {1, -1, 0, 0}; // up,down,left right

bool func(pair<ll, ll> a, pair<ll, ll> b) { return a.first > b.first; }

pair<bool, ll> cal(vector<pair<ll, ll>> &V) {
  pair<bool, ll> ans = mp(false, 0);
  ll cnt = 0;
  rep(i, V.size()) {
    cnt += V[i].first;
    if (cnt < 0)
      return ans;
    cnt += V[i].second;
    if (cnt < 0)
      return ans;
  }

  ans.first = true;
  ans.second = cnt;
  return ans;
}

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

  ll N;
  cin >> N;
  vector<pair<ll, ll>> P, M;

  rep(i, N) {
    string inp;
    cin >> inp;

    ll cnt = 0;
    ll min_cnt = 0;

    rep(j, inp.size()) {
      if (inp[j] == ')')
        cnt--;
      else
        cnt++;

      min_cnt = min(cnt, min_cnt);
    }

    if (cnt > 0)
      P.pb(mp(min_cnt, cnt - min_cnt));
    else {
      cnt = 0;
      min_cnt = 0;
      for (ll j = inp.size() - 1; j >= 0; j--) {
        if (inp[j] == ')')
          cnt++;
        else
          cnt--;

        min_cnt = min(cnt, min_cnt);
      }

      M.pb(mp(min_cnt, cnt - min_cnt));
    }
  }

  sort(all(P), func);
  sort(all(M), func);

  pair<bool, ll> p = cal(P);
  pair<bool, ll> m = cal(M);

  if (!p.first or !m.first or p.second != m.second) {
    cout << "No" << endl;
  } else
    cout << "Yes" << endl;

  // cout << fixed << setprecision(16) << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define MOD 1000000007
#define EPS 1e-10
#define ll long long
#define ld long double
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rmsame(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define rmvector(a, b)                                                         \\
  rep(i, a.size()) rep(j, b.size()) if (a[i] == b[j]) {                        \\
    a.erase(a.begin() + i);                                                    \\
    i--;                                                                       \\
    break;                                                                     \\
  }
#define first_more_idx(ve, num)                                                \\
  upper_bound(all(ve), num) - ve.begin() // idx = first_more_idx(ve,num); if(idx
                                         // = ve.size()) , then not exist
#define first_lessequal_idx(ve, num) lower_bound(all(ve), num) - ve.begin()
#define pq_pair_tB                                                             \\
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
#define pq_pair_ts priority_queue<pair<ll, ll>> // only sort first param
template <typename X> bool exist(vector<X> vec, X item) {
  return find(all(vec), item) != vec.end();
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll x, ll n) {
  if (n == 0)
    return 1;
  return (n % 2) ? x * power(x, n - 1) % MOD : power(x * x % MOD, n / 2);
}
ll dx[] = {0, 0, -1, 1};
ll dy[] = {1, -1, 0, 0}; // up,down,left right

bool func(pair<ll, ll> a, pair<ll, ll> b) { return a.second >= b.second; }

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

  ll N;
  cin >> N;
  vector<pair<ll, ll>> A;

  rep(i, N) {
    string inp;
    cin >> inp;
    ll num = 0;
    ll minnum = 0;
    rep(j, inp.size()) {
      if (inp[j] == '(')
        num++;
      else
        num--;
      minnum = min(minnum, num);
    }
    A.pb(mp(num, minnum));
  }

  sort(all(A), func);

  ll now = 0;
  rep(i, A.size()) {
    ll a = A[i].second;
    ll b = A[i].first - A[i].second;

    now += a;
    if (now < 0) {
      cout << "No" << endl;
      return 0;
    }

    now += b;
    if (now < 0) {
      cout << "No" << endl;
      return 0;
    }
  }

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

  // cout << fixed << setprecision(16) << ans << endl;
  return 0;
}