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

#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second

const int INF = (1 << 30);

int main() {
  int n;
  cin >> n;
  vector<int> max_pref_balance(n), balance(n);
  vector<int> pos, neg;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    int len = static_cast<int>(s.size());
    int op = 0, cl = 0;
    for (int j = 0; j < len; ++j) {
      if (s[j] == '(')
        ++op;
      else
        ++cl;
      max_pref_balance[i] = max(max_pref_balance[i], cl - op);
    }
    if (op >= cl) {
      pos.push_back(i);
      balance[i] = op - cl;
    } else {
      neg.push_back(i);
      balance[i] = cl - op;
    }
  }
  sort(pos.begin(), pos.end(), [&](const int &a, const int &b) {
    return max_pref_balance[a] < max_pref_balance[b];
  });
  int total_balance = 0;
  for (int i = 0; i < (int)pos.size(); i++) {
    int id = pos[i];
    if (max_pref_balance[id] > total_balance) {
      cout << "No\\n";
      return 0;
    }
    total_balance += balance[id];
  }
  sort(neg.begin(), neg.end(), [&](const int &a, const int &b) {
    return max_pref_balance[a] - balance[a] > max_pref_balance[b] - balance[b];
  });

  for (int i = 0; i < (int)neg.size(); i++) {
    int id = neg[i];
    if (max_pref_balance[id] > total_balance) {
      cout << "No\\n";
      return 0;
    }
    total_balance -= balance[id];
  }
  if (total_balance) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  vector<string> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  auto simplify = [](string &s) {
    stack<char> st;
    for (int i = 0; i < (int)s.size(); i++) {
      if (st.empty()) {
        st.push(s[i]);
      } else if (st.top() == '(' && s[i] == ')') {
        st.pop();
      } else {
        st.push(s[i]);
      }
    }
    string ss = "";
    while (!st.empty()) {
      ss.push_back(st.top());
      st.pop();
    }
    reverse(ss.begin(), ss.end());
    return ss;
  };
  string a = "";
  vector<pair<int, int>> vv;
  string c = "";
  for (int i = 0; i < n; i++) {
    string res = simplify(v[i]);
    if (res.size() != 0) {
      if (res[0] == '(' && res.back() == '(') {
        a += res;
      } else if (res[0] == ')' && res.back() == '(') {
        int cl = 0;
        int op = 0;
        for (int j = 0; j < res.size(); j++) {
          cl += (res[j] == ')');
          op = (res[j] == '(');
        }
        vv.push_back({cl, op});
      } else {
        c += res;
      }
    }
  }

  vv.push_back({0, a.size()});
  vv.push_back({c.size(), 0});
  vector<int> deg(n + 1, 0);
  for (auto x : vv) {
    deg[x.first]++;
    deg[x.second]++;
  }
  int ok = true;
  for (int i = 0; i < n + 1; i++) {
    if (deg[i] % 2 == 1) {
      ok = false;
    }
  }
  if (ok) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;

  vector<string> v(N);
  vector<pair<int, int>> w(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];

    int prefix = 0;
    for (int j = 0; j < (int)v[i].size(); ++j) {
      prefix += (v[i][j] == '(' ? 1 : -1);
      w[i].second = min(w[i].second, prefix);
    }
    w[i].first = prefix;
  }

  auto comp = [](pair<int, int> &a, pair<int, int> &b) {
    if (a.first >= 0 && b.first < 0) {
      return true;
    } else if (b.first >= 0 && a.first < 0) {
      return false;
    } else if (a.first >= 0 && b.first >= 0) {
      return a.second > b.second;
    } else {
      return a.first - a.second > b.first - b.second;
    }
  };

  sort(w.begin(), w.end(), comp);

  int prefix = 0;
  for (int i = 0; i < N; ++i) {
    if (prefix + w[i].second < 0) {
      cout << "No\\n";
      return 0;
    }
    prefix += w[i].first;
  }

  if (prefix == 0) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;

  vector<string> v(N);
  vector<pair<int, int>> w(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];

    int prefix = 0;
    for (int j = 0; j < (int)v[i].size(); ++j) {
      prefix += (v[i][j] == '(' ? 1 : -1);
      w[i].second = min(w[i].second, prefix);
    }
    w[i].first = prefix;
  }

  sort(w.begin(), w.end(), [](pair<int, int> a, pair<int, int> b) {
    if (a.first >= 0 && b.first < 0) {
      return true;
    } else if (b.first >= 0 && a.first < 0) {
      return false;
    } else if (a.first >= 0 && b.first >= 0) {
      return a.second >= b.second;
    } else {
      return a.first - a.second >= b.first - b.second;
    }
  });

  int prefix = 0;
  for (int i = 0; i < N; ++i) {
    if (prefix + w[i].second < 0) {
      cout << "No\\n";
      return 0;
    }
    prefix += w[i].first;
  }

  if (prefix == 0) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read() {
  ll a = 0, b = getchar(), c = 1;
  while (!isdigit(b))
    c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b))
    a = a * 10 + b - '0', b = getchar();
  return a * c;
}
const ll mod = 998244353;
ll n, ri, le, s0, f[1000005];
struct node {
  int x, y;
  bool operator<(node b) const { return x - y > b.x - b.y; }
} a[1000005];
char s[1000005];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    ll l = 0, r = 0;
    scanf("%s", s + 1);
    for (int j = 1; s[j]; j++)
      if (s[j] == '(')
        l++;
      else {
        if (l)
          l--;
        else
          r++;
      }
    if (!l and r)
      ri += r;
    else if (l and !r)
      le += l;
    else
      a[i] = (node){l, r};
    s0 += l - r;
  }
  if (s0 != 0)
    return puts("No"), 0;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) {
    if (le < a[i].y)
      return puts("No"), 0;
    le -= a[i].y - a[i].x;
  }
  if (le != ri)
    puts("No");
  else
    puts("Yes");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#define maxn 1000010
