Problem:
巨樹の刻み手
あなたは
N
種類の道具を使って、目の前の巨樹を切り倒そうとしています。はじめは、樹の耐久力は
D
、あなたの経験値は 0 ですが、道具
i
で１回樹を叩くと樹の耐久力は
a
i
減り、あなたは
e
i
の経験値を得ます。ただし、道具
i
を使うためには、あなたは
r
i
以上の経験値を持っていなければなりません。樹の耐久力が 0 以下になると樹は倒れます。
樹の耐久力と道具についての情報が与えられたとき、樹を切り倒すには最低何回樹を叩かなければいけないかを求めるプログラムを作成してください。
入力
入力は複数のデータセットからなる。入力の終わりはゼロ２つの行で示される。各データセットは以下の形式で与えられる。
D
N
a
1
e
1
r
1
a
2
e
2
r
2
:
a
N
e
N
r
N
1 行目に樹の耐久力を表す整数
D
(1 ≤
D
≤ 100) と道具の種類の数
N
(1 ≤
N
≤ 100) が与えられる。続く
N
行に道具 1 から
N
までの情報が与えられる。
a
i
(0 ≤
a
i
≤ 100) と
e
i
(0 ≤
e
i
≤ 100) は、道具iを１回使うことで減る樹の耐久力とあなたが得ることのできる経験値をそれぞれ表す。
r
i
(0 ≤
r
i
≤ 100) は道具を使うために必要な経験値を表す。
a
i
,
e
i
,
r
i
はすべて整数である。
データセットの数は 50 を超えない。
出力
樹を切り倒すのに必要な、樹を叩く最小の回数を１行に出力する。ただし、樹を切り倒すことが不可能な場合は
NA
と出力する。
入出力例
入力例
15 4
1 1 0
1 2 2
5 10 5
8 1 15
60 4
5 2 0
8 8 2
3 5 0
49 0 18
100 1
1 1 1
0 0
出力例
6
4
NA

Submitted solutions:

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

using P = pair<int, int>;

constexpr int INF = 1e9;

