alexjercan/bugnet huggingface dataset:
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>

int D, N;
int dp[101][101];
int A[100], E[100], R[100];

int main() {
  while (scanf("%d %d", &D, &N), D || N) {
    for (int i = 0; i < N; i++) {
      scanf("%d %d %d", A + i, E + i, R + i);
    }

    for (int i = 0; i <= D; i++) {
      for (int j = 0; j <= 100; j++) {
        dp[i][j] = 1001001001;
      }
    }

    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 ni = std::max(i - A[k], 0), nj = std::min(j + E[k], 100);
            dp[ni][nj] = std::min(dp[ni][nj], dp[i][j] + 1);
          }
        }
      }
    }

    int res = 1001001001;
    for (int i = 0; i <= 100; i++) {
      res = std::min(res, dp[0][i]);
    }

    if (res != 1001001001) {
      printf("%d\\n", res);
    } else {
      puts("NA");
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

int D, N;
int dp[101][101];
int A[100], E[100], R[100];

int rec(int health, int exp) {
  // printf("%d, %d\\n",health,exp);

  if (health <= 0) {
    return 0;
  }
  if (dp[health][exp] != -1) {
    return dp[health][exp];
  }

  int res = 1001001001;
  for (int i = 0; i < N; i++) {
    if (exp == 100 && A[i] == 0) {
      continue;
    }

    if (exp >= R[i]) {
      res = std::min(res, rec(health - A[i], std::min(exp + E[i], 100)) + 1);
    }
  }

  return dp[health][exp] = res;
}

int main() {
  while (scanf("%d %d", &D, &N), D || N) {
    memset(dp, -1, sizeof(dp));

    for (int i = 0; i < N; i++) {
      scanf("%d %d %d", A + i, E + i, R + i);
    }

    bool f = false, g = false;
    for (int i = 0; i < N; i++) {
      if (R[i] == 0) {
        if (A[i] > 0) {
          f = true;
          g = true;
        } else if (E[i] > 0) {
          f = true;
        }
      }
      if (A[i] > 0) {
        g = true;
      }
    }

    if (f && g) {
      printf("%d\\n", rec(D, 0));
    } else {
      puts("NA");
    }
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int d, n;
int a[100], e[100], r[100];

int ans;
int dp[101][101];

int solve(int hp, int exp) {

  if (hp <= 0) {
    return 0;
  }

  if (dp[hp][exp] != -1) {
    return dp[hp][exp];
  }

  int ret = INF;
  for (int i = 0; i < n; ++i) {
    if (exp >= r[i]) {
      if (a[i] == 0 && (exp >= 100 || e[i] == 0))
        continue;
      ret = min(ret, solve(max(0, hp - a[i]), min(100, exp + e[i])) + 1);
    }
  }

  return dp[hp][exp] = ret;
}

int main() {

  while (cin >> d >> n, d || n) {

    for (int i = 0; i < n; ++i) {
      cin >> a[i] >> e[i] >> r[i];
    }

    memset(dp, -1, sizeof(dp));

    ans = solve(d, 0);

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int d, n;
int a[100], e[100], r[100];

int ans;
int dp[101][101];

int solve(int hp, int exp) {

  if (hp <= 0) {
    return 0;
  }

  if (dp[hp][exp] != -1) {
    return dp[hp][exp];
  }

  int ret = INF;
  for (int i = 0; i < n; ++i) {
    if (exp >= r[i]) {
      ret = min(ret, solve(max(0, hp - a[i]), min(100, exp + e[i])) + 1);
    }
  }

  return dp[hp][exp] = ret;
}

int main() {

  while (cin >> d >> n, d || n) {

    int cnt = 0;

    for (int i = 0; i < n; ++i) {
      int ai, ei, ri;
      cin >> ai >> ei >> ri;
      if (ai || ei) {
        a[cnt] = ai;
        e[cnt] = ei;
        r[cnt] = ri;
        ++cnt;
      }
    }

    n = cnt;

    memset(dp, -1, sizeof(dp));

    ans = solve(d, 0);

    if (ans == INF) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int D, N, a[111], e[111], r[111], dp[111][111];
int main() {
  while (cin >> D >> N, D | N) {
    for (int i = 0; i < N; i++)
      cin >> a[i] >> e[i] >> r[i];
    int maxr = *max_element(r, r + N);
    for (int i = 0; i <= D; i++)
      fill(dp[i], dp[i] + maxr + 1, 999999999);
    dp[0][0] = 0;
    for (int i = 0; i < D; i++) {
      for (int j = 0; j <= maxr; j++) {
        for (int k = 0; k < N; k++) {
          if (j < r[k])
            continue;
          int p = min(j + e[k], maxr), q = min(i + a[k], D);
          dp[q][p] = min(dp[q][p], dp[i][j] + 1);
        }
      }
    }
    int ret = *min_element(dp[D], dp[D] + maxr + 1);
    if (ret == 999999999)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int D, N, w, a[111], e[111], r[111];
int dp[111][111][222];
int rec(int f1, int f2, int d) {
  if (f1 >= D)
    return d;
  if (dp[f1][f2][d])
    return dp[f1][f2][d] - 1;
  int ret = 255;
  for (int i = 0; i < N; i++) {
    if (!a[i] && (!e[i] || f2 == w))
      continue;
    if (f2 >= r[i])
      ret = min(ret, rec(f1 + a[i], min(f2 + e[i], w), d + 1));
  }
  dp[f1][f2][d] = ret + 1;
  return ret;
}
int main() {
  while (cin >> D >> N, D | N) {
    for (int i = 0; i < N; i++)
      cin >> a[i] >> e[i] >> r[i];
    w = *max_element(r, r + N);
    for (int i = 0; i < D; i++) {
      for (int j = 0; j <= w; j++)
        fill(dp[i][j], dp[i][j] + N * 2, 0);
    }
    int ret = rec(0, 0, 0);
    if (ret == 255)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int D, N;
int memo[256][256];
int a[256], e[256], r[256];
int func(int ex, int HP) {
  if (HP <= 0) {
    return 0;
  }
  if (memo[ex][HP] != -1)
    return memo[ex][HP];
  int res = INT_MAX / 2;
  for (int i = 0; i < N; i++) {
    if (ex >= r[i]) {
      if (a[i] == 0 && e[i] == 0)
        continue;
      if (a[i] == 0 && ex == 110)
        continue;
      res = min(res, func(min(110, ex + e[i]), HP - a[i]) + 1);
    }
  }
  memo[ex][HP] = res;
  return res;
}

int main(void) {
  while (cin >> D >> N, D | N) {
    for (int i = 0; i < 256; i++) {
      for (int j = 0; j < 256; j++) {
        memo[i][j] = -1;
      }
    }
    for (int i = 0; i < N; i++) {
      cin >> a[i] >> e[i] >> r[i];
    }
    int ans = func(0, D);
    if (ans == INT_MAX / 2) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int D, N;
int memo[256][256];
int a[256], e[256], r[256];
int func(int ex, int HP) {
  if (HP <= 0) {
    return 0;
  }
  if (memo[ex][HP] != -1)
    return memo[ex][HP];
  int res = INT_MAX / 2;
  for (int i = 0; i < N; i++) {
    if (ex >= r[i]) {
      if (a[i] <= 0 && e[i] == 0)
        continue;
      res = min(res, func(min(100, ex + e[i]), HP - a[i]) + 1);
    }
  }
  memo[ex][HP] = res;
  return res;
}

int main(void) {
  while (cin >> D >> N, D | N) {
    for (int i = 0; i < 256; i++) {
      for (int j = 0; j < 256; j++) {
        memo[i][j] = -1;
      }
    }
    for (int i = 0; i < N; i++) {
      cin >> a[i] >> e[i] >> r[i];
    }
    int ans = func(0, D);
    if (ans == INT_MAX / 2) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

// 道具を使う順番もあまり関係なかったりする。でも素直に耐久力と経験値に注目しちゃえば良い。
// stack使う＆遷移後枝刈り→TLE(1ケース1秒)、遷移前枝刈りにする→TLE(1ケース0.2秒くらい？)、
// stackっぽいものを配列で自作→？？
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int d, n;
int a[100], e[100], r[100];

signed main() {
  while (cin >> d >> n) {
    if (!d)
      break;
    int i, j;
    for (i = 0; i < n; i++)
      cin >> a[i] >> e[i] >> r[i];

    int dp[101][101]; // dp[残り耐久力][min(EXP,100)]
    for (i = 0; i < 101; i++) {
      for (j = 0; j < 101; j++) {
        dp[i][j] = 1000000;
      }
    }
    int data[10000], dnum = 0;
    data[dnum++] = ((0) << (20)) + ((d) << (10)) + ((0) << (0));

    while (dnum > 0) {
      int Cost = (data[dnum - 1] >> (20));
      int Dif = (data[dnum - 1] >> (10)) % (1 << (10));
      int Exp = (data[dnum - 1]) % (1 << (10));

      dnum--;
      dp[Dif][Exp] = Cost;

      for (i = 0; i < n; i++) {
        if (Exp >= r[i]) {
          // 殴る
          int nCost = Cost + 1;
          int nDif = max(0, Dif - a[i]);
          int nExp = min(100, Exp + e[i]);
          // 枝刈り
          if (dp[nDif][nExp] <= nCost)
            continue;
          // 遷移する
          data[dnum++] = (nCost << (20)) + (nDif << (10)) + (nExp << (0));
        }
      }
    }

    int ans = 100000;
    for (i = 0; i < 101; i++) {
      if (dp[0][i] < 10000) {
        ans = min(dp[0][i], ans);
      }
    }
    if (ans < 10000)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 道具を使う順番もあまり関係なかったりする。でも素直に耐久力と経験値に注目しちゃえば良い。
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

int d, n;
int a[100], e[100], r[100];

int main() {
  while (cin >> d >> n) {
    if (!d)
      break;
    int i, j;
    for (i = 0; i < n; i++)
      cin >> a[i] >> e[i] >> r[i];

    int dp[101][101]; // dp[残り耐久力][min(EXP,100)]
    for (i = 0; i < 101; i++) {
      for (j = 0; j < 101; j++) {
        dp[i][j] = 1000000;
      }
    }
    stack<PIII> data;
    data.push(PIII(0, PII(d, 0)));
    while (!data.empty()) {
      int Cost = data.top().first;
      int Dif = data.top().second.first;
      int Exp = data.top().second.second;
      data.pop();
      dp[Dif][Exp] = Cost;

      for (i = 0; i < n; i++) {
        if (Exp >= r[i]) {
          // 殴る
          int nCost = Cost + 1;
          int nDif = max(0, Dif - a[i]);
          int nExp = min(100, Exp + e[i]);
          // 枝刈り
          if (dp[nDif][nExp] <= nCost)
            continue;
          // 遷移する
          data.push(PIII(nCost, PII(nDif, nExp)));
        }
      }
    }

    int ans = 100000;
    for (i = 0; i < 101; i++) {
      if (dp[0][i] < 10000) {
        ans = min(dp[0][i], ans);
      }
    }
    if (ans < 10000)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1e9;
int d, n;
int a[110], e[110], r[110];
int memo[110][110];

int rec(int hp, int exp) {
  if (hp == 0)
    return 0;
  if (memo[hp][exp] >= 0)
    return memo[hp][exp];

  int res = INF;
  for (int i = 0; i < n; i++) {
    if (exp >= r[i]) {
      int nhp = max(0, hp - a[i]), nexp = min(100, exp + e[i]);
      if (hp == nhp && exp == nexp)
        continue;
      res = min(res, rec(nhp, nexp) + 1);
    }
  }
  return memo[hp][exp] = res;
}

int main() {
  while (cin >> d >> n, d) {
    for (int i = 0; i < n; i++)
      cin >> a[i] >> e[i] >> r[i];

    memset(memo, -1, sizeof(memo));
    int ans = rec(d, 0);
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

const int INF = 1e9;
int d, n;
int a[110], e[110], r[110];
int memo[110][110];

int rec(int hp, int exp) {
  if (hp < 0 || exp > 100)
    return INF;
  if (hp == 0)
    return 0;
  if (memo[hp][exp] >= 0)
    return memo[hp][exp];

  int res = INF;
  for (int i = 0; i < n; i++) {
    if (exp >= r[i]) {
      res = min(res, rec(max(0, hp - a[i]), min(100, exp + e[i])) + 1);
    }
  }
  return memo[hp][exp] = res;
}

int main() {
  while (cin >> d >> n, d) {
    for (int i = 0; i < n; i++)
      cin >> a[i] >> e[i] >> r[i];

    memset(memo, -1, sizeof(memo));
    int ans = rec(d, 0);
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int dp[100][101]; //??¨?????????,?????????????¨???°(100??§????????????)

int n;
int a[100], e[100], r[100]; //??????????¨??????¶???

int solve(int dNow, int eNow) {
  if (dNow < 0)
    return 0; //?????????????????????
  if (dp[dNow][eNow] != -1)
    return dp[dNow][eNow];

  int ans = 114514;
  bool able = false;
  for (int i = 0; i < n; i++) {
    if (eNow < r[i])
      continue;
    if (a[i] == 0 && (100 <= eNow || e[i] == 0))
      continue;
    able = true;
    ans = min(ans, 1 + solve(dNow - a[i], min(eNow + e[i], 100)));
  }

  if (!able) {
    return -1;
  }

  dp[dNow][eNow] = ans;
  return ans;
}

int main() {
  int d; //??¨????????????
  int i, j;
  while (cin >> d, d > 0) {
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> a[i] >> e[i] >> r[i];
    }

    for (i = 0; i < 100; i++) {
      for (j = 0; j < 101; j++) {
        dp[i][j] = -1;
      }
    }
    int ans = solve(d - 1, 0);
    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int dp[100][101]; //??¨?????????,?????????????¨???°(100??§????????????)

int n;
int a[100], e[100], r[100]; //??????????¨??????¶???

int solve(int dNow, int eNow) {
  if (dNow < 0)
    return 0; //?????????????????????
  if (dp[dNow][eNow] != -1)
    return dp[dNow][eNow];

  int ans = 114514;
  for (int i = 0; i < n; i++) {
    if (eNow < r[i])
      continue;
    ans = min(ans, 1 + solve(dNow - a[i], min(eNow + e[i], 100)));
  }

  dp[dNow][eNow] = ans;
  return ans;
}

int main() {
  int d; //??¨????????????
  int i, j;
  bool able;
  while (cin >> d, d > 0) {
    cin >> n;
    able = false;
    for (i = 0; i < n; i++) {
      cin >> a[i] >> e[i] >> r[i];
      if (r[i] == 0)
        able = true;
    }

    if (!able) {
      cout << "NA" << endl;
      continue;
    }

    for (i = 0; i < 100; i++) {
      for (j = 0; j < 101; j++) {
        dp[i][j] = -1;
      }
    }
    cout << solve(d - 1, 0) << endl;
  }
  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 100000000
using namespace std;

int n;
int a[101], e[101], r[101];
int dp[101][20001];
int ans, cnt;
int maxr;
void saiki(int d, int ex, int kai) {
  cnt++;
  if (d <= 0)
    dp[0][ex] = 0;
  if (d > 0) {
    for (int i = 0; i < n; i++) {
      if (r[i] <= ex) {
        int da = d - a[i];
        if (da < 0)
          da = 0;
        if (maxr <= ex && a[i] == 0)
          continue;
        if (a[i] == 0 && e[i] == 0)
          continue;
        if (dp[da][ex + e[i]] < INF) {
          dp[d][ex] = min(dp[d][ex], dp[da][ex + e[i]] + 1);
          continue;
        }
        saiki(da, ex + e[i], kai + 1);
        dp[d][ex] = min(dp[d][ex], dp[da][ex + e[i]] + 1);
      }
    }
  }
}

int main() {

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

    maxr = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> e[i] >> r[i];
      maxr = max(maxr, r[i]);
    }

    for (int i = 0; i <= d; i++)
      for (int j = 0; j <= 20000; j++)
        dp[i][j] = INF;

    saiki(d, 0, 0);

    if (dp[d][0] == INF)
      cout << "NA" << endl;
    else
      cout << dp[d][0] << endl;
    // cout << cnt <<endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 100000000
using namespace std;

int n;
int a[101], e[101], r[101];
int dp[101][20001];
int ans, cnt;
int maxr;
void saiki(int d, int ex, int kai) {
  cnt++;
  if (d <= 0)
    dp[0][ex] = 0;
  if (d > 0) {
    for (int i = 0; i < n; i++) {
      if (r[i] <= ex) {
        int da = d - a[i];
        if (da < 0)
          da = 0;
        if (maxr <= ex && a[i] == 0)
          continue;
        if (dp[da][ex + e[i]] < INF) {
          dp[d][ex] = min(dp[d][ex], dp[da][ex + e[i]] + 1);
          continue;
        }
        saiki(da, ex + e[i], kai + 1);
        dp[d][ex] = min(dp[d][ex], dp[da][ex + e[i]] + 1);
      }
    }
  }
}

int main() {

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

    maxr = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> e[i] >> r[i];
      maxr = max(maxr, r[i]);
    }

    for (int i = 0; i <= d; i++)
      for (int j = 0; j <= 20000; j++)
        dp[i][j] = INF;

    saiki(d, 0, 0);

    if (dp[d][0] == INF)
      cout << "NA" << endl;
    else
      cout << dp[d][0] << endl;
    // cout << cnt <<endl;
  }

  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <ctime>
#include <iostream>
#define INF 100000000
using namespace std;

int n;
int a[101], e[101], r[101];
int dp[101][20001];

int main() {

  while (1) {
    int d;
    cin >> d >> n;
    if (d == 0 && n == 0)
      break;
    int maxr, maxe, mina, flg;
    maxr = maxe = flg = 0;
    maxr = -1;
    mina = INF;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> e[i] >> r[i];
    }

    int N = 101;
    int ans = INF;

    for (int i = 0; i <= d; i++)
      for (int j = 0; j <= N; j++)
        dp[i][j] = INF;
    dp[d][0] = 0;

    int cnt = 0;
    int c = 0;
    for (int j = 0; j <= N; j++) { //?????¨???????¨????
      cnt++;
      for (int i = d; i >= 0; i--) { //?????????????????????
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < n; k++) {
          if (j < r[k])
            continue;
          c++;
          int na = i - a[k];
          int ne = j + e[k];
          if (na < 0)
            na = 0;
          if (ne > N)
            ne = N;
          dp[na][ne] = min(dp[i][j] + 1, dp[na][ne]);
        }
      }
      ans = min(ans, dp[0][j]);
    }

    /*
  for(int j=0;j<=cnt;j++) { //?????¨???????¨????
    cout << j <<"|";
    for(int i=d;i>=0;i--) {//?????????????????????
      if(dp[i][j] == INF) cout <<"#"<<" ";
      else  cout << dp[i][j]<<" ";
    }
    cout << endl;
  }
    */

    //      cout <<"time="<< clock() <<endl;
    // cout << c<<endl;
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 100000000
using namespace std;

int n;
int a[101], e[101], r[101];
int dp[101][20001];

int main() {

  while (1) {
    int d;
    cin >> d >> n;
    if (d == 0 && n == 0)
      break;
    int maxr, maxe, maxa, flg;
    maxr = maxe = maxa = flg = 0;
    maxr = -1;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> e[i] >> r[i];
    }

    int N = 10001;
    int ans = INF;

    for (int i = 0; i <= d; i++)
      for (int j = 0; j <= N; j++)
        dp[i][j] = INF;
    dp[d][0] = 0;

    int cnt = 0;
    for (int j = 0; j <= N; j++) { //?????¨???????¨????
      cnt++;
      for (int i = d; i >= 0; i--) { //?????????????????????
        for (int k = 0; k < n; k++) {
          int na = i - a[k];
          int ne = j + e[k];
          if (na < 0)
            na = 0;
          if (j < r[k])
            continue;
          dp[na][ne] = min(dp[i][j] + 1, dp[na][ne]);
        }
      }
      ans = min(ans, dp[0][j]);
    }

    /*
  for(int j=0;j<=cnt;j++) { //?????¨???????¨????
    cout << j <<"|";
    for(int i=d;i>=0;i--) {//?????????????????????
      if(dp[i][j] == INF) cout <<"#"<<" ";
      else  cout << dp[i][j]<<" ";
    }
    cout << endl;
  }
    */

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

  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 1 << 29;
int a[110], e[110], r[110];
int d, n;
// dp[?????????][????¨????] = ????°?????????°
int dp[110][110];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> d >> n && (d || n)) {

    for (int i = 0; i < n; ++i) {
      cin >> a[i] >> e[i] >> r[i];
    }

    for (int i = 0; i < 110; ++i) {
      for (int j = 0; j < 110; ++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) {
          if (j >= r[k]) {
            int nd = min(i + a[k], d);
            int ne = min(j + e[k], 100);
            dp[nd][ne] = min(dp[nd][ne], dp[i][j] + 1);
          }
        }
      }
    }

    int ans = INF;
    for (int j = 0; j <= 100; ++j) {
      ans = min(ans, dp[d][j]);
    }
    if (ans != INF)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

/*
// ?¨??????????,??????????????°???????¨????
int func(int hp, int m, int k){
        if(hp <= 0) return m;
        int res = INF;
        for(int i = 0; i < n; ++i){
                if(k >= r[i]){
                        res = min(res, func(hp - a[i], m + 1, k + e[i]));
                }
        }
        if(res == INF){
                cout << "NA" << endl;
                return 0;
        }
        if(m == 0){
                cout << res << endl;
        }
        return res;
}

int main(){
        cin.tie(0);
        ios::sync_with_stdio(false);

        while(cin >> d >> n && (d || n)){

                for(int i = 0; i < n; ++i){
                        cin >> a[i] >> e[i] >> r[i];
                }

                func(d, 0, 0);
        }

        return 0;
}
*/
/*
a ??????????????????
e ??????????¨????
r ??????????¨????
*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 1 << 29;
int a[110], e[110], r[110];
int d, n;

// ?¨??????????,??????????????°???????¨????
int func(int hp, int m, int k) {
  if (hp <= 0)
    return m;
  int res = INF;
  for (int i = 0; i < n; ++i) {
    if (k >= r[i]) {
      res = min(res, func(hp - a[i], m + 1, k + e[i]));
    }
  }
  if (res == INF) {
    cout << "NA" << endl;
    return INF;
  }
  if (m == 0) {
    cout << res << endl;
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> d >> n && (d || n)) {

    for (int i = 0; i < n; ++i) {
      cin >> a[i] >> e[i] >> r[i];
    }

    func(d, 0, 0);
  }

  return 0;
}

/*
a ??????????????????
e ??????????¨????
r ??????????¨????
*/
Problem p00284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

int main() {
  int T;
  cin >> T;
  while (T--) {
    ll src, dst;
    cin >> src >> dst;
    int use = 0;
    if (src & 1LL)
      ++use, ++src;
    if (dst & 1LL)
      ++use, --dst;
    bool found = false;
    while (src != dst) {
      if (src == 0) {
        found = true;
        cout << use + __builtin_popcount(dst - src) << endl;
        break;
      }
      for (int shift = 31; shift >= 0; shift--) {
        ll coef = (1LL << shift);
        if (llabs(src) >= coef && src % coef == 0LL) {
          bool used = false;
          if (src + coef <= dst) {
            ++use;
            src += coef;
            break;
          }
        }
      }
    }
    if (!found)
      cout << use << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

int main() {
  int T;
  cin >> T;
  while (T--) {
    ll src, dst;
    cin >> src >> dst;
    int use = 0;
    if (src & 1LL)
      ++use, ++src;
    if (dst & 1LL)
      ++use, --dst;
    bool found = false;
    while (src != dst) {
      if (src == 0) {
        found = true;
        cout << use + __builtin_popcount(dst - src) << endl;
        break;
      }
      for (int shift = 31; shift >= 0; shift--) {
        ll coef = (1LL << shift);
        if (llabs(src) >= coef && src % coef == 0LL) {
          if (src + coef <= dst) {
            ++use;
            src += coef;
          }
          break;
        }
      }
    }
    if (!found)
      cout << use << endl;
  }
  return 0;
}