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

#line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\\n";
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

#line 2 "tmp.cpp"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<string> str(n);
  REP(i, n) { cin >> str[i]; }

  vector<pair<int, int>> up, down;
  for (const auto s : str) {
    int bottom = 0;
    int left = 0;
    int depth = 0;
    for (const auto c : s) {
      if (c == '(') {
        left++;
        depth--;
      } else {
        depth++;
        left--;
      }
      chmax(bottom, depth);
    }

    if (left >= 0) {
      up.push_back({bottom, left});
    } else {
      down.push_back({bottom + left, -left});
    }
  }

  auto comp = [](pair<int, int> r, pair<int, int> l) -> bool {
    if (r.first > l.first) {
      return false;
    }
    if (r.first < l.first) {
      return true;
    }
    return r.second > l.second;
  };

  sort(ALL(up), comp);
  sort(ALL(down), comp);
  int up_cur = 0;
  for (const auto e : up) {
    if (up_cur - e.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    up_cur += e.second;
  }

  int down_cur = 0;
  for (const auto e : down) {
    if (down_cur - e.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    down_cur += e.second;
  }
  say<string>(up_cur == down_cur);
  return 0;
}


Error: 0

Fixed submission:

#line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << endl;
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

#line 2 "tmp.cpp"

bool compare(pair<int, int> a, pair<int, int> b) {
  if (a.second == 0)
    return true;
  if (b.second == 0)
    return false;
  if (a.first != b.first) {
    return a.first > b.first;
  } else {
    return a.second < b.second;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<string> s(n);
  REP(i, n) { cin >> s[i]; }

  vi left(n, 0), right(n, 0);
  REP(i, n) {
    int size = s[i].length();
    REP(j, size) {
      if (s[i][j] == '(') {
        left[i]++;
      } else {
        if (left[i] > 0) {
          left[i]--;
        } else {
          right[i]++;
        }
      }
    }
    // cout << left[i] MM right[i] << endl;
  }

  vector<pair<int, int>> lr(n);
  REP(i, n) { lr[i] = {left[i], right[i]}; }
  sort(lr.begin(), lr.end(), compare);
  int cur = 0;
  REP(i, n) {
    // cout << lr[i].first MM lr[i].second << endl;
    cur -= lr[i].second;
    if (cur < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += lr[i].first;
  }
  say(cur == 0, "Yes", "No");
  return 0;
}