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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
// #define F first
// #define S second
#define debug(x) cout << #x << ": " << x << '\\n';
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;

  // "(" を +1, ")" を -1 として扱う

  struct Unit {
    int m, p;
  };

  vector<Unit> us(N);
  F0R(i, N) {
    string s;
    cin >> s;

    int low = 0, sum = 0;
    for (auto c : s) {
      int n = c == ')' ? -1 : 1;
      sum += n;
      chmin(low, sum);
    }
    us[i] = {-low, sum};
  }

  sort(us.begin(), us.end(), [](const Unit &l, const Unit &r) -> bool {
    // マイナスがないなら、それらを最優先で使う
    if (l.m == 0 || r.m == 0) {
      return l.m != r.m ? l.m < r.m : l.p > r.p;
    }

    // マイナスがある場合、合計でプラスになるものから優先して使う

    // どちらもプラスなら、マイナスが少ない方から
    if (l.p >= 0 && r.p >= 0) {
      return l.m < r.m;
    }

    // 片方だけプラスなら、そちらから使う
    if (l.p >= 0 || r.p >= 0) {
      return l.p > r.p;
    }

    // どちらもマイナスなら、マイナスと合計に差がある、つまり
    // )))((のような形から使う
    return l.p + l.m > r.p + r.m;
  });

  i64 cur = 0;
  bool result = true;

  auto check = [&cur, &result](const Unit &u) -> bool {
    // cout << " -" << u.m << " +" << u.p << newl;

    cur -= u.m;
    if (cur < 0) {
      result = false;
      return false;
    }
    cur += u.m + u.p;
    return true;
  };

  auto calc = [&check](const vector<Unit> &us) {
    for (auto u : us) {
      if (!check(u))
        return;
    }
  };

  calc(us);
  if (cur != 0)
    result = false;

  cout << (result ? "Yes" : "No");
}
#endif


Error: WA

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
// #define F first
// #define S second
#define debug(x) cout << #x << ": " << x << '\\n';
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;

  struct Unit {
    int m, p;
  };

  vector<Unit> us(N);
  F0R(i, N) {
    string s;
    cin >> s;

    int low = 0, sum = 0;
    for (auto c : s) {
      int n = c == ')' ? -1 : 1;
      sum += n;
      chmin(low, sum);
    }
    us[i] = {-low, sum};
  }

  sort(us.begin(), us.end(), [](const Unit &l, const Unit &r) -> bool {
    if (l.m <= 0)
      return true;
    if (r.m <= 0)
      return true;

    if (l.p < 0 || r.p < 0) {
      return l.p > r.p;
    }

    return l.m < r.m;
  });

  i64 cur = 0;
  bool result = false;
  F0R(i, N) {
    cout << "" << i << ": -" << us[i].m << " +" << us[i].p << newl;
    cur -= us[i].m;
    if (cur < 0) {
      result = false;
      break;
    }
    cur += us[i].m + us[i].p;
  }
  if (cur != 0)
    result = false;
  else
    result = true;

  // ( +1 ) -1

  cout << (result ? "Yes" : "No");
}
#endif