Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int(i) = (int)(a); (i) < (int)(n); ++(i))
#define repe(i, a, n) for (int(i) = (int)(a); (i) <= (int)(n); ++(i))
#define repif(i, a, x) for (int(i) = (int)(a); (x); ++(i))
#define repr(i, a, n) for (int(i) = ((int)(a)-1); (i) >= (int)(n); --(i))
#define reper(i, a, n) for (int(i) = (int)(a); (i) >= (int)(n); --(i))
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define RNG(x, a, n) &((x)[a]), &((x)[n])
#define ADD(a, b) ((((a + MOD) % MOD) + ((b + MOD) % MOD)) % MOD)
#define MUL(a, b) ((((a) % MOD) * ((b) % MOD)) % MOD)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define fcout cout << fixed << setprecision(10)
#define pb push_back
#define pf push_front
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define EPS (1e-20)
#define INF INT_MAX
#define INFLL LLONG_MAX
#define MOD 1000000007
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using MII = std::map<int, int>;
using TIII = std::tuple<int, int, int>;
using MCI = std::map<char, int>;
using VPII = std::vector<PII>;
using namespace std;

int N, Ma, Mb;
int a[50], b[50], c[50];
int dp[410][410];

void makeDP() {
  rep(i, 0, 410) fill(RNG(dp[i], 0, 410), INF);
  rep(i, 0, N) {
    reper(j, 410, 1) reper(k, 410, 1) if (dp[j][k] != INF)
        chmin(dp[j + a[i]][k + b[i]], dp[j][k] + c[i]);
    chmin(dp[a[i]][b[i]], c[i]);
  }
}

int solve() {
  int ans = INF;
  repe(i, 1, 40) if (i * Ma <= 400 && i * Mb <= 400)
      chmin(ans, dp[i * Ma][i * Mb]);
  if (ans == INF)
    return -1;
  return ans;
}

signed main() {
  cin >> N >> Ma >> Mb;
  rep(i, 0, N) cin >> a[i] >> b[i] >> c[i];

  makeDP();

  cout << solve() << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int(i) = (int)(a); (i) < (int)(n); ++(i))
#define repe(i, a, n) for (int(i) = (int)(a); (i) <= (int)(n); ++(i))
#define repif(i, a, x) for (int(i) = (int)(a); (x); ++(i))
#define repr(i, a, n) for (int(i) = ((int)(a)-1); (i) >= (int)(n); --(i))
#define reper(i, a, n) for (int(i) = (int)(a); (i) >= (int)(n); --(i))
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define RNG(x, a, n) &((x)[a]), &((x)[n])
#define ADD(a, b) ((((a + MOD) % MOD) + ((b + MOD) % MOD)) % MOD)
#define MUL(a, b) ((((a) % MOD) * ((b) % MOD)) % MOD)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define fcout cout << fixed << setprecision(10)
#define pb push_back
#define pf push_front
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define EPS (1e-20)
#define INF INT_MAX
#define INFLL LLONG_MAX
#define MOD 1000000007
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using MII = std::map<int, int>;
using TIII = std::tuple<int, int, int>;
using MCI = std::map<char, int>;
using VPII = std::vector<PII>;
using namespace std;

int N, Ma, Mb;
int a[50], b[50], c[50];
int dp[410][410];

void makeDP() {
  rep(i, 0, 410) fill(RNG(dp[i], 0, 410), INF);
  rep(i, 0, N) {
    repr(j, 410, 1) repr(k, 410, 1) if (dp[j][k] != INF)
        chmin(dp[j + a[i]][k + b[i]], dp[j][k] + c[i]);
    chmin(dp[a[i]][b[i]], c[i]);
  }
}

int solve() {
  int ans = INF;
  repe(i, 1, 40) if (i * Ma <= 400 && i * Mb <= 400)
      chmin(ans, dp[i * Ma][i * Mb]);
  if (ans == INF)
    return -1;
  return ans;
}

signed main() {
  cin >> N >> Ma >> Mb;
  rep(i, 0, N) cin >> a[i] >> b[i] >> c[i];

  makeDP();

  cout << solve() << endl;

  return 0;
}