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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF 1e9
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}

using ll = long long;
const ll mod = LLONG_MAX;

int a[41], b[41], c[41];
int dp[20][500][500];
int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i]; }
  rep(i, n + 1) rep(j, 410) rep(k, 410) dp[i][j][k] = INF;

  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 40 * 10; j++) {
      for (int k = 0; k <= 40 * 10; 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 + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }

  int ans = INF;
  for (int i = 1; i <= 40 * 10; i++) {
    for (int j = 1; j <= 40 * 10; j++) {
      if (i * mb == j * ma)
        ans = min(ans, dp[n][i][j]);
    }
  }
  if (ans == INF)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF 1e9
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}

using ll = long long;
const ll mod = LLONG_MAX;

int a[41], b[41], c[41];
int dp[50][500][500];
int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i]; }
  rep(i, n + 1) rep(j, 410) rep(k, 410) dp[i][j][k] = INF;

  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 40 * 10; j++) {
      for (int k = 0; k <= 40 * 10; 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 + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }

  int ans = INF;
  for (int i = 1; i <= 40 * 10; i++) {
    for (int j = 1; j <= 40 * 10; j++) {
      if (i * mb == j * ma)
        ans = min(ans, dp[n][i][j]);
    }
  }
  if (ans == INF)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}
Problem p03806 (C++)

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 51

int main() {
  int n, ma, mb;
  int a, b, c;
  int dp[401][401];

  scanf("%d%d%d", &n, &ma, &mb);

  for (int i = 0; i <= n * 10; i++) {
    for (int j = 0; j <= n * 10; j++) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &a, &b, &c);

    for (int j = n * 10; j >= 0; j--) {
      for (int k = n * 10; k >= 0; k--) {
        dp[j + a][k + b] = min(dp[j + a][k + b], dp[j][k] + c);
      }
    }
  }

  int ans = INF;

  for (int i = 1; ma * i <= n * 10 && mb * i <= n * 10; i++) {
    ans = min(ans, dp[ma * i][mb * i]);
  }

  if (ans == INF)
    ans = -1;

  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 51

int main() {
  int n, ma, mb;
  int a, b, c;
  int dp[501][501];

  scanf("%d%d%d", &n, &ma, &mb);

  for (int i = 0; i <= n * 10; i++) {
    for (int j = 0; j <= n * 10; j++) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &a, &b, &c);

    for (int j = n * 10; j >= 0; j--) {
      for (int k = n * 10; k >= 0; k--) {
        dp[j + a][k + b] = min(dp[j + a][k + b], dp[j][k] + c);
      }
    }
  }

  int ans = INF;

  for (int i = 1; ma * i <= n * 10 && mb * i <= n * 10; i++) {
    ans = min(ans, dp[ma * i][mb * i]);
  }

  if (ans == INF)
    ans = -1;

  printf("%d\n", ans);

  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int dp[401][401];
  for (int i = 0; i < 401; ++i) {
    for (int j = 0; j < 401; ++j) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 400; j >= 0; ++j) {
      for (int k = 400; k >= 0; ++k) {
        if (j + a > 400)
          continue;
        if (k + b > 400)
          continue;
        if (dp[j][k] == -1)
          continue;
        if (dp[j + a][k + b] == -1) {
          dp[j + a][k + b] = dp[j][k] + c;
        } else {
          dp[j + a][k + b] = min(dp[j][k] + c, dp[j + a][k + b]);
        }
      }
    }
  }
  int mincost = 100 * 50;
  int x = 1;
  while (x * ma <= 400 && x * mb <= 400) {
    if (dp[x * ma][x * mb] != -1) {
      mincost = min(mincost, dp[x * ma][x * mb]);
    }
    x++;
  }
  if (mincost == 100 * 50)
    cout << -1 << endl;
  else
    cout << mincost << endl;
  return 0;
}

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

