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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<long long, long long> ii;
typedef complex<long double> com;

const ll mod = 998244353;

struct bracket {
  ll open, close;
  bool operator<(const bracket &rhs) const {
    if (open - close == rhs.open - rhs.close) {
      return close < rhs.close;
    }
    return open - close > rhs.open - rhs.close;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<string> arr(n);
  for (int z = 0; z < n; z++) {
    cin >> arr[z];
  }
  vector<bracket> lol;
  ll open = 0;
  ll close = 0;
  for (int z = 0; z < n; z++) {
    ll open1 = 0;
    ll close1 = 0;
    for (int x = 0; x < arr[z].size(); x++) {
      if (arr[z][x] == '(')
        open1++;
      if (arr[z][x] == ')') {
        if (open1 == 0)
          close1++;
        else
          open1--;
      }
    }
    ll open2 = 0;
    ll close2 = 0;
    for (int x = arr[z].size() - 1; x >= 0; x--) {
      if (arr[z][x] == ')')
        close2++;
      if (arr[z][x] == '(') {
        if (close2 == 0)
          open2++;
        else
          close2--;
      }
    }
    if (close1 == 0 && open2 == 0)
      continue;
    else if (close1 == 0) {
      open += open2;
    } else if (open2 == 0) {
      close += close1;
    } else {
      bracket temp;
      temp.open = open2;
      temp.close = close1;
      lol.push_back(temp);
    }
  }
  sort(lol.begin(), lol.end());
  bool ok = true;
  for (int z = 0; z < lol.size() && ok; z++) {
    open -= lol[z].close;
    if (open < 0)
      ok = false;
    open += lol[z].open;
  }
  if (open - close != 0)
    ok = false;
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<long long, long long> ii;
typedef complex<long double> com;

const ll mod = 998244353;

struct bracket {
  ll open, close;
  bool operator<(const bracket &rhs) const {
    if (close == rhs.close) {
      return open >= rhs.open;
    }
    return close < rhs.close;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<string> arr(n);
  for (int z = 0; z < n; z++) {
    cin >> arr[z];
  }
  vector<bracket> lol;
  ll open = 0;
  ll close = 0;
  for (int z = 0; z < n; z++) {
    ll open1 = 0;
    ll close1 = 0;
    for (int x = 0; x < arr[z].size(); x++) {
      if (arr[z][x] == '(')
        open1++;
      else if (arr[z][x] == ')') {
        if (open1 == 0)
          close1++;
        else
          open1--;
      }
    }
    ll open2 = 0;
    ll close2 = 0;
    for (int x = arr[z].size() - 1; x >= 0; x--) {
      if (arr[z][x] == ')')
        close2++;
      else if (arr[z][x] == '(') {
        if (close2 == 0)
          open2++;
        else
          close2--;
      }
    }
    if (close1 == 0 && open2 == 0)
      continue;
    else if (close1 == 0) {
      open += open2;
    } else if (open2 == 0) {
      close += close1;
    } else {
      bracket temp;
      temp.open = open2;
      temp.close = close1;
      lol.push_back(temp);
    }
  }
  sort(lol.begin(), lol.end());
  bool ok = true;
  for (int z = 0; z < lol.size() && ok; z++) {
    open -= lol[z].close;
    if (open < 0)
      ok = false;
    open += lol[z].open;
  }
  if (open - close != 0)
    ok = false;
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}