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])
		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>

using namespace std;

bool budget[1000001];
bool P[1000001];

void eratos()
{
	for( int i = 0;i < 1000001;i++ ) P[i] = true;
	P[0] = P[1] = false;
	for( int i = 0;i < 1000001;i++ ){
		if( P[i] ) for( int j = 2;j * i < 1000001;j++ ) P[i * j] = false;
	}
}

void prim( int n, int x, int *v )
{
	for( int i = 1;i <= n;i++ ) budget[v[i]] = true;

	for( int i = 1;i <= x;i++ ) if( budget[i] ) {
		for( int j = 1;j <= n;j++ ) if( i + v[j] < 1000001 )budget[i + v[j]] = true;
	}
	int i;
	for( i = x;i >= 2;i-- ) if( budget[i] && P[i] ){ cout << i << endl; break;}
	if( i == 1 ) cout << "NA" << endl;
}

int main(int argc, char const* argv[])
{
	int n,x;
	int v[31];

	eratos();

	while( cin >> n >> x && x != 0 ){
		for( int i = 1;i <= n;i++ ) cin >> v[i];
		for( int i = 0;i < 1000001;i++ ) budget[i] = false;

		prim( n, x, v );
	}

	return 0;
}

C++: #include<iostream>
#include<math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

	int K[KMAX];
	int T[KMAX][VMAX];

void eratos(int n,bool prime[]){
	for(int i=0;i<=n;i++)prime[i]=false;
	for(int i=3;i<=n;i+=2)prime[i]=true;
	prime[2]=true;
	int lim=(int)sqrt((double)n)+1;
	for(int i=3;i<=lim;i+=2){
		if(!prime[i])continue;
		for(int j=i+i;j<=n;j+=i)prime[j]=false;
	}
}	

int main(){
	bool prime[VMAX+1];
	eratos(VMAX,prime);
	
	int k,v;

	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>=2;i--){
			if(prime[i] && T[k-1][i]){
				ans=i;
				break;
			}
		}
		if(ans==-1)cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}

}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[1000050],flg[1000050];
int main(void){
	scanf("%d %d",&n,&x);
	for(i=1;i<=1000000;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(j=2;j*i<=1000000;j++){
				flg[i*j]=0;
			}
		}
	}
	while(n!=0 && x!=0){
		for(j=1;j<=x;j++){
			dp[j]=-1;
		}
		dp[0]=0;
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[j]!=-1 && j+kin<=x){
					dp[j+kin]=1;
				}
			}
		}
		int max=0;
		for(i=0;i<=x;i++){
			if(dp[i]==1 && flg[i]==1){
				max=i;
			}
		}
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
#define rep(i,j) for(int i=0;i<j;i++)
#define repi(i,j,k) for(int i=j;i<k;i++)
#define Max_N 1000010
bool prime[Max_N];
int menu[30];
bool dp[Max_N];
void eratos()
{
	repi(i,2,Max_N)prime[i]=true;
	prime[0]=false;
	prime[1]=false;
	for(int i=2;i*i<=Max_N;i++)
	{
		if(prime[i])
		{
			for(int j=2*i;j<Max_N;j+=i)
			{
				prime[j]=false;
			}
		}
	}
	return;
}
int main()
{
	rep(i,Max_N)dp[i]=false;
	eratos();
	while(1)
	{
		int n,x;
		cin >> n >> x;
		if(n==0&&x==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> menu[i];
		}
		dp[0]=true;
		int ans=0;
		for(int i=1;i<=x;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(i-menu[j]>=0&&dp[i-menu[j]])
				{
					dp[i]=true;
				}
			}
			if(prime[i]&&dp[i])
			{
				ans=i;
			}	
		}
		if(ans!=0)
		{
			cout << ans << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, -1, 0, 1 , 1,  1, -1, -1 };

static vector<bool> is_pm( 10000001, true );
static bool dp[ 10000001 ];
static int ans = 0, m;
static char n;
static int ar[ 31 ];

static void solve( const int how_much ) {
	if ( how_much > m  ) {
		return;
	}
	if ( is_pm[ how_much ] ) {
		ans = max( ans, how_much );
	}
	if ( dp[ how_much ] ) {
		return;
	}
	
	dp[ how_much ] = true;
	for ( int i = 0; i < n; i++ ) {
		solve( how_much + ar[ i ] );
	}
}

int main() {
	for ( static int i = 2; i * i < 10000000; i += 2 ) {
		if ( is_pm[ i ] ) {
			for ( static int j = i * i; j < 10000000; j += i ) {
				is_pm[ j ] = false;
				if ( i != 2 ) {
					j += i;
				}
			}
			
		}
		if ( i == 2 ) {
			--i;
		}
	}
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d", &ar[ i ] );
		}
		sort( ar, ar + n );
		reverse( ar, ar + n );
		memset( dp, false, sizeof( dp ) );
		ans = 0;
		solve( 0 );
		if ( ans == 0 ) {
			printf( "NA\n" );
		}
		else {
			printf( "%d\n", ans );
		}
	}
	return 0;
}