using namespace std;

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int dp[401][401];
  for (int i = 0; i < 401; ++i) {
    for (int j = 0; j < 401; ++j) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 400; j >= 0; --j) {
      for (int k = 400; k >= 0; --k) {
        if (j + a > 400)
          continue;
        if (k + b > 400)
          continue;
        if (dp[j][k] == -1)
          continue;
        if (dp[j + a][k + b] == -1) {
          dp[j + a][k + b] = dp[j][k] + c;
        } else {
          dp[j + a][k + b] = min(dp[j][k] + c, dp[j + a][k + b]);
        }
      }
    }
  }
  int mincost = 100 * 50;
  int x = 1;
  while (x * ma <= 400 && x * mb <= 400) {
    if (dp[x * ma][x * mb] != -1) {
      mincost = min(mincost, dp[x * ma][x * mb]);
    }
    x++;
  }
  if (mincost == 100 * 50)
    cout << -1 << endl;
  else
    cout << mincost << endl;
  return 0;
}
Problem p03806 (C++)

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

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int a[n], b[n], c[n];
  int suma = 0, sumb = 0, sumc = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    suma += a[i];
    sumb += b[i];
    sumc += c[i];
  }
  int inf = sumc + 1;

  int dp[n + 1][suma + 1][suma + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= suma; j++) {
      for (int k = 0; k <= sumb; k++) {
        dp[i][j][k] = inf;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= suma; j++) {
      for (int k = 0; k <= sumb; 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 + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }

  int ans = inf;
  for (int j = 1; j <= suma; j++) {
    for (int k = 1; k <= sumb; k++) {
      if (j * mb == k * ma)
        ans = min(ans, dp[n][j][k]);
    }
  }
  cout << (ans != inf ? ans : -1) << endl;
}

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

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int a[n], b[n], c[n];
  int suma = 0, sumb = 0, sumc = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    suma += a[i];
    sumb += b[i];
    sumc += c[i];
  }
  int inf = sumc + 1;

  int dp[n + 1][suma + 1][sumb + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= suma; j++) {
      for (int k = 0; k <= sumb; k++) {
        dp[i][j][k] = inf;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= suma; j++) {
      for (int k = 0; k <= sumb; 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 + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }

  int ans = inf;
  for (int j = 1; j <= suma; j++) {
    for (int k = 1; k <= sumb; k++) {
      if (j * mb == k * ma)
        ans = min(ans, dp[n][j][k]);
    }
  }
  cout << (ans != inf ? ans : -1) << endl;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
ll n, ma, mb, a, b, c, d[40][500][500], res;
const ll INF = 1000000000000000000;
int main(void) {
  // Your code here!
  cin >> n >> ma >> mb;
  for (ll i = 0; i <= 450; i++) {
    for (ll j = 0; j <= 450; j++) {
      for (ll k = 0; k <= n; k++) {
        d[k][i][j] = INF;
      }
    }
  }
  d[0][0][0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a >> b >> c;
    for (ll j = 0; j <= 450; j++) {
      for (ll k = 0; k <= 450; k++) {
        if (d[i - 1][j][k] == INF)
          continue;
        d[i][j][k] = min(d[i][j][k], d[i - 1][j][k]);
        d[i][j + a][k + b] = d[i - 1][j][k] + c;
      }
    }
  }
  res = INF;
  for (ll i = 1; i <= 400 / max(ma, mb); i++) {
    res = min(res, d[n][i * ma][i * mb]);
  }
  cout << (res == INF ? -1 : res) << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
ll n, ma, mb, a, b, c, d[45][500][500], res;
const ll INF = 1000000000000000000;
int main(void) {
  // Your code here!
  cin >> n >> ma >> mb;
  for (ll i = 0; i <= 450; i++) {
    for (ll j = 0; j <= 450; j++) {
      for (ll k = 0; k <= n; k++) {
        d[k][i][j] = INF;
      }
    }
  }
  d[0][0][0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a >> b >> c;
    for (ll j = 0; j <= 450; j++) {
      for (ll k = 0; k <= 450; k++) {
        if (d[i - 1][j][k] == INF)
          continue;
        d[i][j][k] = min(d[i][j][k], d[i - 1][j][k]);
        d[i][j + a][k + b] = d[i - 1][j][k] + c;
      }
    }
  }
  res = INF;
  for (ll i = 1; i <= 400 / max(ma, mb); i++) {
    res = min(res, d[n][i * ma][i * mb]);
  }
  cout << (res == INF ? -1 : res) << endl;
}
Problem p03806 (C++)

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

int n, ma, mb;
int A[41], B[41], C[41];
int dp[41][401][401];

int main() {
  cin >> n >> ma >> mb;
  for (int i = 1; i <= n; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  for (int i = 0; i <= 40; i++) {
    for (int j = 0; j <= 401; j++) {
      for (int k = 0; k <= 401; k++) {
        dp[i][j][k] = 100000000;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int a = 0; a <= 400; a++) {
      for (int b = 0; b <= 400; b++) {
        if (dp[i - 1][a][b] != 100000000) {
          dp[i][a][b] = min(dp[i - 1][a][b], dp[i][a][b]);
          dp[i][a + A[i]][b + B[i]] =
              min(dp[i - 1][a][b] + C[i], dp[i][a + A[i]][b + B[i]]);
        }
      }
    }
  }
  int ans = 100000000;
  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 == 100000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

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

int n, ma, mb;
int A[41], B[41], C[41];
int dp[41][401][401];

int main() {
  cin >> n >> ma >> mb;
  for (int i = 1; i <= n; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  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] = 100000000;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int a = 0; a <= 400; a++) {
      for (int b = 0; b <= 400; b++) {
        if (dp[i - 1][a][b] != 100000000) {
          dp[i][a][b] = min(dp[i - 1][a][b], dp[i][a][b]);
          dp[i][a + A[i]][b + B[i]] =
              min(dp[i - 1][a][b] + C[i], dp[i][a + A[i]][b + B[i]]);
        }
      }
    }
  }
  int ans = 100000000;
  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 == 100000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long infinity = 0x3f3f3f3f;
const int maxn = 2000 + 10;
int n;
int f[maxn][maxn];
int x, y, z;
int a, b;
int c, d;
int main() {
  memset(f, infinity, sizeof(f));
  scanf("%d%d%d", &n, &a, &b);
  f[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    c = 1000 - x, d = 1000 - y;
    for (int j = c; j >= 0; j--) {
      for (int k = d; k >= 0; k--) {
        f[j + x][k + y] = min(f[j][k] + z, f[j + x][k + y]);
      }
    }
  }
  //	cout<<f[3*a][3*b]<<endl;
  int ans = infinity;
  int g = min(1000 / x, 1000 / y);
  for (int i = 1; i <= g; i++) {
    ans = min(ans, f[i * a][i * b]);
  }

  if (ans != infinity)
    printf("%d", ans);
  else
    printf("-1");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long infinity = 0x3f3f3f3f;
const int maxn = 3000 + 10;
int n;
int f[maxn][maxn];
int x, y, z;
int a, b;
int c, d;
int main() {
  memset(f, infinity, sizeof(f));
  scanf("%d%d%d", &n, &a, &b);
  f[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    c = 1000 - x, d = 1000 - y;
    for (int j = c; j >= 0; j--) {
      for (int k = d; k >= 0; k--) {
        f[j + x][k + y] = min(f[j][k] + z, f[j + x][k + y]);
      }
    }
  }
  //	cout<<f[3*a][3*b]<<endl;
  int ans = infinity;
  int g = min(1000 / x, 1000 / y);
  for (int i = 1; i <= g; i++) {
    ans = min(ans, f[i * a][i * b]);
  }

  if (ans != infinity)
    printf("%d", ans);
  else
    printf("-1");

  return 0;
}
Problem p03806 (C++)

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

int N, Ma, Mb;
const int inf = 1000000;

int A[40], B[40], C[40];
int dp[41][400][400];

int solve() {
  int ans = inf;
  for (int i = 0; i < N + 1; i++) {
    for (int a = 0; a <= N * 10; a++) {
      for (int b = 0; b <= N * 10; b++) {
        dp[i][a][b] = inf;
      }
    }
  }
  dp[0][0][0] = 0;

  for (int i = 1; i < N + 1; i++) {
    for (int a = 0; a <= 10 * N; a++) {
      for (int b = 0; b <= 10 * N; b++) {
        if (dp[i - 1][a][b] == inf)
          continue;
        dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b]);
        dp[i][a + A[i - 1]][b + B[i - 1]] =
            min(dp[i][a + A[i - 1]][b + B[i - 1]], dp[i - 1][a][b] + C[i - 1]);
      }
    }
  }

  for (int n = 1; n * Ma <= 10 * N && n * Mb <= 10 * N; n++) {
    ans = min(ans, dp[N][n * Ma][n * Mb]);
  }

  if (ans >= inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

int main() {
  cin >> N >> Ma >> Mb;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  solve();
  return 0;
}

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

int N, Ma, Mb;
const int inf = 1000000;

int A[40], B[40], C[40];
int dp[41][410][410];

int solve() {
  int ans = inf;
  for (int i = 0; i < N + 1; i++) {
    for (int a = 0; a <= N * 10; a++) {
      for (int b = 0; b <= N * 10; b++) {
        dp[i][a][b] = inf;
      }
    }
  }
  dp[0][0][0] = 0;

  for (int i = 1; i < N + 1; i++) {
    for (int a = 0; a <= 10 * N; a++) {
      for (int b = 0; b <= 10 * N; b++) {
        if (dp[i - 1][a][b] == inf)
          continue;
        dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b]);
        dp[i][a + A[i - 1]][b + B[i - 1]] =
            min(dp[i][a + A[i - 1]][b + B[i - 1]], dp[i - 1][a][b] + C[i - 1]);
      }
    }
  }

  for (int n = 1; n * Ma <= 10 * N && n * Mb <= 10 * N; n++) {
    ans = min(ans, dp[N][n * Ma][n * Mb]);
  }

  if (ans >= inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

int main() {
  cin >> N >> Ma >> Mb;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  solve();
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
const int inf = 1e9;
const int mod = 1e9 + 7;

const int nmax = 40, abmax = 10;
int a[nmax], b[nmax], c[nmax];
int dp[nmax][nmax * abmax + 1][nmax * abmax + 1];

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 k = 0; k <= N; k++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        dp[k][ca][cb] = inf;
      }
    }
  }

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

  for (int k = 0; k < N; k++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        if (dp[k][ca][cb] == inf)
          continue;
        dp[k + 1][ca][cb] = min(dp[k + 1][ca][cb], dp[k][ca][cb]);
        dp[k + 1][ca + a[k]][cb + b[k]] =
            min(dp[k + 1][ca + a[k]][cb + b[k]], dp[k][ca][cb] + c[k]);
      }
    }
  }

  int ans = inf;
  for (int ca = 1; ca <= nmax * abmax; ca++) {
    for (int cb = 1; cb <= nmax * abmax; cb++) {
      if (ca * mb == cb * ma)
        ans = min(ans, dp[N][ca][cb]);
    }
  }

  if (ans == inf)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
const int inf = 1e9;
const int mod = 1e9 + 7;

const int nmax = 40, abmax = 10;
int a[nmax], b[nmax], c[nmax];
int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

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 k = 0; k <= N; k++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        dp[k][ca][cb] = inf;
      }
    }
  }

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

  for (int k = 0; k < N; k++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        if (dp[k][ca][cb] == inf)
          continue;
        dp[k + 1][ca][cb] = min(dp[k + 1][ca][cb], dp[k][ca][cb]);
        dp[k + 1][ca + a[k]][cb + b[k]] =
            min(dp[k + 1][ca + a[k]][cb + b[k]], dp[k][ca][cb] + c[k]);
      }
    }
  }

  int ans = inf;
  for (int ca = 1; ca <= nmax * abmax; ca++) {
    for (int cb = 1; cb <= nmax * abmax; cb++) {
      if (ca * mb == cb * ma)
        ans = min(ans, dp[N][ca][cb]);
    }
  }

  if (ans == inf)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p03806 (C++)

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

