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

#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++) {
          if (i + p[j] <= x)
            dp[i + p[j]] = true;
        }
      }
    }
    if (max > 0)
      cout << max << endl;
    else
      cout << "NA" << endl;
  }
}

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] == true && 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;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, b, n) for (int i = b; i < n; i++)
#define SORT(c) sort((c).begin(), (c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1 << 30);

using namespace std;

typedef long long ll;
typedef long double ld;

bool dp[1000002], sosu[1000002];
int n, x;
int v[30];

void sosuu() {
  sosu[0] = false;
  sosu[1] = false;
  for (int i = 2; i * i <= x; i++) {
    if (sosu[i]) {
      for (int j = 2 * i; j <= x; j += i) {
        sosu[j] = false;
      }
    }
  }
}

int main() {
  while (cin >> n >> x, n) {
    rep(i, n) cin >> v[i];
    rep(i, x + 1) {
      sosu[i] = true;
      dp[i] = false;
    }
    sosuu();
    rep(i, n) {
      dp[v[i]] = true;
      for (int j = 1; j <= x; j++)
        if (dp[j] && j + v[i] <= x)
          dp[j + v[i]] = true;
    }
    while (x > 0) {
      if (dp[x] && sosu[x])
        break;
      x--;
    }
    if (x == 0)
      cout << "NA" << endl;
    else
      cout << x << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, b, n) for (int i = b; i < n; i++)
#define SORT(c) sort((c).begin(), (c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1 << 30);

using namespace std;

typedef long long ll;
typedef long double ld;

bool dp[1000001], sosu[1000001];
int n, x;
int v[30];

void sosuu() {
  sosu[0] = false;
  for (int i = 2; i * i <= x; i++) {
    if (sosu[i]) {
      for (int j = 2 * i; j <= x; j += i) {
        sosu[j] = false;
      }
    }
  }
}

int main() {
  while (cin >> n >> x, n) {
    rep(i, n) cin >> v[i];
    rep(i, x + 1) {
      sosu[i] = true;
      dp[i] = false;
    }
    sosuu();
    rep(i, n) {
      dp[v[i]] = true;
      for (int j = v[i]; j <= x; j++)
        if (dp[j])
          dp[j + v[i]] = true;
    }
    while (x > 0) {
      if (dp[x] && sosu[x])
        break;
      x--;
    }
    if (x == 0)
      cout << "NA" << endl;
    else
      cout << x << endl;
  }

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

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define sosu 1000000
bool so[1000002];
int n, x, c[51];
bool his[1000002];
int ans;
queue<int> Q;
int count = 0;

void hantei() {
  so[0] = so[1] = 1;
  for (int i = 2; i * i <= 1000001; i++)
    if (so[i] == 0)
      for (int j = 2; j <= 1000001 / i; j++)
        so[i * j] = 1;
}

void saiki(int sum) {
  cout << "count " << count++ << endl;
  his[sum] = 1;
  if (so[sum] == 0 && ans < sum)
    ans = sum;
  cout << sum << endl;

  for (int i = 0; i < n; i++)
    if (c[i] + sum <= x && his[c[i] + sum] == 0)
      saiki(c[i] + sum);
}

void bfs() {

  Q.push(0);
  ans = 0;
  while (!Q.empty()) {
    // cout <<"count " << count++ << endl;
    int t = Q.front();
    Q.pop();
    if (his[t] == 1) {
      //  cout << "err "<<t<<endl;
      continue;
    }
    his[t] = 1;
    // cout <<"t "<<  t << endl;

    if (ans < t && so[t] == 0)
      ans = t;

    for (int i = 0; i < n; i++)
      if (t + c[i] <= x && his[t + c[i]] == 0)
        Q.push(t + c[i]);
  }
}

int main() {
  hantei();

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

    for (int i = 0; i < n; i++)
      cin >> c[i];
    for (int i = 0; i <= x + 1; i++)
      his[i] = 0;

    ans = 0;
    // saiki(0);
    bfs();
    if (ans == 0)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define sosu 1000000
bool so[2000001];
int n, x, c[31];
bool his[2000001];
int ans;

void hantei() {
  so[0] = so[1] = 1;
  for (int i = 2; i * i <= 1000001; i++)
    if (so[i] == 0)
      for (int j = 2; j <= 1000001 / i; j++)
        so[i * j] = 1;
}

void saiki(int sum) {
  his[sum] = 1;
  if (so[sum] == 0 && ans < sum)
    ans = sum;

  for (int i = 0; i < n; i++)
    if (c[i] + sum <= x && his[c[i] + sum] == 0)
      saiki(c[i] + sum);
}

int main() {
  hantei();

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

    for (int i = 0; i < n; i++)
      cin >> c[i];
    for (int i = 0; i <= x + 1; i++)
      his[i] = 0;

    ans = 1;
    saiki(0);

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

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

const int INF = 1 << 24;
const int N = 1000001;

int main(void) {
  vector<bool> isP(N);
  fill(ALL(isP), true);
  rep(i, 2) isP[i] = false;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i * i; j < N; j += i)
        isP[j] = false;

  for (int n, x; cin >> n >> x, n;) {
    vector<bool> dp(x + 1);
    fill(ALL(dp), false);
    dp[0] = true;
    while (n--) {
      int m;
      cin >> m;

      rep(i, x - m + 1) if (dp[i]) dp[i + m] = true;
    }
    while (~x && !(dp[x] && isP[x]))
      x--;

    if (~x)
      cout << x << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

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

const int INF = 1 << 24;
const int N = 1000001;

int main(void) {
  vector<bool> isP(N);
  fill(ALL(isP), true);
  rep(i, 2) isP[i] = false;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i * i; j < N; j += i)
        isP[j] = false;

  for (int n, x; cin >> n >> x, n;) {
    vector<bool> dp(x + 1);
    fill(ALL(dp), false);
    dp[0] = true;
    while (n--) {
      int m;
      cin >> m;

      rep(i, x) if (dp[i]) dp[i + m] = true;
    }
    while (~x && !(dp[x] && isP[x]))
      x--;

    if (~x)
      cout << x << endl;
    else
      cout << "NA" << endl;
  }

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

#include <bits/stdc++.h>
#define int long long
using namespace std;
bool e[1000001], dp[1001110];
main() {
  e[0] = e[1] = 1;
  for (int i = 2; i < 1000001; i++)
    for (int j = i * 2; j < 1000001; j += i)
      e[j] = 1;
  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i < m + 110; i++)
      dp[i] = 0;
    int a[n], c = 1, p;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n; i++)
      for (int j = 1; j <= m; j++) {
        if (dp[j] && a[i] + j <= m)
          dp[a[i] + j] = 1;
        if (a[i] * j <= m)
          dp[a[i] * j] = 1;
      }
    for (p = m; p >= 0; p--)
      if (dp[p] && !e[p]) {
        c = 0;
        break;
      }
    if (c)
      cout << "NA" << endl;
    else
      cout << p << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool e[1000001], dp[1011110];
int main() {
  e[0] = e[1] = 1;
  for (int i = 2; i < 1000001; i++)
    for (int j = i * 2; j < 1000001; j += i)
      e[j] = 1;
  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i < m + 1110; i++)
      dp[i] = 0;
    int a[n], c = 1, p;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n; i++)
      for (int j = 1; j <= m; j++) {
        if (dp[j] && a[i] + j <= m)
          dp[a[i] + j] = 1;
        if (a[i] * j <= m)
          dp[a[i] * j] = 1;
      }
    for (p = m; p >= 0; p--)
      if (dp[p] && !e[p]) {
        c = 0;
        break;
      }
    if (c)
      cout << "NA" << endl;
    else
      cout << p << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

bool prime[1000001];

int main() {
  int n, x;
  rep(i, 1000002) prime[i] = true;
  prime[0] = prime[1] = false;
  loop(i, 2, 1010) for (int j = i * 2; j < 1000001; j += i) prime[j] = false;
  while (cin >> n >> x, n || x) {
    int dish[n];
    rep(i, n) cin >> dish[i];
    bool cost[x + 10];
    rep(i, x + 10) cost[i] = false;
    rep(i, n) if (dish[i] < x + 1) cost[dish[i]] = true;
    cost[0] = true;
    rep(i, x + 1) {
      if (!cost[i])
        continue;
      rep(j, n) {
        if (i + dish[j] > x)
          continue;
        cost[i + dish[j]] = true;
      }
    }
    bool check = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] && cost[i]) {
        check = false;
        cout << i << endl;
        break;
      }
    }
    if (check)
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

bool prime[1000001];

int main() {
  int n, x;
  rep(i, 1000002) prime[i] = true;
  prime[0] = prime[1] = false;
  loop(i, 2, 1010) for (int j = i * 2; j < 1000001; j += i) prime[j] = false;
  while (cin >> n >> x, n || x) {
    int dish[n];
    rep(i, n) cin >> dish[i];
    bool cost[x + 10];
    rep(i, x + 10) cost[i] = false;
    rep(i, n) cost[dish[i]] = true;
    cost[0] = true;
    rep(i, x + 1) {
      if (!cost[i])
        continue;
      rep(j, n) {
        if (i + dish[j] > x)
          continue;
        cost[i + dish[j]] = true;
      }
    }
    bool check = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] && cost[i]) {
        check = false;
        cout << i << endl;
        break;
      }
    }
    if (check)
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool prime[10000001];
bool dp[10000001];
int menu[30];
int n, x;

void make_prime() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i < 10000001; i++) {
    if (prime[i]) {
      for (int j = i + i; j < 10000001; j += i)
        prime[j] = false;
    }
  }
}

