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

Buggy submission (status: Time Limit Exceeded):
//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> ds(n);
  cin >> ds;
  if (ds[0]) {
    cout << 0 << '\n';
    return 0;
  }
  sort(ds.begin(), ds.end());
  int len = 1;
  ll ans = 1;
  int prev = 1;
  for (int i = 1; i < n;) {
    int cnt = 0;
    while (i < n && ds[i] == len) {
      i++;
      cnt++;
      ans = ans * prev % 998244353;
    }
    printde(i, cnt, len, ans);
    len++;
    prev = cnt;
  }
  cout << ans << '\n';
}


Fixed submission:
//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> ds(n);
  cin >> ds;
  if (ds[0]) {
    cout << 0 << '\n';
    return 0;
  }
  sort(ds.begin(), ds.end());
  int len = 1;
  ll ans = 1;
  int prev = 1;
  for (int i = 1; i < n;) {
    int cnt = 0;
    while (i < n && ds[i] == len) {
      i++;
      cnt++;
      ans = ans * prev % 998244353;
    }
    if (cnt == 0) {
      ans = 0;
      break;
    }
    printde(i, cnt, len, ans);
    len++;
    prev = cnt;
  }
  cout << ans << '\n';
}
Problem p02866 (C++)

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

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1'000'000'000'000'007ll;
const llong mod = 998244353;

llong mpow(llong n, llong m) {
  if (m == 1)
    return n;

  llong ret = mpow(n, m / 2);
  ret = ret * ret % mod;
  if (m & 1)
    ret = ret * n % mod;

  return ret;
}

