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] != 0 {
				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>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

// nまでの素数のリストを作成(nを含む) O(N log log N)
vector<bool> make_prime_list(int n) {
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;

    vector<int> prime_list;
    for (int i = 2; i < n + 1; ++i) {
        if (!is_prime[i]) { continue; }
        prime_list.emplace_back(i);

        for (int j = i + i; j < n + 1; j += i) {
            is_prime[j] = false;
        }
    }
//    return prime_list;  // 素数のリスト
    return is_prime;  // すべての数値について素数かどうか
}

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

	auto prime_list = make_prime_list(1000001);
	prime_list[1] = true;

	while (true) {
        int n, x;
        cin >> n >> x;
        if (n == 0 or x == 0) {
            break;
        }

        vector<int> items;
        FOR(i, 0, n) {
            int p;
            cin >> p;
            items.emplace_back(p);
        }

        vector<bool> dp(x + 10, false);
        dp[0] = true;

        FOR(i, 0, n) {
            int p = items[i];
            FOR(j, 0, dp.size()) {
                if (dp[j] and j + p < dp.size()) {
                    dp[j + p] = true;
                }
            }
        }

        int ans = -1;
        FOR(i, 0, x + 1) {
            if (prime_list[i] and dp[i]) {
                ans = max(ans, i);
            }
        }

        if (ans == -1) {
            print("NA");
        }
        else {
            print(ans);
        }
    }

	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <string>
using namespace std;

#define PI 3.14159265358979

#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
#define repd(i,s,e) for(int i=s; i>=e; i--)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

int n,x;
int d[31];
const int MAX_C = 1000001;
bool primes[MAX_C];
bool dp[MAX_C];

void eratosthenes() {
	fill(primes, primes+MAX_C, true);
	primes[0] = primes[1] = false;
	repa(i,2,MAX_C-1) {
		if(primes[i]) {
			int j = i<<1;
			while(j < MAX_C) {
				primes[j] = false;
				j += i;
			}
		}
	}
}

void solve() {
	memset(dp, 0, sizeof(dp));
	dp[0] = true;
	repa(i,1,x) {
		if(!dp[i]) {
			rep(j,n) {
				if(i-d[j] >= 0 && dp[i-d[j]]) {
					dp[i] = true;
					break;
				}
			}
		}
	}

	repd(i,x,0) {
		if(dp[i] && primes[i]) {
			cout << i << endl;
			return;
		}
	}
	cout << "NA" << endl;
}

int main() {
	eratosthenes();
	while(cin>>n>>x, n|x) {
		rep(i,n) cin>>d[i];
		solve();
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

#define MAX 1000010
#define INF (1<<29)

int N,X;
int a[30],dp[MAX];
bool isprime[MAX];

void sieve(){
  int i,j;

  rep(i,MAX) isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for(i = 2; i*i <= MAX; i++) if(isprime[i]){
    for(j = 2*i; j <= MAX; j += i) isprime[j] = false;
  }
}

int main(){
  int i,j,k;

  sieve();

  for(;;){
    scanf("%d %d",&N,&X); if(N == 0 && X == 0) break;
    rep(i,N) scanf("%d",&a[i]);

    rep(i,MAX) dp[i] = INF;
    dp[0] = 0;

    int res = 0;
    rep(i,N){
      for(j = 0; j <= X; j++) if(j - a[i] >= 0 && dp[j] > dp[j - a[i]] + 1){
        dp[j] = dp[j - a[i]] + 1;
        if(isprime[j]) res = max(res, j);
      }
    }

    if(res == 0) cout << "NA" << endl; else cout << res << endl;
  }

  return 0;
}

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

#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int n,m;
int sina[30];
char memo[1000001];
char sosu[1000001];

int N(int val){
	if(val<0){
		return 0;
	}
	
	if(val==0){
		return 1;
	}
	
	if(memo[val]!=-1){
		return memo[val];
	}
	
	int ga=0;
	rep(i,n){
		if(N(val-sina[i])==1){
			ga=1;
			break;
		}
	}
	
	return memo[val]=ga;
}

int main(){
	sosu[0]=1;sosu[1]=1;
	for(int i=2;i<1000001;i++){
		if(sosu[i]==1){
			continue;
		}
		for(int j=i*2;j<1000001;j+=i){
			sosu[j]=1;
		}
	}
	
	while(1){
		sintt(n,m);
		if(n==0)break;
		rep(i,n){
			sint(sina[i]);
		}
		
		rep(i,1000001){
			memo[i]=-1;
		}
		
		int flg=1;
		int ans=0;
		
		
		for(int i=m;i>0;i--){
			if(sosu[i]!=0){
				continue;
			}
			
			if(N(i)==1){
				ans=i;
				flg=0;
				break;
			}
		}
		
		/*
		rep(i,m){
			if(memo[i]==0){
				printf("%d\n",i);
			}
		}*/
		
		if(flg==1){
			puts("NA");
		}else{
			printf("%d\n",ans);
		}
	}
}