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 <iostream>
#include <queue>
#include <string.h>
#include <math.h>

using namespace std;

const int N = 1000000;

int prime[N+1];
int table[N+1];
int dish[30];
int n, x;
int max_val;

void setPrime()
{
	memset(&prime[0], 0, sizeof(int) * (N+1));
	prime[2] = 1;
	for(int i=2; i<=N; i++)
	{
		bool is_prime = true;
		int limit = sqrt(i);
		for(int k=2; k<=limit; k++)
		{
			if(i % k == 0)
			{
				is_prime = false;
				break;
			}	
		}
		
		if(is_prime)
		{
			prime[i] = 1;
		}
	}
}

void solve()
{
	int price;
	queue<int> q;
	q.push(0);
	
	while(!q.empty())
	{
		price = q.front();
		q.pop();
		
		if(price > x)
		{
			continue;
		}
		
		if(prime[price] == 1 && max_val < price)
		{
			max_val = price;
		}
		
		for(int k=0; k<n; k++)
		{
			int new_price = price + dish[k];
			if(table[new_price] == 0)
			{
				table[new_price] = 1;
				q.push(new_price);
			}
		}
	}
}

int main()
{
	setPrime();
	
	while( 1 )
	{
		cin >> n >> x;
		if(n==0 && x==0)
		{
			break;
		}
		
		clock_t t = clock();
		for(int i=0; i<n; i++)
		{
			cin >> dish[i];
		}
		
		memset(&table[0], 0, sizeof(int)*(N+1));
		max_val = 0;
		solve();
		
		if(max_val != 0)
		{
			printf("%d\n", max_val);
		}
		else
		{
			printf("NA\n");
		}
	}
}

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 <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 NCLR(a) memset((a), -1 ,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 = 1000000007;
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 int dp[ 31 ][ 1000001 ];
static bool is_pm[ 1000001 ];
static int n, m;
static VI _array;

int solve( int now, int many ) {
	if ( dp[ now ][ many ] >= 0 ) {
		return dp[ now ][ many ];
	}
	if ( now == n ) {
		if ( is_pm[ many ] ) {
			return dp[now][many] = many;
		}
		return dp[now][many] = 0;
	}
	int res = 0;
	for ( int i = 0; many + _array[ now ] * i <= m; i++ ) {
		res = max( res, solve( now + 1, many + _array[ now ] * i ) );
	}
	return dp[ now ][ many ] = res;
}

int main() {
	NCLR( is_pm );
	for ( int i = 2; sq( i ) <= 1000000; i += 2 ) {
		if ( is_pm[ i ] ) {
			for ( int j = i * i; j <= 1000000; j += i ) {
				is_pm[ j ] = false;
				if ( i != 2 ) {
					j += i;
				}
			}
		}
		if ( i == 2 ) {
			--i;
		}
	}
	is_pm[ 0 ] = is_pm[ 1 ] = false;
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		_array.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d", &_array[ i ] );
		}
		sort( _array.rbegin(), _array.rend() );
		_array.erase( unique( _array.begin(), _array.end() ), _array.end() );
		n = _array.size();
		NCLR( dp );
		int ans = solve( 0, 0 );
		if ( ans ) {
			printf( "%d\n", ans );
		}
		else {
			printf( "NA\n" );
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>

using namespace std;
 
const int INF = 1<<30;
const int MAX = 1000000;
typedef long long ll;
 
int main() {
    bool dp[MAX+1];
    bool prime[MAX+1];
    int n, m;
    memset(prime, true, sizeof(prime));

    for(int i = 2; i < sqrt(MAX); i++) {
        if(prime[i] == false)
            continue;
        for(int j = i*2; j < MAX; j += i)
            prime[j] = false;
    }

    while(cin >> n >> m, n) {
        for(int i = 0; i <= MAX; i++)
            dp[i] = false;
        dp[0] = true;

        for(int i = 0; i < n; i++) {
            int x;
            cin >> x;
            for(int t = 0; t <= m-x; t++) {
                if(dp[t])
                    dp[t+x] = true;
            }
        }

        int res = -1;
        for(int i = m; i >= 2; --i) {
            if(dp[i] && prime[i]) {
                res = i;
                break;
            }
        }
        if(res ==  -1)
            cout << "NA" << endl;
        else
            cout << res << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int MAX = 1000001;

int main() {
    vector<bool> is_prime(MAX, true);
    is_prime[0] = is_prime[1] = false;
    for(int i=2; i<MAX; ++i) {
        if(is_prime[i]) {
            for(int j=i+i; j<MAX; j+=i) {
                is_prime[j] = false;
            }
        }
    }
    int n, x;
    while(cin >> n >> x, n) {
        vector<int> v(n);
        for(int i=0; i<n; ++i) {
            cin >> v[i];
        }
        vector<bool> dp(x+1, false);
        dp[0] = true;
        for(int i=0; i<n; ++i) {
            for(int j=0; j+v[i]<=x; ++j) {
                dp[j+v[i]] = dp[j+v[i]] || dp[j];
            }
        }
        int res = 0;
        for(int i=x; i>=1; --i) {
            if(dp[i] && is_prime[i]) {
                res = i;
                break;
            }
        }
        if(res == 0) {
            cout << "NA" << endl;
        } else {
            cout << res << endl;
        }
    }
}