#define LL long long
using namespace std;
char s[maxn];
struct node {
  int l, r;
} a[maxn];
int tot;
int cmp(node a, node b) { a.l - a.r > b.l - b.r; }
int main() {
  LL ln, rn;
  int n, i, j, l, r;
  ln = rn = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    l = r = 0;
    scanf("%s", s + 1);
    for (j = 1; s[j]; j++) {
      if (s[j] == '(')
        l++;
      else if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      }
    }
    if (l && !r)
      ln += l;
    else if (!l && r)
      rn += r;
    else
      a[++tot] = (node){l, r};
  }
  sort(a + 1, a + 1 + tot, cmp);
  for (i = 1; i <= tot; i++)
    if (ln < a[i].r)
      return printf("No\\n"), 0;
    else
      ln -= a[i].r, ln += a[i].l;
  if (ln != rn)
    return printf("No\\n"), 0;
  printf("Yes\\n");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
ll read() {
  ll a = 0, b = getchar(), c = 1;
  while (!isdigit(b))
    c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b))
    a = a * 10 + b - '0', b = getchar();
  return a * c;
}
const ll mod = 998244353;
ll n, ri, le, s0, f[1000005];
struct node {
  int x, y;
  bool operator<(node b) const { return x - y > b.x - b.y; }
} a[1000005];
char s[1000005];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    ll l = 0, r = 0;
    scanf("%s", s + 1);
    for (int j = 1; s[j]; j++)
      if (s[j] == '(')
        l++;
      else {
        if (l)
          l--;
        else
          r++;
      }
    if (!l and r)
      ri += r;
    else if (l and !r)
      le += l;
    else
      a[i] = (node){l, r};
    s0 += l - r;
  }
  if (s0 != 0)
    return puts("No"), 0;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) {
    if (le < a[i].y)
      return puts("No"), 0;
    le -= a[i].y - a[i].x;
  }
  if (le != ri)
    puts("No");
  else
    puts("Yes");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#define maxn 1000010
