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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  V<P<ll, ll>> ps, ms, zero;
  ll ttttt = 0;
  for (ll i = 0; i < n; i++) {
    string s;
    cin >> s;
    ll total = 0, mini = 0;
    for (ll j = 0; j < (ll)s.size(); j++) {
      if (s[j] == '(')
        total++;
      else
        total--;
      chmin(mini, total);
    }

    if (total < 0) {
      ms.emplace_back(make_pair(total, mini));
    } else if (total > 0) {
      ps.emplace_back(make_pair(total, mini));
    } else {
      zero.emplace_back(make_pair(total, mini));
    }
    ttttt += total;
  }
  cerr << ttttt << '\\n';

  if (ttttt != 0) {
    cout << "No\\n";
    return 0;
  }

  sort(ALL(ps), [&](P<ll, ll> i, P<ll, ll> j) {
    if (i.second == j.second)
      return i.first > j.first;
    return i.second > j.second;
  });

  ll tmp = 0;
  for (ll i = 0; i < (ll)ps.size(); i++) {
    if (tmp + ps[i].second < 0) {
      cout << "No\\n";
      return 0;
    } else {
      tmp += ps[i].first;
    }
  }

  for (ll i = 0; i < (ll)zero.size(); i++) {
    if (tmp + zero[i].second < 0) {
      cout << "No\\n";
      return 0;
    }
  }

  sort(ALL(ms), [&](P<ll, ll> i, P<ll, ll> j) {
    return i.second - i.first < j.second - j.first;
  });
  for (ll i = 0; i < (ll)ms.size(); i++) {
    if (tmp + ms[i].second < 0) {
      cout << "No\\n";
      return 0;
    } else {
      tmp += ms[i].first;
    }
  }

  cout << "Yes\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  V<P<ll, ll>> ps, ms, zero;
  ll ttttt = 0;
  for (ll i = 0; i < n; i++) {
    string s;
    cin >> s;
    ll total = 0, mini = 0;
    for (ll j = 0; j < (ll)s.size(); j++) {
      if (s[j] == '(')
        total++;
      else
        total--;
      chmin(mini, total);
    }

    if (total < 0) {
      ms.emplace_back(make_pair(total, mini));
    } else if (total > 0) {
      ps.emplace_back(make_pair(total, mini));
    } else {
      zero.emplace_back(make_pair(total, mini));
    }
    ttttt += total;
  }
  cerr << ttttt << '\\n';

  if (ttttt != 0) {
    cout << "No\\n";
    return 0;
  }

  sort(ALL(ps), [&](P<ll, ll> i, P<ll, ll> j) {
    if (i.second == j.second)
      return i.first > j.first;
    return i.second > j.second;
  });

  ll tmp = 0;
  for (ll i = 0; i < (ll)ps.size(); i++) {
    if (tmp + ps[i].second < 0) {
      cout << "No\\n";
      return 0;
    } else {
      tmp += ps[i].first;
    }
  }

  for (ll i = 0; i < (ll)zero.size(); i++) {
    if (tmp + zero[i].second < 0) {
      cout << "No\\n";
      return 0;
    }
  }

  sort(ALL(ms), [&](P<ll, ll> i, P<ll, ll> j) { return i.second - i.first; });
  for (ll i = 0; i < (ll)ms.size(); i++) {
    if (tmp + ms[i].second < 0) {
      cout << "No\\n";
      return 0;
    } else {
      tmp += ms[i].first;
    }
  }

  cout << "Yes\\n";
  return 0;
}