Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define vvsort(v) sort(v.begin(), v.end(), greater<int>());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
typedef priority_queue<int> pq;
typedef priority_queue<int, vector<int>, greater<int>> rpq;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//----------------------------------------------------------------------------------------------------------------------------------//
int n, Ma, Mb;
int a[50], b[50], c[50];
int dp[50][500][500];
int main() {
  cin >> n >> Ma >> Mb;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        dp[i][j][k] = inf;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        dp[i + 1][j + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
      }
    }
  }
  int mindp = inf;
  for (int i = 0; i < 500; i++) {
    for (int j = 0; j < 500; j++) {
      int g = __gcd(i, j);
      if (i / g == Ma && j / g == Mb) {
        mindp = min(mindp, dp[n][i][j]);
      }
    }
  }
  if (mindp == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << mindp << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define vvsort(v) sort(v.begin(), v.end(), greater<int>());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
typedef priority_queue<int> pq;
typedef priority_queue<int, vector<int>, greater<int>> rpq;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//----------------------------------------------------------------------------------------------------------------------------------//
int n, Ma, Mb;
int a[50], b[50], c[50];
int dp[50][500][500];
int main() {
  cin >> n >> Ma >> Mb;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        dp[i][j][k] = inf;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 500; k++) {
        dp[i + 1][j + a[i]][k + b[i]] =
            min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i]);
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
      }
    }
  }
  int mindp = inf;
  for (int i = 1; i < 500; i++) {
    for (int j = 1; j < 500; j++) {
      int g = __gcd(i, j);
      if (i / g == Ma && j / g == Mb) {
        mindp = min(mindp, dp[n][i][j]);
      }
    }
  }
  if (mindp == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << mindp << endl;
}