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, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> Pi;

#define MAX 1000000

vector<bool> prime(MAX, true);
int N, X;
int price[30];
int dp[31][1000001];

void PrimeJudge() {
	prime[0] = false;
	FOR(i, 1, sqrt(MAX)) {
		if(prime[i]) {
			for(int j = (i + 1); (i + 1) * j <= MAX; ++j) {
				prime[(i + 1) * j - 1] = false;
			}
		}
	}
	return;
}

int main() {
	PrimeJudge();
	while(scanf("%d %d", &N, &X), N || X) {
		rep(i, N) scanf("%d", &price[i]);
		rep(i, N) rep(j, X + 1) {
			if(j < price[i]) {
				dp[i + 1][j] = dp[i][j];
			}
			else {
				dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - price[i]] + price[i]);
			}
		}
		bool flag = false;
		for(int i = X; i >= 0; --i) if(dp[N][i] - 1 >= 0) {
			if(prime[dp[N][i] - 1]) {
				printf("%d\n", dp[N][i]);
				flag = true;
				break;
			}
		}
		if(!flag) printf("NA\n");
	}
	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;
 }
}

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( static const char now, static 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 ] );
		}
		sort( ar.begin(), ar.end() );
		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 <iostream>
using namespace std;
int n,budget,price[30];
int dp[31][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[0][j] = 0;
  }

  for(int i=0;i<n;i++) {
    for(int j=0;j<=budget;j++) {
      if(j-price[i]>=0) {
        dp[i+1][j] = max(dp[i][j], dp[i+1][j-price[i]] + price[i]);
      }
      else {
        dp[i+1][j] = dp[i][j];
      }
    }
  }
  for(int i=0;i<budget;i++) {
    if(is_prime[dp[n][budget-i]]) {
      cout << dp[n][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(){
	int n,m,x;
	vector<bool> prime(1000001,true);
	prime[0]=prime[1]=false;
	/*ツづ督づゥツつ「*/
	for(int i=2;i<=1000;i++)if(prime[i])for(int j=i+i;j<=1000000;j+=i)prime[j]=0;
	while(cin >> n >> x && n && x){
		vector<int> item(n);
		vector<bool> able(x+1,false);
		able[0]=true;
		
		for(int i=0;i<n;i++)cin >> item[i];
		
		for(int i=0;i<n;i++){
			for(int j=0;j+item[i]<=x;j++){
				able[j+item[i]] = able[j+item[i]] | able[j];
			}
		}
		for(int i=x;i>=0;i--){
			if(able[i] && prime[i]){
				cout << i << endl;
				break;
			}
			if(!i)cout << "NA" << endl;
		}
	}
}