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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

struct S {
  ll minus, tot;
  bool used;
};

bool f(const S &a, const S &b) { return a.minus > b.minus; }

bool g(const S &a, const S &b) { return (a.tot - a.minus) > (b.tot - b.minus); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  vector<S> vs(n, (S){0, 0, false});
  rep(i, n) {
    rep(j, len(s[i])) {
      vs[i].tot += (s[i][j] == '(') ? 1 : -1;
      vs[i].minus = min(vs[i].minus, vs[i].tot);
    }
  }
  sort(all(vs), f);
  ll sum = 0;
  rep(i, n) {
    if (vs[i].tot < 0)
      continue;
    if ((sum + vs[i].minus) < 0) {
      cout << "No" << endl;
      return 0;
    }
    sum += vs[i].tot;
    vs[i].used = true;
  }
  sort(all(vs), g);
  rep(i, n) {
    if (vs[i].used)
      continue;
    if ((sum + vs[i].minus) < 0) {
      cout << "No" << endl;
      return 0;
    }
    sum += vs[i].tot;
    if (sum < 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << ((sum == 0) ? "Yes" : "No") << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

struct S {
  ll minus, tot;
};

bool f(const S &a, const S &b) { return a.minus > b.minus; }

bool g(const S &a, const S &b) { return (a.tot - a.minus) > (b.tot - b.minus); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  vector<S> vs(n, (S){0, 0});
  rep(i, n) {
    rep(j, len(s[i])) {
      vs[i].tot += (s[i][j] == '(') ? 1 : -1;
      vs[i].minus = min(vs[i].minus, vs[i].tot);
    }
  }
  sort(all(vs), f);
  ll sum = 0;
  for (auto it = vs.begin(); it != vs.end();) {
    if (it->tot < 0) {
      it++;
    } else {
      if ((sum + it->minus) < 0) {
        cout << "No" << endl;
        return 0;
      }
      sum += it->tot;
      vs.erase(it++);
    }
  }
  sort(all(vs), g);
  for (auto it = vs.begin(); it != vs.end(); it++) {
    if ((sum + it->minus) < 0) {
      cout << "No" << endl;
      return 0;
    }
    sum += it->tot;
    if (sum < 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << ((sum == 0) ? "Yes" : "No") << endl;
  return 0;
}