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 ANSWER(x) cout<<x<<endl
#define debug(x) cout<<#x<<": "<<x
const int MAX_INT = 1000000;
bool er[1000001];
int n,x,ans;
int v[31];
int solve(int k,int m);
int memo[31][1000001];
int main(){
  memset(er,true,sizeof(er));
  for(int i=2;i<sqrt(MAX_INT);i++){
    if(er[i]){
      for(int j=0;i*(j+2)<MAX_INT;j++){
	er[i*(j+2)]=false;
      }
    }
  }
  while(1){
    cin >> n >> x;
    if(!n && !x)break;
    memset(memo,-1,sizeof(memo));
    for(int i=0;i<n;i++){
      cin >> v[i];
    }
    
    ans = solve(0,0);
    if(!ans)cout << "NA\n";
    else cout << ans << endl;
  }
}
int solve(int k,int m){
  if(memo[k][m] != -1)return memo[k][m];
  if(m > x)return 0;
  else if(k == n){
    if(m == 0)return 0;
    if(er[m])return m;
    else return 0;
  }
  return memo[k][m] = max(solve(k,m+v[k]),solve(k+1,m));
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
bool SOSU[1000001];
bool ARU[1000001];
void solve(){
	SOSU[0]=true,SOSU[1]=true;
	for(int i=2;i<1000001;i++){
		if(SOSU[i]==false){
			for(int j=2;i*j<1000001;j++){
				SOSU[i*j]=true;
			}
		}
	}
}
int main(){
	fill(SOSU,SOSU+1000000,false);
	solve();
	int n,x,sum,a[30];
	while(1){
	fill(ARU,ARU+1000000,false);
	cin >> n >> x;
	if(n==0&&x==0)break;
	sum=0;
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	ARU[0]=true;
	for(int i=0;i<=x;i++){
		if(ARU[i]==true){
				for(int j=0;j<n;j++){
					if(i+a[j]<=x)
					ARU[i+a[j]]=true;
				}
		}
	}
	int flg=0;
	for(int i=x;i>=1;i--){
		if(ARU[i]==true && SOSU[i]==false){
			cout  << i << endl;
			flg=1;
			break;
		}
	}
	if(flg==0)printf("NA\n");
}
}

C++: #include <iostream>
using namespace std;
int n,budget,price[30];
int dp[1000001];
bool is_prime[1000001];

void sieve() {
  for(int i=0;i<1000001;i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;

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

void solve() {
  for(int j=0;j<=budget;j++) {
    dp[j] = 0;
  }

  for(int i=0;i<n;i++) {
    for(int j=0;j<=budget;j++) {
      if(j-price[i]>=0) {
        dp[j] = max(dp[j], dp[j-price[i]] + price[i]);
      }
      else {
        dp[j] = dp[j];
      }
    }
  }
  for(int i=0;i<budget;i++) {
    if(is_prime[dp[budget-i]]) {
      cout << dp[budget-i] << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  sieve();
  while(cin >> n >> budget) {
    if(n==0&&budget==0) break;
    for(int i=0;i<n;i++) {
      cin >> price[i];
    }
    solve();
  }
}

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

int main() {

	while(true)
	{
		int N,X;
		cin >> N >> X;
		if(N==0 && X==0)break;
		
		vector<int> food;
		for(int i=0;i<N;i++){
			int m;
			cin >> m;
			food.push_back(m);
		}
		
		vector<int> num(X+1);
		vector<int> prim(X+1);
		fill(num.begin(),num.end(),0);
		fill(prim.begin(),prim.end(),1);
		for(int i=0;i<N;i++){
			num[ food[i] ]=1;
		}
		for(int i=0;i<X+1;i++){
			if( num[i]!=0){
				for(int j=0;j<N;j++){
					if( i+food[j]>=num.size())break;
					num[i+food[j]]=1;
				}
			}
		}
		prim[0]=0;
		prim[1]=0;
		for(int i=2;i<X+1;i++){
			if( prim[i]!=0 ){
				int x=i+i;
				while(x<X+1){
					prim[x]=0;
					x+=i;
				}
			}
		}
		int ans=-1;
		for(int x=X;x>=0;x--){
			if( prim[x]==1 && num[x]!=0){
				ans = x;
				break;
			}			
		}
		if(ans>0){
			cout << ans << endl;
		}else{
			cout << "NA" << endl;
		}
	}
	


	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <vector>
#include <cmath>

using namespace std;

int main() {
  const int lim = 1000000;
  vector<bool> primes( lim + 1 );
  fill( primes.begin(), primes.end(), true );
  primes[ 0 ] = primes[ 1 ] = false;
  const int l = (int)sqrt( lim );
  for ( int i = 2; i <= l; i++ ){
	if ( primes[ i ] ) {
	  for ( int j = i * i; j <= lim; j += i ) {
		primes[ j ] = false;
	  }
	}
  }
  
  while( true ) {
	int n, x;
	cin >> n >> x;
	if ( n == 0 and x == 0 ) break;
	
	vector<int> vs( n );
	for ( int i = 0; i < n; i++ ) {
	  cin >> vs[ i ];
	}
	sort( vs.begin(), vs.end() );
	vector<bool> cands( x + 1 );
	int ans = -1;
	fill( cands.begin(), cands.end(), false );
	cands[ 0 ] = true;
	for ( int i = 0; i <= x; i++ ) {
	  if ( cands[ i ] ) {
		for ( int v : vs ) {
		  if ( i + v <= x ) {
			cands[ i + v ] = true;
		  }
		}
		if ( primes[ i ] ) {
		  ans = i;
		}
	  }
	}
	if ( ans == -1 ) {
	  cout << "NA" << endl;	  
	} else {
	  cout << ans << endl;
	}
  }
}

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

using namespace std;

bool prime[1000001],dp[1000001];

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

int n,x;
vector<int> price;

int main(){
  make_prime();

  while(cin>>n && n){
    cin>>x;
    for(int i=0;i<1000001;i++){
      dp[i]=false;
    }
    for(int i=0;i<n;i++){
      int tmp;
      cin>>tmp;
      price.push_back(tmp);
      dp[tmp]=true;
    }

    sort(price.begin(),price.end());

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

    int ans=0;

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

    if(ans==0){
      cout<<"NA"<<endl;
    }else{
      cout<<ans<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<cmath>
using namespace std;
#define KMAX 30
#define VMAX 1000000
int K[KMAX];
unsigned char T[KMAX][VMAX+1];
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 limit = (int)sqrt((double)n) + 1;
 for ( int i = 3; i <= limit; i += 2 ){
if ( !prime[i] ) continue;
for ( int j = i + i; j <= n; j += i ) prime[j] = false;
 }
}
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;
 }
}