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

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

const int MAX = 1000;

int N, M;
vector<int> X;
vector<int> mod, modc, modd;
int counts[MAX];
// pair countを取る

int main() {
  cin >> N >> M;
  X.resize(N);
  for (auto &v : X)
    cin >> v;

  mod.resize(M);
  modc.resize(M);
  modd.resize(M);

  for (int i = 0; i < N; i++) {
    mod[X[i] % M]++;
    counts[X[i]]++;
  }
  for (int i = 0; i < MAX; i++) {
    modc[i % M] += counts[i] / 2 * 2;
    modd[i % M] += counts[i] % 2;
  }
  int ans = 0;
  for (int i = 0; i <= M / 2; i++) {
    if (i == M - i || i == 0) {
      ans += mod[i] / 2;
    } else {
      int s = min(mod[i], mod[M - i]);
      ans += s;

      int t;

      t = max(0, s - modd[i]);
      ans += max(0, modc[i] - t) / 2;

      t = max(0, s - modd[M - i]);
      ans += max(0, modc[M - i] - t) / 2;
    }
  }
  cout << ans << endl;

  return 0;
}

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

const int MAX = 100100;

int N, M;
vector<int> X;
vector<int> mod, modc, modd;
int counts[MAX];
// pair countを取る

int main() {
  cin >> N >> M;
  X.resize(N);
  for (auto &v : X)
    cin >> v;

  mod.resize(M);
  modc.resize(M);
  modd.resize(M);

  for (int i = 0; i < N; i++) {
    mod[X[i] % M]++;
    counts[X[i]]++;
  }
  for (int i = 0; i < MAX; i++) {
    modc[i % M] += counts[i] / 2 * 2;
    modd[i % M] += counts[i] % 2;
  }
  int ans = 0;
  for (int i = 0; i <= M / 2; i++) {
    if (i == M - i || i == 0) {
      ans += mod[i] / 2;
    } else {
      int s = min(mod[i], mod[M - i]);
      ans += s;

      int t;

      t = max(0, s - modd[i]);
      ans += max(0, modc[i] - t) / 2;

      t = max(0, s - modd[M - i]);
      ans += max(0, modc[M - i] - t) / 2;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03912 (C++)

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

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n, m;
  cin >> n >> m;
  vint x(n);
  vint cnt(m);
  map<int, int> mp;
  vector<vint> g(m);
  rep(i, n) {
    cin >> x[i];
    ++cnt[x[i] % m];
    ++mp[x[i]];
    g[x[i] % m].push_back(x[i]);
  }
  rep(i, m) {
    sort(all(g[i]));
    g[i].erase(unique(all(g[i])), g[i].end());
  }
  int ans = 0;
  ans += cnt[0] / 2;
  if (m % 2 == 0)
    ans += cnt[m / 2] / 2;
  reps(i, 1, (m + 1) / 2) {
    int a = i, b = m - i;
    if (cnt[a] < cnt[b])
      swap(a, b);
    // cout<<a<<" "<<b<<" "<<cnt[a]<<" "<<cnt[b]<<endl;
    rep(j, g[a].size()) {
      if (cnt[a] == cnt[b])
        break;
      int x = min(mp[g[a][i]], cnt[a] - cnt[b]);
      ans += x / 2;
      cnt[a] -= x;
    }
    ans += cnt[b];
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n, m;
  cin >> n >> m;
  vint x(n);
  vint cnt(m);
  map<int, int> mp;
  vector<vint> g(m);
  rep(i, n) {
    cin >> x[i];
    ++cnt[x[i] % m];
    ++mp[x[i]];
    g[x[i] % m].push_back(x[i]);
  }
  rep(i, m) {
    sort(all(g[i]));
    g[i].erase(unique(all(g[i])), g[i].end());
  }
  int ans = 0;
  ans += cnt[0] / 2;
  if (m % 2 == 0)
    ans += cnt[m / 2] / 2;
  reps(i, 1, (m + 1) / 2) {
    int a = i, b = m - i;
    if (cnt[a] < cnt[b])
      swap(a, b);
    // cout<<a<<" "<<b<<" "<<cnt[a]<<" "<<cnt[b]<<endl;
    rep(j, g[a].size()) {
      if (cnt[a] == cnt[b])
        break;
      int x = min(mp[g[a][j]] / 2, (cnt[a] - cnt[b]) / 2);
      ans += x;
      cnt[a] -= x * 2;
    }
    ans += cnt[b];
  }
  cout << ans << endl;

  return 0;
}
Problem p03912 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define fi cin
#define fo cout
#define x first
#define y second
#define ll long long
#define IOS                                                                    \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0)
#define p(v) cerr << #v << " = " << v << '\n'
#define p2(v) cerr << #v << " = " << (complex<int>(v.x, v.y)) << '\n'
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define pdd pair<db, db>
#define CF
int main(void) {
#ifdef CF
  freopen("input", "r", stdin);
#endif // CF
  srand(time(0));
  fo << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  int n, m;
  fi >> n >> m;
  static int cnt[1 << 20];
  static map<int, int> s[1 << 20];
  int v;
  for (int i = 1; i <= n; ++i)
    fi >> v, s[v % m][v]++, cnt[v % m]++;
  ;
  ll ans = 0;
  for (int i = 0; i + i <= m; ++i) {
    int a = i;
    int b = (m - i) % m;
    if (a == b)
      ans += cnt[i] / 2;
    else {
      if (cnt[a] < cnt[b])
        swap(a, b);
      for (auto &it : s[a])
        if ((it.y & 1) && cnt[b])
          --cnt[a], --cnt[b], ++ans, --it.y;
      for (auto &it : s[a]) {
        int can = min(cnt[b], it.y);
        cnt[b] -= can;
        it.y -= can;
        ans += can;
        ans += it.y / 2;
      }
    }
  }
  fo << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define fi cin
#define fo cout
#define x first
#define y second
#define ll long long
#define IOS                                                                    \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0)
#define p(v) cerr << #v << " = " << v << '\n'
#define p2(v) cerr << #v << " = " << (complex<int>(v.x, v.y)) << '\n'
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define pdd pair<db, db>
int main(void) {
#ifdef CF
  freopen("input", "r", stdin);
#endif // CF
  srand(time(0));
  fo << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  int n, m;
  fi >> n >> m;
  static int cnt[1 << 20];
  static map<int, int> s[1 << 20];
  int v;
  for (int i = 1; i <= n; ++i)
    fi >> v, s[v % m][v]++, cnt[v % m]++;
  ;
  ll ans = 0;
  for (int i = 0; i + i <= m; ++i) {
    int a = i;
    int b = (m - i) % m;
    if (a == b)
      ans += cnt[i] / 2;
    else {
      if (cnt[a] < cnt[b])
        swap(a, b);
      for (auto &it : s[a])
        if ((it.y & 1) && cnt[b])
          --cnt[a], --cnt[b], ++ans, --it.y;
      for (auto &it : s[a]) {
        int can = min(cnt[b], it.y);
        cnt[b] -= can;
        it.y -= can;
        ans += can;
        ans += it.y / 2;
      }
    }
  }
  fo << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}
Problem p03913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

ll N, A;
ll ans = linf;

int main() {
  cin >> N >> A;
  for (ll i = 1; i < 40; i++) {
    ll st = 0, en = 1000000000001;
    while (en - st > 1) {
      ll mid = (st + en) / 2;
      if (mid > inf && mid < N && i > 1) {
        en = mid;
        continue;
      }
      ll tmp = 1;
      rep(j, i) {
        tmp *= mid;
        if (tmp > N)
          break;
      }
      if (tmp >= N)
        en = mid;
      else
        st = mid;
    }
    ll b = en - 1;
    ll cnt = i * b;
    ll tmp = 1;
    rep(j, i) tmp *= b;
    while (true) {
      tmp = (tmp / b) * (b + 1);
      cnt++;
      if (tmp >= N)
        break;
    }
    ans = min(ans, A * (i - 1) + cnt);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

ll N, A;
ll ans = linf;

int main() {
  cin >> N >> A;
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  for (ll i = 1; i < 40; i++) {
    ll st = 0, en = 1000000000001;
    while (en - st > 1) {
      ll mid = (st + en) / 2;
      if (mid > inf && mid < N && i > 1) {
        en = mid;
        continue;
      }
      ll tmp = 1;
      rep(j, i) {
        tmp *= mid;
        if (tmp > N)
          break;
      }
      if (tmp >= N)
        en = mid;
      else
        st = mid;
    }
    ll b = en - 1;
    ll cnt = i * b;
    ll tmp = 1;
    rep(j, i) tmp *= b;
    while (true) {
      tmp = (tmp / b) * (b + 1);
      cnt++;
      if (tmp >= N)
        break;
    }
    ans = min(ans, A * (i - 1) + cnt);
  }
  cout << ans << endl;
}
Problem p03913 (C++)

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

using namespace std;

typedef long long LL;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define fin "\n"

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)

template <typename T> inline void chmin(T &l, T r) { l = min(l, r); }

template <typename T> inline void chmax(T &l, T r) { l = max(l, r); }

LL pow(LL a, LL n) {
  LL res = 1;
  while (n) {
    if (n & 1)
      res *= a;
    a = a * a;
    n >>= 1;
  }
  return res;
}
int main() {
  LL N, A;
  cin >> N >> A;
  LL res = N;
  FOR(k, 2, 40) {
    LL i = 0;
    for (; pow(i + 1, k) < N; i++)
      ;
    LL m = pow(i, k);
    LL ans = i * k + A * (k - 1);
    while (m < N) {
      m /= i;
      m *= i + 1;
      ans++;
    }
    chmin(res, ans);
  }
  cout << res << endl;

  return 0;
}


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

using namespace std;

typedef long long LL;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define fin "\n"

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)

template <typename T> inline void chmin(T &l, T r) { l = min(l, r); }

template <typename T> inline void chmax(T &l, T r) { l = max(l, r); }

LL pow(LL a, LL n) {
  LL res = 1;
  while (n) {
    if (n & 1)
      res *= a;
    a = a * a;
    n >>= 1;
  }
  return res;
}
int main() {
  LL N, A;
  cin >> N >> A;
  LL res = N;
  FOR(k, 2, 40) {
    LL i = 1;
    for (; pow(i + 1, k) < N; i++)
      ;
    LL m = pow(i, k);
    LL ans = i * k + A * (k - 1);
    while (m < N) {
      m /= i;
      m *= i + 1;
      ans++;
    }
    chmin(res, ans);
  }
  cout << res << endl;

  return 0;
}
Problem p03913 (C++)

Buggy submission (status: Time Limit Exceeded):
//*
#include <algorithm>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable : 4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll n, A;

int param(ll x) {
  ll i, j, k;
  if (x == n)
    return 1;
  for (i = 1;; i++) {
    if (i * A >= x)
      break;
    ll u = x - i * A;
    ll U, V;
    ll pro = 1;
    for (j = 0; j < i + 1; j++) {
      ll now = u / (i + 1);
      if (j < u % (i + 1))
        now++;
      if (now >= (n + pro - 1) / pro)
        return 1;
      pro *= now;
      if (pro == 0)
        break;
    }
  }
  return 0;
}

int main() {
  ll i, j, k, l;
  cin >> n >> A;
  ll low = 1, high = n, mid;
  while (low <= high) {
    mid = (low + high) / 2;
    if (param(mid))
      high = mid - 1;
    else
      low = mid + 1;
  }
  cout << low << endl;
  return 0;
}
//*/

Fixed submission:
//*
#include <algorithm>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable : 4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll n, A;

int param(ll x) {
  ll i, j, k;
  if (x == n)
    return 1;
  for (i = 1; i < 64; i++) {
    if (i * A >= x)
      break;
    ll u = x - i * A;
    ll U, V;
    ll pro = 1;
    for (j = 0; j < i + 1; j++) {
      ll now = u / (i + 1);
      if (j < u % (i + 1))
        now++;
      if (now >= (n + pro - 1) / pro)
        return 1;
      pro *= now;
      if (pro == 0)
        break;
    }
  }
  return 0;
}

int main() {
  ll i, j, k, l;
  cin >> n >> A;
  ll low = 1, high = n, mid;
  while (low <= high) {
    mid = (low + high) / 2;
    if (param(mid))
      high = mid - 1;
    else
      low = mid + 1;
  }
  cout << low << endl;
  return 0;
}
//*/
Problem p03913 (C++)

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

long stpow(long b, long e) {
  long ret = 1;
  while (e--) {
    if (ret < 1e18) {
      ret *= b;
    }
  }
  return ret;
}

long n, a;
long sub(int k) {
  long x = (long)pow(n, 1.0 / (k + 1));
  if (stpow(x, k + 1) >= n)
    --x;
  long curr = stpow(x, k + 1);
  for (int i = 1; i <= k + 1; ++i) {
    curr /= x;
    curr *= x + 1;
    if (curr >= n) {
      return (k + 1) * x + i + k * a;
    }
  }
  return 1e18;
}

int main() {
  cin >> n >> a;
  long mn = 1e18;
  for (int k = 0; k < 45; ++k) {
    mn = min(mn, sub(k));
  }
  cout << mn << endl;
}


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

long stpow(long b, long e) {
  long ret = 1;
  while (e--) {
    if (ret < 1e18) {
      ret *= b;
    }
  }
  return ret;
}

long n, a;
long sub(int k) {
  long x = (long)pow(n, 1.0 / (k + 1));
  if (stpow(x, k + 1) >= n)
    --x;
  long curr = stpow(x, k + 1);
  for (int i = 1; i <= k + 1; ++i) {
    curr /= x;
    curr *= x + 1;
    if (curr >= n) {
      return (k + 1) * x + i + k * a;
    }
  }
  return 1e18;
}

int main() {
  cin >> n >> a;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  long mn = 1e18;
  for (int k = 0; k < 45; ++k) {
    mn = min(mn, sub(k));
  }
  cout << mn << endl;
}
Problem p03913 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;

ll power(ll x, ll n) {
  ll res = 1;
  for (int i = 0; i < n; i++)
    res *= x;
  return res;
}

int main() {
  ll n, a;
  cin >> n >> a;
  ll ans = n;
  for (ll b = 2; b < 60; b++) {
    ll i;
    for (i = 1; i < 1000002; i++) {
      if (power(i, b) >= n) {
        i--;
        break;
      }
    }
    ll s = i * b + a * (b - 1);
    ll d = power(i, b);
    while (d < n) {
      d = d / i * (i + 1);
      s++;
    }
    ans = min(ans, s);
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;

ll power(ll x, ll n) {
  ll res = 1;
  for (int i = 0; i < n; i++)
    res *= x;
  return res;
}

int main() {
  ll n, a;
  cin >> n >> a;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = n;
  for (ll b = 2; b < 60; b++) {
    ll i;
    for (i = 1; i < 1000002; i++) {
      if (power(i, b) >= n) {
        i--;
        break;
      }
    }
    ll s = i * b + a * (b - 1);
    ll d = power(i, b);
    while (d < n) {
      d = d / i * (i + 1);
      s++;
    }
    ans = min(ans, s);
  }
  cout << ans << endl;
}
Problem p03914 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

const int N = 410;

ll fact[N];

ll dp[N][N][N];
ll ppow[N];

int main(void) {
  int n, m;
  cin >> n >> m;
  if (n > m) {
    cout << 0 << endl;
    return 0;
  }
  fact[0] = 1;
  REP(i, 1, N) { fact[i] = fact[i - 1] * i % mod; }
  dp[0][1][0] = 1;
  REP(i, 1, m + 1) {
    REP(j, 1, n + 1) {
      REP(k, 0, j) {
        if (j >= 1) {
          dp[i][j][k] += dp[i - 1][j - 1][k];
        }
        dp[i][j][k] %= mod;
        dp[i][j][k] += dp[i - 1][j][k] * (j - k - 1);
        dp[i][j][k] %= mod;
        dp[i][j][j - 1] += dp[i - 1][j][k] * (k + 1);
        dp[i][j][j - 1] %= mod;
      }
      if (0) {
        cerr << "dp[" << i << "][" << j << "]:";
        REP(k, 0, j) { cerr << dp[i][j][k] << " "; }
        cerr << endl;
      }
    }
  }
  ll tot = 0;
  ppow[0] = 1;
  REP(i, 1, N) { ppow[i] = ppow[i - 1] * (n - 1) % mod; }
  cout << dp[m][n][n - 1] * fact[n - 1] % mod << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

const int N = 310;

ll fact[N];

ll dp[N][N][N];
ll ppow[N];

int main(void) {
  int n, m;
  cin >> n >> m;
  if (n > m) {
    cout << 0 << endl;
    return 0;
  }
  fact[0] = 1;
  REP(i, 1, N) { fact[i] = fact[i - 1] * i % mod; }
  dp[0][1][0] = 1;
  REP(i, 1, m + 1) {
    REP(j, 1, n + 1) {
      REP(k, 0, j) {
        if (j >= 1) {
          dp[i][j][k] += dp[i - 1][j - 1][k];
        }
        dp[i][j][k] %= mod;
        dp[i][j][k] += dp[i - 1][j][k] * (j - k - 1);
        dp[i][j][k] %= mod;
        dp[i][j][j - 1] += dp[i - 1][j][k] * (k + 1);
        dp[i][j][j - 1] %= mod;
      }
      if (0) {
        cerr << "dp[" << i << "][" << j << "]:";
        REP(k, 0, j) { cerr << dp[i][j][k] << " "; }
        cerr << endl;
      }
    }
  }
  ll tot = 0;
  ppow[0] = 1;
  REP(i, 1, N) { ppow[i] = ppow[i - 1] * (n - 1) % mod; }
  cout << dp[m][n][n - 1] * fact[n - 1] % mod << endl;
}
Problem p03914 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define drep(i, r, l) for (int i = r; i >= l; --i)
using namespace std;
const int sz = 301;
const int mod = 1e9 + 7;
int n, m;
int dp[sz][sz][sz];
int main() {
  scanf("%d%d", &n, &m);
  dp[0][1][0] = 1;
  rep(i, 0, m - 1) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        int cur = dp[i][j][k];
        dp[i + 1][j][k] = (dp[i + 1][j][k] + 1ll * cur * k % mod) % mod;
        dp[i + 1][j][k + 1] =
            (dp[i + 1][j][k + 1] + 1ll * cur * (n - j - k) % mod) % mod;
        dp[i + 1][j + k][0] = (dp[i + 1][j + k][0] + 1ll * cur * j % mod) % mod;
      }
    }
  }
  printf("%d\n", dp[m][n][0]);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define drep(i, r, l) for (int i = r; i >= l; --i)
using namespace std;
const int sz = 310;
const int mod = 1e9 + 7;
int n, m;
int dp[sz][sz][sz];
int main() {
  scanf("%d%d", &n, &m);
  dp[0][1][0] = 1;
  rep(i, 0, m - 1) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        int cur = dp[i][j][k];
        dp[i + 1][j][k] = (dp[i + 1][j][k] + 1ll * cur * k % mod) % mod;
        dp[i + 1][j][k + 1] =
            (dp[i + 1][j][k + 1] + 1ll * cur * (n - j - k) % mod) % mod;
        dp[i + 1][j + k][0] = (dp[i + 1][j + k][0] + 1ll * cur * j % mod) % mod;
      }
    }
  }
  printf("%d\n", dp[m][n][0]);
}
Problem p03914 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007ll
#define INF 1000000000ll
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \
  REP(d, a.size()) {                                                           \
    cout << a[d];                                                              \
    if (d != a.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \
  sort(ALL(v));                                                                \
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

ll dp[334][334][334];
ll n, m;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  dp[0][1][1] = 1;
  REP(i, m + 1) REP(j, n + 1) REP(k, n + 1) {
    dp[i][j][k] %= MOD;
    if (i < m) {
      dp[i + 1][j][j] += k * dp[i][j][k];
      dp[i + 1][j][k] += (j - k) * dp[i][j][k];
      if (j < n)
        dp[i + 1][j + 1][k] += (n - j) * dp[i][j][k];
    }
  }
  cout << dp[m][n][n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007ll
#define INF 1000000000ll
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \
  REP(d, a.size()) {                                                           \
    cout << a[d];                                                              \
    if (d != a.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \
  sort(ALL(v));                                                                \
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

ll dp[301][301][301];
ll n, m;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  dp[0][1][1] = 1;
  REP(i, m + 1) REP(j, n + 1) REP(k, n + 1) {
    dp[i][j][k] %= MOD;
    if (i < m) {
      dp[i + 1][j][j] += k * dp[i][j][k];
      dp[i + 1][j][k] += (j - k) * dp[i][j][k];
      if (j < n)
        dp[i + 1][j + 1][k] += (n - j) * dp[i][j][k];
    }
  }
  cout << dp[m][n][n] << endl;
  return 0;
}
Problem p03914 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define int long long
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int mod = 1000000007;
int n, m;
int dp[301][301]
      [301]; //{S}から出る→冒険する(T)→{S}へ戻る. dp[移動回数][|S|][|T|].

signed main() {
  int i, j, k;

  cin >> n >> m;

  dp[0][1][0] = 1;
  rep(i, m) {
    rep(j, n + 1) {
      // k = 0. S->S. S->new
      (dp[i + 1][j][0] += j * dp[i][j][0]) %= mod;
      (dp[i + 1][j][1] += (n - j) * dp[i][j][0]) %= mod;
      // else
      for (k = 1; k <= n; k++) {
        // T->S, T->T, T->new
        (dp[i + 1][j + k][0] += j * dp[i][j][k]) %= mod;
        (dp[i + 1][j][k] += k * dp[i][j][k]) %= mod;
        (dp[i + 1][j][k + 1] += (n - j - k) * dp[i][j][k]) %= mod;
      }
    }
  }

  cout << dp[m][n][0] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define int long long
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int mod = 1000000007;
int n, m;
int dp[301][301]
      [301]; //{S}から出る→冒険する(T)→{S}へ戻る. dp[移動回数][|S|][|T|].

signed main() {
  int i, j, k;

  cin >> n >> m;

  dp[0][1][0] = 1;
  rep(i, m) {
    rep(j, n + 1) {
      // k = 0. S->S. S->new
      (dp[i + 1][j][0] += j * dp[i][j][0]) %= mod;
      (dp[i + 1][j][1] += (n - j) * dp[i][j][0]) %= mod;
      // else
      for (k = 1; k <= n; k++) {
        if (dp[i][j][k] == 0)
          continue;
        // T->S, T->T, T->new
        (dp[i + 1][j + k][0] += j * dp[i][j][k]) %= mod;
        (dp[i + 1][j][k] += k * dp[i][j][k]) %= mod;
        (dp[i + 1][j][k + 1] += (n - j - k) * dp[i][j][k]) %= mod;
      }
    }
  }

  cout << dp[m][n][0] << endl;
  return 0;
}
Problem p03915 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;

#ifdef WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

const int maxn = 100005;

int p[maxn], s[maxn];
set<pair<int, pair<int, int>>> edges;
int n, m;

inline int find(int a) { return p[a] == a ? a : p[a] = find(p[a]); }

inline bool unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return false;
  if (s[a] < s[b])
    swap(a, b);
  p[b] = a;
  if (s[a] == s[b])
    s[a]++;
  return true;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    edges.insert({c, {a, b}});
    edges.insert({c + 1, {a + 1, b}});
  }
  for (int i = 0; i < n; i++)
    p[i] = i, s[i] = 0;
  ll answer = 0;
  while (!edges.empty()) {
    int cura = edges.begin()->se.fi % n;
    int curb = edges.begin()->se.se % n;
    int curc = edges.begin()->fi;
    edges.erase(edges.begin());
    if (unite(cura, curb)) {
      edges.insert({curc + 2, {cura + 1, curb + 1}});
      answer += curc;
    }
  }
  cout << answer << endl;
  return 0;
}

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

using namespace std;

using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;

#ifdef WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

const int maxn = 200005;

int p[maxn], s[maxn];
set<pair<int, pair<int, int>>> edges;
int n, m;

inline int find(int a) { return p[a] == a ? a : p[a] = find(p[a]); }

inline bool unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return false;
  if (s[a] < s[b])
    swap(a, b);
  p[b] = a;
  if (s[a] == s[b])
    s[a]++;
  return true;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    edges.insert({c, {a, b}});
    edges.insert({c + 1, {a + 1, b}});
  }
  for (int i = 0; i < n; i++)
    p[i] = i, s[i] = 0;
  ll answer = 0;
  while (!edges.empty()) {
    int cura = edges.begin()->se.fi % n;
    int curb = edges.begin()->se.se % n;
    int curc = edges.begin()->fi;
    edges.erase(edges.begin());
    if (unite(cura, curb)) {
      edges.insert({curc + 2, {cura + 1, curb + 1}});
      answer += curc;
    }
  }
  cout << answer << endl;
  return 0;
}
Problem p03915 (C++)

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

typedef long long i64;

const int N = 200000 + 10;

struct Edge {
  int x, y, z;
  Edge() {}
  Edge(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
  inline bool operator<(const Edge &rhs) const { return z < rhs.z; }
};

inline void check(int &lhs, int rhs) { lhs = (lhs < rhs ? lhs : rhs); }

int n, tag[N];
std::vector<Edge> edge;

void init() {
  int m;
  scanf("%d%d", &n, &m);
  memset(tag, 0x3f, sizeof tag);
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    edge.push_back(Edge(a, b, c));
    check(tag[a], c + 1);
    check(tag[b], c + 2);
  }
  for (int i = 0; i < 3 * n; ++i)
    check(tag[(i + 1) % n], tag[i] + 2);
}

int anc[N];
int find(int x) { return anc[x] == x ? x : (anc[x] = find(anc[x])); }

int main() {
  init();
  for (int i = 0; i < n; ++i)
    edge.push_back(Edge(i, (i + 1) % n, tag[i]));
  std::sort(edge.begin(), edge.end());
  for (int i = 0; i < n; ++i)
    anc[i] = i;
  i64 ans = 0;
  for (int i = 0; i < edge.size(); ++i) {
    int x = find(edge[i].x), y = find(edge[i].y), z = edge[i].z;
    if (x != y)
      anc[x] = y, ans += z;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

typedef long long i64;

const int N = 200000 + 10;

struct Edge {
  int x, y, z;
  Edge() {}
  Edge(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
  inline bool operator<(const Edge &rhs) const { return z < rhs.z; }
};

inline void check(int &lhs, int rhs) { lhs = (lhs < rhs ? lhs : rhs); }

int n, tag[N];
std::vector<Edge> edge;

void init() {
  int m;
  scanf("%d%d", &n, &m);
  memset(tag, 0x3f, sizeof tag);
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    edge.push_back(Edge(a, b, c));
    check(tag[a], c + 1);
    check(tag[b], c + 2);
  }
  for (int i = 0; i < 3 * n; ++i)
    check(tag[(i + 1) % n], tag[i % n] + 2);
}

int anc[N];
int find(int x) { return anc[x] == x ? x : (anc[x] = find(anc[x])); }

int main() {
  init();
  for (int i = 0; i < n; ++i)
    edge.push_back(Edge(i, (i + 1) % n, tag[i]));
  std::sort(edge.begin(), edge.end());
  for (int i = 0; i < n; ++i)
    anc[i] = i;
  i64 ans = 0;
  for (int i = 0; i < edge.size(); ++i) {
    int x = find(edge[i].x), y = find(edge[i].y), z = edge[i].z;
    if (x != y)
      anc[x] = y, ans += z;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03915 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define RG register
#define LL long long
using namespace std;
const int N = 2e5 + 10;
struct node {
  int a, b;
  LL c;
} pt[N];
int n, m, cnt, tot, fa[N];
LL minn[N], ans;
inline bool cmp(node a, node b) { return a.c < b.c; }
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline LL mi(LL a, LL b) { return a < b ? a : b; }
inline int f(int son) { return fa[son] = fa[son] == son ? son : f(fa[son]); }
int main() {
  // freopen("Zigzag MST.in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  m = read();
  memset(minn, 0x3f, sizeof minn);
  for (int i = 0; i < n; i++)
    fa[i] = i;
  for (int i = 1, a, b, c; i <= m; i++) {
    a = read() % n;
    b = read() % n;
    c = read();
    pt[++cnt] = {a, b, c};
    minn[a] = mi(minn[a], c + 1);
    minn[b] = mi(minn[b], c + 2);
  }
  for (int i = 1; i < 2 * n; i++)
    minn[i % n] = mi(minn[i % n], minn[(i - 1) % n] + 2);
  for (int i = 0; i < n; i++)
    pt[++cnt] = (node){i % n, (i + 1) % n, minn[i]};
  sort(pt + 1, pt + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) {
    int fx = f(pt[i].a), fy = f(pt[i].b);
    if (fx == fy)
      continue;
    fa[fx] = fy;
    ans += pt[i].c;
    if (++tot == n - 1)
      return printf("%lld\n", ans), 0;
  }
  // fclose(stdin); fclose(stdout);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define RG register
#define LL long long
using namespace std;
const int N = 2e6 + 10;
struct node {
  int a, b;
  LL c;
} pt[N];
int n, m, cnt, tot, fa[N];
LL minn[N], ans;
inline bool cmp(node a, node b) { return a.c < b.c; }
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline LL mi(LL a, LL b) { return a < b ? a : b; }
inline int f(int son) { return fa[son] = fa[son] == son ? son : f(fa[son]); }
int main() {
  // freopen("Zigzag MST.in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  m = read();
  memset(minn, 0x3f, sizeof minn);
  for (int i = 0; i < n; i++)
    fa[i] = i;
  for (int i = 1, a, b, c; i <= m; i++) {
    a = read() % n;
    b = read() % n;
    c = read();
    pt[++cnt] = {a, b, c};
    minn[a] = mi(minn[a], c + 1);
    minn[b] = mi(minn[b], c + 2);
  }
  for (int i = 1; i < 2 * n; i++)
    minn[i % n] = mi(minn[i % n], minn[(i - 1) % n] + 2);
  for (int i = 0; i < n; i++)
    pt[++cnt] = (node){i % n, (i + 1) % n, minn[i]};
  sort(pt + 1, pt + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) {
    int fx = f(pt[i].a), fy = f(pt[i].b);
    if (fx == fy)
      continue;
    fa[fx] = fy;
    ans += pt[i].c;
    if (++tot == n - 1)
      return printf("%lld\n", ans), 0;
  }
  // fclose(stdin); fclose(stdout);
  return 0;
}
Problem p03915 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
// This code is written by Itst
using namespace std;

inline int read() {
  int a = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return a;
}

const int MAXN = 2e5 + 3;
struct Edge {
  int s, t, w;
  bool operator<(const Edge a) const { return w < a.w; }
} Ed[MAXN << 1];
int fa[MAXN], minN[MAXN], N, Q, cnt;
long long sum;

int find(int x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); }

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  // freopen("out","w",stdout);
#endif
  memset(minN, 0x7f, sizeof(minN));
  N = read();
  Q = read();
  for (int i = 1; i <= Q; ++i) {
    Ed[++cnt].s = read();
    Ed[cnt].t = read();
    Ed[cnt].w = read();
    minN[Ed[cnt].s] = min(minN[Ed[cnt].s], Ed[cnt].w + 1);
    minN[Ed[cnt].t] = min(minN[Ed[cnt].t], Ed[cnt].w + 2);
  }
  for (int i = 0; i < 2 * N; ++i)
    minN[(i + 1) % N] = min(minN[(i + 1) % N], minN[i % N] + 2);
  for (int i = 0; i < N; ++i) {
    Ed[++cnt].s = i;
    Ed[cnt].t = (i + 1) % N;
    Ed[cnt].w = minN[i];
    fa[i] = i;
  }
  sort(Ed + 1, Ed + cnt + 1);
  for (int i = 1; i <= cnt; ++i)
    if (find(Ed[i].s) != find(Ed[i].t)) {
      fa[find(Ed[i].s)] = find(Ed[i].t);
      sum += Ed[i].w;
    }
  cout << sum;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
// This code is written by Itst
using namespace std;

inline int read() {
  int a = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return a;
}

const int MAXN = 2e5 + 3;
struct Edge {
  int s, t, w;
  bool operator<(const Edge a) const { return w < a.w; }
} Ed[MAXN << 1];
int fa[MAXN], minN[MAXN], N, Q, cnt;
long long sum;

int find(int x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); }

int main() {
  memset(minN, 0x7f, sizeof(minN));
  N = read();
  Q = read();
  for (int i = 1; i <= Q; ++i) {
    Ed[++cnt].s = read();
    Ed[cnt].t = read();
    Ed[cnt].w = read();
    minN[Ed[cnt].s] = min(minN[Ed[cnt].s], Ed[cnt].w + 1);
    minN[Ed[cnt].t] = min(minN[Ed[cnt].t], Ed[cnt].w + 2);
  }
  for (int i = 0; i < 2 * N; ++i)
    minN[(i + 1) % N] = min(minN[(i + 1) % N], minN[i % N] + 2);
  for (int i = 0; i < N; ++i) {
    Ed[++cnt].s = i;
    Ed[cnt].t = (i + 1) % N;
    Ed[cnt].w = minN[i];
    fa[i] = i;
  }
  sort(Ed + 1, Ed + cnt + 1);
  for (int i = 1; i <= cnt; ++i)
    if (find(Ed[i].s) != find(Ed[i].t)) {
      fa[find(Ed[i].s)] = find(Ed[i].t);
      sum += Ed[i].w;
    }
  cout << sum;
  return 0;
}
Problem p03915 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
struct edge {
  int u, v, w;
  edge(int _u = 0, int _v = 0, int _w = 0) {
    u = _u;
    v = _v;
    w = _w;
  }
  inline bool operator<(const edge &a) const { return w < a.w; }
} arr[400005];
int fa[400005], w[400005];
int getfa(int u) {
  if (u == fa[u])
    return u;
  return fa[u] = getfa(fa[u]);
}
int main() {
  // freopen("CF16-final-G.in", "r", stdin);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    fa[i] = i;
    w[i] = w[i + n] = 2e9;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &arr[i].u, &arr[i].v, &arr[i].w);
    w[arr[i].u] = std::min(w[arr[i].u], arr[i].w + 1);
    w[arr[i].v] = std::min(w[arr[i].v], arr[i].w + 2);
  }
  for (int i = 1; i < 1 << n; i++)
    w[i] = std::min(w[i - 1] + 2, w[i]);
  for (int i = 0; i < n; i++)
    arr[m++] = edge(i, (i + 1) % n, std::min(w[i], w[i + n]));
  std::sort(arr, arr + m);
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    int u = getfa(arr[i].u), v = getfa(arr[i].v);
    if (u != v) {
      fa[u] = v;
      ans += arr[i].w;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
struct edge {
  int u, v, w;
  edge(int _u = 0, int _v = 0, int _w = 0) {
    u = _u;
    v = _v;
    w = _w;
  }
  inline bool operator<(const edge &a) const { return w < a.w; }
} arr[400005];
int fa[400005], w[400005];
int getfa(int u) {
  if (u == fa[u])
    return u;
  return fa[u] = getfa(fa[u]);
}
int main() {
  // freopen("CF16-final-G.in", "r", stdin);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    fa[i] = i;
    w[i] = w[i + n] = 2e9;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &arr[i].u, &arr[i].v, &arr[i].w);
    w[arr[i].u] = std::min(w[arr[i].u], arr[i].w + 1);
    w[arr[i].v] = std::min(w[arr[i].v], arr[i].w + 2);
  }
  for (int i = 1; i < n << 1; i++)
    w[i] = std::min(w[i - 1] + 2, w[i]);
  for (int i = 0; i < n; i++)
    arr[m++] = edge(i, (i + 1) % n, std::min(w[i], w[i + n]));
  std::sort(arr, arr + m);
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    int u = getfa(arr[i].u), v = getfa(arr[i].v);
    if (u != v) {
      fa[u] = v;
      ans += arr[i].w;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03915 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;

struct edge {
  int x, y, c;
  bool operator<(const edge &t) const { return c < t.c; }
} E[200200];
int N, M, P[200200], C, U[200200];

int find(int x) {
  if (P[x] != x)
    P[x] = find(P[x]);
  return P[x];
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    P[i] = i;
    U[i] = 1111111111;
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    E[C++] = {a, b, c};
    U[a] = min(U[a], c + 1);
    U[b] = min(U[b], c + 2);
  }
  for (int k = 0; k < 2; k++)
    for (int i = 0; i < N; i++) {
      int x = (i + 1) % N;
      if (U[x] > U[i] + 2)
        U[x] = U[i] + 2;
    }
  for (int i = 0; i < N; i++) {

    int x = (i + 1) % N;
    E[C++] = {i, x, U[i]};
  }

  sort(E, E + C);
  long long ans = 0;
  for (int i = 0; i < C; i++) {
    int x = find(E[i].x), y = find(E[i].y);
    if (x != y) {
      P[x] = y;
      ans += E[i].c;
    }
  }

  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;

struct edge {
  int x, y, c;
  bool operator<(const edge &t) const { return c < t.c; }
} E[400200];
int N, M, P[200200], C, U[200200];

int find(int x) {
  if (P[x] != x)
    P[x] = find(P[x]);
  return P[x];
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    P[i] = i;
    U[i] = 1111111111;
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    E[C++] = {a, b, c};
    U[a] = min(U[a], c + 1);
    U[b] = min(U[b], c + 2);
  }
  for (int k = 0; k < 2; k++)
    for (int i = 0; i < N; i++) {
      int x = (i + 1) % N;
      if (U[x] > U[i] + 2)
        U[x] = U[i] + 2;
    }
  for (int i = 0; i < N; i++) {

    int x = (i + 1) % N;
    E[C++] = {i, x, U[i]};
  }

  sort(E, E + C);
  long long ans = 0;
  for (int i = 0; i < C; i++) {
    int x = find(E[i].x), y = find(E[i].y);
    if (x != y) {
      P[x] = y;
      ans += E[i].c;
    }
  }

  printf("%lld\n", ans);
  return 0;
}
Problem p03915 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int maxn = 200010;
ll ans = 0, a[maxn];
int n, m, fa[maxn];
pair<ll, pair<int, int>> e[maxn];
int find(int x) {
  if (fa[x] == x)
    return x;
  return (fa[x] = find(fa[x]));
}
int nxt(int x) {
  if (x < n)
    return x + 1;
  else
    return 1;
}
int main() {
  // freopen("1.txt","r",stdin);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    a[i] = INF;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    x++;
    y++;
    e[i] = make_pair(z, make_pair(x, y));
    a[x] = min(a[x], z + 1LL);
    a[y] = min(a[y], z + 2LL);
  }
  for (int ii = 1; ii <= 5; ii++)
    for (int i = 1; i <= n; i++)
      a[nxt(i)] = min(a[nxt(i)], a[i] + 2);
  for (int i = 1; i <= n; i++)
    e[m + i] = make_pair(a[i], make_pair(i, nxt(i)));
  sort(e + 1, e + (n + m + 1));
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m + n; i++) {
    int x = e[i].second.first, y = e[i].second.second;
    x = find(x);
    y = find(y);
    if (x == y)
      continue;
    ans += e[i].first;
    fa[x] = y;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int maxn = 500010;
ll ans = 0, a[maxn];
int n, m, fa[maxn];
pair<ll, pair<int, int>> e[maxn];
int find(int x) {
  if (fa[x] == x)
    return x;
  return (fa[x] = find(fa[x]));
}
int nxt(int x) {
  if (x < n)
    return x + 1;
  else
    return 1;
}
int main() {
  // freopen("1.txt","r",stdin);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    a[i] = INF;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    x++;
    y++;
    e[i] = make_pair(z, make_pair(x, y));
    a[x] = min(a[x], z + 1LL);
    a[y] = min(a[y], z + 2LL);
  }
  for (int ii = 1; ii <= 5; ii++)
    for (int i = 1; i <= n; i++)
      a[nxt(i)] = min(a[nxt(i)], a[i] + 2);
  for (int i = 1; i <= n; i++)
    e[m + i] = make_pair(a[i], make_pair(i, nxt(i)));
  sort(e + 1, e + (n + m + 1));
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m + n; i++) {
    int x = e[i].second.first, y = e[i].second.second;
    x = find(x);
    y = find(y);
    if (x == y)
      continue;
    ans += e[i].first;
    fa[x] = y;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03915 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define N 200010

int n, m;
int f[N];
int px[N], py[N], pz[N];
set<PII> A;

int fa(int x) {
  if (f[x] == x)
    return x;
  f[x] = fa(f[x]);
  return f[x];
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> px[i] >> py[i] >> pz[i];
    A.insert(mp(pz[i], i));
    px[i + m] = py[i];
    py[i + m] = (px[i] + 1) % n;
    pz[i + m] = pz[i] + 1;
    A.insert(mp(pz[i] + 1, i + m));
  }
  for (int i = 0; i < n; i++)
    f[i] = i;
  ll S = 0;
  while (!A.empty()) {
    PII _x = *A.begin();
    A.erase(A.begin());
    int i = _x.se;
    int z = _x.fi;
    int x = px[i];
    int y = py[i];
    if (fa(x) != fa(y)) {
      S += z;
      f[fa(x)] = fa(y);
      (px[i] += 1) %= n;
      (py[i] += 1) %= n;
      A.insert(mp(z + 2, i));
    }
  }
  cout << S << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define N 400010

int n, m;
int f[N];
int px[N], py[N], pz[N];
set<PII> A;

int fa(int x) {
  if (f[x] == x)
    return x;
  f[x] = fa(f[x]);
  return f[x];
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> px[i] >> py[i] >> pz[i];
    A.insert(mp(pz[i], i));
    px[i + m] = py[i];
    py[i + m] = (px[i] + 1) % n;
    pz[i + m] = pz[i] + 1;
    A.insert(mp(pz[i] + 1, i + m));
  }
  for (int i = 0; i < n; i++)
    f[i] = i;
  ll S = 0;
  while (!A.empty()) {
    PII _x = *A.begin();
    A.erase(A.begin());
    int i = _x.se;
    int z = _x.fi;
    int x = px[i];
    int y = py[i];
    if (fa(x) != fa(y)) {
      S += z;
      f[fa(x)] = fa(y);
      (px[i] += 1) %= n;
      (py[i] += 1) %= n;
      A.insert(mp(z + 2, i));
    }
  }
  cout << S << endl;
  return 0;
}