Problem p03806 (C++)

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

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define step(i, f, n, a) for (int(i) = (f); (i) < (n); (i) += a)
#define rstep(i, f, n, a) for (int(i) = (f); (i) >= (n); (i) -= a)
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define gets(x)                                                                \
  x;                                                                           \
  cin >> x;
#define puts(x)                                                                \
  x;                                                                           \
  cout << x << endl;
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](const a, const b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }

using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int nmax = 40, abmax = 10, inf = 1000000;
int a[nmax], b[nmax], c[nmax];                        // それぞれの薬品
int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1]; // dp

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, nmax * abmax + 1) {
      rep(k, nmax * abmax + 1) {
        dp[i][j][k] = inf; // 大きな数で初期化
      }
    }
  }
  dp[0][0][0] = 0;

  rep(i, n + 1) {
    rep(j, nmax * abmax + 1) {
      rep(k, nmax * abmax + 1) {
        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 result = inf;
  FOR(i, 1, nmax * abmax + 1) {
    FOR(j, 1, nmax * abmax + 1) {
      if (i * mb == j * ma) { // 比率が正しいなら
        result = min(result, dp[n][i][j]);
      }
    }
  }
  if (result == inf) {
    result = -1;
  }
  puts(result)
}


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

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define step(i, f, n, a) for (int(i) = (f); (i) < (n); (i) += a)
#define rstep(i, f, n, a) for (int(i) = (f); (i) >= (n); (i) -= a)
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define gets(x)                                                                \
  x;                                                                           \
  cin >> x;
#define puts(x)                                                                \
  x;                                                                           \
  cout << x << endl;
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](const a, const b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }

using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int nmax = 40, abmax = 10, inf = 1000000;
int a[nmax], b[nmax], c[nmax];                        // それぞれの薬品
int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1]; // dp

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, nmax * abmax + 1) {
      rep(k, nmax * abmax + 1) {
        dp[i][j][k] = inf; // 大きな数で初期化
      }
    }
  }
  dp[0][0][0] = 0;

  rep(i, n) {
    rep(j, nmax * abmax + 1) {
      rep(k, nmax * abmax + 1) {
        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 result = inf;
  FOR(i, 1, nmax * abmax + 1) {
    FOR(j, 1, nmax * abmax + 1) {
      if (i * mb == j * ma) { // 比率が正しいなら
        result = min(result, dp[n][i][j]);
      }
    }
  }
  if (result == inf) {
    result = -1;
  }
  puts(result)
}