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

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
ll mod = 998244353;
long long fact[200010];
void pre() {
  fact[0] = 1;
  for (long long i = 1; i < 200010; ++i) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}
long long pwr(long long x, long long e) {
  if (e == 0)
    return 1;
  long long tmp = pwr(x, e / 2);
  tmp = (tmp * tmp) % mod;
  if (e % 2)
    tmp = (tmp * x) % mod;
  return tmp;
}
long long cmb(long long x, long long y) {
  long long res, tmp;
  tmp = pwr(fact[x - y], mod - 2);
  res = (fact[x] * tmp) % mod;
  tmp = pwr(fact[y], mod - 2);
  res = (res * tmp) % mod;
  return res;
}
int main() {
  ll n, m, k;
  pre();
  while (cin >> n >> m >> k) {
    ll ans = 0;
    for (int i = 0; i <= k; ++i) {
      ans += ((pwr(m - 1, n - i - 1) * m) % mod) * cmb(n - 1, i);
      ans %= mod;
      // cout<<ans<<endl;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
ll mod = 998244353;
long long fact[100010];
void pre() {
  fact[0] = 1;
  for (long long i = 1; i < 100010; ++i) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}
long long pwr(long long x, long long e) {
  if (e == 0)
    return 1;
  long long tmp = pwr(x, e / 2);
  tmp = (tmp * tmp) % mod;
  if (e % 2)
    tmp = (tmp * x) % mod;
  return tmp;
}
long long cmb(long long x, long long y) {
  long long res, tmp;
  tmp = pwr(fact[x - y], mod - 2);
  res = (fact[x] * tmp) % mod;
  tmp = pwr(fact[y], mod - 2);
  res = (res * tmp) % mod;
  return res;
}
int main() {
  ll n, m, k;
  pre();
  while (cin >> n >> m >> k) {
    ll ans = 0;
    for (int i = 0; i <= k; ++i) {
      ans += ((pwr(m - 1, n - i - 1) * m) % mod) * cmb(n - 1, i);
      ans %= mod;
      // cout<<ans<<endl;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

/* Isn't passion overrated?*/

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll mod = 998244353;
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll ans = power(a, x / 2) % mod;
  ans = (ans % mod * ans % mod) % mod;
  ans %= mod;
  if (x % 2)
    ans = (ans % mod * a) % mod;
  return ans % mod;
}

vector<ll> per(200010);

ll ncr(ll n, ll i) {
  return (per[n] % mod * power(per[i], mod - 2) % mod *
          power(per[n - i], mod - 2) % mod) %
         mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  per[0] = 1;
  ll h = per.size();
  for (ll i = 1; i < h; i++) {
    per[i] = i * per[i - 1];
    per[i] %= mod;
  }
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll temp = ncr(n - 1, i) % mod;
    temp = (temp * m) % mod;
    temp = (temp * power(m - 1, n - 1 - i) % mod) % mod;
    ans = ans + temp;
    ans %= mod;
  }
  cout << ans % mod;
  return 0;
}


Error: WA

Fixed submission:

/* Isn't passion overrated?*/

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll mod = 998244353;
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll ans = power(a, x / 2) % mod;
  ans = (ans % mod * ans % mod) % mod;
  ans %= mod;
  if (x % 2)
    ans = (ans % mod * a) % mod;
  return ans % mod;
}

vector<ll> per(100010);

ll ncr(ll n, ll i) {
  return (per[n] % mod * power(per[i], mod - 2) % mod *
          power(per[n - i], mod - 2) % mod) %
         mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  per[0] = 1;
  ll h = per.size();
  for (ll i = 1; i < h; i++) {
    per[i] = i * per[i - 1];
    per[i] %= mod;
  }
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll temp = ncr(n - 1, i) % mod;
    temp = (temp * m) % mod;
    temp = (temp * power(m - 1, n - 1 - i) % mod) % mod;
    ans = ans + temp;
    ans %= mod;
  }
  cout << ans % mod;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 998244353;
ll ksm(ll x, ll y) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans *= x;
    x *= x;
    x %= mod;
    ans %= mod;
    y >>= 1;
  }
  return ans;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll res = n - k - 1;
  ll ans = 0;
  ll tmp = 1;
  for (ll i = n - res; i <= n - 1; i++) {
    tmp *= i;
    tmp %= mod;
  }
  // printf("%lld\\n", tmp);
  for (ll i = 2; i <= res; i++) {
    tmp *= ksm(i, mod - 2);
    tmp %= mod;
  }
  // printf("%lld\\n", tmp);
  for (ll p = res; p <= n - 1; p++) {
    ans += ((tmp * m) % mod * ksm(m - 1, p)) % mod;
    ans %= mod;
    tmp *= (n - p - 1) % mod;
    tmp %= mod;
    // printf("%lld:%lld\\n", p, tmp);
    tmp *= ksm(p + 1, mod - 2) % mod;
    tmp %= mod;
  }
  // printf("%lld\\n", ans);
  // ans -= m * ksm(m - 1, n - 1);
  ans %= mod;
  ans += mod;
  ans %= mod;
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 998244353;
ll ksm(ll x, ll y) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans *= x;
    x *= x;
    x %= mod;
    ans %= mod;
    y >>= 1;
    assert(x > 0);
    assert(y >= 0);
    assert(ans > 0);
  }
  return ans;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll res = n - k - 1;
  ll ans = 0;
  ll tmp = 1;
  for (ll i = n - res; i <= n - 1; i++) {
    tmp *= i;
    tmp %= mod;
  }
  // printf("%lld\\n", tmp);
  for (ll i = 2; i <= res; i++) {
    tmp *= ksm(i, mod - 2);
    tmp %= mod;
  }
  // printf("%lld\\n", tmp);
  for (ll p = res; p <= n - 1; p++) {
    ans += ((tmp * m) % mod * ksm(m - 1, p)) % mod;
    ans %= mod;
    tmp *= (n - p - 1) % mod;
    tmp %= mod;
    // printf("%lld:%lld\\n", p, tmp);
    tmp *= ksm(p + 1, mod - 2) % mod;
    tmp %= mod;
    assert(ans > 0);
    assert(tmp >= 0);
  }
  // printf("%lld\\n", ans);
  // ans -= m * ksm(m - 1, n - 1);
  ans %= mod;
  printf("%lld\\n", ans);
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
using namespace std;

ll mod = 998244353, u = 1, d[300001];

ll mypow(ll x, ll y) {
  if (x <= 0 || y <= 0)
    return 1;
  if (y == 1)
    return x;
  ll z = mypow(x, y / 2) % mod;
  if (y % 2 == 1) {
    return ((z * z) % mod) * x % mod;
  } else {
    return (z * z) % mod;
  }
}

void pc(ll l) {
  for (ll i = l; i >= 1; i--) {
    u = (u * i) % mod;
  }
  d[0] = 1;
  for (ll i = 1; i <= l; i++) {
    d[i] = (d[i - 1] * i) % mod;
  }
}

ll choose(ll l, ll r) {
  return u * mypow((d[l - r] * d[r]) % mod, mod - 2) % mod;
}

void solve() {
  ll n, m, k, x, y, z;
  cin >> n >> m >> k;
  if (m == 1) {
    if (k == n - 1)
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return;
  }
  x = (mypow(m - 1, n - 1) * m) % mod;
  ll ans = x;
  // z = mypow(m - 1, n - k - 2);
  pc(n - 1);
  for (ll i = 1; i <= k; i++) {
    y = choose(n - 1, i);
    z = mypow(m - 1, n - i - 1);
    ans = (ans + (m * ((y * z) % mod) % mod)) % mod;
  }
  cout << ans << endl;
  return;
}

int main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
using namespace std;

ll mod = 998244353;

ll mypow(ll x, ll y) {
  if (y == 1)
    return x;
  ll z = mypow(x, y / 2) % mod;
  if (y % 2 == 1) {
    return ((z * z) % mod) * x % mod;
  } else {
    return (z * z) % mod;
  }
}

ll choose(ll l, ll r) {
  ll u = 1, d = 1;
  for (ll i = l; i > l - r; i--) {
    u = (u * i) % mod;
  }
  for (ll i = 1; i <= r; i++) {
    d = (d * i) % mod;
  }
  d = mypow(d, mod - 2);
  return (u * d) % mod;
}

void solve() {
  ll n, m, k, x, y, z;
  cin >> n >> m >> k;
  x = (mypow(m - 1, n - 1) * m) % mod;
  ll ans = x;
  for (int i = 1; i <= k; i++) {
    y = choose(n - 1, i);
    z = mypow(m - 1, n - i - 1);
    ans = (ans + (m * ((y * z) % mod) % mod)) % mod;
    // cout << y << " " << z << " " << ans << endl;
  }
  cout << ans << endl;
  return;
}

int main() {
  solve();
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
ll factorial[200001];
ll factorialinv[200001];
ll calc(ll a, ll b, ll c) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll d = calc(a, b / 2, c);
    return d * d % c;
  } else {
    return a * calc(a, b - 1, c) % c;
  }
}
ll combi(ll a, ll b) {
  return factorial[a] % mod * factorialinv[b] % mod * factorialinv[a - b] % mod;
}
int main() {
  long long int n, m, k, ans = 0;
  cin >> n >> m >> k;
  factorial[0] = 1;
  factorialinv[0] = 1;
  for (ll i = 1; i <= n; i++) {
    factorial[i] = factorial[i - 1] * i % mod;
    factorialinv[i] = factorialinv[i - 1] * calc(i, mod - 2, mod) % mod;
  }
  ll waru[n];
  ll temp;
  waru[0] = 1;
  for (long long int i = 1; i < n; i++)
    waru[i] = (waru[i - 1] * (m - 1)) % mod;
  for (long long int i = n - 1; i >= max(n - k - 1, (long long)0); i--) {
    temp = waru[i] * m % mod * combi(n - 1, i);
    cerr << temp;
    ans = (ans + temp) % mod;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, k;
  cin >> n >> m >> k;
  long long int dev = 998244353;
  long long int dp[k + 1], temp[k + 1];
  for (long long int i = 0; i < k + 1; i++) {
    dp[i] = 0;
  }
  dp[0] = m;
  for (long long int j = 0; j < n - 1; j++) {
    for (long long int i = j + 1; i >= 0; i--) {
      if (i != 0)
        dp[i] = (dp[i] * (m - 1) + dp[i - 1]) % dev;
      else
        dp[i] = (dp[i] * (m - 1)) % dev;
    }
  }
  long long int ans = 0;
  for (long long int i = 0; i < k + 1; i++)
    ans = (ans + dp[i]) % dev;
  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} c(200005);

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  mint ans = 0;
  mint col = m;
  for (int x = n - 1; x >= 0; --x) {
    mint now = col;
    now *= c(n - 1, x);
    if (x <= k)
      ans += now;
    col *= m - 1;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n, m, k;
  ll wari = 998244353;
  cin >> n >> m >> k;
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 1));
  rep(i, n + 1) rep(j, k + 1) dp[i][j] = 0;
  dp[0][0] = 0;
  dp[1][0] = m;
  dp[1][1] = 0;
  for (int i = 2; i <= n; i++) {
    int jmax = min(i - 1, (int)(k));
    for (int j = 0; j <= jmax; j++) {
      if (j == 0)
        dp[i][0] = ((m - 1) * dp[i - 1][0]) % wari;
      else
        dp[i][j] = (dp[i - 1][j - 1] + (m - 1) * dp[i - 1][j]) % wari;
    }
  }

  // rep(i,n+1) rep(j,k+1) printf("(%lld,%lld):%lld\\n", i, j, dp[i][j]);

  ll ans = 0;
  rep(i, k + 1) ans = (ans + dp[n][i]) % wari;
  cout << ans << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