int main() {
  int N, MA, MB;
  cin >> N >> MA >> MB;
  int inf = 9999999;
  vector<vector<int>> dp(401, vector<int>(401, inf));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 400; j >= a; j++) {
      for (int k = 400; k >= b; k++) {
        dp[j][k] = min(dp[j][k], dp[j - a][k - b] + c);
      }
    }
  }
  int ans = inf;
  for (int i = 1; max(i * MA, i * MB) <= 400; i++) {
    ans = min(ans, dp[i * MA][i * MB]);
  }
  if (ans < inf)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}

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

int main() {
  int N, MA, MB;
  cin >> N >> MA >> MB;
  int inf = 9999999;
  vector<vector<int>> dp(401, vector<int>(401, inf));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 400; j >= a; j--) {
      for (int k = 400; k >= b; k--) {
        dp[j][k] = min(dp[j][k], dp[j - a][k - b] + c);
      }
    }
  }
  int ans = inf;
  for (int i = 1; max(i * MA, i * MB) <= 400; i++) {
    ans = min(ans, dp[i * MA][i * MB]);
  }
  if (ans < inf)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}
Problem p03806 (C++)

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

using namespace std;
int dp[55][55];
int a[55], b[55], c[55];
int main() {
  int n, x, y, xx, yy;
  scanf("%d%d%d", &n, &xx, &yy);
  int total = 0;
  int i, fir = 0, sec = 0, j;
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    fir += a[i];
    sec += b[i];
  }
  // printf("max sum %d %d\n",fir,sec);
  int first, second;
  int ma = 1000000007;
  for (i = 0; i <= fir; i++)
    for (j = 0; j <= sec; j++)
      dp[i][j] = ma;
  dp[0][0] = 0;
  for (i = 1; i <= n; i++)
    for (first = fir; first >= 0; first--)
      for (second = sec; second >= 0; second--) {
        if (dp[first][second] < ma) {
          x = first + a[i];
          y = second + b[i];
          if (x <= fir && y <= sec)
            dp[x][y] = min(dp[x][y], dp[first][second] + c[i]);
        }
      }
  int ans = ma;
  for (i = 1;; i++) {
    x = xx * i;
    y = yy * i;
    if (x > fir)
      break;
    if (y > sec)
      break;
    //  printf("check for %d %d\n",x,y);
    ans = min(ans, dp[x][y]);
  }
  if (ans == ma)
    ans = -1;
  printf("%d\n", ans);
  return 0;
}


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

