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

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fs first
#define sc second
#define pb push_back
#define nl '\\n'
#define all(a) a.begin(), a.end()
#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;
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>
using namespace std;

bool areParanthesisBalanced(string expr) {
  stack<char> s;
  char x;

  // Traversing the Expression
  for (int i = 0; i < expr.length(); i++) {
    if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') {
      // Push the element in the stack
      s.push(expr[i]);
      continue;
    }

    // IF current current character is not opening
    // bracket, then it must be closing. So stack
    // cannot be empty at this point.
    if (s.empty())
      return false;

    switch (expr[i]) {
    case ')':

      // Store the top element in a
      x = s.top();
      s.pop();
      if (x == '{' || x == '[')
        return false;
      break;

    case '}':

      // Store the top element in b
      x = s.top();
      s.pop();
      if (x == '(' || x == '[')
        return false;
      break;

    case ']':

      // Store the top element in c
      x = s.top();
      s.pop();
      if (x == '(' || x == '{')
        return false;
      break;
    }
  }

  // Check Empty Stack
  return (s.empty());
}

int main() {
  int n;
  cin >> n;
  vector<string> v;
  string x;

  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  int flag = 1;
  int z = v.size();
  sort(v.begin(), v.end());

  do {
    string a;
    for (size_t i = 0; i != v.size(); ++i) {
      a = a + v[i];
    }

    if (areParanthesisBalanced(a)) {
      flag = 0;
      cout << "Yes" << endl;
      break;
    }
  } while (next_permutation(v.begin(), v.end()));

  if (flag == 1)
    cout << "No" << endl;

  return 0;
}