#define LL long long
using namespace std;
char s[maxn];
struct node {
  int l, r;
} a[maxn];
int tot;
int cmp(node a, node b) { a.l - a.r > b.l - b.r; }
int main() {
  LL ln, rn;
  int n, i, j, l, r;
  ln = rn = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    l = r = 0;
    scanf("%s", s + 1);
    for (j = 1; s[j]; j++) {
      if (s[j] == '(')
        l++;
      else if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      }
    }
    if (l && !r)
      ln += l;
    else if (!l && r)
      rn += r;
    else if (l && r)
      a[++tot] = (node){l, r};
  }
  sort(a + 1, a + 1 + tot, cmp);
  for (i = 1; i <= tot; i++)
    if (ln < a[i].r)
      return printf("No\\n"), 0;
    else
      ln -= a[i].r, ln += a[i].l;
  if (ln != rn)
    return printf("No\\n"), 0;
  printf("Yes\\n");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
ll read() {
  ll a = 0, b = getchar(), c = 1;
  while (!isdigit(b))
    c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b))
    a = a * 10 + b - '0', b = getchar();
  return a * c;
}
const ll mod = 998244353;
ll n, ri, le, s0, f[1000005];
struct node {
  int x, y;
  /*bool operator<(node b)const{
          return x-y>b.x-b.y;
  }*/
} a[1000005];
int tot;
char s[1000005];
int cmp(node a, node b) { return a.x - a.y > b.x - b.y; }
int main() {
  // n=read();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    ll l = 0, r = 0;
    scanf("%s", s + 1);
    for (int j = 1; s[j]; j++)
      if (s[j] == '(')
        l++;
      else if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      }
    if (!l && r)
      ri += r;
    else if (l && !r)
      le += l;
    else if (l && r)
      a[++tot] = (node){l, r};
    // s0+=l-r;
  }
  // if(s0!=0)return puts("No"),0;
  sort(a + 1, a + 1 + tot, cmp);
  for (int i = 1; i <= tot; i++) {
    if (le < a[i].y)
      return puts("No"), 0;
    le -= a[i].y - a[i].x;
  }
  if (le != ri)
    puts("No");
  else
    puts("Yes");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#define maxn 1000010
using namespace std;
char s[maxn];
struct node {
  int l, r;
} a[maxn];
int tot = 0;
int cmp(node a, node b) { a.l - a.r > b.l - b.r; }
int main() {
  int n, i, j, l, r, ln, rn;
  ln = rn = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    l = r = 0;
    scanf("%s", s + 1);
    for (j = 1; s[j]; j++) {
      if (s[j] == '(')
        l++;
      else if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      }
    }
    if (l && !r)
      ln += l;
    else if (!l && r)
      rn += r;
    else if (l && r)
      a[++tot] = (node){l, r};
  }
  sort(a + 1, a + 1 + tot, cmp);
  // for(i=1;i<=tot;i++)printf("%d %d\\n",a[i].l,a[i].r);
  for (i = 1; i <= tot; i++)
    if (ln < a[i].r)
      return printf("No\\n") * 0;
    else
      ln -= a[i].r, ln += a[i].l;
  if (ln != rn)
    return printf("No\\n") * 0;
  printf("Yes\\n");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#define maxn 1000010
using namespace std;
char s[maxn];
struct node {
  int l, r;
} a[maxn];
int tot;
int cmp(node a, node b) { return a.l - a.r > b.l - b.r; }
int main() {
  int n, i, j, l, r, ln, rn;
  ln = rn = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    l = r = 0;
    scanf("%s", s + 1);
    for (j = 1; s[j]; j++) {
      if (s[j] == '(')
        l++;
      else if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      }
    }
    if (l && !r)
      ln += l;
    else if (!l && r)
      rn += r;
    else if (l && r)
      a[++tot] = (node){l, r};
  }
  sort(a + 1, a + 1 + tot, cmp);
  for (i = 1; i <= tot; i++) {
    if (ln < a[i].r)
      return printf("No\\n") * 0;
    ln -= a[i].r, ln += a[i].l;
  }
  if (ln != rn)
    return printf("No\\n") * 0;
  printf("Yes\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#define maxn 1000010
using namespace std;
char s[maxn];
struct node {
  int l, r;
} a[maxn];
int tot;
int cmp(node a, node b) { a.l - a.r > b.l - b.r; }
int main() {
  int n, i, j, l, r, ln, rn;
  ln = rn = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    l = r = 0;
    scanf("%s", s + 1);
    for (j = 1; s[j]; j++) {
      if (s[j] == '(')
        l++;
      else if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      }
    }
    if (l && !r)
      ln += l;
    else if (!l && r)
      rn += r;
    else if (l && r)
      a[++tot] = (node){l, r};
  }
  sort(a + 1, a + 1 + tot, cmp);
  for (i = 1; i <= tot; i++) {
    if (ln < a[i].r)
      return printf("No\\n") * 0;
    ln -= a[i].r, ln += a[i].l;
  }
  if (ln != rn)
    return printf("No\\n") * 0;
  printf("Yes\\n");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
