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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
bool mem[1000001];
bool unprime[1000001];
int main(void){
  int n,x;
  for(int i=2; i<1000001; i++){
    if(!unprime[i]){
      for(int k=2; i*k<1000001; k++){
        unprime[i*k] = true;
      }
    }
  }
  while(cin>>n>>x){
    if(n==0) break;
    memset(mem,0,sizeof(mem));
    mem[0] = true;
    REP(i,n){
      int money;
      cin>>money;
      for(int j=0; j+money<=x; j++){
        if(mem[j]) mem[j+money] = true;
      }
    }
    for(int i = x; i>=1; i--){
      if(i==1){
        cout<<"NA"<<endl;
        break;
      }else if(!unprime[i]&&mem[i]){
        cout<<i<<endl;
        break;
      }
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

vector<int> sieve(int n) {
	vector<int> res;
	vector<bool> is_prime(n + 1, true);
	
	is_prime[0] = is_prime[1] = false;
	
	for (int i = 2; i * i <= n; i++) if (is_prime[i]) {
		res.push_back(i);
		is_prime[i] = false;
		for (int j = 2; i * j <= n; j++) if (is_prime[i * j]) {
			is_prime[i * j] = false;
		}
	}
	
	REP(i, n + 1) if (is_prime[i]) res.push_back(i);
	sort(res.begin(), res.end());
	return res;
}

int n, x, v[30];
bool dp[1123456];

int main() {
	vector<int> prime = sieve(1000000);
	
	while (cin >> n >> x, n || x) {
		REP(i, n) scanf("%d", v + i);
		
		memset(dp, 0, sizeof(dp));
		dp[0] = true;
		REP(i, x + 1) if (dp[i]) {
			REP(j, n) if (i + v[j] <= x) dp[i + v[j]] = 1;
		}
		
		int ans = -1;
		REP(i, prime.size()) {
			if (x < prime[i]) break;
			if (dp[prime[i]]) chmax(ans, prime[i]);
		}
		
		if (ans == -1) puts("NA");
		else cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstring>
#define MAX 1000000
using namespace std;

int n,x,d[31];
bool p[MAX+1],dp[MAX+1];

void isPrime(){
   memset(p,1,sizeof(p));
   p[0] = p[1] = false;
   for(int i=2;i*i<=MAX;i++){
      for(int j=i+i;j<=MAX;j+=i)
         p[j] = false;
   }
}

void rec(int sum){
   if(dp[sum]) return;
   dp[sum] = true;
    for(int i=0;i<n;i++){
      if(sum + d[i] > x) return;
      rec(sum + d[i]);
   }
}

int main(){
   isPrime();
   bool ju;
   while(cin >> n >> x,n||x){
      for(int i=0;i<n;i++) cin >> d[i];
      memset(dp,0,sizeof(dp)); ju = false;
      rec(0);
      for(int i=x;i>0;i--){
         if(dp[i] && p[i]){ cout << i << endl; ju = true; break; }
      }
      if(!ju) cout << "NA" << endl;
   }
}

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(new_price > N)
				continue;
			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 <iostream>
#include <cmath>

using namespace std;

const int LIM = 1000001;
const int SQRT = sqrt(LIM);
bool dp[LIM];
bool is_prime[LIM];

using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    for(int i = 0; i < LIM; ++i) {
        is_prime[i] = true;
    }
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i <= SQRT; ++i) {
        if(is_prime[i]) {
            for(int j = i*i; j < LIM; j += i) {
                is_prime[j] = false;
            }
        }
    }
    while(true) {
        int N, X;
        cin >> N >> X;
        if(!N && !X) break;
        for(int i = 0; i <= X; ++i) {
            dp[i] = false;
        }
        dp[0] = true;
        int ans = -1;
        while(N--) {
            int A;
            cin >> A;
            for(int i = A; i <= X; ++i) {
                if(dp[i-A]) {
                    dp[i] = true;
                    if(is_prime[i]) ans = max(ans, i);
                }
            }
        }
        if(ans == -1) cout << "NA" << endl;
        else cout << ans << endl;
    }
    return 0;
}

C++: #include <stdio.h>

int n,m[30];
/*
bool prime[1000000];
*/
bool c[1000000];

void saiki(int total){
	int i;
	if(total<=0){
		return;
	}
	
	for( i=0 ; i<n ; i++){
		total-=m[i];
		if(c[total]==1)continue;
		if(total>=0){
			c[total]=1;
		}
		saiki(total);
	}
}

int check(int prime){
	int flg=0,i;
	if(prime%2==0)return 0;
	for(i=3;i<prime;i+=2){
		if(prime%i==0){
			flg=1;
		}
	}
	if(flg==1)return 0;
	return 1;
}

int main(void)
{
	int i,j;
	//f
	/*
	prime[0]=1;
	prime[1]=1;
	for(i=3;i<1000000;i++){
		if(i%2==0){
			prime[i]=1;
		}
	}
	for(i=3;i<1000000;i++){
		if(prime[i]==0){
			for(j=(i+i);j<1000000;j+=i){
				prime[j]=1;
			}
		}
	}
	*/
	//
	while(1){
		for(;;)
		int x;
		int flg=0;
		scanf("%d%d",&n,&x);
		if(n==0)break;
		
		for(i=0;i<n;i++){
			scanf("%d",&m[i]);
		}
		saiki(x);
		//
		for(i=0;i<x;i++){
			
			if(c[i]==1 && check(x-i)){
				flg=1;
				break;
			}
		}
		if(flg==1){
			printf("%d\n",x-i);
		}
		else puts("NA");
		for(i=0;i<1000000;i++){
			c[i]=0;
		}
		//
	}
	
	
	return 0;
}