using namespace std;
int dp[5500][5500];
int a[55], b[55], c[55];
int main() {
  int n, x, y, xx, yy;
  scanf("%d%d%d", &n, &xx, &yy);
  int total = 0;
  int i, fir = 0, sec = 0, j;
  for (i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    fir += a[i];
    sec += b[i];
  }
  // printf("max sum %d %d\n",fir,sec);
  int first, second;
  int ma = 1000000007;
  for (i = 0; i <= fir; i++)
    for (j = 0; j <= sec; j++)
      dp[i][j] = ma;
  dp[0][0] = 0;
  for (i = 1; i <= n; i++)
    for (first = fir; first >= 0; first--)
      for (second = sec; second >= 0; second--) {
        if (dp[first][second] < ma) {
          x = first + a[i];
          y = second + b[i];
          if (x <= fir && y <= sec)
            dp[x][y] = min(dp[x][y], dp[first][second] + c[i]);
        }
      }
  int ans = ma;
  for (i = 1;; i++) {
    x = xx * i;
    y = yy * i;
    if (x > fir)
      break;
    if (y > sec)
      break;
    //  printf("check for %d %d\n",x,y);
    ans = min(ans, dp[x][y]);
  }
  if (ans == ma)
    ans = -1;
  printf("%d\n", ans);
  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>

int main() {
  size_t N;
  int Ma, Mb;
  scanf("%zu %d %d", &N, &Ma, &Mb);

  std::vector<int> a(N), b(N), c(N);
  for (size_t i = 0; i < N; ++i)
    scanf("%d %d %d", &a[i], &b[i], &c[i]);

  size_t Na = N / 2, Nb = N - Na;
  struct Pack {
    int a, b, c;
    Pack(int a, int b, int c) : a(a), b(b), c(c) {}
    bool operator<(const Pack &rhs) const {
      return (a != rhs.a ? a < rhs.a : b != rhs.b ? b < rhs.b : c < rhs.c);
    }
  };

  std::vector<Pack> X, Y;
  for (int i = 0; i < (1 << Na); ++i) {
    int a_ = 0, b_ = 0, c_ = 0;
    for (size_t j = 0; j < Na; ++j)
      if (i >> j & 1) {
        a_ += a[j];
        b_ += b[j];
        c_ += c[j];
      }

    X.emplace_back(a_, b_, c_);
  }

  for (int i = 0; i < (1 << Nb); ++i) {
    int a_ = 0, b_ = 0, c_ = 0;
    for (size_t j = 0; j < Nb; ++j)
      if (i >> j & 1) {
        a_ += a[Na + j];
        b_ += b[Na + j];
        c_ += c[Na + j];
      }

    Y.emplace_back(a_, b_, c_);
  }

  std::sort(Y.begin(), Y.end());

  int inf = 10000, res = inf;
  for (size_t i = 0; i < X.size(); ++i) {
    int ma = Ma, mb = Mb;
    for (int r = 1; r <= 15; ++r, ma += Ma, mb += Mb) {
      if (ma < X[i].a || mb < X[i].b)
        continue;

      auto lb = std::lower_bound(Y.begin(), Y.end(),
                                 Pack(ma - X[i].a, mb - X[i].b, 0));

      if (lb < Y.end() && lb->a == ma - X[i].a && lb->b == mb - X[i].b)
        res = std::min(res, X[i].c + lb->c);
    }
  }

  printf("%d\n", res < inf ? res : -1);
}


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

int main() {
  size_t N;
  int Ma, Mb;
  scanf("%zu %d %d", &N, &Ma, &Mb);

  std::vector<int> a(N), b(N), c(N);
  for (size_t i = 0; i < N; ++i)
    scanf("%d %d %d", &a[i], &b[i], &c[i]);

  size_t Na = N / 2, Nb = N - Na;
  struct Pack {
    int a, b, c;
    Pack(int a, int b, int c) : a(a), b(b), c(c) {}
    bool operator<(const Pack &rhs) const {
      return (a != rhs.a ? a < rhs.a : b != rhs.b ? b < rhs.b : c < rhs.c);
    }
  };

  std::vector<Pack> X, Y;
  for (int i = 0; i < (1 << Na); ++i) {
    int a_ = 0, b_ = 0, c_ = 0;
    for (size_t j = 0; j < Na; ++j)
      if (i >> j & 1) {
        a_ += a[j];
        b_ += b[j];
        c_ += c[j];
      }

    X.emplace_back(a_, b_, c_);
  }

  for (int i = 0; i < (1 << Nb); ++i) {
    int a_ = 0, b_ = 0, c_ = 0;
    for (size_t j = 0; j < Nb; ++j)
      if (i >> j & 1) {
        a_ += a[Na + j];
        b_ += b[Na + j];
        c_ += c[Na + j];
      }

    Y.emplace_back(a_, b_, c_);
  }

  std::sort(Y.begin(), Y.end());

  int inf = 10000, res = inf;
  for (size_t i = 0; i < X.size(); ++i) {
    int ma = Ma, mb = Mb;
    for (int r = 1; r <= 10; ++r, ma += Ma, mb += Mb) {
      if (ma < X[i].a || mb < X[i].b)
        continue;

      auto lb = std::lower_bound(Y.begin(), Y.end(),
                                 Pack(ma - X[i].a, mb - X[i].b, 0));

      if (lb < Y.end() && lb->a == ma - X[i].a && lb->b == mb - X[i].b)
        res = std::min(res, X[i].c + lb->c);
    }
  }

  printf("%d\n", res < inf ? res : -1);
}
Problem p03806 (C++)

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

typedef long long ll;
typedef pair<int, int> P;
const int INF = (int)1e9 + 1;

const int nmax = 40, abmax = 10;

int a[nmax], b[nmax], c[nmax];
int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main(void) {
  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; 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 ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; 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 <= nmax * abmax; ca++) {
    for (int cb = 1; cb <= nmax * abmax; cb++) {
      if (ca * mb == cb * ma)
        ans = min(ans, dp[n][ca][cb]);
    }
  }

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

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

typedef long long ll;
typedef pair<int, int> P;
const int INF = (int)1e9 + 1;

const int nmax = 40, abmax = 10;

int a[nmax], b[nmax], c[nmax];
int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main(void) {
  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; 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 ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; 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 <= nmax * abmax; ca++) {
    for (int cb = 1; cb <= nmax * abmax; cb++) {
      if (ca * mb == cb * ma)
        ans = min(ans, dp[n][ca][cb]);
    }
  }

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

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

constexpr int N = 40 + 1;
constexpr int M = (N - 1) * 10 + 1;
constexpr int INF = 1000000;
int table[N][M][M];

int main() {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k < M; ++k) {
        table[i][j][k] = INF;
      }
    }
  }

  int n, ma, mb;
  std::cin >> n >> ma >> mb;
  std::vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; ++i) {
    int x, y, z;
    std::cin >> x >> y >> z;
    a[i] = x;
    b[i] = y;
    c[i] = z;
  }

  table[0][0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k < M; ++k) {
        table[i + 1][j][k] = std::min(table[i + 1][j][k], table[i][j][k]);
        if (j + a[i] < M && k + b[i] < M) {
          table[i + i][j + a[i]][k + b[i]] =
              std::min(table[i + i][j + a[i]][k + b[i]], table[i][j][k] + c[i]);
        }
      }
    }
  }

  int cost = INF;
  for (int j = 1; j < M; ++j) {
    for (int k = 1; k < M; ++k) {
      if (j * mb == k * ma) {
        cost = std::min(cost, table[n][j][k]);
      }
    }
  }
  if (cost >= INF) {
    cost = -1;
  }

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


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

