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

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

struct cmp {
  bool operator()(const string &a, const string &b) {
    int brackets_a = 0;
    for (auto i : a)
      if (i == ')')
        brackets_a--;
      else
        brackets_a++;
    int brackets_b = 0;
    for (auto i : b)
      if (i == ')')
        brackets_b--;
      else
        brackets_b++;
    if (brackets_a == brackets_b)
      return a.size() < b.size();
    return brackets_a > brackets_b;
  }
};

int32_t main() {
  int n;
  cin >> n;
  vector<string> v(n);
  vector<string> vs;
  int idx = 0;
  string only_openings;
  string only_closings;
  for (auto &i : v) {
    cin >> i;
    string s;
    stack<char> st;
    for (auto j : i) {
      if (st.empty())
        st.push(j);
      else if (j == ')' && st.top() == '(')
        st.pop();
      else
        st.push(j);
    }
    while (!st.empty())
      s.push_back(st.top()), st.pop();
    reverse(s.begin(), s.end());
    if (s.empty())
      continue;
    if (s.front() == '(' && s.back() == '(')
      only_openings += s;
    else if (s.front() == ')' && s.back() == ')')
      only_closings += s;
    else
      vs.push_back(s);
  }
  sort(vs.begin(), vs.end(), cmp());

  string ans_str = only_openings;
  for (auto i : vs)
    ans_str += i;
  ans_str += only_closings;
  // cout<<ans_str<<"\\n";
  int brackets = 0;
  for (auto i : ans_str)
    if (i == '(')
      brackets++;
    else {
      brackets--;
      if (brackets < 0) {
        cout << "No\\n";
        exit(0);
      }
    }
  if (brackets == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";
  return 0;
}

Error: 0

Fixed submission:

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

struct cmp {
  bool operator()(const string &a, const string &b) {
    int brackets_a = 0;
    for (auto i : a) {
      if (i == '(')
        brackets_a++;
      else
        brackets_a--;
    }
    int brackets_b = 0;
    for (auto i : b) {
      if (i == ')')
        brackets_b++;
      else
        brackets_b--;
    }
    if (brackets_a == brackets_b)
      return (int)a.size() < (int)b.size();
    return brackets_a > brackets_b;
  }
};

int32_t main() {
  int n;
  cin >> n;
  vector<string> v(n);
  vector<string> vs;
  int idx = 0;
  string only_openings;
  string only_closings;
  for (auto &i : v) {
    cin >> i;
    string s;
    stack<char> st;
    for (auto j : i) {
      if (st.empty())
        st.push(j);
      else if (j == ')' && st.top() == '(')
        st.pop();
      else
        st.push(j);
    }
    while (!st.empty())
      s.push_back(st.top()), st.pop();
    reverse(s.begin(), s.end());
    if (s.empty())
      continue;
    if (s.front() == '(' && s.back() == '(')
      only_openings += s;
    else if (s.front() == ')' && s.back() == ')')
      only_closings += s;
    else
      vs.push_back(s);
  }
  sort(vs.begin(), vs.end(), cmp());

  string ans_str = only_openings;
  for (auto i : vs)
    ans_str += i;
  ans_str += only_closings;
  // cout<<ans_str<<"\\n";
  int brackets = 0;
  for (auto i : ans_str)
    if (i == '(')
      brackets++;
    else {
      brackets--;
      if (brackets < 0) {
        cout << "No\\n";
        exit(0);
      }
    }
  if (brackets == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";
  return 0;
}