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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<string> s(N);
  REP(i, N) { cin >> s[i]; }
  vector<pair<int, int>> ok;
  vector<pair<int, int>> v;
  REP(i, N) {
    int cnt = 0, mi = 0;
    for (auto e : s[i]) {
      if (e == '(')
        ++cnt;
      else
        --cnt;
      CHMIN(mi, cnt);
    }
    if (cnt >= 0)
      ok.emplace_back(mi, cnt);
    else
      v.emplace_back(mi, cnt);
  }
  sort(ALL(ok),
       [](pair<int, int> a, pair<int, int> b) { return a.first > b.first; });
  int sum = 0;
  for (auto [a, b] : ok) {
    if (sum + a < 0) {
      out("No");
      return 0;
    }
    sum += b;
  }
  sort(ALL(v), [](pair<int, int> a, pair<int, int> b) {
    return a.first - a.second < b.first - b.second;
  });
  for (auto [a, b] : v) {
    if (sum + a < 0) {
      out("No");
      return 0;
    }
    sum += b;
  }
  if (sum)
    out("No");
  else
    out("Yes");
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<string> s(N);
  REP(i, N) { cin >> s[i]; }
  vector<pair<int, int>> plus, minus;
  REP(i, N) {
    int cnt = 0;
    int mi = 0;
    REP(j, s[i].size()) {
      if (s[i][j] == '(')
        ++cnt;
      else
        --cnt;
      CHMIN(mi, cnt);
    }
    if (cnt > 0)
      plus.emplace_back(mi, cnt);
    else
      minus.emplace_back(mi, -cnt);
  }
  sort(ALL(plus), [](pair<int, int> a, pair<int, int> b) {
    if (a.first > b.first)
      return true;
    else
      return a.second > b.second;
  });
  sort(ALL(minus));
  REP(i, minus.size()) minus[i].second *= -1;
  int now = 0;
  auto fail = []() {
    out("No");
    exit(0);
  };
  plus.insert(plus.end(), minus.begin(), minus.end());
  for (auto [mi, cnt] : plus) {
    if (now < 0 || now + mi < 0)
      fail();
    now += cnt;
  }
  if (now)
    out("No");
  else
    out("Yes");
}