Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define endl '\n'
#define int long long
int h, n;
int a[2000], b[2000];
int dp[1001][10001];
int f(int i, int left) {
  if (left <= 0)
    return 0;
  if (i == n) {
    if (left == 0)
      return 0;
    return 1e18;
  }
  return dp[i][left] = min({f(i + 1, left), b[i] + f(i, left - a[i]),
                            b[i] + f(i + 1, left - a[i])});
}
int32_t main() {
  cin >> h >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  memset(dp, -1, sizeof(dp));
  cout << f(0, h);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define endl '\n'
#define int long long
int h, n;
int a[2000], b[2000];
int dp[1001][10001];
int f(int i, int left) {
  if (left <= 0)
    return 0;
  if (i == n) {
    if (left == 0)
      return 0;
    return 1e18;
  }
  if (dp[i][left] != -1)
    return dp[i][left];
  return dp[i][left] = min({f(i + 1, left), b[i] + f(i, left - a[i]),
                            b[i] + f(i + 1, left - a[i])});
}
int32_t main() {
  cin >> h >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  memset(dp, -1, sizeof(dp));
  cout << f(0, h);
}