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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const ll MOD = 998244353;
const ll MAX_N = 500010;
const ll INF = 999999999999;

bool comp(pair<ll, ll> p1, pair<ll, ll> p2) { return p1.first > p2.first; }

bool check(vector<pair<ll, ll>> vp) {
  ll h = 0;
  for (pair<ll, ll> p : vp) {
    if (h + p.first < 0) {
      return false;
    }
    h += p.second;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> pos, neg;
  ll total = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int h = 0;
    int b = 0;
    for (char c : s) {
      if (c == '(') {
        h++;
      } else {
        h--;
      }
      b = min(b, h);
    }
    if (h >= 0) {
      pos.push_back(make_pair(b, h));
    } else {
      neg.push_back(make_pair(b - h, -h));
    }
    total += h;
  }
  sort(pos.begin(), pos.end());
  reverse(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  reverse(neg.begin(), neg.end());
  // for(pair<ll,ll> p:pos){
  //     cout<<p.first<<" "<<p.second<<endl;
  // }
  if (check(pos) && check(neg) && total == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first)
    return (a.second > b.second);
  else {
    return a.first < b.second;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll q;
  cin >> q;
  vector<pair<ll, ll>> v;
  while (q--) {
    string s;
    cin >> s;
    stack<char> st;
    for (ll i = 0; i < s.size(); i++) {
      if (st.size() == 0) {
        st.push(s[i]);
        continue;
      }
      if (s[i] == '(') {
        st.push(s[i]);
      } else {
        if (st.top() == '(') {
          st.pop();
        } else {
          st.push(s[i]);
        }
      }
    }
    ll a = 0, b = 0;
    while (st.size()) {
      if (st.top() == '(') {
        a++;
      } else {
        b++;
      }
      st.pop();
    }
    v.push_back(make_pair(b, a));
  }
  sort(v.begin(), v.end(), sortbysec);
  stack<char> st;
  for (ll i = 0; i < v.size(); i++) {
    // cout<<v[i].first<<" "<<v[i].second<<"\\n";
    for (ll j = 0; j < v[i].first; j++) {
      if (st.size() == 0) {
        st.push(')');
      } else {
        if (st.top() == '(') {
          st.pop();
        } else {
          st.push(')');
        }
      }
    }
    for (ll j = 0; j < v[i].second; j++) {
      st.push('(');
    }
  }
  if (st.size() == 0) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
}