Problem p02787 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  int H, N;
  cin >> H >> N;
  vector<int> A, B;
  rep(i, N) {
    int a, b;
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }

  int a_max = *max_element(A.begin(), A.end());
  vector<ll> dp; // dp[H] is final goal, but cost for dp[H + 1], dp[H + 2], ...,
                 // dp[H + a_max - 1] might be sometimes smaller
  rep(i, H + a_max - 1) {
    if (i == 0) {
      dp.push_back(0);
      continue;
    }
    ll min_cost = MOD;
    rep(j, N) {
      if (i - A.at(j) >= 0) {
        if (min_cost > (B.at(j) + dp.at(i - A.at(j)))) {
          min_cost = B.at(j) + dp.at(i - A.at(j));
        }
      }
    }
    dp.push_back(min_cost);
  }
  ll ans = MOD;
  for (int i = H; i <= H + a_max - 1; i++) {
    if (dp.at(i) < ans) {
      ans = dp.at(i);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  int H, N;
  cin >> H >> N;
  vector<int> A, B;
  rep(i, N) {
    int a, b;
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }

  int a_max = *max_element(A.begin(), A.end());
  vector<ll> dp; // dp[H] is final goal, but cost for dp[H + 1], dp[H + 2], ...,
                 // dp[H + a_max - 1] might be sometimes smaller
  rep(i, H + a_max) {
    if (i == 0) {
      dp.push_back(0);
      continue;
    }
    ll min_cost = MOD;
    rep(j, N) {
      if (i - A.at(j) >= 0) {
        if (min_cost > (B.at(j) + dp.at(i - A.at(j)))) {
          min_cost = B.at(j) + dp.at(i - A.at(j));
        }
      }
    }
    dp.push_back(min_cost);
  }
  ll ans = MOD;
  for (int i = H; i <= H + a_max - 1; i++) {
    if (dp.at(i) < ans) {
      ans = dp.at(i);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}