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

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

using namespace std;
typedef long long ll;
typedef long double lb;
#define rep(i, j, n) for (ll i = j; i < (n); i++)
#define repr(i, j, n) for (ll i = j; i >= (n); i--)
#define all(x) (x).begin(), (x).end()
#define CLR(mat, f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a <= x && x < b)
#define out(ans) cout << ans << endl
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;
}
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll INF = 1LL << 60;
const ll m = 1LL << 32;

int main() {

  ll n;
  cin >> n;
  ll ma, mb;
  cin >> ma >> mb;
  ll a[n], b[n], c[n];
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  ll max_a = accumulate(a, a + n, 0) + 1;
  ll max_b = accumulate(b, b + n, 0) + 1;

  ll dp[max_a][max_b]; // aグラム][bグラム]
  rep(i, 0, max_a) rep(j, 0, max_b) dp[i][j] = INF;
  dp[0][0] = 0;
  ll ans = INF;
  rep(k, 0, n) {
    repr(i, max_a, a[k]) {
      repr(j, max_b, b[k]) {
        chmin(dp[i][j], dp[i - a[k]][j - b[k]] + c[k]);
        if (i * mb == j * ma) {
          chmin(ans, dp[i][j]);
        }
      }
    }
  }
  if (ans == INF)
    out(-1);
  else
    out(ans);

  return 0;
}


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

using namespace std;
typedef long long ll;
typedef long double lb;
#define rep(i, j, n) for (ll i = j; i < (n); i++)
#define repr(i, j, n) for (ll i = j; i >= (n); i--)
#define all(x) (x).begin(), (x).end()
#define CLR(mat, f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a <= x && x < b)
#define out(ans) cout << ans << endl
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;
}
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll INF = 1LL << 60;
const ll m = 1LL << 32;

int main() {

  ll n;
  cin >> n;
  ll ma, mb;
  cin >> ma >> mb;
  ll a[n], b[n], c[n];
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  ll max_a = accumulate(a, a + n, 0) + 1;
  ll max_b = accumulate(b, b + n, 0) + 1;

  ll dp[max_a][max_b]; // aグラム][bグラム]
  rep(i, 0, max_a) rep(j, 0, max_b) dp[i][j] = INF;
  dp[0][0] = 0;
  ll ans = INF;
  rep(k, 0, n) {
    repr(i, max_a - 1, a[k]) {
      repr(j, max_b - 1, b[k]) {
        chmin(dp[i][j], dp[i - a[k]][j - b[k]] + c[k]);
        if (i * mb == j * ma) {
          chmin(ans, dp[i][j]);
        }
      }
    }
  }
  if (ans == INF)
    out(-1);
  else
    out(ans);

  return 0;
}
Problem p03806 (C++)

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

using namespace std;
typedef long long ll;

const int INF = 1e9;

