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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define PI 3.14159265358979323846264338327950L

int n;
pair<int, int> a[1000001];
pair<int, int> sub[1000001];
pair<int, int> nsub[1000001];
string s;

void solve() {

  cin >> n;

  int insub = 0;
  int ninsub = 0;

  for (int i = 0; i < n; i++) {
    cin >> s;

    a[i].first = 0;
    a[i].second = 0;

    bool leftc = true;
    int right = 0;
    for (char c : s) {
      if (leftc) {
        if (c == ')') {
          a[i].first++;
        } else {
          leftc = false;
          right++;
        }
      } else {
        if (c == ')') {
          right--;
        } else
          right++;

        if (right == 0) {
          leftc = true;
        }
      }
    }
    a[i].second = right;

    int tmp = a[i].second - a[i].first;
    if (tmp > 0) {
      sub[insub].first = a[i].first;
      sub[insub].second = a[i].second;
      insub++;
    } else {
      nsub[ninsub].first = a[i].first;
      nsub[ninsub].second = a[i].second;
      ninsub++;
    }
  }

  sort(sub, sub + insub,
       [](auto &left, auto &right) { return left.first < right.first; });

  sort(nsub, nsub + ninsub,
       [](auto &left, auto &right) { return left.second > right.second; });

  int ans = 0;
  for (int i = 0; i < insub; i++) {
    // cout << a[i].first << " " << a[i].second << endl;
    ans -= sub[i].first;
    if (ans < 0) {
      cout << "No" << endl;
      return;
    }
    ans += sub[i].second;
  }
  for (int i = 0; i < ninsub; i++) {
    ans -= nsub[i].first;
    if (ans < 0) {
      cout << "No" << endl;
      return;
    }
    ans += nsub[i].second;
  }

  if (ans == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define PI 3.14159265358979323846264338327950L

int n;
pair<int, int> a[1000001];
int f[1000001];
string s;

void solve() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> s;

    a[i].first = 0;
    a[i].second = 0;

    bool leftc = true;
    int right = 0;
    for (char c : s) {
      if (leftc) {
        if (c == ')') {
          a[i].first++;
        } else {
          leftc = false;
          right++;
        }
      } else {
        if (c == ')') {
          right--;
        } else
          right++;

        if (right == 0) {
          leftc = true;
        }
      }
    }
    a[i].second = right;
  }

  sort(a, a + n, [](auto &left, auto &right) {
    if (left.second - left.first > 0 && right.second - right.first > 0)
      return left.first < right.first;
    else if (left.second - left.first < 0)
      return false;
    else if (right.second - right.first < 0)
      return true;
    else
      return true;
  });

  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout << a[i].first << " " << a[i].second << endl;
    ans -= a[i].first;
    if (ans < 0) {
      cout << "No" << endl;
      return;
    }
    ans += a[i].second;
  }

  if (ans == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  solve();
}