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

#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;

bool cmp(Pll x, Pll y) {
  if (x.fi == 0) {
    if (y.fi == 0)
      return x.se > y.se;
    else
      return true;
  }
  if (y.fi == 0) {
    return false;
  }
  if (x.se == 0) {
    if (y.se == 0)
      return x.fi < y.fi;
    else
      return false;
  }
  if (y.se == 0) {
    return true;
  }
  if (x.se - x.fi == y.se - y.fi) {
    return x.se > y.se;
  }
  return x.se - x.fi > y.se - y.fi;
}

void Main() {
  int n;
  cin >> n;
  vector<Pll> kakko(n, P(0, 0));
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, s.size()) {
      if (s[j] == '(') {
        kakko[i].se++;
      } else {
        if (kakko[i].se > 0)
          kakko[i].se--;
        else
          kakko[i].fi++;
      }
    }
  }

  stable_sort(all(kakko), cmp);

  /*
  for(auto [x,y] : kakko){
      cout << x << " " << y << endl;
  }
  cout << endl;
  //*/

  Pll ans = Pll(0, 0);
  rep(i, n) {
    if (ans.se >= kakko[i].fi) {
      ans.se -= kakko[i].fi;
    } else {
      ans.fi += kakko[i].fi;
    }
    ans.se += kakko[i].se;
    if (ans.fi > 0) {
      puts("No");
      return;
    }
    // cout << i << ": " << ans.fi << ", " << ans.se << endl;
  }

  if (ans.fi == 0 && ans.se == 0)
    puts("Yes");
  else
    puts("No");
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

bool cmp(P x, P y) {
  if (x.fi == 0)
    return true;
  if (x.se == 0)
    return false;
  return x.se - x.fi > y.se - y.fi;
}

void Main() {
  int n;
  cin >> n;
  vector<P> kakko(n, P(0, 0));
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, s.size()) {
      if (s[j] == '(') {
        kakko[i].se++;
      } else {
        if (kakko[i].se > 0)
          kakko[i].se--;
        else
          kakko[i].fi++;
      }
    }
  }

  /*
  for(auto [x,y] : kakko){
      cout << x << " " << y << endl;
  }
  //*/

  sort(all(kakko), cmp);

  /*
  for(auto [x,y] : kakko){
      cout << x << " " << y << endl;
  }
  //*/

  P ans = P(0, 0);
  rep(i, n) {
    if (ans.se >= kakko[i].fi) {
      ans.se -= kakko[i].fi;
    } else if (ans.se > 0) {
      puts("No");
      return;
    }
    ans.se += kakko[i].se;
  }

  if (ans.fi == 0 && ans.se == 0)
    puts("Yes");
  else
    puts("No");
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}