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

#include <iostream>
#include <stdio.h>
#include <string.h>
int hideo[2000001] = {0};
int main(void) {
  int noyori = 0, debu = 0, n, temp = 1, ruio[32] = {0}, x;
  while (scanf("%d %d", &n, &x)) {
    if (n == 0 && x == 0) {
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &ruio[i]);
      hideo[ruio[i]] = 1;
    }
    while (temp != x) {
      if (hideo[temp] == 1) {
        for (int j = 1; j <= n; j++) {
          if (temp + ruio[j] > x)
            continue;
          if (hideo[temp + ruio[j]] == 0) {
            hideo[temp + ruio[j]] = 1;
          }
        }
      }
      temp++;
    }
    for (int i = x; i > 1; i--) {
      if (hideo[i] == 1) {
        for (int j = 2; j < i - 1; j++) {
          if (i % j == 0) {
            debu = 1;
          }
        }
        if (debu == 0) {
          noyori = i;
          break;
        }
      }
      debu = 0;
    }
    if (noyori > 1) {
      printf("%d\\n", noyori);
    } else {
      printf("NA\\n");
    }
    temp = 1;
    noyori = 0;
    debu = 0;
    memset(ruio, 0, sizeof(ruio));
    memset(hideo, 0, sizeof(hideo));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int hideo[1000001] = {0};
int main(void) {
  int n, x, temp = 1, noyori = 0, debu = 0, ruio[31] = {0};
  while (scanf("%d %d", &n, &x)) {
    if (n == 0 && x == 0) {
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &ruio[i]);
      hideo[ruio[i]] = 1;
    }
    while (temp != x) {
      if (hideo[temp] == 1) {
        for (int j = 1; j <= n; j++) {
          if (hideo[temp + ruio[j]] == 0) {
            hideo[temp + ruio[j]] = 1;
          }
        }
      }
      temp++;
    }
    for (int i = x; i >= 1; i--) {
      if (hideo[i] == 1) {
        for (int j = 2; j < i - 1; j++) {
          if (i % j == 0) {
            debu = 1;
          }
        }
        if (debu == 0) {
          noyori = i;
          break;
        }
      }
      debu = 0;
    }
    if (noyori > 1) {
      printf("%d\\n", noyori);
    } else {
      printf("NA\\n");
    }
    temp = 1;
    noyori = 0;
    debu = 0;
    for (int i = 1; i <= n; i++) {
      ruio[i] = 0;
    }
    for (int i = 1; i <= x; i++) {
      hideo[i] = 0;
    }
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int main(void) {
  static char prime[1000001];
  static char ok[1000001];
  int i, j;
  int n, t;
  int a[30];
  int max;

  for (i = 3; i <= 1000000; i += 2) {
    prime[i] = 1;
  }
  prime[2] = 1;

  for (i = 3; i <= 1000; i += 2) {
    if (prime[i] == 1) {
      for (j = 3; j <= 1000000 / i; j += 2) {
        prime[i * j] = 0;
      }
    }
  }

  while (1) {
    scanf("%d%d", &t, &n);

    if (t + n == 0) {
      break;
    }

    for (i = 0; i < t; i++) {
      scanf("%d", &a[i]);
    }

    memset(ok, 0, sizeof(ok));
    ok[0] = 1;
    max = -100;
    for (i = 0; i < t; i++) {
      for (j = 0; j + a[i] <= n; j++) {
        if (ok[j] != 0) {
          ok[j + a[i]] = ok[j];
          if (j + a[i] > max && prime[j + a[i]]) {
            max = j + a[i];
          }
        }
      }
    }

    if (max != -100) {
      printf("%d\\n", max);
    } else {
      printf("NA\\n");
    }
  }

  return (0);
}

Error: 0

Fixed submission:

#include <stdio.h>
int hideo[1000001] = {0}, ruio[31] = {0};
int main() {
  int n, x, temp = 1, noyori = 0, debu = 0;
  while (scanf("%d %d", &n, &x)) {
    if (n == 0 && x == 0) {
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &ruio[i]);
      hideo[ruio[i]] = 1;
    }
    while (temp != x) {
      if (hideo[temp] == 1) {
        for (int j = 1; j <= n; j++) {
          if (hideo[temp + ruio[j]] == 0) {
            hideo[temp + ruio[j]] += 1;
          }
        }
      }
      temp++;
    }
    for (int i = x; i >= 1; i--) {
      if (hideo[i] == 1) {
        for (int j = 2; j < i - 1; j++) {
          if (i % j == 0) {
            debu++;
          }
        }
        if (debu == 0) {
          noyori = i;
          break;
        }
      }
      debu = 0;
    }
    if (noyori > 0) {
      printf("%d\\n", noyori);
    } else {
      printf("NA\\n");
    }
    temp = 1;
    noyori = 0;
    debu = 0;
    for (int i = 1; i <= n; i++) {
      ruio[i] = 0;
    }
    for (int i = 1; i <= x; i++) {
      hideo[i] = 0;
    }
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
bool sosuu[1111111];
vector<int> v;
int a[33], x, N;
// bool dp[33][1111111];
// bool used[33][1111111];
bool solve(int n, int m) {
  if (m == 0)
    return true;
  if (n == N)
    return false;
  // if(used[n][m]) return dp[n][m];
  // used[n][m] = true;

  bool res = solve(n + 1, m);
  if (!res && m >= a[n])
    res |= solve(n, m - a[n]);
  // return dp[n][m] = res;
  return res;
}
int main(void) {
  sosuu[0] = sosuu[1] = true;
  for (int i = 0; i * i <= 1000000; i++) {
    if (sosuu[i])
      continue;
    for (int j = i + i; j <= 1000000; j += i) {
      sosuu[j] = true;
    }
  }
  for (int i = 0; i <= 1000000; i++) {
    if (!sosuu[i]) {
      v.push_back(i);
    }
  }

  while (cin >> N >> x) {
    if (!N)
      break;
    // memset(used,false,sizeof(used));
    for (int i = 0; i < N; i++) {
      cin >> a[i];
    }
    int i;
    for (i = (int)v.size() - 1; i >= 0; i--) {
      if (x < v[i])
        continue;
      if (solve(0, v[i]))
        break;
    }
    if (i + 1) {
      cout << v[i] << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
bool sosuu[1111111];
vector<int> v;
int a[33], x, N;
bool dp[33][1111111];
bool used[33][1111111];
bool solve(int n, int m) {
  if (m == 0)
    return true;
  if (n == N)
    return false;
  if (used[n][m])
    return dp[n][m];
  used[n][m] = true;

  bool res = solve(n + 1, m);
  if (!res && m >= a[n])
    res |= solve(n, m - a[n]);
  return dp[n][m] = res;
}
int main(void) {
  sosuu[0] = sosuu[1] = true;
  for (int i = 0; i * i <= 1000000; i++) {
    if (sosuu[i])
      continue;
    for (int j = i + i; j <= 1000000; j += i) {
      sosuu[j] = true;
    }
  }
  for (int i = 0; i <= 1000000; i++) {
    if (!sosuu[i]) {
      v.push_back(i);
    }
  }

  while (cin >> N >> x) {
    if (!N)
      break;
    memset(used, false, sizeof(used));
    for (int i = 0; i < N; i++) {
      cin >> a[i];
    }
    int i;
    for (i = (int)v.size() - 1; i >= 0; i--) {
      if (x < v[i])
        continue;
      if (solve(0, v[i]))
        break;
    }
    if (i + 1) {
      cout << v[i] << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int n, m[50];
bool prime[1000000], c[1000000];

void saiki(int total) {
  int i;

  c[total] = 1;

  for (i = total; i >= 0; i--) {
    if (c[i] == 1) {
      for (int j = 0; j < n; j++) {
        if (i - m[j] >= 0) {
          c[i - m[j]] = 1;
        }
      }
    }
  }
}

int main(void) {
  int i, j;
  // f
  prime[0] = 1;
  prime[1] = 1;
  for (i = 3; i < 1000000; i++) {
    if (i % 2 == 0) {
      prime[i] = 1;
    }
  }
  for (i = 3; i < 1000000; i += 2) {
    if (prime[i] == 0) {
      for (j = (i + i); j < 1000000; j += i) {
        prime[j] = 1;
      }
    }
  }
  //
  while (1) {
    int x;
    int flg = 0;
    scanf("%d%d", &n, &x);
    if (n == 0)
      break;

    for (i = 0; i < n; i++) {
      scanf("%d", &m[i]);
    }
    saiki(x);
    //
    for (i = 0; i < x; i++) {
      if (c[i] == 1 && prime[x - i] == 0) {
        flg = 1;
        break;
      }
    }
    if (flg == 1) {
      printf("%d\\n", x - i);
    } else
      puts("NA");
    for (i = 0; i < 1000000; i++) {
      c[i] = 0;
    }
    for (i = 0; i < 50; i++) {
      m[i] = 0;
    }
    //
  }

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int n, m[50];
bool prime[1000000], c[1000000];

void saiki(int total) {
  int i;

  for (i = 0; i < n; i++) {
    total -= m[i];
    if (c[total] == 1)
      continue;
    if (total >= 0) {
      c[total] = 1;
    }
    if (total <= 0)
      continue;
    saiki(total);
  }
}

int main(void) {
  int i, j;
  // f
  prime[0] = 1;
  prime[1] = 1;
  for (i = 3; i < 1000000; i++) {
    if (i % 2 == 0) {
      prime[i] = 1;
    }
  }
  for (i = 3; i < 1000000; i += 2) {
    if (prime[i] == 0) {
      for (j = (i + i); j < 1000000; j += i) {
        prime[j] = 1;
      }
    }
  }
  //
  while (1) {
    int x;
    int flg = 0;
    scanf("%d%d", &n, &x);
    if (n == 0)
      break;

    for (i = 0; i < n; i++) {
      scanf("%d", &m[i]);
    }
    saiki(x);
    //
    for (i = 0; i < x; i++) {
      if (c[i] == 1 && prime[x - i] == 0) {
        flg = 1;
        break;
      }
    }
    if (flg == 1) {
      printf("%d\\n", x - i);
    } else
      puts("NA");
    for (i = 0; i < 1000000; i++) {
      c[i] = 0;
    }
    for (i = 0; i < 50; i++) {
      m[i] = 0;
    }
    //
  }

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

#include <iostream>
using namespace std;
#define MAX 1000000
int main() {
  bool b[MAX + 1] = {0};
  int i, j;
  b[0] = b[1] = 1;
  for (i = 4; i <= MAX; i += 2)
    b[i] = 1;
  for (i = 3; i * i <= MAX; i += 2)
    if (!b[i])
      for (j = i + i; j <= MAX; j += i)
        b[j] = 1;
  int n, x, price[30], dp[MAX + 1];
  bool f;
  while (cin >> n >> x, n | x) {
    for (i = 0; i < n; i++)
      cin >> price[i];
    for (i = 0; i < x + 1; i++)
      dp[i] = (i == 0);
    for (i = 0; i < x + 1; i++) {
      if (!dp[i])
        continue;
      for (j = 0; j < n; j++)
        if (i + price[j] < x + 1)
          dp[i + price[j]] = true;
    }
    f = false;
    for (i = x; i > 0; i--)
      if (dp[i] & !b[i]) {
        f = true;
        break;
      }
    if (f)
      cout << i << endl;
    else
      puts("NA");
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define MAX 1000000
int main() {
  int b[MAX + 1] = {0}, i, j;
  b[0] = b[1] = 1;
  for (i = 4; i <= MAX; i += 2)
    b[i] = 1;
  for (i = 3; i * i <= MAX; i += 2)
    if (!b[i])
      for (j = i + i; j <= MAX; j += i)
        b[j] = 1;
  int n, x;
  bool f;
  vector<int> price;
  vector<bool> dp;
  while (cin >> n >> x, n | x) {
    price.resize(n);
    for (i = 0; i < n; i++)
      cin >> price[i];
    dp.resize(x + 1);
    for (i = 0; i < x + 1; i++)
      dp[i] = (i == 0);
    for (i = 0; i < x + 1; i++) {
      if (!dp[i])
        continue;
      for (j = 0; j < n; j++)
        if (i + price[j] < x + 1)
          dp[i + price[j]] = true;
    }
    f = false;
    for (i = x; i > 0; i--)
      if (dp[i] & !b[i]) {
        f = true;
        break;
      }
    if (f)
      cout << i << endl;
    else
      puts("NA");
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>

#define pb push_back
#define MAX 1000000

int dp[MAX];

using namespace std;

int main(void) {
  int n, x;
  int i1, i2;

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

    vector<int> v(n);
    memset(dp, 0, sizeof(dp));

    for (i1 = 0; i1 < n; i1++) {
      cin >> v[i1];
      dp[v[i1]] = 1;
    }

    for (i1 = 0; i1 <= x; i1++) {
      if (dp[i1] == 1) {
        for (i2 = 0; i2 < n; i2++) {
          if ((i1 + v[i2]) < MAX)
            dp[i1 + v[i2]] = 1;
        }
      }
    }

    for (i1 = x; i1 > 0; i1--) {
      if (dp[i1] == 1) {

        for (i2 = 2; i2 * i2 <= i1; i2++) {
          if (i1 % i2 == 0)
            break;
        }
        if (i2 * i2 > i1)
          break;
      }
    }

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>

#define pb push_back
#define MAX 1000000

int dp[MAX];

using namespace std;

int main(void) {
  int n, x;
  int i1, i2;

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

    vector<int> v(n);
    memset(dp, 0, sizeof(dp));

    for (i1 = 0; i1 < n; i1++) {
      cin >> v[i1];
      dp[v[i1]] = 1;
    }

    for (i1 = 0; i1 <= x; i1++) {
      if (dp[i1] == 1) {
        for (i2 = 0; i2 < n; i2++) {
          dp[i1 + v[i2]] = 1;
        }
      }
    }

    for (i1 = x; i1 > 0; i1--) {
      if (dp[i1] == 1) {

        for (i2 = 2; i2 * i2 <= i1; i2++) {
          if (i1 % i2 == 0)
            break;
        }
        if (i2 * i2 > i1)
          break;
      }
    }

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

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

#include <functional>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
int main() {
  // ツ素ツ青板青カツ青ャ
  const int NUM = 1000000;
  bool prime[NUM];
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < NUM; i++)
    prime[i] = true;
  for (int i = 2; i * i < NUM; i++)
    for (int k = 2; i * k < NUM; k++)
      prime[i * k] = false;

  while (true) {
    int n, x;
    cin >> n >> x;
    if (!n && !x)
      break;
    vector<int> menu(n);
    for (int i = 0; i < n; i++) {
      cin >> menu[i];
    }
    int p = -1;
    bool money[x + 1];
    money[0] = true;
    for (int i = 1; i < x + 1; i++) {
      money[i] = false;
      for (int j = 0; j < n; j++) {
        if (i >= menu[j] && money[i - menu[j]])
          money[i] = true;
      }
      if (money[i] && prime[i])
        p = i;
    }
    if (p == -1)
      cout << "NA" << endl;
    else
      cout << p << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
int main() {
  // ツ素ツ青板青カツ青ャ
  const int NUM = 1000000;
  bool prime[NUM];
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < NUM; i++)
    prime[i] = true;
  for (int i = 2; i * i < NUM; i++)
    for (int k = 2; i * k < NUM; k++)
      prime[i * k] = false;

  while (true) {
    int n, x;
    cin >> n >> x;
    if (!n && !x)
      break;
    vector<int> menu(n);
    for (int i = 0; i < n; i++) {
      cin >> menu[i];
    }
    int p = -1;
    set<int> money;
    set<int>::iterator it;
    money.insert(0);
    while (true) {
      it = money.begin();
      if (*it > x)
        break;
      if (prime[*it])
        p = *it;
      for (int i = 0; i < n; i++) {
        money.insert(menu[i] + *it);
      }
      money.erase(it);
    }
    if (p == -1)
      cout << "NA" << endl;
    else
      cout << p << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
using namespace std;
#define KMAX 30
#define VMAX 1000000

int main() {
  int k, v;
  int K[KMAX];
  char T[VMAX + 1];
  bool prime[VMAX + 1];
  for (int i = 0; i <= VMAX; i++)
    prime[i] = false;
  for (int i = 3; i <= VMAX; i += 2)
    prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)VMAX) + 1;
  for (int i = 3; i <= limit; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= VMAX; j += i)
      prime[j] = false;
  }

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

Error: -11

Fixed submission:

#include <cmath>
#include <iostream>
#define VMAX 1000000
#define KMAX 30
using namespace std;

int main() {
  int k, v;
  int K[KMAX];
  char T[KMAX][VMAX + 1];
  bool prime[VMAX + 1];
  for (int i = 0; i <= VMAX; i++)
    prime[i] = false;
  for (int i = 3; i <= VMAX; i += 2)
    prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)VMAX) + 1;
  for (int i = 3; i <= limit; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= VMAX; j += i)
      prime[j] = false;
  }

  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 > 1; i--)
      if (prime[i] && T[k - 1][i]) {
        ans = i;
        break;
      }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
bool dp[1000001];
bool isprime(int &x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return true;
  return false;
}
int solve(int i) {
  while (true) {
    if (dp[i] && !isprime(i))
      return i;
    i--;
  }
}
int main() {
  int n, x, data[30], ans;
  while (cin >> n >> x, n) {
    fill_n(dp, 1000001, false);
    dp[0] = true;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    for (int i = 0; i <= x; i++) {
      if (dp[i])
        for (int j = 0; j < n; j++)
          if (i + data[j] <= x)
            dp[i + data[j]] = true;
    }
    if (1 < (ans = solve(x)))
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
bool dp[1000001] = {true}, prime[1000001] = {true, true};
int solve(int &i) {
  while (i--)
    if (dp[i] && !prime[i])
      return i;
}
int main() {
  int n, x, data[30], ans;
  for (int i = 2; i * i < 1000001; i++) {
    if (!prime[i])
      for (int j = 2; i * j <= 1000001; j++)
        prime[i * j] = true;
  }
  while (cin >> n >> x, n) {
    for (int i = 1; i <= x; i++)
      dp[i] = false;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    for (int i = 0; i <= x; i++) {
      if (dp[i])
        for (int j = 0; j < n; j++)
          dp[i + data[j]] = true;
    }
    if (ans = solve(x))
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

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

  int n, x;
  while (scanf("%d %d", &n, &x), n | x) {
    int v[30];
    for (int i = 0; i < n; ++i) {
      int t;
      scanf("%d", &t);
      v[i] = t;
    }

    fill(dp, dp + x + 1, false);
    dp[0] = true;
    for (int i = 0; i <= x; ++i)
      if (dp[i])
        for (int j = 0; j < n; ++j)
          if (i + v[j] <= x)
            dp[i + v[j]] = true;

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

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

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

  int n, x;
  while (scanf("%d %d", &n, &x), n | x) {
    int v[30];
    for (int i = 0; i < n; ++i) {
      int t;
      scanf("%d", &t);
      v[i] = t;
    }

    fill(dp, dp + x + 1, false);
    dp[0] = true;
    for (int i = 0; i <= x; ++i)
      if (dp[i])
        for (int j = 0; j < n; ++j)
          dp[i + v[j]] = true;

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

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

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

using namespace std;

int main() {
  const int lim = 1000000;
  vector<bool> primes(lim + 1);
  fill(primes.begin(), primes.end(), true);
  primes[0] = primes[1] = false;
  const int l = (int)sqrt(lim);
  for (int i = 2; i <= l; i++) {
    if (primes[i]) {
      for (int j = i * i; j <= lim; j += i) {
        primes[j] = false;
      }
    }
  }

  while (true) {
    int n, x;
    cin >> n >> x;
    if (n == 0 and x == 0)
      break;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) {
      cin >> vs[i];
    }
    sort(vs.begin(), vs.end());
    vector<bool> cands(x + 1);
    int ans = -1;
    fill(cands.begin(), cands.end(), false);
    cands[0] = true;
    for (int i = 0; i <= x; i++) {
      if (cands[i]) {
        for (int v : vs) {
          if (i + v <= x) {
            cands[i + v] = true;
          }
        }
        if (primes[i]) {
          ans = i;
        }
      }
    }
    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  const int lim = 1000000;
  vector<bool> primes(lim + 1);
  fill(primes.begin(), primes.end(), true);
  primes[0] = primes[1] = false;
  const int l = (int)sqrt(lim);
  for (int i = 2; i <= l; i++) {
    if (primes[i]) {
      for (int j = i * i; j <= lim; j += i) {
        primes[j] = false;
      }
    }
  }

  while (true) {
    int n, x;
    cin >> n >> x;
    if (n == 0 and x == 0)
      break;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) {
      cin >> vs[i];
    }
    sort(vs.begin(), vs.end());
    vector<int> cs;
    cs.push_back(0);
    vector<int> indices(n);
    fill(indices.begin(), indices.end(), 0);
    while (cs.back() <= x) {
      int mv = 100000000;
      int mi = -1;
      for (int i = 0; i < n; i++) {
        if (mv > cs[indices[i]] + vs[i]) {
          mi = i;
          mv = cs[indices[i]] + vs[i];
        }
      }
      if (cs.back() < mv) {
        cs.push_back(mv);
      }
      indices[mi]++;
    }
    reverse(cs.begin(), cs.end());
    bool s = false;
    for (int v : cs) {
      if (primes[v]) {
        s = true;
        cout << v << endl;
        break;
      }
    }
    if (not s) {
      cout << "NA" << endl;
    }
  }
}