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

#include <iostream>
#include <string.h>
using namespace std;

bool prime[1000001];
void is_prime(int n) {
  for (int i = 0; i <= n; ++i)
    prime[i] = true;
  prime[0] = prime[1] = false;

  for (int i = 2; i <= n; ++i) {
    if (prime[i]) {
      for (int j = i * 2; j <= n; j += i)
        prime[j] = false;
    }
  }
}

bool DP[1000001];
int N, X;

int main() {
  is_prime(1000000);
  while (cin >> N >> X && (N || X)) {
    int cook[50], ans = 0;
    memset(DP, 0, sizeof(DP));

    for (int i = 0; i < N; ++i) {
      cin >> cook[i];
      DP[cook[i]] = true;
    }

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= X; ++j) {
        if (DP[j] && j + cook[i] <= X)
          DP[j + cook[i]] = true;
      }
    }

    for (int i = 0; i <= X; ++i)
      if (DP[i] && prime[i])
        ans = i;
    if (ans == 0)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

bool prime[1000001];
void is_prime(int n) {
  for (int i = 0; i <= n; ++i)
    prime[i] = true;
  prime[0] = prime[1] = false;

  for (int i = 2; i <= n; ++i) {
    if (prime[i]) {
      for (int j = i * 2; j <= n; j += i)
        prime[j] = false;
    }
  }
}

int memo[1000001];
int N, X, cook[50], ans;

int rec(int w) {
  if (memo[w] != -1)
    return memo[w];
  if (X - w < 0)
    return 0;
  if (prime[X - w])
    ans = max(ans, X - w);
  int res = 0;
  for (int i = 0; i < N; ++i) {
    if (w - cook[i] >= 0)
      res = max(res, rec(w - cook[i]));
  }
  return memo[w] = res;
}

int main() {
  is_prime(1000000);
  while (cin >> N >> X && (N || X)) {
    ans = 0;
    memset(memo, -1, sizeof(memo));

    for (int i = 0; i < N; ++i)
      cin >> cook[i];

    rec(X);
    if (ans == 0)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }

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

#include <iostream>
#define N 1000001
using namespace std;
int main() {
  int p[N] = {0};
  for (int i = 3; i < N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 3; i <= 1000; i += 2) {
    if (p[i] == 1) {
      for (int j = 2 * i; j < N; j += i)
        p[j] = 0;
    }
  }
  int n, x;
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;
    int t[x + 1];
    for (int i = 0; i <= x; i++)
      t[i] = 0;
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      if (k <= x)
        t[k] = 1;
      for (int j = 1; j <= x; j++) {
        if (t[j] == 1 && j + k <= x)
          t[j + k] = 1;
      }
    }
    bool ok = false;
    for (int i = x; i >= 0; i--) {
      if (t[i] == 1 && p[i] == 1) {
        cout << i << endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout << "NA" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX_N 1000001

int prime[MAX_N];
bool is_prime[MAX_N];

int main() {
  int p = 0;
  for (int i = 0; i <= MAX_N; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= MAX_N; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= MAX_N; j += i)
        is_prime[j] = false;
    }
  }

  int n, x;
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;
    int v[n];
    bool dp[x + 1];
    for (int i = 0; i <= x; i++)
      dp[i] = false;

    for (int i = 0; i < n; i++)
      cin >> v[i];
    dp[v[0]] = true;

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

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

int main() {
  bool is_prime[N];
  for (int i = 0; i < N; i++)
    is_prime[i] = false;

  for (int i = 3; i < N; i += 2)
    is_prime[i] = true;
  is_prime[2] = true;
  for (int i = 3; i <= 1000; i += 2) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < N; j += i)
        is_prime[j] = false;
    }
  }

  int n, x;
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;
    int v;
    bool dp[x + 1];
    for (int i = 0; i <= x; i++)
      dp[i] = false;

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


Error: 0

Fixed submission:

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

int prime[MAX_N + 1];
bool is_prime[MAX_N + 1];

int main() {
  int p = 0;
  for (int i = 0; i <= MAX_N; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= MAX_N; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= MAX_N; j += i)
        is_prime[j] = false;
    }
  }

  int n, x;
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;
    int v[n];
    bool dp[x + 1];
    for (int i = 0; i <= x; i++)
      dp[i] = false;

    for (int i = 0; i < n; i++)
      cin >> v[i];
    dp[v[0]] = true;

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

#include <cstring>
#include <iostream>

constexpr int MAX_X = 1000000, MAX_N = 30;