constexpr int N = 40 + 1;
constexpr int M = (N - 1) * 10 + 1;
constexpr int INF = 1000000;
int table[N][M][M];

int main() {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k < M; ++k) {
        table[i][j][k] = INF;
      }
    }
  }

  int n, ma, mb;
  std::cin >> n >> ma >> mb;
  std::vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; ++i) {
    int x, y, z;
    std::cin >> x >> y >> z;
    a[i] = x;
    b[i] = y;
    c[i] = z;
  }

  table[0][0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k < M; ++k) {
        table[i + 1][j][k] = std::min(table[i + 1][j][k], table[i][j][k]);
        if (j + a[i] < M && k + b[i] < M) {
          table[i + 1][j + a[i]][k + b[i]] =
              std::min(table[i + 1][j + a[i]][k + b[i]], table[i][j][k] + c[i]);
        }
      }
    }
  }

  int cost = INF;
  for (int j = 1; j < M; ++j) {
    for (int k = 1; k < M; ++k) {
      if (j * mb == k * ma) {
        cost = std::min(cost, table[n][j][k]);
      }
    }
  }
  if (cost >= INF) {
    cost = -1;
  }

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

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int dp[41][401][401];
int inf = 1e5;

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int a[n], b[n], c[n];

  rep(i, n) cin >> a[i] >> b[i] >> c[i];

  rep(i, 41) rep(s, 401) rep(t, 401) dp[i][s][t] = inf;

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

  rep(i, n) {
    rep(s, 401) {
      rep(t, 401) {
        dp[i + 1][s][t] = min(dp[i + 1][s][t], dp[i][s][t]);
        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]);
      }
    }
  }

  int ans = inf;

  for (int s = 1; s <= 400; s++) {
    for (int t = 1; t <= 400; t++) {
      if (ma * t == mb * s)
        ans = min(ans, dp[n][s][t]);
    }
  }

  cout << (ans == inf ? -1 : ans) << endl;

  return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int dp[41][401][401];
