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

#include <bits/stdc++.h>

#define _1 first
#define _2 second
#define pb push_back
#define mp make_pair
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define reset(x, y) memset(x, y, sizeof(x))
#define BUG(x) cerr << #x << " = " << (x) << endl
#define BUGP(x) cout << #x << " = " << (x)._1 << ", " << (x)._2 << endl
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dr[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dc[] = {0, -1, 0, 1, -1, 1, -1, 1};
// Head

// struct Node {
//     int final, maxm;
// };

int n, tot;

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(nullptr);

  cin >> n;

  vector<pii> pos, neg;
  for (int i = 0; i < n; ++i) {
    string s;
    int l = 0, r = 0, maxm = 0;
    cin >> s;

    for (int j = 0; j < s.length(); ++j) {
      if (s[j] == '(')
        ++l;
      else
        ++r;
      maxm = max(maxm, r - l);
    }

    if (l - r >= 0)
      pos.pb({l - r, maxm});
    else
      neg.pb({r - l, maxm});
  }

  sort(all(pos), [](const pii &a, const pii &b) { return a._2 < b._2; });
  for (auto &it : pos) {
    if (it._2 > tot) {
      cout << "No\\n";
      return 0;
    }
    tot += it._1;
  }

  sort(all(neg),
       [](const pii &a, const pii &b) { return a._2 - a._1 > b._2 - b._1; });
  for (auto &it : neg) {
    if (it._2 > tot) {
      cout << "No\\n";
      return 0;
    }
    tot -= it._1;
  }

  if (tot)
    cout << "No\\n";
  else
    cout << "Yes\\n";

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define isYES(x) printf("%s\\n", (x) ? "YES" : "NO")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define isYes(x) printf("%s\\n", (x) ? "Yes" : "No")
#define isIn(x, y, h, w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
// using ll = long long;
using P = pair<int, int>;

ostream &operator<<(ostream &os, const P &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    os << (i ? "," : "[") << v[i];
  os << "]";
  return os;
}

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 INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 998244353;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

signed main() {
  using P = pair<int, int>;
  int n;
  cin >> n;
  vector<P> vecUp, vecDown;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int cnt = 0, mi = 0;
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == '(')
        cnt++;
      else
        cnt--;
      mi = min(mi, cnt);
    }
    //        if(cnt >= 0) vecUp.emplace_back(-mi,cnt - mi);
    //        else vecDown.emplace_back(-mi,cnt - mi);
    if (cnt >= 0)
      vecUp.emplace_back(cnt, -mi);
    else
      vecDown.emplace_back(-cnt, -mi);
  }

  //    sort(vecUp.begin(),vecUp.end());
  //    sort(vecDown.begin(),vecDown.end(),[](const P &p1,const P &p2){
  //        return p1.second > p2.second;
  //    });

  sort(vecUp.begin(), vecUp.end(),
       [](const P &a, const P &b) { return a.second < b.second; });
  sort(vecDown.begin(), vecDown.end(), [](const P &a, const P &b) {
    return a.second - a.first >= b.second - b.first;
  });

  int cnt = 0, mi = 0;
  for (P p : vecUp) {
    cnt -= p.first;
    mi = min(mi, cnt);
    cnt += p.second;
  }
  for (P p : vecDown) {
    cnt -= p.first;
    mi = min(mi, cnt);
    cnt += p.second;
  }
  isYes(!cnt && mi >= 0);
}