Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// head

vi a;
vi b;
vi c;
ll dp[41][401][401];
ll solve(int n, int ai, int bi) {
  if (dp[n][ai][bi] > 0)
    return dp[n][ai][bi];
  if (n == 0) {
    if (ai == 0 && bi == 0) {
      return dp[n][ai][bi] = 0;
    } else if (ai - a[0] == 0 && bi - b[0] == 0) {
      return dp[n][ai][bi] = c[0];
    } else {
      return dp[n][ai][bi] = INT_MAX;
    }
  } else {
    if (ai - a[n] >= 0 && bi - b[n] >= 0) {
      return dp[n][ai][bi] = min(solve(n - 1, ai - a[n], bi - b[n]) + c[n],
                                 solve(n - 1, ai, bi));
    } else {
      return dp[n][ai][bi] = solve(n - 1, ai, bi);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, ma, mb;
  cin >> n >> ma >> mb;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ll ans = INT_MAX;
  for (int i = 1; i < 100; i++) {
    int sa = ma * i;
    int sb = mb * i;
    ans = min(ans, solve(n - 1, sa, sb));
  }
  cout << (ans != INT_MAX ? ans : -1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// head

vi a;
vi b;
vi c;
ll dp[41][401][401];
ll solve(int n, int ai, int bi) {
  if (dp[n][ai][bi] > 0)
    return dp[n][ai][bi];
  if (n == 0) {
    if (ai == 0 && bi == 0) {
      return dp[n][ai][bi] = 0;
    } else if (ai - a[0] == 0 && bi - b[0] == 0) {
      return dp[n][ai][bi] = c[0];
    } else {
      return dp[n][ai][bi] = INT_MAX;
    }
  } else {
    if (ai - a[n] >= 0 && bi - b[n] >= 0) {
      return dp[n][ai][bi] = min(solve(n - 1, ai - a[n], bi - b[n]) + c[n],
                                 solve(n - 1, ai, bi));
    } else {
      return dp[n][ai][bi] = solve(n - 1, ai, bi);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, ma, mb;
  cin >> n >> ma >> mb;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ll ans = INT_MAX;
  for (int i = 1; i < 100; i++) {
    int sa = ma * i;
    int sb = mb * i;
    if (sa > 400 || sb > 400)
      break;
    ans = min(ans, solve(n - 1, sa, sb));
  }
  cout << (ans != INT_MAX ? ans : -1) << endl;
  return 0;
}