int main() {
  make_prime();
  for (; cin >> n >> x, n;) {
    memset(dp, false, sizeof(dp));
    for (int i = 0; i < n; i++) {
      cin >> menu[i];
      dp[menu[i]] = true;
    }
    int ans = -1;
    for (int i = 0; i <= x; i++) {
      if (dp[i])
        for (int j = 0; j < n; j++)
          if (i + menu[j] <= x)
            dp[i + menu[j]] = true;
    }
    for (int i = x; i > 0; i--)
      if (prime[i] && dp[i]) {
        ans = i;
        break;
      }
    if (ans != -1)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

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

bool prime[10000001];
bool dp[10000001];
int menu[30];
int n, x;

void make_prime() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i < 10000001; i++) {
    if (prime[i]) {
      for (int j = i + i; j < 10000001; j += i)
        prime[j] = false;
    }
  }
}

int main() {
  make_prime();
  for (; cin >> n >> x, n;) {
    memset(dp, false, sizeof(dp));
    for (int i = 0; i < n; i++) {
      cin >> menu[i];
      dp[menu[i]] = true;
    }
    int ans = -1;
    for (int i = 0; i < 10000001; i++) {
      if (dp[i])
        for (int j = 0; j < n; j++)
          if (i + menu[j] < 10000001)
            dp[i + menu[j]] = true;
    }
    for (int i = x; i > 0; i--)
      if (prime[i] && dp[i]) {
        ans = i;
        break;
      }
    if (ans != -1)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  vector<int> prim(1000000 + 1);
  fill(prim.begin(), prim.end(), 1);
  prim[0] = 0;
  prim[1] = 0;
  for (int i = 2; i < prim.size(); i++) {
    if (prim[i] != 0) {
      int x = i + i;
      while (x < prim.size()) {
        prim[x] = 0;
        x += i;
      }
    }
  }

  while (true) {
    int N, X;
    cin >> N >> X;
    if (N == 0 && X == 0)
      break;

    vector<int> food;
    for (int i = 0; i < N; i++) {
      int m;
      cin >> m;
      food.push_back(m);
    }

    vector<int> num(X + 1);
    fill(num.begin(), num.end(), 0);
    for (int i = 0; i < N; i++) {
      if (food[i] <= X) {
        num[food[i]] = 1;
      }
    }
    for (int i = 0; i <= X; i++) {
      if (num[i] != 0) {
        for (int j = 0; j < N; j++) {
          if (i + food[j] <= X) {
            num[i + food[j]] = 1;
          }
        }
      }
    }
    int ans = -1;
    for (int x = X; x >= 0; x--) {
      if (prim[x] == 1 && num[x] != 0) {
        ans = x;
        break;
      }
    }
    if (ans > 0) {
      cout << ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  vector<int> prim(1000000 + 1);
  fill(prim.begin(), prim.end(), 1);
  prim[0] = 0;
  prim[1] = 0;
  for (int i = 2; i < prim.size(); i++) {
    if (prim[i] != 0) {
      int x = i + i;
      while (x < prim.size()) {
        prim[x] = 0;
        x += i;
      }
    }
  }

  while (true) {
    int N, X;
    cin >> N >> X;
    if (N == 0 && X == 0)
      break;

    vector<int> food;
    for (int i = 0; i < N; i++) {
      int m;
      cin >> m;
      food.push_back(m);
    }

    vector<int> num(X + 1);
    fill(num.begin(), num.end(), 0);
    for (int i = 0; i < N; i++) {
      num[food[i]] = 1;
    }
    for (int i = 0; i < X + 1; i++) {
      if (num[i] != 0) {
        for (int j = 0; j < N; j++) {
          if (i + food[j] >= num.size())
            break;
          num[i + food[j]] = 1;
        }
      }
    }
    int ans = -1;
    for (int x = X; x >= 0; x--) {
      if (prim[x] == 1 && num[x] != 0) {
        ans = x;
        break;
      }
    }
    if (ans > 0) {
      cout << ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }

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

#include <cstring>
#include <iostream>

using namespace std;

#define MAX_X 1000001
#define MAX_N 30

bool is_prime[MAX_X];

void init() {
  memset(is_prime, 1, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < MAX_X; i++)
    if (is_prime[i])
      for (int j = i + i; j < MAX_X; j += i)
        is_prime[j] = false;
}

bool can_make[MAX_X];

int n, x;
int dish[MAX_N];
bool input() {
  cin >> n >> x;
  if (n == 0 && x == 0)
    return false;
  for (int i = 0; i < n; i++)
    cin >> dish[i];

  memset(can_make, 0, sizeof(can_make));
  can_make[0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= x; j++) {
      if (j >= dish[i] && can_make[j - dish[i]])
        can_make[j] = true;
    }
  }
  return true;
}

int main() {
  init();
  while (input()) {
    bool found = false;
    while (x) {
      if (is_prime[x] && can_make[x]) {
        found = true;
        cout << x << endl;
        break;
      }
      x--;
    }
    if (!found)
      cout << "NA" << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

#define MAX_X 1500001

int n;
int x;
vector<int> p;

bool table[MAX_X];
vector<int> prime;

void init() {
  memset(table, 1, sizeof(table));
  table[0] = table[1] = false;
  for (int i = 2; i < MAX_X; i++) {
    if (table[i]) {
      prime.push_back(i);
      for (int j = i + i; j < MAX_X; j += i) {
        table[j] = false;
      }
    }
  }
}

bool cache[MAX_X];
bool cached[MAX_X];

bool can_make(int t) {
  //    cout << t << endl;
  if (t == 0)
    return true;
  else if (t < 0)
    return false;
  if (cached[t])
    return cache[t];

  bool can = false;
  for (int i = 0; i < p.size(); i++) {
    if (can_make(t - p[i])) {
      can = true;
      break;
    }
  }
  cache[t] = can;
  cached[t] = true;
  return can;
}

int main() {
  init();
  //    for (int i = 0; i < prime.size(); i++) {
  //        cout << prime[i] << ' ';
  //    }

  while (cin >> n >> x, n || x) {
    memset(cache, 0, sizeof(cache));
    memset(cached, 0, sizeof(cached));

    p.clear();
    p.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    sort(p.begin(), p.end(), greater<int>());

    bool exist = false;

    int ub = prime.size() - 1;
    int lb = 0;
    int mid;

    while (lb != ub - 1) {
      mid = (lb + ub) / 2;
      if (prime[mid] < x) {
        lb = mid;
      } else {
        ub = mid;
      }
    }

    //        cout << "ub: " << ub << endl;
    for (int i = mid; 0 <= i; i--) {
      if (can_make(prime[i])) {
        cout << prime[i] << endl;
        exist = true;
        break;
      }
    }

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> prime;
  int i, j;

  for (i = 0; i < 1000001; i++)
    prime.push_back(1);

  prime[0] = prime[1] = 0;

  for (i = 2; i < 1000001; i++) {
    if (prime[i] == 1) {
      for (j = 2; j * i < 1000001; j++)
        prime[j * i] = 0;
    }
  }

  int n, x, ans;

  for (;;) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;

    vector<int> price;
    ans = 0;

    for (i = 0; i < n; i++) {
      int a;
      cin >> a;
      price.push_back(a);
    }

    vector<int> sans;

    for (i = 0; i < 1000001; i++)
      sans.push_back(0);

    for (i = 0; i < price.size(); i++)
      sans[price[i]] = 1;

    for (i = 0; i <= x; i++) {
      if (sans[i] == 1) {
        if (prime[i] == 1)
          ans = i;
        for (j = 0; j < price.size(); j++) {
          if (i + price[j] <= x) {
            sans[i + price[j]] = 1;
          }
        }
      }
    }

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

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

vector<int> budget;
int price[30];
int max_budget;
int n;

void serch(int s_price) {
  if (s_price > max_budget)
    return;

  if (budget[s_price] == 1) {
    budget[s_price] = 2;
    //		cout << "s_prime:" << s_price << endl;
  } else
    budget[s_price] = 3;

  for (int i = 0; i < n; i++) {
    if (budget[s_price + price[i]] == 0 || budget[s_price + price[i]] == 1)
      serch(s_price + price[i]);
    //		else
    //			return ;
  }
}

int main() {
  int i;

  for (;;) {
    cin >> n >> max_budget;
    if (n == 0 && max_budget == 0)
      break;

    int ans = 0;
    budget.clear();

    for (i = 0; i < 1000001; i++)
      budget.push_back(1);

    budget[0] = budget[1] = 0;

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

    /*		for(i=0;i<1000001;i++)
                    {
                            if(budget[i]==1)
                                    cout << "prime:" << i << endl;
                    }
    */
    for (i = 0; i < n; i++)
      cin >> price[i];

    for (i = 0; i < n; i++)
      serch(price[i]);

    for (i = max_budget; i >= 0; i--) {
      if (budget[i] == 2) {
        ans = 1;
        cout << i << endl;
        break;
      }
    }

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