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

#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\\n'
using namespace std;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
using P = pair<int, int>;

P get_num(string &str) { // first: 開始位置から最大いくつ下るか  second:
                         // 最終的にいくつ増減するか
  int n = (int)str.size();
  P ret(0, 0);
  int h = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '(') {
      h++;
    } else {
      h--;
      chmin(ret.first, h);
    }
  }
  ret.second = h;
  return ret;
}

bool comp(P l, P r) {
  if (l.second > r.second) {
    return l.second > r.second;
  } else {
    return l.first > r.first;
  }
}

int main() {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
  }

  vector<P> L, R; // 左右から見た時の、first: 開始からどれだけ下がるか second:
                  // 開始からどれだけ上がるか
  for (int i = 0; i < N; i++) {
    int n = (int)S[i].size();
    int b = 0;
    int h = 0;
    for (int j = 0; j < n; j++) {
      if (S[i][j] == '(') {
        h++;
      } else {
        h--;
        chmin(b, h);
      }
    }
    if (h > 0) { // 右肩上がりのもの
      L.push_back(P(b, h));
    } else { // 右肩下がりのもの
      R.push_back(P(b - h, -h));
    }
  }

  sort(ALL(L), greater<P>());
  sort(ALL(R), greater<P>());

  int l = 0;
  for (int i = 0; i < (int)L.size(); i++) {
    // cerr << l << " " << L[i].first << " " << L[i].second << endl;
    if (l + L[i].first < 0) {
      cout << "No\\n";
      return 0;
    }
    l += L[i].second;
  }
  int r = 0;
  for (int i = 0; i < (int)R.size(); i++) {
    // cerr << r << " " << R[i].first << " " << R[i].second << endl;
    if (r + R[i].first < 0) {
      cout << "No\\n";
      return 0;
    }
    r += R[i].second;
  }
  if (l == r) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
}

/*
4
)((
))(((
)))
(
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\\n'
using namespace std;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
using P = pair<int, int>;

P get_num(string &str) { // first: 開始位置から最大いくつ下るか  second:
                         // 最終的にいくつ増減するか
  int n = (int)str.size();
  P ret(0, 0);
  int h = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '(') {
      h++;
    } else {
      h--;
      chmin(ret.first, h);
    }
  }
  ret.second = h;
  return ret;
}

bool comp(P l, P r) {
  if (l.second > r.second) {
    return l.second > r.second;
  } else {
    return l.first > r.first;
  }
}

int main() {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
  }

  vector<P> num(N);
  for (int i = 0; i < N; i++) {
    num.at(i) = get_num(S[i]);
  }

  vector<P> L, R;
  for (int i = 0; i < N; i++) {
    P now = num[i];
    if (now.second > 0) { // 右肩上がりのもの
      L.push_back(now);
    } else { // 右肩下がりのもの
      R.push_back(now);
    }
  }
  sort(ALL(L), greater<P>()); // 開始点から下る量が0に近い順
  sort(ALL(R), comp);         // 開始点から上る量が0に遠い順

  int now = 0;
  for (int i = 0; i < (int)L.size(); i++) {
    // cerr << L[i].first << " " << L[i].second << endl;
    if (now + L[i].first < 0) {
      cout << "No\\n";
      return 0;
    }
    now += L[i].second;
  }
  for (int i = 0; i < (int)R.size(); i++) {
    // cerr << R[i].first << " " << R[i].second << endl;
    if (now + R[i].first < 0) {
      cout << "No\\n";
      return 0;
    }
    now += R[i].second;
  }
  if (now == 0) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
}

/*
4
)((
))(((
)))
(
*/