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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \\
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 1000005;
const int mod = 998244353;

struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
  bool operator<(const P &a) const { return y > a.y; }
};
struct P2 {
  int x, y;
  P2(int x, int y) : x(x), y(y) {}
};

struct P3 {
  int x, y;
  P3(int x, int y) : x(x), y(y) {}
  bool operator<(const P3 &a) const { return y - x < a.y - a.x; }
};

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  string s;
  vector<P> p1;
  vector<P2> p2;
  vector<P3> p3;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int cur = 0;
    int mi = 0;
    for (auto c : s) {
      cur += (c == '(' ? 1 : -1);
      mi = min(mi, cur);
    }
    if (cur < 0)
      p3.emplace_back(cur, mi);
    else if (cur == 0)
      p2.emplace_back(cur, mi);
    else
      p1.emplace_back(cur, mi);
  }
  sort(p1.begin(), p1.end());
  sort(p3.begin(), p3.end());
  int cur = 0;
  for (auto &p : p1) {
    if (cur + p.y < 0 || cur + p.x < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += p.x;
  }
  for (auto &p : p2) {
    if (cur + p.y < 0 || cur + p.x < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += p.x;
  }
  for (auto &p : p3) {
    if (cur + p.y < 0 || cur + p.x < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += p.x;
  }
  if (cur == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \\
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 1000005;
const int mod = 998244353;

struct P {
  int x, y;
  P() {}
  P(int x, int y) : x(x), y(y) {}
  bool operator<(const P &a) const {
    if (x > 0) {
      if (a.x > 0) {
        return y > a.y;
      }
      return true;
    } else if (x == 0) {
      if (a.x > 0)
        return false;
      return true;
    } else {
      if (a.x >= 0)
        return false;
      return y - x < a.y - a.x;
    }
  }
} p[maxn];

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int cur = 0;
    int mi = 0;
    for (int j = 0; j < s.size(); j++) {
      cur += (s[j] == '(' ? 1 : -1);
      mi = min(mi, cur);
    }
    p[i].x = cur;
    p[i].y = mi;
  }
  sort(p, p + n);
  int cur = 0;
  for (int i = 0; i < n; i++) {
    if (cur + p[i].y < 0 || cur + p[i].x < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += p[i].x;
  }
  if (cur == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}