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

#include <iostream>
#include <math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

int K[KMAX];
char T[KMAX][VMAX];

void eratos(int n, bool prime[]) {
  for (int i = 0; i <= n; i++)
    prime[i] = false;
  for (int i = 3; i <= n; i += 2)
    prime[i] = true;
  prime[2] = true;
  int lim = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= lim; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      prime[j] = false;
  }
}

int main() {
  bool prime[VMAX + 1];
  eratos(VMAX, prime);

  int k, v;

  while (cin >> k >> v && !(k == 0 && v == 0)) {
    for (int i = 0; i < k; i++)
      cin >> K[i];

    for (int j = 0; j <= v; j++)
      T[0][j] = 0;
    for (int j = K[0]; j <= v; j += K[0])
      T[0][j] = 1;
    for (int i = 0; i < k; i++)
      T[i][0] = 1;

    for (int i = 1; i < k; i++) {
      for (int j = 1; j < K[i]; j++)
        T[i][j] = T[i - 1][j];
      for (int j = K[i]; j <= v; j++) {
        T[i][j] = (T[i - 1][j] | T[i][j - K[i]]);
      }
    }
    int ans = -1;
    for (int i = v; i >= 2; i--) {
      if (prime[i] && T[k - 1][i]) {
        ans = i;
        break;
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

int K[KMAX];
int T[KMAX][VMAX];

void eratos(int n, bool prime[]) {
  for (int i = 0; i <= n; i++)
    prime[i] = false;
  for (int i = 3; i <= n; i += 2)
    prime[i] = true;
  prime[2] = true;
  int lim = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= lim; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      prime[j] = false;
  }
}

int main() {
  bool prime[VMAX + 1];
  eratos(VMAX, prime);

  int k, v;

  while (cin >> k >> v && !(k == 0 && v == 0)) {
    for (int i = 0; i < k; i++)
      cin >> K[i];

    for (int j = 0; j <= v; j++)
      T[0][j] = 0;
    for (int j = K[0]; j <= v; j += K[0])
      T[0][j] = 1;
    for (int i = 0; i < k; i++)
      T[i][0] = 1;

    for (int i = 1; i < k; i++) {
      for (int j = 1; j < K[i]; j++)
        T[i][j] = T[i - 1][j];
      for (int j = K[i]; j <= v; j++) {
        T[i][j] = (T[i - 1][j] | T[i][j - K[i]]);
      }
    }
    int ans = -1;
    for (int i = v; i >= 2; i--) {
      if (prime[i] && T[k - 1][i]) {
        ans = i;
        break;
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define N 1000000
using namespace std;

int main(void) {
  bool isprime[N + 1];
  for (int i = 0; i <= N; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= N; i++)
    if (isprime[i])
      for (int j = 2 * i; j <= N; j += i)
        isprime[j] = false;
  bool budget[N + 1];
  int n, m;
  while (cin >> n >> m, n | m) {
    for (int i = 0; i <= m; i++)
      budget[i] = false;
    int tmp;
    budget[0] = true;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      for (int j = 0; j <= m; j++)
        if (budget[j] && (j + tmp) <= m)
          budget[j + tmp] = true;
    }
    int MAX = 0;
    for (int i = m; i > 0; i--) {
      if (budget[i] & isprime[i]) {
        MAX = i;
        break;
      }
    }
    if (MAX)
      cout << MAX << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define N 1000000
using namespace std;

int main(void) {
  bool isprime[N + 1];
  for (int i = 0; i <= N; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= N; i++)
    if (isprime[i])
      for (int j = 2 * i; j <= N; j += i)
        isprime[j] = false;

  bool budget[N + 1];
  int n, m;
  while (cin >> n >> m, n | m) {
    for (int i = 0; i <= m; i++)
      budget[i] = false;
    int tmp;
    budget[0] = true;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      for (int j = 0; j <= m; j++)
        if (budget[j])
          budget[j + tmp] = true;
    }
    int MAX = 0;
    for (int i = m; i > 0; i--) {
      if (budget[i] & isprime[i]) {
        MAX = i;
        break;
      }
    }
    if (MAX)
      cout << MAX << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, x, ans;
int in[31], in2[31];
bool dp[1000001];
void solve(int a, int sum) {
  for (int i = a; i <= n; i++) {
    if (sum + in[i] > x && !in2[i]) {
      in[i] /= 10;
      in2[i] = 1;
    }
    if (sum + in[i] <= x) {
      if (dp[sum + in[i]])
        continue;
      if (sum + in[i] % 2) {
        dp[sum + in[i]] = 1;
      }
      solve(i, sum + in[i]);
    }
  }
}
int main() {
  while (cin >> n >> x && n && x) {
    ans = 0;
    fill(dp, dp + 1000001, 0);
    for (int i = 1; i <= n; i++) {
      cin >> in[i];
      in[i] *= 10;
      in2[i] = 0;
    }
    solve(1, 0);
    for (int i = x; i > 0; i--) {
      if (dp[i]) {
        for (int j = 5; j <= x; j++) {
          if (j == i)
            ans = j;
          if (i % j == 0)
            break;
        }
        if (ans)
          break;
      }
    }
    if (ans)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, x, ans;
int in[310];
bool dp[10000001];
void solve(int a, int sum) {
  for (int i = a; i <= n; i++) {
    if (sum + in[i] <= x) {
      if (dp[sum + in[i]])
        continue;
      if (sum + in[i] % 2 && sum + in[i] % 3) {
        dp[sum + in[i]] = 1;
      }
      solve(i, sum + in[i]);
    }
  }
}
int main() {
  while (cin >> n >> x && n && x) {
    ans = 0;
    fill(dp, dp + 1000001, 0);
    for (int i = 1; i <= n; i++)
      cin >> in[i];
    solve(1, 0);
    for (int i = x; i > 0; i--) {
      if (dp[i]) {
        for (int j = 5; j <= x; j++) {
          if (j == i)
            ans = j;
          if (i % j == 0)
            break;
        }
        if (ans)
          break;
      }
    }
    if (ans)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i > n; i--)
using namespace std;

int n, x;
int m[40];
int dp[1000010];
int p[1000000];

void sieve() {
  memset(p, 1, sizeof p);
  p[0] = p[1] = 0;
  for (int i = 2; i * i <= 1000000; i++) {
    if (p[i]) {
      for (int j = 2; i * j <= 1000000; j++)
        p[i * j] = 0;
    }
  }
}

void makeDP() {
  memset(dp, 0, sizeof dp);
  rep(i, 1, n + 1) {
    rep(j, 0, x + 1) {
      if (j >= m[i])
        dp[j] = max(dp[j], dp[j - m[i]] + m[i]);
    }
  }
}

int main() {
  sieve();

  while (cin >> n >> x, n || x) {
    rep(i, 1, n + 1) cin >> m[i];

    makeDP();

    int ans = -1;
    repr(i, x, -1) {
      if (p[dp[i]]) {
        ans = dp[i];
        break;
      }
    }

    if (ans != -1)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n, x;
int m[40];
int dp[40][1000010];

bool judge(int v) {
  if (v <= 1)
    return false;
  if (v == 2)
    return true;
  for (int i = 2; i * i <= x + 1; i++) {
    if (v % i == 0)
      return false;
  }
  return true;
}

void makeDP() {
  rep(i, 1, n + 1) {
    rep(j, 0, x + 1) {
      if (j - m[i] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i][j - m[i]] + m[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
}

int solve() {
  for (int i = x; i >= 0; i--) {
    if (judge(dp[n][i]))
      return dp[n][i];
  }
  return 0;
}

int main() {
  while (cin >> n >> x, n || x) {
    rep(i, 1, n + 1) cin >> m[i];

    makeDP();

    int ans = solve();

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

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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, x, ans;
int in[31], in2[31];
bool dp[1000001];
void solve(int a, int sum) {
  for (int i = a; i <= n; i++) {
    if (sum + in[i] > x && !in2[i]) {
      in[i] /= 1000;
      in2[i] = 1;
    }
    if (sum + in[i] <= x) {
      if (dp[sum + in[i]])
        continue;
      if (sum + in[i] % 2) {
        dp[sum + in[i]] = 1;
      }
      solve(i, sum + in[i]);
    }
  }
}
int main() {
  while (cin >> n >> x && n && x) {
    ans = 0;
    fill(dp, dp + 1000001, 0);
    for (int i = 1; i <= n; i++) {
      cin >> in[i];
      in[i] *= 1000;
      in2[i] = 0;
    }
    solve(1, 0);
    for (int i = x; i > 0; i--) {
      if (dp[i]) {
        for (int j = 5; j <= x; j++) {
          if (j == i)
            ans = j;
          if (i % j == 0)
            break;
        }
        if (ans)
          break;
      }
    }
    if (ans)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, x, ans;
int in[31], in2[31];
bool dp[1000001];
void solve(int a, int sum) {
  for (int i = a; i <= n; i++) {
    if (sum + in[i] > x && !in2[i]) {
      in[i] /= 10000;
      in2[i] = 1;
    }
    if (sum + in[i] <= x) {
      if (dp[sum + in[i]])
        continue;
      if (sum + in[i] % 2) {
        dp[sum + in[i]] = 1;
      }
      solve(i, sum + in[i]);
    }
  }
}
int main() {
  while (cin >> n >> x && n && x) {
    ans = 0;
    fill(dp, dp + 1000001, 0);
    for (int i = 1; i <= n; i++) {
      cin >> in[i];
      in[i] *= 10000;
      in2[i] = 0;
    }
    solve(1, 0);
    for (int i = x; i > 0; i--) {
      if (dp[i]) {
        for (int j = 5; j <= x; j++) {
          if (j == i)
            ans = j;
          if (i % j == 0)
            break;
        }
        if (ans)
          break;
      }
    }
    if (ans)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[1000001];
bool sosu[1000001];
int main() {
  for (int i = 2; i < 1000000; i++) {
    if (!sosu[i]) {
      for (int j = 2; j * i <= 1000000; j++) {
        sosu[i * j] = 1;
      }
    }
  }
  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    for (int i = 0; i < n; i++)
      for (int j = 0; j <= x - v[i]; j++)
        if (dp[j])
          dp[j + v[i]] = dp[j];
    for (int i = x; i >= 0; i--) {
      if (!sosu[i] && dp[i]) {
        if (i)
          cout << i << endl;
        else
          cout << "NA" << endl;
        break;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[1000001];
bool sosu[1000001];
int main() {
  for (int i = 2; i < 1000000; i++) {
    if (!sosu[i]) {
      for (int j = 2; j * i <= 1000000; j++) {
        sosu[i * j] = 1;
      }
    }
  }
  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    for (int i = 0; i < n; i++)
      for (int j = 0; j <= x; j++)
        if (dp[j])
          dp[j + v[i]] = dp[j];
    for (int i = x; i >= 0; i--) {
      if (!sosu[i] && dp[i]) {
        if (i)
          cout << i << endl;
        else
          cout << "NA" << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

bool is_prime[1000000 + 1];
void prime(int n) {
  for (int i = 0; i < n + 1; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < n + 1; i++) {
    if (is_prime[i] == true) {
      for (int j = i + i; j < n + 1; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

int main() {
  int n, m;
  prime(1000000);
  while (cin >> n >> m) {
    if (!n && !m)
      break;
    bool DP[1000000 + 1];
    for (int i = 0; i < m + 1; i++)
      DP[i] = false;
    DP[0] = true;
    int ryori;
    for (int i = 0; i < n; i++) {
      cin >> ryori;
      for (int j = 0; j < m + 1 - ryori; j++)
        if (DP[j])
          DP[j + ryori] = true;
    }

    for (int i = m; i >= 0; i--) {
      if (is_prime[i] && DP[i]) {
        cout << i << endl;
        break;
      } else if (i == 0) {
        cout << "NA" << endl;
        break;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  bool p[1000001];
  int n, x;

  p[0] = false;
  p[1] = false;
  for (int i = 2; i <= 1000000; i++) {
    p[i] = true;
  }

  for (int i = 2; i * i <= 1000000; i++) {
    for (int j = 2; j * i <= 1000000; j++) {
      p[i * j] = false;
    }
  }

  while (true) {
    cin >> n >> x;

    if (n == 0 && x == 0) {
      break;
    }

    vector<int> dp(x + 1, 0);
    vector<int> money(n);

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

    for (int i = 0; i < n; i++) {
      dp[money[i]]++;
    }

    for (int i = 0; i <= x; i++) {
      for (int j = 0; j < n; j++) {
        if (dp[i] == 0 || (i + money[j]) > x) {
          continue;
        }
        dp[i + money[j]]++;
      }
    }

    int ans = 0;
    for (int i = x; i >= 2; i--) {
      if (p[i] == true && dp[i] > 0) {
        ans = i;
        break;
      }
    }

    if (ans == 0) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#define MAX 1000000
using namespace std;

int n, x, d[31];
bool p[MAX + 1], dp[MAX + 1];

void isPrime() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = false;
  for (int i = 2; i * i <= MAX; i++) {
    for (int j = i + i; j <= MAX; j += i)
      p[j] = false;
  }
}

int main() {
  isPrime();
  while (cin >> n >> x, n || x) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> d[i];

    dp[0] = true;
    for (int i = 0; i <= x; i++) {
      if (dp[i]) {
        for (int j = 0; j < n; j++) {
          if (i + d[j] > x)
            continue;
          dp[i + d[j]] = true;
        }
      }
    }

    bool ju = true;
    for (int i = x; i > 0; i--) {
      if (p[i] && dp[i]) {
        cout << i << endl;
        ju = false;
        break;
      }
    }
    if (ju)
      cout << "NA" << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#define MAX 1000000
using namespace std;

int n, x, d[31];
bool p[MAX + 1], dp[MAX + 1];

void isPrime() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = false;
  for (int i = 2; i * i <= MAX; i++) {
    for (int j = i + i; j <= MAX; j += i)
      p[j] = false;
  }
}

int main() {
  isPrime();
  while (cin >> n >> x, n || x) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> d[i];

    dp[0] = true;
    for (int i = 0; i <= x; i++) {
      if (dp[i]) {
        for (int j = 0; j < n; j++) {
          if (i + d[i] >= x)
            continue;
          dp[i + d[j]] = true;
        }
      }
    }

    bool ju = true;
    for (int i = x; i > 0; i--) {
      if (p[i] && dp[i]) {
        cout << i << endl;
        ju = false;
        break;
      }
    }
    if (ju)
      cout << "NA" << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> Pi;

#define MAX 1000000

bool prime[1000000];
int N, X;
int price[30];
int dp[MAX + 1];

void PrimeJudge() {
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  FOR(i, 1, sqrt(MAX)) {
    if (prime[i]) {
      for (int j = (i + 1); (i + 1) * j <= MAX; ++j) {
        prime[(i + 1) * j - 1] = false;
      }
    }
  }
  return;
}

int main() {
  PrimeJudge();
  while (scanf("%d %d", &N, &X), N || X) {
    memset(dp, 0, sizeof(dp));
    rep(i, N) scanf("%d", &price[i]);
    rep(i, N) FOR(j, price[i], X + 1) {
      dp[j] = max(dp[j], dp[j - price[i]] + price[i]);
    }
    bool flag = false;
    for (int i = X; i >= 0; --i)
      if (dp[i] - 1 >= 0) {
        if (prime[dp[i] - 1]) {
          printf("%d\\n", dp[i]);
          flag = true;
          break;
        }
      }
    if (!flag)
      printf("NA\\n");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> Pi;

#define MAX 1000000

bool prime[1000000];
int N, X;
int price[30];
int dp[31][1000001];

void PrimeJudge() {
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  FOR(i, 1, sqrt(MAX)) {
    if (prime[i]) {
      for (int j = (i + 1); (i + 1) * j <= MAX; ++j) {
        prime[(i + 1) * j - 1] = false;
      }
    }
  }
  return;
}

int main() {
  PrimeJudge();
  while (scanf("%d %d", &N, &X), N || X) {
    rep(i, N) scanf("%d", &price[i]);
    rep(i, N) rep(j, X + 1) {
      if (j < price[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - price[i]] + price[i]);
      }
    }
    bool flag = false;
    for (int i = X; i >= 0; --i)
      if (dp[N][i] - 1 >= 0) {
        if (prime[dp[N][i] - 1]) {
          printf("%d\\n", dp[N][i]);
          flag = true;
          break;
        }
      }
    if (!flag)
      printf("NA\\n");
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    int x;
    scanf("%d", &x);

    int p[30];
    rep(i, n) scanf("%d", &p[i]);

    bool dp[1000001] = {false};
    dp[0] = true;
    reps(i, 1, x + 1) { rep(j, n) dp[i] |= i >= p[j] && dp[i - p[j]]; }

    bool isprime[1000001];
    fill(isprime, isprime + x + 1, true);
    isprime[0] = isprime[1] = false;
    reps(i, 2, x) {
      if (isprime[i]) {
        for (int j = i * 2; j <= x; j += i)
          isprime[j] = false;
      }
    }

    int ans = -1;
    for (int i = x + 1; i >= 0; --i) {
      if (dp[i] && isprime[i]) {
        ans = i;
        break;
      }
    }
    if (ans == -1)
      puts("NA");
    else
      printf("%d\\n", ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int ans = -1;
char visited[1000001], isprime[1000001];
int price[30];
int n, x;

void eratos(int m) {
  int i, j;
  for (i = 2; i <= m; ++i)
    isprime[i] = 1;
  isprime[0] = isprime[1] = 0;

  for (i = 2; i * i <= m; ++i) {
    if (isprime[i]) {
      for (j = i * 2; j <= m; j += i)
        isprime[j] = 0;
    }
  }
}

void search(int c) {
  int i;
  if (visited[c])
    return;
  visited[c] = 1;

  if (ans < c && isprime[c])
    ans = c;

  for (i = 0; i < n; ++i) {
    if (c + price[i] <= x)
      search(c + price[i]);
  }
}

int main(void) {
  eratos(1000000);
  while (scanf("%d%d", &n, &x), n) {
    int i;
    for (i = 0; i < n; ++i)
      scanf("%d", &price[i]);

    ans = -1;
    for (i = 0; i <= x; ++i)
      visited[i] = 0;

    search(0);
    if (ans == -1)
      puts("NA");
    else
      printf("%d\\n", ans);
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX 1000001

using namespace std;

bool dp[MAX];
int n, est;
int value[31];

int isPrime(int N) {

  for (int i = 2; i * i + 1 < N; i++) {
    if (N % i == 0)
      return 0;
  }
  return N;
}

void solve(void) {

  dp[0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = value[i]; j <= est; j++) {
      if (dp[j - value[i]])
        dp[j] = true;
    }
  }
}

int main(void) {

  while (cin >> n >> est, n + est) {

    fill(dp, dp + MAX, false);

    for (int i = 0; i < n; i++)
      cin >> value[i];

    solve();

    int ans = 0;
    for (int i = est; i >= 2; i--) {
      if (dp[i]) {
        ans = isPrime(i);
        if (ans)
          break;
      }
    }
    if (ans)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define MAX 1100000

using namespace std;

bool dp[MAX];
int dish[30];

bool isPrime(int n) {

  for (int i = 2; i * i + 1 < n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main(void) {

  int n, value;
  while (cin >> n >> value, n + value) {

    for (int i = 0; i < MAX; i++)
      dp[i] = false;

    for (int i = 0; i < n; i++)
      cin >> dish[i];

    dp[0] = true;
    for (int i = 0; i <= value; i++) {
      if (dp[i]) {
        for (int j = 0; j < n; j++) {
          dp[i + dish[j]] = true;
        }
      }
    }

    bool flag = false;
    for (int i = value; i > 0; i--) {
      if (dp[i] && isPrime(i)) {
        cout << i << endl;
        flag = true;
        break;
      }
    }
    if (!flag)
      cout << "NA" << endl;
  }
}