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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cout << "[" << #var << "] " << var << endl
const ll mod = 1000000007;
const ll INF = 1E+18;

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  // vector<P> x(n);
  vector<P> a;
  vector<P> b;
  vector<P> x1;
  vector<P> x2;

  rep(i, n) {
    cin >> s[i];
    ll x = 0;
    ll y = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        y++;
      else
        y--;
      x = min(x, y);
    }
    if (y >= 0 && x == 0)
      a.emplace_back(x, y);
    else if (y < 0 && x == y)
      b.emplace_back(x, y);
    else if (y >= 0)
      x1.emplace_back(x, y);
    else
      x2.emplace_back(x, y);
  }
  sort(x1.begin(), x1.end());
  sort(x2.begin(), x2.end());
  reverse(x1.begin(), x1.end());
  string ans = "Yes";
  ll now = 0;
  rep(i, a.size()) now += a[i].second;
  for (auto e : x1) {
    if (now < -e.first) {
      ans = "No";
      break;
    }
    now += e.second;
  }
  for (auto e : x2) {
    if (now < -e.first) {
      ans = "No";
      break;
    }
    now += e.second;
  }
  rep(i, b.size()) now += b[i].second;

  if (now != 0)
    ans = "No";

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cout << "[" << #var << "] " << var << endl
const ll mod = 1000000007;
const ll INF = 1E+18;

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  // vector<P> x(n);
  vector<P> a;
  vector<P> b;
  map<P, ll> mp1;
  map<P, ll> mp2;

  rep(i, n) {
    cin >> s[i];
    ll x = 0;
    ll y = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        y++;
      else
        y--;
      x = min(x, y);
    }
    if (y >= 0 && x == 0)
      a.emplace_back(x, y);
    else if (y < 0 && x == y)
      b.emplace_back(x, y);
    else if (y >= 0)
      mp1[P(x, y)]++;
    else
      mp2[P(x, y)]++;
  }
  string ans = "Yes";
  ll now = 0;
  rep(i, a.size()) now += a[i].second;
  while (mp1.size() != 0) {
    auto it = mp1.lower_bound(P(-now, -INF));
    if (it == mp1.end()) {
      cout << "No" << endl;
      return 0;
    }
    it->second--;
    now += it->first.second;
    if (it->second == 0)
      mp1.erase(it->first);
  }
  while (mp2.size() != 0) {
    auto it = mp1.lower_bound(P(-now, -INF));
    if (it == mp2.end()) {
      cout << "No" << endl;
      return 0;
    }
    it->second--;
    now += it->first.second;
    if (it->second == 0)
      mp2.erase(it->first);
  }
  rep(i, b.size()) now += b[i].second;

  if (now != 0)
    ans = "No";

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

#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP(A, B) make_pair(A, B)
#define fi first
#define se second

#define gcd(a, b) __gcd(a, b)
#define pi acos(-1.0)
#define pii pair<int, int>
#define ll long long
#define MAX 1000005
#define MOD 998244353
#define M 998244353
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x & -x)

long long qpow(long long a, long long n) {
  long long ret = 1;
  a = (a % M + M) % M;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = (ret * a) % M;
    a = (a * a) % M;
  }
  return ret;
}

int n;
vector<string> vs, l, r;
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  priority_queue<pii> va, vb;
  ll nsum = 0;
  for (auto &s : vs) {
    pii a(0, 0);
    for (int i = 0; i < s.size(); ++i) {
      a.fi += s[i] == '(' ? 1 : -1;
      a.se = min(a.se, a.fi);
    }
    if (a.se >= 0)
      nsum += a.fi;
    else if (a.fi >= 0)
      va.push(a);
    else
      vb.push((pii){a.fi - a.se, -a.se});
  }
  while (!va.empty()) {
    pii p = va.top();
    va.pop();
    if (nsum + p.se <= 0) {
      cout << "No" << endl;
      return 0;
    }
    nsum += p.fi;
  }

  while (!vb.empty()) {
    pii p = vb.top();
    vb.pop();
    if (nsum - p.se < 0) {
      cout << "No" << endl;
      return 0;
    }
    nsum += p.fi - p.se;
  }
  if (nsum == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP(A, B) make_pair(A, B)
#define fi first
#define se second

#define gcd(a, b) __gcd(a, b)
#define pi acos(-1.0)
#define pii pair<int, int>
#define ll long long
#define MAX 1000005
#define MOD 998244353
#define M 998244353
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x & -x)

