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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int N;
  bool flag = true;
  int conl = 0, conr = 0;
  int sum = 0, maximum = 0;
  vector<string> S;
  vector<pair<int, int>> num;
  vector<pair<int, pair<int, int>>> hoge;
  vector<pair<int, pair<int, int>>> fuga;

  cin >> N;

  S.resize(N);
  num.resize(N);

  for (int i = 0; i < N; i++) {
    cin >> S[i];

    stack<bool> st;

    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] == '(') {
        sum++;
        st.push(true);
      } else {
        if (st.size()) {
          st.pop();
        } else {
          num[i].first++;
        }
      }
    }

    num[i].second = st.size();

    if (num[i].first || num[i].second)
      flag = false;

    if (!num[i].first) {
      conl += num[i].second;
    }

    if (!num[i].second) {
      conr += num[i].first;
    }

    if (num[i].first && num[i].second) {
      if (num[i].first - num[i].second < 0)
        hoge.push_back({num[i].second, num[i]});
      if (num[i].first - num[i].second > 0)
        fuga.push_back({num[i].first, num[i]});
      if (num[i].first - num[i].second == 0)
        maximum = max(maximum, num[i].second);
    }
  }

  if (!sum) {
    cout << "No" << endl;
    return 0;
  }

  if (flag) {
    cout << "Yes" << endl;
    return 0;
  }

  sort(hoge.begin(), hoge.end());
  sort(fuga.begin(), fuga.end());

  for (int i = 0; i < hoge.size(); i++) {
    if (conl < hoge[i].first) {
      if (maximum == 0)
        cout << "No" << endl;
      else
        cout << "Yes" << endl;
      return 0;
    }

    conl += hoge[i].second.second - hoge[i].second.first;
  }

  for (int i = 0; i < fuga.size(); i++) {
    if (conr < fuga[i].first) {
      cout << "No" << endl;
      return 0;
    }

    conr += fuga[i].second.first - fuga[i].second.second;
  }

  if (conr == conl && conr >= maximum) {
    cout << "Yes" << endl;
    return 0;
  }

  cout << "No" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int N;
  bool flag = true;
  int conl = 0, conr = 0;
  int sum = 0;
  vector<string> S;
  vector<pair<int, int>> num;
  vector<pair<int, pair<int, int>>> hoge;
  vector<pair<int, pair<int, int>>> fuga;

  cin >> N;

  S.resize(N);
  num.resize(N);

  for (int i = 0; i < N; i++) {
    cin >> S[i];

    stack<bool> st;

    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] == '(') {
        sum++;
        st.push(true);
      } else {
        if (st.size()) {
          st.pop();
        } else {
          num[i].first++;
        }
      }
    }

    num[i].second = st.size();

    if (num[i].first || num[i].second)
      flag = false;

    if (!num[i].first) {
      conl += num[i].second;
    }

    if (!num[i].second) {
      conr += num[i].first;
    }

    if (num[i].first && num[i].second) {
      if (num[i].first - num[i].second <= 0)
        hoge.push_back({num[i].second, num[i]});
      if (num[i].first - num[i].second > 0)
        fuga.push_back({num[i].first, num[i]});
    }
  }

  if (!sum) {
    cout << "No" << endl;
    return 0;
  }

  if (flag) {
    cout << "Yes" << endl;
    return 0;
  }

  sort(hoge.begin(), hoge.end());
  sort(fuga.begin(), fuga.end());

  for (int i = 0; i < hoge.size(); i++) {
    if (conl < hoge[i].first) {
      exit(2);
      cout << "No" << endl;
      return 0;
    }

    conl += hoge[i].second.second - hoge[i].second.first;
  }

  for (int i = 0; i < fuga.size(); i++) {
    if (conr < fuga[i].first) {
      cout << "No" << endl;
      return 0;
    }

    conr += fuga[i].second.first - fuga[i].second.second;
  }

  if (conr == conl) {
    cout << "Yes" << endl;
    return 0;
  }

  cout << "No" << endl;

  return 0;
}