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

#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> proc(string s) {
  deque<char> dq;
  for (auto c : s) {
    if (c == ' ')
      continue;
    if (c == '(' || dq.empty())
      dq.push_back(c);
    else if (c == ')') {
      if ('(' == dq.back())
        dq.pop_back();
      else
        dq.push_back(c);
    }
  }
  pair<long long, long long> ans(0, 0);
  while (!dq.empty()) {
    if (dq.front() == ')')
      ans.first++;
    else
      ans.second++;
    dq.pop_front();
  }
  return ans;
}
void solve() {
  int n;
  string s;
  cin >> n;
  vector<pair<long long, long long>> lf, rg, mid;
  cin.ignore();
  for (int i = 0; i < n; ++i) {
    getline(cin, s);
    pair<long long, long long> p = proc(s);
    if (p.first > 0 && p.second > 0)
      mid.push_back(p);
    else if (p.first > 0)
      rg.push_back(p);
    else if (p.second > 0)
      lf.push_back(p);
  }
  long long ll = 0;
  for (auto x : lf)
    ll += x.second;
  sort(mid.begin(), mid.end(), [&](auto p1, auto p2) {
    long long diff1 = p1.second - p1.first;
    long long diff2 = p2.second - p2.first;
    if (diff1 >= 0 && diff2 < 0)
      return true;
    if (diff2 >= 0 && diff1 < 0)
      return false;
    if (diff1 >= 0 && diff2 >= 0) {
      return p1.first < p2.first;
    }
    return p1.second > p2.second;

    if (p1.first != p2.first)
      return p1.first < p2.first;
    else
      return p1.second > p2.second;
  });
  for (auto x : mid) {
    if (ll < x.first) {
      cout << "No\\n";
      return;
    }
    ll -= x.first;
    ll += x.second;
  }
  for (auto x : rg) {
    if (ll < x.first) {
      cout << "No\\n";
      return;
    }
    ll -= x.first;
  }
  if (ll == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tc = 1;
  // cin >> tc;
  for (int i = 1; i <= tc; ++i) {
    // cout << "Case #" << i <<": ";
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string proc(string s) {
  deque<char> dq;
  char cc;
  for (auto c : s) {
    if (c == '(' || dq.empty()) {
      dq.push_back(c);
    } else if (c == ')') {
      cc = dq.back();
      if (cc == '(') {
        dq.pop_back();
      } else {
        dq.push_back(c);
      }
    }
  }
  string ans = "";
  while (!dq.empty()) {
    ans.push_back(dq.front());
    dq.pop_front();
  }
  return ans;
}
bool mycomp(pair<int, int> &p1, pair<int, int> &p2) {
  if (p1.first != p1.second)
    return p1.first < p1.second;
  else
    return p1.second > p2.second;
}
void solve() {
  int n;
  string s;
  cin >> n;
  vector<pair<int, int>> lf, rg, mid;

  for (int i = 0; i < n; ++i) {
    cin >> s;
    s = proc(s);
    // cout << s << endl;
    if (s.size() != 0) {
      int ll = 0;
      int rr = 0;
      for (auto c : s) {
        if (c == ')')
          ll++;
        else
          rr++;
      }
      pair<int, int> p;
      p.first = ll;
      p.second = rr;
      if (ll > 0 && rr > 0)
        mid.push_back(p);
      else if (ll > 0)
        rg.push_back(p);
      else
        lf.push_back(p);
    }
  }
  long long ll = 0;
  for (auto x : lf)
    ll += x.second;
  if ((int)mid.size() > 0)
    sort(mid.begin(), mid.end(), mycomp);
  for (auto x : mid) {
    if (ll < x.first) {
      cout << "No\\n";
      return;
    }
    ll -= x.first;
    ll += x.second;
  }
  for (auto x : rg) {
    if (ll < x.first) {
      cout << "No\\n";
      return;
    }
    ll -= x.first;
  }
  if (ll == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tc = 1;
  // cin >> tc;
  for (int i = 1; i <= tc; ++i) {
    // cout << "Case #" << i <<": ";
    solve();
  }
  return 0;
}