const int nmax = 40, abmax = 10;
ll dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main(int argc, const char *argv[]) {
  int N, MA, MB;
  cin >> N >> MA >> MB;
  vector<int> alist(N), blist(N), clist(N);
  for (int i = 0; i < N; i++) {
    cin >> alist[i] >> blist[i] >> clist[i];
  }

  for (int i = 0; i <= nmax; i++) {
    for (int j = 0; j <= nmax * abmax; j++) {
      for (int k = 0; k <= nmax * abmax; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= nmax * abmax; j++) {
      for (int k = 0; k <= nmax * abmax; k++) {
        if (dp[i][j][k] == INF)
          continue;
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        dp[i + 1][j + alist[i]][k + blist[i]] =
            min(dp[i + 1][j + alist[i]][k + blist[i]], dp[i][j][k] + clist[i]);
      }
    }
  }

  ll ans = INF;
  for (int i = 1; i <= N; i++) {
    int a = MA * i, b = MB * i;
    if (a > nmax * abmax || b > nmax * abmax) {
      continue;
    }
    ans = min(ans, dp[N][a][b]);
  }

  if (ans == INF) {
    cout << "-1" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;
typedef long long ll;

const int INF = 1e9;

const int nmax = 40, abmax = 10;
ll dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main(int argc, const char *argv[]) {
  int N, MA, MB;
  cin >> N >> MA >> MB;
  vector<int> alist(N), blist(N), clist(N);
  for (int i = 0; i < N; i++) {
    cin >> alist[i] >> blist[i] >> clist[i];
  }

  for (int i = 0; i <= nmax; i++) {
    for (int j = 0; j <= nmax * abmax; j++) {
      for (int k = 0; k <= nmax * abmax; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= nmax * abmax; j++) {
      for (int k = 0; k <= nmax * abmax; k++) {
        if (dp[i][j][k] == INF)
          continue;
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        dp[i + 1][j + alist[i]][k + blist[i]] =
            min(dp[i + 1][j + alist[i]][k + blist[i]], dp[i][j][k] + clist[i]);
      }
    }
  }

  ll ans = INF;
  for (int i = 1; i <= N; i++) {
    int a = MA * i, b = MB * i;
    if (a > nmax * abmax || b > nmax * abmax) {
      continue;
    }
    ans = min(ans, dp[N][a][b]);
  }

  if (ans == INF) {
    cout << "-1" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p03806 (C++)

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

int arr[40][500][500] = {};
int abc[40][3] = {};

int main(void) {

  int n, ma, mb;

  cin >> n >> ma >> mb;

  for (int i = 0; i < n; i++) {
    cin >> abc[i][0] >> abc[i][1] >> abc[i][2];
  }

  for (int i = 0; i < 40; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        arr[i][j][k] = 1000000;
      }
    }
  }

  arr[0][abc[0][0]][abc[0][1]] = abc[0][2];

  for (int i = 1; i < n; i++) {
    arr[i][abc[i][0]][abc[i][1]] = min(arr[i][abc[i][0]][abc[i][1]], abc[i][2]);
    for (int j = 499; j >= 0; j--) {
      for (int k = 499; k >= 0; k--) {
        arr[i][j][k] = arr[i - 1][j][k];
        if (arr[i - 1][j][k] != 0) {
          arr[i][j + abc[i][0]][k + abc[i][1]] =
              min(arr[i - 1][j][k] + abc[i][2],
                  arr[i][j + abc[i][0]][k + abc[i][1]]);
        }
      }
    }
  }

  int res = 1000000;
  for (int i = 1; ma * i < 500 && mb * i < 500; i++) {
    // cout << arr[n - 1][i*ma][i*mb] << endl;
    res = min(res, arr[n - 1][i * ma][i * mb]);
  }

  if (res == 1000000)
    cout << -1 << endl;
  else
    cout << res << endl;
}


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

int arr[40][500][500] = {};
int abc[40][3] = {};

int main(void) {

  int n, ma, mb;

  cin >> n >> ma >> mb;

  for (int i = 0; i < n; i++) {
    cin >> abc[i][0] >> abc[i][1] >> abc[i][2];
  }

  for (int i = 0; i < 40; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        arr[i][j][k] = 1000000;
      }
    }
  }

  arr[0][abc[0][0]][abc[0][1]] = abc[0][2];

  for (int i = 1; i < n; i++) {
    arr[i][abc[i][0]][abc[i][1]] = min(arr[i][abc[i][0]][abc[i][1]], abc[i][2]);
    for (int j = 450; j >= 0; j--) {
      for (int k = 450; k >= 0; k--) {
        arr[i][j][k] = min(arr[i - 1][j][k], arr[i][j][k]);
        if (arr[i - 1][j][k] != 1000000) {
          arr[i][j + abc[i][0]][k + abc[i][1]] =
              min(arr[i - 1][j][k] + abc[i][2],
                  arr[i][j + abc[i][0]][k + abc[i][1]]);
        }
      }
    }
  }

  int res = 1000000;
  for (int i = 1; ma * i < 500 && mb * i < 500; i++) {
    // cout << arr[n - 1][i*ma][i*mb] << endl;
    res = min(res, arr[n - 1][i * ma][i * mb]);
  }

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

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

int v[40][500][500] = {1, 0};

int gcd(int a, int b) {
  if (a == 0 || b == 0)
    return max(a, b);
  int big = max(a, b), small = min(a, b);
  return gcd(small, big % small);
}

int main() {
  int n, ma, mb, answer = 5000;
  cin >> n >> ma >> mb;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        if (v[i][j][k] != 0) {
          if (v[i + 1][j + a][k + b] != 0)
            v[i + 1][j + a][k + b] =
                min(v[i + 1][j + a][k + b], v[i][j][k] + c);
          else
            v[i + 1][j + a][k + b] = v[i][j][k] + c;

          if (v[i + 1][j][k] != 0)
            v[i + 1][j][k] = min(v[i + 1][j][k], v[i][j][k]);
          else
            v[i + 1][j][k] = v[i][j][k];
        }
      }
    }
  }

  for (int j = 1; j < 500; j++) {
    for (int k = 1; k < 500; k++) {
      if (v[n][j][k] != 0 && j / gcd(j, k) == ma && k / gcd(j, k) == mb) {
        answer = min(answer, v[n][j][k] - 1);
      }
    }
  }

  if (answer != 5000)
    cout << answer;
  else
    cout << -1;
  return 0;
}

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

int v[50][500][500] = {1, 0};

int gcd(int a, int b) {
  if (a == 0 || b == 0)
    return max(a, b);
  int big = max(a, b), small = min(a, b);
  return gcd(small, big % small);
}

int main() {
  int n, ma, mb, answer = 5000;
  cin >> n >> ma >> mb;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        if (v[i][j][k] != 0) {
          if (v[i + 1][j + a][k + b] != 0)
            v[i + 1][j + a][k + b] =
                min(v[i + 1][j + a][k + b], v[i][j][k] + c);
          else
            v[i + 1][j + a][k + b] = v[i][j][k] + c;

          if (v[i + 1][j][k] != 0)
            v[i + 1][j][k] = min(v[i + 1][j][k], v[i][j][k]);
          else
            v[i + 1][j][k] = v[i][j][k];
        }
      }
    }
  }

  for (int j = 1; j < 500; j++) {
    for (int k = 1; k < 500; k++) {
      if (v[n][j][k] != 0 && j / gcd(j, k) == ma && k / gcd(j, k) == mb) {
        answer = min(answer, v[n][j][k] - 1);
      }
    }
  }

  if (answer != 5000)
    cout << answer;
  else
    cout << -1;
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
const int M = 100010;
int INF = 1000000007;
using ll = long long;

