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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\\n"
#define se second
#define ls(s) (s & (-s))
#define ll long long
#define inf 0x3f3f3f3f
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int N = 1e6 + 10;
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod = 998244353;
int n;
string s[N];
bool comp(string s1, string s2) {
  int cnt1 = 0, cnt2 = 0;
  int c1 = 0, c2 = 0;
  for (int i = 0; i < s1.size(); i++) {
    if (s1[i] == '(')
      c1++;
    else
      c1--;
    cnt1 = min(cnt1, c1);
  }
  for (int i = 0; i < s2.size(); i++) {
    if (s2[i] == '(')
      c2++;
    else
      c2--;
    cnt2 = min(cnt2, c2);
  }
  return min(cnt1, c1 + cnt2) > min(cnt2, cnt1 + c2);
}
int main() {
  fast cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  sort(s, s + n, comp);
  string ss = "";
  for (int i = 0; i < n; i++)
    ss += s[i];
  bool ans = true;
  int cnt = 0;
  for (int i = 0; i < ss.size(); i++) {
    if (ss[i] == '(')
      cnt++;
    else
      cnt--;
    if (cnt < 0)
      ans = false;
  }
  if (cnt)
    ans = false;
  if (ans)
    cout << "Yes";
  else
    cout << "No";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\\n"
#define se second
#define ls(s) (s & (-s))
#define ll long long
#define inf 0x3f3f3f3f
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int N = 1e6 + 10;
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod = 998244353;
int n;
string s[N];
bool comp(string s1, string s2) {
  int cnt1 = 1e7, cnt2 = 1e7;
  int c1 = 0, c2 = 0;
  for (int i = 0; i < s1.size(); i++) {
    if (s1[i] == '(')
      c1++;
    else
      c1--;
    cnt1 = min(cnt1, c1);
  }
  for (int i = 0; i < s2.size(); i++) {
    if (s2[i] == '(')
      c2++;
    else
      c2--;
    cnt2 = min(cnt2, c2);
  }
  if (cnt1 > 0)
    return true;
  if (cnt2 > 0)
    return false;
  return c1 - cnt1 > c2 - cnt2 || c1 > c2;
}
int main() {
  fast cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  sort(s, s + n, comp);
  string ss = "";
  for (int i = 0; i < n; i++)
    ss += s[i];
  bool ans = true;
  int cnt = 0;
  for (int i = 0; i < ss.size(); i++) {
    if (ss[i] == '(')
      cnt++;
    else
      cnt--;
    if (cnt < 0)
      ans = false;
  }
  if (cnt)
    ans = false;
  if (ans)
    cout << "Yes";
  else
    cout << "No";
}