Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define all(v) (v).begin(), (v).end()
#define sp cout << fixed << setprecision(10)
typedef pair<int, int> P;
const int inf = (int)1e9;
const int mod = (int)1e9 + 7;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int h, n;
int a[10005], b[10005];
int dp[1005][10005];

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> h >> n;
  rep(i, n) cin >> a[i] >> b[i];

  repe(j, h) dp[0][j] = ceil((double)j / a[0]) * b[0];
  // repe(i, n) {
  //     repe(j, h) { cout << dp[i][j] << " "; }
  //     cout << endl;
  // }
  // cout << endl;

  FOR(i, 1, n) {
    repe(j, h) {
      dp[i][j] = min(
          int(ceil((double)j / a[i]) * b[i]),
          min(dp[i - 1][j],
              min(dp[i - 1][j - a[i]] + b[i],
                  min(dp[i - 1][j - 2 * a[i]] + 2 * b[i],
                      min(dp[i - 1][j - 3 * a[i]] + 3 * b[i],
                          min(dp[i - 1][j - 4 * a[i]] + 4 * b[i],
                              min(dp[i - 1][j - 5 * a[i]] + 5 * b[i],
                                  min(dp[i - 1][j - 6 * a[i]] + 6 * b[i],
                                      min(dp[i - 1][j - 7 * a[i]] + 7 * b[i],
                                          min(dp[i - 1][j - 8 * a[i]] +
                                                  8 * b[i],
                                              min(dp[i - 1][j - 9 * a[i]] +
                                                      9 * b[i],
                                                  min(dp[i - 1][j - 10 * a[i]] +
                                                          10 * b[i],
                                                      min(dp[i - 1]
                                                            [j - 11 * a[i]] +
                                                              11 * b[i],
                                                          min(dp[i - 1]
                                                                [j -
                                                                 12 * a[i]] +
                                                                  12 * b[i],
                                                              min(dp[i - 1]
                                                                    [j -
                                                                     13 *
                                                                         a[i]] +
                                                                      13 * b[i],
                                                                  min(dp[i - 1]
                                                                        [j -
                                                                         14 *
                                                                             a[i]] +
                                                                          14 *
                                                                              b[i],
                                                                      dp[i - 1]
                                                                        [j -
                                                                         15 *
                                                                             a[i]] +
                                                                          15 *
                                                                              b[i]))))))))))))))));
    }
  }

  // repe(i, n) {
  //     repe(j, h) { cout << dp[i][j] << " "; }
  //     cout << endl;
  // }

  cout << dp[n - 1][h] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define all(v) (v).begin(), (v).end()
#define sp cout << fixed << setprecision(10)
typedef pair<int, int> P;
const int inf = (int)1e9;
const int mod = (int)1e9 + 7;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int h, n;
int a[10005], b[10005];
int dp[1005][10005];

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> h >> n;
  rep(i, n) cin >> a[i] >> b[i];

  repe(j, h) dp[0][j] = ceil((double)j / a[0]) * b[0];
  // repe(i, n) {
  //     repe(j, h) { cout << dp[i][j] << " "; }
  //     cout << endl;
  // }
  // cout << endl;

  FOR(i, 1, n) {
    repe(j, h) { dp[i][j] = min(dp[i - 1][j], dp[i][j - a[i]] + b[i]); }
  }

  // repe(i, n) {
  //     repe(j, h) { cout << dp[i][j] << " "; }
  //     cout << endl;
  // }

  cout << dp[n - 1][h] << endl;

  return 0;
}