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<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;

const int INF = 1<<30;
bool isPrime[2000000],dp[2000000];

void Init(){
  for(int i=0;i<=2000000;i++)isPrime[i] = false;
  for(int i=3;i<=2000000;i+=2)isPrime[i] = true;
  isPrime[2] = true;
  int limit = (int)sqrt((double)2000000)+1;
  for(int i=3;i<=limit;i+=2){
    if(!isPrime[i])continue;
    for(int j=i+i;j<=2000000;j+=i)isPrime[j] = false;
  }
}


int main(){
  int n,x,val;
  vector<int> The_rock;
  Init();
  while(cin >> n >> x && n && x){
    The_rock.clear();
    for(int i=0;i<n;i++){
      cin >> val;
      The_rock.push_back(val);
    }
    //int dp[x+10];
  
    for(int i=0;i<=1000000;i++){
      //dp[i] = INF;
      dp[i] = false;
    }
    for(int i=0;i<The_rock.size();i++)dp[The_rock[i]] = true;
    //dp[0] = 0;
    dp[0] = true;
    for(int i=0;i<The_rock.size();i++){
      for(int j=The_rock[i];j+The_rock[i]<=1000000;j++){
	if(dp[j-The_rock[i]])dp[j] = true;
      }
    }
    bool fin = false;
    for(int i=x+9;i>=0;i--){
      if(isPrime[i] && dp[i]){
	cout << i << endl;
	fin = true;
	break;
      }
    }
    if(!fin)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 bool is_pm[ 10000000 ];
static bool dp[ 10000000 ];
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 ( ( how_much % 2 == 1 || how_much == 2 ) && 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 = 3; i * i <= 10000000; i += 2 ) {
		if ( is_pm[ i ] ) {
			for ( int j = i * i; j < 10000000; j += i ) {
				is_pm[ j ] = false;
			}
		}
	}
	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 <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define INF 999999999
#define all(v) begin(v), end(v)
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
    
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

#define int long long

int n, x;
vector<bool> pr(1000005, 0);

int prime(){
    pr[0] = pr[1] = 1;
    FOR(i, 2, 1000005){
        if(pr[i]) continue;
        
        for(int j = 2; i*j <= 1000000; j++){
            pr[i*j] = 1;
        }
    }
}

int solve(){
    vector<int> v(n);
    REP(i, n) cin >> v[i];
    VSORT(v);

    vector<bool> dp(x+5, 0);
    dp[0] = 1;
    FOR(i, 2, x+1){
        REP(j, n){
            if(i-v[j] >= 0 && dp[i-v[j]]) dp[i] = 1;
        }
    }
    
    REPR(i, x){
        if(dp[i] && !pr[i]){ 
           cout << i << endl;
           return 0;
        }
    }
    
    cout << "NA" << endl;
}

int32_t main(){
    prime();
    while(1){
        cin >> n >> x;
        if(!n) return 0;
        solve();
    }
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=(b);i>=(a);i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second

bool prime[1000005];
void init(){
	rep(i,1000005){
		prime[i]=true;
	}
	prime[0]=prime[1]=false;
	reg(i,2,1000004){
		if(!prime[i])continue;
		for(int j=i*2;j<1000005;j+=i)prime[j]=false;
	}
}

int cam[1000005];

int main(void){
	init();
	int m,n;
	for(;;){
		scanf("%d%d",&n,&m);
		if(n==0)break;
		int dat[35];
		rep(i,n){
			scanf("%d",&dat[i]);
		}
		memset(cam,0,sizeof(cam));
		cam[0]=1;
		rep(i,n){
			int no=dat[i];
			reg(j,0,m-no){
				if(cam[j]==1)cam[j+no]=1;
			}
		}
		int ans=0;
		reg(i,2,m){
			if(cam[i]==1 && prime[i])ans=i;
		}
		if(ans!=0)printf("%d\n",ans);
		else printf("NA\n");
	}
		
	return 0;
}