int main() {
  const int max_M = 41, max_cM = 40;
  int n, ma, mb;
  int a[max_M], b[max_M], c[max_M];
  cin >> n >> ma >> mb;
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  // dp[i][j][k]: i番目を選んだ時ma=j,mb=k;
  int dp[max_M][max_cM][max_cM];
  rep(i, 0, n + 1) rep(j, 0, max_cM) rep(k, 0, max_cM) dp[i][j][k] = INF;
  dp[0][0][0] = 0;
  rep(i, 0, n) {
    rep(j, 0, max_cM) {
      rep(k, 0, max_cM) {
        if (dp[i][j][k] == INF)
          continue;
        // i+1番目を選ばない.
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        // i+1番目を選ぶ.
        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 = INF;
  rep(j, 1, max_cM) {
    rep(k, 1, max_cM) {
      // Ma:Mb=j:kとなっているときans候補.
      if (ma * k == mb * j)
        ans = min(ans, dp[n][j][k]);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
const int M = 100010;
int INF = 1000000007;
using ll = long long;

int main() {
  const int max_M = 41, max_cM = 401;
  int n, ma, mb;
  int a[max_M], b[max_M], c[max_M];
  cin >> n >> ma >> mb;
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  // dp[i][j][k]: i番目を選んだ時ma=j,mb=k;
  int dp[max_M][max_cM][max_cM];
  rep(i, 0, n + 1) rep(j, 0, max_cM) rep(k, 0, max_cM) dp[i][j][k] = INF;
  dp[0][0][0] = 0;
  rep(i, 0, n) {
    rep(j, 0, max_cM) {
      rep(k, 0, max_cM) {
        if (dp[i][j][k] == INF)
          continue;
        // i+1番目を選ばない.
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        // i+1番目を選ぶ.
        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 = INF;
  rep(j, 1, max_cM) {
    rep(k, 1, max_cM) {
      // Ma:Mb=j:kとなっているときans候補.
      if (ma * k == mb * j)
        ans = min(ans, dp[n][j][k]);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dp[41][401][401];
int a[40], b[40], c[40];
int main() {
  int n, Ma, Mb;
  cin >> n >> Ma >> Mb;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < n + 1; i++) {
    for (int ca = 0; ca < 401; ca++) {
      for (int cb = 0; cb < 401; cb++) {
        dp[i][ca][cb] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < n + 1; i++) {
    for (int ca = 0; ca < 401; ca++) {
      for (int cb = 0; cb < 401; cb++) {
        if (dp[i][ca][cb] == INF) {
          continue;
        }
        dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb]);
        dp[i + 1][ca + a[i]][cb + b[i]] =
            min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i]);
      }
    }
  }
  int ans = INF;
  for (int ca = 1; ca < 401; ca++) {
    for (int cb = 1; cb < 401; cb++) {
      if (ca * Mb == cb * Ma) {
        ans = min(ans, dp[n][ca][cb]);
      }
    }
  }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dp[41][401][401];
int a[40], b[40], c[40];
int main() {
  int n, Ma, Mb;
  cin >> n >> Ma >> Mb;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < n + 1; i++) {
    for (int ca = 0; ca < 401; ca++) {
      for (int cb = 0; cb < 401; cb++) {
        dp[i][ca][cb] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int ca = 0; ca < 401; ca++) {
      for (int cb = 0; cb < 401; cb++) {
        if (dp[i][ca][cb] == INF) {
          continue;
        }
        dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb]);
        dp[i + 1][ca + a[i]][cb + b[i]] =
            min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i]);
      }
    }
  }
  int ans = INF;
  for (int ca = 1; ca < 401; ca++) {
    for (int cb = 1; cb < 401; cb++) {
      if (ca * Mb == cb * Ma) {
        ans = min(ans, dp[n][ca][cb]);
      }
    }
  }
  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;
#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;
const double PI = acos(-1);
const double EPS = 1e-9;
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;
}

int dp[41][410][410];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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]; }
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  rep(i, 0, N) {
    rep(j, 0, 410) rep(k, 0, 410) {
      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);
  rep(i, 1, 410) rep(j, 1, 410) if (j * Ma == i * Mb) chmin(ans, dp[N][i][j]);
  cout << (ans == INF ? -1 : 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;
const double PI = acos(-1);
const double EPS = 1e-9;
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;
}

int dp[50][450][450];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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]; }
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  rep(i, 0, N) {
    rep(j, 0, 410) rep(k, 0, 410) {
      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);
  rep(i, 1, 410) rep(j, 1, 410) if (j * Ma == i * Mb) chmin(ans, dp[N][i][j]);
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define dump(x) cout << (x) << endl;
typedef long long Int;

Int mod = 1e9 + 7;
Int MOD = 998244353;

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  vector<int> a(n), b(n), c(n);
  for (Int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  int dp[41][401][401] = {};
  for (Int i = 0; i <= n; i++) {
    for (Int j = 0; j <= 400; j++) {
      for (Int k = 0; k <= 400; k++) {
        dp[i][j][k] = 1e9;
      }
    }
  }
  dp[0][0][0] = 0;
  for (Int i = 0; i < n; i++) {
    for (Int j = 0; j < 400; j++) {
      for (Int k = 0; k < 400; k++) {
        dp[i + 1][j][k] = min(dp[i][j][k], dp[i + 1][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 res = 1e9;
  for (Int i = 1; i <= 400; i++) {
    for (Int j = 1; j <= 400; j++) {
      if (i * mb == j * ma)
        res = min(res, dp[n][i][j]);
    }
  }
  dump(res == 1e9 ? -1 : res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define dump(x) cout << (x) << endl;
typedef long long Int;

Int mod = 1e9 + 7;
Int MOD = 998244353;

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  vector<int> a(n), b(n), c(n);
  for (Int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  int dp[41][401][401] = {};
  for (Int i = 0; i <= n; i++) {
    for (Int j = 0; j <= 400; j++) {
      for (Int k = 0; k <= 400; k++) {
        dp[i][j][k] = 1e9;
      }
    }
  }
  dp[0][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 (dp[i][j][k] == 1e9)
          continue;
        dp[i + 1][j][k] = min(dp[i][j][k], dp[i + 1][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 res = 1e9;
  for (Int i = 1; i <= 400; i++) {
    for (Int j = 1; j <= 400; j++) {
      if (i * mb == j * ma)
        res = min(res, dp[n][i][j]);
    }
  }
  dump(res == 1e9 ? -1 : res);
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m[2];
int a[51], b[51], c[51];
int dp[2][401][401];
int main(void) {
  scanf("%d%d%d", &n, &m[0], &m[1]);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  }
  for (int i = 0; i < 2; 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;
  int lobi = 0;
  int lobi2 = 0;
  int now = 1;
  int prev = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= lobi; j++) {
      for (int k = 0; k <= lobi2; k++) {
        for (int l = 0; l <= 1; l++) {
          dp[now][j + a[i] * l][k + b[i] * l] = min(
              dp[now][j + a[i] * l][k + b[i] * l], dp[prev][j][k] + c[i] * l);
        }
      }
    }
    swap(now, prev);
    lobi = lobi + a[i] * 10;
    lobi2 = lobi2 + b[i] * 10;
    for (int j = 0; j <= lobi; j++) {
      for (int k = 0; k <= lobi2; k++) {
        dp[now][j][k] = INF;
      }
    }
  }
  int ans = INF;
  for (int i = 1; i <= 400; i++) {
    int j = i * m[0];
    int k = i * m[1];
    if (j <= 400 && k <= 400) {
      ans = min(ans, dp[prev][j][k]);
    }
  }
  printf("%d\n", ans == INF ? -1 : ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m[2];
int a[51], b[51], c[51];
int dp[2][401][401];
int main(void) {
  scanf("%d%d%d", &n, &m[0], &m[1]);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  }
  for (int i = 0; i < 2; 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;
  int lobi = 0;
  int lobi2 = 0;
  int now = 1;
  int prev = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= lobi; j++) {
      for (int k = 0; k <= lobi2; k++) {
        for (int l = 0; l <= 1; l++) {
          dp[now][j + a[i] * l][k + b[i] * l] = min(
              dp[now][j + a[i] * l][k + b[i] * l], dp[prev][j][k] + c[i] * l);
        }
      }
    }
    swap(now, prev);
    lobi = lobi + a[i];
    lobi2 = lobi2 + b[i];
    for (int j = 0; j <= lobi; j++) {
      for (int k = 0; k <= lobi2; k++) {
        dp[now][j][k] = INF;
      }
    }
  }
  int ans = INF;
  for (int i = 1; i <= 400; i++) {
    int j = i * m[0];
    int k = i * m[1];
    if (j <= 400 && k <= 400) {
      ans = min(ans, dp[prev][j][k]);
    }
  }
  printf("%d\n", ans == INF ? -1 : ans);
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int DP[50][500][500];

int main() {
  for (int i = 0; i < 50; ++i) {
    for (int j = 0; j < 500; ++j) {
      for (int k = 0; k < 500; ++k) {
        DP[i][j][k] = inf;
      }
    }
  }
  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];
  DP[0][0][0] = 0;
  DP[0][a[0]][b[0]] = c[0];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 500; ++j) {
      for (int k = 0; k < 500; ++k) {
        if (DP[i][j][k] < inf) {
          DP[i + 1][j + a[i + 1]][k + b[i + 1]] = min(
              DP[i + 1][j + a[i + 1]][k + b[i + 1]], DP[i][j][k] + c[i + 1]);
          DP[i + 1][j][k] = min(DP[i + 1][j][k], DP[i][j][k]);
        }
      }
    }
  }
  int ans = inf;
  for (int i = 1; (i * Ma < 500) && (i * Mb < 500); ++i) {
    ans = min(ans, DP[N - 1][i * Ma][i * Mb]);
  }
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int DP[50][500][500];

int main() {
  for (int i = 0; i < 50; ++i) {
    for (int j = 0; j < 500; ++j) {
      for (int k = 0; k < 500; ++k) {
        DP[i][j][k] = inf;
      }
    }
  }
  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];
  DP[0][0][0] = 0;
  DP[0][a[0]][b[0]] = c[0];
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 0; j < 500; ++j) {
      for (int k = 0; k < 500; ++k) {
        if (DP[i][j][k] < inf) {
          DP[i + 1][j + a[i + 1]][k + b[i + 1]] = min(
              DP[i + 1][j + a[i + 1]][k + b[i + 1]], DP[i][j][k] + c[i + 1]);
          DP[i + 1][j][k] = min(DP[i + 1][j][k], DP[i][j][k]);
        }
      }
    }
  }
  int ans = inf;
  for (int i = 1; (i * Ma < 500) && (i * Mb < 500); ++i) {
    ans = min(ans, DP[N - 1][i * Ma][i * Mb]);
  }
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#define MAX_N 100001
#define INF 2147483647
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
void init(int n);
int find(int n);
void unite(int x, int y);
bool same(int x, int y);
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
struct data {
  int a;
  int b;
  int c;
  int key;
};

using namespace std;
int main() {
  int Ma, Mb, res = 55555555, N, a[100], b[100], c[100], dp[41][411][411];
  cin >> N >> Ma >> Mb;
  REP(i, N) cin >> a[i] >> b[i] >> c[i];

  REP(i, 41) REP(j, 401) REP(k, 401) dp[i][j][k] = 55555555;
  dp[0][0][0] = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= 400; j++)
      for (int k = 0; k <= 400; k++) {
        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][j][k] + c[i], dp[i + 1][j + a[i]][k + b[i]]);
      }
  for (int j = 1; j <= 400; j++)
    for (int k = 1; k <= 400; k++) {
      if (j * Mb == k * Ma) {
        res = min(res, dp[N][j][k]);
      }
    }
  if (res == 55555555)
    cout << -1 << endl;
  else
    cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#define MAX_N 100001
#define INF 2147483647
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
void init(int n);
int find(int n);
void unite(int x, int y);
bool same(int x, int y);
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
struct data {
  int a;
  int b;
  int c;
  int key;
};

using namespace std;
int main() {
  int Ma, Mb, res = 55555555, N, a[101], b[101], c[101], dp[42][412][412];
  cin >> N >> Ma >> Mb;
  REP(i, N) cin >> a[i] >> b[i] >> c[i];

  REP(i, 41) REP(j, 401) REP(k, 401) dp[i][j][k] = 55555555;
  dp[0][0][0] = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= 400; j++)
      for (int k = 0; k <= 400; k++) {
        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][j][k] + c[i], dp[i + 1][j + a[i]][k + b[i]]);
      }
  for (int j = 1; j <= 400; j++)
    for (int k = 1; k <= 400; k++) {
      if (j * Mb == k * Ma) {
        res = min(res, dp[N][j][k]);
      }
    }
  if (res == 55555555)
    cout << -1 << endl;
  else
    cout << res << endl;
  return 0;
}
Problem p03806 (C++)

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = n - 1; i >= m; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

typedef long long ll;
using namespace std;

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a - (a / b) * b);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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]; }
  int dp[41][400][400];
  REP(i, n + 1) REP(j, 401) REP(k, 401) dp[i][j][k] = INF;

  dp[0][0][0] = 0;
  REP(i, n) {
    REP(j, 10 * n) {
      REP(k, 10 * n) {
        if (j < a[i] || k < b[i])
          dp[i + 1][j][k] = dp[i][j][k];
        else
          dp[i + 1][j][k] = min(dp[i][j][k], dp[i][j - a[i]][k - b[i]] + c[i]);
      }
    }
  }
  int ans = INF;
  FOR(i, 1, INF) {
    if (i * ma > 400 || i * mb > 400)
      break;
    if (i * ma == 1 && i * mb == 1)
      continue;
    ans = min(ans, dp[n][i * ma][i * mb]);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = n - 1; i >= m; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

typedef long long ll;
using namespace std;

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a - (a / b) * b);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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]; }
  int dp[41][401][401];
  REP(i, n + 1) REP(j, 401) REP(k, 401) dp[i][j][k] = INF;

  dp[0][0][0] = 0;
  REP(i, n) {
    REP(j, 10 * n) {
      REP(k, 10 * n) {
        if (j < a[i] || k < b[i])
          dp[i + 1][j][k] = dp[i][j][k];
        else
          dp[i + 1][j][k] = min(dp[i][j][k], dp[i][j - a[i]][k - b[i]] + c[i]);
      }
    }
  }
  int ans = INF;
  FOR(i, 1, INF) {
    if (i * ma > 400 || i * mb > 400)
      break;
    if (i * ma == 1 && i * mb == 1)
      continue;
    ans = min(ans, dp[n][i * ma][i * 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>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF 1000000000LL
using namespace std;
typedef long long ll;

ll N, MA, MB;
vector<ll> A[2], B[2], C[2];
ll dp[2][201][201];

int main(void) {
  cin >> N >> MA >> MB;
  REP(i, 0, N) {
    ll a, b, c;
    cin >> a >> b >> c;
    A[i % 2].push_back(a);
    B[i % 2].push_back(b);
    C[i % 2].push_back(c);
  }

  if (N == 1) {
    cout << (MA * B[0][0] == MB * A[0][0] ? C[0][0] : -1) << endl;
  } else {
    REP(p, 0, 2) {
      ll n = A[p].size();
      REP(i, 0, 201) REP(j, 0, 201) dp[p][i][j] = INF;
      REP(i, 0, 1 << n) {
        ll a = 0, b = 0, c = 0;
        REP(j, 0, n) if (i & (1 << j)) a += A[p][j], b += B[p][j], c += C[p][j];
        dp[p][a][b] = min(dp[p][a][b], c);
      }
    }
    ll ans = INF;
    REP(i, 0, 201) REP(j, 0, 201) REP(k, 0, 201) if ((i + k) % MA == 0) {
      ll l = MB * ((i + k) / MA) - j;
      if (i == 0 && j == 0 && k == 0 && l == 0)
        continue;
      ans = min(ans, dp[0][i][j] + dp[1][k][l]);
    }
    cout << (ans != INF ? ans : -1) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF 1000000000LL
using namespace std;
typedef long long ll;

ll N, MA, MB;
vector<ll> A[2], B[2], C[2];
ll dp[2][201][201];

int main(void) {
  cin >> N >> MA >> MB;
  REP(i, 0, N) {
    ll a, b, c;
    cin >> a >> b >> c;
    A[i % 2].push_back(a);
    B[i % 2].push_back(b);
    C[i % 2].push_back(c);
  }

  if (N == 1) {
    cout << (MA * B[0][0] == MB * A[0][0] ? C[0][0] : -1) << endl;
  } else {
    REP(p, 0, 2) {
      ll n = A[p].size();
      REP(i, 0, 201) REP(j, 0, 201) dp[p][i][j] = INF;
      REP(i, 0, 1 << n) {
        ll a = 0, b = 0, c = 0;
        REP(j, 0, n) if (i & (1 << j)) a += A[p][j], b += B[p][j], c += C[p][j];
        dp[p][a][b] = min(dp[p][a][b], c);
      }
    }
    ll ans = INF;
    REP(i, 0, 201) REP(j, 0, 201) REP(k, 0, 201) if ((i + k) % MA == 0) {
      ll l = MB * ((i + k) / MA) - j;
      if (l > 200 || dp[0][i][j] + dp[1][k][l] == 0)
        continue;
      ans = min(ans, dp[0][i][j] + dp[1][k][l]);
    }
    cout << (ans != INF ? ans : -1) << endl;
  }
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = 999999999;
int dp[41][101][101];

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

  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<vector<int>> chem(N, vector<int>(3));
  REP(i, N) REP(j, 3) cin >> chem[i][j];

  REP(i, 41) REP(j, 101) REP(k, 101) dp[i][j][k] = INF;
  dp[0][0][0] = 0;

  REP(i, N) REP(a, 101) REP(b, 101) {
    // 薬品をとらない
    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b]);
    // 薬品をとる
    dp[i + 1][a + chem[i][0]][b + chem[i][1]] = min(
        dp[i + 1][a + chem[i][0]][b + chem[i][1]], dp[i][a][b] + chem[i][2]);
  }

  int ans = INF;
  FOR(i, 1, 101) FOR(j, 1, 101) {
    if (Ma * j == Mb * i) {
      ans = min(ans, dp[N][i][j]);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = 999999999;
int dp[41][101][101];

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

  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<vector<int>> chem(N, vector<int>(3));
  REP(i, N) REP(j, 3) cin >> chem[i][j];

  REP(i, 41) REP(j, 101) REP(k, 101) dp[i][j][k] = INF;
  dp[0][0][0] = 0;

  REP(i, N) REP(a, 91) REP(b, 91) {
    // 薬品をとらない
    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b]);
    // 薬品をとる
    dp[i + 1][a + chem[i][0]][b + chem[i][1]] = min(
        dp[i + 1][a + chem[i][0]][b + chem[i][1]], dp[i][a][b] + chem[i][2]);
  }

  int ans = INF;
  FOR(i, 1, 101) FOR(j, 1, 101) {
    if (Ma * j == Mb * i) {
      ans = min(ans, dp[N][i][j]);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << "\n";

  return 0;
}
Problem p03806 (Python)

Buggy submission (status: Time Limit Exceeded):
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))


def cost(bits):
    sa = sb = sc = 0
    for i, (a, b, c) in enumerate(ABC):
        if not bits & 1 << i:
            continue
        sa += a
        sb += b
        sc += c
    return sc if sa * Mb == sb * Ma else inf


ans = min([cost(bits) for bits in range(1, 1 << N)])
print(ans if ans < inf else -1)


Fixed submission:
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410, -1, -1):
        for j in range(410, -1, -1):
            if dp[i][j] < inf:
                dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
    ans = min(ans, dp[Ma * n][Mb * n])
    n += 1
print(ans if ans < inf else -1)
Problem p03806 (Python)

Buggy submission (status: Runtime Error):
N, Ma, Mb = map(int, input().split())
items = []
for i in range(N):
    a, b, c = map(int, input().split())
    items.append((a, b, c))

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [
    [[inf for _ in range(a_sum + 40)] for _ in range(b_sum + 40)] for i in range(N + 1)
]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)


Fixed submission:
N, Ma, Mb = map(int, input().split())
items = []
for i in range(N):
    a, b, c = map(int, input().split())
    items.append((a, b, c))

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [
    [[inf for _ in range(a_sum + 50)] for _ in range(b_sum + 50)] for i in range(N + 1)
]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cstdio>

using namespace std;

const int NMax = 40;
const int abMax = 10;
const int cMax = 100;

const int INF = cMax * NMax + 1;

const int abNMax = abMax * NMax;

array<int, NMax> a;
array<int, NMax> b;
array<int, NMax> c;

array<array<array<int, abNMax + 1>, abNMax + 1>, NMax + 1> dp;

int main() {
  int N, Ma, Mb;
  if (scanf("%d%d%d", &N, &Ma, &Mb) < 3)
    return 0;

  // 0-indexed
  for (int i = 0; i < N; i++) {
    if (scanf("%d%d%d", &a[i], &b[i], &c[i]) < 3)
      return 0;
  }

  // none dp[0][0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= abNMax; j++) {
      for (int k = 0; k <= abNMax; k++) {
        dp[i][j][k] = INF;
      }
    }
  }

  // none
  dp[0][0][0] = 0;

  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= abNMax; j++) {
      for (int k = 0; k <= abNMax; k++) {
        if (dp[i][j][k] == INF)
          continue;

        // not select
        dp[i + 1][j][k] = min(dp[i][j][k], dp[i + 1][j][k]);

        // select
        dp[i + 1][j + a[i]][k + b[i]] =
            min(dp[i][j][k] + c[i], dp[i + 1][j + a[i]][k + b[i]]);
      }
    }
  }

  int ans = INF;
  for (int i = 1; i < abNMax; i++) {
    for (int j = 1; j < abNMax; j++) {
      if (i * Mb == j * Ma)
        ans = min(ans, dp[N][i][j]);
    }
  }

  if (ans == INF)
    printf("%d\n", -1);
  else
    printf("%d\n", ans);

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cstdio>

