Problem p03806 (C++)

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

#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++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

#define INF 100000000
#define MOD 1000000007
#define writeln(n) cout << n << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
// vector<tuple<long long, long long, long long>>vec;

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

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

const int inf = 100000000;

llong pownew(llong m, llong n) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 0) {
    return pow(m * m, n / 2);
  }
  return pow(m, n - 1) * m;
}

bool isprime(int p) {
  if (p == 1)
    return false;
  for (int i = 2; i < p; i++) {
    if (p % i == 0)
      return false;
  }
  return true;
}
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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int ma, mb;
  cin >> 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;
  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 <bits/stdc++.h>

#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++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

#define INF 100000000
#define MOD 1000000007
#define writeln(n) cout << n << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
// vector<tuple<long long, long long, long long>>vec;

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

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

const int inf = 100000000;

llong pownew(llong m, llong n) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 0) {
    return pow(m * m, n / 2);
  }
  return pow(m, n - 1) * m;
}

bool isprime(int p) {
  if (p == 1)
    return false;
  for (int i = 2; i < p; i++) {
    if (p % i == 0)
      return false;
  }
  return true;
}
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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int ma, mb;
  cin >> 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;
  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;
}