int main() {
    int D, N;
    while(cin >> D >> N, D) {
        using tool = tuple<int, int, int>;
        vector<tool> v(N);
        for(int i=0; i<N; ++i) {
            int a, e, r;
            cin >> a >> e >> r;
            v[i] = make_tuple(a, e, r);
        }
        vector<vector<int>> dp(D+1, vector<int>(101, INF));
        queue<P> que;
        que.push(make_pair(0, 0));
        dp[0][0] = 0;
        while(!que.empty()) {
            P p = que.front();
            que.pop();
            for(int i=0; i<N; ++i) {
                int a, e, r;
                tie(a, e, r) = v[i];
                if(p.second < r) {
                    continue;
                }
                int nd = min(D, p.first + a);
                int nr = min(100, p.second + e);
                if(dp[nd][nr] > dp[p.first][p.second] + 1) {
                    dp[nd][nr] = dp[p.first][p.second] + 1;
                    que.push(make_pair(nd, nr));
                }
            }
        }
        int res = INF;
        for(int i=0; i<101; ++i) {
            res = min(res, dp[D][i]);
        }
        if(res == INF) {
            cout << "NA" << endl;
        } else {
            cout << res << endl;
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3492
final_rank: 9

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  cout << (x);
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}
template <typename T>
void wt_integer128(T x) {
  char buf[64];
  char *d = end(buf);
  d--; *d = '\0';
  __uint128_t tmp = ((x < 0)? -x : x);
  do {
    d--; *d = char(tmp%10 + '0'); tmp /= 10;
  } while (tmp);
  if (x < 0) {
    d--; *d = '-';
  }
  cout << d;
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer128(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer128(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {}
  return 0;
}

int solve() {
  ll D, N;
  cin >> D >> N;
  if (D==0 and N==0) {return 1;}
  vi a(N), e(N), r(N);
  rep(i, N) {cin >> a[i] >> e[i] >> r[i];}
  ll rmax = MAX(r);
  vv(ll, dp, D+1, rmax+1, inf);
  dp[0][0] = 0;
  rep(i, D) {
    rep(j, rmax+1) {
      rep(k, N) {
        if (r[k] > j) {continue;}
        chmin(dp[min(i+a[k], D)][min(j+e[k], rmax)], dp[i][j]+1);
      }
    }
  }
  ll ans = MIN(dp[D]);
  if (ans == inf) print("NA");
  else print(ans);
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3488
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
int main(){
  while (true){
    int D, N;
    cin >> D >> N;
    if (D == 0 && N == 0){
      break;
    }
    vector<int> a(N), e(N), r(N);
    for (int i = 0; i < N; i++){
      cin >> a[i] >> e[i] >> r[i];
    }
    vector<vector<int>> dp(D + 1, vector<int>(101, INF));
    dp[D][0] = 0;
    for (int i = D; i > 0; i--){
      for (int j = 0; j <= 100; j++){
        for (int k = 0; k < N; k++){
          if (j >= r[k]){
            int i2 = max(i - a[k], 0);
            int j2 = min(j + e[k], 100);
            dp[i2][j2] = min(dp[i2][j2], dp[i][j] + 1);
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i <= 100; i++){
      ans = min(ans, dp[0][i]);
    }
    if (ans == INF){
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}
accuracy: 1
time_ms: 60
memory_kb: 3464
final_rank: 10

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


// {{{

// clang-format off
#define len(x) (static_cast<int>((x).size()))
#define _over_load(_1,_2,_3,_4,NAME,...) NAME
#define range(...) _over_load(__VA_ARGS__, range4, range3, range2)(__VA_ARGS__)
#define range2(i, r) for ( int i = 0; i < static_cast<int>(r); (i) += 1)
#define range3(i, l, r) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += 1)
#define range4(i, l, r, step) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += (step))
#define rrange(...) _over_load(__VA_ARGS__, rrange4, rrange3, rrange2)(__VA_ARGS__)
#define rrange2(i, r) for ( int i = static_cast<int>(r) - 1; i >= 0; (i) -= 1)
#define rrange3(i, l, r) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= 1)
#define rrange4(i, l, r, step) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= (step))
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__)
template <typename T, typename... T2> void debug_function(string_view name, const T &a, T2 &&...rest) {
  stack<char> bs;
  string_view lbs = "({[<", rbs = ")}]>";
  int end = name.size();
  for ( int i = 0; i < len(name); i++ ) {
    if ( lbs.find(name[i]) != string::npos ) bs.push(name[i]);
    if ( rbs.find(name[i]) != string::npos and !bs.empty() ) bs.pop();
    if ( name[i] == ',' and bs.empty() ) {
      end = i;
      break;
    }
  }
  cerr << name.substr(0, end) << ":" << a;
  if constexpr ( sizeof...(rest) == 0 ) {
    cerr << '\n';
  } else {
    cerr << ' ';
    debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward<T2>(rest)...);
  }
}
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << ' ' << p.second; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { for ( pair<T1, T2> x : v ) { os << "(" << x.first <<", " << x.second << ")" << (v.rbegin()->first == x.first ? "" : ", "); } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { if(!v.empty()) { os << v.front(); v.pop(); } while (!v.empty()) { os << " " << v.front(); v.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { if(!v.empty()) { os << v.top(); v.pop(); } while (!v.empty()) { os << " " << v.top(); v.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { range(i, len(v)) { cout << v[i] << (i == len(v) - 1 ? "" : " "); } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { range(i, len(v)) { cout << v[i] << (i == len(v) - 1 ? "" : " "); } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
struct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
using ull = unsigned long long;
using ll = long long;
using Pll = pair<ll, ll>;
using P = pair<int, int>;
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, -1, 1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { -1, 0, 0, 1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = static_cast<int>(1e9) + 7;
constexpr char newl = '\n';
// clang-format on

// }}}

void solve(int d,int n){
  vector<tuple<int,int,int>> tools(n);
  for(auto &[a,e,r]:tools){
    cin>>a>>e>>r;
  }

  constexpr int MAXR = 100;
  auto dp=make_vector(d+1,MAXR+1,INF32);

  dp[0][0]=0;

  range(i,d + 1){
    range(j,MAXR + 1){
      range(k,n){
        auto [a,e,r]=tools[k];
        if(j<r) continue;
        int nxti=min(d,i+a);
        int nxtj=min(MAXR,j+e);
        chmin(dp[nxti][nxtj],dp[i][j]+1);
      }
    }
  }

  int ans = INF32;
  range(j,MAXR+1){
    chmin(ans,dp[d][j]);
  }

  if(ans==INF32){
    cout<<"NA"<<endl;
  }else{
    cout<<ans<<endl;
  }
}

int main() {

  while(1){
    int d,n;
    cin>>d>>n;
    if(d==0 and n==0) break;
    solve(d,n);
  }
}
accuracy: 1
time_ms: 70
memory_kb: 3484
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
using namespace std;
struct x{
  int a,e,r;
};
void solve(int d,int n){
  //dp[101][101] // [回数][現在の経験値] = 耐久値
  int dp[202][101];
  for(int i = 0; 202 > i; i++){
    for(int j = 0; 101 > j; j++){
      dp[i][j] = d+1;
    }
  }
  dp[0][0] = d;
  vector<x> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i].a>>A[i].e>>A[i].r;
  }
  for(int i = 0; 202 > i; i++){
    for(int j = 0; n > j; j++){
      for(int l = A[j].r; 101 > l; l++){
        if(dp[i][l] == d+1)continue;
        if(dp[i+1][min(100,l+A[j].e)] == d+1){
          dp[i+1][min(100,l+A[j].e)] = dp[i][l]-A[j].a;
          if(dp[i][l]-A[j].a <= 0){
            cout << i+1 << endl;
            return;
          }
        }else{
          dp[i+1][min(100,l+A[j].e)] = min(dp[i+1][min(100,l+A[j].e)] ,dp[i][l]-A[j].a);
          if(dp[i][l]-A[j].a <= 0){
            cout << i+1 << endl;
            return;
          }
        }
      }
    }
  }
  cout << "NA" << endl;
}

int main(){
  int d,n;
  while(cin>>d>>n,d){
    solve(d,n);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3496
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
using namespace std;
struct x{
  int a,e,r;
};
void solve(int d,int n){
  //dp[101][101] // [回数][現在の経験値] = 耐久値
  int dp[102][101];
  for(int i = 0; 101 > i; i++){
    for(int j = 0; 101 > j; j++){
      dp[i][j] = d+1;
    }
  }
  dp[0][0] = d;
  vector<x> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i].a>>A[i].e>>A[i].r;
  }
  for(int i = 0; 101 > i; i++){
    for(int j = 0; n > j; j++){
      for(int l = A[j].r; 101 > l; l++){
        if(dp[i][l] == d+1)continue;
        if(dp[i+1][min(100,l+A[j].e)] == d+1){
          dp[i+1][min(100,l+A[j].e)] = dp[i][l]-A[j].a;
          if(dp[i][l]-A[j].a <= 0){
            cout << i+1 << endl;
            return;
          }
        }else{
          dp[i+1][min(100,l+A[j].e)] = min(dp[i+1][min(100,l+A[j].e)] ,dp[i][l]-A[j].a);
          if(dp[i][l]-A[j].a <= 0){
            cout << i+1 << endl;
            return;
          }
        }
      }
    }
  }
  cout << "NA" << endl;
}

int main(){
  int d,n;
  while(cin>>d>>n,d){
    solve(d,n);
  }
}
accuracy: 0.9
time_ms: 10
memory_kb: 3452
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
using namespace std;
struct x{
  int a,e,r;
};
void solve(int d,int n){
  //dp[101][101] // [回数][現在の経験値] = 耐久値
  int dp[101][101];
  for(int i = 0; 101 > i; i++){
    for(int j = 0; 101 > j; j++){
      dp[i][j] = d+1;
    }
  }
  dp[0][0] = d;
  vector<x> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i].a>>A[i].e>>A[i].r;
  }
  for(int i = 0; 101 > i; i++){
    for(int j = 0; n > j; j++){
      for(int l = A[j].r; 101 > l; l++){
        if(dp[i][l] == d+1)continue;
        if(dp[i+1][min(100,l+A[j].e)] == d+1){
          dp[i+1][min(100,l+A[j].e)] = dp[i][l]-A[j].a;
          if(dp[i][l]-A[j].a <= 0){
            cout << i+1 << endl;
            return;
          }
        }else{
          dp[i+1][min(100,l+A[j].e)] = min(dp[i+1][min(100,l+A[j].e)] ,dp[i][l]-A[j].a);
          if(dp[i][l]-A[j].a <= 0){
            cout << i+1 << endl;
            return;
          }
        }
      }
    }
  }
  cout << "NA" << endl;
}

int main(){
  int d,n;
  while(cin>>d>>n,d){
    solve(d,n);
  }
}
accuracy: 0.9
time_ms: 10
memory_kb: 3452
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
using namespace std;
struct x{
  int a,e,r;
};
void solve(int d,int n){
  //dp[101][101] // [幹の耐久力][現在の経験値] = 切り倒す回数
  int dp[101][101];
  bool lck[101][101];
  for(int i = 0; 101 > i; i++){
    for(int j = 0; 101 > j; j++){
      dp[i][j] = -1;
      lck[i][j] = false;
    }
  }
  dp[d][0] = 0;
  lck[d][0] = true; 
  vector<x> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i].a>>A[i].e>>A[i].r;
  }
  for(int i = 0; 101 > i; i++){
    for(int j = 0; n > j; j++){
      for(int k = 0; 101 > k; k++){//耐久力
        for(int l = A[j].r; 101 > l; l++){
          if(dp[k][l] == -1 || lck[max(0,k-A[j].a)][min(100,l+A[j].e)])continue;
          dp[max(0,k-A[j].a)][min(100,l+A[j].e)] = dp[k][l]+1;
          lck[max(0,k-A[j].a)][min(100,l+A[j].e)]  = true;
        }
      }
    }
  }
  int ret = -1;
  for(int i = 0; 101 > i; i++){
    if(dp[0][i] != -1){
      if(ret == -1){
        ret = dp[0][i];
      }else{
        ret = min(dp[0][i],ret);
      }
    }
  }
  if(ret == -1)cout << "NA" << endl;
  else cout << ret << endl;
}

int main(){
  int d,n;
  while(cin>>d>>n,d){
    solve(d,n);
  }
}
accuracy: 0.1
time_ms: 90
memory_kb: 3392
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
using namespace std;
struct x{
  int a,e,r;
};
void solve(int d,int n){
  //dp[101][101] // [幹の耐久力][現在の経験値] = 切り倒す回数
  int dp[101][101];
  bool lck[101][101];
  for(int i = 0; 101 > i; i++){
    for(int j = 0; 101 > j; j++){
      dp[i][j] = -1;
      lck[i][j] = false;
    }
  }
  dp[d][0] = 0;
  vector<x> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i].a>>A[i].e>>A[i].r;
  }
  for(int i = 0; 101 > i; i++){
    for(int j = 0; n > j; j++){
      for(int k = 0; 101 > k; k++){//耐久力
        for(int l = A[j].r; 101 > l; l++){
          if(dp[k][l] == -1 || lck[k-A[j].a][min(100,l+A[j].e)])continue;
          dp[max(0,k-A[j].a)][min(100,l+A[j].e)] = dp[k][l]+1;
          lck[max(0,k-A[j].a)][min(100,l+A[j].e)]  = true;
        }
      }
    }
  }
  int ret = -1;
  for(int i = 0; 101 > i; i++){
    if(dp[0][i] != -1){
      if(ret == -1){
        ret = dp[0][i];
      }else{
        ret = min(dp[0][i],ret);
      }
    }
  }
  if(ret == -1)cout << "NA" << endl;
  else cout << ret << endl;
}

int main(){
  int d,n;
  while(cin>>d>>n,d){
    solve(d,n);
  }
}
accuracy: 0.1
time_ms: 100
memory_kb: 3460
final_rank: 20

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

string func(int d,int n){
    vector<int> dam(n);
    vector<int> ex(n);
    vector<int> need(n);
    rep(i,n){
        dam[i] = in();
        ex[i] = in();
        need[i] = in();
    }
    vvector<int> dp(101,vector<int>(101,-1));
    method(rec,int,int health,int level){
        chmin(level,100);
        if(health <= 0)return 0;
        int &it = dp[health][level];
        if(it >= 0)return it;
        it = INF;
        rep(i,n){
            if(need[i] <= level){
                chmin(it,1+rec(health-dam[i],level+ex[i]));
            }
        }
        return it;
    };
    int res = rec(d,0);
    return res >= INF ? "NA" : to_string(res);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true){
        int d = in();
        int n = in();
        if(d==0)break;
        println(func(d,n));
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3564
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
#include<sstream>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int inf=mod*mod;
signed main(){
	int d,n;
	while(cin>>d>>n,d){
		int a[100],e[100],r[100];
		for(int i=0;i<n;i++)
			cin>>a[i]>>e[i]>>r[i];
		int dp[101][101];
		for(int i=0;i<=d;i++)
			for(int j=0;j<=100;j++)
				dp[i][j]=inf;
		dp[0][0]=0;
		for(int i=0;i<=d;i++)
			for(int j=0;j<=100;j++)
				for(int k=0;k<n;k++)
					dp[i][j]=min(dp[i][j],dp[max(i-a[k],0ll)][max(j-e[k],r[k])]+1);
		if(dp[d][0]==inf)
			cout<<"NA"<<endl;
		else
			cout<<dp[d][0]<<endl;
	}
}
accuracy: 1
time_ms: 50
memory_kb: 3520
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    while(1){
        int d,n;
        cin >> d >> n;
        if(d == 0) break;

        vector<int> a(n),e(n),r(n);
        for(int i=0; i<n; i++){
            cin >> a[i] >> e[i] >> r[i];
        }
        const int inf = 1e9;
        vector<vector<int>> dp(d+1, vector<int>(101, inf));
        dp[d][0] = 0;
        for(int i=d; i>0; i--){
            for(int j=0; j<=100; j++){
                for(int k=0; k<n; k++){
                    if(j < r[k]) continue;
                    int ni = max(0, i-a[k]);
                    int nj = min(100, j+e[k]);
                    dp[ni][nj] = min(dp[ni][nj], dp[i][j]+1);
                }
            }
        }
        int res = *min_element(dp[0].begin(), dp[0].end());
        if(res == inf){
            cout << "NA" << endl;
        }else{
            cout << res << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3412
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define mod 1000000007
#define ad(a,b) a=(a+b)%mod;
#define X 5010
ll po(ll x,ll y){
	ll res=1;
	for(;y;y>>=1){
		if(y&1)res=res*x%mod;
		x=x*x%mod;
	}
	return res;
}
ll fac[X],ivf[X];
void init(){
	fac[0]=1;
	for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod;
	for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2);
}
ll C(ll n,ll k){
	return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}
ll dp[110][110];
bool solve(){
	rep(i,110)rep(j,110)dp[i][j]=1e17;
	ll d,n;
	cin>>d>>n;
	if(n==0&&d==0)return 0;
	ll a[110],e[110],r[110];
	rep(i,n)cin>>a[i]>>e[i]>>r[i];
	dp[d][0]=0;
	ll ans=1e17;
	for(int h=d;h>0;h--)for(int w=0;w<=100;w++){
		for(int i=0;i<n;i++){
			if(a[i]==0&&e[i]==0)continue;
			if(w<r[i])continue;
			if(h-a[i]<=0){
				chmin(ans,dp[h][w]+1);
				continue;
			}
			chmin(dp[h-a[i]][min(100LL,w+e[i])],dp[h][w]+1);
		}
	}
	if(ans>=1e17)cout<<"NA"<<endl;
	else cout<<ans<<endl;
	return 1;
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	while(solve()){}
}
accuracy: 1
time_ms: 90
memory_kb: 3312
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define each(i, a) for(auto &i : a)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
#define elif else if
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }

int main() {
    init();

    for (int d, n; cin >> d >> n && d;) {
        VI a(n), e(n), r(n);
        FOR(i, 0, n)cin >> a[i] >> e[i] >> r[i];
        auto dp = defvec<int>(INF, d + 1, 101);
        dp[0][0] = 0;
        FOR(i, 0, d)eFOR(j, 0, 100)FOR(k, 0, n) {
            if (j < r[k])continue;;
            chmin(dp[min(i + a[k], d)][min(j + e[k], 100)],
                dp[i][j] + 1);
        }
        int ans = *min_element(all(dp[d]));
        if (ans == INF)print("NA");
        else print(ans);
    }

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3288
final_rank: 5

code_snippet: #include <iostream>
#include<vector>
#include<algorithm>
#include<string.h>
#include <queue>
#include<set>
using namespace std;
struct E{
	int a,e,r;
	bool operator<(const E& e1)const{
		if(r!=e1.r)return r<e1.r;
		if(a!=e1.a)return a<e1.a;
		return e<e1.e;
	}
};
struct E2{
	int no,dmg,exp;
	bool operator<(const E2& e1)const{
		if(no!=e1.no)return no<e1.no;
		return dmg<e1.dmg;
	}
};

int dp[102][102];//道具の番号、ダメージ,経験値

int main() {
	// your code goes here
while(true){
	int d,n;
	cin>>d>>n;
	if(d+n==0)break;
	E e;
	vector<E> vs;
	memset(dp,-1,sizeof(dp));
	int sum=0;
	for(int i=0;i<n;i++){
		int a1,e1,r1;
		cin>>a1>>e1>>r1;
		e.a=a1;
		e.e=e1;
		e.r=r1;
		vs.push_back(e);
		sum+=e.a;
	}
	if(sum==0){
		cout<<"NA"<<endl;
		continue;
	}
	sort(vs.begin(),vs.end());
	int ans=1;
	bool ok=false;
	queue<E2> qu;
	E2 e2;
	e2.no=0;
	e2.dmg=0;
	e2.exp=0;
	qu.push(e2);
	int c1=0;

	
	while(qu.size()>0&&c1<202){
		c1++;
		set<E2> ss2;
		while(qu.size()>0){
			E2 e2=qu.front();
			qu.pop();
			//cout<<e2.exp<<" d2="<<e2.dmg<<" "<<e2.no<<" "<<dp[e2.no][e2.dmg]<<endl;
			if(dp[e2.no][e2.dmg]>e2.exp)continue;
			for(int j=e2.no;j<n;j++){
				E e=vs[j];
				//cout<<e.e<<" a="<<e.a<<" "<<e.r<<endl;
				if(e.r>e2.exp)break;
				if(e.a+e.e==0)continue;
				int dmg2=e.a+e2.dmg;
				int exp2=e.e+e2.exp;
				if(dmg2>=d){
					ok=true;
					break;
				}else{
					E2 e3;
					e3.no=j;
					e3.dmg=dmg2;
					e3.exp=min(101,exp2);
					//cout<<e3.exp<<" d="<<e3.dmg<<" "<<e3.no<<endl;
					if(dp[e3.no][e3.dmg]<e3.exp){
						if(ss2.find(e3)==ss2.end()){
							ss2.insert(e3);
						}else{
							auto it=ss2.find(e3);
							E2 e4=(*it);
							if(e4.exp<e3.exp){
								ss2.erase(it);
								ss2.insert(e3);
							}
						}
					}
				}
			}
		}
		if(ok==true){
			break;
		}

		set<E2> dells;
		for(auto it=ss2.begin();it!=ss2.end();it++){
			E2 e1=(*it);
			for(auto it2=it;it2!=ss2.end();it2++){
				E2 e2=(*it2);
				if(e1.no==e2.no){
					if(e1.exp<e2.exp&&e1.dmg<e2.dmg){
						dells.insert(e1);
						break;
					}else if(e1.exp==e2.exp&&e1.dmg<e2.dmg){
						dells.insert(e1);
						break;
					}else if(e1.exp<e2.exp&&e1.dmg==e2.dmg){
						dells.insert(e1);
						break;
					}
				}else{
					 break;
				}
			}
		}
		for(auto it=dells.begin();it!=dells.end();it++){
			E2 e3=(*it);
			ss2.erase(e3);
		}
		for(auto it=ss2.begin();it!=ss2.end();it++){
			E2 e3=(*it);
			qu.push(e3);
			if(dp[e3.no][e3.dmg]<e3.exp){
				dp[e3.no][e3.dmg]=e3.exp;
			}
		}
		//cout<<ss2.size()<<endl;
		
		ans++;
	}
	
	
	if(ok==true){
		cout<<ans<<endl;
	}else{
		cout<<"NA"<<endl;
	}
}
return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 3744
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include <vector>
#include<algorithm>
#include <queue>

using namespace std;

class Item
{
public:
	int a, e, r;
	Item() { a = -1; e = -1; r = -1; }
	Item(int a, int e, int r)
	{
		this->a = a;
		this->e = e;
		this->r = r;
	}
};

int dp[101][101];
#define nil 99999

int DP(vector<Item> items, int D, int N)
{
	for(int i = 0 ; i <= 100; i++)
	    for (int j = 0; j <= 100; j++)
		    dp[i][j] = nil;
	dp[D][0] = 0;
	for (int i = D; i >= 0; i--)
	{
		for (int j = 0; j <= 100; j++)
		{
			for (int k = 0; k < N; k++)
			{
				if (j >= items[k].r)
				{
					int _exp = min(j + items[k].e, 100);
					int _d = max(i - items[k].a, 0);
					dp[_d][_exp] =
						min(dp[_d][_exp], dp[i][j] + 1);
				}
			}
		}
	}
	int minV = dp[0][100];
	for (int j = 0; j < 100; j++)
		minV = min(minV, dp[0][j]);
	return minV;
}
int main()
{
	int D, N;
	cin >> D >> N;
	while (D)
	{
		vector<Item> items;
		bool flag1 = false,flag2 = false;
		for (int i = 0; i < N; i++)
		{
			int a, e, r;
			cin >> a >> e >> r;
			if (a == 0 && e == 0) continue;
			if (r == 0)
				flag1 = true;
			if (a > 0)
				flag2 = true;
			items.push_back(Item(a, e, r));
		}
		if (flag1 && flag2)
			cout << DP(items, D, items.size()) << endl;
		else
			cout << "NA" << endl;
		cin >> D >> N;
	}
	cin >> D;
}
accuracy: 1
time_ms: 40
memory_kb: 3136
final_rank: 2

code_snippet: #include<bits/stdc++.h>

using namespace std;

int D, N, Ans;
int Endurance[100], getExperience[100], needExperience[100];
int DpCount[101][101];

const int INF = 2e9;

void solve() {
	int minLife, maxExperience;

	//fill_n(*DpCount, 101 * 10001, INF);
	for (int i = 0; i < 101; ++i) {
		fill(DpCount[i], DpCount[i] + 101, INF);
	}
	DpCount[D][0] = 0;

	for (int currentLife = D; currentLife > 0; --currentLife) {
		for (int currentExperience = 0; currentExperience < 101; ++currentExperience) {
			if (DpCount[currentLife][currentExperience] == INF) {
				continue;
			}
			for (int i = 0; i < N; ++i) {
				if (currentExperience < needExperience[i]) {
					continue;
				}
				minLife = max(0, currentLife - Endurance[i]);
				maxExperience = min(100, currentExperience + getExperience[i]);
				DpCount[minLife][maxExperience] = min(DpCount[minLife][maxExperience],
					DpCount[currentLife][currentExperience] + 1);
			}
		}
	}
		
	Ans = *min_element(DpCount[0], DpCount[0] + 101);
}

int main() {

	while (cin >> D >> N, D) {
		memset(Endurance, 0, sizeof(Endurance));
		memset(getExperience, 0, sizeof(getExperience));
		memset(needExperience, 0, sizeof(needExperience));
		for (int i = 0; i < N; ++i) {
			cin >> Endurance[i] >> getExperience[i] >> needExperience[i];
		}
		solve();
		if (Ans == INF) {
			cout << "NA" << endl;
			continue;
		}
		cout << Ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3132
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int d, n;
    while(cin >> d >> n, d){
        [&](){
            vector<int> as(n), es(n), rs(n);
            for (int i = 0; i < n; ++i) {
                cin >> as[i] >> es[i] >> rs[i];
            }
            auto dp = make_v(d+1, 101, -1);
            dp[d][0] = 0;
            queue<pair<int, int>> Q;
            Q.emplace(d, 0);
            while(!Q.empty()){
                int i, j; tie(i, j) = Q.front(); Q.pop();
                if(i == 0) {
                    cout << dp[i][j] << "\n";
                    return;
                }
                for (int k = 0; k < n; ++k) {
                    if(rs[k] <= j){
                        int ii = max(0, i-as[k]), jj = min(100, j+es[k]);
                        if(!~dp[ii][jj]){
                            dp[ii][jj] = dp[i][j]+1;
                            Q.emplace(ii, jj);
                        }
                    }
                }
            }
            puts("NA");
        }();
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3216
final_rank: 3

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<unordered_set> 
#include<cmath>
using namespace std;
long long p = 998244353;
//long long p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<int,pin>
#define V vector
#define Endl endl
#define veb vector<bool>
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) lower_bound(h.begin(),h.end(),val)
#define upper(h,val) upper_bound(h.begin(),h.end(),val)
#define mint modint<998244353>
#define end_s(a) cout <<a<<endl;return 0
vel pa;
int root(int x) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x]); pa[x] = ans;
	return ans;
}
void marge(int x, int y) {
	x = root(x);
	y = root(y);
	if (x != y) { pa[x] = y; }
}
V<string> solve2(int a, int b) {
	if (a < 0 || b < 0) { return {}; }
	if (a == 0 && b == 0) {
		V<string> ans(1, "");
		return ans;
	}
	V<string> ans = solve2(a - 1, b);
	V<string> nans;
	for (auto x : ans) { nans.push_back(x+"A"); }
	V<string> ans2 = solve2(a, b - 1);
	for (auto x : ans2) { nans.push_back(x+"B"); }
	return nans;
}
signed main() {
	while (true) {
		int d, n; cin >> d >> n;
		if (d == 0) { break; }
		int inf = 100;
		vvel dp(d + 1, vel(101, 10000));
		vel a(n), e(n), r(n);
		rep(i, n) {
			cin >> a[i] >> e[i] >> r[i];
		}
		dp[0][0] = 0;
		rep(i, d + 1) {
			rep(j, 101) {
				rep(k, n) {
					if (r[k] <= j) {
						mmin(dp[min(i + a[k], d)][min(j + e[k], inf)], dp[i][j] + 1);
					}
				}
			}
		}
		int ans = 10000;
		rep(i, 101) { mmin(ans, dp[d][i]); }
		if (ans == 10000) { cout << "NA" << endl; }
		else { cout << ans << endl; }
	}
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3204
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<unordered_set> 
#include<cmath>
using namespace std;
long long p = 998244353;
//long long p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<int,pin>
#define V vector
#define Endl endl
#define veb vector<bool>
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) lower_bound(h.begin(),h.end(),val)
#define upper(h,val) upper_bound(h.begin(),h.end(),val)
#define mint modint<998244353>
#define end_s(a) cout <<a<<endl;return 0
vel pa;
int root(int x) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x]); pa[x] = ans;
	return ans;
}
void marge(int x, int y) {
	x = root(x);
	y = root(y);
	if (x != y) { pa[x] = y; }
}
V<string> solve2(int a, int b) {
	if (a < 0 || b < 0) { return {}; }
	if (a == 0 && b == 0) {
		V<string> ans(1, "");
		return ans;
	}
	V<string> ans = solve2(a - 1, b);
	V<string> nans;
	for (auto x : ans) { nans.push_back(x+"A"); }
	V<string> ans2 = solve2(a, b - 1);
	for (auto x : ans2) { nans.push_back(x+"B"); }
	return nans;
}
signed main() {
	while (true) {
		int d, n; cin >> d >> n;
		if (d == 0) { break; }
		int inf = 100;
		vvel dp(d + 1, vel(101, 101));
		vel a(n), e(n), r(n);
		rep(i, n) {
			cin >> a[i] >> e[i] >> r[i];
		}
		dp[0][0] = 0;
		rep(i, d + 1) {
			rep(j, 101) {
				rep(k, n) {
					if (r[k] <= j) {
						mmin(dp[min(i + a[k], d)][min(j + e[k], inf)], dp[i][j] + 1);
					}
				}
			}
		}
		int ans = 101;
		rep(i, 101) { mmin(ans, dp[d][i]); }
		if (ans == 101) { cout << "NA" << endl; }
		else { cout << ans << endl; }
	}
	return 0;
}
accuracy: 0.9
time_ms: 70
memory_kb: 3212
final_rank: 16