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 <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1000001
using namespace std;
int n, x;
int isprime[MAX];
int w[30];
int dp[MAX];
int main() {
    memset(isprime, 1, sizeof(isprime));
    isprime[0]=isprime[1]=0;
    for (int i=2; i<MAX; i++) {
        if (isprime[i]) {
            for (int j=i*2; j<MAX; j+=i) isprime[j]=0;
        }
    }
    while (scanf("%d%d",&n,&x)) {
        if (n==0&&x==0) break;
        memset(dp, 0, sizeof(dp));
        for (int i=0; i<n; i++) scanf("%d",&w[i]);
        dp[0]=1;
        for (int i=0; i<=x; i++) {
            if (dp[i]) {
                for (int j=0; j<n; j++) {
                    if (i+w[j]<=x) dp[i+w[j]]=1;
                }
            }
        }
        int res=-1;
        for (int i=x; i>0; i--) {
            if (dp[i]&&isprime[i]) {
                res=i;
                break;
            }
        }
        if (res==-1) puts("NA");
        else printf("%d\n",res);
    }
}

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

bool prime[1000001];
void is_prime(int n){
	for(int i = 0; i <= n; ++i)
		prime[i] = true;
	prime[0] = prime[1] = false;
	
	for(int i = 2; i <= n; ++i){
		if(prime[i]){
			for(int j = i * 2; j <= n; j += i)
				prime[j] = false;
		}
	}
}

int memo[1000001];
int N, X, cook[50], ans;

int rec(int w){
	if(memo[w] != -1)
		return memo[w];
	if(prime[X - w])
		ans = max(ans, X - w);
	int res = 0;
	for(int i = 0; i < N; ++i){
		if(w - cook[i] >= 0)
			res = max(res, rec(w - cook[i]));
	}
	return memo[w] = res;
}

int main() {
	is_prime(1000000);
	while(cin >> N >> X && (N || X)){
		ans = 0;
		memset(memo, -1, sizeof(memo));
		
		for(int i = 0; i < N; ++i)
			cin >> cook[i];
			
		rec(X);
		if(ans == 0)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
	
	
	return 0;
}

C++: #include<iostream>
using namespace std;
const int INF = (1<<25);

bool dp[1000001];
int price[31];
int n, x;

int checkprime(int num) {
  for(int i=2; i*i<=num; i++) {
    if( num%i == 0 ) return true;
  }
  return x == 1;
}

int main() {
  while( cin >> n >> x, n || x ) {
    for(int i=0; i<n; i++) {
      cin >> price[i];
    }

    dp[0] = true;
    for(int i=1; i<=x; i++) {
      dp[i] = false;
    }

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

    int ans = INF;
    for(int i = x; i>0; i--) {
      if( dp[i] && !checkprime(i) ) {
	ans = i;
	break;
      }
    }

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

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    vector<int> primes;
    primes.PB(2);
    for(int i=3;i<=999991;i++){
	bool flg = true;
	for(int j=0;primes[j]*primes[j]<=i;j++){
	    if(i % primes[j] == 0) flg = false;
	}
	if(flg) primes.PB(i);
    }
    while(true){
	int ans = 0;
	int n,x;
	cin >> n >> x;
	if(n==0&&x==0) break;
	bool d[1000001] = {false};
	d[0] = true;
	REP(i,n){
	    int a;
	    cin >> a;
	    for(int i=0;i+a<=1e6;i++){
		if(d[i])
		    d[i+a] = d[i];
	    }
	}
	int s = lower_bound(ALL(primes),x) - primes.begin();
	RREP(i,s){
	    if(d[primes[i]]){
		ans = primes[i];
		break;
	    }
	}
	if(ans==0) cout << "NA" << endl;
	else cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>

using namespace std;

bool isprime[1000001];//false????´???°

int main() {
	isprime[1] = true;
	int n, x;
	int price[30];

	for (int i = 2; i <= 1000000; i++) {
		if (!isprime[i]) {
			for (int j = i * 2; j <= 1000000; j += i) {
				isprime[j] = true;
			}
		}
	}

	while (cin >> n >> x, n * x) {
		bool isprice[1000001];//??????????????????????????????true
		for (int i = 0; i < n; i++) {
			cin >> price[i];
			isprice[price[i]] = true;
		}
		for (int i = 1; i <= x; i++) {
			if (isprice[i]) {
				for (int j = 0; j < n; j++) {
					if (i + price[j] <= x) {
						isprice[i + price[j]] = true;
					}
				}
			}
		}
		int ans = 99999999;
		for (int i = x;i >= 1; i--) {
			if (!isprime[i] && isprice[i]) {
				ans = i;
				break;
			}
		}
		if (ans == 99999999)cout << "NA" << endl;
		else cout << ans << endl;
	}
	return 0;
}

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

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

int main()
{
	int n ,m ;
	int dmon[ 30 ] ;
	int tmp ;
	while( scanf( "%d %d", &n , &m ), n != 0 )
	{
		int min = 0 ;
		for( int i = 0 ; i < n ; i++ )
		{
			cin >> dmon[ i ] ;
			if( min > dmon[ i ] )
			{
				min = dmon[ i ] ;
			}
		}
		int i ;
		int dp[ 1000000 ] = { 0 } ;
		for( i = 0 ; i + min <= m ; )
		{
			for( int j  = 0 ; j < n ; j++ )
			{
				dp[ i + dmon[ j ] ] = 1 ;
			}
			do
			{
				i++ ;
			}while( dp[ i ] != 1 && i <= m ) ;
		}
		for( int i = m ; i >= 0 ; i-- )
		{
			while( dp[ i ] != 1 )
			{
				i-- ;
			}
			if( tmp = sosuu( i ) > 0 )
			{
				cout << i << endl ;
				goto NEXT ;
			}
		}
		cout << "NA" << endl ;
		NEXT : ;
	}
	return 0 ;
}