int value[MAX_N], n, x;
bool dp[MAX_X + 1];
bool is_prime_number[MAX_X + 1];

void initialize();

void solve();

int main() {
  initialize();

  while (true) {
    std::cin >> n >> x;
    if (n == 0 && x == 0) {
      break;
    }

    memset(dp, false, x + 1);

    dp[0] = true;

    for (int i = 0; i < n; ++i) {
      std::cin >> value[i];
      dp[value[i]] = true;
    }

    solve();
  }
  return 0;
}

void solve() {
  int ans = 0;

  for (int i = 2; i < x + 1; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i >= value[j]) {
        if (dp[i - value[j]]) {
          dp[i] = true;

          if (is_prime_number[i]) {
            ans = std::max(ans, i);
          }

          break;
        }
      }
    }
  }

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

void initialize() {
  memset(is_prime_number, true, MAX_X + 1);

  is_prime_number[0] = false, is_prime_number[1] = false;
  for (int i = 2; i * i < MAX_X + 1; ++i) {
    if (is_prime_number[i]) {
      for (int j = 2; i * j < MAX_X + 1; ++j) {
        is_prime_number[i * j] = false;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits>

constexpr int MAX_X = 1000001;
constexpr int MAX_N = 31;

int n, x, values[MAX_N];
bool is_prime_number[MAX_X] = {true};

void initialize();

void solve();

int main() {
  initialize();
  while (true) {
    memset(values, 0, MAX_N);

    std::cin >> n >> x;

    if (n + x == 0)
      break;

    for (int i = 0; i < n; ++i)
      std::cin >> values[i];

    solve();
  }
  return 0;
}

void initialize() {
  memset(is_prime_number, true, MAX_X);
  is_prime_number[0] = false, is_prime_number[1] = false;
  for (int i = 2; i <= 1000; ++i) {
    if (is_prime_number[i]) {
      for (int j = i * 2; j < MAX_X; ++j) {
        if (j % i == 0)
          is_prime_number[j] = false;
      }
    }
  }
}

void solve() {
  int ans = 0;
  int dp[MAX_X];
  memset(dp, 0, MAX_X);

  /*
  std::cout << "x : " << x << std::endl << "n : " << n << std::endl;
  for(int i = 0; i < (x > 1000 ? 1000 : x); ++i){
      std::cout << i << " dp : " << dp[i] << std::endl;
  }
  */

  for (int i = 1; i <= x; ++i) {
    int res = 0;
    for (int j = 0; j < n; ++j) {
      if (values[j] <= i) {
        res = std::max(dp[i - values[j]] + values[j], res);
      }
    }
    dp[i] = std::max(dp[i], res);
    if (is_prime_number[dp[i]]) {
      ans = std::max(dp[i], ans);
    }
  }

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

#include <stdio.h>
int n, x;
int kin;
int i, j;
int dp[1000050], flg[1000050];
int main(void) {
  scanf("%d %d", &n, &x);
  for (i = 1; i <= 1000000; i++) {
    flg[i] = 1;
  }
  for (i = 2; i * i <= 1000000; i++) {
    if (flg[i] == 1) {
      for (j = 2; j * i <= 1000000; j++) {
        flg[i * j] = 0;
      }
    }
  }
  while (n != 0 && x != 0) {
    for (j = 1; j <= x; j++) {
      dp[j] = -1;
    }
    dp[0] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &kin);
      for (j = 0; j <= x; j++) {
        if (dp[j] != -1 && j + kin <= x) {
          dp[j + kin] = 1;
        }
      }
    }
    int max = 0;
    for (i = 0; i <= x; i++) {
      if (dp[i] == 1 && flg[i] == 1) {
        max = i;
      }
    }
    if (max == 0) {
      printf("NA\\n");
    } else {
      printf("%d\\n", max);
    }
    scanf("%d %d", &n, &x);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int n, x;
int kin;
int i, j;
int dp[1000050], flg[1000050];
int main(void) {
  scanf("%d %d", &n, &x);
  while (n != 0 && x != 0) {
    for (i = 1; i <= 1000001; i++) {
      flg[i] = 1;
    }
    for (i = 2; i * i <= x; i++) {
      if (flg[i] == 1) {
        for (j = 2; j * i <= x; j++) {
          flg[i * j] = 0;
        }
      }
    }
    for (j = 0; j <= x; j++) {
      dp[j] = -1;
    }
    dp[0] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &kin);
      for (j = 0; j <= x; j++) {
        if (dp[j] != -1) {
          dp[j + kin] = 1;
        }
      }
    }
    int max = 0;
    for (i = 0; i <= x; i++) {
      if (dp[i] == 1 && flg[i] == 1) {
        max = i;
      }
    }
    if (max == 0) {
      printf("NA\\n");
    } else {
      printf("%d\\n", max);
    }
    scanf("%d %d", &n, &x);
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int is[1000001];
bool isprime[1000001];

int main() {

  isprime[0] = isprime[1] = true;
  for (int i = 4; i <= 1000000; i += 2)
    isprime[i] = true;
  for (int i = 3; i * i <= 1000000; i += 2)
    for (int j = 2; i * j <= 1000000; ++j)
      isprime[i * j] = true;

  int n, x, s;
  while (cin >> n >> x, n | x) {
    memset(is, -1, sizeof(is));
    is[0] = 0;
    for (int i = 1; i <= n; ++i) {
      cin >> s;
      for (int j = 0; j <= x; ++j) {
        if (is[j] >= i)
          continue;
        if (is[j] >= 0) {
          for (int k = 1; j + s * k <= x; ++k)
            is[j + s * k] = i;
        }
      }
    }

    for (int j = x; j >= 0; --j) {
      if (is[j] >= 0 && !isprime[j]) {
        cout << j << endl;
        goto NEXT;
      }
    }
    cout << "NA" << endl;
  NEXT:;
  }
}

Error: TLE

Fixed submission:

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

bool is[1000001];
bool isprime[1000001];

int main() {

  isprime[0] = isprime[1] = true;
  for (int i = 4; i <= 1000000; i += 2)
    isprime[i] = true;
  for (int i = 3; i * i <= 1000000; i += 2)
    for (int j = 2; i * j <= 1000000; ++j)
      isprime[i * j] = true;

  int n, x, s;
  while (cin >> n >> x, n | x) {
    memset(is, false, sizeof(is));
    is[0] = true;
    for (int i = 0; i < n; ++i) {
      cin >> s;
      for (int j = 1000000; j >= 0; --j) {
        if (is[j]) {
          for (int k = 1; j + s * k <= x; ++k)
            is[j + s * k] = true;
        }
      }
    }

    for (int j = x; j >= 0; --j) {
      if (is[j] && !isprime[j]) {
        cout << j << endl;
        goto NEXT;
      }
    }
    cout << "NA" << endl;
  NEXT:;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>

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

bool is_prime(int n) {
  if (n % 2 == 0)
    return false;
  for (int i = 3; i <= std::sqrt(n); i += 2)
    if (n % i == 0)
      return false;
  return true;
}

int main() {
  int n, x;
  while (std::cin >> n >> x && n > 0 && x > 0) {
    auto course = new bool[x + 1]();
    rep(i, x + 1) course[i] = false;

    int prices[n];
    rep(i, n) { std::cin >> prices[i]; }
    course[0] = true;

    int max = -1;
    rep(i, x + 1) rep(j, n) {
      if (i - prices[j] >= 0 && course[i - prices[j]]) {
        if (is_prime(i))
          max = i;
        course[i] = true;
        break;
      }
    }
    if (max < 0)
      std::cout << "NA" << std::endl;
    else
      std::cout << max << std::endl;
    delete[] course;
  }
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>

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

bool is_prime(int n) {
  if (n % 2 == 0)
    return false;
  for (int i = 3; i <= std::sqrt(n); i += 2)
    if (n % i == 0)
      return false;
  return true;
}

int main() {
  int n, x;
  while (std::cin >> n >> x && n > 0 && x > 0) {
    auto course = new bool[x + 1]();
    rep(i, x + 1) course[i] = false;

    int prices[n];
    rep(i, n) { std::cin >> prices[i]; }
    course[0] = true;

    int max = -1;
    rep(i, x + 1) rep(j, n) {
      if (i - prices[j] >= 0 && course[i - prices[j]]) {
        if (is_prime(i))
          max = i;
        course[i] = true;
        break;
      }
    }
    if (max < 0)
      std::cout << "NA" << std::endl;
    else
      std::cout << max << std::endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

bool memo[1000001];
int n, x, m, price[30];

bool sosuu(int k) {
  if (k == 2)
    return true;
  else if (k < 2)
    return false;
  else if (k % 2 == 0)
    return false;
  for (int i = 3; i * i <= k; i += 2)
    if (k % i == 0)
      return false;
  return true;
}
int next() {
  memo[0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = price[i]; j <= x; j++)
      memo[j] |= memo[j - price[i]];
  }
}

int main() {
  while (cin >> n >> x, n || x) {
    for (int i = 0; i < n; ++i)
      cin >> price[i];
    for (int i = 0; i < 1000001; ++i)
      memo[i] = 0;
    m = 0;
    memo[0] = true;
    for (int i = 0; i < n; i++)
      for (int j = price[i]; j <= x; j++)
        memo[j] |= memo[j - price[i]];

    for (int i = x; i; --i)
      if (memo[i] && sosuu(i)) {
        m = i;
        break;
      }

    if (m)
      cout << m << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

bool flag[1000001];
int n, x, m, price[30];

bool sosuu(int k) {
  if (k == 2)
    return true;
  else if (k < 2)
    return false;
  else if (k % 2 == 0)
    return false;
  for (int i = 3; i * i <= k; i += 2)
    if (k % i == 0)
      return false;
  return true;
}
void next(int k) {
  if (flag[k])
    return;
  flag[k] = true;
  if (k > x)
    return;
  if (k > m)
    if (sosuu(k))
      m = k;
  for (int i = 0; i < n; ++i)
    next(k + price[i]);
}

int main() {
  while (cin >> n >> x, n || x) {
    for (int i = 0; i < 1000001; ++i)
      flag[i] = false;
    for (int i = 0; i < n; ++i)
      cin >> price[i];
    m = 0;
    next(0);
    if (m)
      cout << m << endl;
    else
      cout << "NA" << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error


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

using namespace std;

const int N = 1000000;

int prime[N + 1];
int table[N + 1];
int dish[30];
int n, x;
int max_val;

void setPrime() {
  memset(&prime[0], 0, sizeof(int) * (N + 1));
  prime[2] = 1;
  for (int i = 2; i <= N; i++) {
    bool is_prime = true;
    int limit = sqrt(i);
    for (int k = 2; k <= limit; k++) {
      if (i % k == 0) {
        is_prime = false;
        break;
      }
    }

    if (is_prime) {
      prime[i] = 1;
    }
  }
}

void solve() {
  int price;
  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    price = q.front();
    q.pop();

    if (price > x) {
      continue;
    }

    if (prime[price] == 1 && max_val < price) {
      max_val = price;
    }

    for (int k = 0; k < n; k++) {
      int new_price = price + dish[k];
      if (new_price > N)
        continue;
      if (table[new_price] == 0) {
        table[new_price] = 1;
        q.push(new_price);
      }
    }
  }
}

int main() {
  setPrime();

  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0) {
      break;
    }

    clock_t t = clock();
    for (int i = 0; i < n; i++) {
      cin >> dish[i];
    }

    memset(&table[0], 0, sizeof(int) * (N + 1));
    max_val = 0;
    solve();

    if (max_val != 0) {
      printf("%d\\n", max_val);
    } else {
      printf("NA\\n");
    }
  }
}

Error: 0

Fixed submission:

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

using namespace std;

const int N = 1000000;

int prime[N + 1];
int table[N + 1];
int dish[30];
int n, x;
int max_val;

void setPrime() {
  memset(&prime[0], 0, sizeof(int) * (N + 1));
  prime[2] = 1;
  for (int i = 2; i <= N; i++) {
    bool is_prime = true;
    int limit = sqrt(i);
    for (int k = 2; k <= limit; k++) {
      if (i % k == 0) {
        is_prime = false;
        break;
      }
    }

    if (is_prime) {
      prime[i] = 1;
    }
  }
}

void solve() {
  int price;
  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    price = q.front();
    q.pop();

    if (price > x) {
      continue;
    }

    if (prime[price] == 1 && max_val < price) {
      max_val = price;
    }

    for (int k = 0; k < n; k++) {
      int new_price = price + dish[k];
      if (table[new_price] == 0) {
        table[new_price] = 1;
        q.push(new_price);
      }
    }
  }
}

int main() {
  setPrime();

  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0) {
      break;
    }

    clock_t t = clock();
    for (int i = 0; i < n; i++) {
      cin >> dish[i];
    }

    memset(&table[0], 0, sizeof(int) * (N + 1));
    max_val = 0;
    solve();

    if (max_val != 0) {
      printf("%d\\n", max_val);
    } else {
      printf("NA\\n");
    }
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 1000001;
char p[MAX] = {0};
int a[31], n, x;

void solve(int dp[MAX]) {

  for (int i = 0; i < x; i++) {
    if (dp[i]) {
      for (int j = 0; j < n; j++) {
        if (i + a[j] <= x) {
          dp[i + a[j]] = 1;
        }
      }
    }
  }
}

int main() {
  for (int i = 2; i < MAX; i++)
    p[i] = 1;
  for (int i = 2; i < sqrt(MAX) + 1; i++) {
    if (p[i]) {
      for (int j = i + i; j < MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  while (cin >> n >> x, n || x) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

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

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

Error: 0

Fixed submission:

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

const int MAX = 2000000;
char p[MAX + 1] = {0};
int a[31];
int n, x, ans;

void solve(int sum, char dp[MAX + 1]) {
  if (sum > x)
    return;

  for (int i = 0; i < n; i++) {
    if (dp[sum + a[i]] == 0) {
      dp[sum + a[i]] = 1;
      solve(sum + a[i], dp);
    }
  }
}

int main() {
  for (int i = 2; i < MAX + 1; i++)
    p[i] = 1;
  for (int i = 2; i * i < MAX + 1; i++) {
    if (p[i]) {
      for (int j = i + i; j < MAX + 1; j += i) {
        p[j] = 0;
      }
    }
  }

  while (cin >> n >> x, n || x) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    ans = 0;
    char dp[MAX + 1] = {0};
    solve(0, dp);
    for (int i = x; i > 0; i--) {
      if (dp[i] && p[i]) {
        ans = i;
        break;
      }
    }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int dp[1000001], flg[1000001];
int main(void) {
  for (int i = 2; i <= 1000000; i++)
    flg[i] = 1;
  for (int i = 2; i * i <= 1000000; i++) {
    if (flg[i] == 1) {
      for (int j = 2; j * i < 1000000; j++)
        flg[i * j] = 0;
    }
  }
  int kin[30], n, x;
  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> kin[i];
    for (int i = 0; i <= x; i++)
      dp[i] = 0;
    dp[0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= x; j++)
        if (dp[j] == 1) {
          if (j + kin[i] <= x)
            dp[j + kin[i]] = 1;
        }
    }

    int no = 0, data;
    for (int i = x; i > 1; i--) {
      if (flg[i] == 1 && dp[i] == 1) {
        data = i;
        no = 1;
        break;
      }
    }
    if (no == 0)
      cout << "NA" << endl;
    else
      cout << data << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
int dp[1000001], flg[1000001];
using namespace std;

int main(void) {
  for (int i = 2; i <= 1000000; i++)
    flg[i] = 1;
  for (int i = 2; i * i <= 1000000; i++) {
    if (flg[i] == 1) {
      for (int j = 2; j * i < 1000000; j++)
        flg[i * j] = 0;
    }
  }
  int kin[31], n, x;
  int data, no = 0;
  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> kin[i];
    for (int i = 0; i <= x; i++)
      dp[i] = 0;
    dp[0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= x; j++)
        if (dp[j] == 1)
          dp[j + kin[i]] = 1;
    };

    for (int i = x; i > 1; i--) {
      if (flg[i] == 1 && dp[i] == 1) {
        data = i;
        no = 1;
        break;
      }
    }
    if (no == 0)
      cout << "NA" << endl;
    else
      cout << data << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX_P 1000001

int main() {
  bool prime[MAX_P];
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < MAX_P; i++)
    prime[i] = true;
  for (int i = 2; i < MAX_P; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < MAX_P; j += i) {
        prime[j] = false;
      }
    }
  }
  int n, x;
  while (cin >> n >> x, n || x) {
    vector<bool> dp(x + 1);
    for (int i = 0; i <= x; i++)
      dp[i] = false;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    dp[0] = true;
    int max = 0;
    for (int i = 0; i < n; i++) {
      for (int j = p[i]; j <= x; j++) {
        if (dp[j - p[i]]) {
          dp[j] = true;
          if (prime[j] && max < j)
            max = j;
        }
      }
    }
    if (max > 0)
      cout << max << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

#define MAX_P 1000000

int main() {
  bool prime[MAX_P];
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < MAX_P; i++)
    prime[i] = true;
  for (int i = 2; i < MAX_P; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < MAX_P; j += i) {
        prime[j] = false;
      }
    }
  }
  int n, x;
  while (cin >> n >> x, n || x) {
    vector<bool> dp(x + 1);
    for (int i = 0; i <= x; i++)
      dp[i] = false;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    dp[0] = true;
    int max = 0;
    for (int i = 0; i <= x; i++) {
      if (dp[i]) {
        if (prime[i] && max < i)
          max = i;
        for (int j = 0; j < n; j++) {
          dp[i + p[j]] = true;
        }
      }
    }
    if (max > 0)
      cout << max << endl;
    else
      cout << "NA" << endl;
  }
}