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 <vector>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;

int T;
const int MAXT = 30;
int X;
const int MAXX = 1000000;
vector<bool> Prime(MAXX+1, true);

int main(void) {
	// calc prime
	int sq_n = (int)(sqrt((double)MAXX) + 0.9);
	Prime[0] = false;
	Prime[1] = false;
	for (int i = 2; i < sq_n; i++) {
		if (Prime[i]) {
			for (int j = i + i; j < MAXX; j += i) {
				Prime[j] = false;
			}
		}
	}

	while (cin >> T >> X, T || X) {
		// input and init
		vector<int> price(T);
		vector<bool> dp(X+1, false);
		for (int i = 0; i < T; i++) {
			cin >> price[i];
			dp[price[i]] = true;
		}

		// dp
		for (int i = 0; i <= X; i++){
			if (!dp[i]){
				continue;
			}
			for (int j = 0; j < T; j++) {
				int x = i + price[j];
				if (x <= X) {
					dp[x] = true;
				}
			}
		}

		int res = 0;
		for (int i = X; i >= 0; i-- ){
			if (dp[i] && Prime[i]){
				res = i;
				break;
			}
		}

		// output
		if (res){
			cout << res << endl;
		}else {
			cout << "NA" << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
using namespace std ;
void ans( int cnt ,int dp[] )
{
	int a = cnt + 1 ;
	LOOK :
	a-- ;
	while( dp[ a ] != 1 )
	{
		a-- ;
		if( a < 2 )
		{
			END :
			cout << "NA" << endl ;
			return ;
		}
	}
	if( a < 2 )
	{
		goto END ;
	}
	if( a % 2 == 0 )
	{
		goto LOOK ;
	}
	if( a == 2 )
	{
		cout << a << endl ;
		return ;
	}
	for( int i = 3 ; i * i <= a ; i += 2 )
	{
		if( a % i == 0 )
		{
			goto LOOK ;
		}
	}
	cout << a << endl ;
	return ;
}
int main()
{
	int n ,m ;
	int dmon[ 30 ] ;
	int j ;
	while( cin >> n >> m )
	{
		if( n== 0 && m == 0 )
		{
			break ;
		}
		int cnt = m ;
		long long dp[ 1000000 + 1 ] = { 0 } ;
		for( int i = 0 ; i < n ; i++ )
		{
			cin >> dmon[ i ] ;
		}
		for( int i = 0 ; i + dmon[ 0 ] <= m ; )
		{
			for( j = 0 ; j < n && i + dmon[ j ] <= m ; j++ )
			{
				dp[ i + dmon[ j ] ] = 1 ;
			}
			i++ ;
			while( dp[ i ] != 1 )
			{
				i++ ;
			}
		}
		ans( cnt ,dp ) ; 
	}
	return 0 ;
}

C++: #include<bits/stdc++.h>
using namespace std;
bool sosu[1000005]={0};
bool check[1000005];
main(){
????int n,k;
????int shop;
????sosu[1]=1;
????for(int i=2;i<(int)sqrt(1000005);i++){
????????if(sosu[i]==0){
????????????for(int j=i;j<1000005;j+=i){
????????sosu[j]=true;
????????????}
????????}
????}
????while(1){
????????bool a=true;
????????cin>>n>>k;
????????if(n==0&&k==0)break;
????????memset(check,false,sizeof(check));
????????for(int i=1;i<=n;i++){
????????????scanf("%d",&shop);
????????????check[shop]=true;
????????????for(int j=shop;j<=k;j++){
????????if(check[j-shop]==true){
????????????check[j]=true;
????????}
????????????}?? 
????????}
????????for(int i=k;i>0;i--){
????????????if(sosu[i]==false&&check[i]==true){
????????cout<<i<<endl;
????????i=0;
????????a=false;
????????????} 
????????}
????????if(a){
????????????cout<<"NA"<<endl;
????????}
????}
}

C++: #include<iostream>
#include<utility>
#include<queue>
#include<functional>
#include<cstring>
#include<vector>
#include<climits>
#include<map>
using namespace std;
#define FOR(i,n) for(int i=0;i<(int)n;i++)
#define FORI(i,k,n) for(int i=k;i<(int)n;i++)
#define ALL(x) (x).begin(),(x).end()
typedef pair<int,int> pii;
typedef vector<int> vi;

const int M=1010000;
vi pri;
int pf[M]={1,1};

void calc_pri()
{
	FORI(i,2,M)
	{
		if(!pf[i])
		{
			pri.push_back(i);
			for(int j=i+i;j<M;j+=i)
			{
				pf[j]=1;
			}
		}
	}
}	



int a[M];

int main()
{
	calc_pri();
	int x,n;
	while(cin>>n>>x&&n)
	{
		FOR(i,M)a[i]=0;
		a[0]=1;
		FOR(i,n)
		{
			int k;cin>>k;
			FOR(j,M)
			{
				if(j+k>=M)break;
				if(a[j])a[j+k]=1;
			}
		}
		for(int i=x;i>=0;i--)
		{
			if(i==0)cout<<"NA"<<endl;
			if(a[i]&&!pf[i])
			{
				cout<<i<<endl;
				break;
			}
		}
	}
	
}

C++: #include <iostream>
#include <cstring>

constexpr int MAX_X = 1000000, MAX_N = 30;

int value[MAX_N], n, x;
bool dp[MAX_X + 1];
bool is_prime_number[MAX_X + 1];

void initialize();

void solve();

int main(){
    initialize();

    while(true){
        std::cin >> n >> x;
        if(n == 0 && x == 0){
            break;
        }
        
        memset(dp, false, x + 1);

        for(int i = 0; i < n; ++i){
            std::cin >> value[i];
            dp[value[i]] = true;
        }

        solve();
    }
    return 0;
}

void solve(){
    int ans = 0;

    for(int i = 0; i < x + 1; ++i){
        for(int j = 0; j < n; ++j){
            if(i >= value[j]){
                if(dp[i - value[j]]){
                    dp[i] = true;

                    if(is_prime_number[i]){
                        ans = std::max(ans, i);
                    }
                }
            }
        }
    }

    if(ans){
        std::cout << ans << std::endl;
    }
    else{
        std::cout << "NA" << std::endl;
    }
}

void initialize(){
    memset(is_prime_number, true, MAX_X + 1);

    is_prime_number[0] = false, is_prime_number[1] = false;
    for(int i = 2; i * i < MAX_X + 1; ++i){
        if(is_prime_number[i]){
            for(int j = 2; i * j < MAX_X + 1; ++j){
                is_prime_number[i * j] = false;
            }
        }
    }
}

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

const long N=1000000;

bool prime[N+1];
bool dp[N+1];

int main(){
	prime[0]=prime[1]=false;
	for(long i=2; i<=N; ++i) prime[i]=true;
	
	for(long i=2; i<=N; ++i){
		if(prime[i]) for(long j=2; i*j<=N; ++j) prime[i*j]=false;
	}
	
	while(1){
		int n;
		long x, c[30];
	
		scanf(" %d %ld", &n, &x);
		if(n==0) break;
		for(int i=0; i<n; ++i) scanf(" %ld", &c[i]);
		
		for(long i=0; i<=N; ++i) dp[i]=false;
		
		dp[0]=true;
		for(int i=0; i<n; ++i){
			for(long j=0; j+c[i]<=x; ++j){
				if(dp[j]) dp[j+c[i]]=true;		
			}
		}
		
		long max=0;
		for(long i=0; i<=x; ++i){
			if(dp[i] && prime[i]) max=i;
		}
			
		if(max==0) printf("NA\n");
		else printf("%ld\n", max);
	}
}

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*j < 1000010;j+=i){
            notPrime[j] = true;
        }
    }
    int n,x;
    while(cin>>n>>x,n){
        while(n--){
            int a;
            cin>>a;
            dp[0] = true;
            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;
    }
}