alexjercan/bugnet huggingface dataset:
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
constexpr int NMAX = 1000010;

char curPar[NMAX];

int signum(int i) { return (0 < i) - (i < 0); }

void answer(bool b) {
  if (b)
    printf("Yes\\n");
  else
    printf("No\\n");
  exit(0);
}

int main() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int>> parBalance(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", curPar);
    int cBal = 0;
    for (int k = 0; curPar[k]; ++k) {
      cBal += (curPar[k] == '(' ? 1 : -1);
      parBalance[i].first = min(parBalance[i].first, cBal);
      //			printf("%d char %d bal %d\\n", i, k, cBal);
    }
    parBalance[i].first *= -1;
    parBalance[i].second = cBal;
    //		printf("pbal %d\\n", parBalance[i].first);
  }
  sort(begin(parBalance), end(parBalance),
       [](pair<int, int> i, pair<int, int> j) {
         //		printf("sorting %d %d %d %d\\n", i.first, i.second, j.first,
         //j.second);
         if (signum(i.second) != signum(j.second))
           return signum(i.second) > signum(j.second);
         else if (signum(i.second) == 1)
           return i.first < j.first;
         else if (signum(i.second) == -1)
           return i.first + i.second > j.first + j.second;
         else
           return i.first < j.first;
       });
  // now we can attempt to construct the string
  int cCoeff = 0;
  for (auto req_d : parBalance) {
    // printf("order %d %d\\n", req_d.first, req_d.second);
    if (cCoeff < req_d.first) {
      answer(false);
    }
    cCoeff += req_d.second;
  }
  if (cCoeff == 0)
    answer(true);
  else
    answer(false);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
constexpr int NMAX = 1000010;

char curPar[NMAX];

int signum(int i) { return (0 < i) - (i < 0); }

void answer(bool b) {
  if (b)
    printf("Yes\\n");
  else
    printf("No\\n");
  exit(0);
}

int main() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int>> parBalance(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", curPar);
    int cBal = 0;
    for (int k = 0; curPar[k]; ++k) {
      cBal += (curPar[k] == '(' ? 1 : -1);
      parBalance[i].first = min(parBalance[i].first, cBal);
      //			printf("%d char %d bal %d\\n", i, k, cBal);
    }
    parBalance[i].first *= -1;
    parBalance[i].second = cBal;
    //		printf("pbal %d\\n", parBalance[i].first);
  }
  sort(begin(parBalance), end(parBalance),
       [](pair<int, int> i, pair<int, int> j) {
         if (signum(i.second) != signum(j.second))
           return signum(i.second) > signum(j.second);
         else if (signum(i.second) == 1)
           return i.first < j.first;
         else if (signum(i.second) == -1)
           return i.first + i.second > j.first + j.second;
         else
           return true;
       });
  // now we can attempt to construct the string
  int cCoeff = 0;
  for (auto req_d : parBalance) {
    //		printf("order %d %d\\n", req_d.first, req_d.second);
    if (cCoeff < req_d.first) {
      answer(false);
    }
    cCoeff += req_d.second;
  }
  if (cCoeff == 0)
    answer(true);
  else
    answer(false);
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

bool check(vector<pair<ll, ll>> s) {
  ll h = 0;
  for (auto p : s) {
    ll b = h + p.first;
    if (b < 0)
      return false;
    h += p.second;
  }
  return true;
}

