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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF (1 << 30 - 1)
#define LLINF (1LL << 60)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

signed main() {
  cin >> N;
  vector<string> v(N);
  REP(i, N) { cin >> v[i]; }
  list<pair<int, int>> x;
  list<pair<int, int>> y;
  int plus = 0;
  int minus = 0;
  REP(i, N) {
    int cnt = 0;
    string s = v[i];
    int mn = INF;
    REP(j, s.size()) {
      if (s[j] == '(') {
        cnt++;
      } else {
        cnt--;
      }
      mn = min(cnt, mn);
    }
    if (cnt >= 0) {
      if (mn >= 0) {
        plus += cnt;
      } else {
        pair<int, int> tmp = {mn, cnt};
        x.push_back(tmp);
      }
    } else {
      mn -= cnt;
      cnt *= -1;
      if (mn >= 0) {
        minus += cnt;
      } else {
        pair<int, int> tmp = {mn, cnt};
        y.push_back(tmp);
      }
    }
  }
  while (x.size() > 0) {
    bool flag = false;
    for (auto it = x.begin(); it != x.end(); it++) {
      auto p = *it;
      if (plus + p.first >= 0) {
        plus += p.second;
        auto cp = it;
        it++;
        x.erase(cp);
        flag = true;
      }
    }
    if (!flag) {
      break;
    }
  }
  while (y.size() > 0) {
    bool flag = false;
    for (auto it = y.begin(); it != y.end(); it++) {
      auto p = *it;
      if (minus + p.first >= 0) {
        minus += p.second;
        auto cp = it;
        it++;
        y.erase(cp);
        flag = true;
      }
    }
    if (!flag) {
      break;
    }
  }
  if (x.size() > 0 || y.size() > 0) {
    cout << "No" << endl;
  } else {
    if (plus == minus) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF (1 << 30 - 1)
#define LLINF (1LL << 60)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

signed main() {
  cin >> N;
  vector<string> v(N);
  REP(i, N) { cin >> v[i]; }
  vector<pair<int, int>> x;
  int plus = 0;
  int minus = 0;
  REP(i, N) {
    int cnt = 0;
    string s = v[i];
    int mn = INF;
    REP(j, s.size()) {
      if (s[j] == '(') {
        cnt++;
      } else {
        cnt--;
      }
      mn = min(cnt, mn);
    }
    if (cnt >= 0) {
      if (mn >= 0) {
        plus += cnt;
      } else {
        pair<int, int> tmp = {-mn, cnt};
        x.push_back(tmp);
      }
    } else {
      minus += cnt;
    }
  }
  sort(x.begin(), x.end());
  while (x.size() > 0) {
    bool flag = false;
    for (auto it = x.begin(); it != x.end(); it++) {
      auto p = *it;
      dump(p);
      if (plus - p.first >= 0) {
        plus += p.second;
        x.erase(it);
        flag = true;
      }
    }
    if (!flag) {
      break;
    }
  }
  if (x.size() > 0) {
    cout << "No" << endl;
  } else {
    if (plus + minus == 0) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}