Problem p03806 (C++)

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x)                                                             \
  sort(x.begin(), x.end());                                                    \
  x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y)                                                             \
  cerr << "(" << #x << ", " << #y << ") = "                                    \
       << "(" << x << ", " << y << ")" << endl;
#define dbg3(x, y, z)                                                          \
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define dbgB(value, size) cerr << #value << ": " << bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> void printA(vector<T> &printArray, char between = ' ') {
  int paSize = printArray.size();
  for (int i = 0; i < paSize; i++) {
    cerr << printArray[i] << between;
  }
  if (between != '\n') {
    cerr << endl;
  }
}

// ------------------------------------------------------------------------------------------

const int nmax = 40, abmax = 10, inf = 1e6;

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

void solve() {
  for (int i = 0; i < n; i++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        chmin(dp[i + 1][ca][cb], dp[i][ca][cb]);
        chmin(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) {
        chmin(ans, dp[n][ca][cb]);
      }
    }
  }

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

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

  for (int i = 0; i <= n; i++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        dp[i][ca][cb] = inf;
      }
    }
  }
  dp[0][0][0] = 0;

  solve();

  return 0;
}

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x)                                                             \
  sort(x.begin(), x.end());                                                    \
  x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y)                                                             \
  cerr << "(" << #x << ", " << #y << ") = "                                    \
       << "(" << x << ", " << y << ")" << endl;
#define dbg3(x, y, z)                                                          \
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define dbgB(value, size) cerr << #value << ": " << bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> void printA(vector<T> &printArray, char between = ' ') {
  int paSize = printArray.size();
  for (int i = 0; i < paSize; i++) {
    cerr << printArray[i] << between;
  }
  if (between != '\n') {
    cerr << endl;
  }
}

// ------------------------------------------------------------------------------------------

const int nmax = 40, abmax = 10, inf = 1e6;

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

void solve() {
  for (int i = 0; i < n; i++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        chmin(dp[i + 1][ca][cb], dp[i][ca][cb]);
        if (ca + a[i] <= nmax * abmax && cb + b[i] <= nmax * abmax) {
          chmin(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) {
        chmin(ans, dp[n][ca][cb]);
      }
    }
  }

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

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

  for (int i = 0; i <= n; i++) {
    for (int ca = 0; ca <= nmax * abmax; ca++) {
      for (int cb = 0; cb <= nmax * abmax; cb++) {
        dp[i][ca][cb] = inf;
      }
    }
  }
  dp[0][0][0] = 0;

  solve();

  return 0;
}