Problem p02686 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21,
// stdin), p1 == p2) ? EOF : *p1++) char buf[(1 << 21) + 1], * p1 = buf, * p2 =
// buf; #define int long long
#define lowbit(x) (x & (-x))
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r
#define pb push_back
typedef unsigned long long ull;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<ll> VI;
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define SZ(x) ((int)((x).size()))
#define bug puts("BUG")
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T> inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
#ifdef LOCAL
FILE *_INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT=freopen("output.txt", "w", stdout);
#endif
using namespace std;
const int maxn = 2e6 + 10;
char s[maxn];
pii bracket[maxn];
bool cmp(pii a, pii b) { return b.first + a.second >= a.first + b.second; }
int main() {
  int n;
  int sum = 0;
  read(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    int cur = 0, minn = inf;
    for (int j = 0; s[j]; ++j) {
      cur += s[j] == '(' ? 1 : -1;
      minn = min(minn, cur);
    }
    bracket[i] = pii(cur, minn);
    sum += cur;
  }
  if (sum)
    puts("No");
  else {
    sort(bracket, bracket + n, cmp);
    int cur = 0;
    bool ok = 1;
    for (int i = 0; i < n; ++i) {
      if (bracket[i].second + cur < 0) {
        ok = 0;
        break;
      }
      cur += bracket[i].first;
    }
    puts(ok ? "Yes" : "No");
  }
}

Fixed submission:
// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21,
// stdin), p1 == p2) ? EOF : *p1++) char buf[(1 << 21) + 1], * p1 = buf, * p2 =
// buf; #define int long long
#define lowbit(x) (x & (-x))
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r
#define pb push_back
typedef unsigned long long ull;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<ll> VI;
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define SZ(x) ((int)((x).size()))
#define bug puts("BUG")
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T> inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
#ifdef LOCAL
FILE *_INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT=freopen("output.txt", "w", stdout);
#endif
using namespace std;
const int maxn = 2e6 + 10;
char s[maxn];
pii bracket[maxn];
bool cmp(pii a, pii b) {
  return min(a.second, a.first + b.second) > min(b.second, b.first + a.second);
}
int main() {
  int n;
  int sum = 0;
  read(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    int cur = 0, minn = inf;
    for (int j = 0; s[j]; ++j) {
      cur += s[j] == '(' ? 1 : -1;
      minn = min(minn, cur);
    }
    bracket[i] = pii(cur, minn);
    sum += cur;
  }
  if (sum)
    puts("No");
  else {
    sort(bracket, bracket + n, cmp);
    int cur = 0;
    bool ok = 1;
    for (int i = 0; i < n; ++i) {
      if (bracket[i].second + cur < 0) {
        ok = 0;
        break;
      }
      cur += bracket[i].first;
    }
    puts(ok ? "Yes" : "No");
  }
}