Problem p02686 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed << setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int MOD = 998244353;
const int N = (1e6) + 5;

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n;
    cin >> n;
    pii p[n];
    int chk = 0;
    for (int ii = 0; ii < n; ii++) {
      string s;
      cin >> s;
      stack<char> st;
      for (int i = 0; i < s.length(); i++) {
        if (!st.size())
          st.push(s[i]);
        else {
          if (s[i] == ')' && st.top() == '(')
            st.pop();
          else
            st.push(s[i]);
        }
      }
      int op = 0, cl = 0;
      while (st.size()) {
        if (st.top() == '(')
          op++;
        else
          cl++;
        st.pop();
      }
      p[ii] = {cl, op};
      chk += op;
      chk -= cl;
    }
    // for(int i=0;i<n;i++)cout<<p[i].ff<<" "<<p[i].ss<<endl;
    if (chk)
      return cout << "No", 0;
    chk = 1;
    vector<pii> a, b, c;
    for (int i = 0; i < n; i++) {
      if (p[i].ff && p[i].ss)
        b.pb(p[i]);
      else {
        if (p[i].ff)
          c.pb(p[i]);
        else if (p[i].ss)
          a.pb(p[i]);
      }
    }
    if (!b.size())
      return cout << "Yes", 0;
    vector<pii> plus, minus;
    for (auto p : a)
      plus.pb({0, p.ss});
    for (auto p : c)
      minus.pb({0, p.ff});
    for (auto p : b) {
      if (p.ss - p.ff >= 0)
        plus.pb({-p.ff, p.ss - p.ff});
      else
        minus.pb({-p.ss, p.ff - p.ss});
    }
    sort(all(plus));
    reverse(all(plus));
    sort(all(minus));
    reverse(all(minus));
    int curr = 0;
    for (auto p : plus) {
      int now = curr + p.ff;
      if (now < 0)
        chk = 0;
      curr += p.ss;
    }
    curr = 0;
    for (auto p : minus) {
      int now = curr + p.ff;
      if (now < 0)
        chk = 0;
      curr += p.ss;
    }
    cout << (chk ? "Yes" : "No") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed << setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int MOD = 998244353;
const int N = (1e6) + 5;

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n;
    cin >> n;
    pii p[n];
    int chk = 0;
    for (int ii = 0; ii < n; ii++) {
      string s;
      cin >> s;
      stack<char> st;
      for (int i = 0; i < s.length(); i++) {
        if (!st.size())
          st.push(s[i]);
        else {
          if (s[i] == ')' && st.top() == '(')
            st.pop();
          else
            st.push(s[i]);
        }
      }
      int op = 0, cl = 0;
      while (st.size()) {
        if (st.top() == '(')
          op++;
        else
          cl++;
        st.pop();
      }
      p[ii] = {cl, op};
      chk += op;
      chk -= cl;
    }
    // for(int i=0;i<n;i++)cout<<p[i].ff<<" "<<p[i].ss<<endl;
    if (chk)
      return cout << "No", 0;
    chk = 1;
    vector<pii> a, b, c;
    for (int i = 0; i < n; i++) {
      if (p[i].ff && p[i].ss)
        b.pb(p[i]);
      else {
        if (p[i].ff)
          c.pb(p[i]);
        else
          a.pb(p[i]);
      }
    }
    sort(all(b));
    int curr = 0;
    for (auto p : a)
      curr += p.ss;
    multiset<int> ms;
    int j = 0;
    while (j < b.size() && b[j].ff <= curr)
      ms.insert(b[j].ss - b[j].ff);
    for (int i = 0; chk && i < b.size(); i++) {
      if (!ms.size()) {
        chk = 0;
        break;
      }
      int ma = *ms.rbegin();
      ms.erase(ms.find(ma));
      curr += ma;
      while (j < b.size() && b[j].ff <= curr)
        ms.insert(b[j].ss - b[j].ff);
      if (curr < 0)
        chk = 0;
    }
    cout << (chk ? "Yes" : "No") << endl;
  }
  return 0;
}