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 <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
int n;//料理の種類
int x;
int sum;
bool prime[1000001];
int main()
{

  for(int i = 0;i <1000001;i++){
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for(int i = 2;i <1000001;i++){
    if(prime[i]){
      for(int j = 2*i;j <1000001;j+=i){
	prime[j]= false;
      }
    }
  }
  while(cin>>n>>x,n||x){
    bool flag1=false;
    int dish[40];
    bool check[1000001];
    for(int i = 0;i < 1000001;i++){
      check[i]=false;
    }
    for(int i = 0;i < n;i++){
      cin>>dish[i];
    }
    check[0] =true;//0はどの料理からでも作れる
    for(int i = 0;i <=x;i++){
      if(check[i]){
	for(int j = 0;j <n;j++){
	  if(i + dish[j]<=x)
	    check[i+dish[j]] = true;
	}
      }
    }
    int ans = 0;

    for(int i =x;i >0;i--){//上から調べる
      if(check[i]&&prime[i]){
	flag1 = true;
	ans = i;
	break;
      }
    }
 for(int i = 0 ;i < 100000000;i++){
     x +=1;
     x -=1;
}
    if(flag1)
      cout <<ans<<endl;
    else
      cout <<"NA"<<endl;
  }

  return 0;
}

C++: #include<stdio.h>
#include<string.h>
int hideo[1000001] = { 0 };
int noyori = 0, debu = 0, n, x, temp = 1, ruio[31] = { 0 };
int main(void)
{
	while (scanf("%d %d", &n, &x)) {
		if (n == 0 && x == 0) {
			return 0;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &ruio[i]);
			hideo[ruio[i]] = 1;
		}
		while (temp != x) {
			if (hideo[temp] == 1) {
				for (int j = 1; j <= n; j++) {
					if (temp + ruio[j] > x)continue;
					if (hideo[temp + ruio[j]] == 0) {
						hideo[temp + ruio[j]] = 1;
					}
				}
			}
			temp++;
		}
		for (int i = x; i >= 1; i--) {
			if (hideo[i] == 1) {
				for (int j = 2; j < i - 1; j++) {
					if (i%j == 0) {
						debu = 1;
					}
				}
				if (debu == 0) {
					noyori = i;
					break;
				}
			}
			debu = 0;
		}
		if (noyori > 1 && noyori>7) {
			printf("%d\n", noyori);
		}
		else {
			printf("NA\n");
		}
		temp = 1;
		noyori = 0;
		debu = 0;
		for (int i = 0; i <= n; i++) {
			ruio[i] = 0;
		}
		for (int i = 0; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[30][1000001];
bool sosu[1000001];
int main()
{
	for(int i = 2; i < 1000000; i++)
	{
		if(!sosu[i])
		{
			for(int j = 2; j * i <= 1000000; j++)
			{
				sosu[i * j] = 1;
			}
		}
	}
	while(1)
	{
		cin >> n >> x;
		if(n == 0 && x == 0)
			break;
		for(int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;

		for(int i = 0; i < n - 1; i++)
			for(int j  = 0; j <= x; j++)
				if(dp[i][j])
					for(int k = 0; j + k * v[i] <= x; k++)
						dp[i + 1][j + k * v[i]] = dp[i][j];

					for(int i = x; i >= 0; i--)
					{
						if(!sosu[i] && dp[n - 1][i])
						{
							if(i)
								cout << i << endl;
							else
								cout << "NA" << endl;
							break;
						}
					}
				}
				return 0;
			}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


vector<bool> getPrimeTable(const int& max)
{
	vector<bool> table(max + 1);
    
	table[2] = true;
    
	int i, j;
	for(i = 3; i <= max; i += 2)
	{
		table[i] = i % 2 == 1;
		for(j = 3; j*j <= i; j += 2)
		{
			if(!(i % j))
			{
				table[i] = false;
				break;
			}
		}
	}
    
	return table;
}


int main()
{
	int n, x;
	vector<bool> table = getPrimeTable(1000000);

	while(cin >> n >> x && n)
	{
		vector<bool> v(x + 1, 0);
		v[0] = 1;

		rep(i, n)
		{
			int in;
			cin >> in;

			rep(i, x - in + 1)
				v[i + in] = v[i + in] || v[i];
		}

		for(int i = x; 0 < i; i--)
		{
			if(table[i] && v[i])
			{
				cout << i << endl;
				goto ng;
			}
		}

		cout << "NA" << endl;
ng:
		;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define xMAX 1000001
#define nMAX 31
bool prime[xMAX];

int dp[nMAX][xMAX];

void sieve(){
  prime[0] = prime[1] = false;
  for(int i=2;i<xMAX+1;i++)
    prime[i] = true;
  for(int i=2;i<xMAX+1;i++)
    if(prime[i])
      for(int j=i*2;j<xMAX+1;j+=i)
	prime[j]=false;
}

bool isPrime(int n){
  return prime[n];
}

void makedp(){
  rep(i,nMAX)
    dp[i][0]=0;
  rep(i,xMAX)
    dp[0][i]=0;
}

void printdp(int n, int x){
  putchar('\n');
  rep(i,n+1){
    printf("%d :",i+1);
    rep(j,x+1){
      printf("%2d ",dp[i][j]);
    }
    putchar('\n'); 
  }
}

void solve(int n, int x, const int *v){
  int max=0;
  for(int i=1;i<n+1;i++)
    for(int j=1;j<x+1;j++){
      int Do =  dp[i][j-v[i]]+v[i];
      int Not = dp[i-1][j];
      if(j<v[i-1])
	dp[i][j] = Not;
      else{
	if (Do > Not){
	  dp[i][j] = Do;
	  if(max<Do && isPrime(Do) && Do<=x)
	    max=Do;
	}
	else
	  dp[i][j] = Not;
      }
    }

  //printdp(n,x);

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

int main(){
  sieve();
  makedp();
  int v[nMAX];
  v[0]=0;
  for(;;){
    int n,x;
    cin >> n >> x;
    if(!(n+x)) break;
    for(int i=1;i<n+1;i++)
      cin >> v[i];
    solve(n,x,v);
  }
  return 0;
}