int inf = 1e5;

int main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int a[n], b[n], c[n];

  rep(i, n) cin >> a[i] >> b[i] >> c[i];

  rep(i, 41) rep(s, 401) rep(t, 401) dp[i][s][t] = inf;

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

  rep(i, n) {
    rep(s, 401) {
      rep(t, 401) {
        dp[i + 1][s][t] = min(dp[i + 1][s][t], dp[i][s][t]);
        if (s + a[i] > 400 || t + b[i] > 400)
          continue;
        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]);
      }
    }
  }

  int ans = inf;

  for (int s = 1; s <= 400; s++) {
    for (int t = 1; t <= 400; t++) {
      if (ma * t == mb * s)
        ans = min(ans, dp[n][s][t]);
    }
  }

  cout << (ans == inf ? -1 : ans) << endl;

  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, ma, mb, dp[41][400][400], INF = 1e4;
int main(void) {
  cin >> n >> ma >> mb;
  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] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 0; j <= 400; j++) {
      for (int k = 0; k <= 400; k++) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j >= a && k >= b)
          dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - a][k - b] + c);
      }
    }
  }
  int m = INF;
  for (int i = 1; i <= 400; i++) {
    int a = ma * i, b = mb * i;
    if (a <= 400 && b <= 400)
      m = min(m, dp[n][a][b]);
  }
  if (m >= INF) {
    cout << -1 << endl;
  } else {
    cout << m << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int n, ma, mb, dp[41][401][401], INF = 1e4;
int main(void) {
  cin >> n >> ma >> mb;
  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] = INF;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    for (int j = 0; j <= 400; j++) {
      for (int k = 0; k <= 400; k++) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j >= a && k >= b)
          dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - a][k - b] + c);
      }
    }
  }
  int m = INF;
  for (int i = 1; i <= 400; i++) {
    int a = ma * i, b = mb * i;
    if (a <= 400 && b <= 400)
      m = min(m, dp[n][a][b]);
  }
  if (m >= INF) {
    cout << -1 << endl;
  } else {
    cout << m << endl;
  }
}
Problem p03806 (C++)

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

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)

