Problem p02787 (C++)

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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
typedef long long ll;

int dp[10010];
int a[1010], b[1010];
signed main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  vector<pair<double, double>> v(n);
  REP(i, n)
  cin >> a[i] >> b[i];

  vector<pair<double, double>> p(n);

  int cnt = 0;
  for (int i = 0; i <= 10000; i++) {
    dp[i] = INF;
  }
  // cerr << dp[0] << endl;
  for (int i = 0; i < n; i++) {
    dp[0] = min(dp[0], b[i]);
  }
  // cerr << dp[0] << endl;
  // dp[0] = 0;
  dp[0] = 0;
  for (int i = 0; i <= h; i++) {
    bool ok = false;
    for (int j = 0; j < n; j++) {
      dp[i + a[j]] = min(dp[i] + b[j], dp[i + a[j]]);
      ok = true;
    }
  }
  /*  for (int i = 0; i <= h; i++)
    {
        cerr << dp[i] << endl;
    }*/
  int ans = INF;
  for (int i = h; i <= 10000; i++) {
    ans = min(ans, dp[i]);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
typedef long long ll;

int dp[100010];
int a[1010], b[1010];
signed main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  vector<pair<double, double>> v(n);
  REP(i, n)
  cin >> a[i] >> b[i];

  vector<pair<double, double>> p(n);

  int cnt = 0;
  for (int i = 0; i <= 10000; i++) {
    dp[i] = INF;
  }
  // cerr << dp[0] << endl;
  for (int i = 0; i < n; i++) {
    dp[0] = min(dp[0], b[i]);
  }
  // cerr << dp[0] << endl;
  // dp[0] = 0;
  dp[0] = 0;
  for (int i = 0; i <= h; i++) {
    bool ok = false;
    for (int j = 0; j < n; j++) {
      dp[i + a[j]] = min(dp[i] + b[j], dp[i + a[j]]);
      ok = true;
    }
  }
  /*  for (int i = 0; i <= h; i++)
    {
        cerr << dp[i] << endl;
    }*/
  int ans = INF;
  for (int i = h; i <= 10000; i++) {
    ans = min(ans, dp[i]);
  }
  cout << ans << endl;

  return 0;
}