Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
// #define endl "\\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
  return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

int D, N;
int a[111], e[111], r[111];
int INF = INT_MAX / 4;
int dp[111][111];
int solve(int d, int exp) {
  if (d == D)
    return 0;
  int &res = dp[d][exp];
  if (res != -1)
    return res;
  res = INF;
  rep(i, N) {
    int dd = min(D, d + a[i]), ee = min(100, exp + e[i]);
    if ((dd == d && ee == exp) || r[i] > exp)
      continue;
    res = min(res, 1 + solve(dd, ee));
  }
  return res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> D >> N && D) {
    rep(i, N) {
      cin >> a[i] >> e[i] >> r[i];
      if (a[i] == 0 && e[i] == 0)
        r[i] = INF;
    }
    memset(dp, -1, sizeof(dp));
    int ans = solve(0, 0);
    if (ans >= INF / 10)
      cout << "NA";
    else
      cout << ans;
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
// #define endl "\\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
  return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

int D, N;
int a[1111], e[1111], r[1111];
int INF = INT_MAX / 4;
int dp[1111][1111];
int solve(int d, int exp) {
  if (d >= D)
    return 0;
  if (exp >= 1111)
    return INF;
  int &res = dp[d][exp];
  if (res != -1)
    return res;
  res = INF;
  rep(i, N) {
    if (r[i] <= exp) {
      if (a[i] == 0 && e[i] == 0)
        continue;
      res = min(res, 1 + solve(d + a[i], exp + e[i]));
    }
  }
  return res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> D >> N && D) {
    rep(i, N) cin >> a[i] >> e[i] >> r[i];
    memset(dp, -1, sizeof(dp));
    int ans = solve(0, 0);
    if (ans == INF)
      cout << "NA";
    else
      cout << ans;
    cout << endl;
  }
}