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 <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e8
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define N (1 << 7)

const int maxn = 45;
int n, ma, mb, ans = INF;
int a[maxn], b[maxn], c[maxn];
int dp[2][405][405];

int main() {
  scanf("%d%d%d", &n, &ma, &mb);
  REP(i, n) scanf("%d%d%d", &a[i], &b[i], &c[i]);
  for (int cnt = 1; cnt <= min(400 / ma, 400 / mb); cnt++) {
    int x = ma * cnt, y = mb * cnt;
    for (int p = 0; p < 2; p++)
      for (int i = 0; i <= x; i++)
        for (int j = 0; j <= y; j++)
          dp[p][i][j] = INF;
    dp[0][0][0] = 0;
    for (int p = 1; p <= n; p++)
      for (int i = 0; i <= x; i--)
        for (int j = 0; j <= y; j--) {
          dp[p & 1][i][j] = dp[(p - 1) & 1][i][j];
          if (i >= a[p] && j >= b[p])
            dp[p & 1][i][j] = min(dp[(p - 1) & 1][i - a[p]][j - b[p]] + c[p],
                                  dp[p & 1][i][j]);
        }
    ans = min(ans, dp[n & 1][x][y]);
  }
  if (ans == INF)
    printf("-1");
  else
    printf("%d", ans);
  return 0;
}
/*
40 2 3
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
*/

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 <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e8
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define N (1 << 7)

const int maxn = 45;
int n, ma, mb, ans = INF;
int a[maxn], b[maxn], c[maxn];
int dp[2][405][405];

int main() {
  scanf("%d%d%d", &n, &ma, &mb);
  REP(i, n) scanf("%d%d%d", &a[i], &b[i], &c[i]);
  for (int cnt = 1; cnt <= min(400 / ma, 400 / mb); cnt++) {
    int x = ma * cnt, y = mb * cnt;
    for (int p = 0; p < 2; p++)
      for (int i = 0; i <= x; i++)
        for (int j = 0; j <= y; j++)
          dp[p][i][j] = INF;
    dp[0][0][0] = 0;
    for (int p = 1; p <= n; p++)
      for (int i = 0; i <= x; i++)
        for (int j = 0; j <= y; j++) {
          dp[p & 1][i][j] = dp[(p - 1) & 1][i][j];
          if (i >= a[p] && j >= b[p])
            dp[p & 1][i][j] = min(dp[(p - 1) & 1][i - a[p]][j - b[p]] + c[p],
                                  dp[p & 1][i][j]);
        }
    ans = min(ans, dp[n & 1][x][y]);
  }
  if (ans == INF)
    printf("-1");
  else
    printf("%d", ans);
  return 0;
}
/*
40 2 3
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
10 10 100
*/