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

#include <bits/stdc++.h>

using namespace std;

vector<string> str;
vector<pair<int, int>> opclo;

bool cmp1(const int &a, const int &b) {
  return (opclo[a].first < opclo[b].first);
}
bool cmp2(const int &a, const int &b) {
  return (opclo[a].second < opclo[b].second);
}
vector<int> ind;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  str.assign(n, string());
  opclo.assign(n, pair<int, int>{0, 0});
  ind.assign(n, 0);
  for (int i = 0; i < n; ind[i] = i, i++)
    cin >> str[i];
  vector<int> pos, neg, zero;
  for (int i = 0; i < n; i++) {
    pair<int, int> tmp{0, 0};
    stack<int> ss;
    for (int j = 0; j < str[i].size(); j++) {
      if (str[i][j] == '(')
        ss.push(j);
      else if (!ss.empty())
        ss.pop();
      else
        tmp.first++;
    }
    while (!ss.empty()) {
      ss.pop();
      tmp.second++;
    }
    opclo[i] = tmp;
    if (tmp.first == 0 && tmp.second == 0)
      zero.push_back(i);
    else if (tmp.second - tmp.first >= 0)
      pos.push_back(i);
    else
      neg.push_back(i);
  }
  // cout << "beore sort" << endl;
  sort(pos.begin(), pos.end(), cmp1);
  sort(neg.rbegin(), neg.rend(), cmp2);
  // cout << "after sort" << endl;
  string thestr = "";
  for (int i = 0; i < pos.size(); i++)
    thestr += str[pos[i]];
  for (int i = 0; i < zero.size(); i++)
    thestr += str[zero[i]];
  for (int i = 0; i < neg.size(); i++)
    thestr += str[neg[i]];
  stack<int> ss;
  bool fg = true;
  for (int i = 0; i < thestr.size(); i++) {
    if (thestr[i] == '(')
      ss.push(i);
    else {
      if (!ss.empty())
        ss.pop();
      else {
        fg = false;
        break;
      }
    }
  }
  if (ss.empty() && fg)
    cout << "Yes\\n";
  else
    cout << "No\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<string> str;
vector<pair<int, int>> opclo;
bool cmp(const int &a, const int &b) {
  int sum1 = opclo[a].second - opclo[a].first;
  int sum2 = opclo[b].second - opclo[b].first;
  if (sum1 == 0 && sum2 == 0)
    return a < b;
  if (sum1 == 0 && sum2 > 0)
    return false;
  if (sum1 == 0 && sum2 < 0)
    return true;
  if (sum2 == 0 && sum1 > 0)
    return true;
  if (sum2 == 0 && sum1 < 0)
    return false;
  if (sum1 > 0 && sum2 < 0)
    return true;
  if (sum1 < 0 && sum2 > 0)
    return false;
  if (sum1 > 0 && sum2 > 0)
    return opclo[a].first < opclo[b].first;
  if (sum1 < 0 && sum2 < 0)
    return !(opclo[a].second < opclo[b].second);
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  str.assign(n, string());
  opclo.assign(n, pair<int, int>{0, 0});
  vector<int> ind(n);
  for (int i = 0; i < n; ind[i] = i, i++)
    cin >> str[i];
  vector<int> pos, neg;
  for (int i = 0; i < n; i++) {
    pair<int, int> tmp{0, 0};
    stack<int> ss;
    for (int j = 0; j < str[i].size(); j++) {
      if (str[i][j] == '(')
        ss.push(j);
      else if (!ss.empty())
        ss.pop();
      else
        tmp.first++;
    }
    while (!ss.empty()) {
      ss.pop();
      tmp.second++;
    }
    opclo[i] = tmp;
  }
  sort(ind.begin(), ind.end(), cmp);
  string thestr = "";
  for (auto i : ind)
    thestr += str[i];
  stack<int> ss;
  bool fg = true;
  for (int i = 0; i < thestr.size(); i++) {
    if (thestr[i] == '(')
      ss.push(i);
    else {
      if (!ss.empty())
        ss.pop();
      else {
        fg = false;
        break;
      }
    }
  }
  if (ss.empty() && fg)
    cout << "Yes\\n";
  else
    cout << "No\\n";
  return 0;
}