alexjercan/bugnet huggingface dataset:
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;

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  vector<pair<int, int>> a(n);
  rep(i, n) {
    cin >> s[i];
    int cnt = 0, mn = 0;
    for (char c : s[i]) {
      if (c == '(')
        cnt++;
      else
        cnt--, mn = min(mn, cnt);
    }
    a[i] = {cnt, min(0, cnt) - mn};
  }

  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);

  sort(ord.begin(), ord.end(), [&](int i, int j) {
    int ti = (a[i].first >= 0 ? 0 : 1);
    int tj = (a[j].first >= 0 ? 0 : 1);
    if (ti != tj)
      return ti < tj;
    if (ti == 0)
      return a[i].second < a[j].second;
    else
      return a[i].second > a[j].second;
  });

  int cnt = 0;
  for (int i : ord) {
    for (char c : s[i]) {
      if (c == '(')
        cnt++;
      else
        cnt--;
      if (cnt < 0)
        return puts("No"), 0;
    }
  }
  puts(cnt == 0 ? "Yes" : "No");

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  vector<pair<int, int>> a(n);
  rep(i, n) {
    cin >> s[i];
    int cnt = 0, mn = 0;
    for (char c : s[i]) {
      if (c == '(')
        cnt++;
      else
        cnt--, mn = min(mn, cnt);
    }
    a[i] = {cnt, min(0, cnt) - mn};
  }

  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);

  sort(ord.begin(), ord.end(), [&](int i, int j) {
    int ti = (a[i].first >= 0 ? 0 : 1);
    int tj = (a[i].first >= 0 ? 0 : 1);
    if (ti != tj)
      return ti < tj;
    if (ti == 0)
      return a[i].second < a[j].second;
    else
      return a[i].second > a[j].second;
  });

  int cnt = 0;
  for (int i : ord) {
    for (char c : s[i]) {
      if (c == '(')
        cnt++;
      else
        cnt--;
      if (cnt < 0)
        return puts("N0"), 0;
    }
  }
  puts(cnt == 0 ? "Yes" : "No");

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

#include <bits/stdc++.h>

using namespace std;

long long f(priority_queue<pair<int, int>> &pq) {
  long long sum = 0;
  while (!pq.empty() && sum + pq.top().first >= 0) {
    sum += pq.top().second;
    pq.pop();
  }

  if (pq.empty())
    return sum;
  else
    return -1;
}

int main() {
  int N;
  cin >> N;

  priority_queue<pair<int, int>> pqa, pqd;
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    int b = 0, c = 0;
    for (char ch : S) {
      if (ch == '(')
        ++c;
      else
        --c;
      b = min(c, b);
    }

    if (c >= 0)
      pqa.push(make_pair(b, c));
    else
      pqd.push(make_pair(b - c, -c));
  }

  long long suma = f(pqa), sumd = f(pqd);
  if (suma >= 0 && suma == sumd)
    puts("Yes");
  else
    puts("No");

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<pair<int, int>> c(N);
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    c[i].first = 0;
    c[i].second = 0;
    for (int j = 0; j < (int)S.size(); j++) {
      if (S[j] == '(')
        ++c[i].second;
      else
        --c[i].second;
      c[i].first = min(c[i].second, c[i].first);
    }
  }
  sort(c.begin(), c.end(), greater<pair<int, int>>());

  int sum = 0;
  bool able = true;
  for (int i = 0; i < N; i++) {
    if (sum + c[i].first < 0) {
      able = false;
      break;
    }
    sum += c[i].second;
  }

  if (able && sum == 0)
    puts("Yes");
  else
    exit(1);

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

