Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define equals(a, b) (fabs((a) - (b)) < EPS)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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;
}

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

template <typename T> void print_array(const T &ary, int size) {
  REP(i, size) { cout << ary[i] << " "; }
  cout << endl;
}

const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);

bool cmp(const PII &a, const PII &b) {
  int af = a.first, as = a.second;
  int bf = b.first, bs = b.second;

  if (as > 0 and bs > 0) {
    return af >= bf;
  } else if (as > 0 and bs <= 0) {
    return true;
  } else if (as <= 0 and bs > 0) {
    return false;
  } else {
    // int da = -af - (-as);
    // int db = -bf - (-bs);
    // return da >= db;
    return af - as < bf - bs;
  }

  // if(as > 0 and bs <= 0) return true;
  // if(as <= 0 and bs > 0) return false;
  // if(as < 0) return af - as < bf - bs;
  // else return af > bf;
}

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);

  int N;
  cin >> N;
  vector<pair<int, int>> S;
  int sum = 0;
  REP(i, N) {
    string s;
    cin >> s;
    int acc = 0;
    int tmin = 0;
    for (char c : s) {
      if (c == '(') {
        acc++;
      } else {
        acc--;
      }
      chmin(tmin, acc);
    }
    S.eb(tmin, acc);
    sum += acc;
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  sort(S.begin(), S.end(), cmp);
  int now = 0;
  int tmin = 0;
  REP(i, N) {
    int a, b;
    tie(a, b) = S[i];
    chmin(tmin, now + a);
    now += b;
  }
  if (tmin == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define equals(a, b) (fabs((a) - (b)) < EPS)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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;
}

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

template <typename T> void print_array(const T &ary, int size) {
  REP(i, size) { cout << ary[i] << " "; }
  cout << endl;
}

const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);

bool cmp(const PII &a, const PII &b) {
  int af = a.first, as = a.second;
  int bf = b.first, bs = b.second;

  if (as > 0 and bs > 0) {
    return af > bf;
  } else if (as > 0 and bs <= 0) {
    return true;
  } else if (as <= 0 and bs > 0) {
    return false;
  } else {
    // int da = -af - (-as);
    // int db = -bf - (-bs);
    // return da >= db;
    return af - as < bf - bs;
  }

  // if(as > 0 and bs <= 0) return true;
  // if(as <= 0 and bs > 0) return false;
  // if(as < 0) return af - as < bf - bs;
  // else return af > bf;
}

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);

  int N;
  cin >> N;
  vector<pair<int, int>> S;
  int sum = 0;
  REP(i, N) {
    string s;
    cin >> s;
    int acc = 0;
    int tmin = 0;
    for (char c : s) {
      if (c == '(') {
        acc++;
      } else {
        acc--;
      }
      chmin(tmin, acc);
    }
    S.eb(tmin, acc);
    sum += acc;
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  sort(S.begin(), S.end(), cmp);
  int now = 0;
  int tmin = 0;
  REP(i, N) {
    int a, b;
    tie(a, b) = S[i];
    chmin(tmin, now + a);
    now += b;
  }
  if (tmin == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}