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] != -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>
#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;
		}
	}
}

C++: #include <iostream>
#include <string.h>
#include <set>
using namespace std;

int main()
{
	bool prime[ 1000000 ];
	memset( prime, false, sizeof(prime) );
	prime[ 0 ] = prime[ 1 ] = true;
	for( int i = 2; i < 1000000; ++i ){
		if( prime[ i ] ) continue;
		for( int j = i + i; j < 1000000; j += i ){
			prime[ j ] = true;
		}
	}

	int n, x;
	while( cin >> n >> x && ( n || x ) ){
		bool* price = new bool[ x + 1 ]();
		set< int > menu;
		for( int i = 0; i < n; ++i ){
			int p;
			cin >> p;
			menu.insert( p );
			price[ p ] = true;
		}
		int ans = 0;
		for( int i = 0; i <= x; ++i ){
			if( !price[ i ] ) continue;
			if( !prime[ i ] ){
				ans = i;
			}
			set< int >::iterator itr = menu.begin();
			while( itr != menu.end() ){
				int a = i + *itr;
				if( a <= x ){
					price[ a ] = true;
				}
				++itr;
			}
		}

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

		delete [] price;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;

int main(){
    bool nonprime[1000001] = {};
    nonprime[0] = nonprime[1] = true;
    for(int i = 2; i <= 1000000; i++){
        if(!nonprime[i]){
            for(int j = i+i; j <= 1000000; j += i){
                nonprime[j] = true;
            }
        }
    }
    
    while(1){
        int n, x;
        cin >> n >> x;
        
        if(n + x == 0)  break;
    
        bool valid[x+1] = {};
        valid[0] = true;
        
        int v;
        for(int i = 0; i < n; i++){
            cin >> v;
            for(int j = 0; j <= x-v; j++){
                if(valid[j])    valid[j+v] = true;
            }
        }
        
        int ans = -1;
        for(int i = x; i >= 1; i--){
            if(!nonprime[i]){
                ans = i;
                break;
            }
        }
        
        if(ans == -1)   cout << "NA" << endl;
        else            cout << ans << endl;
    }
    return 0;
}