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

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

pair<int, int> calc_mn_total(string &s) {
  int mn = 0, total = 0;
  int sz = s.length();
  for (int i = 0; i < sz; ++i) {
    if (s[i] == '(')
      ++total;
    else {
      --total;
      mn = min(mn, total);
    }
  }
  return pair<int, int>(mn, total);
}

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> s(N);
  for (int i = 0; i < N; ++i) {
    string si;
    cin >> si;
    s[i] = calc_mn_total(si);
  }

  vector<pair<int, int>> zero_pos, neg;
  for (auto &p : s) {
    if (p.second > 0)
      zero_pos.push_back(p);
    else
      neg.push_back(p);
  }

  sort(zero_pos.begin(), zero_pos.end(),
       [&](auto p1, auto p2) { return p1.first > p2.first; });
  int now = 0;
  for (auto p : zero_pos) {
    if (now + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += p.second;
  }

  sort(neg.begin(), neg.end(), [&](auto p1, auto p2) {
    return (p1.second - p1.first) > (p2.second - p2.first);
  });
  for (auto p : neg) {
    if (now + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += p.second;
  }

  string res = (now == 0) ? "Yes" : "No";
  cout << res << endl;

  return 0;
}

Error: -6
330eb3ed-a88a-4136-a02f-6f22f72f910e.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02686/C++/s638847345.cpp:84: int main(): Assertion `false' failed.


Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  int n;
  cin >> n;
  vector<int> t(n), m(n);
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    int now = 0;
    int mn = 0;
    for (int j = 0; j < s.length(); ++j) {
      if (s[j] == '(')
        ++now;
      else {
        --now;
        mn = min(mn, now);
      }
    }
    t[i] = now;
    m[i] = mn;
  }

  /*
  for (int i = 0; i < n; ++i)
          cout << t[i] << " " << m[i] << endl;
  */

  ll total = 0;
  for (auto ti : t)
    total += ti;

  if (total != 0) {
    cout << "No" << endl;
    return 0;
  }

  // mが0なものは初めに置く

  ll tpos = 0;
  for (int i = 0; i < n; ++i)
    if (m[i] == 0)
      tpos += t[i];

  // tは非負なのにmは負なものを処理

  vector<pair<int, int>> todd;
  for (int i = 0; i < n; ++i) {
    if (t[i] >= 0 && m[i] < 0)
      todd.emplace_back(t[i], m[i]);
  }

  map<int, vector<int>> m_to_t;
  for (auto p : todd)
    m_to_t[-p.second].push_back(p.first);

  priority_queue<int> ts;
  for (auto &e : m_to_t) {
    if (-e.first + tpos >= 0) {
      for (auto _t : e.second)
        ts.push(_t);
      continue;
    } else if (ts.empty()) {
      cout << "No" << endl;
      return 0;
    }
    while (!ts.empty()) {
      int _t = ts.top();
      ts.pop();
      tpos += _t;
    }
  }
  while (!ts.empty()) {
    int _t = ts.top();
    ts.pop();
    tpos += _t;
  }

  assert(false);

  // 残りはともに負
  vector<pair<int, int>> tneg;
  for (int i = 0; i < n; ++i) {
    if (t[i] < 0 && m[i] < 0) {
      tneg.emplace_back(t[i], m[i]);
    }
  }

  sort(tneg.begin(), tneg.end(), [&](auto a, auto b) {
    if (a.first != b.first)
      return a.first > b.first;
    return a.second < b.second;
  });

  for (auto p : tneg) {
    if (tpos + p.second < 0) {
      cout << "No" << endl;
      return 0;
    }
    tpos += p.first;
  }

  cout << "Yes" << endl;

  return 0;
}