Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define pb push_back
#define pii pair<int, int>
#define print(x)                                                               \
  for (auto it : x)                                                            \
    cout << it << ' ';
#define endl "\n" // TODO: remove
#define int long long

const int N = (int)(1e6) + 322;
const int inf = (int)(2e9);
const int mod = (int)(1e9) + 7; // check modulo

pair<int, int> calc(string s) {
  int bal = 0;
  int mn = inf, mx = 0;
  for (auto i : s) {
    bal += (i == '(' ? 1 : -1);
    mn = min(mn, bal);
  }
  return {bal, mn};
}

pair<int, int> bals[N];
string s[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    bals[i] = calc(s[i]);
  }
  sort(bals + 1, bals + n + 1, [&](pair<int, int> x, pair<int, int> y) {
    if ((x.first >= 0) != (y.second >= 0))
      return (x.first >= 0);
    if (x.first >= 0)
      return x.second > y.second;
    return x.first + y.second > y.first + x.second;
  });
  int bal = 0;
  for (int i = 1; i <= n; ++i) {
    if (bal + bals[i].second < 0) {
      cout << "No";
      return;
    }
    bal += bals[i].first;
  }
  if (bal != 0) {
    cout << "No";
    return;
  }
  cout << "Yes";
}

signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  //    freopen("elevator.in", "r", stdin);
  //    freopen("elevator.out", "w", stdout);
  //    int test; cin >> test;
  //    for(int t = 1; t <= test; ++t) {
  solve();
  //    }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define pb push_back
#define pii pair<int, int>
#define print(x)                                                               \
  for (auto it : x)                                                            \
    cout << it << ' ';
#define endl "\n" // TODO: remove
#define int long long

const int N = (int)(1e6) + 322;
const int inf = (int)(2e9);
const int mod = (int)(1e9) + 7; // check modulo

pair<int, int> calc(string s) {
  int bal = 0;
  int mn = inf, mx = 0;
  for (auto i : s) {
    bal += (i == '(' ? 1 : -1);
    mn = min(mn, bal);
  }
  return {bal, mn};
}

pair<int, int> bals[N];
string s[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    bals[i] = calc(s[i]);
  }
  sort(bals + 1, bals + n + 1, [&](pair<int, int> x, pair<int, int> y) {
    if ((x.first >= 0) != (y.first >= 0))
      return (x.first >= 0);
    if (x.first >= 0)
      return x.second > y.second;
    return x.first + y.second > y.first + x.second;
  });
  int bal = 0;
  for (int i = 1; i <= n; ++i) {
    if (bal + bals[i].second < 0) {
      cout << "No";
      return;
    }
    bal += bals[i].first;
  }
  if (bal != 0) {
    cout << "No";
    return;
  }
  cout << "Yes";
}

signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  //    freopen("elevator.in", "r", stdin);
  //    freopen("elevator.out", "w", stdout);
  //    int test; cin >> test;
  //    for(int t = 1; t <= test; ++t) {
  solve();
  //    }
  return 0;
}