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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / __gcd((a), (b))
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vi>
#define mt make_tuple
#define mp make_pair
#define hell 1000000007
#define mod 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define ign cin.ignore();
#define g(a, b) get<a>(b)
#define vt(a) vector<a>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = b - 1; i >= a; i--)
#define fi first
#define se second

#define o_set                                                                  \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define o_setll                                                                \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// to delete an element
// s.erase(s.find_by_order(s.order_of_key(ele)));
// where s is the name of the ordered_set
// member functions :
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

using namespace std;
using namespace __gnu_pbds;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef MY_PROJECT
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

tuple<int, int> process(string &s) {
  int mini = INT_MAX;
  int delta = 0;
  rep(i, 0, s.length()) {
    if (s[i] == '(')
      delta++;
    else
      delta--;
    mini = min(mini, delta);
  }
  int required = -mini;
  return make_tuple(delta, required);
}

class Compare2 {
public:
  // delta = +ve, required = +ve
  bool operator()(tuple<int, int> &a, tuple<int, int> &b) {
    return g(1, a) > g(1, b);
  }
};

class Compare3 {
public:
  // delta = -ve, required = +ve
  bool operator()(tuple<int, int> &a, tuple<int, int> &b) {
    int first_before_second = max(g(1, a), g(1, b) - g(0, a));
    int second_before_first = max(g(1, b), g(1, a) - g(0, b));
    return first_before_second > second_before_first;
  }
};

class Compare4 {
public:
  // delta = -ve, required = -ve
  bool operator()(tuple<int, int> &a, tuple<int, int> &b) {
    int first_before_second = max(g(1, a), g(1, b) - g(0, a));
    int second_before_first = max(g(1, b), g(1, a) - g(0, b));
    return first_before_second > second_before_first;
  }
};

int main() {
  IOS
      /*
      freopen("random_11.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      */
      ll ntc = 1;
  // cin>>ntc;
  rep(tc, 1, ntc + 1) {
    // cout<<"Case #"<<tc<<": ";
    int n;
    cin >> n;
    string s;
    priority_queue<tuple<int, int>, vector<tuple<int, int>>> pq1;
    priority_queue<tuple<int, int>, vector<tuple<int, int>>, Compare2> pq2;
    priority_queue<tuple<int, int>, vector<tuple<int, int>>, Compare3> pq3;
    priority_queue<tuple<int, int>, vector<tuple<int, int>>, Compare4> pq4;
    for (int i = 0; i < n; i++) {
      cin >> s;
      tuple<int, int> tup = process(s);
      if (g(0, tup) >= 0) {
        if (g(1, tup) <= 0)
          pq1.push(tup);
        else
          pq2.push(tup);
      } else {
        if (g(1, tup) >= 0)
          pq3.push(tup);
        else
          pq4.push(tup);
      }
    }
    int delta = 0;
    bool can = true;
    while (!pq1.empty()) {
      if (delta >= g(1, pq1.top())) {
        delta = delta + g(0, pq1.top());
        pq1.pop();
      } else {
        debug("1");
        can = false;
        break;
      }
    }
    if (can) {
      while (!pq2.empty()) {
        if (delta >= g(1, pq2.top())) {
          delta = delta + g(0, pq2.top());
          pq2.pop();
        } else {
          debug("2");
          can = false;
          break;
        }
      }
    }
    if (can) {
      while (!pq3.empty()) {
        if (delta >= g(1, pq3.top())) {
          delta = delta + g(0, pq3.top());
          pq3.pop();
        } else {
          debug("3");
          can = false;
          break;
        }
      }
    }
    if (can) {
      while (!pq4.empty()) {
        if (delta >= g(1, pq4.top())) {
          delta = delta + g(0, pq4.top());
          pq4.pop();
        } else {
          debug("4");
          can = false;
          break;
        }
      }
    }

    if (can && delta == 0) {
      cout << "Yes\\n";
    } else
      cout << "No\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / __gcd((a), (b))
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vi>
#define mt make_tuple
#define mp make_pair
#define hell 1000000007
#define mod 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define ign cin.ignore();
#define g(a, b) get<a>(b)
#define vt(a) vector<a>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = b - 1; i >= a; i--)
#define fi first
#define se second

#define o_set                                                                  \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define o_setll                                                                \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// to delete an element
// s.erase(s.find_by_order(s.order_of_key(ele)));
// where s is the name of the ordered_set
// member functions :
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

using namespace std;
using namespace __gnu_pbds;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef MY_PROJECT
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

bool isValid(string str) {
  int crt = 0;
  for (int i = 0; i < str.length(); i++) {
    if (str[i] == '(')
      crt++;
    else
      crt--;
    if (crt < 0)
      return false;
  }
  if (crt == 0)
    return true;
  return false;
}

int main() {
  IOS
      /*
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      */
      ll ntc = 1;
  // cin>>ntc;
  rep(tc, 1, ntc + 1) {
    // cout<<"Case #"<<tc<<": ";
    int n;
    cin >> n;
    vt(string) v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    vpii vec[2];
    for (int i = 0; i < n; i++) {
      int cnto = 0, cntc = 0;
      int a = INT_MIN;
      for (int j = 0; j < v[i].length(); j++) {
        if (v[i][j] == '(')
          cnto++;
        else
          cntc++;
        a = max(cntc - cnto, a);
      }
      int b = INT_MIN;
      cnto = 0;
      cntc = 0;
      repi(j, v[i].length(), 0) {
        if (v[i][j] == '(')
          cnto++;
        else
          cntc++;
        b = max(cnto - cntc, b);
      }
      if (b - a < 0) {
        vec[0].pb(mp(a, i));
      } else {
        vec[1].pb(mp(b, i));
      }
    }
    sort(all(vec[0]));
    sort(all(vec[1]), greater<>());
    string str;
    for (int i = 0; i < vec[0].size(); i++) {
      str.append(v[vec[0][i].se]);
    }
    for (int i = 0; i < vec[1].size(); i++) {
      str.append(v[vec[1][i].se]);
    }
    if (isValid(str)) {
      cout << "Yes\\n";
    } else
      cout << "No\\n";
  }
  return 0;
}