using namespace std;

const int NMax = 40;
const int abMax = 10;
const int cMax = 100;

const int INF = cMax * NMax + 1;

const int abNMax = abMax * NMax;

array<int, NMax> a;
array<int, NMax> b;
array<int, NMax> c;

array<array<array<int, abNMax + 1>, abNMax + 1>, NMax + 1> dp;

int main() {
  int N, Ma, Mb;
  if (scanf("%d%d%d", &N, &Ma, &Mb) < 3)
    return 0;

  // 0-indexed
  for (int i = 0; i < N; i++) {
    if (scanf("%d%d%d", &a[i], &b[i], &c[i]) < 3)
      return 0;
  }

  // none dp[0][0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= abNMax; j++) {
      for (int k = 0; k <= abNMax; k++) {
        dp[i][j][k] = INF;
      }
    }
  }

  // none
  dp[0][0][0] = 0;

  // update dp[i + 1]
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= abNMax; j++) {
      for (int k = 0; k <= abNMax; k++) {
        if (dp[i][j][k] == INF)
          continue;

        // not select
        dp[i + 1][j][k] = min(dp[i][j][k], dp[i + 1][j][k]);

        // select
        dp[i + 1][j + a[i]][k + b[i]] =
            min(dp[i][j][k] + c[i], dp[i + 1][j + a[i]][k + b[i]]);
      }
    }
  }

  int ans = INF;
  for (int i = 1; i < abNMax; i++) {
    for (int j = 1; j < abNMax; j++) {
      if (i * Mb == j * Ma)
        ans = min(ans, dp[N][i][j]);
    }
  }

  if (ans == INF)
    printf("%d\n", -1);
  else
    printf("%d\n", ans);

  return 0;
}
Problem p03806 (C++)

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

