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<iostream>
#include<vector>

using namespace std;

vector<int> budget;
int price[30];
int max_budget;
int n;

void serch(int s_price)
{
	if(s_price>max_budget)
		return ;
	
	if(budget[s_price]==1)
	{
		budget[s_price]=2;
//		cout << "s_prime:" << s_price << endl;
	}
	else
		budget[s_price]=3;
	
	for(int i=0;i<n;i++)
	{
		if(budget[s_price+price[i]]==0 || budget[s_price+price[i]]==1)
			serch(s_price+price[i]);
//		else
//			return ;
	}
}

int main()
{
	int i;
	
	for(;;)
	{
		cin >> n >> max_budget ;
		if(n==0 && max_budget==0)
			break;
		
		int ans=0;
		budget.clear();
		
		for(i=0;i<1000000;i++)
			budget.push_back(1);
		
		budget[0]=budget[1]=0;
		
		for(i=0;i<1000000;i++)
		{
			if(budget[i]==1)
			{
				for(int j=2;i*j<1000000;j++)
				{
					budget[i*j]=0;
				}
			}
		}
		
/*		for(i=0;i<1000000;i++)
		{
			if(budget[i]==1)
				cout << "prime:" << i << endl;
		}
*/		
		for(i=0;i<n;i++)
			cin >> price[i] ;
		
		for(i=0;i<n;i++)
			serch(price[i]);
		
		for(i=max_budget;i>=0;i--)
		{
			if(budget[i]==2)
			{
				ans=1;
				cout << i << endl;
				break;
			}
		}
		
		if(ans==0)
			cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>

using namespace std;

int main() {
	int n, x;
	vector<int> furui(1000000+1, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i <= 1000000; i++) {
		if (furui[i] == 0) {
			for (int j = 2; i*j <= 1000000; j++) {
				furui[i*j] = 1;
			}
		}
	}
	while (cin >> n >> x) {
		if (n == 0 && x == 0) {
			break;
		}
		vector<int> en;
		priority_queue<int, vector<int>, greater<int> > pq;
		vector<int> dp(x+1, 0);
		for (int i = 0; i < n; i++) {
			int k;
			cin >> k;
			if (k > x || k == 0) {
				continue;
			}
			dp[k] = 1;
			pq.push(k);
		}
		en.push_back(pq.top());
		pq.pop();
		while (!pq.empty()) {
			int k = pq.top();
			pq.pop();
			bool hantei2 = true;
			for (int i = 0; i < en.size(); i++) {
				if (k % en[i] == 0) {
					hantei2 = false;
					break;
				}
			}
			if (hantei2) {
				en.push_back(k);
			}
		}
		for (int i = 0; i < en.size(); i++) {
			for (int j = 0; j <= x; j++) {
				if (j < en[i]) {
				} else {
					dp[j] = max(dp[j], dp[j-en[i]]);
				}
			}
		}
		bool hantei = true;
		for (int i = x; i >= 0; i--) {
			if (dp[i] > 0 && furui[i] == 0) {
				cout << i << endl;
				hantei = false;
				break;
			}
		}
		if (hantei) {
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
bool DP[31][1145149];
int cost[31];
bool prime(int N){
	if(N==0)return false;
	for(int i=2;i*i<=N;i++){
		if(N%i==0)return false;
	}
	return true;
}
int main()
{
	while(true){
		int N,X;
		scanf("%d%d",&N,&X);
		if(N==0&&X==0)return 0;
		for(int i=0;i<N;i++)scanf("%d",&cost[i]);
		for(int i=0;i<=N;i++){
			for(int j=0;j<=X;j++){
				DP[i][j]=false;
			}
		}
		DP[0][0]=true;
		for(int i=1;i<=N;i++){
			for(int j=cost[i-1];j<=X;j++){
				if(DP[i-1][j])DP[i][j]=true;
				if(DP[i-1][j-cost[i-1]])DP[i][j]=true;
				if(DP[i][j-cost[i-1]])DP[i][j]=true;
			}
		}
		bool F=true;
		for(int i=X;i>=1;i--){
			if(F&&DP[N][i]&&prime(i)){
				printf("%d\n",i);
				F=false;
			}
		}
		if(F)printf("NA\n");
	}
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
#define rep(i,j) for(int i=0;i<j;i++)
#define repi(i,j,k) for(int i=j;i<k;i++)
#define Max_N 1000010
bool prime[Max_N];
int menu[30];
bool dp[Max_N];
void eratos()
{
	repi(i,2,Max_N)prime[i]=true;
	prime[0]=false;
	prime[1]=false;
	for(int i=2;i<=sqrt((double)Max_N)+1;i++)
	{
		if(prime[i])
		{
			for(int j=2*i;j<Max_N;j+=i)
			{
				prime[j]=false;
			}
		}
	}
	return;
}
int main()
{
	fill(dp,dp+Max_N,false);
	eratos();
	while(1)
	{
		int n,x;
		cin >> n >> x;
		if(n==0&&x==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> menu[i];
		}
		dp[0]=true;
		int ans=0;
		for(int i=1;i<=x;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(i-menu[j]>=0&&dp[i-menu[j]])
				{
					dp[i]=true;
				}
			}
			if(prime[i]&&dp[i])
			{
				ans=i;
			}	
		}
		if(ans!=0)
		{
			cout << ans << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>

using namespace std;
int n, x, m, prises[30], flag[1000000];

int sosuu(int n) {
	if (n == 2) return 1;
	if (n < 2 || n % 2 == 0) return 0;
	for (int i = 3; i * i <= n; i += 2)
			if (n % i == 0) return 0;
	return 1;
}

void max(int p) {
	if (p >= 1000000) return; 
	if (flag[p]) return;
	flag[p] = 1;
	for (int i = 0; i < n; ++i) {
		int k = p + prises[i];
		if (k > x) continue;
		if (k > m && sosuu(k)) m = k;
		max(k);
	}
}

int main() {
	for (;;) {
		for (int i = 0; i < 1000000; ++i) flag[i] = 0;
		m = 0;
		cin >> n >> x;
		if (n == 0 && x == 0) break;
		for (int i = 0; i < n; ++i)
			cin >> prises[i];

		max(0);

		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
	cin >> m;
	return 1;
}

C++: #include<stdio.h>
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
long p,cnt,cnt2,food;
int n,d=0,g,kakaku[31],flag,youso[1000001]={0};
int main()
{
	scanf("%d %ld",&n,&p);
	while(n!=0&&p!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%ld",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=1;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					if(cnt+kakaku[cnt2]<=p){
						youso[cnt+kakaku[cnt2]]=1;
					}
				}
			}
		}
		for(cnt=p;cnt>0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					flag=1;
					d=cnt;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		d=0;
		flag=0;
		for(cnt=1;cnt<=p;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %ld",&n,&p);
	}
	return 0;
}