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>
using namespace std;
int n, x;
int primeTable[1000001];

void makePrimeTable(void) {

    primeTable[0] = false;
    primeTable[1] = false;
    for(int i = 2; i < 1000001; i++) {
        primeTable[i] = true;
    }
    
    for(int i = 2; i < 1000001; i++) {
        if(primeTable[i]) {
            for(int j = i + i; j < 1000001; j += i) {
                primeTable[j] = false;
            }
        }
    }
}


void search(int k, int * price, bool * numbers) {
    
    for(int i = 0; i < n; i++) {
        if(k + price[i] < x + 1) {
            if(!numbers[k + price[i]]) {
                numbers[k + price[i]] = true;
                search(k + price[i], price, numbers);
            }
        }
    }
}

int main(void){
    // Your code here!
    makePrimeTable();
    
    while(cin >> n >> x) {
        if(n == 0 && x == 0) break;
        
        int price[n];
        bool numbers[x+1];
        
        for(int i = 0; i < n; i++) {
            cin >> price[i];
        }
        for(int i = 0; i < x+1; i++) {
            numbers[i] = false;
        }
        numbers[0] = true;

        search(0, price, numbers);
        
        bool flag = true;
        for(int i = x; i >= 0; i--) {
            if(numbers[i] && primeTable[i]) {
                flag = false;
                cout << i << endl;
                break;
            }
        }
        if(flag) cout << "NA" << endl;
        
    }
}

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

constexpr int MAX_X = 1000001;
constexpr int MAX_N = 31;

int n, x, values[MAX_N];
bool is_prime_number[MAX_X] = {true};

void initialize();

void solve();

int main(){
    initialize();
    while(true){
        memset(values, 0, MAX_N);

        std::cin >> n >> x;
        
        if(n + x == 0) break;
        
        for(int i = 0; i < n; ++i)
            std::cin >> values[i];

        solve();
    }
}

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

void solve(){
    int ans = 0;
    int dp[MAX_X];
    memset(dp, 0, MAX_X);

    /*
    std::cout << "x : " << x << std::endl << "n : " << n << std::endl;
    for(int i = 0; i < (x > 1000 ? 1000 : x); ++i){
        std::cout << i << " dp : " << dp[i] << std::endl;
    }
    */

    for(int i = 1; i <= x; ++i){
        int res = 0;
        for(int j = 0; j < n; ++j){
            if(values[j] <= i){
                res = std::max(dp[i - values[j]] + values[j], res);
            }
        }
        dp[i] = std::max(dp[i], res);
        if(is_prime_number[dp[i]]){
            ans = std::max(dp[i], ans);
        }
    }

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

}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[1000050],flg[1000050];
int main(void){
	for(i=1;i<=1000000;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(j=2;j*i<=1000000;j++){
				flg[i*j]=0;
			}
		}
	}
	scanf("%d %d",&n,&x);
	while(n!=0 && x!=0){
		for(j=0;j<=x;j++){
			dp[j]=-1;
		}
		dp[0]=0;
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[j]!=-1){
					dp[j+kin]=1;
				}
			}
		}
		int max=0;
		for(i=0;i<=x;i++){
			if(dp[i]==1 && flg[i]==1){
				max=i;
			}
		}
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

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

bool dp[1000010];
int n,x;
int cook[33];
int solve();
bool prime(int now);

int main(){
	while(1){
		cin >> n >> x;
		if(n == 0 && x == 0) break;
		memset(dp,false,x+2);
		dp[0] = true;
		for(int i=0;i<n;i++){
			cin >> cook[i];
			for(int k=cook[i];k<=x;k++){
				if(dp[k-cook[i]]){
					dp[k] = true;
				}
			}
		}
		int ans = solve();
		if(ans) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

int solve (){
	int now = x;
	while(1){
		if(dp[now] && !(prime(now))) return now;
		now--;
	}
}

bool prime(int now) {
	for(int i=2;i*i<=now;i++){
		if(now%i == 0) return true;
	}
	return false;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool dp[1000001]={true};
bool prime(int& x){
  for(int i = 2 ; i * i <= x ; i++ ) if( x % i == 0) return true;
  return x == 1;
}
int solve(int &i){
  while(i--) if(dp[i] && !prime(i)) return i;
}
int main(){
  int n,x,data[30],ans;
  while(cin >> n >> x , n){
    for(int i = 1 ; i <= x ; i++ ) dp[i] = false;
    for(int i = 0 ; i < n ; i++ ) cin >> data[i];
    for(int i = 0 ; i <= x ; i++ ){
      if(dp[i]) for(int j = 0 ; j < n ; j++ )
        if(i+data[j] <= x) dp[i+data[j]] = true;
    }
    if(1 < (ans = solve(x))) cout << ans << endl;
    else cout << "NA" << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#define rep(i, a, n) for(int i = a;i < n;i++)
using namespace std;


int n, x;
int m[40];
int dp[40][1000010];


bool judge(int v){
	if(v <= 1) return false;
	if(v == 2) return true;
	for(int i = 2;i*i <= x+1;i++){
		if(v % i == 0) return false;
	}
	return true;
}


void makeDP(){
	rep(i, 1, n+1){
		rep(j, 0, x+1){
			if(j - m[i] >= 0) dp[i][j] = max(dp[i-1][j], dp[i][j - m[i]] + m[i]);
			else dp[i][j] = dp[i-1][j];
		}
	}
}


int solve(){
	for(int i = x;i >= 0;i--){
		if(judge(dp[n][i])) return dp[n][i];
	}
	return 0;
}


int main(){
	while(cin >> n >> x, n || x){
		rep(i, 1, n+1) cin >> m[i];

		makeDP();

		int ans = solve();

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

	return 0;
}

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

const int MAX = 1000001;
char p[MAX] = {0};
int a[31],n,x;

void solve(int dp[MAX]){
	
	for(int i=0 ; i < x ; i++ ){
		if( dp[i] ){
			for(int j=0 ; j < n ; j++ ){
				if( i+a[j] <= x ){
					dp[ i + a[j] ] = 1;
				}
			}
		}
	}
}

int main(){
	for(int i=2 ; i < MAX ; i++ ) p[i] = 1;
	for(int i=2 ; i < sqrt(MAX)+1 ; i++ ){
		if( p[i] ){
			for(int j=i+i ; j < MAX ; j += i ){
				p[j] = 0;
			}
		}
	}

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

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

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