Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#define ll long long
#define fi first
#define se second
using namespace std;

int n, a, b;
vector<pair<int, pair<int, int>>> v;
const int inf = 999999999;
int dp[41][500][500];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++) {
    int e, f, g;
    cin >> e >> f >> g;
    v.push_back(make_pair(e, make_pair(f, g)));
  }
  for (int i = 0; i < 41; 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][k] = min(dp[i][j][k], dp[i + 1][j][k]);
        dp[i + 1][j + v[i].fi][k + v[i].se.fi] = min(
            dp[i + 1][j + v[i].fi][k + v[i].se.fi], dp[i][j][k] + v[i].se.se);
      }
    }
  }
  int ans = inf;
  for (int i = 1; i < 500; i++)
    for (int j = 1; j < 500; j++) {
      if (i * b == j * a)
        ans = min(ans, dp[n][i][j]);
    }
  cout << (ans == inf ? -1 : ans) << "\n";
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#define ll long long
#define fi first
#define se second
using namespace std;

int n, a, b;
vector<pair<int, pair<int, int>>> v;
const int inf = 999999999;
int dp[41][500][500];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++) {
    int e, f, g;
    cin >> e >> f >> g;
    v.push_back(make_pair(e, make_pair(f, g)));
  }
  for (int i = 0; i < 41; 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 < 400; j++) {
      for (int k = 0; k < 400; k++) {
        dp[i + 1][j][k] = min(dp[i][j][k], dp[i + 1][j][k]);
        dp[i + 1][j + v[i].fi][k + v[i].se.fi] = min(
            dp[i + 1][j + v[i].fi][k + v[i].se.fi], dp[i][j][k] + v[i].se.se);
      }
    }
  }
  int ans = inf;
  for (int i = 1; i < 500; i++)
    for (int j = 1; j < 500; j++) {
      if (i * b == j * a)
        ans = min(ans, dp[n][i][j]);
    }
  cout << (ans == inf ? -1 : ans) << "\n";
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}