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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define endl "\\n"
#define forn(i, n) for (int i = 0; i < n; i++)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void subsetsUtil(vector<int> &A, vector<vector<int>> &res, vector<int> &subset,
                 int index) {
  res.push_back(subset);
  for (int i = index; i < A.size(); i++) {

    // include the A[i] in subset.
    subset.push_back(A[i]);

    // move onto the next element.
    subsetsUtil(A, res, subset, i + 1);

    // exclude the A[i] from subset and triggers
    // backtracking.
    subset.pop_back();
  }

  return;
}

// below function returns the subsets of vector A.
vector<vector<int>> subsets(vector<int> &A) {
  vector<int> subset;
  vector<vector<int>> res;

  // keeps track of current element in vector A;
  int index = 0;
  subsetsUtil(A, res, subset, index);

  return res;
}

int32_t main() {
  zeus();

  int n;
  cin >> n;

  string s[n];

  vector<pair<int, int>> type1, type2;
  int suma = 0, sumb = 0;

  forn(i, n) {
    cin >> s[i];
    stack<char> x;

    int a = 0, b = 0;
    forn(j, s[i].length()) {
      if (s[i][j] == '(') {
        suma++;
      } else {
        sumb++;
      }

      if (x.empty()) {
        x.push(s[i][j]);
        if (s[i][j] == '(') {
          b++;
        } else {
          a++;
        }
      } else if (s[i][j] == ')' && x.top() == '(') {
        x.pop();
        b--;
      } else {
        x.push(s[i][j]);
        if (s[i][j] == '(') {
          b++;
        } else {
          a++;
        }
      }
    }
    if (a <= b) {
      type1.pb(mp(a, b));
    } else {
      type2.pb(mp(-1 * b, a));
    }
  }

  if (suma != sumb) {
    cout << "No" << endl;
    return 0;
  }

  sort(type1.begin(), type1.end());

  sort(type2.begin(), type2.end());

  int brack = 0;
  int flag = 1;
  forn(i, type1.size()) {
    brack -= type1[i].ff;
    if (brack < 0) {
      flag = 0;
      break;
    }
    brack += type1[i].ss;
  }
  if (flag != 0) {
    forn(i, type2.size()) {
      brack -= type2[i].ss;
      if (brack < 0) {
        flag = 0;
        break;
      }
      brack -= type2[i].ff;
    }
  }

  if (flag == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define endl "\\n"
#define forn(i, n) for (int i = 0; i < n; i++)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void subsetsUtil(vector<int> &A, vector<vector<int>> &res, vector<int> &subset,
                 int index) {
  res.push_back(subset);
  for (int i = index; i < A.size(); i++) {

    // include the A[i] in subset.
    subset.push_back(A[i]);

    // move onto the next element.
    subsetsUtil(A, res, subset, i + 1);

    // exclude the A[i] from subset and triggers
    // backtracking.
    subset.pop_back();
  }

  return;
}

// below function returns the subsets of vector A.
vector<vector<int>> subsets(vector<int> &A) {
  vector<int> subset;
  vector<vector<int>> res;

  // keeps track of current element in vector A;
  int index = 0;
  subsetsUtil(A, res, subset, index);

  return res;
}

int32_t main() {
  zeus();

  int n;
  cin >> n;

  string s[n];

  stack<char> st[n];
  vector<pair<int, int>> type1, type2;
  int suma = 0, sumb = 0;

  forn(i, n) {
    cin >> s[i];
    stack<char> x;

    int a = 0, b = 0;
    forn(j, s[i].length()) {
      if (s[i][j] == '(') {
        suma++;
      } else {
        sumb++;
      }

      if (x.empty()) {
        x.push(s[i][j]);
        if (s[i][j] == '(') {
          b++;
        } else {
          a++;
        }
      } else if (s[i][j] == ')' && x.top() == '(') {
        x.pop();
        b--;
      } else {
        x.push(s[i][j]);
        if (s[i][j] == '(') {
          b++;
        } else {
          a++;
        }
      }
    }
    st[i] = x;
    if (a <= b) {
      type1.pb(mp(a, b));
    } else {
      type2.pb(mp(-1 * b, a));
    }
  }

  if (suma != sumb) {
    cout << "No" << endl;
    return 0;
  }

  sort(type1.begin(), type1.end());

  sort(type2.begin(), type2.end());

  int brack = 0;
  int flag = 1;
  forn(i, type1.size()) {
    brack -= type1[i].ff;
    if (brack < 0) {
      flag = 0;
      break;
    }
    brack += type1[i].ss;
  }
  if (flag != 0) {
    forn(i, type2.size()) {
      brack -= type2[i].ss;
      if (brack < 0) {
        flag = 0;
        break;
      }
      brack -= type2[i].ff;
    }
  }

  if (flag == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}