constexpr long long MOD = 998244353LL;

// MOD考慮power関数
template <typename type> inline type ModPow(type a, type b) {
  type ret = 1;
  while (b > 0) {
    if (b & 0x1)
      ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}

// MOD考慮Combination関数使用準備関数
vector<long long> fac;
vector<long long> finv;
vector<long long> inv;
inline void ModCombiSetup(int size) {
  fac.resize(size + 1);
  finv.resize(size + 1);
  inv.resize(size + 1);
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < size; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// MOD考慮Combination関数
inline long long ModCombi(long long n, long long r) {
  if (n == 0 && r == 0)
    return 1;
  if (n < r || n < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

int main() {
  long long n, m, k;
  cin >> n >> m >> k;

  long long ans = 0;

  ModCombiSetup(n);

  for (long long i = 0; i <= k; i++) {
    long long cnt =
        ((m * ModCombi(n - 1, i) % MOD) * ModPow(m - 1, n - 1 - i)) % MOD;
    ans = (ans + cnt) % MOD;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

constexpr long long MOD = 998244353LL;

// MOD考慮power関数
template <typename type> inline type ModPow(type a, type b) {
  type ret = 1;
  for (type i = 0; i < b; i++) {
    ret *= a;
    ret %= MOD;
  }
  return ret;
}

// MOD考慮Combination関数使用準備関数
vector<long long> fac;
vector<long long> finv;
vector<long long> inv;
inline void ModCombiSetup(int size) {
  fac.resize(size + 1);
  finv.resize(size + 1);
  inv.resize(size + 1);
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < size; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// MOD考慮Combination関数
inline long long ModCombi(long long n, long long r) {
  if (n == 0 && r == 0)
    return 1;
  if (n < r || n < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

int main() {
  long long n, m, k;
  cin >> n >> m >> k;

  long long ans = 0;

  ModCombiSetup(n);

  for (long long i = 0; i <= k; i++) {
    long long cnt =
        ((m * ModCombi(n - 1, i) % MOD) * ModPow(m - 1, n - 1 - i)) % MOD;
    ans = (ans + cnt) % MOD;
  }

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

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

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

  ll n;
  cin >> n;
  string s;
  vector<pair<ll, ll>> p;

  for (ll i = 0; i < n; i++) {
    cin >> s;
    ll x = 0, d = 0;
    for (ll j = 0; j < (ll)s.size(); j++) {
      if (s[j] == '(')
        x++;
      if (s[j] == ')') {
        x--;
        d = min(d, x);
      }
    }
    if (x < 0)
      d -= x;
    p.push_back(make_pair(-d, x));
  }

  sort(p.begin(), p.end());

  ll h = 0;
  for (ll i = 0; i < n; i++) {
    if (p[i].second >= 0) {
      if (h - p[i].first < 0) {
        cout << "No" << '\\n';
        return 0;
      }
      h += p[i].second;
    }
  }
  for (ll i = n - 1; i >= 0; i--) {
    if (p[i].second < 0) {
      if (h - p[i].first + p[i].second < 0) {
        cout << "No" << '\\n';
        return 0;
      }
      h += p[i].second;
    }
  }
  if (h != 0) {
    cout << "No" << '\\n';
    return 0;
  }

  cout << "Yes" << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl /*, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
// #define rep2(i, n) for(ll i = 1; i <= ll(n); ++i)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
#define rep2(i, k, n) for (ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

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

  ll n;
  cin >> n;
  string s;
  vpl p;

  rep(i, n) {
    cin >> s;
    ll x = 0, d = 0;
    rep(j, s.size()) {
      if (s[j] == '(')
        x++;
      else {
        x--;
        chmin(d, x);
      }
    }
    p.push_back(pl(d, x));
  }

  sort(all(p), greater<pl>());

  ll h = 0, l = 0;
  for (; p[l].first >= 0; l++)
    h += p[l].second;
  rep2(i, l, n - 1) {
    if (p[i].second >= 0) {
      if (h + p[i].first < 0)
        goto OUT;
      h += p[i].second;
    }
  }
  rep2(i, l, n - 1) {
    if (p[i].second < 0) {
      if (h + p[i].first < 0)
        goto OUT;
      h += p[i].second;
    }
  }
  if (h != 0)
    goto OUT;

  cout << "Yes" << newl;
  return 0;

OUT:
  cout << "No" << newl;
  return 0;
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
int main() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> a;
  rep(i, n) {
    string s;
    cin >> s;
    long long lef = 0;
    long long worst = s.size();
    rep(j, s.size()) {
      if (s.at(j) == '(')
        lef++;
      else {
        lef--;
        worst = min(worst, lef);
      }
    }
    pair<long long, long long> date(worst, lef);
    a.push_back(date);
  }
  sort(a.begin(), a.end());
  long long cou = 0;
  long long y = 0;
  while (int(a.size()) - 2 - y >= 0) {
    if (a.at(int(a.size()) - 1 - y).second >= 0 &&
        cou + a.at(int(a.size()) - 1 - y).first >= 0) {
      cou += a.at(int(a.size()) - 1 - y).second;
      a.erase(a.begin() + int(a.size()) - 1 - y);
      y = 0;
    } else
      y++;
  }
  rep(i, a.size()) {
    a.at(i).first -= a.at(i).second;
    a.at(i).second *= -1;
  }
  sort(a.begin(), a.end());
  rep(i, a.size()) {
    a.at(i).first -= a.at(i).second;
    a.at(i).second *= -1;
  }
  long long x = 0;
  bool ans = true;
  rep(i, a.size()) {
    if (cou + a.at(i).first < 0) {
      ans = false;
      break;
    } else
      cou += a.at(i).second;
  }
  if (cou == 0 && ans)
    cout << "Yes";
  else
    cout << "No";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
int main() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> a;
  rep(i, n) {
    string s;
    cin >> s;
    long long lef = 0;
    long long worst = s.size();
    rep(j, s.size()) {
      if (s.at(j) == '(')
        lef++;
      else {
        lef--;
        worst = min(worst, lef);
      }
    }
    pair<long long, long long> date(worst, lef);
    a.push_back(date);
  }
  sort(a.begin(), a.end());
  long long cou = 0;
  long long y = 0;
  while (int(a.size()) - 2 - y >= 0) {
    if (a.at(n - 1 - y).second >= 0 && cou + a.at(n - 1 - y).first >= 0) {
      cou += a.at(n - 1 - y).second;
      a.erase(a.begin() + (n - 1 - y));
      y = 0;
    } else
      y++;
  }
  rep(i, a.size()) swap(a.at(i).first, a.at(i).second);
  sort(a.begin(), a.end());
  long long x = 0;
  bool ans = true;
  while (int(a.size()) != 0) {
    if ((a.at(int(a.size()) - 1 - x).second) * (-1) <= cou) {
      cou += a.at(int(a.size()) - 1 - x).second;
      a.erase(a.begin() + (int(a.size()) - 1 - x));
      x = 0;
    } else if (int(a.size()) - 2 - x >= 0)
      x++;
    else {
      ans = false;
      break;
    }
  }
  if (cou == 0 && ans)
    cout << "Yes";
  else
    cout << "No";
}
Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define y1 zzz
#define fi first
#define se second
#define PII pair<int, int>
#define PII2 pair<int, PII>
#define PII3 pair<PII, int>
#define PII4 pair<PII, PII>
#define all(x) (x).begin(), (x).end()
#define pb push_back
PII p[1000010];
vector<PII> up, down;
int n;
string s;
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int res = 0, tmp = 0;
    for (char ch : s) {
      if (ch == '(')
        res++;
      if (ch == ')')
        res--;
    }
    if (res >= 0) {
      res = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(')
          res++;
        if (s[i] == ')')
          res--;
        tmp = min(tmp, res);
      }
      up.pb({tmp, res});
    }
    if (res < 0) {
      res = 0;
      for (int i = s.size() - 1; i >= 0; i--) {
        if (s[i] == '(')
          res--;
        if (s[i] == ')')
          res++;
        tmp = min(tmp, res);
      }
      down.pb({tmp, res});
    }
  }
  sort(all(up), greater<PII>());
  sort(all(down), greater<PII>());
  int res_up = 0, res_down = 0;
  for (PII it : up) {
    if (res_up + it.fi < 0) {
      cout << "No";
      return;
    }
    res_up += it.se;
  }
  for (PII it : down) {
    if (res_down + it.fi < 0) {
      cout << "No";
      return;
    }
    res_down += it.se;
  }
  if (res_up == res_down)
    cout << "Yes";
  else
    cout << "No";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("CUBE.inp","r",stdin);
  // freopen("CUBE.out","w",stdout);
  // int t;cin>>t;while(t--)
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define y1 zzz
#define fi first
#define se second
#define PII pair<int, int>
#define PII2 pair<int, PII>
#define PII3 pair<PII, int>
#define PII4 pair<PII, PII>
#define all(x) (x).begin(), (x).end()
#define pb push_back
PII p[1000010];
int n;
string s;
int cmp(PII a, PII b) {
  if (a.se >= 0 && b.se >= 0) {
    return a.fi > b.fi;
  }
  if (a.se >= 0 && b.se < 0) {
    return 1;
  }
  if (a.se < 0 && b.se >= 0) {
    return 0;
  }
  if (a.se < 0 && b.se < 0) {
    return 1;
  }
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (char ch : s) {
      if (ch == '(')
        p[i].se++;
      if (ch == ')')
        p[i].se--;
      p[i].fi = min(p[i].fi, p[i].se);
    }
  }
  sort(p, p + n, cmp);
  int cur = 0;
  for (int i = 0; i < n; i++) {
    if (cur + p[i].fi < 0) {
      cout << "No";
      return;
    }
    cur += p[i].se;
  }
  if (cur == 0)
    cout << "Yes";
  else
    cout << "No";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("CUBE.inp","r",stdin);
  // freopen("CUBE.out","w",stdout);
  // int t;cin>>t;while(t--)
  solve();
  return 0;
}