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 <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <bitset>

using namespace std;
const int MAX = 1000010;
//bool isPrime[MAX] = {0};
bitset<MAX> isPrime;

void sieve(){
    bitset<MAX> find;
    int i;
    for(i=2;i*i < MAX;i++){
        if(find[i]) continue;
        isPrime.set(i);
        for(int j=i;j<MAX;j++){
            if(j%i == 0){
                find.set(j);
            }
        }
    }
    for(;i<MAX;i++){
        if(!find[i]) {
            //isPrime[i] = true;
            isPrime.set(i);
        }
    }
}

int main(){
    sieve();

    while(true){
        int n,x;
        cin >> n >> x;
        if(n==0 && x==0) return 0;
        vector<int> R(n);
        for(int i=0;i<n;i++){
            cin >> R[i];
        }

        bitset<MAX> S;
        S.set(0);
        //set<int> S;
        //S.insert(0);
        for(int i=0;i<R.size();i++){
            //    for(set<int>::iterator it=S.begin();it!=S.end();++it){
            for(int j=0;j<=x;j++){
                if(!S[j]) continue;
                //int newe = *it + R[i];
                int newe = j + R[i];
                if(newe > x) continue;
                //S.insert(newe);
                if(S[j]){
                    S.set(newe);
                }
            }
        }
        int maxn = -1;
        /*
        for(set<int>::iterator it=S.begin();it!=S.end();++it){
            if(isPrime[*it] && *it > maxn) maxn = *it;
        }
        */
        for(int i=0;i<=x;i++){
            if(isPrime[i] && S[i]) maxn = i;
        }
        if(maxn == -1) cout << "NA" << endl;
        else cout << maxn << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

const int N_MAX = 1e7 + 10;
bool is_prime[N_MAX];

// エラトステネスのふるい
void Eratosthenes(){
    FOR(i, 0, N_MAX){
        is_prime[i] = true;
    }
    is_prime[0] = false;
    is_prime[1] = false;
    for(ll i=2; i*i<=N_MAX; i++){
        if(is_prime[i]){
            int p = i;
            int step_p = p;
            // それの倍数を消していく
            while(p < N_MAX){
                p += step_p;
                if(p<N_MAX){
                    is_prime[p] = 0;
                }
            }
        }
    }
}

// その金額を作れるかどうか
bool dp[1000001];

ll f(VI& V, ll budget){
  rep(i, 1000001) dp[i] = false;

  dp[0]=true;
  for(ll v : V){
    rep(i, 1000001){
      if(dp[i]){
        if(i+v>1000000) break;
        dp[i+v] = true;
      }
    }
  }
  for(int i=budget; i>=0; i--){
    if(dp[i] && is_prime[i]) return i;
  }
  return -1;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll n, x;

    Eratosthenes();

    while(cin>>n>>x){
      if(n==0&&x==0) return 0;
      VI V(n);
      rep(i, n){
        cin >> V[i];
      }
      ll ans = f(V, x);
      if(ans==-1){
        p("NA");
      }else{
        p(ans);
      }

    }
    
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, X;
int A[30];
bool dp[1000001];

bool is_prime(int x) {
  int d = sqrt(x);
  for (int i=2; i<=d; i++) {
    if (x % i == 0) return false;
  }
  return true;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> N >> X) {
    if (N == 0 && X == 0) break;
    rep(i, N) cin >> A[i];
    rep(i, X+1) dp[i] = 0;

    dp[0] = 1;
    for (int i=0; i<N; i++) {
      int a = A[i];
      for (int j=0; j<=X-a; j++) dp[j+a] |= dp[j];
    }
    int m = 0;
    for (int i=X; i>=2; i--) {
      if (dp[i] && is_prime(i)) {
        m = i;
        break;
      }
    }
    if (m == 0) cout << "NA\n";
    else cout << m << "\n";
  }
  return 0;
}