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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
bool com[1000005];
int main()
{
	for(int i=2;i<=1000000;i++)
	{
		if(com[i]) continue;
		for(int j=2;i*j<=1000000;j++) com[i*j] = true;
	}
	while(1)
	{
		int n,lim;
		bool dp[1000005]={};
		cin >> n >> lim; if(n==0) return 0;
		int h[35];
		for(int i=0;i<n;i++) cin >> h[i];
		dp[0] = true;
		for(int i=0;i<n;i++)
		{
			for(int j=h[i];j<=lim;j++)
			{
				if(dp[j]) continue;
				if(dp[j-h[i]]) dp[j] = true;
			}
		}
		for(int i=lim;i>=1;i--)
		{
			if(dp[i] && !com[i])
			{
				cout << i << endl; goto end;
			}
		}
		puts("NA"); end:;
	}
}

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( 1000001, true );;
static bool dp[ 31 ][ 1000001 ];
static int ans = 0, m;
static char n;
static int ar[ 31 ];

static void solve( const char now, 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 ( static int i = 2; i * i < 1000000; i++ ) {
		if ( is_pm[ i ] ) {
			for ( static int j = i * i; j <= 1000000; 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 ) {
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d", &ar[ i ] );
		}
		sort( ar, ar + n );
		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 <bits/stdc++.h>

#ifndef INCLUDE_BOOL_ARRAY_HPP
#define INCLUDE_BOOL_ARRAY_HPP

#include <cstring>
#include <limits>
#include <cassert>

namespace orliv {
template<unsigned int CAP>
struct BoolArray {
  typedef int memory_t;
  memory_t K;
  memory_t data[CAP];
  BoolArray() { clear_deep(); }
  void clear_deep(){ std::memset(data, sizeof(data), 0); K = 1; }
  void clear(){ K++; }
  bool within_k(int x, int k){ return data[x] + k >= K; }
  inline bool get(int x){ return data[x] == K; }
  inline void set(int x){ data[x] = K; }
};
}

#endif

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;
//{{{ templates
#define TT_ template<typename T>inline
#define TTF_ template<typename T,typename F>inline
TT_ T sq(T x){return x*x;}
TT_ T In(){T x;cin>>x;return x;}
TT_ void Out(T&x){cout<<x;}
TT_ void sort(T&v){sort(begin(v),end(v));}
TT_ void revs(T&v){reverse(begin(v),end(v));}
TT_ void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT_ int ubnd(T&v,typename T::value_type x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT_ int lbnd(T&v,typename T::value_type x){return lower_bound(begin(v),end(v),x)-begin(v);}
TTF_ void inpt(T&v,int n,F f){for(v.reserve(n);n--;v.emplace_back(f()));}
TTF_ void show(T&v,F f,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),f(x);cout<<e;}
TT_ typename T::iterator minel(T&v){return min_element(begin(v),end(v));}
TT_ typename T::iterator maxel(T&v){return max_element(begin(v),end(v));}

inline void fast_io(){ios_base::sync_with_stdio(0);cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline ll pow_mod(ll a,ll k,ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a,ll p){return pow_mod(a,p-2,p);}
//}}} priority_queue queue deque front stringstream max_element min_element insert count make_tuple

const int MAX_N = 10100100;
orliv::BoolArray<MAX_N> dp;
bool sieve[MAX_N];

int main()
{
  for (int i = 2; i * i <= MAX_N; i++){
    if (sieve[i]) continue;
    for (int j = i * i; j <= MAX_N; j += i){
      sieve[j] = true;
    }
  }

  int n, x;
  while (n = in(), x = in(), n){
    dp.set(0);
    for (int i = 0; i < n; i++){
      int cost = in();
      for (int j = 0; j + cost <= x; j++){
        if (dp.get(j)) dp.set(j + cost);
      }
    }
    for (int i = x; i > 0; i--){
      if (sieve[i] && dp.get(i)){
        printf("%d\n", i);
        goto END;
      }
    }
    puts("NA");
END:;
    dp.clear();
  }
  return 0;
}