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

#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(i, a) for (auto &i : (a))
#define sz(x) (int)(x).size()
#define all(v) (v).begin(), (v).end()
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)

void solve();

signed main() {
  clock_t beg = clock();
  cout << fixed << setprecision(15);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  // freopen("input.txt", "r", stdin);
#endif
  int t = 1;
  // cin >> t;
  int tt = t;
  while (t--) {
    // cout << "Case #" << tt - t << ": ";
    solve();
  }
  cerr << "\\nExecution time: " << (clock() - beg) / 1000 << '\\n';
  return 0;
}
int n;
const int N = 1e6 + 5;
string s[N];
int cur = 0;
array<int, 3> get(string &s) {
  ++cur;
  int mn = 0, tot = 0;
  for (auto &i : s) {
    if (i == '(')
      tot++;
    else
      tot--;
    mn = min(mn, tot);
  }
  return {tot, mn, ++cur};
}
bool cmp_pos(array<int, 3> a, array<int, 3> b) {
  if (a[1] != b[1])
    return a[1] > b[1];
  if (a[0] != b[0])
    return a[0] > b[0];
  return a[2] < b[2];
}
bool cmp_neg(array<int, 3> a, array<int, 3> b) {
  if (abs(a[0] - a[1]) != abs(b[0] - b[1]))
    return abs(a[0] - a[1]) > abs(b[0] - b[1]);
  if (a[1] != b[1])
    return a[1] > b[1];
  if (a[0] != b[0])
    return a[0] > b[0];
  return a[2] < b[2];
}
vector<array<int, 3>> pos, neg;
vector<array<int, 3>> sol;
void solve() {
  cin >> n;
  rep(i, 0, n) {
    cin >> s[i];
    auto lol = get(s[i]);
    if (lol[1] == 0)
      sol.push_back(lol);
    else if (lol[0] >= 0)
      pos.push_back(lol);
    else
      neg.push_back(lol);
  }
  sort(all(pos), cmp_pos);
  sort(all(neg), cmp_neg);
  for (auto &i : pos)
    sol.push_back(i);
  for (auto &i : neg)
    sol.push_back(i);
  int tot = 0, mn = 0;
  for (auto &i : sol) {
    mn = min(mn, tot + i[1]);
    tot += i[0];
  }
  if (tot == 0 && mn == 0)
    cout << "Yes";
  else
    cout << "No";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(i, a) for (auto &i : (a))
#define sz(x) (int)(x).size()
#define all(v) (v).begin(), (v).end()
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)

void solve();

signed main() {
  clock_t beg = clock();
  cout << fixed << setprecision(15);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  // freopen("input.txt", "r", stdin);
#endif
  int t = 1;
  // cin >> t;
  int tt = t;
  while (t--) {
    // cout << "Case #" << tt - t << ": ";
    solve();
  }
  cerr << "\\nExecution time: " << (clock() - beg) / 1000 << '\\n';
  return 0;
}
int n;
const int N = 1e6 + 5;
string s[N];
pair<int, int> get(string &s) {
  int mn = 0, tot = 0;
  for (auto &i : s) {
    if (i == '(')
      tot++;
    else
      tot--;
    mn = min(mn, tot);
  }
  return {tot, mn};
}
bool cmp_pos(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second)
    return a.second > b.second;
  if (a.first != b.first)
    return a.first > b.first;
  return true;
}
bool cmp_neg(pair<int, int> a, pair<int, int> b) {
  if (abs(a.first - a.second) != abs(b.first - b.second))
    return abs(a.first - a.second) > abs(b.first - b.second);
  if (a.second != b.second)
    return a.second > b.second;
  if (a.first != b.first)
    return a.first > b.first;
  return true;
}
vector<pair<int, int>> pos, neg;
vector<pair<int, int>> sol;
void solve() {
  cin >> n;
  rep(i, 0, n) {
    cin >> s[i];
    auto lol = get(s[i]);
    if (lol.second == 0)
      sol.push_back(lol);
    else if (lol.first >= 0)
      pos.push_back(lol);
    else
      neg.push_back(lol);
  }
  sort(all(pos), cmp_pos);
  sort(all(neg), cmp_neg);
  for (auto &i : pos)
    sol.push_back(i);
  for (auto &i : neg)
    sol.push_back(i);
  int tot = 0, mn = 0;
  for (auto &i : sol) {
    mn = min(mn, tot + i.second);
    tot += i.first;
  }
  if (tot == 0 && mn == 0)
    cout << "Yes";
  else
    cout << "No";
}