Problem p03806 (C++)

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

using namespace std;
// template for creating 2d vector
template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

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

const int inf = 100000000;

typedef long long ll;

const int max_n = 40;
const int max_a = max_n * 10;
const int max_b = max_n * 10;

int dp[max_n][max_a][max_b];

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

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

  // dp[i][j][k] := i番目までの薬品を使い、薬品A が j グラム、薬品B が k
  // グラムとなるような物質を作ることにかかる最小のお金
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= max_a; j++) {
      for (int k = 0; k <= max_b; k++) {
        dp[i][j][k] = inf;
      }
    }
  }

  // for(int i = 0; i <= N; i++) {
  //   dp[i][0][0] = 0;
  // }
  dp[0][0][0] = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= max_a; j++) {
      for (int k = 0; k <= max_b; k++) {
        if ((a[i - 1] <= j) && (b[i - 1] <= k)) {
          dp[i][j][k] = min(dp[i - 1][j][k],
                            dp[i - 1][j - a[i - 1]][k - b[i - 1]] + c[i - 1]);
        } else {
          dp[i][j][k] = dp[i - 1][j][k];
        }
      }
    }
  }

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

  cout << (ans == inf ? -1 : ans) << "\n";

  return 0;
}


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

using namespace std;
// template for creating 2d vector
template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

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

const int inf = 100000000;

typedef long long ll;

const int max_n = 40;
const int max_a = max_n * 10;
const int max_b = max_n * 10;

int dp[max_n + 1][max_a + 1][max_b + 1];

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

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

  // dp[i][j][k] := i番目までの薬品を使い、薬品A が j グラム、薬品B が k
  // グラムとなるような物質を作ることにかかる最小のお金
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= max_a; j++) {
      for (int k = 0; k <= max_b; k++) {
        dp[i][j][k] = inf;
      }
    }
  }

  // for(int i = 0; i <= N; i++) {
  //   dp[i][0][0] = 0;
  // }
  dp[0][0][0] = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= max_a; j++) {
      for (int k = 0; k <= max_b; k++) {
        if ((a[i - 1] <= j) && (b[i - 1] <= k)) {
          dp[i][j][k] = min(dp[i - 1][j][k],
                            dp[i - 1][j - a[i - 1]][k - b[i - 1]] + c[i - 1]);
        } else {
          dp[i][j][k] = dp[i - 1][j][k];
        }
      }
    }
  }

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

  cout << (ans == inf ? -1 : ans) << "\n";

  return 0;
}