#include <string>
#include <utility>
using namespace std;
string s[1000010];
int a[1000010], b[1000100];
multiset<pair<int, int>> s1, s2, s3;
int main() {
  int i, j, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
    int cnt = 0;
    b[i] = 0;
    for (j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '(')
        cnt++;
      else
        cnt--;
      b[i] = min(b[i], cnt);
    }
    a[i] = cnt;
  }
  int sum = 0;
  for (i = 0; i < n; i++) {
    sum += a[i];
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  for (i = 0; i < n; i++) {
    if (a[i] >= 0) {
      s1.insert({-b[i], a[i]});
    }
    if (a[i] == 0) {
      s2.insert({b[i], a[i]});
    }
    if (a[i] < 0) {
      s3.insert({-(b[i] - a[i]), -a[i]});
    }
  }
  sum = 0;
  while (s1.size()) {
    auto it = s1.begin();
    pair<int, int> p = *it;
    if (sum - p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    s1.erase(it);
    sum += p.second;
  }
  while (s2.size()) {
    auto it = s2.begin();
    pair<int, int> p = *it;
    if (sum + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    s2.erase(it);
  }
  sum = 0;
  while (s3.size()) {
    auto it = s3.begin();
    pair<int, int> p = *it;
    if (sum - p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    s3.erase(it);
    sum += p.second;
  }
  cout << "Yes" << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <utility>
using namespace std;
string s[200010];
int a[200010], b[200100];
multiset<pair<int, int>> s1, s2, s3;
int main() {
  int i, j, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
    int cnt = 0;
    b[i] = 0;
    for (j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '(')
        cnt++;
      else
        cnt--;
      b[i] = min(b[i], cnt);
    }
    a[i] = cnt;
  }
  int sum = 0;
  for (i = 0; i < n; i++) {
    sum += a[i];
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  for (i = 0; i < n; i++) {
    if (a[i] >= 0) {
      s1.insert({-b[i], a[i]});
    }
    if (a[i] == 0) {
      s2.insert({b[i], a[i]});
    }
    if (a[i] < 0) {
      s3.insert({-(b[i] - a[i]), -a[i]});
    }
  }
  sum = 0;
  while (s1.size()) {
    auto it = s1.begin();
    pair<int, int> p = *it;
    if (sum - p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    s1.erase(it);
    sum += p.second;
  }
  while (s2.size()) {
    auto it = s2.begin();
    pair<int, int> p = *it;
    if (sum + p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    s2.erase(it);
  }
  sum = 0;
  while (s3.size()) {
    auto it = s3.begin();
    pair<int, int> p = *it;
    if (sum - p.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    s3.erase(it);
    sum += p.second;
  }
  cout << "Yes" << endl;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 233;
pii a[maxn], b[maxn];
string s;
void shit() {
  puts("No");
  exit(0);
}
bool cmp(pii a, pii b) { return a.fi - a.se > b.fi - b.se; }
int main() {
  ios::sync_with_stdio(false);
  int n, ca = 0, cb = 0, sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    a[i].fi = 0;
    a[i].se = 0;
    int cur = 0, mini = 0;
    cin >> s;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '(')
        cur++;
      else
        cur--;
      mini = min(mini, cur);
    }
    //		cout<<cur<<" "<<mini<<endl;
    if (mini >= 0) {
      sum += cur;
    } else {
      if (cur >= 0)
        a[++ca] = mp(-mini, cur);
      else
        b[++cb] = mp(cur, mini);
    }
  }
  sort(a + 1, a + 1 + ca);
  for (int i = 1; i <= ca; i++) {
    if (sum - a[i].fi >= 0) {
      sum += a[i].se;
    } else {
      shit();
    }
  }
  sort(b + 1, b + 1 + cb, cmp);
  for (int i = 1; i <= cb; i++) {
    if (sum + b[i].se < 0) {
      shit();
    }
    sum += b[i].fi;
  }
  if (sum == 0)
    puts("Yes");
  else
    puts("No");
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 233;
pii a[maxn], b[maxn];
string s;
void shit() {
  puts("No");
  exit(0);
}
int main() {
  ios::sync_with_stdio(false);
  int n, ca = 0, cb = 0, sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    a[i].fi = 0;
    a[i].se = 0;
    int cur = 0, mini = 0;
    cin >> s;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '(')
        cur++;
      else
        cur--;
      mini = min(mini, cur);
    }
    //		cout<<cur<<" "<<mini<<endl;
    if (mini >= 0) {
      sum += cur;
    } else {
      if (cur >= 0)
        a[++ca] = mp(mini, cur);
      else
        b[++cb] = mp(-cur, mini);
    }
  }
  sort(a + 1, a + 1 + ca);
  for (int i = 1; i <= ca; i++) {
    if (sum + a[i].fi >= 0) {
      sum += a[i].se;
    } else {
      shit();
    }
  }
  sort(b + 1, b + 1 + cb);
  for (int i = 1; i <= cb; i++) {
    if (sum + b[i].se < 0) {
      shit();
    }
    sum -= b[i].fi;
  }
  if (sum == 0)
    puts("Yes");
  else
    puts("No");
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct na {
  int l, r;
  bool operator<(const na &rr) const { return r < rr.r; }
};
struct nb {
  int l, r;
  bool operator<(const nb &rr) const { return l > rr.l; }
};

string s;
int n;

vector<na> a;
vector<nb> b;

// bool cmp(node x, node y){
//     return x.r < y.r;
// }

// bool cmp1(node x, node y){
//     return x.l > y.l;
// }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    int len = s.length();
    int l = 0, r = 0;
    for (int j = 0; j < len; j++) {
      if (s[j] == '(')
        l++;
      else if (l)
        l--;
      else
        r++;
    }
    if (l >= r)
      a.push_back({l, r});
    else
      b.push_back({l, r});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  int sum = 0, ok = 0;
  for (int i = 0; i < a.size(); i++) {
    sum -= a[i].r;
    if (sum < 0) {
      ok = 1;
      break;
    }
    sum += a[i].l;
  }
  int sum1 = 0;
  for (int i = 0; i < b.size(); i++) {
    sum -= b[i].r;
    if (sum < 0) {
      ok = 1;
      break;
    }
    sum += b[i].l;
  }
  if (ok) {
    cout << "No \\n";
  } else {
    if (sum == 0) {
      cout << "Yes\\n";
    } else {
      cout << "No\\n";
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct na {
  int l, r;
  bool operator<(const na &rr) const { return r < rr.l; }
};
struct nb {
  int l, r;
  bool operator<(const nb &rr) const { return l > rr.r; }
};

string s;
int n;

vector<na> a;
vector<nb> b;

// bool cmp(node x, node y){
//     return x.r < y.r;
// }

// bool cmp1(node x, node y){
//     return x.l > y.l;
// }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    int len = s.length();
    int l = 0, r = 0;
    for (int j = 0; j < len; j++) {
      if (s[j] == '(')
        l++;
      else if (l)
        l--;
      else
        r++;
    }
    if (l >= r)
      a.push_back({l, r});
    else
      b.push_back({l, r});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  int sum = 0, ok = 0;
  for (int i = 0; i < a.size(); i++) {
    sum -= a[i].r;
    if (sum < 0) {
      ok = 1;
      break;
    }
    sum += a[i].l;
  }
  int sum1 = 0;
  for (int i = 0; i < b.size(); i++) {
    sum -= b[i].r;
    if (sum < 0) {
      ok = 1;
      break;
    }
    sum += b[i].l;
  }
  if (ok) {
    cout << "No \\n";
  } else {
    if (sum == 0) {
      cout << "Yes\\n";
    } else {
      cout << "No\\n";
    }
  }
}