long long qpow(long long a, long long n) {
  long long ret = 1;
  a = (a % M + M) % M;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = (ret * a) % M;
    a = (a * a) % M;
  }
  return ret;
}

int n;
vector<string> vs, l, r;
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  vector<pii> va;
  for (auto &s : vs) {
    pii a(0, 0);
    for (int i = 0; i < s.size(); ++i) {
      a.fi += s[i] == '(' ? 1 : -1;
      a.se = min(a.se, a.fi);
    }
    va.push_back(a);
  }
  auto cmp = [](const pii &a, const pii &b) { return a.se >= b.se; };
  sort(va.begin(), va.end(), cmp);
  // for (auto &p : va) {
  //     cout << p.fi << ' ' << p.se << endl;
  // }
  ll front = 0, nsum = 0;
  auto cmp2 = [](pii a, pii b) { return a.fi >= b.fi; };
  priority_queue<pii, vector<pii>, decltype(cmp2)> q(cmp2);
  while (front < va.size()) {
    int lastf = front;
    while (front < va.size() && nsum + va[front].se >= 0) {
      q.push(va[front++]);
    }
    if (q.empty()) {
      cout << "No" << endl;
      return 0;
    }
    pii p = q.top();
    q.pop();
    // cout << nsum << ' ' <<p.fi << endl;
    nsum += p.fi;
  }
  if (nsum == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

// -----------------------------------
// Author     : MatsuTaku
// Country    : Japan
// Created    : 05/10/20 23:28:18
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n;
  cin >> n;
  vector<pair<int, int>> I(n);

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    I[i].first = 0;
    I[i].second = 0;
    for (auto c : s) {
      if (c == '(')
        I[i].first++;
      else
        I[i].first--;
      I[i].second = min(I[i].second, I[i].first);
    }
  }
  auto solve = [&]() {
    vector<pair<int, int>> U, D;
    for (auto p : I) {
      if (p.first >= 0)
        U.push_back(p);
      else
        D.push_back(p);
    }
    int s = 0;
    {
      sort(U.begin(), U.end(),
           [](auto l, auto r) { return l.second > r.second; });
      for (auto u : U) {
        if (s + u.second < 0)
          return false;
        s += u.first;
      }
    }
    {
      sort(D.begin(), D.end(), [](auto l, auto r) {
        return (l.second - l.first) < (r.second - r.first);
      });
      for (auto u : D) {
        if (s + u.second < 0)
          return false;
        s += u.first;
      }
    }
    return s == 0;
  };
  cout << (solve() ? "Yes" : "No") << endl;

  return 0;
}


Error: 0

Fixed submission:

// -----------------------------------
// Author     : MatsuTaku
// Country    : Japan
// Created    : 05/10/20 23:28:18
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n;
  cin >> n;
  vector<string> S(n);
  vector<pair<int, int>> I(n);

  for (int i = 0; i < n; i++) {
    cin >> S[i];
    I[i].first = 0;
    I[i].second = 0;
    for (auto c : S[i]) {
      if (c == '(')
        I[i].first++;
      else
        I[i].first--;
      I[i].second = min(I[i].second, I[i].first);
    }
  }
  sort(I.begin(), I.end(), [](auto l, auto r) { return l.second > r.second; });
  auto solve = [&]() {
    int t = 0;
    priority_queue<pair<int, int>> qs;
    while (I[t].second >= 0)
      qs.push(I[t++]);
    int s = 0;
    for (int i = 0; i < n; i++) {
      if (qs.empty())
        return false;
      auto q = qs.top();
      if (q.first < 0)
        break;
      qs.pop();
      s += q.first;
      while (t < n and I[t].second >= -s)
        qs.push(I[t++]);
    }
    if (t != n)
      return false;
    vector<pair<int, int>> T;
    while (not qs.empty()) {
      T.push_back(qs.top());
      qs.pop();
    }
    sort(T.begin(), T.end(),
         [](auto l, auto r) { return l.second < r.second; });
    for (auto i : T) {
      if (s + i.second < 0)
        return false;
      s += i.first;
    }
    return s == 0;
  };
  cout << (solve() ? "Yes" : "No") << endl;

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using ll = long long int;
using P = std::pair<int, int>;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

