alexjercan/bugnet huggingface dataset:
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int min(int a, int b) { return a < b ? a : b; }

int main(void) {
  int n, d, i, j, k, inf = 10e8, l;
  while (1) {
    scanf("%d%d", &d, &n);
    if (!d)
      break;
    int a[n], e[n], r[n], dp[d + 1][200];
    for (i = 0; i < n; ++i)
      scanf("%d%d%d", &a[i], &e[i], &r[i]);
    for (i = 0; i <= d; ++i)
      for (j = 0; j < 200; ++j)
        dp[i][j] = inf;
    dp[0][0] = 0;
    for (i = 0; i <= 100; ++i) { // 経験値
      for (j = 0; j < n; ++j)
        if (i >= r[j]) { // 道具
          for (k = 0; k <= d; ++k) {
            if (dp[min(d, k + a[j])][min(100, i + e[j])] > dp[k][i] + 1)
              dp[min(d, k + a[j])][min(100, i + e[j])] = dp[k][i] + 1;
          }
        }
    }
    /*for(i = 0; i <= d; ++i) {
      for(j = 0; j < 20; ++j) printf("%d ", dp[i][j]);
      printf("\\n");
    }*/
    int ans = inf;
    for (i = 0; i <= 100; ++i)
      if (ans > dp[d][i])
        ans = dp[d][i];
    if (ans != inf)
      printf("%d\\n", ans);
    else
      printf("NA\\n");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

int min(int a, int b) { return a < b ? a : b; }

int main(void) {
  int n, d, i, j, k, inf = 10e8, l;
  while (1) {
    scanf("%d%d", &d, &n);
    if (!d)
      break;
    int a[n], e[n], r[n], dp[d + 1][200];
    for (i = 0; i < n; ++i)
      scanf("%d%d%d", &a[i], &e[i], &r[i]);
    for (i = 0; i <= d; ++i)
      for (j = 0; j < 200; ++j)
        dp[i][j] = inf;
    dp[0][0] = 0;
    for (i = 0; i <= 100; ++i) { // 経験値
      for (j = 0; j < n; ++j)
        if (i >= r[j]) { // 道具
          for (k = 0; k <= d; ++k) {
            for (l = 0; l <= i + e[j] && l <= 100; ++l) {
              if (dp[min(d, k + a[j])][l] > dp[k][i] + 1)
                dp[min(d, k + a[j])][l] = dp[k][i] + 1;
            }
          }
        }
    }
    /*for(i = 0; i <= d; ++i) {
      for(j = 0; j < 20; ++j) printf("%d ", dp[i][j]);
      printf("\\n");
    }*/
    int ans = inf;
    for (i = 0; i <= 100; ++i)
      if (ans > dp[d][i])
        ans = dp[d][i];
    if (ans != inf)
      printf("%d\\n", ans);
    else
      printf("NA\\n");
  }
  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1000000;

struct Tool {
  int a, e, r;
};
// bool operator<(const Tool& lhs, const Tool& rhs) {return lhs.r < rhs.r;}
bool operator<(const Tool &lhs, const Tool &rhs) {
  if (lhs.r != rhs.r)
    return lhs.r < rhs.r;
  return lhs.e < rhs.e;
}

int solve(int D, vector<Tool> tool) {
  sort(begin(tool), end(tool));
  vector<vector<int>> dp(D + 1, vector<int>(101, INF));
  dp[D][0] = 0;
  for (const auto &t : tool) {
    for (int hp = D; 0 <= hp; --hp) {
      for (int exp = 0; exp <= 100; ++exp) {
        if (exp < t.r)
          continue;
        int next_hp = max(0, hp - t.a);
        int next_exp = min(100, exp + t.e);
        dp[next_hp][next_exp] = min(dp[next_hp][next_exp], dp[hp][exp] + 1);
      }
    }
  }

  int result = INF;
  for (const auto &i : dp[0])
    result = min(result, i);
  return result;
}

int main() {
  int D, N;
  while (cin >> D >> N, D | N) {
    vector<Tool> tool(N);
    for (auto &t : tool)
      cin >> t.a >> t.e >> t.r;

    int answer = solve(D, tool);
    if (answer == INF)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

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

const int INF = 1000000;
struct Tool {
  int a, e, r;
};

vector<vector<int>> memo;

int dfs(int hp, int exp, int depth, const vector<Tool> &tool) {
  if (200 < depth)
    return INF;
  if (hp <= 0)
    return depth;
  if (memo[hp][exp] != -1)
    if (memo[hp][exp] <= depth)
      return memo[hp][exp];
  int result = INF;
  for (const auto &t : tool) {
    if (exp < t.r)
      continue;
    result = min(result, dfs(hp - t.a, exp + t.e, depth + 1, tool));
  }
  return memo[hp][exp] = result;
}

int main() {
  int D, N;
  while (cin >> D >> N, D | N) {
    vector<Tool> tool(N);
    for (auto &t : tool)
      cin >> t.a >> t.e >> t.r;

    memo.assign(101, vector<int>(201, -1));

    int answer = dfs(D, 0, 0, tool);
    if (answer == INF)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

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

int dp[110][110];
struct S {
  int a, e, r;
  bool operator<(const S &x) const { return r < x.r; }
} t[100];

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

  int D, N;
  while (cin >> D >> N, D) {
    for (int i = 0; i < N; i++) {
      cin >> t[i].a >> t[i].e >> t[i].r;
    }

    const int INF = 1 << 25;
    fill((int *)begin(dp), (int *)end(dp), INF);
    dp[0][D] = 0;

    typedef pair<int, int> P;
    queue<P> q;
    q.push({0, D});
    while (q.size()) {
      int ex = q.front().first, d = q.front().second;
      q.pop();
      for (int i = 0; i < N; i++) {
        if (ex < t[i].r)
          continue;
        int nex = min(100, ex + t[i].e), nd = max(0, d - t[i].a);
        if (dp[nex][nd] > dp[ex][d] + 1) {
          dp[nex][nd] = dp[ex][d] + 1;
          q.push({nex, nd});
        }
      }
    }

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

Error: 0

Fixed submission:

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

int dp[110][110];
struct S {
  int a, e, r;
  bool operator<(const S &x) const { return r < x.r; }
} t[100];

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

  int D, N;
  while (cin >> D >> N, D) {
    for (int i = 0; i < N; i++) {
      cin >> t[i].a >> t[i].e >> t[i].r;
    }
    sort(t, t + N);

    const int INF = 1 << 25;
    fill((int *)begin(dp), (int *)end(dp), INF);
    dp[0][D] = 0;

    typedef pair<int, int> P;
    queue<P> q;
    q.push({0, D});
    while (q.size()) {
      int ex = q.front().first, d = q.front().second;
      q.pop();
      for (int i = 0; i < N; i++) {
        if (ex < t[i].r)
          continue;
        int nex = ex + t[i].e, nd = max(0, d - t[i].a);
        if (dp[nex][nd] > dp[ex][d] + 1) {
          dp[nex][nd] = dp[ex][d] + 1;
          q.push({nex, nd});
        }
      }
    }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF (2 << 28)
using namespace std;

int main() {

  int d, n, dp[111][111];
  int a[111], e[111], r[111];

  while (cin >> d >> n, d || n) {
    for (int i = 0; i < n; i++)
      cin >> a[i] >> e[i] >> r[i];
    fill_n(*dp, 111 * 111, 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 (r[k] <= j)
            dp[max(0, i - a[k])][min(100, j + e[k])] =
                min(dp[max(0, i - a[k])][min(100, j + e[k])], dp[i][j] + 1);
        }
      }
    }
    int res = *min_element(dp[0], dp[0] + 101);
    if (res == INF)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF (2 << 28)
using namespace std;

int d, n, dp[111][111];
int a[111], e[111], r[111];

int rec(int exp, int hp) {

  if (hp <= 0)
    return 0;

  int ret = INF;
  for (int i = 0; i < n; i++) {
    if (r[i] <= exp)
      ret = min(ret, rec(min(100, exp + e[i]), hp - a[i]) + 1);
  }
  return dp[exp][hp] = 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));
    int dd = rec(0, d);
    if (dd == INF)
      cout << "NA" << endl;
    else
      cout << dd << endl;
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

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

int D, N;
vector<int> a(110), e(110), r(110); // 耐久力 経験値 必要な経験値
int dp[110][11000]; // 耐久力 i の時に 経験値 j の時の回数

int solve(int d, int exp) {
  int ret = 10e8;
  if (d <= 0)
    return 0;
  if (dp[d][exp] != -1)
    return dp[d][exp];
  for (int i = 0; i < N; i++) {
    int E = min(110, exp + e[i]);
    if (a[i] == 0 && E == exp)
      continue;
    if (r[i] <= exp)
      ret = min(ret, solve(d - a[i], E) + 1);
  }
  dp[d][exp] = ret;
  return ret;
}

int main() {
  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 < 11000; j++)
        dp[i][j] = -1;
    int ret = solve(D, 0);
    if (ret == 10e8)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}

Error: 0

Fixed submission:

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

int D, N;
vector<int> a(110), e(110), r(110); // 耐久力 経験値 必要な経験値
int dp[110][11000]; // 耐久力 i の時に 経験値 j の時の回数

int solve(int d, int exp) {
  int ret = 10e8;
  if (d <= 0)
    return 0;
  if (dp[d][exp] != -1)
    return dp[d][exp];
  for (int i = 0; i < N; i++) {
    int E = min(110, exp + e[i]);
    if (r[i] <= exp)
      ret = min(ret, solve(d - a[i], E) + 1);
  }
  dp[d][exp] = ret;
  return ret;
}

int main() {
  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 < 11000; j++)
        dp[i][j] = -1;
    int ret = solve(D, 0);
    if (ret == 10e8)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define cmin(a, b) (a = min(a, b))
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int a[100], e[100], r[100];
int dp[101][101];

int main() {
  int d, n;
  while (scanf("%d%d", &d, &n), d) {
    rep(i, n) scanf("%d%d%d", &a[i], &e[i], &r[i]);
    memset(dp, 0x3f, sizeof(dp));
    rep(i, 101) { dp[0][i] = 0; }
    for (int i = 0; i <= d; i++) {
      for (int j = 100; j >= 0; j--) {
        rep(k, n) {
          if (j >= r[k])
            cmin(dp[i][j], dp[max(0, i - a[k])][min(100, j + e[k])] + 1);
        }
      }
    }
    if (dp[d][0] == INF)
      puts("NA");
    else
      printf("%d\\n", dp[d][0]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define cmin(a, b) (a = min(a, b))
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int a[100], e[100], r[100];
int dp[101][101];
int main() {
  int d, n;
  while (scanf("%d%d", &d, &n), d) {
    rep(i, n) scanf("%d%d%d", &a[i], &e[i], &r[i]);
    memset(dp, 0x3f, sizeof(dp));
    rep(i, 101) dp[0][i] = 0;
    rep(i, d + 1) for (int j = 100; j >= 0; j--) {
      if (dp[i][j] == INF)
        continue;
      rep(k, n) {
        if (j - e[k] >= r[k]) {
          rep(p, e[k] + 1) rep(t, a[k] + 1) {
            cmin(dp[min(d, i + t)][j - p], dp[i][j] + 1);
            if (min(d, i + t) == d)
              break;
          }
        }
      }
    }
    if (dp[d][0] == INF)
      puts("NA");
    else
      printf("%d\\n", dp[d][0]);
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_V = 10000;
struct edge {};
vector<edge> G[MAX_V];

// ??§????????°INF
#define INF (1 << 30)
// ?¨±?????????????????
#define EPS (1e-10)
// 2??????????????????????????????????????????
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int D, N;
  while (true) {
    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 < 101; ++j) {
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < N; ++k) {
          if (j < r[k])
            continue;
          int tmp_i = max(0, i - a[k]), tmp_j = min(100, j + e[k]);
          dp[tmp_i][tmp_j] = min(dp[tmp_i][tmp_j], dp[i][j] + 1);
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 101; ++i)
      ans = min(ans, dp[0][i]);
    if (ans == INF) {
      cout << "NA" << endl;
      continue;
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_V = 10000;
struct edge {};
vector<edge> G[MAX_V];

// ??§????????°INF
#define INF (1 << 30)
// ?¨±?????????????????
#define EPS (1e-10)
// 2??????????????????????????????????????????
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int D, N;
  while (true) {
    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, N));
    dp[D][0] = 0;
    for (int i = D; i >= 0; ++i) {
      for (int j = 0; j < 101; ++j) {
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < N; ++k) {
          if (j < r[k])
            continue;
          int tmp_i = max(0, i - a[k]), tmp_j = min(100, j + e[k]);
          dp[tmp_i][tmp_j] = min(dp[tmp_i][tmp_j], dp[i][j] + 1);
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 101; ++i)
      ans = min(ans, dp[0][i]);
    if (ans == INF) {
      cout << "NA" << endl;
      continue;
    }
    cout << ans << endl;
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
int d, n;
vector<int> a, e, r;
vector<vector<int>> memo;
const int INF = 1000000000;

void rec(int c = 0, int exp = 0, int end = d) {
  if (memo[exp][end] <= c)
    return;
  memo[exp][end] = c;

  rep(i, n) {
    if (exp < r[i])
      continue;
    rec(c + 1, min(exp + e[i], 100), max(end - a[i], 0));
  }
}

int main() {
  while (cin >> d >> n, d) {
    a = e = r = vector<int>(n);
    rep(i, n) cin >> a[i] >> e[i] >> r[i];
    memo = vector<vector<int>>(105, vector<int>(105, INF));
    rec();
    int ans = INF;
    rep(i, 101) ans = min(ans, memo[i][0]);
    cout << (ans == INF ? "NA" : to_string(ans)) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
int d, n, memo[110][110];
vector<int> a, e, r;
const int INF = 100000000;

int rec(int exp = 0, int end = d) {
  if (end <= 0) {
    return 0;
  }
  if (memo[exp][end] != -1) {
    return memo[exp][end];
  }

  int res = INF;
  rep(i, n) {
    if (exp < r[i])
      continue;
    if (a[i] == 0 && e[i] == 0)
      continue;
    res = min(res, rec(exp + e[i], end - a[i]) + 1);
  }
  return memo[exp][end] = res;
}

int main() {
  while (cin >> d >> n, d) {
    a = e = r = vector<int>(n);
    rep(i, n) cin >> a[i] >> e[i] >> r[i];
    memset(memo, -1, sizeof(memo));
    int ans = rec();
    cout << (ans == INF ? "NA" : to_string(ans)) << endl;
  }
  return 0;
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define N1 102
#define N2 102
#define INF 2000000000
using namespace std;
void DP();
int D, N, a[N1], e[N1], r[N1];
int dp[N1 + N1 + N1 + N1][N2], minv;
int main() {
  while (1) {
    cin >> D >> N;
    if (D == 0 && N == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> a[i] >> e[i] >> r[i];
    DP();
  }
  return 0;
}
void DP() {
  for (int i = 0; i < N1 + N1; i++) {
    for (int j = 0; j < N2; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 1; i < N2; i++)
    dp[0][i] = i - 1;
  for (int i = D, k = 1; i >= 0; i--, k++)
    dp[k][0] = i;
  dp[1][1] = 0;
  for (int i = 1; i <= D + 1; i++) {
    for (int j = 1; j < N2; j++) {
      if (dp[i][j] != INF) {
        for (int k = 0; k < N; k++) {
          if (dp[0][j] >= r[k]) {
            if (j + e[k] >= N2) {
              if (dp[i + a[k]][N2 - 1] > dp[i][j] + 1)
                dp[i + a[k]][N2 - 1] = dp[i][j] + 1;
            } else if (dp[i + a[k]][j + e[k]] > dp[i][j] + 1)
              dp[i + a[k]][j + e[k]] = dp[i][j] + 1;
          }
        }
      }
    }
  }
  minv = INF;
  for (int i = D + 1; i < N1 + N1; i++) {
    for (int j = 1; j < N2; j++) {
      minv = min(minv, dp[i][j]);
    }
  }
  if (minv != INF)
    cout << minv << endl;
  else
    cout << "NA" << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define N1 101
#define N2 100 * 100 + 50
#define INF 2000000000
using namespace std;
void DP();
int D, N, a[N1], e[N1], r[N1];
int dp[N1 + N1 + N1 + N1][N2], minv;
int main() {
  while (1) {
    cin >> D >> N;
    if (D == 0 && N == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> a[i] >> e[i] >> r[i];
    DP();
  }
  return 0;
}
void DP() {
  for (int i = 0; i < N1 + N1; i++) {
    for (int j = 0; j < N2; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 1; i < N1 + N1; i++)
    dp[0][i] = i - 1;
  for (int i = D, k = 1; i >= 0; i--, k++)
    dp[k][0] = i;
  dp[1][1] = 0;
  for (int i = 1; i <= D + 1; i++) {
    for (int j = 1; j < N2; j++) {
      if (dp[i][j] != INF) {
        for (int k = 0; k < N; k++) {
          if (dp[0][j] >= r[k]) {
            if (dp[i + a[k]][j + e[k]] > dp[i][j] + 1)
              dp[i + a[k]][j + e[k]] = dp[i][j] + 1;
          }
        }
      }
    }
  }
  minv = INF;
  for (int i = D + 1; i < N1 + N1; i++) {
    for (int j = 1; j < N2; j++) {
      minv = min(minv, dp[i][j]);
    }
  }
  if (minv != INF)
    cout << minv << endl;
  else
    cout << "NA" << endl;
}
Problem p00283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int D, N;
int A[100], E[100], R[100];
int DP[101][101];
int main() {
  while (true) {
    int ans = 100000;
    scanf("%d%d", &D, &N);
    if (D == 0 && N == 0)
      return 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d%d", &A[i], &E[i], &R[i]);
    }
    for (int i = 0; i <= 100; i++)
      for (int j = 0; j <= 100; j++)
        DP[i][j] = 100000;
    DP[0][0] = 0;
    for (int i = 0; i <= D; i++) {
      for (int j = 0; j <= 100; j++) {
        if (DP[i][j] == 100000)
          continue;
        for (int k = 0; k < N; k++) {
          if (R[k] > j)
            continue;
          DP[min(100, i + A[k])][min(100, j + E[k])] =
              min(DP[min(100, i + A[k])][min(100, j + E[k])], DP[i][j] + 1);
        }
      }
    }

    for (int i = D; i <= 100; i++) {
      for (int j = 0; j <= 100; j++) {
        if (DP[i][j] != 100000) {
          ans = min(ans, DP[i][j]);
        }
      }
    }
    if (ans == 100000)
      printf("NA\\n");
    else
      printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int D, N;
int A[100], E[100], R[100];
int DP[101][10010];
int main() {
  while (true) {
    int ans = 100000;
    scanf("%d%d", &D, &N);
    if (D == 0 && N == 0)
      return 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d%d", &A[i], &E[i], &R[i]);
    }
    for (int i = 0; i <= 100; i++)
      for (int j = 0; j < 10010; j++)
        DP[i][j] = 100000;
    DP[0][0] = 0;
    for (int i = 0; i <= D; i++) {
      for (int j = 0; j <= N * 100; j++) {
        if (DP[i][j] == 100000)
          continue;
        for (int k = 0; k < N; k++) {
          if (R[k] > j)
            continue;
          DP[min(100, i + A[k])][j + E[k]] =
              min(DP[min(D, i + A[k])][j + E[k]], DP[i][j] + 1);
        }
      }
    }

    for (int i = D; i <= 100; i++) {
      for (int j = 0; j < 10010; j++) {
        if (DP[i][j] != 100000) {
          ans = min(ans, DP[i][j]);
        }
      }
    }
    if (ans == 100000)
      printf("NA\\n");
    else
      printf("%d\\n", ans);
  }
}
Problem p00283 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> pi;

const int INF = 1234567;
int c[101][101];

int main() {
  int D, N;
  int a[100], e[100], r[100];
  while (scanf(" %d %d", &D, &N), D) {
    rep(i, N) scanf(" %d %d %d", &a[i], &e[i], &r[i]);

    fill(c[0], c[101], INF);
    c[D][0] = 0;
    queue<pi> que;
    que.push(pi(D, 0));
    while (!que.empty()) {
      pi v = que.front();
      que.pop();
      int rem = v.fi, ex = v.se;
      rep(i, N) {
        if (ex < r[i])
          continue;

        int nr = max(0, rem - a[i]), ne = min(100, ex + e[i]);
        if (c[nr][ne] > c[rem][ex] + 1) {
          c[nr][ne] = c[rem][ex] + 1;
          que.push(pi(nr, ne));
        }
      }
    }

    int ans = INF;
    rep(i, 101) ans = min(ans, c[0][i]);

    if (ans == INF)
      printf("NA\\n");
    else
      printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

int D, N;
int a[100], e[100], r[100];

const int INF = 1234567;
int dp[101][101];
int dfs(int rem, int ex) {
  if (dp[rem][ex] >= 0)
    return dp[rem][ex];
  if (rem == 0)
    return 0;

  int ret = INF;
  rep(i, N) {
    if (ex < r[i])
      continue;

    int nr = max(0, rem - a[i]);
    int ne = min(100, ex + e[i]);
    ret = min(ret, dfs(nr, ne) + 1);
  }

  return dp[rem][ex] = ret;
}

int main() {
  while (scanf(" %d %d", &D, &N), D) {
    rep(i, N) scanf(" %d %d %d", &a[i], &e[i], &r[i]);
    memset(dp, -1, sizeof(dp));
    int ans = dfs(D, 0);
    if (ans == INF)
      printf("NA\\n");
    else
      printf("%d\\n", ans);
  }
  return 0;
}