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

#include <bits/stdc++.h>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
#include <variant>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<tuple<int, int>> up, flat, down;
  REP(i, n) {
    string s;
    cin >> s;
    int sum = 0;
    int mn = 0;
    for (auto &&c : s) {
      if (c == '(') {
        ++sum;
      } else {
        --sum;
        mn = min(mn, sum);
      }
    }
    if (sum > 0) {
      up.emplace_back(mn, sum);
    } else if (sum == 0) {
      flat.emplace_back(mn, sum);
    } else {
      down.emplace_back(mn - sum, sum);
    }
  }
  sort(ALL(up),
       [](auto &&lhs, auto &&rhs) { return get<0>(lhs) > get<0>(rhs); });
  sort(ALL(down),
       [](auto &&lhs, auto &&rhs) { return get<0>(lhs) < get<0>(rhs); });
  int sum = 0;
  bool ok = true;
  for (auto [mn, diff] : up) {
    if (sum + mn < 0)
      ok = false;
    sum += diff;
  }
  for (auto [mn, diff] : flat) {
    if (sum + mn < 0)
      ok = false;
    sum += diff;
  }
  for (auto [mn, diff] : down) {
    sum += diff;
    if (sum + mn < 0)
      ok = false;
  }
  if (sum != 0)
    ok = false;
  if (ok) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
#include <variant>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<tuple<int, int>> up, flat, down;
  REP(i, n) {
    string s;
    cin >> s;
    int sum = 0;
    int mn = 0;
    for (auto &&c : s) {
      if (c == '(') {
        ++sum;
      } else {
        --sum;
        mn = min(mn, sum);
      }
    }
    if (sum > 0) {
      up.emplace_back(mn, sum);
    } else if (sum == 0) {
      flat.emplace_back(mn, sum);
    } else {
      down.emplace_back(mn - sum, sum);
    }
  }
  sort(ALL(up),
       [](auto &&lhs, auto &&rhs) { return get<0>(lhs) > get<0>(rhs); });
  sort(ALL(down),
       [](auto &&lhs, auto &&rhs) { return get<0>(lhs) < get<1>(rhs); });
  int sum = 0;
  bool ok = true;
  for (auto [mn, diff] : up) {
    if (sum + mn < 0)
      ok = false;
    sum += diff;
  }
  for (auto [mn, diff] : flat) {
    if (sum + mn < 0)
      ok = false;
    sum += diff;
  }
  for (auto [mn, diff] : down) {
    sum += diff;
    if (sum + mn < 0)
      ok = false;
  }
  if (sum != 0)
    ok = false;
  if (ok) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
  return 0;
}