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

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, int> dp;
map<string, int> buy;
map<string, vector<string>> reci;

const int INF = 1000000;

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

int solve(string to) {
  int rtn;
  if (dp[to] == INF) {
    if (reci[to].size() != 0) {
      int ari = 0;
      rep(i, reci[to].size()) { ari += solve(reci[to][i]); }
      rtn = min(buy[to], ari);
    } else {
      rtn = buy[to];
    }
    dp[to] = rtn;
  } else {
    rtn = dp[to];
  }
  return rtn;
}

int main() {
  int n;
  while (cin >> n, n) {
    dp.clear();
    buy.clear();
    reci.clear();
    rep(i, n) {
      string str;
      int p;
      cin >> str >> p;
      buy[str] = p;
      dp[str] = INF;
    }
    int r;
    cin >> r;
    rep(i, r) {
      string str;
      int m;
      cin >> str >> m;
      rep(j, m) {
        string ing;
        cin >> ing;
        reci[str].push_back(ing);
      }
    }

    string to;
    cin >> to;
    cout << solve(to) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, int> dp;
map<string, int> buy;
map<string, vector<string>> reci;

const int INF = 1000000;

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

int solve(string to) {
  int ari = 0;
  rep(i, reci[to].size()) { ari += solve(reci[to][i]); }
  if (ari == 0) {
    ari = buy[to];
  }

  int rtn = min(ari, dp[to]);
  dp[to] = rtn;
  return rtn;
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, n) {
      string str;
      int p;
      cin >> str >> p;
      buy[str] = p;
      dp[str] = INF;
    }
    int r;
    cin >> r;
    rep(i, r) {
      string str;
      int m;
      cin >> str >> m;
      rep(j, m) {
        string ing;
        cin >> ing;
        reci[str].push_back(ing);
      }
    }

    // solve

    string to;
    cin >> to;
    cout << solve(to) << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, b, n) for (int i = b; i < n; i++)
#define RFOR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

#define LIMIT 1000010
using namespace std;

bool isnotprime[LIMIT] = {0};
bool dp[31][LIMIT] = {0};
int n, x;
int dishes[31];

void erathos() {
  CLR(isnotprime);

  FOR(i, 2, LIMIT) {
    if (!isnotprime[i]) {
      for (int j = 2; i * j < LIMIT; j++) {
        isnotprime[i * j] = true;
      }
    }
  }

  return;
}

int solve() {
  int res = 0;

  CLR(dp);

  FOR(j, 0, n) {
    for (int i = dishes[j]; i <= x; i += dishes[j]) {
      dp[j][i] = true;
    }

    if (j > 0)
      for (int i = 1; i <= x; i++) {
        if ((dp[j - 1][i]) || ((i - dishes[j] >= 0) && dp[j][i - dishes[j]])) {
          dp[j][i] = true;
        }
      }
  }

  RFOR(i, 0, x + 1) {
    if (!isnotprime[i] && dp[n - 1][i]) {
      res = i;
      break;
    }
  }

  return res;
}

int main() {
  erathos();

  while (cin >> n >> x, (n || x)) {
    FOR(i, 0, n) { cin >> dishes[i]; }

    int r = solve();

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, b, n) for (int i = b; i < n; i++)
#define RFOR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

#define LIMIT 1000010
using namespace std;

bool isnotprime[LIMIT] = {0};
bool dp[40][LIMIT] = {0};
int n, x;
int dishes[100];

void erathos() {
  CLR(isnotprime);

  FOR(i, 2, LIMIT) {
    if (!isnotprime[i]) {
      for (int j = 2; i * j < LIMIT; j++) {
        isnotprime[i * j] = true;
      }
    }
  }

  return;
}

int solve() {
  int res = 0;

  CLR(dp);

  FOR(j, 0, n) {
    for (int i = dishes[j]; i <= x; i += dishes[j]) {
      dp[j][i] = true;
    }

    if (j > 0)
      for (int i = 1; i <= x; i++) {
        if ((dp[j - 1][i]) ||
            ((i - dishes[j] > 0) && dp[j - 1][i - dishes[j]])) {
          dp[j][i] = true;
        }
      }
  }

  RFOR(i, 0, x + 1) {
    if (!isnotprime[i] && dp[n - 1][i]) {
      res = i;
      break;
    }
  }

  return res;
}

int main() {
  erathos();

  while (cin >> n >> x, (n || x)) {
    FOR(i, 0, n) { cin >> dishes[i]; }

    int r = solve();

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

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, x, ord[35];
  bool prime[1000000] = {1, 1, 0};

  rep(i, 1000000) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000000; j += i) {
        prime[j] = 1;
      }
    }
  }

  while (cin >> n >> x, n || x) {
    bool total[1000000] = {1};
    rep(i, n) { cin >> ord[i]; }
    rep(i, x) {
      if (total[i]) {
        rep(j, n) {
          if (i + ord[j] > x)
            continue;
          total[i + ord[j]] = 1;
        }
      }
    }
    bool c = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] == 0 && total[i] == 1) {
        cout << i << endl;
        c = false;
        break;
      }
    }
    if (c)
      cout << "NA" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, x, ord[35];
  bool prime[1000000] = {1, 1, 0};

  rep(i, 1000000) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000000; j += i) {
        prime[j] = 1;
      }
    }
  }

  while (cin >> n >> x, n || x) {
    bool total[1000000] = {1};
    rep(i, n) { cin >> ord[i]; }
    rep(i, x) {
      if (total[i]) {
        rep(j, n) { total[i + ord[j]] = 1; }
      }
    }
    bool c = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] == 0 && total[i] == 1) {
        cout << i << endl;
        c = false;
        break;
      }
    }
    if (c)
      cout << "NA" << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

