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 int				long long
#define FOR( i, m, n )	for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )		FOR( i, 0, n )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )		(a).begin(), (a).end()
#define MP				make_pair

typedef pair<int, int> P;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

bool is_prime[1000001];
bool dp[1000001];

signed main(){
	fill( is_prime, is_prime + 1000001, true );
	is_prime[0] = is_prime[1] = false;

	REP( i, 1000001 ) {
		if( is_prime[i] ) {
			for( int j = i * i; j < 1000001; j += i ) {
				is_prime[j] = false;
			}
		}
	}

	int n, x;
	while( cin >> n >> x, n, x ) {
		fill( dp, dp + 1000001, false );

		vector<int> v( n );
		REP( i, n ) cin >> v[i];
		
		dp[0] = true;
		REP( i, x ) {
			if( dp[i] ) {
				REP( j, v.size() ) {
					if( i + v[j] <= x ) {
						dp[i + v[j]] = true;
					}
				}
			}
		}

		int ans = 0;
		REPR( i, x ) {
			if( dp[i] && is_prime[i] ) {
				ans =i;
				break;
			}
		}

		if( ans ) cout << ans << endl;
		else	  cout << "NA" << endl;

	}

}

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

bool prime[10000001];
bool dp[10000001];
int menu[30];
int n,x;

void make_prime()
{
  memset(prime,true,sizeof(prime));
  prime[0]=prime[1]=false;
  for(int i=2;i<10000001;i++)
    {
      if(prime[i])
	{
	  for(int j=i+i;j<10000001;j+=i)
	    prime[j]=false;
	}
    }
}

int main()
{
  make_prime();
  for(;cin>>n>>x,n;)
    {
      memset(dp,false,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  cin>>menu[i];
	  dp[menu[i]]=true;	  
	}
      int ans=-1;
      for(int i=0;i<x;i++)
	{
	  if(dp[i])
	    for(int j=0;j<n;j++)
	      if(i+menu[j]<x)
		dp[i+menu[j]]=true;	  
	}
      for(int i=x;i>0;i--)
	if(prime[i]&&dp[i])
	  {
	    ans=i;
	    break;
	  }
      if(ans!=-1)
	cout<<ans<<endl;
      else
	cout<<"NA"<<endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, x;
int a[100];

bool is_prime[1001000];

bool dp[1001000];

int main(){
	REP(i, 1001000)is_prime[i] = 1;
	is_prime[0] = 0;
	for (int i = 2; i <= 1000000; i++){
		if (is_prime[i]){
			for (int j = 2 * i; j <= 1000000; j += i){
				is_prime[j] = 0;
			}
		}
	}
	while (cin >> n >> x&&n + x){
		REP(i, 1001000)dp[i] = 0;
		REP(i, n)cin >> a[i];
		dp[0] = 1;
		sort(a, a + n);
		REP(i, n){
			REP(j, x){
				if (dp[j] == 1 && j + a[i] <= x)	dp[j + a[i]] = 1;
			}
		}
		bool f = 1;
		for (int i = x; i > 0; i--){
			if (dp[i] && is_prime[i]){
				cout << i << endl;
				f = 0;
				break;
			}
		}
		if (f)cout << "NA" << endl;
	}
}

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 MAX 1000000+1

bool prime[MAX+1];

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

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

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

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

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

  if(dp[n][x])
    cout << dp[n][x] << endl;
  else
    cout << "NA" << endl;
}

int main(){
  sieve();
  int v[30];
  for(;;){
    int n,x;
    cin >> n >> x;
    if(!(n+x)) break;
    rep(i,n)
      cin >> v[i];
    solve(n,x,v);
  }
}

C++: // 2012/11/19 Tazoe

#include <iostream>
using namespace std;

int main()
{
	bool is_prime[1000001];

	is_prime[0] = is_prime[1] = false;
	for(int i=2; i<=1000000; i++){
		is_prime[i] = true;
	}

	for(int i=2; i*i<=1000000; i++){
		if(is_prime[i]){
			for(int j=i*i; j<=1000000; j+=i){
				is_prime[j] = false;
			}
		}
	}

	while(true){
		int n, x;
		cin >> n >> x;

		if(n==0&&x==0)
			break;

		int K[30];
		for(int i=0; i<n; i++){
			cin >> K[i];
		}

		bool dp[1000001];

		dp[0] = true;
		for(int i=1; i<=x; i++){
			dp[i] = false;
		}

		for(int i=0; i<n; i++){
			for(int j=K[i]; j<=x; j++){
				if(!dp[j]&&dp[j-K[i]]){
					dp[j] = true;
				}
			}
		}

		int ans = -1;
		for(int i=x; i>=2; i--){
			if(is_prime[i]&&dp[i]){
				ans = i;
				break;
			}
		}

		if(ans!=-1){
			cout << ans << endl;
		}
		else{
			cout << "NA" << endl;
		}
	}

	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<cctype>
#include<string>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define INF 2100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define sz(x) ((int)(x).size())
#define SORT(x) sort((x).begin(), (x).end())
#define RSORT(x) sort((x).begin(), (x).end(), greater<int>() )
#define clr(a,b) memset((a),(b),sizeof(a))
int kin[4545],flg[45454545],dp[45454545];	
void sosu()
{
	for(int i=2;i<=1000000;i++) flg[i]=1;
	for(int i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(int j=2;j*i<1000000;j++) flg[i*j]=0;
		}
	}
}
int main(void)
{
	sosu();
	int n,x,ff;
	while(cin>>n>>x) {
		if(!n&&!x) break;
		rep(i,0,n) cin>>kin[i];
		rep(i,0,x+1) dp[i]=0;
		dp[0]=1;
		rep(i,0,n) {
			rep(j,0,x) {
				if(dp[j]==1) {
					if(j+kin[i]<=x) dp[j+kin[i]]=1;
				}
			}
		}
		ff=0;
		for(int i=x;i>=2;i--) {
			if(dp[i]&&flg[i]) {cout<<i<<endl,ff=1;break;}
		}
		if(ff==0) cout<<"NA"<<endl;
	}
	return 0;
}