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++: #include <iostream>
#include <cstring>
using namespace std;

#define MAXNUM 1000001

bool prime[MAXNUM];

void eratosthenes()
{
	prime[0]=prime[1]=false;

	for(int i=2; i<MAXNUM; i++)
	{
		prime[i]=true;
	}
	
	for(int i=2; i*i<MAXNUM; i++)
	{
		if(prime[i])
		{
			for(int j=i*i; j<MAXNUM; j+=i)
			{
				prime[j]=false;
			}
		}
	}
}


bool dp[1000001];

int main()
{
	eratosthenes();
	int N,B,c[30];
	while(cin >> N >> B, (N||B))
	{
		memset(dp,0,sizeof(dp));
		for(int i=0; i<N; i++)
			cin >> c[i];
			
		dp[0]=1;
		
		for(int i=0; i<N; i++)
		for(int j=0; j<B; j++)
		{
			if(j+c[i]>B) break;
			if(dp[j]) dp[j+c[i]]=1;
		}
		
		bool g=false;
		for(int i=B; i>1; i--)
		{
			if(dp[i]&&prime[i])
			{
				g=true;
				cout << i << endl;
				break;
			}
		}
		if(!g) cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) string((s).rbegin(), (s).rend())
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;


bool isPrime[1000001];

void setPrime(){
	fill(isPrime, isPrime+1000001, true);
	for(int i = 4; i < 1000001; i+=2){
		isPrime[i] = false;
	}
	isPrime[0] = isPrime[1] = 0;
	for(int i = 3; i < 1000001; i+=2){
		if(isPrime[i] == true){
			for(int j = i*2; j < 1000001; j+=i){
				isPrime[j] = false;
			}
		}
	}
}

int dp[1000001];

int solve(vector<int> menu, int x){
	fill(dp, dp+1000001, -1);
	dp[0] = 0;

	int best = 0;
	for(int i = 0; i < menu.size(); i++){
		for(int j = 0; j+menu[i] <= x; j++){
			if(dp[j] != -1){
				dp[j+menu[i]] = true;
				if(isPrime[j+menu[i]]){
					best = max(best, j+menu[i]);
				}
			}
		}
	}

	return best;
}

int main(){
	setPrime();

	while(1){
		int n, x;
		cin >> n >> x;
		if(!n && !x) break;
		vector<int> menu(n);
		rep(i, n){
			cin >> menu[i];
		}

		int cost = solve(menu, x);
		if(cost){
			cout << cost << endl;
		}else{
			cout << "NA" << endl;
		}
	}

	return 0;
}

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

int sn=0;
int s[1000001];

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

int main(){
	p[0]=false;p[1]=false;
	for(int i=2;i<=1000000;i++){
		p[i]=true;
		for(int j=0;j<sn;j++){
			if(i%s[j] == 0){
				p[i]=false;
				break;
			}
			if(i<=s[j]*s[j]) break;
		}
		if(p[i]){
			s[sn]=i;
			sn++;
		}
	}

	while(cin>>n>>x,x!=0){
		for(int i=0;i<n;i++) cin>>v[i];
		fill(dp,dp+x,false);dp[0]=true;
		int ans=0;
		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;
				}
				if(p[i]) ans=i;
			}
		}
		if(ans) cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>

#define N 1000001

using namespace std;

bool prm[N];
void mkPrm()
{
  fill(prm, prm+N, true);
  prm[0] = prm[1] = false;

  for(int i = 2; i < N; i++)
    if(prm[i])
      for(int j = i+i; j < N; j+=i) prm[j] = false;

  return ;
}

int main()
{
  int n, x, a[30];
  bool val[N];

  mkPrm();
  while(cin>>n>>x && (n+x)){
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    fill(val, val+N, false);
    val[0] = true;

    for(int i = 0; i < n; i++){
      val[a[i]] = true;
      for(int j = i; j < n; j++)
	for(int k = a[i]+a[j]; k <= x; k+=a[j]) val[k] = true;
    }

    n = 0;
    for(int i = x; i > 1; i--)
      if(val[i] && prm[i]){
	n = i;
	break;
      }

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

  return 0;
}

C++: 
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};


//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

#define MAX_COUNT 1000000 + 1

vector<bool> prime_number(LL n){
	vector<bool> res(n + 1);
	for(LL i = 2; n >=  i * i; i++){
		if(res[i]  == false){
			for(LL j = i + i; n >= j; j += i){
				res[j] = true;
			}
		}
	}
	return res;
}


int main(int argc, char const *argv[]){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, x;
	vector<bool> primes = prime_number(1000000);
	while(true){
		cin >> n >> x;
		if(n == 0 && x == 0) break;
		vector<vector<bool> >  dp(n + 1, vector<bool>(x + 1));
		VI items(n);
		REP(i, n){
			cin >> items[i];
		}
		dp[0][0] = true;
		REP(i, n){
			REP(j, x + 1){
				int item_value = items[i];
				int v = item_value + j;

				if(dp[i][j]){
					dp[i + 1][j] = true;
					if(v < x + 1)
						dp[i][v] = true;
				}
			}
		}
		int ans = -1;

		for(int i = x; i >= 0; i--){
			if(dp[n][i] && !primes[i]){
				ans = i;
				break;
			}
		}
		if(ans > 0){
			cout <<ans << endl;
		}else{
			cout << "NA" << endl;
		}
	}
	return 0;
}