Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = a; (i) < (b); (i)++)
#define all(x) (x).begin(), (x).end()

using namespace std;
const int inf = 1001001000;
const long long int Inf = 1001001001001001000;

void print(vector<vector<int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<vector<long long int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<int> a) {
  int n = a.size();
  for (int j = 0; j < n; j++) {
    if (j != n - 1)
      cout << a[j] << " ";
    else
      cout << a[j] << endl;
  }
}

void print(set<int> a) {
  for (auto x : a)
    cout << x << " ";
  cout << endl;
}

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  vector<vector<int>> dp(n + 1, vector<int>(h + 1, inf));
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, a[i] + 1) {
      if (j == 0) {
        dp[i + 1][j] = 0;
      } else
        dp[i + 1][j] = b[i];
    }
  }
  rep(i, n) {
    rep(j, h + 1) {
      // rep2(k,max(j-a[i],0),j){
      // 	if (k >= 0){
      // 		dp[i + 1][j] = min({dp[i + 1][j], dp[i][j], dp[i + 1][k]
      // + b[i]}); 	} else 		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      // }
      if (j - a[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - a[i]] + b[i]);
      else
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][h] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = a; (i) < (b); (i)++)
#define all(x) (x).begin(), (x).end()

using namespace std;
const int inf = 1001001000;
const long long int Inf = 1001001001001001000;

void print(vector<vector<int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<vector<long long int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<int> a) {
  int n = a.size();
  for (int j = 0; j < n; j++) {
    if (j != n - 1)
      cout << a[j] << " ";
    else
      cout << a[j] << endl;
  }
}

void print(set<int> a) {
  for (auto x : a)
    cout << x << " ";
  cout << endl;
}

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  vector<vector<int>> dp(n + 1, vector<int>(h + 1, inf));
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, min(h + 1, a[i] + 1)) {
      if (j == 0) {
        dp[i + 1][j] = 0;
      } else
        dp[i + 1][j] = b[i];
    }
  }
  rep(i, n) {
    rep(j, h + 1) {
      // rep2(k,max(j-a[i],0),j){
      // 	if (k >= 0){
      // 		dp[i + 1][j] = min({dp[i + 1][j], dp[i][j], dp[i + 1][k]
      // + b[i]}); 	} else 		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      // }
      if (j - a[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - a[i]] + b[i]);
      else
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][h] << endl;
  return 0;
}