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

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

#define pb push_back
#define fi first
#define SZ(x) (int((x).size()))
#define se second
#define ll long long
#define pq priority_queue
#define MP make_pair
#define pii pair<int, int>
#define mod 998244353
#define inf 0x3f3f3f3f
#define debug(x) cerr << #x << "=" << x << '\\n'
#define rep(i, a, b) for (int i = a; i < (b); i++)

const int maxn = 1e6 + 10;

int n;
pii a[maxn];

bool cmp(pii x, pii y) {
  return (x.se > y.se || y.fi < 0) && (y.se - y.fi > x.se - x.fi || x.fi > 0);
}

int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  rep(i, 0, n) {
    string s;
    cin >> s;
    int mn = mod, val = 0;
    rep(j, 0, SZ(s)) {
      if (s[j] == '(')
        val++;
      else
        val--;
      mn = min(val, mn);
    }
    a[i] = MP(val, mn);
  }
  sort(a, a + n, cmp);
  int sum = 0;
  rep(i, 0, n) {
    if (sum + a[i].se < 0) {
      cout << "No";
      return 0;
    }
    sum += a[i].fi;
  }
  if (sum == 0)
    cout << "Yes";
  else
    cout << "No";
  return 0;
}

Error: WA

Fixed submission:

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

#define pb push_back
#define fi first
#define SZ(x) (int((x).size()))
#define se second
#define ll long long
#define pq priority_queue
#define MP make_pair
#define pii pair<int, int>
#define mod 998244353
#define inf 0x3f3f3f3f
#define debug(x) cerr << #x << "=" << x << '\\n'
#define rep(i, a, b) for (int i = a; i < (b); i++)

const int maxn = 1e6 + 10;

int n;
pii a[maxn];

bool cmp(pii x, pii y) {
  return (x.se > y.se || y.fi < 0) && (y.se - y.fi > x.se - x.fi || x.fi > 0);
}

int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  rep(i, 0, n) {
    string s;
    cin >> s;
    int mn = mod, val = 0;
    rep(j, 0, SZ(s)) {
      if (s[i] == '(')
        val++;
      else
        val--;
      mn = min(val, mn);
    }
    a[i] = MP(val, mn);
  }
  sort(a, a + n, cmp);
  int sum = 0;
  rep(i, 0, n) {
    if (sum + a[i].se < 0) {
      cout << "No";
      return 0;
    }
    sum += a[i].fi;
  }
  if (sum == 0)
    cout << "Yes";
  else
    cout << "No";
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

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

using std::pair;
using std::vector;

pair<int, int> get_bracket(const std::string &s) {
  int r_bracket = 0, l_bracket = 0;
  for (const auto &c : s) {
    if (c == '(') {
      ++l_bracket;
    } else {
      if (l_bracket > 0)
        --l_bracket;
      else
        ++r_bracket;
    }
  }
  return std::make_pair(l_bracket, r_bracket);
}

bool solve(vector<pair<int, int>> &ge_0, vector<pair<int, int>> &l_0) {
  std::sort(ge_0.begin(), ge_0.end(),
            [](const auto &x, const auto &y) { return x.second < y.second; });
  std::sort(l_0.begin(), l_0.end(),
            [](const auto &x, const auto &y) { return x.first > y.first; });
  int pre_sum = 0;
  for (const auto &[l, r] : ge_0) {
    pre_sum -= r;
    if (pre_sum < 0)
      return false;
    pre_sum += l;
  }
  for (const auto &[l, r] : l_0) {
    pre_sum -= r;
    if (pre_sum < 0)
      return false;
    pre_sum += l;
  }
  return pre_sum == 0;
}

int main() {
  int n;
  std::cin >> n;
  vector<pair<int, int>> ge_0, l_0;
  for (int i = 0; i < n; ++i) {
    std::string s;
    std::cin >> s;
    auto p = get_bracket(s);
    if (p.first >= p.second)
      ge_0.emplace_back(p);
    else
      l_0.emplace_back(p);
  }
  if (solve(ge_0, l_0))
    std::cout << "Yes" << std::endl;
  else
    std::cout << "No" << std::endl;
  return 0;
}


Error: 0

Fixed submission:

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

std::pair<int, int> get_bracket(const std::string &s) {
  int r_bracket = 0, l_bracket = 0;
  for (const auto &c : s) {
    if (c == '(') {
      ++l_bracket;
    } else {
      if (l_bracket > 0)
        --l_bracket;
      else
        ++r_bracket;
    }
  }
  return std::make_pair(l_bracket, r_bracket);
}

