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

#include <bits/stdc++.h>
#define bear ryan
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
#define endl '\\n'
#define ends ' '
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#define LOCAL
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#else
#define debug(...) 917
#endif

const ll inf = 1e9;
const ll mod = 998244353;

void rmax(ll &r, ll x) { r = max(r, x); }

ll power(ll a, ll b) {
  ll r = 1, t = a;
  while (b) {
    if (b & 1)
      r = r * t % mod;
    t = t * t % mod;
    b >>= 1;
  }
  return r;
}

const int maxn = 3e6 + 10;

ll n;
vector<pll> a, b;

int main(void) {
  fastio;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    ll mn = 0, diff = 0;
    for (auto c : s) {
      diff += (c == '(' ? 1 : -1);
      mn = min(mn, diff);
    }
    if (diff >= 0)
      a.eb(diff, mn);
    else
      b.eb(diff, mn);
    sum += diff;
  }
  if (sum != 0) {
    cout << "No";
    return 0;
  }
  sort(all(a), [](pll l, pll r) { return l.se > r.se; });
  sum = 0;
  for (auto t : a) {
    ll diff = t.fi, mn = t.se;
    if (sum + mn < 0) {
      cout << "No";
      return 0;
    }
    sum += diff;
  }

  sort(all(b), [](pll l, pll r) { return l.fi - l.se > r.fi - r.se; });
  for (auto t : b) {
    ll diff = t.fi, mn = t.se;
    if (sum + mn < 0) {
      cout << "No";
      return 0;
    }
    sum += diff;
  }
  cout << "Yes";
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define bear ryan
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
#define endl '\\n'
#define ends ' '
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#define LOCAL
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#else
#define debug(...) 917
#endif

const ll inf = 1e9;
const ll mod = 998244353;

void rmax(ll &r, ll x) { r = max(r, x); }

ll power(ll a, ll b) {
  ll r = 1, t = a;
  while (b) {
    if (b & 1)
      r = r * t % mod;
    t = t * t % mod;
    b >>= 1;
  }
  return r;
}

const int maxn = 1e6 + 10;

ll n;
vector<pll> v;

int main(void) {
  fastio;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    ll mn = 0, diff = 0;
    for (auto c : s) {
      diff += (c == '(' ? 1 : -1);
      mn = min(mn, diff);
    }
    v.eb(diff, mn);
  }
  sort(all(v), [](pll l, pll r) {
    if ((l.fi >= 0) != (r.fi >= 0))
      return l.fi > r.fi;
    if (l.fi > 0)
      return l.se > r.se;
    else
      return l.se < r.se;
  });
  ll sum = 0;
  for (auto t : v) {
    ll diff = t.fi, mn = t.se;
    if (sum + mn < 0) {
      cout << "No";
      return 0;
    }
    sum += diff;
  }
  if (sum == 0)
    cout << "Yes";
  else
    cout << "No";
  return 0;
}