Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  auto f = [](string s) {
    int left = 0, right = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '(')
        right++;
      if (s[i] == ')') {
        if (right > 0)
          right--;
        else
          left++;
      }
    }
    return make_pair(left, right);
  };
  int N;
  cin >> N;
  pair<int, int> bra[N];
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    bra[i] = f(S);
  }
  auto comp = [](pair<int, int> l, pair<int, int> r) {
    if (l.first == 0)
      return true;
    if (r.first == 0)
      return false;
    if (r.second == 0)
      return true;
    if (l.second == 0)
      return false;
    return l.first == r.first ? l.second > r.second : l.first < r.first;
  };
  sort(bra, bra + N, comp);
  debugArray(bra, N);
  int buf = 0;
  bool isok = true;
  for (int i = 0; i < N; i++) {
    debug(buf);
    debug(bra[i].first);
    if (buf < bra[i].first) {
      isok = false;
      break;
    }
    buf += bra[i].second - bra[i].first;
  }
  debug(buf);
  isok &= buf == 0;
  debug(buf);
  cout << (isok ? "Yes" : "No") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  auto f = [](string s) {
    int left = 0, right = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '(')
        right++;
      if (s[i] == ')') {
        if (right > 0)
          right--;
        else
          left++;
      }
    }
    return make_pair(left, right);
  };
  int N;
  cin >> N;
  pair<int, int> bra[N];
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    bra[i] = f(S);
  }
  auto comp = [](pair<int, int> l, pair<int, int> r) {
    if (l.first - l.second < 0 && r.first - r.second < 0)
      return l.first < r.first;
    if (l.first - l.second > 0 && r.first - r.second > 0)
      return l.second > r.second;
    return l.first - l.second < r.first - r.second;
  };
  sort(bra, bra + N, comp);
  debugArray(bra, N);
  int buf = 0;
  bool isok = true;
  for (int i = 0; i < N; i++) {
    debug(buf);
    debug(bra[i].first);
    if (buf < bra[i].first) {
      isok = false;
      break;
    }
    buf += bra[i].second - bra[i].first;
  }
  debug(buf);
  isok &= buf == 0;
  debug(buf);
  cout << (isok ? "Yes" : "No") << endl;
  return 0;
}