int main() {
  llong N;
  vector<llong> D;
  map<llong, llong> m;

  cin >> N;
  D.resize(N);
  llong maxi = -inf;
  for (auto &in : D) {
    cin >> in;
    ++m[in];
    maxi = max(maxi, in);
  }

  llong ans = 1;
  if (!(m[0] == 1 && D[0] == 0)) {
    ans = 0;
  }
  for (int i = 1; i <= maxi; ++i) {
    ans = ans * mpow(m[i - 1], m[i]) % mod;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1'000'000'000'000'007ll;
const llong mod = 998244353;

llong mpow(llong n, llong m) {
  if (m == 0)
    return 0;
  if (m == 1)
    return n;

  llong ret = mpow(n, m / 2);
  ret = ret * ret % mod;
  if (m & 1)
    ret = ret * n % mod;

  return ret;
}

int main() {
  llong N;
  vector<llong> D;
  map<llong, llong> m;

  cin >> N;
  D.resize(N);
  llong maxi = -inf;
  for (auto &in : D) {
    cin >> in;
    ++m[in];
    maxi = max(maxi, in);
  }

  llong ans = 1;
  if (!(m[0] == 1 && D[0] == 0)) {
    ans = 0;
  }
  for (int i = 1; i <= maxi; ++i) {
    ans = ans * mpow(m[i - 1], m[i]) % mod;
  }

  cout << ans << endl;

  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 998244353;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

// 繰り返し二乗法
ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> d(n);
  for (ll i = 0; i < n; i++) {
    cin >> d[i];
    if (i != 0 && d[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (d[0] != 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> cnt(n, 0);
  for (ll i = 0; i < n; i++) {
    cnt[d[i]]++;
  }
  ll l;
  for (ll i = 1; i < n; i++) {
    if (cnt[i] == 0) {
      l = i;
      for (ll j = i + 1; j < n; j++) {
        if (cnt[j] != 0) {
          cout << 0 << endl;
          return 0;
        }
      }
    }
  }
  ll ans = 1;
  for (ll i = 1; i < min(l, n); i++) {
    ans *= mod_pow(cnt[i - 1] % mod, cnt[i]);
    ans %= mod;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 998244353;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

// 繰り返し二乗法
ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> d(n);
  for (ll i = 0; i < n; i++) {
    cin >> d[i];
    if (i != 0 && d[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (d[0] != 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> cnt(n, 0);
  for (ll i = 0; i < n; i++) {
    cnt[d[i]]++;
  }
  ll l;
  for (ll i = 1; i < n; i++) {
    if (cnt[i] == 0) {
      l = i;
      for (ll j = i + 1; j < n; j++) {
        if (cnt[j] != 0) {
          cout << 0 << endl;
          return 0;
        }
        break;
      }
    }
  }
  ll ans = 1;
  for (ll i = 1; i < min(l, n); i++) {
    ans *= mod_pow(cnt[i - 1] % mod, cnt[i]);
    ans %= mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p02866 (C++)

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

typedef long double ld;
typedef long long ll;

using namespace std;

const ll p = 998244353;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  freopen("input.txt", "r", stdin);
  cout << setprecision(20);
  int n;
  cin >> n;
  vector<int> a(n, 0);
  int det;
  cin >> det;
  a[det]++;
  for (int i = 1; i < n; i++) {
    int x;
    cin >> x;
    a[x]++;
  }

  if (a[0] != 1 || det != 0) {
    cout << "0";
    return 0;
  }
  ll ans = 1;
  for (int i = 1; i < n; i++) {
    ll pw = 1;
    for (int j = 0; j < a[i]; j++) {
      pw *= a[i - 1];
      pw %= p;
    }
    ans *= pw;
    ans %= p;
  }
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef long double ld;
typedef long long ll;

using namespace std;

const ll p = 998244353;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  // freopen("input.txt", "r", stdin);
  cout << setprecision(20);
  int n;
  cin >> n;
  vector<int> a(n, 0);
  int det;
  cin >> det;
  a[det]++;
  for (int i = 1; i < n; i++) {
    int x;
    cin >> x;
    a[x]++;
  }

  if (a[0] != 1 || det != 0) {
    cout << "0";
    return 0;
  }
  ll ans = 1;
  for (int i = 1; i < n; i++) {
    ll pw = 1;
    for (int j = 0; j < a[i]; j++) {
      pw *= a[i - 1];
      pw %= p;
    }
    ans *= pw;
    ans %= p;
  }
  cout << ans;

  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int N;
  cin >> N;
  int D[100050];
  for (int i = 1; i <= N; i++)
    cin >> D[i];

  int bucket_max = 0;
  int bucket[100050];
  for (int i = 1; i <= N; i++) {
    bucket_max = (D[i] > bucket_max) ? D[i] : bucket_max;
    bucket[D[i]]++;
  }

  if (!(bucket[0] == 1 && D[1] == 0)) {
    cout << 0 << endl;
    return 0;
  }

  long ans = 1;
  for (int i = 1; i <= bucket_max; i++) {
    for (int k = 0; k < bucket[i]; k++) {
      ans = ans * bucket[i - 1] % 998244353;
    }
  }

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


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N;
  cin >> N;
  int D[100050];
  for (int i = 1; i <= N; i++)
    cin >> D[i];

  int bucket_max = 0;
  int bucket[100050] = {};
  for (int i = 1; i <= N; i++) {
    bucket_max = (D[i] > bucket_max) ? D[i] : bucket_max;
    bucket[D[i]]++;
  }

  if (!(bucket[0] == 1 && D[1] == 0)) {
    cout << 0 << endl;
    return 0;
  }

  long ans = 1;
  for (int i = 1; i <= bucket_max; i++) {
    for (int k = 0; k < bucket[i]; k++) {
      ans = ans * bucket[i - 1] % 998244353;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#define ll long long
#define mod 998244353
using namespace std;
int n, x;
int cnt[1000010];
ll ans = 1;
ll ksm(ll x, ll i) {
  ll res = 1;
  for (; i; i >>= 1, x = x * x % mod)
    if (i & 1)
      res = res * x % mod;
  return res;
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (i == 1 && x != 0) {
      printf("0");
      return 0;
    }
    cnt[x]++;
  }
  if (cnt[0] != 1) {
    printf("0");
    return 0;
  }
  for (register int i = 1; i <= 100000; i++) {
    if (!cnt[i]) {
      for (register int j = i + 1; j <= 100000; j++) {
        if (cnt[j]) {
          printf("0");
          return 0;
        }
      }
    }
    ans = ans * ksm(cnt[i - 1], cnt[i]) % mod;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define ll long long
#define mod 998244353
using namespace std;
int n, x;
int cnt[1000010];
ll ans = 1;
ll ksm(ll x, ll i) {
  ll res = 1;
  for (; i; i >>= 1, x = x * x % mod)
    if (i & 1)
      res = res * x % mod;
  return res;
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (i == 1 && x != 0) {
      printf("0");
      return 0;
    }
    cnt[x]++;
  }
  if (cnt[0] != 1) {
    printf("0");
    return 0;
  }
  for (register int i = 1; i <= 100000; i++) {
    if (!cnt[i]) {
      for (register int j = i + 1; j <= 100000; j++) {
        if (cnt[j]) {
          printf("0");
          return 0;
        }
      }
      break;
    }
    ans = ans * ksm(cnt[i - 1], cnt[i]) % mod;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int64_t modpow(int64_t a, int64_t b, int64_t MOD) {
  int64_t ret = 1;
  while (b > 0) {
    if (b & 1) {
      (ret *= a) %= MOD;
    }
    (a *= a) %= MOD;
    b >>= 1;
  }
  return ret % MOD;
}

int main() {
  const Int MOD = 998244353;
  int N;
  cin >> N;
  vector<Int> D(N);
  vector<Int> c(100010, 0);
  for (int i = 0; i < N; ++i) {
    cin >> D[i];
    ++c[D[i]];
  }

  Int ans = 0;
  if (D[0] == 0 && c[0] == 1) {
    ans = 1;
  }
  for (int i = 1; i <= *max_element(D.begin(), D.end()); ++i) {
    (ans *= modpow(c[i - 1], c[i], MOD)) %= MOD;
  }
  cout << ans % MOD << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int64_t modpow(int64_t a, int64_t b, int64_t MOD) {
  int64_t ret = 1;
  while (b > 0) {
    if (b & 1) {
      (ret *= a) %= MOD;
    }
    (a *= a) %= MOD;
    b >>= 1;
  }
  return ret % MOD;
}

int main() {
  const Int MOD = 998244353;
  int N;
  cin >> N;
  vector<Int> D(N);
  vector<Int> c(100010, 0);
  for (int i = 0; i < N; ++i) {
    cin >> D[i];
    ++c[D[i]];
  }

  Int ans = 0;
  if (D[0] == 0 && c[0] == 1) {
    ans = 1;
  }
  int max_d = 0;
  for (int i = 0; i < N; ++i) {
    max_d = max(max_d, (int)D[i]);
  }
  for (int i = 1; i <= max_d; ++i) {
    if (c[i - 1] <= 0 || c[i] <= 0) {
      ans = 0;
      break;
    }
    (ans *= modpow(c[i - 1], c[i], MOD)) %= MOD;
  }
  cout << ans % MOD << endl;

  return 0;
}
Problem p02866 (C++)

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

// @author: pashka

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

  int n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> d(n);
  for (int i = 0; i < n; i++)
    d[a[i]]++;
  if (a[0] != 0 || d[0] != 1) {
    cout << 0;
    return 0;
  }

  long res = 1;
  for (int i = 1; i < n; i++) {
    int x = a[i];
    res *= d[x - 1];
    res %= 998244353;
  }
  cout << res << "\n";

  return 0;
}

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

// @author: pashka

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> d(n);
  for (int i = 0; i < n; i++)
    d[a[i]]++;
  if (a[0] != 0 || d[0] != 1) {
    cout << 0;
    return 0;
  }

  long res = 1;
  for (int i = 1; i < n; i++) {
    int x = a[i];
    res *= d[x - 1];
    res %= 998244353;
  }
  cout << res << "\n";

  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define N_MAX 100001
#define MOD 998244353

long long int intpow(int base, int p) {
  if (1 == p) {
    return base;
  }
  if (0 == p % 2) {
    long long int val = intpow(base, p / 2);
    return (val * val) % MOD;
  } else {
    return (intpow(base, p - 1) * (long long int)base) % MOD;
  }
}

void func() {
  int N;
  scanf("%d", &N);
  int D0;
  scanf("%d", &D0);
  if (0 != D0) {
    printf("0");
    return;
  }

  int table[N_MAX];
  int maxdist = 0;
  fill(table, table + N, 0);
  table[0] = 1;
  for (int i = 1; i < N; i++) {
    int d;
    scanf("%d", &d);
    table[d]++;
    maxdist = max(maxdist, d);
  }

  long long int ret = 1;
  if (1 != table[0]) {
    printf("0");
    return;
  }
  for (int i = 1; i <= maxdist; i++) {
    ret = (ret * intpow(table[i - 1], table[i])) % MOD;
  }

  printf("%lld", ret);

  return;
}

int main() {
  FILE *fin = NULL, *fout = NULL;
  // fin = freopen("input.txt", "r", stdin);
  // fout = freopen("output.txt", "w", stdout);

  func();

  // finalize
  if (NULL != fin)
    fclose(fin);
  if (NULL != fout)
    fclose(fout);

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define N_MAX 100001
#define MOD 998244353

long long int intpow(int base, int p) {
  if (0 == p) {
    return 1;
  }
  if (1 == p) {
    return base;
  }
  if (0 == p % 2) {
    long long int val = intpow(base, p / 2);
    return (val * val) % MOD;
  } else {
    return (intpow(base, p - 1) * (long long int)base) % MOD;
  }
}

void func() {
  int N;
  scanf("%d", &N);
  int D0;
  scanf("%d", &D0);
  if (0 != D0) {
    printf("0");
    return;
  }

  int table[N_MAX];
  int maxdist = 0;
  fill(table, table + N, 0);
  table[0] = 1;
  for (int i = 1; i < N; i++) {
    int d;
    scanf("%d", &d);
    table[d]++;
    maxdist = max(maxdist, d);
  }

  long long int ret = 1;
  if (1 != table[0]) {
    printf("0");
    return;
  }
  for (int i = 1; i <= maxdist; i++) {
    ret = (ret * intpow(table[i - 1], table[i])) % MOD;
  }

  printf("%lld", ret);

  return;
}

int main() {
  FILE *fin = NULL, *fout = NULL;
  // fin = freopen("input.txt", "r", stdin);
  // fout = freopen("output.txt", "w", stdout);

  func();

  // finalize
  if (NULL != fin)
    fclose(fin);
  if (NULL != fout)
    fclose(fout);

  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

// a^n mod p
// n > 0
// #include <cmath>
long long modpow(long long a, long long n, long long p) {
  a %= p;
  if (a == 0)
    return 0;
  if (a == 1)
    return 1;
  long long b = 1, cnt = 0;
  while (b < p && cnt < n) {
    b *= a;
    cnt++;
  }
  if (b < p)
    return b;
  b %= p;
  return modpow(b, n / cnt, p) * (long long)pow(a, n % cnt) % p;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // const ll p = 1e9 + 7;
  const ll p = 998244353;

  ll n;
  cin >> n;
  vll d(n);
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> d[i];
    mp[d[i]]++;
  }
  if (mp[0] != 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 1;
  for (ll i = 1; i <= (mp.end()->first); i++) {
    ans *= modpow(mp[i - 1], mp[i], p);
    ans %= p;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

// a^n mod p
// n > 0
// #include <cmath>
long long modpow(long long a, long long n, long long p) {
  a %= p;
  if (a == 0)
    return 0;
  if (a == 1)
    return 1;
  long long b = 1, cnt = 0;
  while (b < p && cnt < n) {
    b *= a;
    cnt++;
  }
  if (b < p)
    return b;
  b %= p;
  return modpow(b, n / cnt, p) * (long long)pow(a, n % cnt) % p;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // const ll p = 1e9 + 7;
  const ll p = 998244353;

  ll n;
  cin >> n;
  vll d(n);
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> d[i];
    mp[d[i]]++;
  }
  if (mp[0] != 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 1;
  for (ll i = 1; i < n; i++) {
    ans *= mp[d[i] - 1];
    ans %= p;
  }
  cout << ans << endl;
}
Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

const ll mod = 998244353;

int main(void) {

  ll n, i, j, d, ans, dmax = -1;
  ll dcnt[100010];

  for (i = 0; i < n; i++) {
    dcnt[i] = 0;
  }

  cin >> n;

  cin >> d;
  if (d != 0) {
    cout << 0 << endl;
    return 0;
  }
  dcnt[d]++;

  for (i = 1; i < n; i++) {
    cin >> d;
    if (dmax < d)
      dmax = d;
    dcnt[d]++;
  }

  if (dcnt[0] != 1) {
    cout << 0 << endl;
    return 0;
  }

  ans = 1;
  for (i = 1; i <= dmax; i++) {
    if (dcnt[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
    for (j = 0; j < dcnt[i]; j++) {
      ans *= dcnt[i - 1];
      ans %= mod;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

const ll mod = 998244353;

int main(void) {

  ll n, i, j, d, ans, dmax = -1;
  ll dcnt[100010] = {};

  cin >> n;

  cin >> d;
  if (d != 0) {
    cout << 0 << endl;
    return 0;
  }
  dcnt[d]++;

  for (i = 1; i < n; i++) {
    cin >> d;
    if (dmax < d)
      dmax = d;
    dcnt[d]++;
  }

  if (dcnt[0] != 1) {
    cout << 0 << endl;
    return 0;
  }

  ans = 1;
  for (i = 1; i <= dmax; i++) {
    if (dcnt[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
    for (j = 0; j < dcnt[i]; j++) {
      ans *= dcnt[i - 1];
      ans %= mod;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02866 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define mod 1000000007 // 10^9+7
#define div 998244353
#define rep(i, n) for (int i = 0; i < n; i++)
int n, cnt = 1, ans = 1, maxl = 0;
int d[200000];
map<int, int> mp;
int my_pow(int a, int b) {
  if (b == 1)
    return a;
  if (b % 2)
    return (my_pow(a, b / 2) * my_pow(a, (b / 2) + 1)) % div;
  return (my_pow(a, b / 2) * my_pow(a, b / 2)) % div;
}
signed main() {
  cin >> n;
  rep(i, n) {
    cin >> d[i];
    mp[d[i]]++;
    maxl = max(maxl, d[i]);
  }
  if (d[0] != 0 || mp[0] > 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= maxl; i++) {
    ans *= my_pow(mp[i - 1], mp[i]);
    ans %= div;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define mod 1000000007 // 10^9+7
#define div 998244353
#define rep(i, n) for (int i = 0; i < n; i++)
int n, cnt = 1, ans = 1, maxl = 0;
int d[200000];
map<int, int> mp;
int my_pow(int a, int b) {
  if (b == 1)
    return a;
  if (b % 2)
    return (my_pow(a, b / 2) * my_pow(a, (b / 2) + 1)) % div;
  return (my_pow(a, b / 2) * my_pow(a, b / 2)) % div;
}
signed main() {
  cin >> n;
  rep(i, n) {
    cin >> d[i];
    mp[d[i]]++;
    maxl = max(maxl, d[i]);
  }
  if (d[0] != 0 || mp[0] > 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= maxl; i++) {
    if (mp[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
    ans *= my_pow(mp[i - 1], mp[i]);
    ans %= div;
  }
  cout << ans << endl;
  return 0;
}
Problem p02866 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())
Dlist = list(map(int, input().split()))
DCounter = Counter(Dlist)
if DCounter[0] == 1 and Dlist[0] == 0:
    num = 1
    mae = 1
    for key, value in sorted(DCounter.items()[1:]):
        num *= mae**value
        mae = value
        num %= 998244353
    print(num % 998244353)
else:
    print(0)


Fixed submission:
from collections import Counter

N = int(input())
Dlist = list(map(int, input().split()))
DCounter = Counter(Dlist)
if DCounter[0] == 1 and Dlist[0] == 0:
    num = 1
    for i in range(1, max(Dlist) + 1):
        num *= DCounter[i - 1] ** DCounter[i]

    print(num % 998244353)
else:
    print(0)
Problem p02866 (Python)

Buggy submission (status: Runtime Error):
LARGE = 998244353


def solve(n, d_list):
    cnt = [0] * (max(d_list) + 1)
    for i in range(n):
        cnt[d_list[i]] += 1
    if cnt[0] != 1 or d_list[0] != 0:
        return 0
    res = 1
    for i in range(max(d_list)):
        res *= pow(cnt[i], cnt[i + 1], LARGE)
        res %= LARGE
    return res


def main():
    n = int(input())
    d_list = list(map(int(input())))
    res = solve(n, d_list)
    print(res)


def test():
    assert solve(4, [0, 1, 1, 2]) == 2
    assert solve(4, [1, 1, 1, 1]) == 0
    assert solve(7, [0, 3, 2, 1, 2, 2, 1]) == 24


if __name__ == "__main__":
    test()
    main()


Fixed submission:
LARGE = 998244353


def solve(n, d_list):
    cnt = [0] * (max(d_list) + 1)
    for i in range(n):
        cnt[d_list[i]] += 1
    if cnt[0] != 1 or d_list[0] != 0:
        return 0
    res = 1
    for i in range(max(d_list)):
        res *= pow(cnt[i], cnt[i + 1], LARGE)
        res %= LARGE
    return res


def main():
    n = int(input())
    d_list = list(map(int, input().split()))
    res = solve(n, d_list)
    print(res)


def test():
    assert solve(4, [0, 1, 1, 2]) == 2
    assert solve(4, [1, 1, 1, 1]) == 0
    assert solve(7, [0, 3, 2, 1, 2, 2, 1]) == 24


if __name__ == "__main__":
    test()
    main()
Problem p02866 (Python)

Buggy submission (status: Runtime Error):
import collections


def mod_pow(a, n, mod):
    result = 1
    a_n = a
    while n > 0:
        if n & 1:
            result = result * a_n % mod
        a_n = a_n * a_n % mod
        n >>= 1
    return result


def main(n, d):
    MOD = 998244353
    counter = collections.Counter(d)
    if counter[0] != 1:
        return 0
    if 0 in counter.values:
        return 0
    pows = [
        mod_pow(a=counter[i - 1], n=counter[i], mod=MOD)
        for i in range(1, max(counter.keys()) + 1)
    ]
    patterns = 1
    for p in pows:
        patterns *= p
        patterns %= MOD
    return patterns


if __name__ == "__main__":
    N = int(input())
    D = list(map(int, input().split()))
    print(main(n=N, d=D))


Fixed submission:
import collections


def mod_pow(a, n, mod):
    result = 1
    a_n = a
    while n > 0:
        if n & 1:
            result = result * a_n % mod
        a_n = a_n * a_n % mod
        n >>= 1
    return result


def main(n, d):
    MOD = 998244353
    counter = collections.Counter(d)
    if counter[0] != 1 or d[0] != 0:
        return 0
    pows = [
        mod_pow(a=counter[i - 1], n=counter[i], mod=MOD)
        for i in range(1, max(counter.keys()) + 1)
    ]
    patterns = 1
    for p in pows:
        patterns *= p
        patterns %= MOD
    return patterns


if __name__ == "__main__":
    N = int(input())
    D = list(map(int, input().split()))
    print(main(n=N, d=D))
Problem p02866 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

#define MOD 998244353
int main() {
  ll n;
  cin >> n;

  ll cnt[100010] = {};
  vector<ll> d(n);
  rep(i, n) {
    cin >> d[i];
    cnt[d[i]]++;
  }
  if (d.front() != 0 || cnt[0] > 1) {
    cout << 0;
    return 0;
  }

  ll ans = 1;
  rep(i, 100010) if (d[i] > 0) ans = (ans * d[i]) % MOD;

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

#define MOD 998244353
int main() {
  ll n;
  cin >> n;

  ll cnt[100010] = {};
  vector<ll> d(n);
  rep(i, n) {
    cin >> d[i];
    cnt[d[i]]++;
  }
  if (d.front() != 0 || cnt[0] > 1) {
    cout << 0;
    return 0;
  }

  ll ans = 1;
  for (ll pos = 1; pos < n; ++pos) {
    if (cnt[pos] == 0)
      continue;
    ll base = 1;
    rep(i, cnt[pos]) base = (base * cnt[pos - 1]) % MOD;
    ans = (ans * base) % MOD;
  }
  cout << ans << endl;
}
Problem p02867 (C++)

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

bool hasCycle = false;

void search(int curr, int &n, vector<vector<int>> &v, vector<int> &visited,
            int count) {
  if (hasCycle)
    return;
  visited[curr] = count;
  for (auto &to : v[curr]) {
    if (visited[to] != -1) {
      if (count - visited[to] + 1 != n)
        hasCycle = true;
      continue;
    }
    search(to, n, v, visited, count + 1);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[i] = a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    d[i] = b[i];
  }
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  for (int i = 0; i < n; i++) {
    if (c[i] > d[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int ci = upper_bound(c.begin(), c.end(), d[i]) - c.begin();
    if (ci - i > 1) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  assert(false);
  map<int, int> cm, dm;
  for (int i = 0; i < n; i++) {
    cm[c[i]] = i;
    dm[d[i]] = i;
  }
  vector<vector<int>> v(n);
  vector<int> visited(n, -1);
  for (int i = 0; i < n; i++) {
    v[cm[a[i]]].emplace_back(dm[b[i]]);
    if (cm[a[i]] == dm[b[i]]) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (visited[i] == -1)
      search(i, n, v, visited, 0);
  }
  if (hasCycle)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

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

bool hasCycle = false;

void search(int curr, int &n, vector<vector<int>> &v, vector<int> &visited,
            int count) {
  if (hasCycle)
    return;
  visited[curr] = count;
  for (auto &to : v[curr]) {
    if (visited[to] != -1) {
      if (count - visited[to] + 1 != n)
        hasCycle = true;
      continue;
    }
    search(to, n, v, visited, count + 1);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[i] = a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    d[i] = b[i];
  }
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  for (int i = 0; i < n; i++) {
    if (c[i] > d[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int ci = upper_bound(c.begin(), c.end(), d[i]) - c.begin();
    if (ci - i > 1) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  map<int, int> cm, dm;
  for (int i = 0; i < n; i++) {
    cm[c[i]] = i;
    dm[d[i]] = i;
  }
  vector<vector<int>> v(n);
  vector<int> visited(n, -1);
  for (int i = 0; i < n; i++) {
    v[cm[a[i]]].emplace_back(dm[b[i]]);
    if (cm[a[i]] == dm[b[i]]) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (visited[i] == -1)
      search(i, n, v, visited, 0);
  }
  if (hasCycle)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p02867 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<ll, int> P;

int main() {
  int N;
  cin >> N;
  vector<P> A(N), B(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i].first;
    A[i].second = i;
  }

  for (int i = 0; i < N; ++i) {
    cin >> B[i].first;
    B[i].second = A[i].first;
  }

  sort(B.begin(), B.end());
  sort(A.begin(), A.end());

  bool ans = true;
  for (int i = 0; i < N; ++i) {
    if (A[i].first > B[i].first) {
      ans = false;
      break;
    }
  }

  vector<int> p(N);
  if (ans) {
    for (int i = 0; i < N; ++i) {
      p[A[i].second] = B[i].second;
    }
    int count = 0, root = 0, node = 0;
    while (1) {
      node = p[node];
      ++count;
      if (root == node)
        break;
    }
    if (count == N) {
      ans = false;
      for (int i = 0; i < N - 1; ++i) {
        if (A[i + 1].first <= B[i].first) {
          ans = true;
          break;
        }
      }
    }
  }

  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


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

using namespace std;

typedef long long ll;
typedef pair<ll, int> P;

int main() {
  int N;
  cin >> N;
  vector<P> A(N), B(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i].first;
    A[i].second = i;
  }

  for (int i = 0; i < N; ++i) {
    cin >> B[i].first;
    B[i].second = i;
  }

  sort(B.begin(), B.end());
  sort(A.begin(), A.end());

  bool ans = true;
  for (int i = 0; i < N; ++i) {
    if (A[i].first > B[i].first) {
      ans = false;
      break;
    }
  }

  vector<int> p(N);
  if (ans) {
    for (int i = 0; i < N; ++i) {
      p[A[i].second] = B[i].second;
    }
    int count = 0, root = 0, node = 0;
    while (1) {
      node = p[node];
      ++count;
      if (root == node)
        break;
    }
    if (count == N) {
      ans = false;
      for (int i = 0; i < N - 1; ++i) {
        if (A[i + 1].first <= B[i].first) {
          ans = true;
          break;
        }
      }
    }
  }

  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02867 (C++)

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

namespace TYC {
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

int n, A[N], B[N], P[N];
map<int, int> idA, idB;

inline int read() {
  int x = 0, f = 0, ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}

inline void End(const int t) {
  puts(t ? "Yes" : "No");
  exit(0);
}

void work() {
  n = read();
  for (int i = 1; i <= n; i++)
    A[i] = read(), idA[A[i]] = i;
  for (int i = 1; i <= n; i++)
    B[i] = read(), idB[B[i]] = i;
  sort(A + 1, A + 1 + n);
  sort(B + 1, B + 1 + n);
  for (int i = 1; i <= n; i++)
    if (A[i] > B[i])
      End(0);
  for (int i = 1; i < n; i++)
    if (A[i] == A[i + 1] || B[i] == B[i + 1])
      End(1);
  for (int i = 1; i < n; i++)
    if (A[i + 1] <= B[i])
      End(1);
    else
      P[idA[A[i]]] = idB[B[i]];
  int c = 1, u = P[1];
  while (u != 1)
    c++, u = P[u];
  End(c != n);
}
} // namespace TYC

int main() {
  TYC::work();
  return 0;
}

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

namespace TYC {
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

int n, A[N], B[N], P[N];
map<int, int> idA, idB;

inline int read() {
  int x = 0, f = 0, ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}

inline void End(const int t) {
  puts(t ? "Yes" : "No");
  exit(0);
}

void work() {
  n = read();
  for (int i = 1; i <= n; i++)
    A[i] = read(), idA[A[i]] = i;
  for (int i = 1; i <= n; i++)
    B[i] = read(), idB[B[i]] = i;
  sort(A + 1, A + 1 + n);
  sort(B + 1, B + 1 + n);
  for (int i = 1; i <= n; i++)
    if (A[i] > B[i])
      End(0);
  for (int i = 1; i < n; i++)
    if (A[i] == A[i + 1] || B[i] == B[i + 1])
      End(1);
  for (int i = 1; i <= n; i++)
    if (i < n && A[i + 1] <= B[i])
      End(1);
    else
      P[idA[A[i]]] = idB[B[i]];
  int c = 1, u = P[1];
  while (u != 1)
    c++, u = P[u];
  End(c != n);
}
} // namespace TYC

int main() {
  TYC::work();
  return 0;
}
Problem p02867 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const int P = 1e9 + 7;
const int N = 2005;
const double pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct dat {
  int a, b;
} c[N];
int n, a[N], b[N], to[N];

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", a + i);
  rep(i, 1, n) scanf("%d", b + i);
  rep(i, 1, n) c[i].a = a[i], c[i].b = b[i];

  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  rep(i, 1, n) if (a[i] > b[i]) {
    printf("No\n");
    return 0;
  }
  rep(i, 1, n) if (a[i] <= b[i - 1]) {
    printf("Yes\n");
    return 0;
  }

  sort(c + 1, c + n + 1, [](dat x, dat y) { return x.b < y.b; });
  // rep(i,1,n)cout<<c[i].a<<" "<<c[i].b<<endl;
  rep(i, 1, n) {
    int l = 1, r = n;
    while (l <= r) {
      int mid = l + r >> 1;
      if (c[i].a <= c[mid].b)
        to[i] = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    // cout<<i<<" "<<to[i]<<endl;
  }
  int cnt = 1, i = 1;
  while (to[i] != 1) {
    i = to[i], cnt++;
  }
  printf(cnt < n ? "Yes\n" : "No\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const int P = 1e9 + 7;
const int N = 1e5 + 5;
const double pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct dat {
  int a, b;
} c[N];
int n, a[N], b[N], to[N];

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", a + i);
  rep(i, 1, n) scanf("%d", b + i);
  rep(i, 1, n) c[i].a = a[i], c[i].b = b[i];

  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  rep(i, 1, n) if (a[i] > b[i]) {
    printf("No\n");
    return 0;
  }
  rep(i, 1, n) if (a[i] <= b[i - 1]) {
    printf("Yes\n");
    return 0;
  }

  sort(c + 1, c + n + 1, [](dat x, dat y) { return x.b < y.b; });
  // rep(i,1,n)cout<<c[i].a<<" "<<c[i].b<<endl;
  rep(i, 1, n) {
    int l = 1, r = n;
    while (l <= r) {
      int mid = l + r >> 1;
      if (c[i].a <= c[mid].b)
        to[i] = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    // cout<<i<<" "<<to[i]<<endl;
  }
  int cnt = 1, i = 1;
  while (to[i] != 1) {
    i = to[i], cnt++;
  }
  printf(cnt < n ? "Yes\n" : "No\n");
  return 0;
}
Problem p02867 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<pair<int, int>> A, B;
  for (int n = 0; n < N; ++n) {
    int a;
    scanf("%d", &a);
    A.emplace_back(a, n);
  }
  for (int n = 0; n < N; ++n) {
    int b;
    scanf("%d", &b);
    B.emplace_back(b, n);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  for (int n = 0; n < N; ++n) {
    if (B[n].first < A[n].first) {
      printf("No\n");
      return 0;
    }
  }
  for (int n = 1; n < N; ++n) {
    if (A[n].first <= B[n - 1].first) {
      printf("Yes\n");
      return 0;
    }
  }
  vector<int> Ans(N), Now(N), Pos(N);
  for (int n = 0; n < N; ++n) {
    Now[n] = n;
    Pos[n] = n;
    Ans[B[n].second] = A[n].second;
  }
  for (int n = 0; n < N - 1; ++n) {
    if (Now[n] == Ans[n]) {
      printf("Yes\n");
      return 0;
    }
    int i = Pos[n];
    swap(Now[i], Now[n]);
    // Now[i] = Now[n];
    assert(false);
    Pos[Now[n]] = n;
    Pos[Now[i]] = i;
  }
  printf("No\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<pair<int, int>> A, B;
  for (int n = 0; n < N; ++n) {
    int a;
    scanf("%d", &a);
    A.emplace_back(a, n);
  }
  for (int n = 0; n < N; ++n) {
    int b;
    scanf("%d", &b);
    B.emplace_back(b, n);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  for (int n = 0; n < N; ++n) {
    if (B[n].first < A[n].first) {
      printf("No\n");
      return 0;
    }
  }
  for (int n = 1; n < N; ++n) {
    if (A[n].first <= B[n - 1].first) {
      printf("Yes\n");
      return 0;
    }
  }
  vector<int> Ans(N), Now(N), Pos(N);
  for (int n = 0; n < N; ++n) {
    Now[n] = n;
    Pos[n] = n;
    Ans[B[n].second] = A[n].second;
  }
  for (int n = 0; n < N - 1; ++n) {
    if (Now[n] == Ans[n]) {
      printf("Yes\n");
      return 0;
    }
    int i = Pos[Ans[n]];
    Now[i] = Now[n];
    Pos[Now[i]] = i;
  }
  printf("No\n");
  return 0;
}
Problem p02867 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;

int g[100000];

int dfs(int x) {
  if (g[x] == x) {
    return x;
  } else {
    return x = dfs(g[x]);
  }
}
int main() {
  int n, x, y;
  vector<pair<int, int>> a, b;
  int p = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    a.push_back({x, i});
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    b.push_back({x, i});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    if (a[i].first > b[i].first) {
      printf("No\n");
      return 0;
    }
  }
  p = 0;
  for (int i = 0; i < n; i++) {
    if (i == p) {
      while (a[p].first <= b[i].first) {
        p++;
      }
      for (int j = i; j < p; j++) {
        a[j].first = b[i].first;
        b[j].first = b[i].first;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i].second == b[i].second) {
      printf("Yes\n");
      return 0;
    }
  }
  for (int i = 1; i < n; i++) {
    if (a[i].first == a[i - 1].first || b[i].first == b[i - 1].first) {
      printf("Yes\n");
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    g[i] = i;
  }
  for (int i = 0; i < n; i++) {
    x = dfs(a[i].second);
    y = dfs(b[i].second);
    if (x != y) {
      g[max(x, y)] = g[min(x, y)];
    }
  }
  for (int i = 0; i < n; i++) {
    x = dfs(i);
    if (x) {
      printf("Yes\n");
      return 0;
    }
  }
  printf("No\n");
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;

int g[100000];

int dfs(int x) {
  if (g[x] == x) {
    return x;
  } else {
    return x = dfs(g[x]);
  }
}
int main() {
  int n, x, y;
  vector<pair<int, int>> a, b;
  int p = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    a.push_back({x, i});
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    b.push_back({x, i});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    if (a[i].first > b[i].first) {
      printf("No\n");
      return 0;
    }
  }
  p = 0;
  for (int i = 0; i < n; i++) {
    if (i == p) {
      while (p < n && a[p].first <= b[i].first) {
        p++;
      }
      for (int j = i; j < p; j++) {
        a[j].first = b[i].first;
        b[j].first = b[i].first;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i].second == b[i].second) {
      printf("Yes\n");
      return 0;
    }
  }
  for (int i = 1; i < n; i++) {
    if (a[i].first == a[i - 1].first || b[i].first == b[i - 1].first) {
      printf("Yes\n");
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    g[i] = i;
  }
  for (int i = 0; i < n; i++) {
    x = dfs(a[i].second);
    y = dfs(b[i].second);
    if (x != y) {
      g[max(x, y)] = g[min(x, y)];
    }
  }
  for (int i = 0; i < n; i++) {
    x = dfs(i);
    if (x) {
      printf("Yes\n");
      return 0;
    }
  }
  printf("No\n");
}
Problem p02868 (C++)

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

struct edge {
  int to;
  ll cost;
};
typedef pair<int, int> P;
const ll INF = 1e18;
const int MAX_V = 1e5;

vector<edge> G[MAX_V];
vector<ll> d(MAX_V, INF);

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = n - 1; i >= 1; i--)
    G[i].push_back(edge{i - 1, 0});
  rep(i, m) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back(edge{r, c});
  }
  dijkstra(0);
  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
  return 0;
}

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

struct edge {
  int to;
  ll cost;
};
typedef pair<ll, int> P;
const ll INF = 1e18;
const int MAX_V = 1e5;

vector<edge> G[MAX_V];
vector<ll> d(MAX_V, INF);

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = n - 1; i >= 1; i--)
    G[i].push_back(edge{i - 1, 0});
  rep(i, m) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back(edge{r, c});
  }
  dijkstra(0);
  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 100010;
const ll mod = 1e9 + 7;

typedef vector<vector<pair<ll, int>>> wgraph;
typedef pair<ll, int> pli;

vector<ll> bfs(wgraph v, int x) {
  priority_queue<pli, vector<pli>, greater<pli>> q;
  vector<ll> dist(v.size(), longinf);
  dist[x] = 0;
  q.push({0LL, x});

  while (q.size()) {
    pli p = q.top();
    q.pop();
    ll d = p.F, now = p.S;
    if (dist[now] < d) {
      continue;
    }
    for (auto to : v[now]) {
      if (dist[to.S] > d + to.F) {
        dist[to.S] = d + to.F;
        q.push({dist[to.S], to.S});
      }
    }
  }
  return dist;
}

int main() {
  int n, m;
  cin >> n >> m;
  wgraph v(n);
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    v[x].emplace_back(z, y);
    for (int j = x + 1; j < y; j++) {
      v[y].emplace_back(0, j);
    }
  }
  vector<ll> dist = bfs(v, 0);
  ll ans = dist[n - 1];
  if (ans == longinf)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 100010;
const ll mod = 1e9 + 7;

typedef vector<vector<pair<ll, int>>> wgraph;
typedef pair<ll, int> pli;

vector<ll> bfs(wgraph v, int x) {
  priority_queue<pli, vector<pli>, greater<pli>> q;
  vector<ll> dist(v.size(), longinf);
  dist[x] = 0;
  q.push({0LL, x});

  while (q.size()) {
    pli p = q.top();
    q.pop();
    ll d = p.F, now = p.S;
    if (dist[now] < d) {
      continue;
    }
    for (auto to : v[now]) {
      if (dist[to.S] > d + to.F) {
        dist[to.S] = d + to.F;
        q.push({dist[to.S], to.S});
      }
    }
  }
  return dist;
}

int main() {
  int n, m;
  cin >> n >> m;
  wgraph v(n);
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    v[x].emplace_back(z, y);
  }
  for (int i = n - 1; i > 0; i--) {
    v[i].emplace_back(0, i - 1);
  }
  vector<ll> dist = bfs(v, 0);
  ll ans = dist[n - 1];
  if (ans == longinf)
    ans = -1;
  cout << ans << endl;
  return 0;
}