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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<string> S(n);
  int N = 0;
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
    N += S[i].length();
  }

  auto check = [&](string &s) {
    int n = s.length();

    int bal = 0;
    for (auto c : s) {
      if (c == '(') {
        ++bal;
      } else {
        --bal;
      }
      if (bal < 0) {
        return false;
      }
    }

    return bal == 0;
  };

  vector<int> left(n, 0);
  vector<int> right(n, 0);
  vector<int> incs(n, 0);
  for (int i = 0; i < n; ++i) {
    auto &s = S[i];
    int m = s.length();
    int bal = 0;
    for (int j = 0; j < m; ++j) {
      if (s[j] == '(') {
        ++bal;
      } else if (s[j] == ')') {
        --bal;
      }
      left[i] = min(left[i], bal);
    }
    incs[i] = bal;

    bal = 0;
    for (int j = m - 1; j >= 0; --j) {
      if (s[j] == ')') {
        ++bal;
      } else {
        --bal;
      }
      right[i] = min(right[i], bal);
    }
    // cout << i << " " << left[i] << " " << incs[i] << " " << right[i] << endl;
  }

  string res;

  // for (auto x : ord) {
  //     res += S[x];
  // }

  // vector<bool> used(n, false);
  // int BAL = 0;
  // for (int i = 0; i < n; ++i) {
  //     if (incs[i] >= 0 && left[i] >= 0) {
  //         res += S[i];
  //         used[i] = true;
  //         BAL += incs[i];
  //     }
  // }

  vector<vector<int>> reqs(N + 12);
  for (int i = 0; i < n; ++i) {
    reqs[-left[i]].push_back(i);
  }
  priority_queue<pair<int, int>> pq;

  int BAL = 0;
  int cnt = 0;
  for (int i = 0; i <= BAL; ++i) {
    for (auto x : reqs[i]) {
      pq.emplace(incs[x], x);
    }
    while (!pq.empty()) {
      int x = pq.top().first;
      int idx = pq.top().second;
      if (x >= 0) {
        ++cnt;
        BAL += x;
        res += S[idx];
        pq.pop();
      } else {
        break;
      }
    }
  }
  bool ok = true;
  vector<int> A;
  if (cnt + pq.size() == n) {
    while (!pq.empty()) {
      int x = pq.top().first;
      A.push_back(pq.top().second);
      BAL += x;
      pq.pop();
      if (BAL < 0) {
        ok = false;
        break;
      }
    }
    if (BAL != 0) {
      ok = false;
    }
  } else {
    ok = false;
  }

  if (ok) {
    sort(A.begin(), A.end(), [&](int l, int r) {
      if (right[l] == right[r]) {
        if (left[l] == left[r]) {
          return l < r;
        }
        return left[l] > left[r];
      }
      return right[l] < right[r];
    });
    for (auto x : A) {
      // cout << S[x] << " " << x << endl;
      res += S[x];
    }
    // cout << res << endl;
    if (!check(res)) {
      ok = false;
    }
  }

  // cout << res << endl;
  if (ok) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<string> S(n);
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }

  auto check = [&](string &s) {
    int n = s.length();

    int bal = 0;
    for (auto c : s) {
      if (c == '(') {
        ++bal;
      } else {
        --bal;
      }
      if (bal < 0) {
        return false;
      }
    }

    return bal == 0;
  };

  vector<int> left(n, 0);
  vector<int> right(n, 0);
  vector<int> incs(n, 0);
  for (int i = 0; i < n; ++i) {
    auto &s = S[i];
    int m = s.length();
    int bal = 0;
    for (int j = 0; j < m; ++j) {
      if (s[j] == '(') {
        ++bal;
      } else if (s[j] == ')') {
        --bal;
      }
      left[i] = min(left[i], bal);
    }
    incs[i] = bal;

    bal = 0;
    for (int j = m - 1; j >= 0; --j) {
      if (s[j] == ')') {
        ++bal;
      } else {
        --bal;
      }
      right[i] = min(right[i], bal);
    }
    // cout << i << " " << left[i] << " " << incs[i] << " " << right[i] << endl;
  }

  string res;

  // for (auto x : ord) {
  //     res += S[x];
  // }

  // vector<bool> used(n, false);
  // int BAL = 0;
  // for (int i = 0; i < n; ++i) {
  //     if (incs[i] >= 0 && left[i] >= 0) {
  //         res += S[i];
  //         used[i] = true;
  //         BAL += incs[i];
  //     }
  // }

  vector<vector<int>> reqs(n + 12);
  for (int i = 0; i < n; ++i) {
    reqs[-left[i]].push_back(i);
  }
  priority_queue<pair<int, int>> pq;

  int BAL = 0;
  int cnt = 0;
  for (int i = 0; i <= BAL; ++i) {
    for (auto x : reqs[i]) {
      pq.emplace(incs[x], x);
    }
    while (!pq.empty()) {
      int x = pq.top().first;
      int idx = pq.top().second;
      if (x >= 0) {
        ++cnt;
        BAL += x;
        res += S[idx];
        pq.pop();
      } else {
        break;
      }
    }
  }
  bool ok = true;
  vector<int> A;
  if (cnt + pq.size() == n) {
    while (!pq.empty()) {
      int x = pq.top().first;
      A.push_back(pq.top().second);
      BAL += x;
      pq.pop();
      if (BAL < 0) {
        ok = false;
        break;
      }
    }
    if (BAL != 0) {
      ok = false;
    }
  } else {
    ok = false;
  }

  if (ok) {
    sort(A.begin(), A.end(), [&](int l, int r) {
      if (right[l] == right[r]) {
        if (left[l] == left[r]) {
          return l < r;
        }
        return left[l] > left[r];
      }
      return right[l] < right[r];
    });
    for (auto x : A) {
      // cout << S[x] << " " << x << endl;
      res += S[x];
    }
    // cout << res << endl;
    if (!check(res)) {
      ok = false;
    }
  }

  // cout << res << endl;
  if (ok) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

  return 0;
}