Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int i = 0; i < (n); i++)                                              \
      cerr << v[i] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

#define INF 10000000

int dp[2][401][401];

int main() {
  REP(k, 2) REP(i, 401) REP(j, 401) dp[k][i][j] = INF;
  dp[0][0][0] = dp[1][0][0] = 0;

  int n, ma, mb;
  cin >> n >> ma >> mb;

  REP(v, n) {
    int a, b, c;
    cin >> a >> b >> c;
    REP(i, 401 - a) REP(j, 401 - b) {
      if (dp[v % 2][i][j] != INF) {
        dp[(v + 1) % 2][i][j] = min(dp[v % 2][i][j], dp[(v + 1)][i][j]);
        dp[(v + 1) % 2][i + a][j + b] =
            min(dp[(v + 1) % 2][i + a][j + b],
                min(dp[v % 2][i + a][j + b], dp[v % 2][i][j] + c));
      }
    }
  }
  int ans = INF;

  rep(i, 1, 401) rep(j, 1, 401) {
    if (ma * j == mb * i)
      ans = min(ans, min(dp[0][i][j], dp[1][i][j]));
  }

  if (ans != INF) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int i = 0; i < (n); i++)                                              \
      cerr << v[i] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

#define INF 10000000

int dp[2][401][401];

int main() {
  REP(k, 2) REP(i, 401) REP(j, 401) dp[k][i][j] = INF;
  dp[0][0][0] = dp[1][0][0] = 0;

  int n, ma, mb;
  cin >> n >> ma >> mb;

  REP(v, n) {
    int a, b, c;
    cin >> a >> b >> c;
    REP(i, 401 - a) REP(j, 401 - b) {
      if (dp[v % 2][i][j] != INF) {
        dp[(v + 1) % 2][i][j] = min(dp[v % 2][i][j], dp[(v + 1) % 2][i][j]);
        dp[(v + 1) % 2][i + a][j + b] =
            min(dp[(v + 1) % 2][i + a][j + b],
                min(dp[v % 2][i + a][j + b], dp[v % 2][i][j] + c));
      }
    }
  }
  int ans = INF;

  rep(i, 1, 401) rep(j, 1, 401) {
    if (ma * j == mb * i)
      ans = min(ans, min(dp[0][i][j], dp[1][i][j]));
  }

  if (ans != INF) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}