Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Rep(i, n) for (int i = 0; i < n; ++i)
#define rep(i, n) for (int i = 1; i <= n; ++i)
#define ll long long int
#define INF 1e9
#define LINF 1e18
#define MOD 1000000007
#define PI 3.14159265359
#define EPS 1e-10
#define All(a) (a).begin(), (a).end()

using namespace std;
using P = pair<ll, ll>;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

/**
 * Usage:
 *
 * G[from].push_back(Edge(to, weight));
 * using Graph = vector<vector<Edge>>;
 *
 */
struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) {}
};

/**
 * Usage:
 *
 * chmin(old, new);
 * chmax(old, new);
 *
 */
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void printv(vector<T> &v) {
  if (v.size() == 0) {
    cout << "\n";
    return;
  }

  Rep(i, v.size()) { cout << v[i] << ((i + 1 < v.size()) ? ' ' : '\n'); }
}

int H, N;
int a[1100], b[1100];
int dp[1010][20010];

int main() {
  cin >> H >> N;
  rep(i, N) { cin >> a[i] >> b[i]; }

  Rep(i, 1010) {
    Rep(j, 20010) { dp[i][j] = INF; }
  }

  dp[0][0] = 0;

  rep(i, N) {
    Rep(j, 20000) {
      chmin(dp[i][j], dp[i - 1][j]);

      if (dp[i - 1][j] != INF) {
        int ct = 1;
        while (true) {
          int k = j + a[i] * ct;
          chmin(dp[i][k], dp[i - 1][j] + b[i] * ct);
          if (k >= H)
            break;
          ct++;
        }
      }
    }
  }

  int ans = INF;
  for (int j = H; j <= 20000; ++j) {
    chmin(ans, dp[N][j]);
  }
  cout << ans << "\n";
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Rep(i, n) for (int i = 0; i < n; ++i)
#define rep(i, n) for (int i = 1; i <= n; ++i)
#define ll long long int
#define INF 1e9
#define LINF 1e18
#define MOD 1000000007
#define PI 3.14159265359
#define EPS 1e-10
#define All(a) (a).begin(), (a).end()

using namespace std;
using P = pair<ll, ll>;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

/**
 * Usage:
 *
 * G[from].push_back(Edge(to, weight));
 * using Graph = vector<vector<Edge>>;
 *
 */
struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) {}
};

/**
 * Usage:
 *
 * chmin(old, new);
 * chmax(old, new);
 *
 */
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void printv(vector<T> &v) {
  if (v.size() == 0) {
    cout << "\n";
    return;
  }

  Rep(i, v.size()) { cout << v[i] << ((i + 1 < v.size()) ? ' ' : '\n'); }
}

int H, N;
int a[1100], b[1100];
int dp[1010][20010];

int main() {
  cin >> H >> N;
  rep(i, N) { cin >> a[i] >> b[i]; }

  Rep(i, 1010) {
    Rep(j, 20010) { dp[i][j] = INF; }
  }

  dp[0][0] = 0;

  rep(i, N) {
    Rep(j, 20000) {
      if (j < a[i]) {
        chmin(dp[i][j], dp[i - 1][j]);
      } else {
        chmin(dp[i][j], min(dp[i - 1][j], dp[i][j - a[i]] + b[i]));
      }
    }
  }

  int ans = INF;
  for (int j = H; j <= 20000; ++j) {
    chmin(ans, dp[N][j]);
  }
  cout << ans << "\n";
}