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 <iostream>
#include <algorithm>
#include <cstring>

constexpr int MAX_N = 31, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
bool dp[MAX_C];
bool prime_num[MAX_C];

int main() {
	while (1) {
		std::cin >> n >> max;
		if (n + max == 0)
			break;
		std::memset(prime_num, false, sizeof(prime_num));
		std::memset(dp, false, sizeof(dp));
		for (int i = 0; i < n; ++i)
			std::cin >> cost[i];
		for (int i = 2; i * i <= max; ++i) {
			if (!prime_num[i]) {
				for (int j = 2; i*j <= max; ++j)
					prime_num[i*j] = true;
			}
		}
		int ans = 0;
		for (int i = 0; i < n; ++i)
			dp[cost[i]] = true;

		for (int i = 1; i <= MAX_C; ++i) {
			for (int j = 0; j < n; ++j) {
				if (i + cost[j] <= max&&dp[i])
					dp[i + cost[j]] = true;
			}
		}

		for (int i = max; i >= 0; --i) {
			if (dp[i] && !prime_num[i]) {
				ans = i; 
				break;
			}
		}
		if (!ans)
			std::cout << "NA" <<std::endl;
		else
			std::cout << ans << std::endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
bool p[1000001] = { false };
void prime();
int main()
{	prime();
while (true){
	bool dp[1000001] = { false };
	int a[31];
	int n, v;
	cin >> n >> v;
	if (n == 0){
		break;
	}
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	dp[0] = true;
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 < v - a[i]; i2++){
			if (dp[i2]){
				dp[i2 + a[i]] = true;
			}
		}
	}

	bool flag = true;
	for (int i = 1000000; i > 0; i--){
		if (dp[i] && !p[i]){
			flag = false;
			cout << i << endl;
			break;
		}
	}
	if (flag)
		cout << "NA" << endl;
}

}
void prime(){
	for (int i = 2; i <= 100000; i++){
		for (int i2 = 2; i2*i <= 100000; i2++){
			if (!p[i2*i]){
				p[i2*i] = true;
			}
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

vector<int> ch(1000001, 1);

int main(){
	ch[0]=ch[1]=0;
	FOR(i, 2, 1000001){
		if(ch[i]==0) continue; 
		
		for(int j=2*i; j<=1000000; j+=i){
			ch[j]=0;
		}
	}
	
	while(1){
		int n, x;
		scanf("%d%d", &n, &x);
		if(n==0) return 0;
		
		vector<int> v(n);
		REP(i, n) scanf("%d", &v[i]);
		
		vector<vector<int> > dp(n+1, vector<int>(x+1, -1));
		dp[0][0]=0;
		REP(i, n){
			REP(j, x+1){
				if(dp[i][j]!=-1){
					dp[i+1][j]=dp[i][j];
				}
				else {
					if(j>=v[i] && dp[i+1][j-v[i]]!=-1){
						dp[i+1][j]=ch[j];
					}
				}
			}
		}
		
		int ans=-1;
		REP(i, x+1) if(dp[n][i]==1) ans=i;
		
		if(ans!=-1) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[31][1000005];
int flg[1000005];
int main(void){
	for(i=1;i<1000001;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(j=2;j*i<=1000000;j++){
				flg[i*j]=0;
			}
		}
	}
	scanf("%d %d",&n,&x);
	while(n!=0 && x!=0){
		for(i=0;i<n;i++){
			for(j=0;j<=x;j++){
				if(j>0){
					dp[i][j]=-1;
				}
				else if(j==0){
					dp[i][j]=0;
				}
			}
		}
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[i][j]!=-1){
					dp[i][j+kin]=1;
				}
			}
			for(j=0;j<=x;j++){
				dp[i+1][j]=dp[i][j];
			}
		}
		int max=0;
		for(i=0;i<=x;i++){
			if(dp[n][i]==1 && flg[i]==1){
				max=i;
			}
		}
/*		for(i=0;i<=x;i++){
			printf("%d=%d ",i,flg[i]);
		}
		printf("\n");*/
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#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];
      sort(d,d+n,greater<int>());
      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 <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[2][1000001];
bool sosu[1000001];
int main()
{
	for(int i = 2; i < 1000000; i++)
	{
		if(!sosu[i])
		{
			for(int j = 2; j * i <= 1000000; j++)
			{
				sosu[i * j] = 1;
			}
		}
	}
	while(1)
	{
		cin >> n >> x;
		if(n == 0 && x == 0)
			break;
		for(int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;

		for(int i = 0; i < n; i++)
		{
			for(int j  = 0; j <= x; j++)
				if(dp[0][j])
					for(int k = 0; j + k * v[i] <= x; k++)
						dp[1][j + k * v[i]] = dp[0][j];
		
			for(int j = 0; j <= x; j++)
				dp[0][j] = dp[1][j];
			fill(dp[1], dp[1] + x + 1, 0);
		}
					for(int i = x; i >= 0; i--)
					{
						if(!sosu[i] && dp[0][i])
						{
							if(i)
								cout << i << endl;
							else
								cout << "NA" << endl;
							break;
						}
					}
				}
				return 0;
			}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <bitset>

using namespace std;
const int MAX = 1000000;
//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];
        }

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