typedef long long ll;
#define INF 1e9

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

  vector<vector<vector<int>>> dp(
      N + 1, vector<vector<int>>(410, vector<int>(410, 1e9)));
  dp[0][0][0] = 0;
  REP(i, N) {
    FOR(j, 0, 401) {
      FOR(k, 0, 401) {
        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 = 1e9;
  FOR(i, 1, 401) {
    FOR(j, 1, 401) {
      if (i % Ma == 0 && j % Mb == 0 && i / Ma == j / Mb) {
        ans = min(dp[N][i][j], ans);
      }
    }
  }

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

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)

typedef long long ll;
#define INF 1e9

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

  vector<vector<vector<int>>> dp(
      N + 1, vector<vector<int>>(411, vector<int>(411, 1e9)));
  dp[0][0][0] = 0;
  REP(i, N) {
    FOR(j, 0, 401) {
      FOR(k, 0, 401) {
        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 = 1e9;
  FOR(i, 1, 401) {
    FOR(j, 1, 401) {
      if (i % Ma == 0 && j % Mb == 0 && i / Ma == j / Mb) {
        ans = min(dp[N][i][j], ans);
      }
    }
  }

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

  return 0;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

const int nmax = 40;
const int abmax = 10;
const int INF = 1 << 29;

int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  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];

  for (int i = 0; i < nmax + 1; i++)
    for (int j = 0; j < nmax * abmax + 1; j++)
      for (int k = 0; k < nmax * abmax + 1; k++)
        dp[i][j][k] = INF;
  dp[0][0][0] = 0;

  for (int i = 0; i <= N - 1; i++) {
    for (int j = 0; j <= nmax * abmax; j++) {
      for (int k = 0; k <= nmax * abmax; k++) {
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        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]);
      }
    }
  }

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

  cout << ((ans == INF) ? -1 : ans) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

const int nmax = 40;
const int abmax = 10;
const int INF = 1 << 29;

int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  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];

  for (int i = 0; i < nmax + 1; i++)
    for (int j = 0; j < nmax * abmax + 1; j++)
      for (int k = 0; k < nmax * abmax + 1; k++)
        dp[i][j][k] = INF;
  dp[0][0][0] = 0;

  for (int i = 0; i <= N - 1; 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 + 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]);
      }
    }
  }

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

  cout << ((ans == INF) ? -1 : ans) << endl;
}
Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N_MAX 40
#define w_MAX 10
#define INF 100000

int dp[N_MAX + 1][w_MAX * (N_MAX + 10) + 1][w_MAX * (N_MAX + 10) + 1];
int a[N_MAX], b[N_MAX], c[N_MAX];