bool solve(std::vector<std::pair<int, int>> &a) {
  std::sort(a.begin(), a.end(), [](const auto &x, const auto &y) {
    if (x.second == 0)
      return true;
    else if (y.second == 0)
      return false;
    else if (x.first - x.second >= 0) {
      if (y.first - y.second >= 0)
        return x.second < y.second;
      else
        return true;
    } else {
      if (y.first - y.second >= 0)
        return false;
      else
        return x.second > y.second;
    }
  });
  int pre_sum = 0;
  for (const auto &[l, r] : a) {
    pre_sum -= r;
    if (pre_sum < 0)
      return false;
    pre_sum += l;
  }
  return pre_sum == 0;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<std::pair<int, int>> a;
  for (int i = 0; i < n; ++i) {
    std::string s;
    std::cin >> s;
    a.emplace_back(get_bracket(s));
  }
  if (solve(a))
    std::cout << "Yes" << std::endl;
  else
    std::cout << "No" << std::endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
char str[1000005];
struct node {
  int l, r;
  bool operator<(node b) const {
    // 原来是从大轮到小。
    // return 0:前者大于后者，前者置前。
    // return 1:后者置前
    // if(l==b.l) return r<b.r;
    // else return l>b.l;
    // return r-l<b.r-b.l;
    if (r - l >= 0) {
      if (b.r - b.l <= 0)
        return 0;
      else
        return b.l < l;
    } else if (r - l <= 0) {
      if (b.r - b.l >= 0)
        return 1;
      else
        return b.r > r;
    }
  }
};
int main() {
  int n;
  scanf("%d", &n);
  getchar();
  priority_queue<node> pq;
  for (int i = 1; i <= n; i++) {
    fgets(str, 1000005, stdin);
    str[strlen(str) - 1] = '\\0';
    int l = 0, r = 0;
    for (int j = 0; j < strlen(str); j++) {
      if (str[j] == '(') {
        r++;
      } else {
        if (r != 0)
          r--;
        else
          l++;
      }
    }
    if (l == 0 && r == 0)
      continue;
    pq.push(node{l, r});
  }
  int l = 0, r = 0;
  if (pq.empty()) {
    printf("Yes\\n");
    return 0;
  }
  node cur = pq.top();
  pq.pop();
  // printf("%d %d\\n",cur.l,cur.r);
  l = cur.l, r = cur.r;
  while (!pq.empty()) {
    cur = pq.top();
    pq.pop();
    // printf("%d %d\\n",cur.l,cur.r);
    int tl = cur.l, tr = cur.r;
    if (r > tl) {
      r -= tl;
      r += tr;
    } else {
      l += (tl - r);
      r = tr;
    }
    if (l != 0)
      break;
  }
  if (r == 0 && l == 0)
    printf("Yes\\n");
  else
    printf("No\\n");
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char str[1000005];
struct node {
  int l, r;
  bool operator<(node b) const {
    if (l == b.l)
      return r < b.r;
    else
      return l > b.l;
  }
};
int main() {
  int n;
  scanf("%d", &n);
  priority_queue<node> pq;
  for (int i = 1; i <= n; i++) {
    scanf("%s", str);
    int l = 0, r = 0;
    for (int j = 0; j < strlen(str); j++) {
      if (str[j] == '(') {
        r++;
      } else {
        if (r != 0)
          r--;
        else
          l++;
      }
    }
    if (l == 0 && r == 0)
      continue;
    pq.push(node{l, r});
  }
  int l = 0, r = 0;
  node cur = pq.top();
  pq.pop();
  l = cur.l, r = cur.r;
  while (!pq.empty()) {
    cur = pq.top();
    pq.pop();
    int tl = cur.l, tr = cur.r;
    if (r > tl) {
      r -= tl;
      r += tr;
    } else {
      l += (tl - r);
      r = tr;
    }
    if (l != 0)
      break;
  }
  if (r == 0 && l == 0)
    printf("Yes\\n");
  else
    printf("No\\n");
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e6 + 10;

int arr[MAX], Plus[MAX], Minus[MAX];
int n;

bool cmp(int &i, int &j) {
  if (Minus[i] == 0 && Minus[j] == 0) {
    return Plus[i] > Plus[j];
  } else if (Minus[i] == 0 || Minus[j] == 0) {
    return Minus[i] < Minus[j];
  } else {
    if (Plus[i] - Minus[i] > 0 && Plus[j] - Minus[j] > 0) {
      return Minus[i] < Minus[j];
    } else if (Plus[i] - Minus[i] > 0 || Plus[j] - Minus[j] > 0) {
      return Plus[i] - Minus[i] > Plus[j] - Minus[j];
    } else {
      if (Plus[i] > 0 && Plus[j] > 0) {
        return Plus[i] - Minus[i] > Plus[j] - Minus[j];
      } else if (Plus[i] > 0 || Plus[j] > 0) {
        return Plus[i] > Plus[j];
      } else {
        Minus[i] < Minus[j];
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  string s;
  vector<int> perm;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int cnt = 0;
    for (auto &c : s) {
      if (c == '(') {
        cnt++;
      } else {
        if (cnt == 0) {
          Minus[i]++;
        }
        cnt--;
        cnt = max(cnt, 0);
      }
    }
    Plus[i] = cnt;
    perm.push_back(i);
  }
  sort(perm.begin(), perm.end(), cmp);
  int cnt = 0;
  for (auto &i : perm) {
    cnt -= Minus[i];
    if (cnt < 0) {
      cout << "No" << endl;
      return 0;
    }
    cnt += Plus[i];
  }
  if (cnt == 0) {
    cout << "Yes" << endl;
    return 0;
  } else {
    cout << "No" << endl;
    return 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int Maxn = 2000000;
string s;
int n;
struct Node {
  int a, b;
  friend bool operator<(Node p, Node q) {
    if (p.a == 0) {
      if (q.a == 0) {
        return p.b > q.b;
      }
      return 1;
    }
    if (q.a == 0) {
      return 0;
    }
    if (p.b >= 0 && q.b >= 0) {
      return p.a > q.a;
    }
    if (p.b >= 0) {
      return 1;
    }
    if (q.b >= 0) {
      return 1;
    }
    return p.b > q.b;
  }
} a[Maxn + 5];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  int tmp, sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    tmp = 0;
    a[i].a = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      if (s[j] == '(') {
        tmp++;
        sum++;
      } else {
        tmp--;
        sum--;
        a[i].a = min(a[i].a, tmp);
      }
    }
    a[i].b = tmp;
  }
  if (sum != 0) {
    puts("No");
    return 0;
  }
  sort(a + 1, a + 1 + n);
  int now = 0;
  for (int i = 1; i <= n; i++) {
    if (now + a[i].a < 0) {
      puts("No");
      return 0;
    }
    now += a[i].b;
  }
  puts("Yes");
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> up, down(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int now = 0, mi = 0;
    for (int j = 0; j < s.size(); j++) {
      s[j] == '(' ? now++ : now--;
      mi = min(mi, now);
    }

    if (now >= 0)
      up.push_back({mi, now - mi});
    else
      down.push_back({mi - now, mi});
  }

  sort(up.begin(), up.end(), greater<>()), sort(down.begin(), down.end());
  bool possible = true;
  int remain = 0;
  for (auto x : up) {
    remain += x.first;
    if (remain < 0)
      possible = false;
    remain += x.second;
  }
  for (auto x : down) {
    remain += x.second;
    if (remain < 0)
      possible = false;
    remain -= x.first;
  }

  cout << (possible && remain == 0 ? "Yes" : "No") << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct pdata {
  int min, res;
};

bool comp1(const pdata &lh, const pdata &rh) { return lh.min < rh.min; }

bool comp2(const pdata &lh, const pdata &rh) {
  if (lh.min != rh.min)
    return lh.min > rh.min;
  return lh.res > rh.res;
}

int main() {
  int n;
  cin >> n;
  vector<pdata> v(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int now = 0;
    int mi = 0;
    for (char &c : s) {
      c == '(' ? now++ : now--;
      mi = min(mi, now);
    }
    v[i] = {mi, now};
    sum += now;
  }

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

  sort(v.begin(), v.end(), comp1);
  int remain = 0;
  while (v.back().min >= 0)
    remain += v.back().res, v.pop_back();

  reverse(v.begin(), v.end());
  vector<pdata> after;
  for (auto &x : v) {
    if (x.res >= 0) {
      if (remain + x.min >= 0)
        remain += x.res;
      else {
        cout << "No" << endl;
        return 0;
      }
    } else {
      after.push_back(x);
    }
  }

  sort(after.begin(), after.end(), comp2);
  for (auto &x : after) {
    if (remain + x.min >= 0)
      remain += x.res;
    else {
      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;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;

int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v, vv;
  ll cnt = 0;
  rep(i, n) {
    string s;
    cin >> s;
    ll te = 0, tt = 0;
    rep(j, s.size()) {
      if (s[j] == ')' && te == 0) {
        tt++;
        cnt--;
      } else if (s[j] == ')') {
        te--;
        cnt--;
      } else {
        te++;
        cnt++;
      }
    }
    if (te >= tt)
      v.emplace_back(tt, te);
    else
      vv.emplace_back(te, tt);
  }
  if (cnt != 0) {
    cout << "No" << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  sort(vv.begin(), vv.end());
  reverse(vv.begin(), vv.end());
  ll nw = 0;
  for (auto r : v) {
    nw -= r.first;
    if (nw < 0) {
      cout << "No" << endl;
      return 0;
    }
    nw += r.second;
  }
  for (auto r : vv) {
    nw -= r.second;
    if (nw < 0) {
      cout << "No" << endl;
      return 0;
    }
    nw += r.first;
  }
  cout << "Yes" << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v(n);
  ll cnt = 0;
  rep(i, n) {
    string s;
    cin >> s;
    ll te = 0;
    rep(j, s.size()) {
      if (s[j] == ')' && te == 0) {
        v[i].first++;
        cnt--;
      } else if (s[j] == ')') {
        te--;
        cnt--;
      } else {
        te++;
        cnt++;
      }
    }
    v[i].second = te;
  }
  if (cnt != 0) {
    cout << "No" << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  auto c = [](pair<ll, ll> l, pair<ll, ll> r) {
    return l.second - l.first < r.second - r.first;
  };
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, decltype(c)> q(c);
  ll t = 0, itr = 0;
  rep(i, n) {
    while (itr < n && v[itr].first <= t) {
      q.push(v[itr]);
      itr++;
    }
    auto r = q.top();
    q.pop();
    t -= r.first;
    if (t < 0) {
      cout << "No" << endl;
      return 0;
    }
    t += r.second;
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  vector<pair<ll, ll>> cnt(n, {0, 0}), a;
  rep(i, n) cin >> s[i];
  ll cnt1 = 0, cnt2 = 0;
  rep(i, n) {
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        cnt[i].first++;
      else {
        if (cnt[i].first)
          cnt[i].first--;
        else
          cnt[i].second++;
      }
    }
  }
  ll now = 0;
  rep(i, n) {
    if (!cnt[i].second)
      now += cnt[i].first;
  }
  rep(i, n) {
    if (cnt[i].first && cnt[i].second) {
      a.push_back({cnt[i].second, cnt[i].first - cnt[i].second});
    }
  }
  sort(all(a));
  ll j = 0, ma = 0;
  priority_queue<pair<ll, ll>> que;
  rep(i, a.size()) {
    while (j < a.size() && a[j].first <= now) {
      que.push({a[j].second, j});
      j++;
    }
    if (que.empty()) {
      cout << "No" << endl;
      return 0;
    }
    if (a[que.top().second].first > now) {
      cout << "No" << endl;
      return 0;
    }
    now += que.top().first;
    que.pop();
  }
  rep(i, n) {
    if (!cnt[i].first)
      now -= cnt[i].second;
  }
  cout << (now == 0 ? "Yes" : "No") << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

// NOの判定miss
int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  vector<pair<ll, ll>> cnt(n, {0, 0}), a;
  rep(i, n) cin >> s[i];
  rep(i, n) {
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        cnt[i].first++;
      else {
        if (cnt[i].first)
          cnt[i].first--;
        else
          cnt[i].second++;
      }
    }
  }
  ll now = 0;
  rep(i, n) {
    if (!cnt[i].second)
      now += cnt[i].first;
  }
  rep(i, n) {
    if (cnt[i].first && cnt[i].second) {
      a.push_back({cnt[i].second, cnt[i].first - cnt[i].second});
    }
  }
  sort(all(a));
  ll j = 0;
  priority_queue<ll> que;
  rep(i, a.size()) {
    if (j < a.size()) {
      while (a[j].first <= now) {
        que.push(a[j].second);
        j++;
      }
    }
    if (que.empty()) {
      cout << "No" << endl;
      return 0;
    }
    now += que.top();
    que.pop();
  }
  rep(i, n) {
    if (!cnt[i].first)
      now -= cnt[i].second;
  }
  cout << (now == 0 ? "Yes" : "No") << endl;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
string s[1000000];
int main() {
  ll n;
  cin >> n;
  rep(i, n) cin >> s[i];
  vector<P> pl, mi;
  int z = 1, SUM = 0;
  rep(i, n) rep(j, s[i].size()) {
    if (s[i][j] == '(')
      SUM++;
    else
      SUM--;
  }
  if (SUM != 0)
    z = 0;
  rep(i, n) {
    ll sum = 0, mok = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        sum++;
      else
        sum--;
      mok = min(mok, sum);
    }
    if (sum >= 0) {
      pl.push_back(P(mok, sum));
    } else {
      mok = mok * (-1);
      sum = sum * (-1);
      mi.push_back(P(mok - sum, sum));
    }
  }
  ll sum = 0;
  sort(all(pl), greater<P>());
  sort(all(mi), greater<P>());
  rep(i, pl.size()) {
    if (sum < pl[i].first * (-1))
      z = 0;
    sum += pl[i].second;
  }
  rep(i, mi.size()) {
    if (sum < mi[i].first + mi[i].second)
      z = 0;
    sum -= mi[i].second;
  }
  if (z)
    cout << "Yes";
  else
    cout << "No";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
string s[10];
int main() {
  ll n;
  cin >> n;
  rep(i, n) cin >> s[i];
  vector<P> pl, mi;
  int z = 1, SUM = 0;
  rep(i, n) rep(j, s[i].size()) {
    if (s[i][j] == '(')
      SUM++;
    else
      SUM--;
  }
  if (SUM != 0)
    z = 0;
  rep(i, n) {
    ll sum = 0, mok = 0;
    rep(j, s[i].size()) {

      if (s[i][j] == '(')
        sum++;
      else
        sum--;
      mok = min(mok, sum);
    }
    if (sum >= 0) {
      pl.push_back(P(mok, sum));
    } else {
      mok = mok * (-1);
      sum = sum * (-1);
      mi.push_back(P(mok - sum, sum));
    }
  }
  ll sum = 0;
  sort(all(pl), greater<P>());
  sort(all(mi));
  rep(i, pl.size()) {
    if (sum < pl[i].first * (-1))
      z = 0;
    sum += pl[i].second;
  }
  rep(i, mi.size()) {
    if (sum < mi[i].first + mi[i].second)
      z = 0;
    sum -= mi[i].second;
  }
  if (z)
    cout << "Yes";
  else
    cout << "No";
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r, i;
  bool operator<(const node &x) const {
    if ((l >= r) == (x.l >= x.r)) {
      if ((l >= r))
        return r < x.r;
      return l > x.l;
    }
    return l - r > x.l - x.r;
  }
} arr[2000007], brr[2000007];
int main() {
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int l = 0, r = 0;
    for (int j = 0; j < (int)s.length(); j++) {
      if (s[j] == '(')
        l++;
      else if (l)
        l--;
      else
        r++;
    }
    arr[i] = {l, r, i};
    brr[i] = {r, l, i};
  }
  sort(arr, arr + n);
  sort(brr, brr + n);
  int ll = 0;
  for (int i = 0; i < n; i++) {
    if (ll < arr[i].r) {
      cout << "No" << endl;
      return 0;
    } else
      ll += arr[i].l - arr[i].r;
  }

  if (ll == 0) {
    cout << "Yes" << endl;
    /* for(int i=0; i<n; i++)
         cout<<arr[i].i+1<<' ';
     cout<<endl;*/
  } else
    cout << "No" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r, i;
  bool operator<(const node &x) const {
    if ((l >= r) == (x.l >= x.r)) {
      if ((l >= r))
        return r < x.r;
      return l > x.l;
    }
    return l - r > x.l - x.r;
  }
} arr[200007], brr[200007];
int main() {
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int l = 0, r = 0;
    for (int j = 0; j < (int)s.length(); j++) {
      if (s[j] == '(')
        l++;
      else if (l)
        l--;
      else
        r++;
    }
    arr[i] = {l, r, i};
    brr[i] = {r, l, i};
  }
  sort(arr, arr + n);
  sort(brr, brr + n);
  int ll = 0;
  for (int i = 0; i < n; i++) {
    if (ll < arr[i].r) {
      cout << "No" << endl;
      return 0;
    } else
      ll += arr[i].l - arr[i].r;
  }

  if (ll == 0) {
    cout << "Yes" << endl;
    /* for(int i=0; i<n; i++)
         cout<<arr[i].i+1<<' ';
     cout<<endl;*/
  } else
    cout << "No" << endl;
  return 0;
}