int main() {
  ll N, sum = 0;
  cin >> N;
  vector<pair<ll, ll>> up, down;
  rep(i, N) {
    string s;
    cin >> s;
    int n = s.size();
    int mini = 0;
    int diff = 0;
    for (int j = 0; j < n; j++) {
      if (s[j] == '(') {
        diff++;
      } else {
        diff--;
      }
      mini = min(mini, diff);
    }
    if (diff > 0) {
      up.emplace_back(mini, diff);
    } else {
      down.emplace_back(mini - diff, -diff);
    }
    sum += diff;
  }
  sort(up.rbegin(), up.rend());
  sort(down.rbegin(), down.rend());
  puts(check(up) && check(down) && sum == 0 ? "Yes" : "No");
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N;
  cin >> N;
  vector<vector<int>> score(N, vector<int>(2, 0));
  unordered_map<int, vector<int>> adj;
  multiset<pair<int, int>> counter;
  rep(i, N) {
    string s;
    cin >> s;
    int n = s.size();
    bool flag = true;
    for (int j = 0; j < n; j++) {
      if (flag && s[j] == ')') {
        score[i][0]--;
        continue;
      }
      if (s[j] == '(') {
        flag = false;
        score[i][1]++;
      }
      if (s[j] == ')') {
        score[i][1]--;
      }
    }
    adj[-score[i][0]].push_back(score[i][1]);
    counter.insert({-score[i][0], score[i][1]});
  }
  // for(auto c : score){
  //     cout << c[0] << " " << c[1] << endl;
  // }
  for (int start : adj[0]) {
    counter.erase({0, start});
    queue<int> q;
    int prevEnd = start;
    q.push(start);
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      prevEnd = adj[cur][adj[cur].size() - 1];
      if (adj[cur].size() == 0 || !counter.count({cur, prevEnd})) {
        cout << "No" << endl;
        return 0;
      }
      adj[cur].pop_back();
      q.push(prevEnd);
      counter.erase({cur, prevEnd});
      if (prevEnd == 0)
        break;
    }
    if (prevEnd != 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  if (counter.size() > 0) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
}
Problem p02686 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct Comp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
    if (a.first - a.second <= 0 && b.first - b.second <= 0) {
      return a.first < b.first;
    } else if (a.first - a.second > 0 && b.first - b.second > 0) {
      return a.second > b.second;
    }
    return a.first - a.second <= 0;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int left = 0, right = 0;
  vector<pair<int, int>> mid;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    int a = 0;
    int c = 0;
    for (int j = 0; j < t.size(); j++) {
      if (t[j] == '(') {
        c++;
      } else {
        c--;
        if (c < 0) {
          a++;
          c = 0;
        }
      }
    }
    if (a == 0) {
      left += c;
    } else if (c == 0) {
      right += a;
    } else {
      mid.push_back({a, c});
    }
  }

  sort(mid.begin(), mid.end(), Comp());

  int cur = left;
  for (int i = 0; i < mid.size(); i++) {
    cur -= mid[i].first;
    if (cur < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += mid[i].second;
  }

  if (cur != right) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct Comp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second - a.first == b.second - b.first) {
      return a.first <= b.first;
    }
    return a.second - a.first > b.second - b.first;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int left = 0, right = 0;
  vector<pair<int, int>> mid;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    int a = 0;
    int c = 0;
    for (int j = 0; j < t.size(); j++) {
      if (t[j] == '(') {
        c++;
      } else {
        c--;
        if (c < 0) {
          a++;
          c = 0;
        }
      }
    }
    if (a == 0) {
      left += c;
    } else if (c == 0) {
      right += a;
    } else {
      mid.emplace_back(a, c);
    }
  }

  sort(mid.begin(), mid.end(), Comp());
  int cur = left;
  for (int i = 0; i < mid.size(); i++) {
    cur -= mid[i].first;
    if (cur < 0) {
      cout << "No" << endl;
      return 0;
    }
    cur += mid[i].second;
  }
  if (cur != right) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

bool comp1(pl a, pl b) {
  if (a.second == b.second)
    return a.first > b.first;
  return a.second < b.second;
}

bool comp2(pl a, pl b) { return a.first > b.first; }

