Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define time_taken_start int begtime = clock();
#define time_taken_end                                                         \
  int endtime = clock();                                                       \
  cerr << "\n\n"                                                               \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms\n\n";                                                           \
  return 0;
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 998244353
#define inf 1000000000000000007
#define eps 0.0000000001
#define pi acosl(-1)
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vpl vector<pll>
#define vll vector<ll>
#define mseti multiset<ll>
#define msetd multiset<ll, greater<ll>>
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define stp fixed << setprecision(20)
#define endl '\n'
vpl v;
bool comparion(pll a, pll b) {
  ll x = min(a.ss, a.ff + b.ss);
  ll y = min(b.ss, b.ff + a.ss);
  return (x >= y);
}
void solve() {
  ll n;
  cin >> n;
  string s;
  for (ll i = 0; i < n; i++) {
    cin >> s;
    ll d = 0, x = 0;
    for (ll j = 0; j < s.length(); j++) {
      if (s[j] == '(')
        x++;
      else
        x--;
      d = min(d, x);
    }
    v.pb({x, d});
  }
  sort(v.begin(), v.end(), comparion);
  ll cnt = 0;
  for (auto i : v) {
    ll x = cnt + i.ss;
    if (x < 0) {
      cout << "No";
      return;
    }
    cnt += i.ff;
    if (cnt < 0) {
      cout << "No";
      return;
    }
  }
  if (cnt != 0)
    cout << "No";
  else
    cout << "Yes";
}
int main() {
  FAST
#ifdef ayk_16
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  time_taken_start ll t = 1;
  // cin>>t;
  for (ll i = 0; i < t; i++) {
    // cout<<"Case #"<<i+1<<": ";
    solve();
  }
  time_taken_end
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define time_taken_start int begtime = clock();
#define time_taken_end                                                         \
  int endtime = clock();                                                       \
  cerr << "\n\n"                                                               \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms\n\n";                                                           \
  return 0;
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 998244353
#define inf 1000000000000000007
#define eps 0.0000000001
#define pi acosl(-1)
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vpl vector<pll>
#define vll vector<ll>
#define mseti multiset<ll>
#define msetd multiset<ll, greater<ll>>
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define stp fixed << setprecision(20)
#define endl '\n'
vpl v;
bool comparion(pll a, pll b) {
  ll x = min(a.ss, a.ff + b.ss);
  ll y = min(b.ss, b.ff + a.ss);
  return (x > y);
}
void solve() {
  ll n;
  cin >> n;
  string s;
  for (ll i = 0; i < n; i++) {
    cin >> s;
    ll d = 0, x = 0;
    for (ll j = 0; j < s.length(); j++) {
      if (s[j] == '(')
        x++;
      else
        x--;
      d = min(d, x);
    }
    v.pb({x, d});
  }
  sort(v.begin(), v.end(), comparion);
  ll cnt = 0;
  for (auto i : v) {
    ll x = cnt + i.ss;
    if (x < 0) {
      cout << "No";
      return;
    }
    cnt += i.ff;
    if (cnt < 0) {
      cout << "No";
      return;
    }
  }
  if (cnt != 0)
    cout << "No";
  else
    cout << "Yes";
}
int main() {
  FAST
#ifdef ayk_16
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  time_taken_start ll t = 1;
  // cin>>t;
  for (ll i = 0; i < t; i++) {
    // cout<<"Case #"<<i+1<<": ";
    solve();
  }
  time_taken_end
}