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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
const int MAX = 1000010;
// bool isPrime[MAX] = {0};
bitset<MAX> isPrime;

void sieve() {
  bitset<MAX> find;
  int i;
  for (i = 2; i * i < MAX; i++) {
    if (find[i])
      continue;
    isPrime.set(i);
    for (int j = i; j < MAX; j++) {
      if (j % i == 0) {
        find.set(j);
      }
    }
  }
  for (; i < MAX; i++) {
    if (!find[i]) {
      // isPrime[i] = true;
      isPrime.set(i);
    }
  }
}

int main() {
  sieve();

  while (true) {
    int n, x;
    cin >> n >> x;
    if (n == 0 && x == 0)
      return 0;
    vector<int> R(n);
    for (int i = 0; i < n; i++) {
      cin >> R[i];
    }

    bitset<MAX> S;
    S.set(0);
    // set<int> S;
    // S.insert(0);
    for (int i = 0; i < R.size(); i++) {
      //    for(set<int>::iterator it=S.begin();it!=S.end();++it){
      for (int j = 0; j <= x; j++) {
        if (!S[j])
          continue;
        // int newe = *it + R[i];
        int newe = j + R[i];
        if (newe > x)
          continue;
        // S.insert(newe);
        if (S[j]) {
          S.set(newe);
        }
      }
    }
    int maxn = -1;
    /*
    for(set<int>::iterator it=S.begin();it!=S.end();++it){
        if(isPrime[*it] && *it > maxn) maxn = *it;
    }
    */
    for (int i = 0; i <= x; i++) {
      if (isPrime[i] && S[i])
        maxn = i;
    }
    if (maxn == -1)
      cout << "NA" << endl;
    else
      cout << maxn << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
const int MAX = 1000000;
// bool isPrime[MAX] = {0};
bitset<MAX> isPrime;

void sieve() {
  bitset<MAX> find;
  int i;
  for (i = 2; i * i < MAX; i++) {
    if (find[i])
      continue;
    isPrime.set(i);
    for (int j = i; j < MAX; j++) {
      if (j % i == 0) {
        find.set(j);
      }
    }
  }
  for (; i < MAX; i++) {
    if (!find[i]) {
      // isPrime[i] = true;
      isPrime.set(i);
    }
  }
}

int main() {
  sieve();

  while (true) {
    int n, x;
    cin >> n >> x;
    if (n == 0 && x == 0)
      return 0;
    vector<int> R(n);
    for (int i = 0; i < n; i++) {
      cin >> R[i];
    }

    set<int> S;
    S.insert(0);
    for (int i = 0; i < R.size(); i++) {
      for (set<int>::iterator it = S.begin(); it != S.end(); ++it) {
        int newe = *it + R[i];
        if (newe > x)
          continue;
        S.insert(newe);
      }
    }
    int maxn = -1;
    for (set<int>::iterator it = S.begin(); it != S.end(); ++it) {
      if (isPrime[*it] && *it > maxn)
        maxn = *it;
    }
    if (maxn == -1)
      cout << "NA" << endl;
    else
      cout << maxn << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, x, ans;
int t[30];
char dp[1000001];
char p[1000001];

void mkPrime(void) {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;

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

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

  for (int i = 0; i <= x; i++) {
    if (dp[i] && p[i] && i > ans)
      ans = i;
  }
}

int main(void) {
  mkPrime();

  while (cin >> n >> x && (n || x)) {
    for (int i = 0; i < n; i++)
      cin >> t[i];

    memset(dp, 0, sizeof(dp));
    ans = 0;

    solve();

    if (ans == 0)
      cout << "NA\\n";
    else
      cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int n, x, ans;
int t[30];
char dp[1000001];
char p[1000001];

void mkPrime(void) {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;

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

void solve(int sum, int idx) {
  if (dp[sum])
    return;
  if (idx == n)
    return;

  dp[sum] = 1;
  if (p[sum] && ans < sum)
    ans = sum;

  for (int i = 0;; i++) {
    int nsum = sum + i * t[idx];
    if (nsum > x)
      return;
    solve(nsum, idx + 1);
  }
}

int main(void) {
  mkPrime();

  while (cin >> n >> x && (n || x)) {
    for (int i = 0; i < n; i++)
      cin >> t[i];

    memset(dp, 0, sizeof(dp));
    ans = 0;

    solve(0, 0);

    if (ans == 0)
      cout << "NA\\n";
    else
      cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))
#define X 1000001
int ans;
int n, x;
bool p[X];
char dp[2 * X];

int main() {
  p[0] = 1;
  reps(i, 2, X) if (!p[i]) for (int j = i + i; j < X; j += i) p[j] = 1;
  for (;;) {
    scanf("%d%d", &n, &x);
    if (!n)
      break;
    x++;
    vector<int> v(n);
    rep(i, n) { cin >> v[i]; }
    fill_n(dp, X, 0);
    dp[0] = 1;
    ans = 0;
    rep(i, n) {
      rep(j, x) {
        if (dp[j] && j + v[i] < x) {
          dp[j + v[i]] = 1;
          if (!p[j + v[i]])
            ans = max(ans, j + v[i]);
        }
      }
    }
    if (ans)
      printf("%d\\n", ans);
    else
      puts("NA");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))
#define X 1000001
int ans;
int n, x, v[30];
bool p[X];
char dp[31][X];

int main() {
  p[0] = 1;
  reps(i, 2, X) if (!p[i]) for (int j = i + i; j < X; j += i) p[j] = 1;
  for (;;) {
    scanf("%d%d", &n, &x);
    if (!n)
      break;
    x++;
    rep(i, n) { scanf("%d", v + i); }
    fill_n(dp[0], 31 * X, 0);
    dp[0][0] = 1;
    rep(i, n) {
      rep(j, x) {
        if (dp[i][j])
          for (int k = 0; j + k * v[i] < x; k++) {
            dp[i + 1][j + k * v[i]] = 1;
          }
      }
    }
    ans = 0;
    rep(j, x) {
      if (dp[n][j] && !p[j])
        ans = max(ans, j);
    }
    if (ans)
      printf("%d\\n", ans);
    else
      puts("NA");
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
bool dp[5000000];
int menu[30];
int main() {
  while (1) {
    int n, x;
    cin >> n >> x;
    if (!n)
      return 0;
    for (int i = 0; i < n; ++i) {
      cin >> menu[i];
    }
    fill(dp, dp + 5000000, false);
    dp[0] = true;
    for (int i = 0; i <= x; ++i) {
      if (dp[i]) {
        for (int j = 0; j < n; ++j) {
          dp[i + menu[j]] = true;
        }
      }
    }
    int m = 0;
    for (int i = x; i >= 0; --i) {
      bool flag = true;
      if (dp[i]) {
        for (int j = 2; j * j <= i; ++j) {
          if (!(i % j)) {
            flag = false;
          }
        }
        if (flag) {
          m = i;
          break;
        }
      }
    }
    if (m)
      cout << m << endl;
    else
      cout << "NA" << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
bool dp[1000010];
int menu[30];
int main() {
  while (1) {
    int n, x;
    cin >> n >> x;
    if (!n)
      return 0;
    for (int i = 0; i < n; ++i) {
      cin >> menu[i];
    }
    fill(dp, dp + 1000010, false);
    dp[0] = true;
    for (int i = 0; i <= x; ++i) {
      if (dp[i]) {
        for (int j = 0; j < n; ++j) {
          dp[i + menu[j]] = true;
        }
      }
    }
    int m = 0;
    for (int i = x; i >= 0; --i) {
      bool flag = true;
      if (dp[i]) {
        for (int j = 2; j * j <= i; ++j) {
          if (!(i % j)) {
            flag = false;
          }
        }
        if (flag) {
          m = i;
          break;
        }
      }
    }
    if (m)
      cout << m << endl;
    else
      cout << "NA" << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define M 1000001
bool p[M];
int dish[31];
int dp[M];
int n, x;
int main() {
  rep(i, M) p[i] = true;
  p[0] = p[1] = false;
  for (int i = 2; i * i < M; ++i)
    for (int j = 2 * i; j < M; j += i)
      if (p[j])
        p[j] = false;

  while (true) {
    scanf("%d%d", &n, &x);
    if ((n | x) == 0)
      break;
    rep(i, n) scanf("%d", dish + i);

    rep(j, x + 1) dp[j] = -1;
    dp[0] = (1 << 20);

    int k = 1;
    rep(i, n) REP(j, 1, x + 1) {
      if (dp[j] >= 0)
        dp[j] = (1 << 20);
      else if (j < dish[i] || dp[j - dish[i]] <= 0)
        dp[j] = -1;
      else
        dp[j] = dp[j - dish[i]] - 1;
    }
    int res = 0;
    rep(j, x + 1) if (p[j] && dp[j] >= 0) res = j;
    if (res > 0)
      printf("%d\\n", res);
    else
      puts("NA");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define M 1000001
bool p[M];
int dish[31];
int dp[31][M];
int n, x;
int main() {
  rep(i, M) p[i] = true;
  p[0] = p[1] = false;
  for (int i = 2; i * i < M; ++i)
    for (int j = 2 * i; j < M; j += i)
      if (p[j])
        p[j] = false;

  while (true) {
    scanf("%d%d", &n, &x);
    if ((n | x) == 0)
      break;
    rep(i, n) scanf("%d", dish + i);

    rep(i, 31) dp[i][0] = (1 << 20);
    rep(j, x + 1) dp[0][j] = -1;

    REP(i, 1, n + 1) REP(j, 1, x + 1) {
      if (dp[i - 1][j] >= 0)
        dp[i][j] = (1 << 20);
      else if (j >= dish[i - 1] && dp[i - 1][j - dish[i - 1]] > 0)
        dp[i][j] = dp[i - 1][j - dish[i - 1]] - 1;
      else if (j >= dish[i - 1] && dp[i][j - dish[i - 1]] > 0)
        dp[i][j] = dp[i][j - dish[i - 1]] - 1;
      else
        dp[i][j] = -1;
    }
    int res = 0;
    rep(j, x + 1) if (p[j] && dp[n][j] >= 0) res = j;
    if (res > 0)
      printf("%d\\n", res);
    else
      puts("NA");
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

const int LIMIT = 1000000;

int main() {
  bool *prime = new bool[LIMIT + 1]();
  prime[0] = prime[1] = true;
  for (int i = 2; i < LIMIT + 1; ++i) {
    if (prime[i])
      continue;
    for (int j = i + i; j < LIMIT + 1; j += i) {
      prime[j] = true;
    }
  }

  int n, x;
  while (cin >> n >> x && (n || x)) {
    bool *price = new bool[x + 1]();
    set<int> menu;
    for (int i = 0; i < n; ++i) {
      int p;
      cin >> p;
      if (p <= x) {
        menu.insert(p);
        price[p] = true;
      }
    }
    int ans = 0;
    for (int i = 0; i <= x; ++i) {
      if (!price[i])
        continue;
      if (!prime[i]) {
        ans = i;
      }
      set<int>::iterator itr = menu.begin();
      while (itr != menu.end()) {
        int a = i + *itr;
        if (a <= x) {
          price[a] = true;
        }
        ++itr;
      }
    }

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

    delete[] price;
  }

  delete[] prime;

  return 0;
}

Error: 0

Fixed submission:

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

const int LIMIT = 1000000;

int main() {
  bool prime[LIMIT + 1];
  memset(prime, false, sizeof(prime));
  prime[0] = prime[1] = true;
  for (int i = 2; i < LIMIT + 1; ++i) {
    if (prime[i])
      continue;
    for (int j = i + i; j < LIMIT + 1; j += i) {
      prime[j] = true;
    }
  }

  int n, x;
  while (cin >> n >> x && (n || x)) {
    bool *price = new bool[x + 1]();
    set<int> menu;
    for (int i = 0; i < n; ++i) {
      int p;
      cin >> p;
      if (p <= LIMIT) {
        menu.insert(p);
        price[p] = true;
      }
    }
    int ans = 0;
    for (int i = 0; i <= x; ++i) {
      if (!price[i])
        continue;
      if (!prime[i]) {
        ans = i;
      }
      set<int>::iterator itr = menu.begin();
      while (itr != menu.end()) {
        int a = i + *itr;
        if (a <= x) {
          price[a] = true;
        }
        ++itr;
      }
    }

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

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

#include <algorithm>
#include <array>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_PAY = 1e6 + 10;
array<bool, MAX_PAY> PrimeTable;

void makePrimeTable() {
  fill(PrimeTable.begin(), PrimeTable.end(), true);

  PrimeTable.at(0) = PrimeTable.at(1) = false;

  for (int i = 2; i * i <= MAX_PAY; ++i) {
    for (int j = 2; j * i < MAX_PAY; ++j) {
      PrimeTable.at(j * i) = false;
    }
  }
}

int main() {
  makePrimeTable();

  int cook_num, max_pay;

  while (cin >> cook_num >> max_pay) {
    // input
    if (cook_num == 0 && max_pay == 0)
      break;

    vector<int> dish(cook_num);
    vector<bool> dp(MAX_PAY);

    for (auto &it : dish) {
      cin >> it;
      dp.at(it) = true;
    }

    // solve
    for (int i = 0; i < cook_num; ++i) {
      for (int j = 0; j <= max_pay; ++j) {
        if (dp.at(j) && j + dish.at(i) <= max_pay) {
          try {
            dp.at(j + dish.at(i)) = true;
          } catch (...) {
            cout << j + dish.at(i) << endl;
          }
        }
      }
    }

    // return max_ans
    int max_ans = 0;
    for (int temp = 0; temp <= max_pay; ++temp) {
      if (dp.at(temp) && PrimeTable.at(temp))
        max_ans = temp;
    }

    // output
    if (max_ans) {
      cout << max_ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  array::at: __n (which is 1000010) >= _Nm (which is 1000010)


Fixed submission:

#include <algorithm>
#include <array>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_PAY = 1e6 + 10;
array<bool, MAX_PAY> PrimeTable;

void makePrimeTable() {
  fill(PrimeTable.begin(), PrimeTable.end(), true);

  for (int i = 2; i * i < MAX_PAY; ++i) {
    for (int j = 2; j * i <= MAX_PAY; ++j) {
      PrimeTable.at(j * i) = false;
    }
  }
}

int main() {
  makePrimeTable();

  int cook_num, max_pay;

  while (cin >> cook_num >> max_pay) {
    // input
    if (cook_num == 0 && max_pay == 0)
      break;

    vector<int> dish(cook_num);
    vector<bool> dp(max_pay);

    for (auto &it : dish) {
      cin >> it;
      dp.at(it) = true;
    }

    // solve
    for (int i = 0; i < cook_num; ++i) {
      for (int j = 0; j < max_pay; ++j) {
        if (dp.at(j) && j + dish.at(i) <= max_pay) {
          dp.at(j + dish.at(i)) = true;
        }
      }
    }

    // return max_ans
    int max_ans = 0;
    for (int temp = 0; temp <= max_pay; ++temp) {
      if (dp.at(temp) && PrimeTable.at(temp))
        max_ans = temp;
    }

    switch (max_ans) {
    case 0:
      cout << "NA" << endl;
      break;
    defalut:
      cout << max_ans << endl;
    }
  }

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

#include <iostream>
using namespace std;
int n, budget, price[30];
int dp[1000001];
bool is_prime[1000001];

void sieve() {
  for (int i = 0; i < 1000001; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;

  for (int i = 0; i * i < 1000001; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < 1000001; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

void solve() {
  for (int j = 0; j <= budget; j++) {
    dp[j] = 0;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= budget; j++) {
      if (j - price[i] >= 0) {
        dp[j] = max(dp[j], dp[j - price[i]] + price[i]);
      } else {
        dp[j] = dp[j];
      }
    }
  }
  for (int i = 0; i < budget; i++) {
    if (is_prime[dp[budget - i]]) {
      cout << dp[budget - i] << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  sieve();
  while (cin >> n >> budget) {
    if (n == 0 && budget == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> price[i];
    }
    solve();
  }
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
int n, budget, price[30];
int dp[31][1000001];
bool is_prime[1000001];

void sieve() {
  for (int i = 0; i < 1000001; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;

  for (int i = 0; i * i < 1000001; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < 1000001; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

void solve() {
  for (int j = 0; j <= budget; j++) {
    dp[0][j] = 0;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= budget; j++) {
      if (j - price[i] >= 0) {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - price[i]] + price[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  for (int i = 0; i < budget; i++) {
    if (is_prime[dp[n][budget - i]]) {
      cout << dp[n][budget - i] << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  sieve();
  while (cin >> n >> budget) {
    if (n == 0 && budget == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> price[i];
    }
    solve();
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>

const int Max = 1000001;

int dishes[31];
bool dish[Max];
bool isPrime[Max];

int main() {

  for (int i = 2; i < Max; i++)
    isPrime[i] = true;
  for (int i = 2; i < std::sqrt(Max); i++) {
    if (isPrime[i]) {
      for (int j = i * 2; j < Max; j += i)
        isPrime[j] = false;
    }
  }

  int n, x;
  while (std::cin >> n, n) {

    std::cin >> x;
    memset(dish, 0, sizeof(dish));

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

    dish[0] = true;
    for (int i = 0; i < x; i++) {
      if (dish[i]) {
        for (int j = 0; j < n; j++) {
          if (i + dishes[j] <= Max)
            dish[i + dishes[j]] = true;
        }
      }
    }

    for (int i = x; i >= 0; i--) {
      if (dish[i] && isPrime[i]) {
        std::cout << i << std::endl;
        break;
      }
      if (i == 0)
        std::cout << "NA" << std::endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>

const int Max = 1000001;

int dishes[31];
bool dish[Max];
bool isPrime[Max];

int main() {

  for (int i = 2; i < Max; i++)
    isPrime[i] = true;
  for (int i = 2; i < std::sqrt(Max); i++) {
    if (isPrime[i]) {
      for (int j = i * 2; j < Max; j += i)
        isPrime[j] = false;
    }
  }

  int n, x;
  while (std::cin >> n, n) {

    std::cin >> x;
    memset(dish, 0, sizeof(dish));

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

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

    for (int i = x; i >= 0; i--) {
      if (dish[i] && isPrime[i]) {
        std::cout << i << std::endl;
        break;
      }
      if (i == 0)
        std::cout << "NA" << std::endl;
    }
  }

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

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

bool prime[MAX];
bool order[MAX + 1];
int menu[30];
int n, money;

void init() {
  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (prime[i]) {
      for (int j = 2; j * i < MAX; j++)
        prime[j * i] = false;
    }
  }
}
void search() {
  queue<int> que;
  int m, l;
  que.push(0);
  while (!que.empty()) {
    m = que.front();
    que.pop();
    for (int i = 0; i < n; i++) {
      l = m + menu[i];
      if (l <= money && !order[l]) {
        order[l] = true;
        que.push(l);
      }
    }
  }
}
int main() {
  init();
  while (cin >> n >> money, n | money) {
    for (int i = 0; i < n; i++)
      cin >> menu[i];
    fill(order, order + (MAX + 1), false);
    search();
    int ans = -1;
    for (int i = money; i > 1; i--) {
      if (prime[i] && order[i]) {
        ans = i;
        break;
      }
    }
    if (ans != -1)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

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

bool prime[MAX];
bool order[MAX + 1];
int menu[30];
int n, money;

void init() {
  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (prime[i]) {
      for (int j = 2; j * i < MAX; j++)
        prime[j * i] = false;
    }
  }
}
void search() {
  queue<int> que;
  int m;
  que.push(0);
  while (!que.empty()) {
    m = que.front();
    que.pop();
    if (money < m || order[m])
      continue;
    order[m] = true;
    for (int i = 0; i < n; i++)
      que.push(m + menu[i]);
  }
}
int main() {
  init();
  while (cin >> n >> money, n | money) {
    for (int i = 0; i < n; i++)
      cin >> menu[i];
    fill(order, order + (MAX + 1), false);
    search();
    int ans = -1;
    for (int i = money; i > 1; i--) {
      if (prime[i] && order[i]) {
        ans = i;
        break;
      }
    }
    if (ans != -1)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}