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

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
int pp[1000010] = {};
int min_val[1000010] = {};
int mm[1000010] = {};
string S[1000010];
bool use[1000010] = {};
typedef struct {
  ll a;
  ll b;
  ll c;
} P;

void solve() {
  ll N;
  cin >> N;
  rep(i, 0, N) cin >> S[i];
  // if(N >= 5000) exit(1);
  ll c0 = 0, c1 = 0;
  rep(i, 0, N) min_val[i] = 1e9;
  rep(i, 0, N) {
    int r = 0;
    for (auto &e : S[i]) {
      if (e == '(') {
        c0++;
        pp[i]++;
        r++;
      } else {
        c1++;
        r--;
        pp[i]--;
      }
      min_val[i] = min(min_val[i], r);
      mm[i] = max(mm[i], r);
    }
  }
  if (c0 != c1) {
    print("No");
    return;
  }
  ll cur = 0;
  bool ok = true;
  ll cnt = 0;
  // rep(i,0,N){
  //     print(pp[i], min_val[i]);
  // }
  vector<P> lp, lp2;
  rep(i, 0, N) {
    if (pp[i] >= 0) {
      lp.push_back({pp[i], min_val[i], mm[i]});
    } else {
      lp2.push_back({pp[i], min_val[i], mm[i]});
    }
  }
  sort(lp.begin(), lp.end(), [](P l1, P l2) { return l1.b > l2.b; });
  sort(lp2.begin(), lp2.end(),
       [](P l1, P l2) { return l1.a - l1.b > l2.a - l2.b; });
  for (auto &e : lp) {
    if (cur + e.b < 0) {
      ok = false;
      break;
    }
    cur += e.a;
  }
  for (auto &e : lp2) {
    if (cur + e.b < 0) {
      ok = false;
      break;
    }
    cur += e.a;
  }

  if (ok) {
    print("Yes");
  } else {
    print("No");
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}

Error: WA

Fixed submission:

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
int pp[1000010] = {};
int min_val[1000010] = {};
int mm[1000010] = {};
string S[1000010];
bool use[1000010] = {};
typedef struct {
  ll a;
  ll b;
  ll c;
} P;

void solve() {
  ll N;
  cin >> N;
  rep(i, 0, N) cin >> S[i];
  // if(N >= 5000) exit(1);
  ll c0 = 0, c1 = 0;
  rep(i, 0, N) min_val[i] = 1e9;
  rep(i, 0, N) {
    int r = 0;
    for (auto &e : S[i]) {
      if (e == '(') {
        c0++;
        pp[i]++;
        r++;
      } else {
        c1++;
        r--;
        pp[i]--;
      }
      min_val[i] = min(min_val[i], r);
      mm[i] = max(mm[i], r);
    }
  }
  if (c0 != c1) {
    print("No");
    return;
  }
  ll cur = 0;
  bool ok = true;
  ll cnt = 0;
  // rep(i,0,N){
  //     print(pp[i], min_val[i]);
  // }
  vector<P> lp;
  rep(i, 0, N) lp.push_back({pp[i], min_val[i], mm[i]});
  sort(lp.begin(), lp.end(),
       [](P l1, P l2) { return l1.a + l2.a > abs(l1.b + l2.b); });
  rep(i, 0, N) {
    // print(lp[i].b);
    if (cur + lp[i].b < 0) {
      ok = false;
      break;
    }
    cur += lp[i].a;
  }

  // while(cnt < N){
  //     ll max_idx = -1, max_val = -INF;

  //     rep(i,0,N){
  //         if(use[i]) continue;
  //         if(cur + min_val[i] >= 0){
  //             if(max_val < pp[i]){
  //                 max_val = pp[i];
  //                 max_idx = i;
  //             }
  //         }
  //     }
  //     if(max_idx == -1){
  //         ok = false;
  //         break;
  //     }
  //     print(max_idx);
  //     use[max_idx] = true;
  //     cur += max_val;
  //     // print(cur);
  //     cnt++;
  // }

  if (ok) {
    print("Yes");
  } else {
    print("No");
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}