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 <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool isPrime[1000001];
bool dp[1000001];
int main()
{
	fill(isPrime, isPrime+1000001, true);
	isPrime[0] = isPrime[1] = false;
	for (int i = 2; i < 1000001; ++i)
	{
		if (isPrime[i])
		{
			for (int j = 2*i; j < 1000001; j += i)
				isPrime[j] = false;
		}
	}

	int n, x;
	while (scanf("%d %d", &n, &x), n | x)
	{
		int v[30];
		for (int i = 0; i < n; ++i)
		{
			int t;
			scanf("%d", &t);
			v[i] = t;
		}

		fill(dp, dp+x+1, false);
		dp[0] = true;
		for (int i = 0; i <= x; ++i)
			if (dp[i])
				for (int j = 0; j < n; ++j)
					dp[i + v[j]] = true;

		int ans;
		for (ans = x; ans > 0; --ans)
			if (dp[ans] && isPrime[ans])
				break;
		if (ans)
			printf("%d\n", ans);
		else
			puts("NA");
	}

	return 0;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

bool prime[1000001];

int main(){
	int n,x;
	rep(i,1000002)prime[i]=true;
	prime[0]=prime[1]=false;
	loop(i,2,1010)for(int j=i*2;j<1000001;j+=i)prime[j]=false;
	while(cin>>n>>x,n||x){
		int dish[n];
		rep(i,n)cin>>dish[i];
		bool cost[x+10];
		rep(i,x+10)cost[i]=false;
		rep(i,n)if(dish[i]<x+1)cost[dish[i]]=true;
		cost[0]=true;
		rep(i,x+1){
			if(!cost[i])continue;
			rep(j,n){
				if(i+dish[j]>x)continue;
				cost[i+dish[j]]=true;
			}
		}
		bool check=true;
		for(int i=x;i>=0;i--){
			if(prime[i] && cost[i]){
				check=false;
				cout<<i<<endl;
				break;
			}
		}
		if(check)cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int n, x, ans;
int in[31], in2[31];
bool dp[1000001];
void solve(int a, int sum){
	for (int i = a; i <= n; i++){
		if (sum + in[i] > x&&!in2[i]){
			in[i] /= 100000;
			in2[i] = 1;
		}
		if (sum + in[i] <= x){
			if (dp[sum + in[i]])
				continue;
			if (sum + in[i] % 2){
				dp[sum + in[i]] = 1;
			}
			solve(i, sum + in[i]);
		}
	}
}
int main(){
	while (cin >> n >> x&&n&&x){
		ans = 0;
		fill(dp, dp + 1000001, 0);
		for (int i = 1; i <= n; i++){
			cin >> in[i];
			in[i] *= 100000;
			in2[i] = 0;
		}
		solve(1, 0);
		for (int i = x; i > 0; i--){
			if (dp[i]){
				for (int j = 5; j <= x; j++){
					if (j == i)
						ans = j;
					if (i % j == 0)
						break;
				}
				if (ans)
					break;
			}
		}
		if (ans)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

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 bool is_pm[ 10000010 ];
static bool dp[ 10000010 ];
static ull ans = 0, m;
static char n;
static vector<ull> ar;

static void solve( const ull how_much ) {
	if ( how_much > m  ) {
		return;
	}
	
	if ( dp[ how_much ] ) {
		return;
	}
	dp[ how_much ] = true;
	if ( is_pm[ how_much ] ) {
		ans = max( ans, how_much );
	}
	if ( how_much == m ) {
		return;
	}
	for ( int i = 0; i < ar.size(); i++ ) {
		solve( how_much + ar[ i ] );
	}
}

int main() {
	memset( is_pm, -1, sizeof( is_pm ) );
	for ( ull i = 2; i * i <= 10000004; i += 2 ) {
		if ( is_pm[ i ] ) {
			for ( ull j = i * i; j <= 10000004; j += i) {
				is_pm[ j ] = false;
				if ( i % 2 == 1 ) {
					j += i;
				}
			}
		}
		if ( i == 2 ) {
			--i;
		}
	}
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		ar.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d", &ar[ i ] );
		}
		sort( ar.begin(), ar.end() );
		ar.erase( unique( ar.begin(), ar.end() ), ar.end() );
		sort( ar.rbegin(), ar.rend() );
		memset( dp, false, sizeof( dp ) );
		ans = 0;
		solve( 0 );
		if ( ans == 0 ) {
			printf( "NA\n" );
		}
		else {
			printf( "%d\n", ans );
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string.h>

using namespace std;
vector<int> prime;
bool isPrime[1000001];
bool dp[1000001];
vector<int> price;

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

void solve()
{
	PrimeCalc();
	int n, x;
	while(cin >> n >> x, n || x)
	{
		price.clear();
		price.resize(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> price[i];
		}
		memset(dp, 0, sizeof(dp));
		for(int i = 0; i < n; ++i)
		{
			dp[price[i]] = true;
		}
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j <= x; ++j)
			{
				if(dp[j] && j + price[i] <= x)
				{
					dp[j + price[i]] = true;
				}
			}
		}
		bool ok_flag = false;
		for(int i = x; i >= 1; --i)
		{
			if(dp[i] == true && isPrime[i] == true)
			{
				cout << i << endl;
				ok_flag = true;
				break;
			}
		}
		if(!ok_flag)
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
bool a[1000001]{};
a[0]=a[1]=true;
for(int b=2;b<1000001;b++){
	if(!a[b]){
		for(int c=b*2;c<1000001;c+=b)a[c]=true;
	}
}
int x,y;
while(scanf("%d%d",&x,&y),x|y){
	bool u[1000000]{};
	u[0]=true;
	int p;
	for(int g=0;g<x;g++){
		scanf("%d",&p);
		for(int q=p;q<=y;q++)if(u[q-p])u[q]=true;
	}
	for(int h=y;h!=-1;h--){
		if(u[h]&&a[h]==false){printf("%d\n",h);break;}
		if(h==0)printf("NA\n");
	}
}
}