Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

bool cmp(const pii &a, const pii &b) {
  int tmp1 = max(a.first, b.first + a.second);
  int tmp2 = max(b.first, a.first + b.second);
  if (tmp1 < tmp2)
    return true;
  return false;
}

int main() {
  int N;
  cin >> N;
  vector<pii> cand1, cand2, cand3;
  for (int i = 0; i < N; i++) {
    string tmp;
    cin >> tmp;
    int mxneed = 0, backup = 0;
    for (int j = 0; j < (int)tmp.length(); j++) {
      if (tmp[j] == ')') {
        if (backup)
          backup--;
        else
          mxneed--;
      } else {
        backup++;
      }
    }
    if (mxneed == 0)
      cand1.push_back(make_pair(0, backup));
    else if (mxneed + backup >= 0) {
      cand2.push_back(make_pair(-mxneed, backup + mxneed));
    } else {
      cand3.push_back(make_pair(-mxneed, -(backup + mxneed)));
    }
  }
  sort(cand1.begin(), cand1.end());
  int sum = 0;
  for (auto i : cand1)
    sum += i.second;
  sort(cand2.begin(), cand2.end());
  bool OK = true;
  for (auto i : cand2) {
    if (sum < i.first) {
      OK = false;
    }
    sum += i.second;
  }
  sort(cand3.begin(), cand3.end(), cmp);
  for (auto i : cand3) {
    if (sum < i.first) {
      // cout << "sum = " << sum << "need = " << i.first << "\\n";
      // cout << "lose = " << i.second << "\\n";
      OK = false;
    }
    sum -= i.second;
  }
  if (sum != 0 || !OK)
    cout << "No\\n";
  else
    cout << "Yes\\n";
  return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
by Benq;
*/


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

bool cmp(const pii &a, const pii &b) {
  int tmp1 = max(a.first, b.second + a.first);
  int tmp2 = max(b.first, a.first + b.second);
  if (tmp1 <= tmp2)
    return true;
  return false;
}

int main() {
  int N;
  cin >> N;
  vector<pii> cand1, cand2, cand3;
  for (int i = 0; i < N; i++) {
    string tmp;
    cin >> tmp;
    int mxneed = 0, backup = 0;
    for (int j = 0; j < tmp.length(); j++) {
      if (tmp[j] == ')') {
        if (backup)
          backup--;
        else
          mxneed--;
      } else {
        backup++;
      }
    }
    if (mxneed == 0)
      cand1.push_back(make_pair(0, backup));
    else if (mxneed + backup >= 0) {
      cand2.push_back(make_pair(-mxneed, backup + mxneed));
    } else {
      cand3.push_back(make_pair(-mxneed, -(backup + mxneed)));
    }
  }
  sort(cand1.begin(), cand1.end());
  int sum = 0;
  for (auto i : cand1)
    sum += i.second;
  sort(cand2.begin(), cand2.end());
  bool OK = true;
  for (auto i : cand2) {
    if (sum < i.first)
      OK = false;
    sum += i.second;
  }
  sort(cand3.begin(), cand3.end(), cmp);
  for (auto i : cand3) {
    if (sum < i.first)
      OK = false;
    sum -= i.second;
  }
  if (sum != 0 || !OK)
    cout << "No\\n";
  else
    cout << "Yes\\n";
  return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
by Benq;
*/