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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <float.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;

const ll INF = 1LL << 62;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;

int dp[41][401][401];

int main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<int> a(N), b(N), c(N);
  rep(i, N) cin >> a[i] >> b[i] >> c[i];

  rep(i, N + 1) {
    rep(j, 401) {
      rep(k, 401) { dp[i][j][k] = 100000000; }
    }
  }
  dp[0][0][0] = 0;
  rep(i, N + 1) {
    rep(j, 401) {
      rep(k, 401) {
        chmin(dp[i + 1][j][k], dp[i][j][k]);
        if (j + a[i] > 400 || k + b[i] > 400)
          continue;
        chmin(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
  const int max_v = 100000000;
  int ans = max_v;
  for (int i = 1; i <= 400; i++) {
    for (int j = 1; j <= 400; j++) {
      if (i * Mb == j * Ma) {
        ans = min(ans, dp[N][i][j]);
      }
    }
  }

  if (ans == max_v)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

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

const ll INF = 1LL << 62;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;

int dp[41][401][401];

int main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<int> a(N), b(N), c(N);
  rep(i, N) cin >> a[i] >> b[i] >> c[i];

  rep(i, N + 1) {
    rep(j, 401) {
      rep(k, 401) { dp[i][j][k] = 100000000; }
    }
  }
  dp[0][0][0] = 0;
  rep(i, N) {
    rep(j, 401) {
      rep(k, 401) {
        chmin(dp[i + 1][j][k], dp[i][j][k]);
        if (j + a[i] > 400 || k + b[i] > 400)
          continue;
        chmin(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
  const int max_v = 100000000;
  int ans = max_v;
  for (int i = 1; i <= 400; i++) {
    for (int j = 1; j <= 400; j++) {
      if (i * Mb == j * Ma) {
        ans = min(ans, dp[N][i][j]);
      }
    }
  }

  if (ans == max_v)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<int> a(N), b(N), c(N);
  rep(i, 0, N) { cin >> a[i] >> b[i] >> c[i]; }
  static int dp[41][410][410] = {};
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  rep(i, 0, N) {
    rep(j, 0, 410 - a[i]) {
      rep(k, 0, 410 - b[i]) {
        if (dp[i][j][k] == INF)
          continue;
        chmin(dp[i + 1][j][k], dp[i][j][k]);
        chmin(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
  int ans = INF;
  for (int i = 1;; i++) {
    int ma = Ma * i, mb = Mb * i;
    if (ma > 410 || mb > 410)
      break;
    chmin(ans, dp[N][ma][mb]);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<int> a(N), b(N), c(N);
  rep(i, 0, N) { cin >> a[i] >> b[i] >> c[i]; }
  static int dp[44][410][410] = {};
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  rep(i, 0, N) {
    rep(j, 0, 410 - a[i]) {
      rep(k, 0, 410 - b[i]) {
        if (dp[i][j][k] == INF)
          continue;
        chmin(dp[i + 1][j][k], dp[i][j][k]);
        chmin(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
  int ans = INF;
  for (int i = 1;; i++) {
    int ma = Ma * i, mb = Mb * i;
    if (ma > 410 || mb > 410)
      break;
    chmin(ans, dp[N][ma][mb]);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p03806 (C++)

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

static const int INF = 1e9;

int main() {
  int N, ma, mb;
  cin >> N >> ma >> mb;
  int a[N], b[N], c[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  int dp[N + 1][40][401];
  for (int i = 0; i <= N; i++) {
    for (int s = 0; s <= 400; s++) {
      for (int t = 0; t <= 400; t++) {
        dp[i][s][t] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int s = 0; s <= 400; s++) {
      for (int t = 0; t <= 400; t++) {
        if (s + a[i] <= 400 && t + b[i] <= 400) {
          dp[i + 1][s + a[i]][t + b[i]] =
              min(dp[i + 1][s + a[i]][t + b[i]], dp[i][s][t] + c[i]);
        }
        dp[i + 1][s][t] = min(dp[i + 1][s][t], dp[i][s][t]);
      }
    }
  }
  int ans = INF;
  for (int i = 1; i <= 400; i++) {
    for (int j = 1; j <= 400; j++) {
      // cout << dp[N][i][j] << " ";
      if (i * mb == j * ma) {
        ans = min(ans, dp[N][i][j]);
      }
    }
    // cout << endl;
  }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

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

static const int INF = 1e9;

int main() {
  int N, ma, mb;
  cin >> N >> ma >> mb;
  int a[N], b[N], c[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  int dp[N + 1][401][401];
  for (int i = 0; i <= N; i++) {
    for (int s = 0; s <= 400; s++) {
      for (int t = 0; t <= 400; t++) {
        dp[i][s][t] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int s = 0; s <= 400; s++) {
      for (int t = 0; t <= 400; t++) {
        if (s + a[i] <= 400 && t + b[i] <= 400) {
          dp[i + 1][s + a[i]][t + b[i]] =
              min(dp[i + 1][s + a[i]][t + b[i]], dp[i][s][t] + c[i]);
        }
        dp[i + 1][s][t] = min(dp[i + 1][s][t], dp[i][s][t]);
      }
    }
  }
  int ans = INF;
  for (int i = 1; i <= 400; i++) {
    for (int j = 1; j <= 400; j++) {
      // cout << dp[N][i][j] << " ";
      if (i * mb == j * ma) {
        ans = min(ans, dp[N][i][j]);
      }
    }
    // cout << endl;
  }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
Problem p03806 (C++)

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

using namespace std;
using ll = long long;

const int mod = 1e9 + 7;
const ll oo = 10000000;
const int maxn = 40, maxab = 10;
int n, ma, mb, dp[maxn][maxn * maxab + 1][maxn * maxab + 1], a[maxn + 1],
    b[maxn + 1], c[maxn + 1];

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

  cin >> n >> ma >> mb;

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

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= maxn * maxab; ++j) {
      for (int k = 0; k <= maxn * maxab; ++k) {
        dp[i][j][k] = oo;
      }
    }
  }

  dp[0][0][0] = 0;

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i <= maxn * maxab; ++i) {
      for (int j = 0; j <= maxn * maxab; ++j) {
        if (dp[k][i][j] == oo)
          continue;
        dp[k + 1][i][j] = min(dp[k][i][j], dp[k + 1][i][j]);
        dp[k + 1][i + a[k]][j + b[k]] =
            min(dp[k + 1][i + a[k]][j + b[k]], dp[k][i][j] + c[k]);
      }
    }
  }

  int ans = oo;

  for (int i = 1; i <= maxn * maxab; i++) {
    for (int j = 1; j <= maxn * maxab; j++) {
      if (i * mb == j * ma)
        ans = min(ans, dp[n][i][j]);
    }
  }
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
  return 0;
}

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

using namespace std;
using ll = long long;

const int mod = 1e9 + 7;
const ll oo = 10000000;
const int maxn = 40, maxab = 10;
int n, ma, mb, dp[maxn + 1][maxn * maxab + 1][maxn * maxab + 1], a[maxn + 1],
    b[maxn + 1], c[maxn + 1];

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

  cin >> n >> ma >> mb;

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

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= maxn * maxab; ++j) {
      for (int k = 0; k <= maxn * maxab; ++k) {
        dp[i][j][k] = oo;
      }
    }
  }

  dp[0][0][0] = 0;

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i <= maxn * maxab; ++i) {
      for (int j = 0; j <= maxn * maxab; ++j) {
        if (dp[k][i][j] == oo)
          continue;
        dp[k + 1][i][j] = min(dp[k][i][j], dp[k + 1][i][j]);
        dp[k + 1][i + a[k]][j + b[k]] =
            min(dp[k + 1][i + a[k]][j + b[k]], dp[k][i][j] + c[k]);
      }
    }
  }

  int ans = oo;

  for (int i = 1; i <= maxn * maxab; i++) {
    for (int j = 1; j <= maxn * maxab; j++) {
      if (i * mb == j * ma)
        ans = min(ans, dp[n][i][j]);
    }
  }
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p03806 (C++)

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

using namespace std;

typedef long long int ll;

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

const int INF = 1e9;
const ll MAXN = 1e9;

int main() {
  int n, m_a, m_b;
  cin >> n >> m_a >> m_b;
  vector<int> a(n + 1), b(n + 1), c(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

  int dp[41][401][401] = {0};

  for (int i = 0; i <= 40; i++) {
    for (int j = 0; j <= 400; j++) {
      for (int k = 0; k <= 400; k++) {
        dp[i][j][k] = INF;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 1; i <= 40; i++) {
    for (int j = 400; j >= 0; j--) {
      for (int k = 400; k >= 0; k--) {
        // if(j-a[i]>=0 && k-b[i]>=0){
        // 	dp[i][j][k] = min(dp[i-1][j-a[i]][k-b[i]]+c[i],dp[i-1][j][k]);
        // }else{
        // 	dp[i][j][k] = min(dp[i][j][k],dp[i-1][j][k]);
        // }
        dp[i][j][k] =
            min(dp[i - 1][j - a[i]][k - b[i]] + c[i], dp[i - 1][j][k]);
      }
    }
  }

  int ans = INF;
  for (int j = 1; j <= 400; j++) {
    for (int k = 1; k <= 400; k++) {
      if (k * m_a == j * m_b && dp[n][j][k] < ans)
        ans = dp[n][j][k];
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;

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

const int INF = 1e9;
const ll MAXN = 1e9;

int main() {
  int n, m_a, m_b;
  cin >> n >> m_a >> m_b;
  vector<int> a(n + 1), b(n + 1), c(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

  int dp[41][401][401] = {0};

  for (int i = 0; i <= 40; i++) {
    for (int j = 0; j <= 400; j++) {
      for (int k = 0; k <= 400; k++) {
        dp[i][j][k] = INF;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 1; i <= 40; i++) {
    for (int j = 400; j >= 0; j--) {
      for (int k = 400; k >= 0; k--) {
        if (j - a[i] >= 0 && k - b[i] >= 0) {
          dp[i][j][k] =
              min(dp[i - 1][j - a[i]][k - b[i]] + c[i], dp[i - 1][j][k]);
        } else {
          // dp[i][j][k] = min(dp[i][j][k],dp[i-1][j][k]);
          dp[i][j][k] = dp[i - 1][j][k];
        }
      }
    }
  }

  int ans = INF;
  for (int j = 1; j <= 400; j++) {
    for (int k = 1; k <= 400; k++) {
      if (k * m_a == j * m_b && dp[n][j][k] < ans)
        ans = dp[n][j][k];
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define vecint vector<int>
#define Pii pair<int, int>

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define print(a) cout << a << endl

#define MOD 1000000007
#define NIL -1

int N, Ma, Mb;

int dp[40 + 1][400][400];
int c[40], b[40], a[40];

void init() {
  rep(i, N + 1) {
    rep(j, N * 10) {
      rep(k, N * 10) { dp[i][j][k] = 40 * 100 + 1; }
    }
  }
  dp[0][0][0] = 0;
}

void solve() {
  init();
  rep(i, N + 1) {
    rep(j, N * 10) {
      rep(k, N * 10) {
        if (dp[i][j][k] == 100 * 40 + 1)
          continue;
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        dp[i + 1][j + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
  int ans = 40 * 100 + 1;
  REP(i, 1, N + 1) {
    REP(j, 1, N * 10) {
      REP(k, 1, N * 10) {
        if (j * Mb == k * Ma) {
          //                    print(dp[i][j][k]);
          ans = min(ans, dp[i][j][k]);
        }
        //                print(dp[i][j][k]);
      }
    }
  }
  if (ans == 40 * 100 + 1) {
    print("-1");
  } else {
    print(ans);
  }
  return;
}

int main() {
  cin >> N >> Ma >> Mb;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define vecint vector<int>
#define Pii pair<int, int>

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define print(a) cout << a << endl

#define MOD 1000000007
#define NIL -1

int N, Ma, Mb;

int dp[40 + 1][400][400];
int c[40], b[40], a[40];

void init() {
  rep(i, N + 1) {
    rep(j, N * 10) {
      rep(k, N * 10) { dp[i][j][k] = 40 * 100 + 1; }
    }
  }
  dp[0][0][0] = 0;
}

void solve() {
  init();
  rep(i, N) {
    rep(j, N * 10) {
      rep(k, N * 10) {
        if (dp[i][j][k] == 100 * 40 + 1)
          continue;
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        dp[i + 1][j + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
  int ans = 40 * 100 + 1;
  REP(i, 1, N + 1) {
    REP(j, 1, N * 10) {
      REP(k, 1, N * 10) {
        if (j * Mb == k * Ma) {
          //                    print(dp[i][j][k]);
          ans = min(ans, dp[i][j][k]);
        }
        //                print(dp[i][j][k]);
      }
    }
  }
  if (ans == 40 * 100 + 1) {
    print("-1");
  } else {
    print(ans);
  }
  return;
}

int main() {
  cin >> N >> Ma >> Mb;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }
  solve();
  return 0;
}
Problem p03806 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
long long INF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

#define MAX_N 40
pair<int, int> Y[MAX_N], ps[MAX_N / 2];

int inline bs(int begin, int end, int x) {
  int left = begin, right = end - 1, mid;
  if (x < ps[left].first || x > ps[right].first)
    return -1;
  while (left <= right) {
    mid = (left + right) / 2;
    if (x < ps[mid].first)
      right = mid - 1;
    else
      left = mid + 1;
  }
  if (x == ps[right].first)
    return right;
  else
    return -1;
}

int main() {
  int ans = MOD, n, ma, mb;

  scanf("%d %d %d", &n, &ma, &mb);
  REP(i, n) {
    int a, b;
    scanf("%d %d %d", &a, &b, &Y[i].second);
    Y[i].first = ma * b - mb * a;
  }

  int n2 = n / 2;
  REP(i, 1 << n2) {
    int s = 0, v = 0;
    REP(j, n2) {
      if (i >> j & 1) {
        s += Y[j].first;
        v += Y[j].second;
      }
    }
    ps[i] = make_pair(s, v);
  }

  ps[0] = ps[1];
  sort(ps, ps + (1 << n2));
  int m = 1;
  FOR(i, 1, 1 << n2) if (ps[m - 1].first != ps[i].first) ps[m++] = ps[i];

  REP(i, 1 << (n - n2)) {
    int s = 0, v = 0;
    REP(j, n - n2) {
      if (i >> j & 1) {
        s += Y[n2 + j].first;
        v += Y[n2 + j].second;
      }
    }
    if (s != 0 || v == 0) {
      if (bs(0, m, -s) != -1)
        ans = min(ans, v + ps[bs(0, m, -s)].second);
    } else
      ans = min(ans, v);
  }

  if (ans != MOD && ans != 0)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
long long INF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

#define MAX_N 40
pair<int, int> Y[MAX_N], ps[1 << (MAX_N / 2)];

int inline bs(int begin, int end, int x) {
  int left = begin, right = end - 1, mid;
  if (x < ps[left].first || x > ps[right].first)
    return -1;
  while (left <= right) {
    mid = (left + right) / 2;
    if (x < ps[mid].first)
      right = mid - 1;
    else
      left = mid + 1;
  }
  if (x == ps[right].first)
    return right;
  else
    return -1;
}

int main() {
  int ans = MOD, n, ma, mb;

  scanf("%d %d %d", &n, &ma, &mb);
  REP(i, n) {
    int a, b;
    scanf("%d %d %d", &a, &b, &Y[i].second);
    Y[i].first = ma * b - mb * a;
  }

  int n2 = n / 2;
  REP(i, 1 << n2) {
    int s = 0, v = 0;
    REP(j, n2) {
      if (i >> j & 1) {
        s += Y[j].first;
        v += Y[j].second;
      }
    }
    ps[i] = make_pair(s, v);
  }

  ps[0] = ps[1];
  sort(ps, ps + (1 << n2));
  int m = 1;
  FOR(i, 1, 1 << n2) if (ps[m - 1].first != ps[i].first) ps[m++] = ps[i];

  REP(i, 1 << (n - n2)) {
    int s = 0, v = 0;
    REP(j, n - n2) {
      if (i >> j & 1) {
        s += Y[n2 + j].first;
        v += Y[n2 + j].second;
      }
    }
    if (s != 0 || v == 0) {
      if (bs(0, m, -s) != -1)
        ans = min(ans, v + ps[bs(0, m, -s)].second);
    } else
      ans = min(ans, v);
  }

  if (ans != MOD && ans != 0)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}
Problem p03806 (C++)

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

const int N = 44;
const int inf = 1e8;

int n, ma, mb, a[N], b[N], c[N];
int cache[44][404][404];
bool ready[44][404][404];

int dp(int k, int x, int y) {
  if (k == n) {
    if (x == 0 and y == 0)
      return inf;
    int g = __gcd(x, y);
    x /= g;
    y /= g;
    if (ma == x and mb == y)
      return 0;
    return inf;
  }

  if (ready[k][x][y])
    return cache[k][x][y];
  int &rv = cache[k][x][y];
  rv = dp(k + 1, x, y);
  rv = min(rv, c[k] + dp(k + 1, x + a[k], y + b[k]));
  return rv;
}

int main() {
  cin >> n >> ma >> mb;
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i] >> c[i];
  int res = dp(0, 0, 0);
  printf("%d\n", res >= inf ? -1 : res);
}

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

const int N = 44;
const int inf = 1e8;

int n, ma, mb, a[N], b[N], c[N];
int cache[44][404][404];
bool ready[44][404][404];

int dp(int k, int x, int y) {
  if (k == n) {
    if (x == 0 and y == 0)
      return inf;
    int g = __gcd(x, y);
    x /= g;
    y /= g;
    if (ma == x and mb == y)
      return 0;
    return inf;
  }

  if (ready[k][x][y])
    return cache[k][x][y];
  ready[k][x][y] = true;
  int &rv = cache[k][x][y];
  rv = dp(k + 1, x, y);
  rv = min(rv, c[k] + dp(k + 1, x + a[k], y + b[k]));
  return rv;
}

int main() {
  cin >> n >> ma >> mb;
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i] >> c[i];
  int res = dp(0, 0, 0);
  printf("%d\n", res >= inf ? -1 : res);
}
Problem p03806 (C++)

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int d[n][3];
  for (int i = 0; i < n; ++i) {
    cin >> d[i][0] >> d[i][1] >> d[i][2];
  }
  int value[401][401];
  for (int i = 0; i < 401; ++i) {
    for (int j = 0; j < 401; ++j) {
      value[i][j] = 5000;
    }
  }
  value[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 400; ++j) {
      for (int k = 0; k <= 400; ++k) {
        if (value[j][k] == 5000) {
          continue;
        }
        value[j + d[i][0]][k + d[i][1]] =
            min(value[j][k] + d[i][2], value[j + d[i][0]][k + d[i][1]]);
      }
    }
  }
  int pow = 1;
  int best = 5000;
  while (pow * ma <= 400 && pow * mb <= 400) {
    best = min(value[pow * ma][pow * mb], best);
    pow++;
  }
  if (best == 5000) {
    cout << -1 << endl;
  } else {
    cout << best << endl;
  }
  return 0;
}


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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int d[n][3];
  for (int i = 0; i < n; ++i) {
    cin >> d[i][0] >> d[i][1] >> d[i][2];
  }
  int value[401][401];
  for (int i = 0; i < 401; ++i) {
    for (int j = 0; j < 401; ++j) {
      value[i][j] = 5000;
    }
  }
  value[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i * 10; j >= 0; --j) {
      for (int k = i * 10; k >= 0; --k) {
        if (value[j][k] == 5000) {
          continue;
        }
        value[j + d[i][0]][k + d[i][1]] =
            min(value[j][k] + d[i][2], value[j + d[i][0]][k + d[i][1]]);
      }
    }
  }
  int pow = 1;
  int best = 5000;
  while (pow * ma <= 400 && pow * mb <= 400) {
    best = min(value[pow * ma][pow * mb], best);
    pow++;
  }
  if (best == 5000) {
    cout << -1 << endl;
  } else {
    cout << best << endl;
  }
  return 0;
}
Problem p03807 (Python)

Buggy submission (status: Runtime Error):
num_ints = int(input())
sum_ints = sum([int(i) for i in input().split()])

if sum % 2 == 0:
    print("YES")
else:
    print("NO")


Fixed submission:
num_ints = int(input())
sum_ints = sum([int(i) for i in input().split()])

if sum_ints % 2 == 0:
    print("YES")
else:
    print("NO")
Problem p03807 (C++)

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

using namespace std;

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

  for (int i = 0; i < n; i++) {
    if (a[i] % 2 != 0) {
      odd++;
    }
  }

  if (odd % 2 == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

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

  for (int i = 0; i < n; i++) {
    if (a[i] % 2 != 0) {
      odd++;
    }
  }

  if (odd % 2 == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
Problem p03807 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, a[110], j = 0, o = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] % 2 == 0)
      o++;
    else
      j++;
  }
  if (j % 2 != 0)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, a[100001], j = 0, o = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] % 2 == 0)
      o++;
    else
      j++;
  }
  if (j % 2 != 0)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}
Problem p03807 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int n, a;
bool f;
int main() {
  scanf("%d", &n);
  for (; scanf("%d", &a); f ^= a & 1)
    ;
  puts(f ? "NO\n" : "YES\n");
}

Fixed submission:
#include <iostream>
int a, f;
main() {
  scanf("%*d");
  for (; std::cin >> a; f ^= a & 1)
    ;
  puts(f ? "NO\n" : "YES\n");
}
Problem p03807 (C++)

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

using namespace std;

int main() {
  int n;
  unsigned long long A[10000];
  int ki = 0;

  // input
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] % 2 == 1)
      ki++;
  }

  if (ki % 2 == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n;
  int A[100000];
  int ki = 0;

  // input
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] % 2 == 1)
      ki++;
  }

  if (ki % 2 == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03807 (C++)

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

int main() {
  int N, a[1000000000], sum;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    sum += a[i] % 2;
  }
  if (sum % 2 == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


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

int main() {
  int N, a[100000], sum;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    sum += a[i] % 2;
  }
  if (sum % 2 == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03807 (C++)

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

const int mnx = 3e5 + 9;
const int mod = 1e9 + 7;

ll n;
ll a[111];
ll a1, a0;

int main() {

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] % 2 != 0) {
      a1++;
    }
  }

  if (a1 % 2 != 0) {
    cout << "NO\n";
    return 0;
  }
  if (a1 % 2 == 0) {
    cout << "YES\n";
    return 0;
  }

  return 0;
}


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

const int mnx = 3e5 + 9;
const int mod = 1e9 + 7;

ll n;
ll a[mnx];
ll a1, a0;

int main() {

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] % 2 != 0) {
      a1++;
    }
  }

  if (a1 % 2 != 0) {
    cout << "NO\n";
    return 0;
  }
  if (a1 % 2 == 0) {
    cout << "YES\n";
    return 0;
  }

  return 0;
}
Problem p03807 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define for_p(i, a, b) for (int i = a; i < b; i++)
#define for_m(i, a, b) for (int i = a - 1; i >= b; i--)

// a = s1.size();
// vector<string> s1;
// string s2 = accumulate( s1.begin(), s1.end(), string() );

using namespace std;

int main() {
  int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
  int i = 0, j = 0;
  double d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0;
  int a1[256], a2[512], a3[1024];
  char s1[200000], s2[200000], s3[200000];

  cin >> a;

  for (i = 0; i < a; i++) {
    cin >> a1[i];
    b += a1[i];
  }
  if (b % 2 == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define for_p(i, a, b) for (int i = a; i < b; i++)
#define for_m(i, a, b) for (int i = a - 1; i >= b; i--)

// a = s1.size();
// vector<string> s1;
// string s2 = accumulate( s1.begin(), s1.end(), string() );

using namespace std;

int main() {
  int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
  int i = 0, j = 0;
  double d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0;
  int a1[100000], a2[512], a3[1024];
  char s1[200000], s2[200000], s3[200000];

  cin >> a;

  for (i = 0; i < a; i++) {
    cin >> a1[i];
    b += a1[i];
  }
  if (b % 2 == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p03807 (C++)

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

bool IsEven(int x) {
  if (x % 2 == 0)
    return true;
  else
    return false;
}

int main() {
  int N;
  cin >> N;
  int A[10100];
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int EvenCnt = 0;
  int OddCnt = 0;
  for (int i = 0; i < N; i++) {
    if (IsEven(A[i]))
      EvenCnt++;
    else
      OddCnt++;
  }

  if (IsEven(OddCnt) == true || OddCnt == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


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

bool IsEven(int x) {
  if (x % 2 == 0)
    return true;
  else
    return false;
}

int main() {
  int N;
  cin >> N;
  int A[100010];
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int EvenCnt = 0;
  int OddCnt = 0;
  for (int i = 0; i < N; i++) {
    if (IsEven(A[i]))
      EvenCnt++;
    else
      OddCnt++;
  }

  if (IsEven(OddCnt) == true || OddCnt == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03807 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int N, A[112345];
int cou;

int main(void) {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
  }

  for (int i = 0; i < N; ++i) {
    if (A[i] % 2 == 1) {
      ++cou;
    }
  }

  if (cou % 2 == 0) {
    printf("YES\n");
  } else {
    printf("TLE Test\n");
    while (true) {
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int N, A[112345];
int cou;

int main(void) {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
  }

  for (int i = 0; i < N; ++i) {
    if (A[i] % 2 == 1) {
      ++cou;
    }
  }

  if (cou % 2 == 0) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }
  return 0;
}
Problem p03807 (C++)

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

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n;
  cin >> n;

  list<int> a;
  rep(i, n) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  bool found = false;
  list<int>::iterator it = a.begin();
  while (it != a.end()) {
    int s = *it;
    list<int>::iterator it2 = it;
    ++it2;
    while (it2 != a.end()) {
      int t = s + *it2;
      if (t % 2 == 0) {
        a.erase(it);
        a.erase(it2);
        a.push_back(t);
        it = a.begin();
        found = true;
        break;
      }
      ++it2;
    }
    if (found)
      found = false;
    else
      ++it;
  }

  if (a.size() == 1) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n;
  cin >> n;

  list<int> a;
  rep(i, n) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  bool found = false;
  list<int>::iterator it = a.begin();
  while (it != a.end()) {
    int s = *it;
    list<int>::iterator it2 = it;
    ++it2;
    while (it2 != a.end()) {
      int t = s + *it2;
      if (t % 2 == 0) {
        a.erase(it);
        *it2 = t;
        it = a.begin();
        found = true;
        break;
      }
      ++it2;
    }
    if (found)
      found = false;
    else
      ++it;
  }

  if (a.size() == 1) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}
Problem p03807 (C++)

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

int main() {
  int n;
  vector<int> a(n + 1);
  cin >> n;
  int oddcnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] % 2 == 1)
      oddcnt++;
  }
  // cout<<oddcnt<<endl;
  cout << ((oddcnt % 2 == 1) ? "NO" : "YES") << endl;
}


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

int main() {
  int n;
  vector<long int> a(100010);
  cin >> n;
  int oddcnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] % 2 == 1)
      oddcnt++;
  }
  // cout<<oddcnt<<endl;
  cout << ((oddcnt % 2 == 1) ? "NO" : "YES") << endl;
}
Problem p03808 (C++)

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

using namespace std;

long long n, sum, a[10001];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sum += a[i];
  if (sum % (n * (n + 1) / 2) != 0)
    cout << "NO", exit(0);
  sum /= (n * (n + 1) / 2);
  a[n + 1] = a[1];
  for (int i = 1; i <= n; i++) {
    if (a[i + 1] - a[i] - sum > 0 || (a[i + 1] - a[i] - sum) % n != 0)
      cout << "NO", exit(0);
  }
  cout << "YES";
}

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

using namespace std;

long long n, sum, a[100001];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sum += a[i];
  if (sum % (n * (n + 1) / 2) != 0)
    cout << "NO", exit(0);
  sum /= (n * (n + 1) / 2);
  a[n + 1] = a[1];
  for (int i = 1; i <= n; i++) {
    if (a[i + 1] - a[i] - sum > 0 || (a[i + 1] - a[i] - sum) % n != 0)
      cout << "NO", exit(0);
  }
  cout << "YES";
}
Problem p03808 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
const int maxn = 2e5 + 20;
int a[maxn];
vector<int> da;
void work() {
  int n;
  LL sum = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  LL add = 1LL * n * (n + 1) / 2;
  if (sum % add != 0) {
    cout << "NO" << endl;
    return;
  }
  for (int i = 2; i <= n; ++i) {
    da.push_back(a[i] - a[i - 1]);
  }
  da.push_back(a[1] - a[n]);
  LL k = sum / add;
  for (int i = 0; i < da.size(); ++i) {
    LL t = k - da[i];
    LL t1 = da[i] + k * (n - 1);
    if (t > t1)
      while (1)
        ;
    if (t % n != 0 || t < 0) {
      cout << "NO" << endl;
      return;
    }
  }
  cout << "YES" << endl;
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
const int maxn = 2e5 + 20;
int a[maxn];
vector<int> da;
void work() {
  int n;
  LL sum = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  LL add = 1LL * n * (n + 1) / 2;
  if (sum % add != 0) {
    cout << "NO" << endl;
    return;
  }
  for (int i = 2; i <= n; ++i) {
    da.push_back(a[i] - a[i - 1]);
  }
  da.push_back(a[1] - a[n]);
  LL k = sum / add;
  for (int i = 0; i < da.size(); ++i) {
    LL t = k - da[i];
    //        LL t = da[i] + k * (n - 1);
    if (t % n != 0 || t < 0) {
      cout << "NO" << endl;
      return;
    }
  }
  cout << "YES" << endl;
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}
Problem p03808 (C++)

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

using namespace std;

int main() {
  long long d, m;
  cin >> d >> m;
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  long long v = (n + 1) * n / 2;
  if (sum % v != 0) {
    cout << "NO";
    return 0;
  }
  v = sum / v;
  long long x = a[0] - a[n - 1];
  long long k = v - x;
  if (k < 0) {
    k = -k;
    k %= n;
    k = n - k;
  }
  if (k % n != 0) {
    cout << "NO";
    return 0;
  }
  for (int i = 1; i < n; i++) {
    long long t = v - a[i] + a[i - 1];
    if (t < 0) {
      t = -t;
      t %= n;
      t = n - t;
    }
    if (t % n != 0) {
      cout << "NO";
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] < v) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";
}


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

using namespace std;

int main() {
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  long long v = (n + 1) * n / 2;
  if (sum % v != 0) {
    cout << "NO";
    return 0;
  }
  v = sum / v;
  long long x = a[0] - a[n - 1];
  long long k = v - x;
  if (k < 0) {
    k = -k;
    k %= n;
    k = n - k;
  }
  if (k % n != 0) {
    cout << "NO";
    return 0;
  }
  for (int i = 1; i < n; i++) {
    long long t = v - a[i] + a[i - 1];
    if (t < 0) {
      t = -t;
      t %= n;
      t = n - t;
    }
    if (t % n != 0) {
      cout << "NO";
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] < v) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";
}
Problem p03808 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define int long long
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
int a[N], b[N], n, sum, res;
signed main() {
  scanf("%lld", &n);
  fp(i, 1, n) scanf("%lld", &a[i]), sum += a[i];
  if (sum % (n * (n + 1) >> 1))
    return puts("NO"), 0;
  sum /= (n * (n + 1) >> 1);
  fp(i, 2, n) b[i] = a[i] - a[i - 1];
  b[1] = a[1] - a[n];
  sort(b + 1, b + 1 + n);
  fp(i, 1, n - 1) {
    if (b[i] != b[i + 1] && b[i] != b[i + 1] - n)
      return puts("NO"), 0;
    if (b[i] == b[i + 1] - n)
      res = i;
  }
  assert(false);
  //	printf("%lld %lld\n",sum,res);
  if (sum < res || (sum - res) % n)
    return puts("NO"), 0;
  puts("YES");
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define int long long
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
int a[N], b[N], n, sum, res;
signed main() {
  scanf("%lld", &n);
  fp(i, 1, n) scanf("%lld", &a[i]), sum += a[i];
  if (sum % (n * (n + 1) >> 1))
    return puts("NO"), 0;
  sum /= (n * (n + 1) >> 1);
  fp(i, 2, n) b[i] = a[i] - a[i - 1];
  b[1] = a[1] - a[n];
  sort(b + 1, b + 1 + n);
  fp(i, 1, n - 1) if ((b[i + 1] - b[i]) % n) return puts("NO"), 0;
  fp(i, 1, n - 1) res += (b[n] - b[i]) / n;
  //	assert(false);
  //	printf("%lld %lld\n",sum,res);
  if (sum < res || (sum - res) % n)
    return puts("NO"), 0;
  puts("YES");
  return 0;
}
Problem p03808 (C++)

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

using namespace std;
typedef long long ll;

int getint() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
#define N 212345
int a[N], b[N];
int main() {
  int n = getint();
  int m = 0;
  ll s = 0;
  for (int i = 1; i <= n; i++)
    a[i] = getint(), s += a[i];
  for (int i = 1; i <= n; i++)
    b[i] = a[i] - a[i - 1];
  b[1] = a[1] - a[n];
  for (int i = 1; i <= n; i++)
    m = max(m, b[i]);
  int sum = 0;
  bool flag = true;
  //	for(int i=1;i<=n;i++) cout<<b[i]<<" "; cout<<endl;
  //	cout<<"m "<<m<<endl;
  for (int i = 1; i <= n; i++) {
    int x = m - b[i]; // cout<<x<<" ";
    if (x % n != 0)
      flag = false;
    sum += x / n;
  }
  //	cout<<endl;
  //	cout<<flag<<endl;
  //	cout<<sum<<endl;
  if (sum != m)
    flag = false;
  //	if(n==2) {
  //		int x=max(a[1],a[2]),y=min(a[1],a[2]);
  //		flag=((x==y)||(x==2*y));
  //	}
  if (s % (ll(n) * (n + 1) / 2 * m) != 0)
    flag = false;
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
/*
5
6 9 12 10 8
*/

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
typedef long long ll;

int getint() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
#define N 212345
int a[N], b[N];
int main() {
  int n = getint();
  int m = 0;
  ll s = 0;
  for (int i = 1; i <= n; i++)
    a[i] = getint(), s += a[i];
  for (int i = 1; i <= n; i++)
    b[i] = a[i] - a[i - 1];
  b[1] = a[1] - a[n];
  for (int i = 1; i <= n; i++)
    m = max(m, b[i]);
  int sum = 0;
  bool flag = true;
  //	for(int i=1;i<=n;i++) cout<<b[i]<<" "; cout<<endl;
  //	cout<<"m "<<m<<endl;
  for (int i = 1; i <= n; i++) {
    int x = m - b[i]; // cout<<x<<" ";
    if (x % n != 0)
      flag = false;
    sum += x / n;
  }
  //	cout<<endl;
  //	cout<<flag<<endl;
  //	cout<<sum<<endl;
  if (sum != m)
    flag = false;
  //	if(n==2) {
  //		int x=max(a[1],a[2]),y=min(a[1],a[2]);
  //		flag=((x==y)||(x==2*y));
  //	}
  if (m != 0)
    if (s % (ll(n) * (n + 1) / 2 * m) != 0)
      flag = false;
  if (m == 0) {
    if (s % (ll(n) * (n + 1) / 2 * n) != 0)
      flag = false;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
/*
5
6 9 12 10 8
*/
Problem p03808 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100005;
int a[N], n, d[N], sum;
void no() {
  puts("NO");
  exit(0);
}
signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), sum += a[i], d[i] = a[i] - a[i - 1];
  d[1] = a[1] - a[n];
  int tmp = n * (n - 1) / 2;
  if (sum % tmp)
    no();
  int k = sum / tmp, sum2 = 0;
  for (int i = 1; i <= n; i++) {
    int x = d[i] + k * (n - 1);
    if (x % n != 0)
      no();
    x /= n;
    if (k - x < 0 || k - x > k)
      no();
    sum2 += k - x;
  }
  printf("%s\n", sum2 == k ? "YES" : "NO");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100005;
int a[N], n, d[N], sum;
void no() {
  puts("NO");
  exit(0);
}
signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), sum += a[i], d[i] = a[i] - a[i - 1];
  d[1] = a[1] - a[n];
  int tmp = n * (n + 1) / 2;
  if (sum % tmp)
    no();
  int k = sum / tmp, sum2 = 0;
  for (int i = 1; i <= n; i++) {
    int x = d[i] + k * (n - 1);
    if (x % n != 0)
      no();
    x /= n;
    if (k - x < 0 || k - x > k)
      no();
    sum2 += k - x;
  }
  printf("%s\n", sum2 == k ? "YES" : "NO");
}
Problem p03808 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define N 110000
inline char gc() {
  static char now[1 << 16], *T, *S;
  if (T == S) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S)
      return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0;
  char ch = gc();
  while (ch < '0' || ch > '9')
    ch = gc();
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = gc();
  }
  return x;
}
int n, a[N], ans[N], differ[N];
int main() {
  // freopen("box.in","r",stdin);
  n = read();
  long long sum = 0;
  long long sum1 = 0;
  sum1 = (long long)n * (n - 1) >> 1;
  for (int i = 1; i <= n; ++i)
    a[i] = read(), sum += a[i];
  a[n + 1] = a[1];
  for (int i = 1; i <= n; ++i)
    differ[i] = a[i + 1] - a[i];
  int k = sum / sum1;
  if (sum % sum1) {
    printf("NO");
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    double x = (differ[i] - k) / (-1.0 * n);
    if (x < 0) {
      printf("NO");
      return 0;
    }
    int xx = x;
    if (xx != x) {
      printf("NO");
      return 0;
    }
    ans[i] = x;
  }
  int ans1 = 0;
  for (int i = 1; i <= n; ++i)
    ans1 += ans[i];
  if (ans1 == k)
    printf("YES");
  else
    printf("NO");
  return 0;
}

Fixed submission:
#include <cstdio>
#define N 110000
inline char gc() {
  static char now[1 << 16], *T, *S;
  if (T == S) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S)
      return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0;
  char ch = gc();
  while (ch < '0' || ch > '9')
    ch = gc();
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = gc();
  }
  return x;
}
int n, a[N], ans[N], differ[N];
int main() {
  // freopen("box.in","r",stdin);
  n = read();
  long long sum = 0;
  long long sum1 = 0;
  if (n == 1) {
    printf("YES");
    return 0;
  }
  sum1 = (long long)n * (n + 1) >> 1;
  for (int i = 1; i <= n; ++i)
    a[i] = read(), sum += a[i];
  a[n + 1] = a[1];
  for (int i = 1; i <= n; ++i)
    differ[i] = a[i + 1] - a[i];
  int k = sum / sum1;
  if (sum % sum1) {
    printf("NO");
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    double x = (differ[i] - k) / (-1.0 * n);
    if (x < 0) {
      printf("NO");
      return 0;
    }
    int xx = x;
    if (xx != x) {
      printf("NO");
      return 0;
    }
    ans[i] = x;
  }
  int ans1 = 0;
  for (int i = 1; i <= n; ++i)
    ans1 += ans[i];
  if (ans1 == k)
    printf("YES");
  else
    printf("NO");
  return 0;
}