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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  cin >> n;

  int tot = 0;
  vector<string> bracks;
  rep(i, 0, n) {
    string s;
    cin >> s;
    bracks.pb(s);
    rep(j, 0, sz(s)) if (s[j] == ')')-- tot;
    else ++tot;
  }

  if (tot != 0)
    return cout << "No\\n", 0;

  vii pos, neg;
  rep(i, 0, n) {
    int t = 0;
    int mt = 0;
    for (auto c : bracks[i]) {
      if (c == ')') {
        --t;
        ckmin(mt, t);
      } else
        ++t;
    }

    if (t <= 0) {
      neg.eb(-mt, -t);
    } else {
      pos.eb(mt, t);
    }
  }

  sort(all(pos));
  priority_queue<int, vector<int>, greater<int>> pq;

  auto ad = [&]() {
    while (not pos.empty() and -pos.back().fst <= tot) {
      pq.push(pos.back().snd);
      pos.pop_back();
    }
  };

  ad();
  while (not pq.empty()) {
    int u = pq.top();
    pq.pop();
    tot += u;
    ad();
  }

  sort(all(neg), [&](pii a, pii b) { return a.fst - a.snd > b.fst - b.snd; });
  bool suc = pos.empty();
  for (auto [mn, tk] : neg) {
    suc &= mn <= tot;
    tot -= tk;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  cin >> n;

  int tot = 0;
  vector<string> bracks;
  rep(i, 0, n) {
    string s;
    cin >> s;
    bracks.pb(s);
    rep(j, 0, sz(s)) if (s[j] == ')')-- tot;
    else ++tot;
  }

  if (tot != 0)
    return cout << "No\\n", 0;

  vii pos;
  vector<tuple<int, int, int>> neg;
  vvi nd(1123456);
  vi how(n), need(n);
  rep(i, 0, n) {
    int t = 0;
    int mt = 0;
    for (auto c : bracks[i]) {
      if (c == ')') {
        --t;
        ckmin(mt, t);
      } else
        ++t;
    }

    if (t <= 0) {
      neg.eb(-t, -mt, i);
      nd[-mt].pb(i);
      how[i] = -t;
      need[i] = -mt;
    } else {
      pos.eb(mt, t);
    }
  }

  sort(all(pos));
  priority_queue<int, vector<int>, greater<int>> pq;

  auto ad = [&]() {
    while (not pos.empty() and -pos.back().fst <= tot) {
      pq.push(pos.back().snd);
      pos.pop_back();
    }
  };

  ad();
  while (not pq.empty()) {
    int u = pq.top();
    pq.pop();
    tot += u;
    ad();
  }

  sort(all(neg));
  vi done(n);
  bool suc = pos.empty();

  auto take = [&](int i) {
    if (done[i])
      return;
    if (tot < 0)
      suc = false;
    if (tot < need[i])
      suc = false;
    tot -= how[i];
    done[i] = 1;
  };

  for (auto [tk, mn, i] : neg) {
    if (done[i])
      continue;
    int wd = tot - tk;
    per(j, wd + 1, tot + 1) for (auto x : nd[j]) take(x);
    take(i);
  }

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