Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \
  std::vector<type> c(n);                                                      \
  for (auto &i : c)                                                            \
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;

vector<int> bitSearch(int bit, int n) {
  vector<int> S, False(1, -1);
  rep(i, n) if (bit & (1 << i)) S.push_back(i);
  return S;
}

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

const ll INF = 1e9;
int dp[1005][10005];
int main() {
  int h, n;
  cin >> h >> n;
  vector<int> attack(n), cost(n);
  rep(i, n) { cin >> attack[i] >> cost[i]; }
  rep(i, n + 10) rep(j, h + 10) dp[i][j] = INF;
  rep(i, n) dp[i][0] = 0;

  rep(i, n) {
    rep(j, h + 1) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][min(j + attack[i], h)] =
          min(dp[i + 1][min(j + attack[i], h)], dp[i + 1][j] + cost[i]);
    }
  }
  cout << dp[n][h] << endl;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \
  std::vector<type> c(n);                                                      \
  for (auto &i : c)                                                            \
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;

vector<int> bitSearch(int bit, int n) {
  vector<int> S, False(1, -1);
  rep(i, n) if (bit & (1 << i)) S.push_back(i);
  return S;
}

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

const ll INF = 1e9;
ll dp[1010][10100];
int main() {
  int h, n;
  cin >> h >> n;
  vector<int> attack(n), cost(n);
  rep(i, n) { cin >> attack[i] >> cost[i]; }
  rep(i, n + 10) rep(j, h + 10) dp[i][j] = INF;
  rep(i, n) dp[i][0] = 0;

  rep(i, n) {
    rep(j, h + 1) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][min(j + attack[i], h)] =
          min(dp[i + 1][min(j + attack[i], h)], dp[i + 1][j] + cost[i]);
    }
  }
  cout << dp[n][h] << endl;
}