Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define RREP(i, n) for (int(i) = (int)(n)-1; i >= 0; i--)
#define FILL(Itr, n) fill((Itr).begin(), (Itr).end(), n)
#define REMOVE(Itr, n)                                                         \
  (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end())
#define UNIQUE(Itr)                                                            \
  sort((Itr).begin(), (Itr).end());                                            \
  (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end())
#define LBOUND(Itr, val) lower_bound((Itr).begin(), (Itr).end(), (val))
#define UBOUND(Itr, val) upper_bound((Itr).begin(), (Itr).end(), (val))
typedef long long ll;
#define MOD 1000000007

ll dp[5000][5000];

int main() {

  ll N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<ll> A(N), B(N), C(N);
  REP(i, N) cin >> A[i] >> B[i] >> C[i];

  if (Ma > Mb) {
    bool flag = false;
    REP(i, N) {
      if (A[i] > B[i])
        flag = true;
    }
    if (!flag) {
      cout << -1 << endl;
      return 0;
    }
  } else {
    bool flag = false;
    REP(i, N) {
      if (A[i] < B[i])
        flag = true;
    }
    if (!flag) {
      cout << -1 << endl;
      return 0;
    }
  }

  REP(i, 5000) REP(j, 5000) dp[i][j] = 1e18;
  dp[0][0] = 0;

  REP(kai, N) {
    for (ll i = 5000; i >= A[kai]; i--) {
      for (ll j = 5000; j >= B[kai]; j--) {
        dp[i][j] = min(dp[i - A[kai]][j - B[kai]] + C[kai], dp[i][j]);
      }
    }
  }

  /*REP(i,5){
      REP(j,5)cout<<dp[i][j]<<" ";
      cout<<endl;
  }*/

  ll ans = 1e18;
  for (int i = 1; i * Ma < 5000 && i * Mb < 5000; i++) {
    ans = min(ans, dp[Ma * i][Mb * i]);
  }

  if (ans == 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define RREP(i, n) for (int(i) = (int)(n)-1; i >= 0; i--)
#define FILL(Itr, n) fill((Itr).begin(), (Itr).end(), n)
#define REMOVE(Itr, n)                                                         \
  (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end())
#define UNIQUE(Itr)                                                            \
  sort((Itr).begin(), (Itr).end());                                            \
  (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end())
#define LBOUND(Itr, val) lower_bound((Itr).begin(), (Itr).end(), (val))
#define UBOUND(Itr, val) upper_bound((Itr).begin(), (Itr).end(), (val))
typedef long long ll;
#define MOD 1000000007

ll dp[5010][5010];

int main() {

  ll N, Ma, Mb;
  cin >> N >> Ma >> Mb;
  vector<ll> A(N), B(N), C(N);
  REP(i, N) cin >> A[i] >> B[i] >> C[i];

  if (Ma > Mb) {
    bool flag = false;
    REP(i, N) {
      if (A[i] > B[i])
        flag = true;
    }
    if (!flag) {
      cout << -1 << endl;
      return 0;
    }
  } else {
    bool flag = false;
    REP(i, N) {
      if (A[i] < B[i])
        flag = true;
    }
    if (!flag) {
      cout << -1 << endl;
      return 0;
    }
  }

  REP(i, 5000) REP(j, 5000) dp[i][j] = 1e18;
  dp[0][0] = 0;

  REP(kai, N) {
    for (ll i = 5000; i >= A[kai]; i--) {
      for (ll j = 5000; j >= B[kai]; j--) {
        dp[i][j] = min(dp[i - A[kai]][j - B[kai]] + C[kai], dp[i][j]);
      }
    }
  }

  /*REP(i,5){
      REP(j,5)cout<<dp[i][j]<<" ";
      cout<<endl;
  }*/

  ll ans = 1e18;
  for (int i = 1; i * Ma < 5000 && i * Mb < 5000; i++) {
    ans = min(ans, dp[Ma * i][Mb * i]);
  }

  if (ans == 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}