alexjercan/bugnet huggingface dataset:
Problem p02686 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#pragma GCC target("avx")

#include <bits/stdc++.h>
using namespace std;
#define incID(i, l, r) for (int i = (l); i < (r); ++i)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define dec(i, n) decID(i, 0, n)

// ---- ----

#define setmin(a, b)                                                           \\
  if (b < a) {                                                                 \\
    a = b;                                                                     \\
  }
#define setmax(a, b)                                                           \\
  if (b > a) {                                                                 \\
    a = b;                                                                     \\
  }

const int L = 1'000'000 / 2 + 1;

int A[L], B[L], aa, bb, mi, sum;
char s[1 << 21];
int c;
int main() {
  int size = fread_unlocked(s, 1, 1 << 21, stdin);
  while (s[c] != '\\n') {
    c++;
  }
  while (++c < size) {
    if (s[c] == '\\n') {
      int a = -mi, b = sum - mi;
      if (a <= b) {
        A[a] += sum;
        setmax(aa, a + 1);
      } else {
        B[b] += sum;
        setmax(bb, b + 1);
      }
      mi = 0, sum = 0;
    } else {
      if (s[c] == '(') {
        sum++;
      } else {
        sum--;
        setmin(mi, sum);
      }
    }
  }

  inc(i, aa) {
    setmin(mi, sum - i);
    sum += A[i];
  }
  dec(i, bb) {
    sum += B[i];
    setmin(mi, sum - i);
  }
  puts(mi == 0 && sum == 0 ? "Yes" : "No");
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("avx")

#include <bits/stdc++.h>
using namespace std;
#define incID(i, l, r) for (int i = (l); i < (r); ++i)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define dec(i, n) decID(i, 0, n)

// ---- ----

#define setmin(a, b)                                                           \\
  if (b < a) {                                                                 \\
    a = b;                                                                     \\
  }
#define setmax(a, b)                                                           \\
  if (b > a) {                                                                 \\
    a = b;                                                                     \\
  }

const int L = 1'000'000 / 2 + 1;

int A[L], B[L], aa, bb, mi, sum;
char s[1 << 21], c;
int main() {
  int size = fread_unlocked(s, 1, 1 << 21, stdin);
  while (s[c] != '\\n') {
    c++;
  }

  while (++c < size) {
    if (s[c] == '\\n') {
      int a = -mi, b = sum - mi;
      if (a <= b) {
        A[a] += sum;
        setmax(aa, a + 1);
      } else {
        B[b] += sum;
        setmax(bb, b + 1);
      }
      mi = 0, sum = 0;
    } else {
      if (s[c] == '(') {
        sum++;
      } else {
        sum--;
        setmin(mi, sum);
      }
    }
  }

  inc(i, aa) {
    setmin(mi, sum - i);
    sum += A[i];
  }
  dec(i, bb) {
    sum += B[i];
    setmin(mi, sum - i);
  }
  puts(mi == 0 && sum == 0 ? "Yes" : "No");
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;

char S[1000005];

vector<ii> A;
vector<ii> B;

bool cmp1(ii x, ii y) {
  return ii(-x.second, x.first) < ii(-y.second, y.first);
}

bool cmp2(ii x, ii y) {
  return ii(x.second, -x.first) < ii(y.second, -y.first);
}

int main() {
  int N;
  scanf("%d", &N);

  int total_sum = 0;
  for (int i = 0; i < N; i++) {
    scanf(" %s", S);
    int len = strlen(S);

    int x = 0; // final sum
    int y = 0; // minimum sum encountered

    for (int j = 0; j < len; j++) {
      if (S[j] == '(') {
        x++;
      } else {
        x--;
      }
      y = min(y, x);
    }

    if (x >= 0) {
      A.emplace_back(x, y);
    } else {
      B.emplace_back(-x, y - x);
    }

    total_sum += x;
  }

  if (total_sum != 0) {
    printf("No");
    return 0;
  }

  sort(A.begin(), A.end(), cmp1);
  sort(B.begin(), B.end(), cmp1);

  int sum = 0;
  for (ii temp : A) {
    int x = temp.first;
    int y = temp.second;
    // printf("A: ii(%d, %d)\\n", x, y);
    if (sum + y < 0) {
      printf("No");
      return 0;
    }
    sum += x;
  }

  sum = 0;
  for (ii temp : B) {
    int x = temp.first;
    int y = temp.second;
    // printf("B: ii(%d, %d)\\n", x, y);
    if (sum + y < 0) {
      printf("No");
      // throw;
      return 0;
    }
    sum += x;
  }

  printf("Yes");
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;

char S[1000005];

vector<ii> A;
vector<ii> B;

bool cmp1(ii x, ii y) {
  return ii(-x.second, x.first) < ii(-y.second, y.first);
}

bool cmp2(ii x, ii y) { return ii(x.second, x.first) < ii(y.second, y.first); }

int main() {
  int N;
  scanf("%d", &N);

  int total_sum = 0;
  for (int i = 0; i < N; i++) {
    scanf(" %s", S);
    int len = strlen(S);

    int x = 0; // final sum
    int y = 0; // minimum sum encountered

    for (int j = 0; j < len; j++) {
      if (S[j] == '(') {
        x++;
      } else {
        x--;
      }
      y = min(y, x);
    }

    if (x >= 0) {
      A.emplace_back(x, y);
    } else {
      B.emplace_back(x, y);
    }

    total_sum += x;
  }

  if (total_sum != 0) {
    printf("No");
    return 0;
  }

  sort(A.begin(), A.end(), cmp1);
  sort(B.begin(), B.end(), cmp2);

  int sum = 0;
  for (ii temp : A) {
    int x = temp.first;
    int y = temp.second;
    // printf("A: ii(%d, %d)\\n", x, y);
    if (sum + y < 0) {
      printf("No");
      return 0;
    }
    sum += x;
  }

  for (ii temp : B) {
    int x = temp.first;
    int y = temp.second;
    // printf("B: ii(%d, %d)\\n", x, y);
    if (sum + y < 0) {
      printf("No");
      throw;
      return 0;
    }
    sum += x;
  }

  if (sum == 0) {
    printf("Yes");
  } else {
    printf("No");
  }
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
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;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;

  // 最下点、最終的な和
  vector<pair<int, int>> p_plus;  // 最終的な和がプラスのもの
  vector<pair<int, int>> p_minus; // 最終的な和がマイナスのもの
  int sum = 0;
  rep(i, n) {
    string s;
    cin >> s;
    pair<int, int> p(0, 0);
    rep(i, s.size()) {
      if (s[i] == '(')
        p.second++;
      else {
        p.second--;
        chmin(p.first, p.second);
      }
    }
    sum += p.second;
    if (p.second >= 0)
      p_plus.push_back(p);
    else
      p_minus.push_back(p);
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  int now_pos = 0;
  sort(p_plus.begin(), p_plus.end(), greater<pair<int, int>>());
  rep(i, p_plus.size()) {
    if (now_pos + p_plus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_plus[i].second;
  }

  now_pos = 0;
  rep(i, p_minus.size()) {
    p_minus[i].second *= -1;
    p_minus[i].first += p_minus[i].second;
  }
  sort(p_minus.begin(), p_minus.end(), greater<pair<int, int>>());
  rep(i, p_minus.size()) {
    if (now_pos + p_minus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_minus[i].second;
  }
  cout << "Yes" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
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;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;

  // 最下点、最終的な和
  vector<pair<int, int>> p_plus;  // 最終的な和がプラスのもの
  vector<pair<int, int>> p_minus; // 最終的な和がマイナスのもの
  int sum = 0;
  rep(i, n) {
    string s;
    cin >> s;
    pair<int, int> p(0, 0);
    rep(i, s.size()) {
      if (s[i] == '(')
        p.second++;
      else {
        p.second--;
        chmin(p.first, p.second);
      }
    }
    sum += p.second;
    if (p.second >= 0)
      p_plus.push_back(p);
    else
      p_minus.push_back(p);
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  int now_pos = 0;
  sort(p_plus.begin(), p_plus.end(), greater<pair<int, int>>());
  rep(i, p_plus.size()) {
    if (now_pos + p_plus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_plus[i].second;
  }

  now_pos = 0;
  rep(i, p_minus.size()) {
    p_minus[i].second *= -1;
    p_minus[i].first += p_minus[i].second;
  }
  sort(p_minus.begin(), p_minus.end(), greater<pair<int, int>>());
  rep(i, p_plus.size()) {
    if (now_pos + p_minus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_minus[i].second;
  }
  cout << "Yes" << endl;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> pref, suff;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s = "";
    char c = getchar();
    while (c != '(' && c != ')')
      c = getchar();
    int fi = 0, se = 0;
    for (; c == '(' || c == ')';) {
      if (c == '(')
        se++;
      else {
        if (se == 0)
          fi++;
        else
          se--;
      }
      //	cout<<c<<" "<<fi<<" "<<se<<endl;
      c = getchar();
    }
    //	cout<<endl;
    if (se > fi)
      pref.push_back({fi, se});
    else
      suff.push_back({se, fi});
  }
  sort(pref.begin(), pref.end());
  sort(suff.begin(), suff.end());
  int tot1 = 0, tot2 = 0;
  for (pair<int, int> i : pref) {
    if (i.first > tot1) {
      cout << "No\\n";
      return 0;
    }
    tot1 += i.second - i.first;
  }
  for (pair<int, int> i : suff) {
    if (i.first > tot2) {
      cout << "No\\n";
      return 0;
    }
    tot2 += i.second - i.first;
  }
  if (tot1 != tot2) {
    cout << "No\\n";
    return 0;
  }
  cout << "Yes\\n";
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> pref, suff;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    pair<int, int> nw = {0, 0};
    for (int j = 0; j < s.size(); j++) {
      if (s[i] == '(')
        nw.second++;
      else if (nw.second)
        nw.second--;
      else
        nw.first++;
    }
    if (nw.second > nw.first)
      pref.push_back(nw);
    else
      suff.push_back({nw.second, nw.first});
  }
  sort(pref.begin(), pref.end());
  sort(suff.begin(), suff.end());
  int tot1 = 0, tot2 = 0;
  for (pair<int, int> i : pref) {
    if (i.first > tot1) {
      cout << "No\\n";
      return 0;
    }
    tot1 += i.second - i.first;
  }
  for (pair<int, int> i : suff) {
    if (i.first > tot2) {
      cout << "No\\n";
      return 0;
    }
    tot2 += i.second - i.first;
  }
  if (tot1 != tot2) {
    cout << "No\\n";
    return 0;
  }
  cout << "Yes\\n";
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

pair<int, bool> solve(const vector<pair<int, int>> &v) {
  int sh = 0;
  for (auto p : v) {
    int d, h;
    tie(d, h) = p;
    if (d > sh)
      return make_pair(0, false);
    sh += h;
  }
  return make_pair(sh, true);
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> vp;
  vector<pair<int, int>> vn;
  REP(i, n) {
    string s;
    cin >> s;
    int h = 0;
    int d = 0;
    for (char c : s) {
      if (c == '(')
        ++h;
      else
        --h;
      d = min(d, h);
    }
    if (h >= 0)
      vp.emplace_back(-d, h);
    else
      vn.emplace_back(h - d, -h);
  }
  sort(ALL(vp));
  sort(ALL(vn));
  int h1, h2;
  bool v1, v2;
  tie(h1, v1) = solve(vp);
  tie(h2, v2) = solve(vn);
  if (h1 != h2 || !v1 || !v2) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

using pi = pair<int, int>;

pi calc(const string &s) {
  int mn = 0;
  int c = 0;
  rep(i, s.size()) {
    if (s[i] == '(')
      ++c;
    else
      --c;
    mn = min(mn, c);
  }

  return {c, mn};
}

string rev(const string &s) {
  string t = s;
  int n = s.size();
  rep(i, n) t[n - 1 - i] = (s[i] == '(' ? ')' : '(');
  return t;
}

int dep(const vector<pi> &v) {
  multiset<pi> ms;

  int n = v.size();
  rep(i, n) ms.insert({-v[i].fi, v[i].se});

  int ret = 0;
  while (!ms.empty()) {
    auto itr = ms.begin();
    while (itr != ms.end()) {
      if (ret + itr->se >= 0)
        break;
      ++itr;
    }

    if (itr == ms.end())
      return -1;

    ret += -itr->fi;
    ms.erase(itr);
  }

  return ret;
}

bool solve() {
  int n;
  cin >> n;

  vector<pi> l, r;
  rep(i, n) {
    string s;
    cin >> s;

    pi v = calc(s);
    if (v.fi >= 0)
      l.pb(v);
    else
      r.pb(calc(rev(s)));
  }

  int ld = dep(l), rd = dep(r);
  return (ld != -1 && rd != -1 && ld == rd);
}

int main() {
  cout << (solve() ? "Yes" : "No") << endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define DEBUG

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 998244353, maxn = 200200;
const double PI = acos(-1);

bool comp(pii a, pii b) { return a.ss > b.ss; }

int main() {
  int n;
  string s;
  bool can = true;
  cin >> n;

  vector<pii> v1, v2;

  for (int i = 0; i < n; i++) {
    cin >> s;
    int tam = s.size(), a = 0, b = 0, q = 0;

    for (int j = 0; j < tam; j++) {
      if (s[j] == ')') {
        if (q > 0)
          q--;
        else
          a++;
      } else
        ++q;
    }
    b = q;
    if (a < b)
      v1.emplace_back(a, b);
    else
      v2.emplace_back(a, b);
  }

  sort(all(v1));
  sort(all(v2), comp);

  int q = 0;

  for (auto x : v1) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }

  for (auto x : v2) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }
  if (q != 0)
    can = false;

  if (can) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define DEBUG

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 998244353, maxn = 200200;
const double PI = acos(-1);

bool comp(pii a, pii b) { return a.ss > b.ss; }

int main() {
  int n;
  string s;
  bool can = true;
  cin >> n;

  vector<pii> v1, v2;

  for (int i = 0; i < n; i++) {
    cin >> s;
    int tam = s.size(), a = 0, b = 0, q = 0;

    for (int j = 0; j < tam; j++) {
      if (s[i] == ')') {
        if (q > 0)
          q--;
        else
          a++;
      } else
        ++q;
    }
    b = q;
    if (a < b)
      v1.emplace_back(a, b);
    else
      v2.emplace_back(a, b);
  }

  sort(all(v1));
  sort(all(v2), comp);

  int q = 0;

  for (auto x : v1) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }

  for (auto x : v2) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }
  if (q != 0)
    can = false;

  if (can) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
int main() {
  int n = ri();
  std::string s[n];
  for (auto &i : s)
    std::cin >> i;
  auto calc = [&](const std::string &s) -> std::pair<int, int> {
    int n = s.size();
    int res = 0, cur = 0;
    for (auto i : s) {
      if (i == '(')
        cur++;
      else
        cur--;
      res = std::min(res, cur);
    }
    int first = -res;
    res = cur = 0;
    for (int i = n; i--;) {
      if (s[i] == ')')
        cur++;
      else
        cur--;
      res = std::min(res, cur);
    }
    return {first, -res};
  };
  std::vector<std::pair<int, int>> pos, neg;
  for (int i = 0; i < n; i++) {
    auto tmp = calc(s[i]);
    if (tmp.second >= tmp.first)
      pos.push_back(tmp);
    else
      neg.push_back(tmp);
  }
  std::sort(pos.begin(), pos.end());
  std::sort(neg.begin(), neg.end(), [](auto i, auto j) {
    return i.first + (j.first - j.second) > j.first + (i.first - i.second);
  });
  int cur = 0;
  for (auto i : pos) {
    if (i.first > cur) {
      puts("No");
      return 0;
    }
    cur += i.second - i.first;
  }
  for (auto i : neg) {
    if (i.first > cur) {
      puts("No");
      return 0;
    }
    cur += i.second - i.first;
  }
  puts(cur ? "No" : "Yes");

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
int main() {
  int n = ri();
  std::string s[n];
  for (auto &i : s)
    std::cin >> i;
  auto calc = [&](const std::string &s) -> std::pair<int, int> {
    int n = s.size();
    int res = 0, cur = 0;
    for (auto i : s) {
      if (i == '(')
        cur++;
      else
        cur--;
      res = std::min(res, cur);
    }
    int first = -res;
    res = cur = 0;
    for (int i = n; i--;) {
      if (s[i] == ')')
        cur++;
      else
        cur--;
      res = std::min(res, cur);
    }
    return {first, -res};
  };
  std::vector<std::pair<int, int>> pos, neg;
  for (int i = 0; i < n; i++) {
    auto tmp = calc(s[i]);
    if (tmp.second >= tmp.first)
      pos.push_back(tmp);
    else
      neg.push_back(tmp);
  }
  std::sort(pos.begin(), pos.end());
  std::sort(neg.begin(), neg.end(), std::greater<>());
  std::multiset<int> usable;
  int head = 0;
  int cur = 0;
  for (auto i : pos) {
    while (head < (int)pos.size() && pos[head].first <= cur)
      usable.insert(pos[head].second - pos[head].first), head++;
    if (!usable.size()) {
      puts("No");
      return 0;
    }
    cur += *usable.begin();
    usable.erase(usable.begin());
  }
  // assert(!usable.size());
  for (auto i : neg)
    usable.insert(i.second - i.first);
  head = 0;
  for (auto i : neg) {
    while (head < (int)neg.size() && neg[head].first > cur)
      usable.erase(usable.find(neg[head].second - neg[head].first)), head++;
    if (!usable.size()) {
      puts("No");
      return 0;
    }
    cur += *usable.begin();
    usable.erase(usable.begin());
  }
  puts(cur ? "No" : "Yes");

  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 10;

struct X {
  int y, z;
  int id, tp;
} s[maxn];
int n;
char t[maxn];

bool cmp(const X &a, const X &b) {
  if (a.tp != b.tp)
    return a.tp < b.tp;
  if (a.tp == 2) {
    if (a.y != b.y)
      return a.y < b.y;
    return a.z > b.z;
  } else if (a.tp == 3) {
    return a.z > b.z;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", t);
    int z = 0, y = 0;
    for (int j = 0; t[j]; j++) {
      if (t[j] == '(')
        z++;
      else {
        if (z)
          z--;
        else
          y++;
      }
    }
    s[i].y = y;
    s[i].z = z;
    s[i].id = i;
    /* ))) y个 ((( z个 */
    if (y + z == 0)
      s[i].tp = 0;
    else if (y == 0 && z != 0)
      s[i].tp = 1;
    else if (y != 0 && z == 0)
      s[i].tp = 4;
    else if (z - y >= 0)
      s[i].tp = 2;
    else
      s[i].tp = 3;
  }
  sort(s, s + n, cmp);
  int sum = 0;
  int fail = 0;
  for (int i = 0; i < n; i++) {
    sum = sum - s[i].y;
    if (sum < 0) {
      fail = 1;
      break;
    }
    sum = sum + s[i].z;
  }
  if (fail || sum != 0) {
    printf("No\\n");
  } else {
    printf("Yes\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;

struct X {
  int y, z;
  int id, tp;
} s[maxn];
int n;
char t[maxn];

bool cmp(const X &a, const X &b) {
  if (a.tp != b.tp)
    return a.tp < b.tp;
  if (a.tp == 2) {
    if (a.y != b.y)
      return a.y < b.y;
    return a.z > b.z;
  } else if (a.tp == 3) {
    return a.z > b.z;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", t);
    int z = 0, y = 0;
    for (int j = 0; t[j]; j++) {
      if (t[j] == '(')
        z++;
      else {
        if (z)
          z--;
        else
          y++;
      }
    }
    s[i].y = y;
    s[i].z = z;
    s[i].id = i;
    /* ))) y个 ((( z个 */
    if (y + z == 0)
      s[i].tp = 0;
    else if (y == 0 && z != 0)
      s[i].tp = 1;
    else if (y != 0 && z == 0)
      s[i].tp = 4;
    else if (z - y >= 0)
      s[i].tp = 2;
    else
      s[i].tp = 3;
  }
  sort(s, s + n, cmp);
  int sum = 0;
  int fail = 0;
  for (int i = 0; i < n; i++) {
    sum = sum - s[i].y;
    if (sum < 0) {
      fail = 1;
      break;
    }
    sum = sum + s[i].z;
  }
  if (fail || sum != 0) {
    printf("No\\n");
  } else {
    printf("Yes\\n");
  }
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int M = 1e6 + 100;
int l[M], r[M];
int id[M], tp[M];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int x = 0, y = 0;
    for (char c : s) {
      if (c == '(')
        ++x;
      else if (x)
        --x;
      else
        ++y;
    }
    l[i] = x, r[i] = y, id[i] = i;
    if (y == 0)
      tp[i] = 1;
    else if (x == 0)
      tp[i] = 4;
    else if (x >= y)
      tp[i] = 2;
    else
      tp[i] = 3;
  }
  sort(id, id + n, [&](int a, int b) {
    if (tp[a] != tp[b])
      return tp[a] < tp[b];
    if (tp[a] == 2) {
      if (r[a] != r[b])
        return r[a] < r[b];
      else
        return l[a] > l[b];
    }
    if (tp[a] == 3)
      return l[a] > l[b];
    return false;
  });
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum -= r[id[i]];
    if (sum < 0)
      break;
    sum += l[id[i]];
  }
  cout << (sum == 0 ? "Yes" : "No");
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int M = 1e6 + 100;
int l[M], r[M];
int id[M], tp[M];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int x = 0, y = 0;
    for (char c : s) {
      if (c == '(')
        ++x;
      else if (x)
        --x;
      else
        ++y;
    }
    l[i] = x, r[i] = y, id[i] = i;
    if (y == 0)
      tp[i] = 1;
    else if (x == 0)
      tp[i] = 4;
    else if (y >= x)
      tp[i] = 2;
    else
      tp[i] = 3;
  }
  sort(id, id + n, [&](int a, int b) {
    if (tp[a] != tp[b])
      return tp[a] < tp[b];
    if (tp[a] == 2) {
      if (r[a] != r[b])
        return r[a] < r[b];
      else
        return l[a] > l[b];
    }
    if (tp[a] == 3)
      return l[a] > l[b];
    return true;
  });
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum -= r[id[i]];
    if (sum < 0)
      break;
    sum += l[id[i]];
  }
  cout << (sum == 0 ? "Yes" : "No");
}