#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1000005;
int n, a[maxn], b[maxn], id[maxn];
char s[maxn];
bool cmp(int x, int y) {
  if (b[x] >= 0 && b[y] >= 0)
    return a[x] < a[y];
  if (b[x] >= 0 && b[y] < 0)
    return 1;
  if (b[x] < 0 && b[y] >= 0)
    return 0;
  if (b[x] < 0 && b[y] < 0)
    return a[x] + b[x] > a[y] + b[y];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; s[j]; j++)
      b[i] += s[j] == '(' ? 1 : -1, a[i] = max(a[i], -b[i]);
    b[0] += b[i];
  }
  if (b[0]) {
    puts("No");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    id[i] = i;
  sort(id + 1, id + 1 + n, cmp);
  for (int _i = 1, i = id[_i], sum = 0; _i <= n && b[i] >= 0; i = id[++_i]) {
    if (sum < a[i]) {
      puts("No");
      return 0;
    }
    sum += b[i];
  }
  for (int _i = n, i = id[_i], sum = 0; _i && b[i] < 0; i = id[--_i]) {
    sum -= b[i];
    if (sum < a[i]) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Error: 255

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1000005;
int n, a[maxn], b[maxn], id[maxn];
char s[maxn];
bool cmp(int x, int y) {
  if (b[x] >= 0 && b[y] >= 0)
    return a[x] < a[y];
  if (b[x] >= 0 && b[y] < 0)
    return 1;
  if (b[x] < 0 && b[y] >= 0)
    return 0;
  if (b[x] < 0 && b[y] < 0)
    return a[x] + b[x] > a[y] + b[y];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; s[j]; j++)
      b[i] += s[j] == '(' ? 1 : -1, a[i] = max(a[i], -b[i]);
    b[0] += b[i];
  }
  if (b[0]) {
    puts("No");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    id[i] = i;
  sort(id + 1, id + 1 + n, cmp);
  for (int i = 1, sum = 0; i <= n && b[i] >= 0; i++) {
    if (sum < a[i]) {
      puts("No");
      return 0;
    }
    sum += b[i];
  }
  for (int i = n, sum = 0; i && b[i] < 0; i--) {
    sum -= b[i];
    if (sum < a[i]) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return -1;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

// 解説動画
#include <bits/stdc++.h>
using namespace std;

bool check(vector<pair<int, int>> &A) {
  int cur = 0;
  for (auto &p : A) {
    if (cur + p.first < 0) {
      return false;
    }
    cur += p.second;
  }
  return true;
}

int main() {
  int N;
  cin >> N;
  int Sum = 0;
  vector<pair<int, int>> Left, Right;
  vector<pair<int, int>> A(N);
  {
    string S;
    for (int i = 0; i < N; ++i) {
      cin >> S;
      int Lmin = 0, Last = 0;
      for (auto &ch : S) {
        if (ch == ')')
          --Last;
        else
          ++Last;
        Lmin = min(Lmin, Last);
      }
      if (Last > 0)
        Left.push_back(make_pair(Lmin, Last));
      else
        Right.push_back(make_pair(-Last + Lmin, -Last));
      Sum += Last;
    }
  }

  // auto cmp = [](const pair<int,int> &a, const pair<int,int> &b) -> bool {
  // return ( a.first > b.first ) || (!(a.first > b.first) && a.second >
  // b.second); };
  sort(Left.rbegin(), Left.rend());
  sort(Right.rbegin(), Right.rend());
  /*
  cout << "Left: " << endl;
  for(auto &p : Left) {
      cout << p.first << " " << p.second << endl;
  }
  cout << "Right: " << endl;
  for(auto &p : Right) {
      cout << p.first << " " << p.second << endl;
  }
  */

  // 左側と右側で貪欲に詰めていく。
  if (check(Left) && check(Right) && Sum == 0) {
    cout << "Yes" << endl;
  } else
    cout << "No" << endl;
  return 0;
}

Error: 0

Fixed submission:

// 解説動画
#include <bits/stdc++.h>
using namespace std;

bool check(vector<pair<int, int>> &A) {
  int cur = 0;
  for (auto &p : A) {
    if (cur + p.first < 0) {
      return false;
    }
    cur += p.second;
  }
  return cur >= 0;
}

int main() {
  int N;
  cin >> N;
  int Sum = 0;
  vector<pair<int, int>> Left, Right;
  vector<pair<int, int>> A(N);
  {
    string S;
    for (int i = 0; i < N; ++i) {
      cin >> S;
      int Lmin = 0, Last = 0;
      for (auto &ch : S) {
        if (ch == ')')
          --Last, --Sum;
        else
          ++Last, ++Sum;
        Lmin = min(Lmin, Last);
      }
      if (Last > 0)
        Left.push_back(make_pair(Lmin, Last));
      else
        Right.push_back(make_pair(-Last + Lmin, -Last));
    }
  }

  auto cmp = [](const pair<int, int> &a, const pair<int, int> &b) -> bool {
    return (a.first > b.first) || (!(a.first > b.first) && a.second > b.second);
  };
  sort(Left.begin(), Left.end(), cmp);
  sort(Right.begin(), Right.end(), cmp);
  /*
  cout << "Left: " << endl;
  for(auto &p : Left) {
      cout << p.first << " " << p.second << endl;
  }
  cout << "Right: " << endl;
  for(auto &p : Right) {
      cout << p.first << " " << p.second << endl;
  }
  */

  if (Sum != 0) {
    cout << "No" << endl;
    return 0;
  }

  // 左側と右側で貪欲に詰めていく。
  if (check(Left) && check(Right)) {
    cout << "Yes" << endl;
  } else
    cout << "No" << endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
vector<pair<int, int>> prefix, postfix;

pair<int, int> cntBrace(string &str) {
  int l = 0, r = 0;
  for (auto s : str) {
    if (s == '(')
      r++;
    else
      r--;
    l = min(l, r);
  }
  return {l, r};
}

void setVector(int l, int r) {
  if (r < 0)
    postfix.emplace_back(-r + l, -r);
  else
    prefix.emplace_back(l, r);
}

string getAns() {
  int i = 0, j = 0;
  for (auto o : prefix) {
    if (o.first + i < 0)
      return "No";
    i += o.second;
  }
  for (auto c : postfix) {
    if (c.first + j < 0)
      return "No";
    j += c.second;
  }
  if (i != j)
    return "No";
  return "Yes";
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    int l = 0, r = 0;
    tie(l, r) = cntBrace(str);
    setVector(l, r);
  }
  auto cmp = [](pair<int, int> l, pair<int, int> r) -> bool {
    if (l.first == r.first)
      return l.second > r.second;
    return l.first > r.first;
  };
  sort(prefix.begin(), prefix.end(), cmp);
  sort(postfix.begin(), postfix.end(), cmp);

  cout << getAns() << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n, cnt;
map<int, vector<pair<int, int>>> m;
string str;

void makeMap(string &brace, int index) {
  int MIN = 1e9, cur;
  for (const auto &b : brace) {
    if (b == '(')
      cur++;
    else
      cur--;
    MIN = min(MIN, cur);
  }
  if (MIN >= 0) {
    MIN = 0;
  }

  if (MIN || cur - MIN)
    m[MIN].emplace_back(cur - MIN, index);
}

bool findPair(int n) {
  for (auto f = m.begin(); f != m.end(); ++f) {
    int lhs = f->first;
    int index = f->second.size() - 1;
    for (auto ff = f->second.begin(); ff != f->second.end(); ++ff, index--) {
      int key = ff->first * -1;
      if (key) {
        for (auto p = m[key].begin(); p != m[key].end(); ++p) {
          if (p->second != ff->second) {
            if (lhs || p->first)
              m[lhs].emplace_back(p->first, n);
            m[key].erase(p);
            f->second.erase(f->second.begin() + index);
            return true;
          }
        }
      }
    }
  }
  return false;
}

int mapSize() {
  int res = 0;
  for (auto it : m) {
    res += it.second.size();
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> str;
    makeMap(str, i);
  }
  while (findPair(++n))
    ;
  if (mapSize() == 0)
    cout << "Yes";
  else
    cout << "No";

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

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

const int mn = 1e6 + 7;
string s, a[mn];
int n, cnt, l, r;

struct node {
  int la, ra;
} b[mn];

bool cmp(node a, node b) {
  if (a.la - a.ra != b.la - b.ra)
    return a.la - a.ra > b.la - b.ra;
  else
    return a.la < b.la;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    int top = 0;
    for (int j = 0; j < s.size(); ++j) {
      if (s[j] == '(')
        top++;
      else if (s[j] == ')') {
        if (top)
          top--;
        else
          a[i] += ')';
      }
    }
    while (top)
      a[i] += '(', top--;
  }
  sort(a + 1, a + n + 1);

  int f = 1;
  //	for(int i=1;i<=n;++i) cout<<a[i]<<endl;
  for (int i = 1; i <= n; ++i) {
    if (a[i].size() == 0)
      continue;
    if (a[i][0] == a[i][a[i].size() - 1] && a[i][0] == '(') {
      l += a[i].size();
      continue;
    }
    if (a[i][0] == a[i][a[i].size() - 1] && a[i][0] == ')') {
      r += a[i].size();
      continue;
    }
    int la = 0, ra = 0;
    for (int j = 0; j < a[i].size(); ++j) {
      if (a[i][j] == '(')
        la++;
      else
        ra++;
    }
    b[++cnt] = (node){la, ra};
  }
  sort(b + 1, b + cnt + 1, cmp);
  for (int i = 1; i <= cnt; ++i) {
    if (l >= b[i].ra)
      l += b[i].la - b[i].ra;
    else
      f = 0;
  }
  if (l != r)
    f = 0;
  puts(f ? "Yes" : "No");
}

Error: 0

Fixed submission:

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

const int mn = 1e6 + 7;
string s, a[mn];
int n;

bool cmp(string a, string b) {
  int l = min(a.size(), b.size());
  for (int i = 0; i < l; ++i) {
    if (a[i] != b[i])
      return a[i] < b[i];
  }
  if (a.size() == b.size())
    return 1;
  if (a.size() > l) {
    if (a[a.size() - 2] == '(')
      return 1;
    else
      return 0;
  } else {
    if (b[b.size() - 2] == '(')
      return 0;
    else
      return 1;
  }
  return 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    int top = 0;
    for (int j = 0; j < s.size(); ++j) {
      if (s[j] == '(')
        top++;
      else if (s[j] == ')') {
        if (top)
          top--;
        else
          a[i] += ')';
      }
    }
    while (top)
      a[i] += '(', top--;
  }
  sort(a + 1, a + n + 1, cmp);
  int f = 1, top = 0;
  //	for(int i=1;i<=n;++i) cout<<a[i]<<endl;
  for (int i = 1; i <= n; ++i) {
    if (a[i].size() == 0)
      continue;
    for (int j = 0; j < a[i].size(); ++j) {
      if (a[i][j] == '(')
        top++;
      if (a[i][j] == ')') {
        if (!top)
          f = 0;
        else
          top--;
      }
    }
  }
  if (top)
    f = 0;
  puts(f ? "Yes" : "No");
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ii pair<int, int>
using namespace std;
const int N = 1e6 + 6;

int n;
vector<ii> pos, neg;

bool cmp_pos(ii lhs, ii rhs) { return lhs.second < rhs.second; }
bool cmp_neg(ii lhs, ii rhs) { return lhs.first > rhs.first; }
int main() {
  //    freopen("in.txt", "r", stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int m = s.length();
    int r = 0, l = 0;
    for (int j = 0; j < m; j++) {
      if (s[j] == ')') {
        if (l)
          l--;
        else
          r++;
      } else
        l++;
    }
    if (l - r >= 0)
      pos.push_back({l, r});
    else
      neg.push_back({l, r});
  }
  sort(pos.begin(), pos.end(), cmp_pos);
  sort(neg.begin(), neg.end(), cmp_neg);
  int S = 0;
  for (ii p : pos) {
    if (S - p.second < 0) {
      cout << "No";
      exit(0);
    }
    S += p.first - p.second;
  }
  if (!neg.empty() && neg.back().first > 0) {
    cout << "No";
    exit(0);
  }
  for (ii p : neg) {
    if (S - p.second < 0) {
      cout << "No";
      exit(0);
    }
    S += p.first - p.second;
  }
  if (S != 0)
    cout << "No";
  else
    cout << "Yes";
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ii pair<int, int>
using namespace std;
const int N = 1e6 + 6;

int n;
vector<ii> pos, neg;

bool cmp_pos(ii lhs, ii rhs) { return lhs.second < rhs.second; }
bool cmp_neg(ii lhs, ii rhs) { return lhs.first > rhs.first; }
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int r = 0, l = 0;
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == ')') {
        if (l - r <= 0)
          r++, l = 0;
        else
          l--;
      } else
        l++;
    }
    if (l - r >= 0)
      pos.push_back({l, r});
    else
      neg.push_back({l, r});
  }
  sort(pos.begin(), pos.end(), cmp_pos);
  sort(neg.begin(), neg.end(), cmp_neg);
  int S = 0;
  for (ii p : pos) {
    if (S - p.second < 0) {
      cout << "No";
      exit(0);
    }
    S += p.first - p.second;
  }
  if (neg.back().first > 0) {
    cout << "No";
    exit(0);
  }
  for (ii p : neg) {
    if (S - p.second < 0) {
      cout << "No";
      exit(0);
    }
    S += p.first - p.second;
  }
  if (S != 0)
    cout << "No";
  else
    cout << "Yes";
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pii;

bool compa(Pii a, Pii b) {
  if (a.first < b.first)
    return true;
  if ((a.first == b.first) && (a.second > b.second))
    return true;
  return false;
}

bool compb(Pii a, Pii b) {
  if (a.second == 0 && b.second == 0)
    return a.first > b.first;
  if (a.second == 0)
    return false;
  if (b.second == 0)
    return true;
  int da = a.second - a.first;
  int db = b.second - b.first;
  if (da > db)
    return true;
  if (da == db)
    return a.first > b.first;
  return false;
}

int main() {
  int N;
  cin >> N;
  vector<Pii> bra(N);
  for (int i = 0; i < N; i++) {
    bra[i].first = 0;
    bra[i].second = 0;
    string S;
    cin >> S;
    for (char x : S) {
      if (x == '(') {
        bra[i].second++;
      } else {
        if (bra[i].second > 0) {
          bra[i].second--;
        } else {
          bra[i].first++;
        }
      }
    }
  }

  sort(bra.begin(), bra.end(), compa);
  vector<Pii> amari;
  int k = 0;
  for (int i = 0; i < N; i++) {
    if (bra[i].first - bra[i].second < 0) {
      k -= bra[i].first;
      if (k < 0) {
        cout << "No" << endl;
        return 0;
      }
      k += bra[i].second;
    } else {
      amari.push_back(bra[i]);
    }
  }

  sort(amari.begin(), amari.end(), compb);
  for (auto x : amari) {
    k -= x.first;
    if (k < 0) {
      cout << "No" << endl;
      return 0;
    }
    k += x.second;
  }

  if (k != 0)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<Pii> kakko(N, make_pair(0, 0));
  int sum = 0;
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    int t = S.length();

    for (int j = 0; j < t; j++) {
      if (S[j] == ')') {
        sum--;
        if (kakko[j].second == 0) {
          kakko[j].first++;
        } else {
          kakko[j].second--;
        }
      } else {
        sum++;
        kakko[j].second++;
      }
    }
  }
  int l = 0, r = 0;
  for (int i = 0; i < N; i++) {
    if (kakko[i].first >= 1 && kakko[i].second == 0) {
      r++;
    }
    if (kakko[i].first == 0 && kakko[i].second >= 1) {
      l++;
    }
  }

  if (l >= 1 && r >= 1 && sum == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
int n;
char a[N];
int l[N], r[N], cnt, vis[N], f[N];
char q[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    cin >> a + 1;
    cnt = 0;
    int n = strlen(a + 1);
    for (int j = 1; j <= n; j++) {
      if (a[j] == '(') {
        q[++cnt] = '(';
      } else {
        if (q[cnt] == '(')
          --cnt;
        else
          q[++cnt] = ')';
      }
    }
    for (int j = 1; j <= cnt; j++) {
      if (q[j] == '(')
        l[i]++;
      else
        r[i]++;
    }
  }
  int L = 0, R = 0;
  sort(f + 1, f + 1 + n, [](int x, int y) {
    if (!r[x] && !r[y])
      return (int)(l[x] > l[y]);
    if (!l[x] && !l[y])
      return (int)(r[x] < r[y]);

    if (l[x] >= r[x] && l[y] < r[y])
      return 1;
    if (l[x] < r[x] && l[y] >= r[y])
      return 0;
    if (l[x] >= r[x] && l[y] >= r[y])
      return (int)(r[x] < r[y]);
    if (l[x] <= r[x] && l[y] <= r[y])
      return (int)(l[x] > l[y]);
  });
  for (int i = 1; i <= n; i++) {
    if (L < r[f[i]])
      return cout << "No\\n", 0;
    L -= r[f[i]];
    L += l[f[i]];
  }
  if (!L)
    cout << "Yes\\n";
  else
    cout << "No\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
int n;
char a[N];
int l[N], r[N], cnt, vis[N], f[N];
char q[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    cin >> a + 1;
    cnt = 0;
    int n = strlen(a + 1);
    for (int j = 1; j <= n; j++) {
      if (a[j] == '(') {
        q[++cnt] = '(';
      } else {
        if (q[cnt] == '(')
          --cnt;
        else
          q[++cnt] = ')';
      }
    }
    for (int j = 1; j <= cnt; j++) {
      if (q[j] == '(')
        l[i]++;
      else
        r[i]++;
    }
  }
  int L = 0, R = 0;
  sort(f + 1, f + 1 + n, [](int x, int y) {
    if (!r[x] && !r[y])
      return (int)(l[x] > l[y]);
    if (!l[x] && !l[y])
      return (int)(r[x] < r[y]);

    if (l[x] >= r[x] && l[y] < r[y])
      return 1;
    if (l[x] < r[x] && l[y] >= r[x])
      return 0;
    if (l[x] >= r[x] && l[y] >= r[y])
      return (int)(r[x] < r[y]);
    if (l[x] <= r[x] && l[y] <= r[y])
      return (int)(l[x] > l[y]);
  });
  for (int i = 1; i <= n; i++) {
    if (L < r[f[i]])
      return cout << "No\\n", 0;
    L -= r[f[i]];
    L += l[f[i]];
  }
  if (!L)
    cout << "Yes\\n";
  else
    cout << "No\\n";
  return 0;
}