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 <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 bool is_pm[ 10000001 ];
static bool dp[ 10000001 ];
static int ans = 0, m;
static char n;
static int ar[ 30 ];

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

int main() {
	memset( is_pm, 1, sizeof( is_pm ) );
	for ( int i = 2; i * i <= 10000000; i += 2 ) {
		if ( is_pm[ i ] ) {
			for ( int j = i * i; j <= 10000000; j += i) {
				is_pm[ j ] = false;
				if ( i % 2 == 1 ) {
					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;
}

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
ll m=0; int n=0; int ans=0;
bool flag;
vi v;
vb memo(MAX+10,true);
/*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++;
        }
    }
    while(cin>>n>>m,n+m){
        int temp;
        flag=false;
        rep(i,n){cin>>temp; v.push_back(temp);}
        for(int i=(int)m;i>=0;i--){
            if(check[i]){
                ans=i;break;}
        }
        bool array[MAX]={false};
        rep(i,n)array[v[i]]=true;
        for(int i=1;i<=ans;i++) {
            if(array[i])rep(j,n)if(i+v[j]<=ans)array[i+v[j]]=true;
        }
        flag=array[ans];
        if(flag) cout<<ans<<endl;
        else cout<<"NA"<<endl;
        v.clear();
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

/*素数判定*/
const int MAX = 1000100;
bool prime[MAX];

void sieve(){
	fill(prime, prime+MAX, true);
	prime[0] = prime[1] = false;

	for(int i=1; i < MAX; i++){
		if(!prime[i]) continue;
		for(int j=i+i; j < MAX; j += i){
			prime[j] = false;
		}
	}
}

const int MAX_X = 1000000;
const int MAX_N = 30;

bool b[MAX_X+5];

int main(){
	int n, x;
	sieve();

	while(cin >> n >> x, n+x){
		vector<int> v(n);
		rep(i, n) cin >> v[i];

		fill(b, b+MAX_X+5, false);
		b[0] = true;
		rep(i, n){
			for(int j=0; j <= x; j++){
				if(b[j] && v[i]+j <= x) b[v[i]+j] = true;
			}
		}
		int ret = -1;
		for(int i=0; i < x; i++) if(b[i] && prime[i]) ret = i;
		if(ret == -1) cout << "NA" << endl;
		else cout << ret << endl;
	}
}

C++: #include <iostream>
#include <cmath>
#define VMAX  1000000
using namespace std;

int main(){
  int k, v;
  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)){
    int K[k];
    char T[k][v+1];
    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;

  }
}