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 = max(N, (int)S[i].length());
  }

  vector<vector<int>> T(N + 1);

  vector<int> reqs(n, 0);
  vector<int> chgs(n, 0);
  for (int i = 0; i < n; ++i) {
    int bal = 0;
    auto &s = S[i];
    int m = s.length();
    for (int j = 0; j < m; ++j) {
      bal += s[j] == '(' ? 1 : -1;
      reqs[i] = max(reqs[i], -bal);
    }
    chgs[i] = bal;
    T[reqs[i]].push_back(i);
  }

  string res;
  int BAL = 0;
  int cnt = 0;
  for (int i = 0; i <= BAL && i <= N; ++i) {
    for (auto x : T[i]) {
      if (chgs[x] >= 0) {
        BAL += chgs[x];
        res += S[x];
        ++cnt;
      }
    }
  }

  priority_queue<pair<int, int>> pq;
  for (int i = 0; i < n; ++i) {
    if (chgs[i] < 0) {
      pq.emplace(reqs[i] + chgs[i], i);
    }
  }

  while (!pq.empty()) {
    int x = pq.top().second;
    if (BAL < reqs[x]) {
      break;
    }
    pq.pop();
    BAL += chgs[x];
    res += S[x];
    ++cnt;
  }

  if (cnt == n && BAL == 0) {
    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);
  int N = 0;
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
    N = max(N, (int)S[i].length());
  }

  vector<vector<int>> T(N + 1);

  vector<int> reqs(n, 0);
  vector<int> chgs(n, 0);
  for (int i = 0; i < n; ++i) {
    int bal = 0;
    auto &s = S[i];
    int m = s.length();
    for (int j = 0; j < m; ++j) {
      bal += s[j] == '(' ? 1 : -1;
      reqs[i] = max(reqs[i], -bal);
    }
    chgs[i] = bal;
    T[reqs[i]].push_back(i);
  }

  string res;
  int BAL = 0;
  int cnt = 0;
  for (int i = 0; i <= BAL; ++i) {
    for (auto x : T[i]) {
      if (chgs[x] >= 0) {
        BAL += chgs[x];
        res += S[x];
        ++cnt;
      }
    }
  }

  priority_queue<pair<int, int>> pq;
  for (int i = 0; i < n; ++i) {
    if (chgs[i] < 0) {
      pq.emplace(reqs[i] + chgs[i], i);
    }
  }

  while (!pq.empty()) {
    int x = pq.top().second;
    if (BAL < reqs[x]) {
      break;
    }
    pq.pop();
    BAL += chgs[x];
    res += S[x];
    ++cnt;
  }

  if (cnt == n && BAL == 0) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

  return 0;
}