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

#define _CRT_SECURE_NO_DEPRECATE
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define SYNC                                                                   \\
  std::ios_base::sync_with_stdio(0);                                           \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     data_set;
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define deb(x) cout << (#x) << " is " << (x) << endl
#define dbg(x) cout << x << ' '
#define here cout << " Hey baitch!!\\n";
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define sz(a) (int)((a).size())
#define sq(x) ((x) * (x))
const int MOD = 1e9 + 7;
const int MOD1 = 998244353;
const int N = 2e5 + 5;
const int INF = 1000111000111000111LL;
const ld PI = 3.1415926535898;
struct pt {
  int op;
  int cl;
  int id;
};
bool comp(pt x, pt y) { return (x.op * y.cl > y.op * x.cl); }
int32_t main() {
  SYNC int n;
  cin >> n;
  vector<string> v(n);
  rep(i, 0, n) cin >> v[i];
  vector<pt> a(n);
  rep(i, 0, n) {
    string s = v[i];
    int mn = 0;
    int cnt = 0;
    a[i].op = 0, a[i].cl = 0;
    for (auto it : s) {
      if (it == '(')
        a[i].op++;
      else
        a[i].cl++;
    }
    a[i].id = i;
  }
  sort(all(a), comp);
  string ans;
  rep(i, 0, n) { ans += v[a[i].id]; }
  int cnt = 0;
  for (auto it : ans) {
    if (it == '(')
      cnt++;
    else
      cnt--;
    if (cnt < 0)
      return cout << "No", 0;
  }
  if (cnt != 0)
    return cout << "No", 0;
  cout << "Yes";
  return 0;
}

Error: WA

Fixed submission:

#define _CRT_SECURE_NO_DEPRECATE
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define SYNC                                                                   \\
  std::ios_base::sync_with_stdio(0);                                           \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     data_set;
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define deb(x) cout << (#x) << " is " << (x) << endl
#define dbg(x) cout << x << ' '
#define here cout << " Hey baitch!!\\n";
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define sz(a) (int)((a).size())
#define sq(x) ((x) * (x))
const int MOD = 1e9 + 7;
const int MOD1 = 998244353;
const int N = 2e5 + 5;
const int INF = 1000111000111000111LL;
const ld PI = 3.1415926535898;
struct pt {
  int lw;
  int profit;
  int id;
};
bool comp(pt x, pt y) {

  if (x.lw > 0 && y.lw > 0)
    return x.profit > y.profit;
  if (x.lw > 0)
    return true;
  if (x.lw < 0 && y.lw > 0)
    return false;
  if (x.lw < 0) {
    return (x.profit > y.profit);
  } else {
    return (x.lw > y.lw);
  }
}
int32_t main() {
  SYNC int n;
  cin >> n;
  vector<string> v(n);
  rep(i, 0, n) cin >> v[i];
  vector<pt> a(n);
  rep(i, 0, n) {
    string s = v[i];
    int mn = 0;
    int cnt = 0;
    for (auto it : s) {
      if (it == '(')
        cnt++;
      else
        cnt--;
      mn = min(mn, cnt);
    }
    a[i].lw = mn;
    a[i].profit = cnt;
    a[i].id = i;
  }
  sort(all(a), comp);
  string ans;
  rep(i, 0, n) { ans += v[a[i].id]; }
  int cnt = 0;
  for (auto it : ans) {
    if (it == '(')
      cnt++;
    else
      cnt--;
    if (cnt < 0)
      return cout << "No", 0;
  }
  if (cnt != 0)
    return cout << "No", 0;
  cout << "Yes";
  return 0;
}