Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define RREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
#define YY cout << "Yes" << endl
#define NN cout << "No" << endl
const long long INF = 1000000007;
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int solve() {
  int h, n;
  cin >> h >> n;
  vector<int> dp(100000, INF);
  vector<pair<int, int>> v(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    v[i].first = a;
    v[i].second = b;
  }
  dp[0] = 0;
  rep(i, h + 1) {
    rep(j, n) {
      int a = v[j].first, b = v[j].second;
      if (i - a < 0)
        dp[i] = min(dp[i], b);
      dp[i] = min(dp[i], dp[i - a] + b);
    }
  }
  cout << dp[h] << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define RREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
#define YY cout << "Yes" << endl
#define NN cout << "No" << endl
const long long INF = 1000000007;
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int solve() {
  int h, n;
  cin >> h >> n;
  vector<int> dp(100000, INF);
  vector<pair<int, int>> v(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    v[i].first = a;
    v[i].second = b;
  }
  dp[0] = 0;
  rep(i, h + 1) {
    rep(j, n) {
      int a = v[j].first, b = v[j].second;
      if (i - a < 0)
        dp[i] = min(dp[i], b);
      else
        dp[i] = min(dp[i], dp[i - a] + b);
    }
  }
  cout << dp[h] << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}