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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 998244353, MAX = 200005, INF = 1 << 30;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int sum = 0;
  vector<pair<int, int>> A, B, C, D;

  for (int i = 0; i < N; i++) {
    string U;
    cin >> U;
    int mi = si(U), now = 0;

    for (char x : U) {
      if (x == '(')
        now++;
      else
        now--;
      chmin(mi, now);
    }

    sum += now;

    if (now >= 0) {
      if (mi >= 0)
        A.push_back({now, mi});
      else
        B.push_back({now, mi});
    } else {
      if (mi >= 0)
        C.push_back({now, mi});
      else
        D.push_back({now, mi});
    }
  }

  sort(all(B), [](auto a, auto b) { return a.se > b.se; });

  sort(all(D), [](auto a, auto b) { return a.se - a.fi < b.se - b.fi; });

  int now = 0;

  bool ok = true;
  if (sum)
    ok = false;

  for (int i = 0; i < si(A); i++) {
    now += A[i].fi;
  }

  for (int i = 0; i < si(B); i++) {
    if (now + B[i].se < 0)
      ok = false;
    now += B[i].fi;
  }

  for (int i = 0; i < si(D); i++) {
    if (now + D[i].se < 0)
      ok = false;
    now += D[i].fi;
  }

  for (int i = 0; i < si(C); i++) {
    if (now + C[i].se < 0)
      ok = false;
    now += C[i].fi;
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 998244353, MAX = 200005, INF = 1 << 30;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int sum = 0;
  vector<pair<int, int>> S(N);

  for (int i = 0; i < N; i++) {
    string U;
    cin >> U;
    int mi = 10, now = 0;

    for (char x : U) {
      if (x == '(')
        now++;
      else
        now--;
      chmin(mi, now);
    }

    S[i] = mp(now, mi);
    sum += now;
  }

  sort(all(S), [](auto a, auto b) {
    if (a.se > 0) {
      return a.se > b.se;
    } else {
      if (b.se > 0)
        return b < a;
      else {
        return a.fi > b.fi;
      }
    }
  });

  // for(int i=0;i<N;i++) cout<<S[i].fi<<" "<<S[i].se<<endl;

  int now = 0;

  bool ok = true;
  if (sum)
    ok = false;

  for (int i = 0; i < N; i++) {
    if (now + S[i].se < 0)
      ok = false;
    now += S[i].fi;
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}