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] != 0 {
				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 <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[ 31 ][ 10000001 ];
static int ans = 0, m;
static char n;
static VI ar;

static void solve( const char now, const int how_much ) {
	if ( how_much > m  ) {
		return;
	}
	if ( is_pm[ how_much ] ) {
		ans = max( ans, how_much );
	}
	if ( now == n || dp[ now ][ how_much ] ) {
		return;
	}
	
	dp[ now ][ how_much ] = true;
	solve( now, how_much + ar[ now ] );
	solve( now + 1, how_much );
}

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

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

int canmake[1100000];
int sosu[1100000];

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		
		if(n==0 && m==0)break;
		
		rep(i,1100000)canmake[i]=sosu[i]=0;
		
		
		sosu[0]=sosu[1]=1;
		for(int i=2;i<=1000000;i++){
			if(sosu[i]==1)continue;
			for(int j=i*2;j<1000000;j+=i){
				sosu[j]=1;
			}
		}
		
		
		canmake[0]=1;
		
		int sina[100];
		rep(i,n)cin>>sina[i];
		for(int i=0;i<1000000;i++){
			if(canmake[i]==0)continue;
			rep(j,n){
				if(i+sina[j]<=1000000){
					canmake[i+sina[j]]=1;
				}
			}
		}
		
		
		int ans=-1;
		for(int i=m;i>=0;i--){
			if(sosu[i] == 0 && canmake[i]==1){
				ans =i;
				break;
			}
		}
		
		
		if(ans==-1){
			puts("NA");
		}else{
			printf("%d\n",ans);
		}
	}
}

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

const int Max = 1000001;

int dishes[31];
bool dish[Max];
bool isPrime[Max];

int main(){

	for (int i = 2; i < Max; i++)isPrime[i] = true;
	for (int i = 2; i < std::sqrt(Max); i++){
		if (isPrime[i]){
			for (int j = i * 2; j < Max; j += i)isPrime[j] = false;
		}
	}

	int n, x;
	while (std::cin >> n, n){

		std::cin >> x;
		memset(dish, 0, sizeof(dish));

		for (int i = 0; i < n; i++){
			std::cin >> dishes[i];
		}

		dish[0] = true;
		for (int i = 0; i < x; i++){
			if (dish[i]){
				for (int j = 0; j < n; j++){
					dish[i + dishes[j]] = true;
				}
			}
		}

		for (int i = x; i >= 0; i--){
			if (dish[i] && isPrime[i]){
				std::cout << i << std::endl;
				break;
			}
			if (i == 0)std::cout << "NA" << std::endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 1000009
/*void dfs(int num){
    if(flag) return;
    if(ans==num){flag=true;return;}
    if(num>ans){memo[num]=false;return;}
    else rep(i,n){
        cout<<num<<endl;
      if(num+v[i]<=ans&&memo[num+v[i]])dfs(num+v[i]);
    }
    memo[num]=false;
    return;
}*/
int main(){
    vector<bool>check(MAX+1);  //index number will be checked
    rep(i, MAX+1)check[i] = true;
    vector<int>Primenumber(MAX+1,0); //Primearray
    int counter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(check[i]){
            for(int j = 2;i*j<MAX;j++)
                check[i*j] = false;//?´???°??????Primearray???????´??????????????????°?????¨???false???
            Primenumber[counter] = i;
            counter++;
        }
    }
    int m,n,ans; bool flag;
    while(cin>>n>>m,n+m){
        vi v;
        int temp;
        flag=false;
        rep(i,n){cin>>temp; v.push_back(temp);}
        for(int i=m;i>=2;i--){
            if(check[i]){
                ans=i;break;}
        }
        bool array[MAX]; rep(i,MAX)array[i]=false;
        rep(i,n)array[v[i]]=true;
        for(int i=2;i<=ans;i++) {
            if(array[i])rep(j,n)if(i+v[j]<=MAX)array[i+v[j]]=true;
            if(array[ans])break;
        }
        if(array[ans]) cout<<ans<<endl;
        else cout<<"NA"<<endl;
    }
}