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])
		if n == 0 && x == 0 {
			break
		}
		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 <stdio.h>
#include <algorithm>
using namespace std;
int n, x, a[30]; bool dp[1000001], prime[1000001];
int main() {
	prime[0] = true, prime[2] = true;
	for(int i = 3; i <= 1000000; i += 2) prime[i] = true;
	for(int i = 3; i <= 1000; i += 2) {
		if(!prime[i]) continue;
		for(int j = i * i; j <= 1000000; j += i * 2) prime[j] = false;
	}
	while(scanf("%d%d", &n, &x), n) {
		for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n);
		for(int i = 0; i < x; i++) dp[i + 1] = false; dp[0] = true;
		for(int i = 1; i <= x; i++) {
			for(int j = 0; j < n && i >= a[j]; j++) {
				if(dp[i - a[j]]) {
					dp[i] = true; break;
				}
			}
		}
		for(int i = x; i >= 0; i--) {
			if(dp[i] && prime[i]) {
				printf(i ? "%d\n" : "NA\n", i); break;
			}
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[2][1000001];
bool sosu[1000001];
int main()
{
	for(int i = 2; i < 1000000; i++)
	{
		if(!sosu[i])
		{
			for(int j = 2; j * i <= 1000000; j++)
			{
				sosu[i * j] = 1;
			}
		}
	}
	while(1)
	{
		cin >> n >> x;
		if(n == 0 && x == 0)
			break;
		for(int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;

		for(int i = 0; i < n - 1; i++)
		{
			for(int j  = 0; j <= x; j++)
				if(dp[0][j])
					for(int k = 0; j + k * v[i] <= x; k++)
						dp[1][j + k * v[i]] = dp[0][j];
		
			for(int j = 0; j <= x; j++)
				dp[0][j] = dp[1][j];
			fill(dp[1], dp[1] + x + 1, 0);
		}
					for(int i = x; i >= 0; i--)
					{
						if(!sosu[i] && dp[0][i])
						{
							if(i)
								cout << i << endl;
							else
								cout << "NA" << endl;
							break;
						}
					}
				}
				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[ 10000001 ];
static bool dp[ 10000001 ];
static ull ans = 0, m;
static char n;
static ull ar[ 30 ];

static void solve( const ull 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 <algorithm>
using namespace std;

const int MAX = 2000000;
char p[MAX+1] = {0};
int a[31];
int n,x,ans;

void solve(int sum, char dp[MAX+1] ){
	if( sum > x ) return;
	
	for(int i=0 ; i < n ; i++ ){
		if( dp[sum+a[i]] == 0 ){
			dp[ sum+a[i] ] = 1;
			solve( sum+a[i] , dp );
		}
	}
}

int main(){
	for(int i=2 ; i < MAX+1 ; i++ ) p[i] = 1;
	for(int i=2 ; i*i < MAX+1 ; i++ ){
		if( p[i] ){
			for(int j=i+i ; j < MAX+1 ; j += i ){
				p[j] = 0;
			}
		}
	}

	while( cin >> n >> x , n||x ){
		for(int i=0 ; i < n ; i++ ){
			cin >> a[i];
		}

		ans = 0;
		char dp[MAX+1] = {0};
		solve( 0 , dp );
		for(int i=x ; i > 0 ; i-- ){
			if( dp[i] && p[i] ){
				ans = i;
				break;
			}
		}

		if( ans > 0 )
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
}

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

using namespace std;

bool table[1000001], dp[1000001];
int cs[30];
vector<int> primes;

int main() {
  table[2] = true;
  for (int i = 3; i <= 1000000; i += 2)
    table[i] = true;
  for (int i = 3; i <= 1000; i += 2)
    if (table[i])
      for (int j = 3 * i; j <= 1000000; j += 2 * i)
        table[j] = false;
  primes.push_back(2);
  for (int i = 3; i <= 1000000; i += 2)
    if (table[i])
      primes.push_back(i);
  for (;;) {
    int n, x;
    cin >> n >> x;
    if (!n && !x) return 0;
    for (int i = 0; i < n; i++)
      cin >> cs[i];
    fill(dp, dp+x+1, false);
    dp[0] = true;
    for (int i = 1; i <= x; i++)
      for (int j = 0; j < n; j++)
        if (i - cs[j] >= 0 && dp[i-cs[j]]) {
          dp[i] = true;
          break;
        }
    bool f = true;
    for (int i = upper_bound(primes.begin(), primes.end(), x)-primes.begin()-1; i >= 0; i--)
      if (dp[primes[i]]) {
        f = false;
        cout << primes[i] << endl;
        break;
      }
    if (f)
      cout << "NA" << endl;
  }
  return 0;
}