P v[1001001];

bool comp(P a, P b) {
  if ((a.first - a.second) >= 0 && (b.first - b.second) < 0) {
    return true;
  }
  if ((a.first - a.second) < 0 && (b.first - b.second) >= 0) {
    return false;
  }
  if (a.first - a.second >= 0) {
    return a.second < b.second;
  } else {
    return a.first > b.first;
  }
}
int main(void) {
  int n, i, m, j, x = 0, y = 0;
  std::string s;
  std::cin >> n;
  for (i = 0; i < n; ++i) {
    std::cin >> s;
    m = s.l_ength();
    for (j = 0; j < m; ++j) {
      if (s[j] == '(') {
        ++v[i].first;
      } else if (v[i].first) {
        --v[i].first;
      } else {
        ++v[i].second;
      }
    }
  }
  sort(v, v + n, comp);
  for (i = 0; i < n; ++i) {
    if (v[i].first - v[i].second < 0) {
      break;
    }
    if (x - v[i].second < 0) {
      std::cout << "No" << std::endl;
      return 0;
    }
    x -= v[i].second;
    x += v[i].first;
  }
  for (i = (n - 1); i >= 0; --i) {
    if (v[i].first - v[i].second >= 0) {
      break;
    }
    if (y - v[i].first < 0) {
      std::cout << "No" << std::endl;
      return 0;
    }
    y -= v[i].first;
    y += v[i].second;
  }
  std::cout << ((x == y) ? "Yes" : "No") << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using ll = long long int;
using P = std::pair<int, int>;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

P v[1001001];

bool comp(P a, P b) {
  if (a.first - a.second == b.first - b.second) {
    return (a.first < a.second) ^ (a.second < b.second);
  }
  return ((a.first - a.second) > (b.first - b.second));
}

int main(void) {
  int n, i, m, j, x = 0;
  std::string s;
  std::cin >> n;
  for (i = 0; i < n; ++i) {
    std::cin >> s;
    m = s.l_ength();
    for (j = 0; j < m; ++j) {
      if (s[j] == '(') {
        ++v[i].first;
      } else if (v[i].first) {
        --v[i].first;
      } else {
        ++v[i].second;
      }
    }
  }
  sort(v, v + n, comp);
  for (i = 0; i < n; ++i) {
    if (x - v[i].second < 0) {
      std::cout << "No" << std::endl;
      return 0;
    }
    x -= v[i].second;
    x += v[i].first;
  }
  std::cout << ((x == 0) ? "Yes" : "No") << std::endl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
int n, ck;
char s[N];
typedef pair<int, int> pii;
vector<pii> vc;
void cal() {
  int a = 0, b = INF;
  for (int i = 0; s[i]; i++) {
    if (s[i] == '(')
      a++;
    else
      a--;
    b = min(b, a);
  }
  if (b >= 0)
    ck += a;
  else
    vc.push_back(pii(a, b));
}
bool cmp(const pii &a, const pii &b) {
  if (a.first >= 0 || b.first >= 0) {
    if (a.first >= 0 && b.first >= 0)
      return a.second > b.second;
    else
      return a.first >= 0;
  }
  return a.first + b.second > b.first + a.second;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    cal();
  }
  sort(vc.begin(), vc.end(), cmp);
  for (pii p : vc) {
    if (ck + p.second < 0) {
      puts("No");
      return 0;
    }
    ck += p.first;
  }
  puts(ck ? "No" : "Yes");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
int n;
char s[N];
typedef pair<int, int> pii;
vector<pii> vc;
void cal() {
  int a = 0, b = INF;
  for (int i = 0; s[i]; i++) {
    if (s[i] == '(')
      a++;
    else
      a--;
    b = min(b, a);
  }
  vc.push_back(pii(a, b));
}
bool cmp(const pii &a, const pii &b) {
  // if (a.first >= 0 && b.first >= 0)return a.second > b.second;
  if (a.second >= 0 || b.second >= 0) {
    return a.second > b.second;
  }
  return a.first + b.second > b.first + a.second;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    cal();
  }
  sort(vc.begin(), vc.end(), cmp);
  for (pii p : vc) {
    if (p.second > 0 && p.second != p.first)
      while (1)
        ;
  }
  int ck = 0;
  for (pii p : vc) {
    if (ck + p.second < 0) {
      puts("No");
      return 0;
    }
    ck += p.first;
  }
  puts(ck ? "No" : "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;

void add(stack<char> &s, char c) {
  if (c == '(') {
    s.push(c);
  } else if (c == ')') {
    if (!s.empty() && s.top() == '(') {
      s.pop();
    } else {
      s.push(c);
    }
  } else {
    abort();
  }
}

void add(stack<char> &s, string str) {
  for (auto c : str) {
    add(s, c);
  }
}

int main() {
  ll N;
  cin >> N;
  string Si;
  vector<string> lr(N);
  vector<array<int, 2>> order(N);
  int i_left_candidate = 0;
  int i_right_candidate = 0;
  for (int i = 0; i < N; i++) {
    cin >> Si;
    stack<char> s;
    add(s, Si);
    while (!s.empty()) {
      auto v = s.top();
      s.pop();
      lr[i] += v;
    }
    reverse(lr[i].begin(), lr[i].end());
    order[i][0] = 0;
    for (auto c : lr[i]) {
      if (c == '(')
        order[i][0]--;
      else if (c == ')')
        order[i][0]++;
      else {
        abort();
      };
    }
    order[i][1] = i;
    if (lr[i].size() > 0 && lr[i].front() == '(')
      i_left_candidate = i;
    if (lr[i].size() > 0 && lr[i].back() == ')')
      i_right_candidate = i;
  }
  sort(order.begin(), order.end());

  // if(i_left_candidate == -1 || i_right_candidate == -1){
  //     cout << "No" << endl;
  //     return 0;
  // }

  stack<char> s;
  add(s, lr[i_left_candidate]);
  for (auto v : order) {
    if (v[1] == i_left_candidate)
      continue;
    if (v[1] == i_right_candidate)
      continue;
    add(s, lr[v[1]]);
  }
  add(s, lr[i_right_candidate]);
  if (s.size() == 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;

void add(stack<char> &s, char c) {
  if (c == '(') {
    s.push(c);
  } else if (c == ')') {
    if (!s.empty() && s.top() == '(') {
      s.pop();
    } else {
      s.push(c);
    }
  } else {
    abort();
  }
}

void add(stack<char> &s, string str) {
  for (auto c : str) {
    add(s, c);
  }
}

int main() {
  ll N;
  cin >> N;
  string Si;
  vector<string> lr(N);
  vector<array<int, 2>> order(N);
  int i_left_candidate = -1;
  for (int i = 0; i < N; i++) {
    cin >> Si;
    stack<char> s;
    add(s, Si);
    while (!s.empty()) {
      auto v = s.top();
      s.pop();
      lr[i] += v;
    }
    reverse(lr[i].begin(), lr[i].end());
    order[i][0] = 0;
    for (auto c : lr[i]) {
      if (c == '(')
        order[i][0]--;
      else if (c == ')')
        order[i][0]++;
      else {
        abort();
      };
    }
    order[i][1] = i;
    if (lr[i].size() > 0 && lr[i].front() == '(')
      i_left_candidate = i;
  }
  sort(order.begin(), order.end());

  stack<char> s;
  add(s, lr[i_left_candidate]);
  for (auto v : order) {
    if (v[1] == i_left_candidate)
      continue;
    add(s, lr[v[1]]);
  }
  if (s.size() == 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;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pair<int, int>> u, v;
  rep(i, n) {
    string s;
    cin >> s;
    int cnt = 0, mi = 0;
    rep(j, s.size()) {
      if (s[j] == '(') {
        cnt++;
      } else {
        cnt--;
      }
      chmin(mi, cnt);
    }
    if (cnt >= 0) {
      u.push_back({mi, cnt});
    } else {
      v.push_back({cnt - mi, cnt});
    }
  }
  sort(u.begin(), u.end(), greater<pair<int, int>>());
  sort(v.begin(), v.end(), greater<pair<int, int>>());
  int sum = 0;
  for (auto x : u) {
    if (sum + x.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    sum += x.second;
  }
  for (auto x : v) {
    if (sum + x.second - x.first < 0) {
      cout << "No" << endl;
      return 0;
    }
    sum += x.second;
  }
  cout << (sum ? "No" : "Yes") << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pair<int, int>> u, v;
  rep(i, n) {
    string s;
    cin >> s;
    int cnt = 0, mi = 0;
    rep(j, s.size()) {
      if (s[i] == '(') {
        cnt++;
      } else {
        cnt--;
      }
      chmin(mi, cnt);
    }
    if (cnt >= 0) {
      u.push_back({mi, cnt});
    } else {
      v.push_back({cnt - mi, cnt});
    }
  }
  sort(u.begin(), u.end(), greater<pair<int, int>>());
  sort(v.begin(), v.end(), greater<pair<int, int>>());
  int sum = 0;
  for (auto x : u) {
    sum += x.second;
    if (sum < 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (auto x : v) {
    sum += x.second;
    if (sum < 0) {
      cout << "No" << endl;
    }
  }
  cout << (sum ? "No" : "Yes") << endl;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ln '\\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \\
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = int(1e6);
int n, sum, pre[nsz + 5], mn[nsz + 5];
pair<int, int> dat[nsz + 5];
string s[nsz + 5];

bool inline cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first > 0 && b.first > 0)
    return a.second > b.second;
  if (a.first < 0 && b.first < 0)
    return a.second - a.first < b.second - b.first;
  return a.first > b.first;
}

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cont(i, n) {
    cin >> s[i];
    int cur = 0;
    loop(j, s[i].size()) {
      char c = s[i][j];
      cur += c == '(' ? +1 : -1;
      mn[i] = min(mn[i], cur);
    }
    pre[i] = cur;
    sum += cur;
    dat[i] = make_pair(pre[i], mn[i]);
  }
  if (sum != 0) {
    cout << "No" << ln;
    return 0;
  }
  sort(dat + 1, dat + n + 1, cmp);
  int cur = 0;
  cont(i, n) {
    int a = dat[i].first, b = dat[i].second;
    if (cur + b < 0) {
      cout << "No" << ln;
      return 0;
    }
    cur += a;
  }
  cout << "Yes" << ln;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ln '\\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \\
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = int(1e3);
int n, sum, pre[nsz + 5], mn[nsz + 5];
pair<int, int> dat[nsz + 5];
string s[nsz + 5];

bool inline cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first > 0 && b.first > 0)
    return a.second > b.second;
  if (a.first < 0 && b.first < 0)
    return a.second - a.first < b.second - b.first;
  return a.first > b.first;
}

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cont(i, n) {
    cin >> s[i];
    int cur = 0;
    loop(j, s[i].size()) {
      char c = s[i][j];
      cur += c == '(' ? +1 : -1;
      mn[i] = min(mn[i], cur);
    }
    pre[i] = cur;
    sum += cur;
    dat[i] = make_pair(pre[i], mn[i]);
  }
  if (sum != 0) {
    cout << "No" << ln;
    return 0;
  }
  sort(dat + 1, dat + n + 1, cmp);
  int cur = 0;
  cont(i, n) {
    int a = dat[i].first, b = dat[i].second;
    if (cur + b < 0) {
      cout << "No" << ln;
      return 0;
    }
    cur += a;
  }
  cout << "Yes" << ln;
}