void solve(int N) {
  // 初期化
  for (int i = 0; i < N_MAX + 1; i++) {
    for (int j = 0; j < w_MAX * (N_MAX + 10) + 1; j++) {
      for (int k = 0; k < w_MAX * (N_MAX + 10) + 1; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  dp[0][0][0] = 0;

  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < w_MAX * N_MAX + 1; j++) {
      for (int k = 0; k < w_MAX * N_MAX + 1; 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 + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
}

int main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;

  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i] >> c[i];
  solve(N);
  int ans = INF;
  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < w_MAX * N_MAX + 1; j++) {
      for (int k = 1; k < w_MAX * N_MAX + 1; k++) {
        if (j * Mb == k * Ma)
          ans = min(ans, dp[i][j][k]);
      }
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N_MAX 40
#define w_MAX 10
#define INF 100000

int dp[N_MAX + 1][w_MAX * (N_MAX + 10) + 1][w_MAX * (N_MAX + 10) + 1];
int a[N_MAX], b[N_MAX], c[N_MAX];

void solve(int N) {
  // 初期化
  for (int i = 0; i < N_MAX + 1; i++) {
    for (int j = 0; j < w_MAX * (N_MAX + 10) + 1; j++) {
      for (int k = 0; k < w_MAX * (N_MAX + 10) + 1; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  dp[0][0][0] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < w_MAX * N_MAX + 1; j++) {
      for (int k = 0; k < w_MAX * N_MAX + 1; 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 + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
      }
    }
  }
}

int main() {
  int N, Ma, Mb;
  cin >> N >> Ma >> Mb;

  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i] >> c[i];
  solve(N);
  int ans = INF;
  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < w_MAX * N_MAX + 1; j++) {
      for (int k = 1; k < w_MAX * N_MAX + 1; k++) {
        if (j * Mb == k * Ma)
          ans = min(ans, dp[i][j][k]);
      }
    }
  }

  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, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int a[n], b[n], c[n];
  const int lim = 1000000;
  vector<vector<vector<int>>> dp(
      41, vector<vector<int>>(401, vector<int>(401, lim)));
  rep(i, 41) dp[i][0][0] = 0;
  rep(i, n) cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++) {
    rep(j, 401) {
      rep(k, 401) { dp[i][j][k] = dp[i - 1][j][k]; }
    }
    for (int j = 0; j + a[i] <= 400; j++) {
      for (int k = 0; k + b[i] <= 400; k++) {
        dp[i][j + a[i - 1]][k + b[i - 1]] =
            min(dp[i][j + a[i - 1]][k + b[i - 1]], dp[i - 1][j][k] + c[i - 1]);
      }
    }
  }
  int res = lim;
  for (int i = 1; ma * i <= 400 && mb * i <= 400; i++) {
    res = min(res, dp[n][ma * i][mb * i]);
  }

  cout << (res == lim ? -1 : res) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int a[n], b[n], c[n];
  const int lim = 1000000;
  vector<vector<vector<int>>> dp(
      41, vector<vector<int>>(401, vector<int>(401, lim)));
  rep(i, 41) dp[i][0][0] = 0;
  rep(i, n) cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++) {
    rep(j, 401) {
      rep(k, 401) { dp[i][j][k] = dp[i - 1][j][k]; }
    }
    for (int j = 0; j + a[i - 1] <= 400; j++) {
      for (int k = 0; k + b[i - 1] <= 400; k++) {
        dp[i][j + a[i - 1]][k + b[i - 1]] =
            min(dp[i][j + a[i - 1]][k + b[i - 1]], dp[i - 1][j][k] + c[i - 1]);
      }
    }
  }
  int res = lim;
  for (int i = 1; ma * i <= 400 && mb * i <= 400; i++) {
    res = min(res, dp[n][ma * i][mb * i]);
  }

  cout << (res == lim ? -1 : res) << endl;
  return 0;
}
Problem p03806 (C++)

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

using namespace std;
typedef long long ll;
typedef long int li;
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 int INF = 1000000;
const double PI = 3.14159265359;

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 dp[410][410];
  rep(i, 0, 410) rep(j, 0, 410) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, 0, n) {
    repr(j, 400, a[i]) {
      repr(k, 400, b[i]) { chmin(dp[j][k], dp[j - a[i]][k - b[i]] + c[i]); }
    }
  }
  ll ans = INF;
  rep(i, 1, 401) chmin(ans, dp[ma * i][mb * i]);

  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 int li;
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 int INF = 1000000;
const double PI = 3.14159265359;

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 dp[410][410];
  rep(i, 0, 410) rep(j, 0, 410) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, 0, n) {
    repr(j, 400, a[i]) {
      repr(k, 400, b[i]) { chmin(dp[j][k], dp[j - a[i]][k - b[i]] + c[i]); }
    }
  }
  ll ans = INF;
  rep(i, 1, n + 1) chmin(ans, dp[ma * i][mb * i]);

  if (ans == INF)
    out(-1);
  else
    out(ans);

  return 0;
}