Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <queue>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, s, n) for (ll i = s; i < (n); ++i)
using ll = long long;
using pll = pair<ll, ll>;
constexpr ll INF = (1LL << 60);
constexpr ll MOD = (1e9 + 7);
// constexpr ll MOD = (998244353);

using vl = vector<ll>;
using vvl = vector<vector<ll>>;

template <class T> vector<vector<T>> vvt(T init, ll m, ll n) {
  vector<vector<T>> ans = vector<vector<T>>(m, vector<T>(n, init));
  return move(ans);
}

template <class T> vector<T> vt(T init, ll n) {
  vector<T> ans = vector<T>(n, init);
  return move(ans);
}

template <class T> T maxVec(vector<T> &v) {
  T ans = -INF;
  rep(i, 0, v.size()) { ans = max(ans, v[i]); }
  return ans;
}

// 素数判定
bool judge(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n %= i)
      return false;
  return true;
}

template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, ", "));
  os << std::endl;
}

ll count(ll n, ll r) {
  ll ans = 0, check = 1;
  rep(i, 0, r) {
    if ((n & check) > 0) {
      ans++;
    }
    check = check << 1;
  }
  return ans;
}

bool sortreverse(ll a, ll b) { return a > b; }

bool kansu(pll a, pll b) { return a.first < b.first; }

ll kiriage(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}

ll dp[10010], maryoku[10010];

int main() {
  ll h, n;
  cin >> h >> n;
  vl a(n), b(n);
  rep(i, 0, n) { cin >> a[i] >> b[i]; }
  dp[h] = 0;
  rep(i, 0, 10010) {
    if (i == h)
      continue;
    dp[i] = 100000000000;
  }
  for (ll i = 1; i < h + 1; i++) {
    ll ref = INF, ma = 0;
    for (ll j = 0; j < n; j++) {
      if (ref > dp[h - i + a[j]] + b[j]) {
        ref = dp[h - i + a[j]] + b[j];
      }
    }
    dp[h - i] = ref;
  }
  ll ans = INF;
  rep(i, 0, n) {
    rep(j, 0, a[i] + 1) { ans = min(ans, dp[j] + b[i]); }
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <queue>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, s, n) for (ll i = s; i < (n); ++i)
using ll = long long;
using pll = pair<ll, ll>;
constexpr ll INF = (1LL << 60);
constexpr ll MOD = (1e9 + 7);
// constexpr ll MOD = (998244353);

using vl = vector<ll>;
using vvl = vector<vector<ll>>;

template <class T> vector<vector<T>> vvt(T init, ll m, ll n) {
  vector<vector<T>> ans = vector<vector<T>>(m, vector<T>(n, init));
  return move(ans);
}

template <class T> vector<T> vt(T init, ll n) {
  vector<T> ans = vector<T>(n, init);
  return move(ans);
}

template <class T> T maxVec(vector<T> &v) {
  T ans = -INF;
  rep(i, 0, v.size()) { ans = max(ans, v[i]); }
  return ans;
}

// 素数判定
bool judge(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n %= i)
      return false;
  return true;
}

template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, ", "));
  os << std::endl;
}

ll count(ll n, ll r) {
  ll ans = 0, check = 1;
  rep(i, 0, r) {
    if ((n & check) > 0) {
      ans++;
    }
    check = check << 1;
  }
  return ans;
}

bool sortreverse(ll a, ll b) { return a > b; }

bool kansu(pll a, pll b) { return a.first < b.first; }

ll kiriage(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}

ll dp[40010];

int main() {
  ll h, n;
  cin >> h >> n;
  vl a(n), b(n);
  rep(i, 0, n) { cin >> a[i] >> b[i]; }
  dp[h] = 0;
  rep(i, 0, 10010) {
    if (i == h)
      continue;
    dp[i] = 100000000000;
  }
  for (ll i = 1; i < h + 1; i++) {
    ll ref = INF, ma = 0;
    for (ll j = 0; j < n; j++) {
      if (ref > dp[h - i + a[j]] + b[j]) {
        ref = dp[h - i + a[j]] + b[j];
      }
    }
    dp[h - i] = ref;
  }
  ll ans = INF;
  rep(i, 0, n) {
    rep(j, 0, a[i] + 1) { ans = min(ans, dp[j] + b[i]); }
  }
  printf("%lld\n", ans);
}