Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		x, _ := strconv.Atoi(buf[1])
		if n == 0 && x == 0 {
			break
		}
		prime_table := make([]int, 1000001)
		prime_table[0] = -1
		prime_table[1] = -1
		for i := 2; i < 1000001; i++ {
			if prime_table[i] != -1 {
				prime_table[i] = 1
				for j := 2*i; j < 1000001; j += i {
					prime_table[j] = -1 // -1 means "not prime"
				}
			}
		}
		dp := make([]int, x+1)
		dp[0] = 1
		menu := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			menu[i], _ = strconv.Atoi(scanner.Text())
		}
		for i := 0; i < n; i++ {
			for j := 0; j <= x; j++ {
				if menu[i] <= j && dp[j-menu[i]] != 0 {
					// log.Println(dp)
					dp[j] = 1
				}
			}
		}
		flag := true
		for j := x; j >= 0; j-- {
			if prime_table[j] == 1 && dp[j] == 1 {
				flag = false
				fmt.Println(j)
				break
			}
		}
		if flag {
			fmt.Println("NA")
		}
	}
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

constexpr bool isPrime(const int n) {
  if (n == 1) return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

main {
  int n, x;
  while (cin >> n >> x, n) {
    vi v(n);
    cin >> v;
    vi dp(x + 1);
    dp[0] = true;
    for (auto &t : v) rep(i, x + 1 - t) dp[i + t] |= dp[i];
    for (int i = x; i >= 0; i--) {
      if (dp[i] && isPrime(i)) {
        cout << i << endl;
        goto next;
      }
    }
    cout << "NA" << endl;
  next:;
  }
}

C++: #include <iostream>
#include <cstring>
#define MAX 1000000
using namespace std;

int n,x,d[31];
bool p[MAX+1],dp[MAX+1];

void isPrime(){
   memset(p,1,sizeof(p));
   p[0] = p[1] = false;
   for(int i=2;i*i<=MAX;i++){
      for(int j=i+i;j<=MAX;j+=i)
         p[j] = false;
   }
}

int main(){
   isPrime();
   while(cin >> n >> x,n||x){
      memset(dp,0,sizeof(dp));
      for(int i=0;i<n;i++) cin >> d[i];
      
      dp[0] = true;
      for(int i=0;i<=x;i++){
         if(dp[i]){
            for(int j=0;j<n;j++){
               if(i + d[i] >= x) continue;
               dp[i + d[j]] = true;
            }
         }
      }
      
      bool ju = true;
      for(int i=x;i>0;i--){
         if(p[i] && dp[i]){ cout << i << endl; ju = false; break; }
      }
      if(ju) cout << "NA" << endl;
   }
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>

using namespace std;

int main() {
	int n, x;
	vector<int> furui(1000000+1, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i <= 1000000; i++) {
		if (furui[i] == 0) {
			for (int j = 2; i*j <= 1000000; j++) {
				furui[i*j] = 1;
			}
		}
	}
	while (cin >> n >> x) {
		if (n == 0 && x == 0) {
			break;
		}
		vector<int> en;
		priority_queue<int, vector<int>, greater<int> > pq;
		vector<int> dp(x+1, 0);
		for (int i = 0; i < n; i++) {
			int k;
			cin >> k;
			if (k > x || k == 0) {
				continue;
			}
			dp[k] = 1;
			pq.push(k);
		}
		int p = pq.top();
		pq.pop();
		en.push_back(p);
		/*
		while (!pq.empty()) {
			int k = pq.top();
			pq.pop();
			bool hantei2 = true;
			for (int i = 0; i < en.size(); i++) {
				if (k % en[i] == 0) {
					hantei2 = false;
					break;
				}
			}
			if (hantei2) {
				en.push_back(k);
			}
		}
		for (int i = 0; i < en.size(); i++) {
			for (int j = 0; j <= x; j++) {
				if (j < en[i]) {
				} else {
					dp[j] = max(dp[j], dp[j-en[i]]);
				}
			}
		}
		bool hantei = true;
		for (int i = x; i >= 0; i--) {
			if (dp[i] > 0 && furui[i] == 0) {
				cout << i << endl;
				hantei = false;
				break;
			}
		}
		if (hantei) {
			cout << "NA" << endl;
		}*/
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define xMAX 1000001
#define nMAX 31
bool prime[xMAX];

int dp[nMAX][xMAX];

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

bool isPrime(int n){
  return prime[n];
}

void solve(int n, int x, const int *v){
  rep(i,n+1)
    dp[i][0]=0;
  rep(i,x+1)
    dp[0][i]=0;

  int max=0;
  for(int i=1;i<n+1;i++)
    for(int j=1;j<x+1;j++){
      int Do =  dp[i][j-v[i]]+v[i];
      int Not = dp[i-1][j];
      if(j<v[i-1])
	dp[i][j] = Not;
      else{
	if (Do > Not){
	  dp[i][j] = Do;
	  if(max<Do && isPrime(Do) && Do<=x)
	    max=Do;
	}
	else
	  dp[i][j] = Not;
      }
    }

  /*
    putchar('\n');
    rep(i,n+1){
    printf("%d :",i+1);
    rep(j,x){
    printf("%2d ",dp[i][j]);
    }
    putchar('\n');
  */

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

int main(){
  sieve();
  int v[nMAX];
  for(;;){
    int n,x;
    cin >> n >> x;
    if(!(n+x)) break;
    v[0]=0;
    for(int i=1;i<n+1;i++)
      cin >> v[i];
    solve(n,x,v);
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

#define N_MAX 30
#define X_MAX 1000000

int n, x;
int P[N_MAX];
int dp[X_MAX+1];
bool isPrime[X_MAX+1];

void make() {
  fill(isPrime, isPrime + X_MAX + 1, true);
  isPrime[0] = isPrime[1] = false;
  for(int i = 2; i <= X_MAX; ++i) {
    if(!isPrime[i]) continue;
    for(int j = i*2; j <= X_MAX; j += i) isPrime[j] = false;
  }
}

void solve() {
  fill(dp, dp + x + 1, -1);
  dp[0] = 0;
  for(int i = 0; i < n; ++i) {
    for(int j = P[i]; j <= x; ++j) {
      if(dp[j-P[i]] == -1) continue;
      dp[j] = max(dp[j], dp[j-P[i]] + P[i]);
    }
  }
  for(int i = x; i >= 0; --i) {
    if(dp[i] != -1 && isPrime[dp[i]]) {
      cout << dp[i] << endl;
      return;
    }
  }
  cout << "NA" << endl;
}

main() {
  make();
  while((cin >> n >> x) && (n || x)) {
    for(int i = 0; i < n; ++i) {
      cin >> P[i];
    }
    solve();
  }
}