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<algorithm>
using namespace std;
bool dp[1000020];
bool notPrime[1000020];
int main(){
    notPrime[0] = notPrime[1] = true;
    for(int i = 2;i*i < 1000010;i++){
        for(int j = 2 * i;j < 1000010;j+=i){
            notPrime[j] = true;
        }
    }
    int n,x;
    while(cin>>n>>x,n){
        dp[0] = true;
        while(n--){
            int a;
            cin>>a;
            for(int i = 0;i <= x;i++){
                if(dp[i])dp[i+a] = true;
            }
        }
        bool flg = false;
        for(int i = x;i >= 2;i--){
            if(!notPrime[i] & dp[i]){
                cout<<i<<endl;
                flg = true;
                break;
            }
        }
        if(!flg)cout<<"NA"<<endl;
        for(int i = 0;i < 1000020;i++)dp[i] = false;
    }
}

C++: #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

int dt[40];
bool prime[100000];
bool dp[1000000];

int main(void){
	int n, x,lm;
	rep(i,100000){
		prime[i]=true;
	}
	for(int i=2;i<100000;i++){
		for(int j=i*2;j<100000;j+=i){
			prime[j]=false;
		}
	}

	while(cin>>n>>lm,n||lm){
		bool find = false;
		rep(i,100000){
			dp[i]=false;
		}
		dp[0] = 1;
		loop(i,0,n){
			cin>>dt[i];
			rep(j,lm){
				if(j + dt[i] > lm)continue;
				dp[j + dt[i]] |= dp[j];
			}
			
		}
		//rep(i,sizeof(okapon))if(okapon[i])cout<<i<<endl;
		for(int i = lm ; i >= 0 ; i --){
			if(i==1){cout<<"NA"<<endl;break;}
			if(dp[i] && prime[i]){cout<<i<<endl;break;}
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <map>
#include <cmath>

using namespace std;

vector<int> price;
list<int> primes;

const int M = 1000000;
const int L = 30;
const int D = 1;
const int S = M/D;
pair<int,int> memo[S][L];

void mk_primes(int x)
{
	for( int i = 3; i <= x; i+=2 ) {
		for( int j = 3; j <= sqrt(i); j++ ) {
			if( i % j == 0 ) {
				goto NEXT;
			}
		}
		primes.push_front(i);
	NEXT:;		
	}
}

int solve(int x, int pos)
{
	pair<int,int>& pa = memo[x%S][pos];
	if( pa.first == x && -1 < pa.second )
		return pa.second;

	int result = 0;

	int p = price[pos++];
	int s = x / p;

	if( price.size() == pos ) {

		if( s*p == x ) {
			result = 1;
			// cout << "(" << p << "*" << s << ")";
		}
		
		
	} else {

		for( int i = 0; i <= s; i++ ) {

			int t = i * p;
			result = solve(x-t, pos);
			if( result ) {
				// cout << "(" << p << "*" << i << ")";
				break;
			}
		}
	}

	pa.first = x;
	pa.second = result;
	return result;
}

int main(int argc, char* argv[])
{
	while(1) {

		int n; cin >> n;
		int x; cin >> x;

		if( n == 0 && x == 0)
			break;
	
		for( int i = 0; i < n; i++ ) {
			int p; cin >> p;
			price.push_back(p);
		}

		mk_primes(x);

		for( int i = 0; i < S; i++ ) {
			for( int j = 0; j < L; j++ ) {			
				memo[i][j] = make_pair(-1,-1);
			}
		}

		for( auto p : primes ) {
			if( solve(p, 0) ) {
				cout << p << endl;
				goto LAST;
			}
		}

		cout << "NA" << endl;

	LAST:;

		price.clear();

	}

    return 0;
}

C++: #include<stdio.h>
#include <string.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;
}
int p,cnt,cnt2,food;
int n,d=0,g,kakaku[31],flag,youso[1000001]={0};
int main()
{
	scanf("%d %d",&n,&p);
	while(1){
		memset(kakaku, 0, sizeof(kakaku));
		memset(youso, 0, sizeof(youso));
		if(n==0&&p==0){
			break;
		}
		for(cnt=1;cnt<=n;cnt++){
			scanf("%d",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=0;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					if(cnt+kakaku[cnt2]<=p){
						youso[cnt+kakaku[cnt2]]=1;
					}
				}
			}
		}
		flag=0;
		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=0;cnt<=p;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %d",&n,&p);
	}
	return 0;
}

C++: #include <stdio.h>
#include <map>

using namespace std;


int *ms;
int n, m;

bool *dp;
bool *data;

bool search(int m){
	if(m == 0) return true;
	else if(m < 0) return false;
	else if(dp[m]) return data[m];

	for(int i=0;i<n;i++){
		if(m%ms[i] == 0){
			return dp[m] = data[m] = true;
		}else if(search(m-ms[i])){
			return dp[m] = data[m] = true;
		}
	}
	
	dp[m] = true;
	return data[m]=false;
}

int isprime(int p){
	if(p%2==0) return 0;
	for(int i=3;i*i<p;i++){
		if(p%i==0) return 0;
	}
	return 1;
}

int main(){
	while(1){
		scanf("%d%d", &n,&m);
		
		if(n==0) break;
		
		int i;
		dp = new bool[m+1];
		data = new bool[m+1];
		for(i=0;i<=m;i++) dp[i] = false;
		
		ms = new int[n];
		for(i=0;i<n;i++) scanf("%d", ms+i);
		
		int flag = 1;
		for(i=m;i>=0;i--){
			if(search(i) && isprime(i)){
				printf("%d\n", i);
				flag = 0;
				break;
			}
		}
		if(flag) puts("NA");
		
		delete dp;
		delete data;
		delete ms;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
	bool p[1000001];
	int n, x;

	p[0] = false;
	p[1] = false;
	for(int i = 2; i <= 1000000; i++) {
		p[i] = true;
	}

	for(int i = 2; i * i <= 1000000; i++) {
		for(int j = 2; j * i <= 1000000; j++) {
			p[i * j] = false;
		}
	}

	while( true ) {
		cin >> n >> x;

		if(n == 0 && x == 0) {
			break;
		}

		vector< int > dp(x + 1, 0);
		vector< int > money(n);

		for(int i = 0; i < n; i++) {
			cin >> money[i];
		}

		for(int i = 0; i < n; i++) {
			dp[money[i]]++;
		}

		for(int i = 0; i <= x; i++) {
			for(int j = 0; j < n; j++) {
				if(dp[i] == 0 || i + money[j] > x) {
					continue;
				}
				dp[i + money[j]]++;
			}
		}

		int ans = 0;
		for(int i = x; i >= 2; i--) {
			if(p[i] == true && dp[i] > 0) {
				ans = i;
				break;
			}
		}

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