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 <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
 
using namespace std; 

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 1000001;

int main(void){
    vector<bool> isP(N);
    fill(ALL(isP), true);
    rep(i, 2) isP[i] = false;
    for(int i = 2; i * i < N; i ++) if(isP[i]) for(int j = i * i; j < N; j += i) isP[j] = false;

    for(int n, x; cin >> n >> x, n;){
        vector<bool> dp(x + 1);
        fill(ALL(dp), false);
        dp[0] = true;
        while(n --){
            int m; cin >> m;

            rep(i, x) if(dp[i]) dp[i + m] = true;
        }
        while(~x && !(dp[x] && isP[x])) x --;
     
        if(~x) cout << x << endl;
        else cout << "NA" << endl;
    }
 
    return 0;
}

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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int SIZE = 1000010;
int n, x;
int v[30];
bool dp[SIZE];
bool is_prime[SIZE];
vector<int> p;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  fill_n((bool*)is_prime, SIZE, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < SIZE; i++) {
    if (!is_prime[i]) continue;
    p.emplace_back(i);
    for (int j = 2 * i; j < SIZE; j += i) {
      is_prime[j] = false;
    }
  }

  while (cin >> n >> x, n) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    dp[0] = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j + v[i] <= x; j++) {
        if (dp[j]) dp[j + v[i]] = true;
      }
    }
    int ans = 0;
    for (int i : p) {
      if (i > x) break;
      if (dp[i]) ans = i;
    }
    if (ans == 0) cout << "NA" << endl;
    else cout << ans << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool dp[1000001];
bool isprime(int& x){
  for(int i = 2 ; i * i <= x ; i++ ) if( x % i == 0) return true;
  return false;
}
int solve(int i){
  while(true){ if(dp[i] && !isprime(i)) return i; i--; }
}
int main(){
  int n,x,data[30],ans;
  while(cin >> n >> x , n){
    fill_n(dp,1000001,false); dp[0] = true;
    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>

using namespace std;

bool pri[1000001];
int price[31];
int n,x;
int dp[31][1000001];
int dps(int i,int sum_m){
  int ss=0;
  int ans;
  if(dp[i][sum_m]!=-1)
    {
      return dp[i][sum_m];
    }
  if(i>=n)
    return 0;
  if(sum_m>=x)
    return 0;
  //  cout<<sum_m<<endl;
  if(pri[price[i]+sum_m] && ans<price[i]+sum_m &&sum_m+price[i]<=x)
    {
      ans=price[i]+sum_m;
      if(ss<ans)
	ss=ans;
    }
  ans= dps(i,price[i]+sum_m);
  if(ss<ans)
    ss=ans;
  ans = dps(i+1,sum_m);
  if(ss<ans)
    ss=ans;
  dps(i+1,sum_m+price[i]);
  if(ss<ans)
    ss=ans;
  
  dp[i][sum_m]=ss;
  //  cout<<ss<<endl;
  return ss;
}

int main()
{
  int ans;
  fill(pri,pri+1000001,true);
  for(int i=2;i<1000001/2;i++)
    {
      for(int x=i+i;x<1000001;x+=i)
	pri[x]=false;
    }
  for(;cin>>n>>x,n;)
    {
      for(int i=0;i<n;i++)
	{
	  cin>>price[i];
	}
      for(int i=0;i<31;i++)
	for(int j=0;j<1000001;j++)
      	  dp[i][j]=-1; 
      ans=dps(0,0);
      if(ans==0)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int n, x, ans;
int in[31], in2[31];
bool dp[1000001];
void solve(int a, int sum){
	for (int i = a; i <= n; i++){
		if (sum + in[i] > x&&!in2[i]){
			in[i] /= 10000;
			in2[i] = 1;
		}
		if (sum + in[i] <= x){
			if (dp[sum + in[i]])
				continue;
			if (sum + in[i] % 2){
				dp[sum + in[i]] = 1;
			}
			solve(i, sum + in[i]);
		}
	}
}
int main(){
	while (cin >> n >> x&&n&&x){
		ans = 0;
		fill(dp, dp + 1000001, 0);
		for (int i = 1; i <= n; i++){
			cin >> in[i];
			in[i] *= 10000;
			in2[i] = 0;
		}
		solve(1, 0);
		for (int i = x; i > 0; i--){
			if (dp[i]){
				for (int j = 5; j <= x; j++){
					if (j == i)
						ans = j;
					if (i % j == 0)
						break;
				}
				if (ans)
					break;
			}
		}
		if (ans)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}