int main() {

  int n;
  cin >> n;

  vc<vc<pl>> lr(2);

  rep(i, n) {
    string t;
    cin >> t;
    int l = 0, r = 0;
    rep(j, t.length()) {
      if (t[j] == '(')
        l++;
      else {
        if (l)
          l--;
        else
          r++;
      }
    }
    if (l - r > 0)
      lr[0].emplace_back(l, r);
    else
      lr[1].emplace_back(l, r);
  }

  sort(all(lr[0]), comp1);
  sort(all(lr[1]), comp2);
  // rep(i,2)rep(j,lr[i].size())cerr<<"( = "<<lr[i][j].first<<"  ) =
  // "<<lr[i][j].second<<"\\n";
  ll ans = 0;

  rep(i, 2) {
    rep(j, lr[i].size()) {
      if (ans - lr[i][j].second < 0) {
        cout << "No\\n";
        return 0;
      }
      ans += lr[i][j].first - lr[i][j].second;
    }
  }

  if (ans == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

bool comp(pl a, pl b) {
  if (a.second == 0 || b.second == 0)
    return !a.second;
  if (a.second == b.second)
    return a.first > b.first;
  return a.second < b.second;
}

int main() {

  int n;
  cin >> n;

  vc<pl> lr;

  rep(i, n) {
    string t;
    cin >> t;
    int l = 0, r = 0;
    rep(j, t.length()) {
      if (t[j] == '(')
        l++;
      else {
        if (l)
          l--;
        else
          r++;
      }
    }
    lr.emplace_back(l, r);
  }

  sort(all(lr), comp);
  // rep(i,n)cerr<<"( = "<<lr[i].first<<"  ) = "<<lr[i].second<<"\\n";
  ll ans = 0;

  rep(i, n) {
    if (ans - lr[i].second < 0) {
      cout << "No\\n";
      return 0;
    }
    ans += lr[i].first - lr[i].second;
  }

  if (ans == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";

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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 1e6 + 10;

int n, left, right, s0, s1;
struct node {
  int x, y;
  bool operator<(node b) const { return x - y > b.x - b.y; }
} a[N];

char s[N];

int main() {
  scanf("%d", &n);
  for (int i = 1, l, r; i <= n; i++) {
    scanf("%s", s + 1);
    l = r = 0;
    for (int j = 1; s[j]; j++)
      if (s[j] == '(')
        l++;
      else {
        if (l)
          l--;
        else
          r++;
      }
    if (!l && r)
      right += r;
    else if (!r && l)
      left += l;
    else
      a[i] = (node){l, r};
    s0 += l;
    s1 += r;
  }
  if (s0 ^ s1)
    puts("No"), exit(0);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    if (left < a[i].y)
      puts("No"), exit(0);
    left -= a[i].y - a[i].x;
  }
  if (left ^ right)
    puts("No");
  else
    puts("Yes");
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 1e6 + 10;

int n, left, right, s0, s1;
struct node {
  int x, y;
  bool operator<(node b) const { return x - y >= b.x - b.y; }
} a[N];

char s[N];

int main() {
  scanf("%d", &n);
  for (int i = 1, l, r; i <= n; i++) {
    scanf("%s", s + 1);
    l = r = 0;
    for (int j = 1; s[j]; j++)
      if (s[j] == '(')
        l++;
      else {
        if (l)
          l--;
        else
          r++;
      }
    if (!l)
      right += r;
    else if (!r)
      left += l;
    else
      a[i] = (node){l, r};
    s0 += l;
    s1 += r;
  }
  if (right * left == 0 || s0 ^ s1)
    puts("No"), exit(0);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    if (left < a[i].y)
      puts("No"), exit(0);
    left -= a[i].y - a[i].x;
  }
  if (left ^ right)
    puts("No");
  else
    puts("Yes");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

struct num {
  int a;
  int b;
  bool operator<(const num &right) const {
    if (b - a >= 0) {
      if (right.b - right.a < 0)
        return true;
      if (a == right.a)
        return b > right.b;
      return a < right.a;
    }
    if (right.b - right.a >= 0)
      return false;
    return b > right.b;
  }
};

int main(void) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  // A, B
  vector<num> numV(N);
  for (int i = 0; i < N; i++) {
    string s = S[i];
    int a = 0, b = 0;
    for (unsigned int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        b++;
      else {
        if (b == 0)
          a++;
        else
          b--;
      }
    }
    num n = {a, b};
    numV[i] = n;
  }
  sort(numV.begin(), numV.end());
  //  check
  ll total_b = 0;
  for (int i = 0; i < N; i++) {
    total_b -= ll(numV[i].a);
    if (total_b < 0) {
      cout << "No" << endl;
      return 0;
    }
    total_b += ll(numV[i].b);
  }
  if (total_b == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

struct num {
  int a;
  int b;
  bool operator<(const num &right) const {
    if (b - a >= 0) {
      if (right.b - right.a <= 0)
        return true;
      if (a == right.a)
        return b > right.b;
      return a < right.a;
    }
    if (right.b - right.a >= 0)
      return false;
    return b > right.b;
  }
};

int main(void) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  // A, B
  vector<num> numV(N);
  for (int i = 0; i < N; i++) {
    string s = S[i];
    int a = 0, b = 0;
    for (unsigned int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        b++;
      else {
        if (b == 0)
          a++;
        else
          b--;
      }
    }
    num n = {a, b};
    numV[i] = n;
  }
  sort(numV.begin(), numV.end());
  //  check
  ll total_b = 0;
  for (int i = 0; i < N; i++) {
    total_b -= ll(numV[i].a);
    if (total_b < 0) {
      cout << "No" << endl;
      return 0;
    }
    total_b += ll(numV[i].b);
  }
  if (total_b == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> P(0);
  vector<pair<int, int>> M(0);
  int l, r, pp, pm, pos, n, b, h;
  string s;
  bool flgL = true, flgR = true;
  for (int i = 0; i < N; i++) {
    cin >> s;
    b = 0, h = 0;
    n = s.size();
    for (int j = 0; j < n; j++) {
      if (s[j] == '(') {
        h++;
      } else {
        h--;
        if (b > h) {
          b = h;
        }
      }
    }
    if (h < 0) {
      M.push_back(make_pair(b - h, -h));
    } else if (h == 0) {
      if (b < 0) {
        M.push_back(make_pair(b - h, -h));
      } else {
        P.push_back(make_pair(b, h));
      }
    } else {
      P.push_back(make_pair(b, h));
    }
  }
  sort(P.rbegin(), P.rend());
  sort(M.rbegin(), M.rend());
  l = P.size(), r = M.size();
  pp = 0, pm = 0;
  for (int i = 0; i < l; i++) {
    if (P[i].first + pp < 0) {
      flgL = false;
      break;
    } else {
      pp += P[i].second;
    }
  }
  for (int i = 0; i < r; i++) {
    if (M[i].first + pm < 0) {
      flgR = false;
      break;
    } else {
      pm += M[i].second;
    }
  }
  if (flgL && flgR && pp == pm) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> P(0);
  vector<pair<int, int>> M(0);
  int l, r, pp, pm, pos, n, b, h;
  string s;
  bool flgL = true, flgR = true;
  for (int i = 0; i < N; i++) {
    cin >> s;
    b = 0, h = 0;
    n = s.size();
    for (int j = 0; j < n; j++) {
      if (s[i] == '(') {
        h++;
      } else {
        h--;
        if (b > h) {
          b = h;
        }
      }
    }
    if (h < 0) {
      M.push_back(make_pair(b - h, -h));
    } else if (h == 0) {
      if (b < 0) {
        M.push_back(make_pair(b - h, -h));
      } else {
        P.push_back(make_pair(b, h));
      }
    } else {
      P.push_back(make_pair(b, h));
    }
  }
  sort(P.rbegin(), P.rend());
  sort(M.rbegin(), M.rend());
  l = P.size(), r = M.size();
  pp = 0, pm = 0;
  for (int i = 0; i < l; i++) {
    if (P[i].first + pp < 0) {
      flgL = false;
      break;
    } else {
      pp += P[i].second;
    }
  }
  for (int i = 0; i < r; i++) {
    if (M[i].first + pm < 0) {
      flgR = false;
      break;
    } else {
      pm += M[i].second;
    }
  }
  if (flgL && flgR && pp == pm) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
int n, ac, bc;
struct str {
  int m;
  string st;
} a[N], b[N];
bool cmp(const str &a, const str &b) { return a.m > b.m; }
string last = "";
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    int sum = 0, mx = -1e9, mn = 1e9;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        sum++;
      else
        sum--;
      mn = min(mn, sum);
    }
    sum = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (s[j] == '(')
        sum++;
      else
        sum--;
      mx = max(mx, sum);
    }
    if (sum >= 0)
      a[++ac] = (str){mn, s};
    else
      b[++bc] = (str){mx, s};
  }
  sort(a + 1, a + ac + 1, cmp), sort(b + 1, b + bc + 1, cmp);
  for (int i = 1; i <= ac; i++)
    last += a[i].st;
  for (int i = 1; i <= bc; i++)
    last += b[i].st;
  int sum = 0;
  for (int i = 0; i < last.size(); i++) {
    if (last[i] == '(')
      sum++;
    else
      sum--;
    if (sum < 0)
      return puts("No"), 0;
  }
  if (sum == 0)
    puts("Yes");
  else
    puts("No");
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int n, ac, bc;
struct str {
  int m;
  string st;
} a[N], b[N];
bool cmp(const str &a, const str &b) { return a.m > b.m; }
string last = "";
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    int sum = 0, mx = -1e9, mn = 1e9;
    for (int j = 0; j <= s.size(); j++) {
      if (s[j] == '(')
        sum++;
      else
        sum--;
      mx = max(mx, sum), mn = min(mn, sum);
    }
    if (sum >= 0)
      a[++ac] = (str){mn, s};
    else
      b[++bc] = (str){mx, s};
  }
  sort(a + 1, a + ac + 1, cmp), sort(b + 1, b + bc + 1, cmp);
  for (int i = 1; i <= ac; i++)
    last += a[i].st;
  for (int i = 1; i <= bc; i++)
    last += b[i].st;
  int sum = 0;
  for (int i = 0; i < last.size(); i++) {
    if (last[i] == '(')
      sum++;
    else
      sum--;
    if (sum < 0)
      return puts("No"), 0;
  }
  if (sum == 0)
    puts("Yes");
  else
    puts("No");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

bool par(vector<int> &a, vector<int> &b) {
  return (a[1] - a[0]) > (b[1] - b[0]);
}

bool cmpa(vector<int> &a, vector<int> &b) {
  if (a[0] == b[0])
    return (a[1] - a[0]) > (b[1] - b[0]);
  return a[0] < b[0];
}

bool cmpb(vector<int> &a, vector<int> &b) {
  if (a[1] == b[1])
    return (a[1] - a[0]) > (b[1] - b[0]);
  return a[1] > b[1];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string no = "No\\n";
  string yes = "Yes\\n";
  vector<vector<int>> a(n, vector<int>(2));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    // bal for minimum needed open bracket also open bracket that will be taken
    int bal = 0, add = 0;
    for (int j = 0; j < (int)s.length(); j++) {
      if (s[j] == '(')
        bal++, add++;
      else
        bal--, add--;
      if (add < 0)
        add = 0;
      a[i][0] = max(a[i][0], -bal);
    }
    a[i][1] = add;
  }
  sort(a.begin(), a.end(), par);
  int mid = 0;
  while (mid < n && a[mid][1] - a[mid][0] >= 0)
    mid++;
  sort(a.begin(), a.begin() + mid, cmpa);
  sort(a.begin() + mid, a.end(), cmpb);
  //	for (int i=0; i<n; i++) cout << a[i][0] << ' ' << a[i][1] << '\\n';
  int bal = 0;
  for (int i = 0; i < n; i++) {
    if (bal < a[i][0]) {
      cout << no;
      return 0;
    }
    bal -= a[i][0];
    bal += a[i][1];
    if (bal < 0) {
      cout << no;
      return 0;
    }
  }
  if (bal)
    cout << no;
  else
    cout << yes;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

bool par(vector<int> &a, vector<int> &b) {
  return (a[1] - a[0]) > (b[1] > b[0]);
}

bool cmpa(vector<int> &a, vector<int> &b) {
  if (a[0] == b[0])
    return a[1] > b[1];
  return a[0] < b[0];
}

bool cmpb(vector<int> &a, vector<int> &b) {
  if (a[0] == b[0])
    return a[1] > b[1];
  return a[0] > b[0];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string no = "No\\n";
  string yes = "Yes\\n";
  vector<vector<int>> a(n, vector<int>(2));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    // bal for minimum needed open bracket also open bracket that will be taken
    int bal = 0, add = 0;
    for (int j = 0; j < (int)s.length(); j++) {
      if (s[j] == '(')
        bal++, add++;
      else
        bal--, add--;
      if (add < 0)
        add = 0;
      a[i][0] = max(a[i][0], -bal);
    }
    a[i][1] = add;
  }
  sort(a.begin(), a.end(), par);
  int mid = 0;
  while (mid < n && a[mid][1] - a[mid][0] >= 0)
    mid++;
  sort(a.begin(), a.begin() + mid, cmpa);
  sort(a.begin() + mid, a.end(), cmpb);
  //	for (int i=0; i<n; i++) cout << a[i][0] << ' ' << a[i][1] << '\\n';
  int bal = 0;
  for (int i = 0; i < n; i++) {
    if (bal < a[i][0]) {
      cout << no;
      return 0;
    }
    bal -= a[i][0];
    bal += a[i][1];
    if (bal < 0) {
      cout << no;
      return 0;
    }
  }
  if (bal)
    cout << no;
  else
    cout << yes;
}