using namespace std;

int dp[45][405][405];
int a[40], b[40], c[40];
int suma, sumb;

int main() {
  int n, A, B;
  scanf("%d %d %d", &n, &A, &B);
  memset(dp, 0, sizeof dp);
  suma = sumb = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    suma += a[i];
    sumb += b[i];
    dp[i][a[i]][b[i]] = c[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= suma; j++) {
      for (int k = 1; k <= sumb; k++) {
        if (dp[i - 1][j - a[i]][k - b[i]] != 0 &&
            ((dp[i - 1][j][k] > dp[i - 1][j - a[i]][k - b[i]] + c[i]) ||
             dp[i - 1][j][k] == 0))
          dp[i][j][k] = dp[i - 1][j - a[i]][k - b[i]] + c[i];
        else
          dp[i][j][k] = dp[i - 1][j][k];
      }
    }
    if ((dp[i][a[i]][b[i]] > c[i] && dp[i][a[i]][b[i]] != 0) ||
        dp[i][a[i]][b[i]] == 0)
      dp[i][a[i]][b[i]] = c[i];
  }
  int ans = 5000;
  /*
      for(int i = 1; i <= 3; i++) {
          for(int j = 1; j <= 6; j++) {
              for(int k = 1; k <= 6; k++) {
                  printf("%d ", dp[i][j][k]);
              }
              printf("\n");
          }
          printf("\n");
      }
  */
  for (int i = 1;; i++) {
    if (A * i > suma || B * i > sumb)
      break;
    if (dp[n][A * i][B * i] < ans && dp[n][A * i][B * i] != 0)
      ans = dp[n][A * i][B * i];
  }
  if (ans == 5000)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}


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