constexpr int MAX_N = 31, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
bool dp[MAX_C];
bool prime_num[MAX_C];

int main() {
  while (1) {
    std::cin >> n >> max;
    if (n + max == 0)
      break;
    std::memset(prime_num, false, sizeof(prime_num));
    std::memset(dp, false, sizeof(dp));
    for (int i = 0; i < n; ++i)
      std::cin >> cost[i];
    for (int i = 2; i * i <= max; ++i) {
      if (!prime_num[i]) {
        for (int j = 2; i * j <= max; ++j)
          prime_num[i * j] = true;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; ++i)
      dp[cost[i]] = true;

    for (int i = 1; i <= MAX_C; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i + cost[j] <= max && dp[i])
          dp[i + cost[j]] = true;
      }
    }

    for (int i = max; i >= 0; --i) {
      if (dp[i] && !prime_num[i]) {
        ans = i;
        break;
      }
    }
    if (!ans)
      std::cout << "NA" << std::endl;
    else
      std::cout << ans << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

constexpr int MAX_N = 31, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
bool dp[MAX_C];
bool prime_num[MAX_C];

int main() {
  while (1) {
    std::cin >> n >> max;
    if (n + max == 0)
      break;
    std::memset(prime_num, false, sizeof(prime_num));
    std::memset(dp, false, sizeof(dp));
    for (int i = 0; i < n; ++i)
      std::cin >> cost[i];
    for (int i = 2; i * i <= max; ++i) {
      if (!prime_num[i]) {
        for (int j = 2; i * j <= max; ++j)
          prime_num[i * j] = true;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; ++i)
      dp[cost[i]] = true;

    for (int i = 1; i <= MAX_C; ++i) {
      for (int j = 0; j < n; ++j) {
        if (dp[i])
          dp[i + cost[j]] = true;
      }
    }

    for (int i = max; i >= 0; --i) {
      if (dp[i] && !prime_num[i]) {
        ans = i;
        break;
      }
    }
    if (!ans)
      std::cout << "NA" << std::endl;
    else
      std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <set>
#include <stdio.h>
using namespace std;
const int N = 1000000;
int main() {
  int n, x, temp;
  bitset<N> b;
  set<int> prime;
  bool f;
  b.reset();
  prime.insert(2);
  for (long long int i = 3; i < 1000000; i += 2)
    if (!b[i]) {
      prime.insert(static_cast<int>(i));
      for (long long int j = i * i; j < 1000000; j += i * 2)
        b[j] = true;
    }
  while (true) {
    scanf("%d%d", &n, &x);
    if (n == 0 && x == 0)
      return 0;
    b.reset();
    b[0] = true;
    while (n--) {
      scanf("%d", &temp);
      while (temp <= x) {
        b |= b << temp;
        temp <<= 1;
      }
    }
    f = true;
    for (set<int>::iterator u = prime.upper_bound(x);
         f && u != prime.begin();) {
      u--;
      if (b[*u]) {
        f = false;
        printf("%d\\n", *u);
      }
    }
    if (f)
      printf("NA\\n");
  }
}


Error: TLE

Fixed submission:

#include <bitset>
#include <set>
#include <stdio.h>
using namespace std;
const int N = 1000000;
int main() {
  int n, x, temp;
  bitset<N> b;
  set<int> prime;
  bool f;
  b.reset();
  prime.insert(2);
  for (long long int i = 3; i < 1000000; i += 2)
    if (!b[i]) {
      prime.insert(static_cast<int>(i));
      for (long long int j = i * i; j < 1000000; j += i * 2)
        b[j] = true;
    }
  while (true) {
    scanf("%d%d", &n, &x);
    if (n == 0 && x == 0)
      return 0;
    b.reset();
    b[0] = true;
    while (n--) {
      scanf("%d", &temp);
      for (int i = temp; i <= x; i += temp)
        b |= b << temp;
    }
    f = true;
    for (set<int>::iterator u = prime.upper_bound(x);
         f && u != prime.begin();) {
      u--;
      if (b[*u]) {
        f = false;
        printf("%d\\n", *u);
      }
    }
    if (f)
      printf("NA\\n");
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
bool dp[1000120];
bool notPrime[1000120];
int main() {
  notPrime[0] = notPrime[1] = true;
  for (int i = 2; i * i < 1000010; i++) {
    for (int j = 2 * i; j < 1000010; j += i) {
      notPrime[j] = true;
    }
  }
  int n, x;
  while (cin >> n >> x, n) {
    dp[0] = true;
    while (n--) {
      int a;
      cin >> a;
      for (int i = 0; i <= x; i++) {
        if (dp[i] && i + a <= 1000010)
          dp[i + a] = true;
      }
    }
    bool flg = false;
    for (int i = x; i >= 2; i--) {
      if (!notPrime[i] & dp[i]) {
        cout << i << endl;
        flg = true;
        break;
      }
    }
    if (!flg)
      cout << "NA" << endl;
    for (int i = 0; i < 1000020; i++)
      dp[i] = false;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
bool dp[1000120];
bool notPrime[1000120];
int main() {
  notPrime[0] = notPrime[1] = true;
  for (int i = 2; i * i < 1000010; i++) {
    for (int j = 2 * i; j < 1000010; j += i) {
      notPrime[j] = true;
    }
  }
  int n, x;
  while (cin >> n >> x, n) {
    dp[0] = true;
    while (n--) {
      int a;
      cin >> a;
      for (int i = 0; i <= x; i++) {
        if (dp[i])
          dp[i + a] = true;
      }
    }
    bool flg = false;
    for (int i = x; i >= 2; i--) {
      if (!notPrime[i] & dp[i]) {
        cout << i << endl;
        flg = true;
        break;
      }
    }
    if (!flg)
      cout << "NA" << endl;
    for (int i = 0; i < 1000020; i++)
      dp[i] = false;
  }
}
Problem p00202 (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, b) range(i, 0, b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

const int kN = 1000005;

void primeNumber(bool prime[kN]) {
  rep(i, kN) prime[i] = 1;
  prime[0] = prime[1] = 0;
  rep(i, kN) {
    if (prime[i]) {
      for (int j = i + i; j < kN; j += i) {
        prime[j] = 0;
      }
    }
  }
}

void primeOfCuisine(int n, int a[40], bool prime[kN]) {
  prime[0] = 1;
  rep(i, n) {
    rep(j, kN) {
      if (prime[j] == 1) {
        if (j + a[i] >= kN)
          continue;
        prime[j + a[i]] = 1;
      }
    }
  }
}

void outputAns(bool a[kN], bool b[kN], int x) {
  for (int i = x; i >= 0; i--) {
    if (a[i] == 1 && b[i] == 1) {
      cout << i << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  int n, x, a[40];
  while (cin >> n >> x, n || x) {
    bool is_prime[kN] = {0};
    bool is_prime_of_cuisine[kN] = {0};
    rep(i, n) cin >> a[i];
    primeNumber(is_prime);
    primeOfCuisine(n, a, is_prime_of_cuisine);
    outputAns(is_prime, is_prime_of_cuisine, x);
  }
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

const int kN = 1000005;

void primeNumber(bool prime[kN]) {
  rep(i, kN) prime[i] = 1;
  prime[0] = prime[1] = 0;
  rep(i, kN) {
    if (prime[i]) {
      for (int j = i + i; j < kN; j += i) {
        prime[j] = 0;
      }
    }
  }
}

void primeOfCuisine(int n, int a[40], bool prime[kN]) {
  prime[0] = 1;
  rep(i, n) {
    rep(j, kN) {
      if (prime[j] == 1) {
        prime[j + a[i]] = 1;
      }
    }
  }
}

void outputAns(bool a[kN], bool b[kN], int x) {
  for (int i = x; i >= 0; i--) {
    if (a[i] == 1 && b[i] == 1) {
      cout << i << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  int n, x, a[40];
  while (cin >> n >> x, n || x) {
    bool is_prime[kN] = {0};
    bool is_prime_of_cuisine[kN] = {0};
    rep(i, n) cin >> a[i];
    primeNumber(is_prime);
    primeOfCuisine(n, a, is_prime_of_cuisine);
    outputAns(is_prime, is_prime_of_cuisine, x);
  }
}
Problem p00202 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int MAX = 1000000;
bool prime[1000001];
bool dp[1000001];

void Init() {
  memset(dp, false, sizeof(dp));
  dp[0] = true;
}

void era() {
  prime[0] = prime[1] = false;
  for (int i = 2; i <= MAX; i++) {
    if (prime[i]) {
      for (int j = i * 2; j <= MAX; j += i)
        prime[j] = false;
    }
  }
}

int main() {
  int n, limit;
  memset(prime, true, sizeof(prime));
  era();
  while (cin >> n >> limit && ((n != 0) || (limit != 0))) {
    Init();
    int maxc = 0;
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      dp[tmp] = true;
      for (int j = 0; j + tmp <= limit; j++) {
        if (dp[j]) {
          dp[j + tmp] = true;
          if (prime[j + tmp] && maxc < j + tmp)
            maxc = j + tmp;
        }
      }
    }
    if (!maxc)
      cout << "NA" << endl;
    else
      cout << maxc << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int MAX = 1000000;
bool prime[1000001];
bool dp[31][1000001];

void Init() {
  for (int i = 0; i <= 30; i++) {
    memset(dp[i], false, sizeof(dp[i]));
    dp[i][0] = true;
  }
}

void era() {
  prime[0] = prime[1] = false;
  for (int i = 2; i <= MAX; i++) {
    if (prime[i]) {
      for (int j = i * 2; j <= MAX; j += i)
        prime[j] = false;
    }
  }
}

int main() {
  int n, limit;
  memset(prime, true, sizeof(prime));
  era();
  while (cin >> n >> limit && ((n != 0) || (limit != 0))) {
    Init();
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      int money = tmp;
      while (money <= limit) {
        dp[i][money] = true;
        for (int j = 0; j <= limit; j++) {
          if (money > j)
            dp[i][j] = dp[i - 1][j] | dp[i][j];
          else
            dp[i][j] = dp[i][money] & dp[i - 1][j - money] ? true
                       : dp[i][j]                          ? true
                                                           : false;
        }
        money += tmp;
      }
    }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#define MAX_X 1000100
using namespace std;

int n;
int x;
int d[100];
bool p[MAX_X];
bool dp[MAX_X];

int main() {

  memset(p, true, sizeof(p));
  p[0] = false;
  for (int i = 2; i * i < MAX_X; i++) {
    if (p[i])
      for (int j = i + i; j < MAX_X; j += i)
        p[j] = false;
  }

  while (true) {
    cin >> n >> x;
    if (!n && !x)
      break;

    memset(dp, false, sizeof(dp));
    dp[0] = true;
    for (int i = 0; i < n; i++)
      cin >> d[i];

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

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

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define MAX_X 1000000 + 100
using namespace std;

int n;
int x;
int d[100];
bool p[MAX_X];
bool memo[MAX_X];

int solve(int a, int k) {

  if (a > x)
    return 0;
  if (memo[a])
    return memo[a];

  memo[a] = true;

  for (int i = k; i < n; i++)
    solve(a + d[i], k);

  return memo[a];
}

int main() {

  memset(p, true, sizeof(p));
  p[0] = false;
  for (int i = 2; i * i < MAX_X; i++) {
    if (p[i])
      for (int j = i + i; j < MAX_X; j += i)
        p[j] = false;
  }

  while (true) {
    cin >> n >> x;
    if (!n && !x)
      break;

    memset(memo, false, sizeof(memo));
    for (int i = 0; i < n; i++)
      cin >> d[i];

    solve(0, 0);

    int ans = -1;
    for (int i = 0; i <= x; i++) {
      if (memo[i] && p[i])
        ans = i;
    }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main() {
  bool prime[1000005] = {}; // true
  int n, x, i, j, k;

  rep(i, 1000005) prime[i] = true;
  for (i = 2; i < 1000005; i++) {
    if (prime[i])
      for (j = 2; i * j < 1000005; j++)
        prime[i * j] = false;
  }

  while (1) {
    int price[30] = {};
    int cost[2000005] = {};
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    rep(i, n) {
      cin >> price[i];
      cost[price[i]]++;
    }

    rep(i, x) if (cost[i]) rep(j, n) cost[i + price[j]]++;

    for (i = x; i > 0; i--)
      if (cost[i] > 0 && prime[i])
        break;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main() {
  bool prime[1000005] = {}; // true
  int n, x, i, j, k;

  rep(i, 1000005) prime[i] = true;
  for (i = 2; i < 1000005; i++) {
    if (prime[i])
      for (j = 2; i * j < 1000005; j++)
        prime[i * j] = false;
  }

  while (1) {
    int price[30] = {};
    int cost[1000005] = {};
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    rep(i, n) {
      cin >> price[i];
      cost[price[i]]++;
    }

    rep(i, x) if (cost[i]) rep(j, n) cost[i + price[j]]++;

    for (i = x; i > 0; i--)
      if (cost[i] > 0 && prime[i])
        break;

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