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

/** 	Author: Apoorva Mittal  **/
/**   Published: 13/05/2020 01:07:32 **/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<pair<int, int>>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i >= b; i--)
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define init(i, k) memset(i, k, sizeof i)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define inp(a) rep(i, 0, a.size()) cin >> a[i];
//--------------------------------------------debugging
//showoff---------------------------------------------------------
#define out(a)                                                                 \\
  cout << #a << ": " << endl;                                                  \\
  rep(i, 0, a.size()) cout << a[i] << " ";                                     \\
  cout << endl;
#define out2d(a)                                                               \\
  cout << #a << ": " << endl;                                                  \\
  rep(i, 0, a.size()) {                                                        \\
    rep(j, 0, a[0].size()) cout << a[i][j] << " ";                             \\
    cout << endl;                                                              \\
  }
#define sim template <class c
#define ris return *this
#define dor > deb &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, deb &>::type operator<<(c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct deb {
#ifdef LOCAL
  ~deb() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define show(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// -DLOCAL
//--------------------------------------------useless
//stuff---------------------------------------------------------------------
#define flush fflush(stdout);
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define mxpque(T) priority_queue<T>
#define mnpque(T) priority_queue<T, vector<T>, greater<T>>
#define precise(x) cout << fixed << showpoint << setprecision(x);
#define setbits(x) __builtin_popcountll(x)
#define lastsetbit(x)                                                          \\
  __builtin_clz(x) // "31-lastsetbit(x)" to find 0 based index of last set bit
                   // from right
#define gcd(a, b) __gcd(a, b)
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
//------------------------------------------------------------------------------------------------------------------------------
const int mod = 1e9 + 7;
// ====== Useful functions begin ======
inline int add(int a, int b) { return ((a % mod) + (b % mod)) % mod; }
inline int sub(int a, int b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }
// ====== Useful functions end ======
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
//--------------------------------------------------------------------------------------------------------------------------------
const int inf = 1e18;
const int maxn = 1e3 + 5;
struct p {
  int o;
  int c;
};
//--------------------------------------------------------------------------------------------------------------------------------
bool fnc(p a, p b) {
  int d1 = a.c - a.o;
  int d2 = b.c - b.o;
  // if (d1 < 0 && d2 > 0)
  //     return 1;
  // if (d1 < 0 && d2 < 0)
  // {
  //     return a.c < b.c;
  // }
  // else if (d1 > 0 && d2 > 0)
  // {
  //     return a.o > b.o;
  // }
  // if (d1 < 0 && d2 == 0)
  //     return 1;
  // if (d1 == 0 && d2 > 0)
  //     return 1;

  if (d1 <= 0 && d2 > 0) {
    return true;
  } else if (d1 <= 0 && d2 <= 0) {
    return a.c < b.c;
  } else if (d1 > 0 && d2 <= 0) {
    return false;
  } else if (d1 > 0 && d2 > 0) {
    return a.o > b.o;
  }
}
void solve() {
  int n;
  cin >> n;
  vector<string> arr(n);
  cin >> arr;
  vector<p> a2;
  for (int i = 0; i < n; i++) {
    int o = 0;
    int c = 0;
    for (auto ch : arr[i]) {
      if (ch == '(')
        o++;
      else {
        if (o > 0)
          o--;
        else
          c++;
      }
    }
    a2.pb({o, c});
  }
  sort(all(a2), fnc);
  // deb() << show(a2);
  // for (auto p : a2)
  // {
  //     cout << p.c << " " << p.o << ", ";
  // }
  // cout << endl;

  int sum = 0;
  for (auto p : a2) {

    sum -= p.c;
    if (sum < 0) {
      cout << "No" << endl;
      return;
    }
    sum += p.o;
  }
  if (sum == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
signed main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  speed;
  int t = 1;
  // cin >> t;

  for (int tt = 1; tt <= t; tt++) {
    // cout<<"Case #"<<tt<<": ";
    solve();
  }

  // CERTAIN STUFF to REMEMBER::
  // 1.Check integer overflow
  // 2.Check edge case n=1 probable
  // 3.If conditions can be checked without bits, then never check with bits,
  // never try to be oversmart! 4.WA can come bcz of very silly things ->  base
  // case, if with bits,bla bla
}


Error: 0

Fixed submission:

/** 	Author: Apoorva Mittal  **/
/**   Published: 13/05/2020 01:07:32 **/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<pair<int, int>>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i >= b; i--)
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define init(i, k) memset(i, k, sizeof i)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define inp(a) rep(i, 0, a.size()) cin >> a[i];
//--------------------------------------------debugging
//showoff---------------------------------------------------------
#define out(a)                                                                 \\
  cout << #a << ": " << endl;                                                  \\
  rep(i, 0, a.size()) cout << a[i] << " ";                                     \\
  cout << endl;
#define out2d(a)                                                               \\
  cout << #a << ": " << endl;                                                  \\
  rep(i, 0, a.size()) {                                                        \\
    rep(j, 0, a[0].size()) cout << a[i][j] << " ";                             \\
    cout << endl;                                                              \\
  }
#define sim template <class c
#define ris return *this
#define dor > deb &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, deb &>::type operator<<(c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct deb {
#ifdef LOCAL
  ~deb() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define show(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// -DLOCAL
//--------------------------------------------useless
//stuff---------------------------------------------------------------------
#define flush fflush(stdout);
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define mxpque(T) priority_queue<T>
#define mnpque(T) priority_queue<T, vector<T>, greater<T>>
#define precise(x) cout << fixed << showpoint << setprecision(x);
#define setbits(x) __builtin_popcountll(x)
#define lastsetbit(x)                                                          \\
  __builtin_clz(x) // "31-lastsetbit(x)" to find 0 based index of last set bit
                   // from right
#define gcd(a, b) __gcd(a, b)
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
//------------------------------------------------------------------------------------------------------------------------------
const int mod = 1e9 + 7;
// ====== Useful functions begin ======
inline int add(int a, int b) { return ((a % mod) + (b % mod)) % mod; }
inline int sub(int a, int b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }
// ====== Useful functions end ======
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
//--------------------------------------------------------------------------------------------------------------------------------
const int inf = 1e18;
const int maxn = 1e3 + 5;
struct p {
  int o;
  int c;
};
//--------------------------------------------------------------------------------------------------------------------------------
bool fnc(p a, p b) {
  int d1 = a.c - a.o;
  int d2 = b.c - b.o;
  if (d1 < 0 && d2 > 0)
    return 1;
  if (d1 < 0 && d2 < 0) {
    return a.c <= b.c;
  } else if (d1 > 0 && d2 > 0) {
    return a.c <= b.c;
  }
  if (d1 < 0 && d2 == 0)
    return 1;
  if (d1 == 0 && d2 > 0)
    return 1;
  return a.c <= b.c;
}
void solve() {
  int n;
  cin >> n;
  vector<string> arr(n);
  // cin >> arr;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  vector<p> a2;
  for (int i = 0; i < n; i++) {
    int o = 0;
    int c = 0;
    for (auto ch : arr[i]) {
      if (ch == '(')
        o++;
      else {
        if (o > 0)
          o--;
        else
          c++;
      }
    }
    a2.pb({o, c});
  }
  sort(all(a2), fnc);
  // deb() << show(a2);
  // for (auto p : a2)
  // {
  //     cout << p.c << " " << p.o << ", ";
  // }
  // cout << endl;

  int sum = 0;
  for (auto p : a2) {

    sum -= p.c;
    if (sum < 0) {
      cout << "No" << endl;
      return;
    }
    sum += p.o;
  }
  if (sum == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
signed main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  speed;
  int t = 1;
  // cin >> t;

  for (int tt = 1; tt <= t; tt++) {
    // cout<<"Case #"<<tt<<": ";
    solve();
  }

  // CERTAIN STUFF to REMEMBER::
  // 1.Check integer overflow
  // 2.Check edge case n=1 probable
  // 3.If conditions can be checked without bits, then never check with bits,
  // never try to be oversmart! 4.WA can come bcz of very silly things ->  base
  // case, if with bits,bla bla
}