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>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multi_set;

#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-9

#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fs first
#define sc second
#define pb push_back
#define sp printf(" ")
#define nl '\\n'
#define all(a) a.begin(), a.end()
#define unique(c) (c).resize(unique(all(c)) - (c).begin())
#define itn int

#define set0(a) memset(a, 0, sizeof(a))
#define setneg(a) memset(a, -1, sizeof(a))
#define setinf(a) memset(a, 126, sizeof(a))

#define REP(i, start, end) for (int i = start; i <= end; i++)
#define RREP(i, end, start) for (int i = end; i >= start; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;
typedef vector<vector<LL>> matrix;
typedef vector<vector<int>> graph;

class seq {
public:
  int low, high, bal, id;
  seq(int a, int b, int c, int d) {
    low = a;
    high = b;
    bal = c;
    id = d;
  }
};

bool custom(seq a, seq b) {
  if (a.low == b.low)
    return a.bal > b.bal;
  return a.low < b.low;
}
bool custom2(seq a, seq b) {
  if (a.high == b.high)
    return a.bal < b.bal;
  return a.high < b.high;
}
void solve() {
  int n;
  cin >> n;
  vector<seq> vect1, vect2;
  REP(i, 1, n) {
    string s;
    cin >> s;
    int low = 0;
    int high = 0;
    int bal = 0;
    REP(j, 0, s.size() - 1) {
      bal += (s[j] == '(');
      bal -= (s[j] == ')');
      low = min(low, bal);
    }
    bal = 0;
    RREP(j, s.size() - 1, 0) {
      bal -= (s[j] == '(');
      bal += (s[j] == ')');
      high = min(high, bal);
    }
    seq sq(-low, -high, -bal, i);
    if (bal < 0)
      vect1.pb(sq);
    else
      vect2.pb(sq);
  }
  sort(all(vect1), custom);
  sort(all(vect2), custom2);
  int bal = 0;
  if (vect1.size() >= 1) {
    REP(i, 0, vect1.size() - 1) {
      if (vect1[i].low > bal) {
        cout << "No" << nl;
        return;
      }
      bal += vect1[i].bal;
    }
  }
  int bal2 = 0;
  if (vect2.size() >= 1) {
    REP(i, 0, vect2.size() - 1) {
      if (vect2[i].high > bal2) {

        cout << "No" << nl;
        return;
      }
      bal2 -= vect2[i].bal;
    }
  }
  if (bal == bal2) {
    cout << "Yes" << nl;
  }

  else
    cout << "No" << nl;
}
int main() {
  fast;
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multi_set;

#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-9

#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fs first
#define sc second
#define pb push_back
#define sp printf(" ")
#define nl '\\n'
#define all(a) a.begin(), a.end()
#define unique(c) (c).resize(unique(all(c)) - (c).begin())
#define itn int

#define set0(a) memset(a, 0, sizeof(a))
#define setneg(a) memset(a, -1, sizeof(a))
#define setinf(a) memset(a, 126, sizeof(a))

#define REP(i, start, end) for (int i = start; i <= end; i++)
#define RREP(i, end, start) for (int i = end; i >= start; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;
typedef vector<vector<LL>> matrix;
typedef vector<vector<int>> graph;

class seq {
public:
  int low, high, bal, id;
  seq(int a, int b, int c, int d) {
    low = a;
    high = b;
    bal = c;
    id = d;
  }
};

bool custom(seq a, seq b) {
  if (a.low == b.low)
    return a.bal > b.bal;
  return a.low < b.low;
}
bool custom2(seq a, seq b) {
  if (a.high == b.high)
    return a.bal < b.bal;
  return a.high < b.high;
}
void solve() {
  int n;
  cin >> n;
  vector<seq> vect1, vect2;
  REP(i, 1, n) {
    string s;
    cin >> s;
    int low = 0;
    int high = 0;
    int bal = 0;
    REP(j, 0, s.size() - 1) {
      bal += (s[j] == '(');
      bal -= (s[j] == ')');
      low = min(low, bal);
    }
    bal = 0;
    RREP(j, s.size() - 1, 0) {
      bal -= (s[j] == '(');
      bal += (s[j] == ')');
      high = min(high, bal);
    }
    cerr << s << " " << -low << " " << -bal << nl;
    seq sq(-low, -high, -bal, i);
    if (bal < 0)
      vect1.pb(sq);
    else
      vect2.pb(sq);
  }
  sort(all(vect1), custom);
  sort(all(vect2), custom2);
  stringstream ss, ss2;
  int bal = 0;
  if (vect1.size() >= 1) {
    REP(i, 0, vect1.size() - 1) {
      if (vect1[i].low > bal) {
        cout << "No" << nl;
        return;
      }
      bal += vect1[i].bal;
      ss << vect1[i].id << " ";
    }
  }
  int bal2 = 0;
  if (vect2.size() >= 1) {
    REP(i, 0, vect2.size() - 1) {
      if (vect2[i].high > bal2) {

        cout << "No" << nl;
        return;
      }
      bal2 -= vect2[i].bal;
      ss2 << vect2[i].id << " ";
    }
  }
  // cerr<<bal<<" "<<bal2<<nl;
  if (bal == bal2) {
    cout << "Yes" << nl;
  }

  else
    cout << "No" << nl;
}
int main() {
  fast;
  solve();
}