Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
#define orz 1000000007
// 作者:中国江苏南京丁天行
// from CrAzYUnIoN
// I prefer being hacked to being FST
using namespace std;
// 数组不要开小了
int n, a[50], b[50], c[50], ma, mb;
int f[205][205];
int main() {
  scanf("%d%d%d", &n, &ma, &mb);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", a + i, b + i, c + i);
  for (int i = 0; i <= ma * 20; i++) {
    for (int j = 0; j <= mb * 20; j++)
      f[i][j] = orz;
  }
  f[0][0] = 0;
  int maa = 20 * ma, mbb = 20 * mb;
  for (int i = 1; i <= n; i++) {
    for (int j = maa; j >= a[i]; j--) {
      for (int k = mbb; k >= b[i]; k++)
        f[j][k] = min(f[j][k], f[j - a[i]][k - b[i]] + c[i]);
    }
  }
  int ans = orz;
  for (int i = 1; i <= 20; i++)
    ans = min(ans, f[i * ma][i * mb]);
  if (ans == orz)
    puts("-1");
  else
    printf("%d\n", ans);
  //	system("pause");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
#define orz 1000000007
// 作者:中国江苏南京丁天行
// from CrAzYUnIoN
// I prefer being hacked to being FST
using namespace std;
// 数组不要开小了
int n, a[50], b[50], c[50], ma, mb;
int f[205][205];
int main() {
  scanf("%d%d%d", &n, &ma, &mb);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", a + i, b + i, c + i);
  for (int i = 0; i <= ma * 20; i++) {
    for (int j = 0; j <= mb * 20; j++)
      f[i][j] = orz;
  }
  f[0][0] = 0;
  int maa = 20 * ma, mbb = 20 * mb;
  for (int i = 1; i <= n; i++) {
    for (int j = maa; j >= a[i]; j--) {
      for (int k = mbb; k >= b[i]; k--)
        f[j][k] = min(f[j][k], f[j - a[i]][k - b[i]] + c[i]);
    }
  }
  int ans = orz;
  for (int i = 1; i <= 20; i++)
    ans = min(ans, f[i * ma][i * mb]);
  if (ans == orz)
    puts("-1");
  else
    printf("%d\n", ans);
  //	system("pause");
  return 0;
}