using namespace std;

int dp[45][405][405];
int a[40], b[40], c[40];
int suma, sumb;

int main() {
  int n, A, B;
  scanf("%d %d %d", &n, &A, &B);
  memset(dp, 0, sizeof dp);
  suma = sumb = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    suma += a[i];
    sumb += b[i];
    dp[i][a[i]][b[i]] = c[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= suma; j++) {
      for (int k = 1; k <= sumb; k++) {
        if (j >= a[i] && k >= b[i]) {
          if (dp[i - 1][j - a[i]][k - b[i]] != 0 &&
              ((dp[i - 1][j][k] > dp[i - 1][j - a[i]][k - b[i]] + c[i]) ||
               dp[i - 1][j][k] == 0))
            dp[i][j][k] = dp[i - 1][j - a[i]][k - b[i]] + c[i];
          else
            dp[i][j][k] = dp[i - 1][j][k];
        } else
          dp[i][j][k] = dp[i - 1][j][k];
      }
    }
    if ((dp[i][a[i]][b[i]] > c[i] && dp[i][a[i]][b[i]] != 0) ||
        dp[i][a[i]][b[i]] == 0)
      dp[i][a[i]][b[i]] = c[i];
  }
  int ans = 5000;
  /*
      for(int i = 1; i <= 3; i++) {
          for(int j = 1; j <= 6; j++) {
              for(int k = 1; k <= 6; k++) {
                  printf("%d ", dp[i][j][k]);
              }
              printf("\n");
          }
          printf("\n");
      }
  */
  for (int i = 1;; i++) {
    if (A * i > suma || B * i > sumb)
      break;
    if (dp[n][A * i][B * i] < ans && dp[n][A * i][B * i] != 0)
      ans = dp[n][A * i][B * i];
  }
  if (ans == 5000)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
