prithivMLmods/Coder-Stat huggingface dataset:
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
// #define DEBUG 10

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 998244353, maxn = 155, maxm = 15;
const double PI = acos(-1);
ll fat[maxn], inv[maxn];

ll mul(ll a, ll b) {
  ll ans = 0;
  while (b) {
    if (b & 1)
      ans = (ans + a) % mod;
    a = (a + a) % mod;
    b = (b >> 1);
  }
  return ans;
}

ll sum(ll a, ll b) { return (a % mod + b % mod) % mod; }

ll sub(ll a, ll b) { return (a - b + mod + mod) % mod; }

ll fexp(ll a, ll b) {
  ll ans = 1;

  while (b) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    a = mul(a, a);
    b = (b >> 1);
  }
  return ans;
}

void init() {
  fat[0] = fat[1] = inv[0] = inv[1] = 1;
  for (ll i = 2; i < maxn; i++) {
    fat[i] = mul(fat[i - 1], i);
    inv[i] = fexp(fat[i], mod - 2);
  }
}

ll C(ll a, ll b) {
  if (a < b)
    return 0;
  if (a == b || b == 0)
    return 1;
  return mul(mul(fat[a], inv[b]), inv[a - b]);
}

int main() {
  ios_base::sync_with_stdio(false);
  ll n, k, m, ans = 0;
  init();
  cin >> n >> m >> k;
  for (ll i = 0; i <= k; i++) {
    ll x = n - i - 1;
    ans = sum(ans, mul(mul(m, C(n - 1, x)), fexp(m - 1, x)));
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
// #define DEBUG 10

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 998244353, maxn = 2 * 1e5 + 10, maxm = 15;
const double PI = acos(-1);
ll fat[maxn], inv[maxn];

ll mul(ll a, ll b) {
  ll ans = 0;
  while (b) {
    if (b & 1)
      ans = (ans + a) % mod;
    a = (a + a) % mod;
    b = (b >> 1);
  }
  return ans;
}

ll sum(ll a, ll b) { return (a % mod + b % mod) % mod; }

ll sub(ll a, ll b) { return (a - b + mod + mod) % mod; }

ll fexp(ll a, ll b) {
  ll ans = 1;

  while (b) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    a = mul(a, a);
    b = (b >> 1);
  }
  return ans;
}

void init() {
  fat[0] = fat[1] = inv[0] = inv[1] = 1;
  for (ll i = 2; i < maxn; i++) {
    fat[i] = mul(fat[i - 1], i);
    inv[i] = fexp(fat[i], mod - 2);
  }
}

ll C(ll a, ll b) {
  if (a < b)
    return 0;
  if (a == b || b == 0)
    return 1;
  return mul(mul(fat[a], inv[b]), inv[a - b]);
}

int main() {
  ios_base::sync_with_stdio(false);
  ll n, k, m, ans = 0;
  init();
  cin >> n >> m >> k;
  for (ll i = 0; i <= k; i++) {
    ll x = n - i - 1;
    ans = sum(ans, mul(mul(m, C(n - 1, x)), fexp(m - 1, x)));
  }

  cout << ans << endl;
  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
const int MOD = 998244353;

const int MAX = 200010;

ll fac[MAX], finv[MAX], inv[MAX];

void cmbInit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

ll cmb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> p(n);
  p[0] = 1;
  rep(i, n) p[i + 1] = (p[i] * (m - 1)) % MOD;
  ll ans = 0;
  cmbInit();
  rep(i, k + 1) {
    ll a = (m * p[n - (i + 1)]) % MOD, b = cmb(n - 1, i);
    ans += (a * b) % MOD;
    ans = ans % MOD;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
const int MOD = 998244353;

const int MAX = 200010;

ll fac[MAX], finv[MAX], inv[MAX];

void cmbInit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

ll cmb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> p(n + 1);
  p[0] = 1;
  rep(i, n) p[i + 1] = (p[i] * (m - 1)) % MOD;
  ll ans = 0;
  cmbInit();
  rep(i, k + 1) {
    ll a = (m * p[n - (i + 1)]) % MOD, b = cmb(n - 1, i);
    ans += (a * b) % MOD;
    ans = ans % MOD;
  }
  cout << ans << endl;

  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 998244353;

ll pd[200005];
ll rd[200005];
ll Ep(int x, int y) {
  if (y == 1)
    return x;
  ll r = Ep(x, y / 2);
  if (y % 2 == 1)
    return r * r % MOD * x % MOD;
  else
    return r * r % MOD;
}
ll Ftl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (pd[x] != 0)
    return pd[x];
  return pd[x] = Ftl(x - 1) * x % MOD;
}
ll rFtl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (rd[x] != 0)
    return rd[x];
  return rd[x] = rFtl(x - 1) * Ep(x, MOD - 2) % MOD;
}
ll comb(ll m, ll n) {
  if (m < n)
    return 0;
  return Ftl(m) * rFtl(n) % MOD * rFtl(m - n) % MOD;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; ++i) {
    ll nans = m * Ep(m - 1, n - i - 1) % MOD;
    nans *= comb(n - 1, i);
    nans %= MOD;
    ans += nans;
    ans %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 998244353;

ll pd[200005];
ll rd[200005];
ll Ep(int x, int y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  ll r = Ep(x, y / 2);
  if (y % 2 == 1)
    return r * r % MOD * x % MOD;
  else
    return r * r % MOD;
}
ll Ftl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (pd[x] != 0)
    return pd[x];
  return pd[x] = Ftl(x - 1) * x % MOD;
}
ll rFtl(ll x) {
  if (x == 1 || x == 0)
    return 1;
  if (rd[x] != 0)
    return rd[x];
  return rd[x] = rFtl(x - 1) * Ep(x, MOD - 2) % MOD;
}
ll comb(ll m, ll n) {
  if (m < n)
    return 0;
  return Ftl(m) * rFtl(n) % MOD * rFtl(m - n) % MOD;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; ++i) {
    ll nans = m * Ep(m - 1, n - i - 1) % MOD;
    nans *= comb(n - 1, i);
    nans %= MOD;
    ans += nans;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// constexpr long MOD = 1000000007;
constexpr long MOD = 998244353;
vector<long> fact, inv;

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

long com(long n, long r) {
  long x = 1, y = 1;
  for (int i = 1; i <= r; i++) {
    x = x * (n - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * mpow(y, MOD - 2) % MOD;
}

void init(int n) {
  fact.push_back(1);
  inv.push_back(1);
  for (int i = 0; i < n; i++) {
    fact.push_back(fact.at(i) * (i + 1) % MOD);
    inv.push_back(mpow(fact.at(i + 1), MOD - 2));
  }
}

long qcom(long n, long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fact.at(n) * inv.at(r) % MOD * inv.at(n - r) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  init(100000);
  long N, M, K;
  cin >> N >> M >> K;
  long ans = 0;
  for (int i = 0; i <= K; i++) {
    long tmp = qcom(N - 1, i);
    tmp *= M;
    tmp %= MOD;
    tmp *= mpow(M - 1, N - i - 1);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << "\n";
}

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

// constexpr long MOD = 1000000007;
constexpr long MOD = 998244353;
vector<long> fact, inv;

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

long com(long n, long r) {
  long x = 1, y = 1;
  for (int i = 1; i <= r; i++) {
    x = x * (n - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * mpow(y, MOD - 2) % MOD;
}

void init(int n) {
  fact.push_back(1);
  inv.push_back(1);
  for (int i = 0; i < n; i++) {
    fact.push_back(fact.at(i) * (i + 1) % MOD);
    inv.push_back(mpow(fact.at(i + 1), MOD - 2));
  }
}

long qcom(long n, long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  return fact.at(n) * inv.at(r) % MOD * inv.at(n - r) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  init(200000);
  long N, M, K;
  cin >> N >> M >> K;
  long ans = 0;
  for (int i = 0; i <= K; i++) {
    long tmp = qcom(N - 1, i);
    tmp *= M;
    tmp %= MOD;
    tmp *= mpow(M - 1, N - i - 1);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << "\n";
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int64_t MOD = 998244353;
double PI = 3.141592653589793;
int64_t MAX = 500000;
vector<int64_t> fac(MAX), finv(MAX), inv(MAX);

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int64_t mypow(int64_t x, int64_t y) // x ^ y
{
  if (y == 1)
    return x;
  else if (y % 2 == 1)
    return mypow(x, y - 1) * x % MOD;
  else {
    int64_t tmp = mypow(x, y / 2);
    return tmp * tmp % MOD;
  }
}

int main() {
  COMinit();
  int64_t N, M, K, tmp, ans = 0;
  cin >> N >> M >> K;
  for (int64_t i = 0; i <= K; i++) {
    tmp = M * mypow(M - 1, N - i - 1) % MOD;
    tmp = (tmp * COM(N - 1, i)) % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans;
}

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

int64_t MOD = 998244353;
double PI = 3.141592653589793;
int64_t MAX = 500000;
vector<int64_t> fac(MAX), finv(MAX), inv(MAX);

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int64_t mypow(int64_t x, int64_t y) // x ^ y
{
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  else if (y % 2 == 1)
    return mypow(x, y - 1) * x % MOD;
  else {
    int64_t tmp = mypow(x, y / 2);
    return tmp * tmp % MOD;
  }
}

int main() {
  COMinit();
  int64_t N, M, K, tmp, ans = 0;
  cin >> N >> M >> K;
  for (int64_t i = 0; i <= K; i++) {
    tmp = M * mypow(M - 1, N - i - 1) % MOD;
    tmp = (tmp * COM(N - 1, i)) % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans;
}
Problem p02685 (Python)

Buggy submission (status: Runtime Error):
n, m, k = [int(t) for t in open(0).read().split()]
mod = 998244353


def solve(n):
    if n <= k + 1:
        return pow(m, n, mod)
    elif n == k + 2:
        return (pow(m, n, mod) + mod - m) % mod
    else:
        return (m * solve(n - 1) - (m - 1) * solve(n - k - 2)) % mod


print(solve(n))


Fixed submission:
n, m, k = [int(t) for t in open(0).read().split()]
mod = 998244353
mem = [0] * max(k + 3, n + 1)
for i in range(k + 2):
    mem[i] = pow(m, i, mod)
c = 1
for i in range(k + 2, n + 1):
    mem[i] = (m * mem[i - 1] - m * c * pow(m - 1, i - k - 2, mod)) % mod
    c = (c * (i - 1) * pow(i - k - 1, -1, mod)) % mod
print(mem[n])
Problem p02685 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from math import factorial

input = sys.stdin.readline
N, M, K = map(int, input().split())
MOD = 998244353
inv = [1] * N
FAC = factorial(N - 1) % MOD
if M == 1:
    print(1 if K == N - 1 else 0)
else:
    res = 0
    for i in range(1, N):
        inv[i] = pow(factorial(i), MOD - 2, MOD)
    for k in range(K + 1):
        # k個の○とN-k-1個の|を並べる順列
        # punc = (N-1)!/k!/(N-k-1)!
        # punc = factorial(
        #     k + (N - k - 1)) // factorial(k) // factorial(N - k - 1)
        punc = FAC
        # punc *= pow(factorial(k), MOD - 2, MOD)
        punc *= inv[k]
        punc = punc % MOD
        # punc *= pow(factorial(N - k - 1), MOD - 2, MOD)
        punc *= inv[N - k - 1]
        punc = punc % MOD
        color = M * pow(M - 1, N - k - 1, MOD)
        res += ((punc % MOD) * color) % MOD
        res = res % MOD
    print(res % MOD)


Fixed submission:
import sys
from math import factorial

input = sys.stdin.readline
N, M, K = map(int, input().split())
MOD = 998244353
inv = [1] * N
FAC = factorial(N - 1) % MOD
if M == 1:
    print(1 if K == N - 1 else 0)
else:
    res = 0
    for i in range(1, N):
        inv[i] = (inv[i - 1] * pow(i, MOD - 2, MOD)) % MOD
    for k in range(K + 1):
        # k個の○とN-k-1個の|を並べる順列
        # punc = (N-1)!/k!/(N-k-1)!
        # punc = factorial(
        #     k + (N - k - 1)) // factorial(k) // factorial(N - k - 1)
        punc = FAC
        # punc *= pow(factorial(k), MOD - 2, MOD)
        punc *= inv[k]
        punc = punc % MOD
        # punc *= pow(factorial(N - k - 1), MOD - 2, MOD)
        punc *= inv[N - k - 1]
        punc = punc % MOD
        color = M * pow(M - 1, N - k - 1, MOD)
        res += ((punc % MOD) * color) % MOD
        res = res % MOD
    print(res % MOD)
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

int mod = 998244353;

int expo(int num, int p) {
  if (num == 0) {
    if (p == 0)
      assert(false);
    return 0;
  }
  num = num % mod;
  int ret = 1;
  while (p) {
    if (p & 1)
      ret = (ret * num) % mod;
    num = (num * num) % mod;
    p /= 2;
  }
  return ret;
}

signed main() {
  IOS;
  int fact[200005], inv[200005];
  fact[0] = 1;
  for (int i = 1; i < 200005; ++i)
    fact[i] = (fact[i - 1] * i) % mod;
  for (int i = 0; i < 200005; ++i)
    inv[i] = expo(fact[i], mod - 2);
  int n, m, k;
  cin >> n >> m >> k;
  int ans = 0;
  // if(m == 1) {
  //     if(k == n - 1) cout << 1;
  //     else cout << 0;
  //     return 0;
  // }
  for (int i = 0; i <= k; ++i) {
    int cur = (m * expo(m - 1, n - i - 1)) % mod;
    cur = (cur * fact[n - 1]) % mod;
    cur = (cur * inv[i]) % mod;
    cur = (cur * inv[n - i - 1]) % mod;
    ans = (ans + cur) % mod;
  }
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

int mod = 998244353;

int expo(int num, int p) {
  if (num == 0) {
    if (p == 0)
      return 1;
    return 0;
  }
  num = num % mod;
  int ret = 1;
  while (p) {
    if (p & 1)
      ret = (ret * num) % mod;
    num = (num * num) % mod;
    p /= 2;
  }
  return ret;
}

signed main() {
  IOS;
  int fact[200005], inv[200005];
  fact[0] = 1;
  for (int i = 1; i < 200005; ++i)
    fact[i] = (fact[i - 1] * i) % mod;
  for (int i = 0; i < 200005; ++i)
    inv[i] = expo(fact[i], mod - 2);
  int n, m, k;
  cin >> n >> m >> k;
  int ans = 0;
  // if(m == 1) {
  //     if(k == n - 1) cout << 1;
  //     else cout << 0;
  //     return 0;
  // }
  for (int i = 0; i <= k; ++i) {
    int cur = (m * expo(m - 1, n - i - 1)) % mod;
    cur = (cur * fact[n - 1]) % mod;
    cur = (cur * inv[i]) % mod;
    cur = (cur * inv[n - i - 1]) % mod;
    ans = (ans + cur) % mod;
  }
  cout << ans << "\n";

  return 0;
}
Problem p02685 (C++)

Buggy submission (status: 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 = 1e5 + 10;

int n, m, K;
ll fact[maxn], invf[maxn];

ll p0w(ll base, int p) {
  ll res = 1;
  while (p) {
    if (p % 2 == 1)
      (res *= base) %= mod;
    (base *= base) %= mod;
    p /= 2;
  }
  return res;
}

ll C(int x, int y) {
  if (y == 0 || x == y)
    return 1;
  return fact[x] * invf[y] % mod * invf[x - y] % mod;
}

int main() {
  std::ios::sync_with_stdio(false);
  invf[0] = fact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * (ll)i % mod;
    invf[i] = p0w(fact[i], mod - 2);
  }
  cin >> n >> m >> K;
  n--;
  int ans = 0;
  rep(i, 0, K + 1) { (ans += C(n, i) * p0w(m - 1, n - i) % mod) %= mod; }
  ans = (ll)ans * (ll)m % mod;
  cout << ans;
  return 0;
}


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 = 3e5 + 10;

int n, m, K;
ll fact[maxn], invf[maxn];

ll p0w(ll base, int p) {
  ll res = 1;
  while (p) {
    if (p % 2 == 1)
      (res *= base) %= mod;
    (base *= base) %= mod;
    p /= 2;
  }
  return res;
}

ll C(int x, int y) {
  if (y == 0 || x == y)
    return 1;
  return fact[x] * invf[y] % mod * invf[x - y] % mod;
}

int main() {
  std::ios::sync_with_stdio(false);
  invf[0] = fact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * (ll)i % mod;
    invf[i] = p0w(fact[i], mod - 2);
  }
  cin >> n >> m >> K;
  n--;
  int ans = 0;
  rep(i, 0, K + 1) { (ans += C(n, i) * p0w(m - 1, n - i) % mod) %= mod; }
  ans = (ll)ans * (ll)m % mod;
  cout << ans;
  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define f(i, l, h) for (ll i = l; i < h; i++)
#define fr(i, h, l) for (ll i = h; i >= l; i--)

using namespace std;

typedef pair<ll, ll> pll;
const ll mod = (ll)(998244353);
typedef vector<ll> vll;
ll fact[(ll)(2e5 + 1)];

ll poww(ll a, ll b) {
  ll res = 1;
  a %= mod;

  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }

  return res;
}

ll C(ll n, ll r) {
  if (n < r) {
    return 0;
  }

  ll lower = n - r, curr = 1;

  while (n > lower) {
    curr = (curr * n) % mod;

    n--;
  }

  return (curr * poww(fact[r], mod - 2)) % mod;
}

int main() {
  // cout << "Hello world!" << endl;
  ll n, m, k;
  cin >> n >> m >> k;

  fact[0] = 1;
  f(i, 1, 2e5 + 1) { fact[i] = (fact[i - 1] * i) % mod; }

  ll temp = 0;
  f(i, 0, k + 1) {
    temp = (temp + (((C(n - 1, i) * m) % mod) * poww(m - 1, n - 1 - i)) % mod) %
           mod;
  }
  cout << temp << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define f(i, l, h) for (ll i = l; i < h; i++)
#define fr(i, h, l) for (ll i = h; i >= l; i--)

using namespace std;

typedef pair<ll, ll> pll;
const ll mod = (ll)(998244353);
typedef vector<ll> vll;
ll fact[(ll)(2e5 + 1)];

ll poww(ll a, ll b) {
  ll res = 1;
  a %= mod;

  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }

  return res;
}

ll C(ll n, ll r) {
  if (n < r) {
    return 0;
  }

  return (((poww(fact[n - r], mod - 2) * poww(fact[r], mod - 2)) % mod) *
          fact[n]) %
         mod;
}

int main() {
  // cout << "Hello world!" << endl;
  ll n, m, k;
  cin >> n >> m >> k;

  fact[0] = 1;
  f(i, 1, 2e5 + 1) { fact[i] = (fact[i - 1] * i) % mod; }

  ll temp = 0;
  f(i, 0, k + 1) {
    temp = (temp + (((C(n - 1, i) * m) % mod) * poww(m - 1, n - 1 - i)) % mod) %
           mod;
  }
  cout << temp << endl;

  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

uint32_t pow_mod(uint32_t base, uint32_t exp, uint32_t mod) {
  if (exp == 1)
    return base % mod;

  uint64_t half = pow_mod(base, exp / 2, mod);
  uint64_t ans = (half * half) % mod;
  return (ans * (exp % 2 == 0 ? 1ll : base)) % mod;
}

uint32_t inv_mod(uint32_t x, uint32_t mod) { return pow_mod(x, mod - 2, mod); }

uint32_t comb_mod(uint32_t n, uint32_t k, const std::vector<uint64_t> &fact_mod,
                  uint32_t mod) {
  if (n < k)
    return -1;
  return (fact_mod[n] * inv_mod((fact_mod[n - k] * fact_mod[k]) % mod, mod)) %
         mod;
}

int main() {
  const uint32_t mod = 998'244'353;
  uint32_t N = 0, M = 0, K = 0;
  std::cin >> N >> M >> K;

  std::vector<uint64_t> fact_mod(N, 1);
  for (size_t i = 1; i < fact_mod.size(); i++)
    fact_mod[i] = (i * fact_mod[i - 1]) % mod;

  uint32_t ans = 0;
  for (size_t k = 0; k < K + 1; k++) {
    uint64_t pow = pow_mod(M - 1, N - 1 - k, mod);
    pow = (pow * M) % mod;
    uint64_t comb = comb_mod(N - 1, k, fact_mod, mod);
    ans = (ans + (pow * comb) % mod) % mod;
  }

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

uint32_t pow_mod(uint32_t base, uint32_t exp, uint32_t mod) {
  if (exp == 0)
    return 1;

  uint64_t half = pow_mod(base, exp / 2, mod);
  uint64_t ans = (half * half) % mod;
  return (ans * (exp % 2 == 0 ? 1ll : base)) % mod;
}

uint32_t inv_mod(uint32_t x, uint32_t mod) { return pow_mod(x, mod - 2, mod); }

uint32_t comb_mod(uint32_t n, uint32_t k, const std::vector<uint64_t> &fact_mod,
                  uint32_t mod) {
  if (n < k)
    return -1;
  return (fact_mod[n] * inv_mod((fact_mod[n - k] * fact_mod[k]) % mod, mod)) %
         mod;
}

int main() {
  const uint32_t mod = 998'244'353;
  uint32_t N = 0, M = 0, K = 0;
  std::cin >> N >> M >> K;

  std::vector<uint64_t> fact_mod(N, 1);
  for (size_t i = 1; i < fact_mod.size(); i++)
    fact_mod[i] = (i * fact_mod[i - 1]) % mod;

  uint32_t ans = 0;
  for (size_t k = 0; k < K + 1; k++) {
    uint64_t pow = pow_mod(M - 1, N - 1 - k, mod);
    pow = (pow * M) % mod;
    uint64_t comb = comb_mod(N - 1, k, fact_mod, mod);
    ans = (ans + (pow * comb) % mod) % mod;
  }

  std::cout << ans << std::endl;

  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define so(v) sort(v.begin(), v.end())
using ll = long long;
using vi = vector<int>;
using vii = vector<vi>;

const int MAX = 510000;
const int MOD = 998244353;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll f(ll n, ll m) {
  // k=0の時の答え
  ll ans = m;
  rep(i, n - 1) {
    ans *= m - 1;
    ans %= MOD;
  }
  return ans;
}
int main() {
  // 前処理
  COMinit();
  ll n, m, k;
  cin >> n >> m >> k;
  ll answer = 0;
  rep(i, k + 1) {
    answer += (COM(n - 1, i) % MOD) * (f(n - i, m) % MOD) % MOD;
    answer %= MOD;
  }
  cout << answer << endl;
  // cout<<COM(n-1,1)%MOD<<endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define so(v) sort(v.begin(), v.end())
using ll = long long;
using vi = vector<int>;
using vii = vector<vi>;

const int MAX = 510000;
const int MOD = 998244353;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll f(ll n, ll m) {
  // k=0の時の答え
  ll ans = m;
  rep(i, n - 1) {
    ans *= m - 1;
    ans %= MOD;
  }
  return ans;
}
int main() {
  // 前処理
  COMinit();
  ll n, m, k;
  cin >> n >> m >> k;
  ll answer = 0;
  ll ff = f(n - k, m);
  for (int i = k; i >= 0; i--) {
    answer += (COM(n - 1, i) % MOD) * (ff % MOD) % MOD;
    ff = (ff * (m - 1)) % MOD;
    answer %= MOD;
  }
  cout << answer << endl;
  // cout<<COM(n-1,1)%MOD<<endl;
}
Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> P(0);
  vector<pair<int, int>> M(0);
  int l, r, pp, pm, pos, n, b, h;
  string s;
  bool flgL = true, flgR = true;
  for (int i = 0; i < N; i++) {
    cin >> s;
    b = 0, h = 0;
    n = s.size();
    for (int j = 0; j < n; j++) {
      if (s[i] == '(') {
        h++;
      } else {
        h--;
        if (b > h) {
          b = h;
        }
      }
    }
    if (h < 0) {
      M.push_back(make_pair(b - h, -h));
    } else if (h == 0) {
      if (b < 0) {
        M.push_back(make_pair(b - h, -h));
      } else {
        P.push_back(make_pair(b, h));
      }
    } else {
      P.push_back(make_pair(b, h));
    }
  }
  sort(P.rbegin(), P.rend());
  sort(M.rbegin(), M.rend());
  l = P.size(), r = M.size();
  pp = 0, pm = 0;
  for (int i = 0; i < l; i++) {
    if (P[i].first + pp < 0) {
      flgL = false;
      break;
    } else {
      pp += P[i].second;
    }
  }
  for (int i = 0; i < r; i++) {
    if (M[i].first + pm < 0) {
      flgR = false;
      break;
    } else {
      pm += M[i].second;
    }
  }
  if (flgL && flgR && pp == pm) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> P(0);
  vector<pair<int, int>> M(0);
  int l, r, pp, pm, pos, n, b, h;
  string s;
  bool flgL = true, flgR = true;
  for (int i = 0; i < N; i++) {
    cin >> s;
    b = 0, h = 0;
    n = s.size();
    for (int j = 0; j < n; j++) {
      if (s[j] == '(') {
        h++;
      } else {
        h--;
        if (b > h) {
          b = h;
        }
      }
    }
    if (h < 0) {
      M.push_back(make_pair(b - h, -h));
    } else if (h == 0) {
      if (b < 0) {
        M.push_back(make_pair(b - h, -h));
      } else {
        P.push_back(make_pair(b, h));
      }
    } else {
      P.push_back(make_pair(b, h));
    }
  }
  sort(P.rbegin(), P.rend());
  sort(M.rbegin(), M.rend());
  l = P.size(), r = M.size();
  pp = 0, pm = 0;
  for (int i = 0; i < l; i++) {
    if (P[i].first + pp < 0) {
      flgL = false;
      break;
    } else {
      pp += P[i].second;
    }
  }
  for (int i = 0; i < r; i++) {
    if (M[i].first + pm < 0) {
      flgR = false;
      break;
    } else {
      pm += M[i].second;
    }
  }
  if (flgL && flgR && pp == pm) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;

  // 最下点、最終的な和
  vector<pair<int, int>> p_plus;  // 最終的な和がプラスのもの
  vector<pair<int, int>> p_minus; // 最終的な和がマイナスのもの
  int sum = 0;
  rep(i, n) {
    string s;
    cin >> s;
    pair<int, int> p(0, 0);
    rep(i, s.size()) {
      if (s[i] == '(')
        p.second++;
      else {
        p.second--;
        chmin(p.first, p.second);
      }
    }
    sum += p.second;
    if (p.second >= 0)
      p_plus.push_back(p);
    else
      p_minus.push_back(p);
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  int now_pos = 0;
  sort(p_plus.begin(), p_plus.end(), greater<pair<int, int>>());
  rep(i, p_plus.size()) {
    if (now_pos + p_plus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_plus[i].second;
  }

  now_pos = 0;
  rep(i, p_minus.size()) {
    p_minus[i].second *= -1;
    p_minus[i].first += p_minus[i].second;
  }
  sort(p_minus.begin(), p_minus.end(), greater<pair<int, int>>());
  rep(i, p_plus.size()) {
    if (now_pos + p_minus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_minus[i].second;
  }
  cout << "Yes" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;

  // 最下点、最終的な和
  vector<pair<int, int>> p_plus;  // 最終的な和がプラスのもの
  vector<pair<int, int>> p_minus; // 最終的な和がマイナスのもの
  int sum = 0;
  rep(i, n) {
    string s;
    cin >> s;
    pair<int, int> p(0, 0);
    rep(i, s.size()) {
      if (s[i] == '(')
        p.second++;
      else {
        p.second--;
        chmin(p.first, p.second);
      }
    }
    sum += p.second;
    if (p.second >= 0)
      p_plus.push_back(p);
    else
      p_minus.push_back(p);
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  int now_pos = 0;
  sort(p_plus.begin(), p_plus.end(), greater<pair<int, int>>());
  rep(i, p_plus.size()) {
    if (now_pos + p_plus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_plus[i].second;
  }

  now_pos = 0;
  rep(i, p_minus.size()) {
    p_minus[i].second *= -1;
    p_minus[i].first += p_minus[i].second;
  }
  sort(p_minus.begin(), p_minus.end(), greater<pair<int, int>>());
  rep(i, p_minus.size()) {
    if (now_pos + p_minus[i].first < 0) {
      cout << "No" << endl;
      return 0;
    }
    now_pos += p_minus[i].second;
  }
  cout << "Yes" << endl;
}
Problem p02686 (C++)

Buggy submission (status: 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[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;
}


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[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;
}
Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define DEBUG

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 998244353, maxn = 200200;
const double PI = acos(-1);

bool comp(pii a, pii b) { return a.ss > b.ss; }

int main() {
  int n;
  string s;
  bool can = true;
  cin >> n;

  vector<pii> v1, v2;

  for (int i = 0; i < n; i++) {
    cin >> s;
    int tam = s.size(), a = 0, b = 0, q = 0;

    for (int j = 0; j < tam; j++) {
      if (s[i] == ')') {
        if (q > 0)
          q--;
        else
          a++;
      } else
        ++q;
    }
    b = q;
    if (a < b)
      v1.emplace_back(a, b);
    else
      v2.emplace_back(a, b);
  }

  sort(all(v1));
  sort(all(v2), comp);

  int q = 0;

  for (auto x : v1) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }

  for (auto x : v2) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }
  if (q != 0)
    can = false;

  if (can) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define DEBUG

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 998244353, maxn = 200200;
const double PI = acos(-1);

bool comp(pii a, pii b) { return a.ss > b.ss; }

int main() {
  int n;
  string s;
  bool can = true;
  cin >> n;

  vector<pii> v1, v2;

  for (int i = 0; i < n; i++) {
    cin >> s;
    int tam = s.size(), a = 0, b = 0, q = 0;

    for (int j = 0; j < tam; j++) {
      if (s[j] == ')') {
        if (q > 0)
          q--;
        else
          a++;
      } else
        ++q;
    }
    b = q;
    if (a < b)
      v1.emplace_back(a, b);
    else
      v2.emplace_back(a, b);
  }

  sort(all(v1));
  sort(all(v2), comp);

  int q = 0;

  for (auto x : v1) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }

  for (auto x : v2) {
    q -= x.ff;
    if (q < 0)
      can = false;
    q += x.ss;
  }
  if (q != 0)
    can = false;

  if (can) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;

struct X {
  int y, z;
  int id, tp;
} s[maxn];
int n;
char t[maxn];

bool cmp(const X &a, const X &b) {
  if (a.tp != b.tp)
    return a.tp < b.tp;
  if (a.tp == 2) {
    if (a.y != b.y)
      return a.y < b.y;
    return a.z > b.z;
  } else if (a.tp == 3) {
    return a.z > b.z;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", t);
    int z = 0, y = 0;
    for (int j = 0; t[j]; j++) {
      if (t[j] == '(')
        z++;
      else {
        if (z)
          z--;
        else
          y++;
      }
    }
    s[i].y = y;
    s[i].z = z;
    s[i].id = i;
    /* ))) y个 ((( z个 */
    if (y + z == 0)
      s[i].tp = 0;
    else if (y == 0 && z != 0)
      s[i].tp = 1;
    else if (y != 0 && z == 0)
      s[i].tp = 4;
    else if (z - y >= 0)
      s[i].tp = 2;
    else
      s[i].tp = 3;
  }
  sort(s, s + n, cmp);
  int sum = 0;
  int fail = 0;
  for (int i = 0; i < n; i++) {
    sum = sum - s[i].y;
    if (sum < 0) {
      fail = 1;
      break;
    }
    sum = sum + s[i].z;
  }
  if (fail || sum != 0) {
    printf("No\n");
  } else {
    printf("Yes\n");
  }
  return 0;
}

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

const int maxn = 1e6 + 10;

struct X {
  int y, z;
  int id, tp;
} s[maxn];
int n;
char t[maxn];

bool cmp(const X &a, const X &b) {
  if (a.tp != b.tp)
    return a.tp < b.tp;
  if (a.tp == 2) {
    if (a.y != b.y)
      return a.y < b.y;
    return a.z > b.z;
  } else if (a.tp == 3) {
    return a.z > b.z;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", t);
    int z = 0, y = 0;
    for (int j = 0; t[j]; j++) {
      if (t[j] == '(')
        z++;
      else {
        if (z)
          z--;
        else
          y++;
      }
    }
    s[i].y = y;
    s[i].z = z;
    s[i].id = i;
    /* ))) y个 ((( z个 */
    if (y + z == 0)
      s[i].tp = 0;
    else if (y == 0 && z != 0)
      s[i].tp = 1;
    else if (y != 0 && z == 0)
      s[i].tp = 4;
    else if (z - y >= 0)
      s[i].tp = 2;
    else
      s[i].tp = 3;
  }
  sort(s, s + n, cmp);
  int sum = 0;
  int fail = 0;
  for (int i = 0; i < n; i++) {
    sum = sum - s[i].y;
    if (sum < 0) {
      fail = 1;
      break;
    }
    sum = sum + s[i].z;
  }
  if (fail || sum != 0) {
    printf("No\n");
  } else {
    printf("Yes\n");
  }
  return 0;
}
Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
using namespace std;
const int maxn = 1100;
string a[maxn];
int arr[maxn], n;

vector<pair<int, int>> v;

int main() {
  cin >> n;

  int minind = -1, maxind = -1;
  int minbal = INT_MAX, maxbal = INT_MIN;

  for (int i = 0; i < n; i++) {
    cin >> a[i];

    int bal = 0;
    for (char j : a[i]) {
      if (j == '(')
        bal--;
      else
        bal++;
    }

    if (a[i][0] == '(' && bal < minbal) {
      minbal = bal;
      minind = i;
    } else if (a[i][sz(a[i]) - 1] == ')') {
      if (bal > maxbal) {
        maxbal = bal;
        maxind = i;
      }
    }
  }

  if (minind == -1 || maxind == -1) {
    cout << "No\n";
    return 0;
  }

  arr[0] = minind;
  arr[n - 1] = maxind;

  for (int i = 0; i < n; i++) {
    if (i == minind || i == maxind) {
      continue;
    }

    int bal = 0;
    for (char j : a[i]) {
      if (j == '(')
        bal--;
      else
        bal++;
    }

    v.pb(mp(bal, i));
  }

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

  for (int i = 0; i < n - 2; i++) {
    arr[i + 1] = v[i].second;
  }

  int bal = 0;
  for (int i = 0; i < n; i++) {
    for (char j : a[arr[i]]) {
      if (j == '(')
        bal--;
      else
        bal++;

      if (bal > 0) {
        cout << "No\n";
        return 0;
      }
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
using namespace std;
const int maxn = 1000100;
string a[maxn];
int arr[maxn], n;

vector<pair<int, int>> v;

int main() {
  cin >> n;

  int minind = -1, maxind = -1;
  int minbal = INT_MAX, maxbal = INT_MIN;

  for (int i = 0; i < n; i++) {
    cin >> a[i];

    int bal = 0;
    for (char j : a[i]) {
      if (j == '(')
        bal--;
      else
        bal++;
    }

    if (a[i][0] == '(' && bal < minbal) {
      minbal = bal;
      minind = i;
    } else if (a[i][sz(a[i]) - 1] == ')') {
      if (bal > maxbal) {
        maxbal = bal;
        maxind = i;
      }
    }
  }

  if (minind == -1 || maxind == -1) {
    cout << "No\n";
    return 0;
  }

  arr[0] = minind;
  arr[n - 1] = maxind;

  for (int i = 0; i < n; i++) {
    if (i == minind || i == maxind) {
      continue;
    }

    int bal = 0;
    for (char j : a[i]) {
      if (j == '(')
        bal--;
      else
        bal++;
    }

    v.pb(mp(bal, i));
  }

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

  for (int i = 0; i < n - 2; i++) {
    arr[i + 1] = v[i].second;
  }

  int bal = 0;
  for (int i = 0; i < n; i++) {
    for (char j : a[arr[i]]) {
      if (j == '(')
        bal--;
      else
        bal++;

      if (bal > 0) {
        cout << "No\n";
        return 0;
      }
    }
  }

  if (bal != 0) {
    cout << "No\n";
  } else {
    cout << "Yes\n";
  }
}
Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

struct num {
  int a;
  int b;
  bool operator<(const num &right) const {
    if (b - a >= 0) {
      if (right.b - right.a <= 0)
        return true;
      if (a == right.a)
        return b > right.b;
      return a < right.a;
    }
    if (right.b - right.a >= 0)
      return false;
    return b > right.b;
  }
};

int main(void) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  // A, B
  vector<num> numV(N);
  for (int i = 0; i < N; i++) {
    string s = S[i];
    int a = 0, b = 0;
    for (unsigned int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        b++;
      else {
        if (b == 0)
          a++;
        else
          b--;
      }
    }
    num n = {a, b};
    numV[i] = n;
  }
  sort(numV.begin(), numV.end());
  //  check
  ll total_b = 0;
  for (int i = 0; i < N; i++) {
    total_b -= ll(numV[i].a);
    if (total_b < 0) {
      cout << "No" << endl;
      return 0;
    }
    total_b += ll(numV[i].b);
  }
  if (total_b == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

struct num {
  int a;
  int b;
  bool operator<(const num &right) const {
    if (b - a >= 0) {
      if (right.b - right.a < 0)
        return true;
      if (a == right.a)
        return b > right.b;
      return a < right.a;
    }
    if (right.b - right.a >= 0)
      return false;
    return b > right.b;
  }
};

int main(void) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  // A, B
  vector<num> numV(N);
  for (int i = 0; i < N; i++) {
    string s = S[i];
    int a = 0, b = 0;
    for (unsigned int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        b++;
      else {
        if (b == 0)
          a++;
        else
          b--;
      }
    }
    num n = {a, b};
    numV[i] = n;
  }
  sort(numV.begin(), numV.end());
  //  check
  ll total_b = 0;
  for (int i = 0; i < N; i++) {
    total_b -= ll(numV[i].a);
    if (total_b < 0) {
      cout << "No" << endl;
      return 0;
    }
    total_b += ll(numV[i].b);
  }
  if (total_b == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02686 (C++)

Buggy submission (status: 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[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;
}

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[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;
}