Problem p03806 (C++)

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

int main() {
  std::array<std::array<std::int_fast64_t, 401>, 401> dp;
  for (std::array<std::int_fast64_t, 401> &line : dp) {
    line.fill(-1);
  }
  dp[0][0] = 0;

  std::int_fast64_t n, ma, mb;
  std::cin >> n >> ma >> mb;
  for (std::int_fast64_t i = 0; i < n; ++i) {
    std::int_fast64_t a, b, c;
    std::cin >> a >> b >> c;
    for (std::int_fast64_t x = i * 40; x >= 0; --x) {
      for (std::int_fast64_t y = i * 40; y >= 0; --y) {
        if (dp[x][y] >= 0) {
          dp[x + a][y + b] = dp[x + a][y + b] >= 0
                                 ? std::min(dp[x][y] + c, dp[x + a][y + b])
                                 : dp[x][y] + c;
        }
      }
    }
  }

  std::int_fast64_t result = -1;
  for (std::int_fast64_t x = ma, y = mb; x <= 400 && y <= 400;
       x += ma, y += mb) {
    if (dp[x][y] >= 0) {
      result = result >= 0 ? std::min(dp[x][y], result) : dp[x][y];
    }
  }

  std::cout << result << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>

int main() {
  std::array<std::array<std::int_fast64_t, 401>, 401> dp;
  for (std::array<std::int_fast64_t, 401> &line : dp) {
    line.fill(-1);
  }
  dp[0][0] = 0;

  std::int_fast64_t n, ma, mb;
  std::cin >> n >> ma >> mb;
  for (std::int_fast64_t i = 0; i < n; ++i) {
    std::int_fast64_t a, b, c;
    std::cin >> a >> b >> c;
    for (std::int_fast64_t x = i * 10; x >= 0; --x) {
      for (std::int_fast64_t y = i * 10; y >= 0; --y) {
        if (dp[x][y] >= 0) {
          dp[x + a][y + b] = dp[x + a][y + b] >= 0
                                 ? std::min(dp[x][y] + c, dp[x + a][y + b])
                                 : dp[x][y] + c;
        }
      }
    }
  }

  std::int_fast64_t result = -1;
  for (std::int_fast64_t x = ma, y = mb; x <= 400 && y <= 400;
       x += ma, y += mb) {
    if (dp[x][y] >= 0) {
      result = result >= 0 ? std::min(dp[x][y], result) : dp[x][y];
    }
  }

  std::cout << result << std::endl;
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
#define Mod 1000000007
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
#define INF 1234567890

int main() {
  ll i, j, k;
  ll n, ma, mb;
  cin >> n >> ma >> mb;
  VVI dp(404, VI(404, INF));
  dp[0][0] = 0;
  for (k = 0; k < n; k++) {
    ll a, b, c;
    cin >> a >> b >> c;
    for (i = 404; i >= a; i--) {
      for (j = 404; j >= b; j--) {
        dp[i][j] = min(dp[i][j], dp[i - a][j - b] + c);
      }
    }
  }
  ll ans = INF;
  for (i = 1; i * max(ma, mb) < 404; i++) {
    ans = min(ans, dp[i * ma][i * mb]);
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
#define Mod 1000000007
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
#define INF 1234567890

int main() {
  ll i, j, k;
  ll n, ma, mb;
  cin >> n >> ma >> mb;
  VVI dp(404, VI(404, INF));
  dp[0][0] = 0;
  for (k = 0; k < n; k++) {
    ll a, b, c;
    cin >> a >> b >> c;
    for (i = 403; i >= a; i--) {
      for (j = 403; j >= b; j--) {
        dp[i][j] = min(dp[i][j], dp[i - a][j - b] + c);
      }
    }
  }
  ll ans = INF;
  for (i = 1; i * max(ma, mb) < 404; i++) {
    ans = min(ans, dp[i * ma][i * mb]);
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}