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])
		if n == 0 && x == 0 {
			break
		}
		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 <stdio.h>
#include <algorithm>
using namespace std;
int n, x, a[30]; bool dp[1000001], prime[1000001];
int main() {
	prime[0] = true, prime[2] = true;
	for(int i = 3; i <= 1000000; i += 2) prime[i] = true;
	for(int i = 3; i <= 1000; i += 2) {
		if(!prime[i]) continue;
		for(int j = i * i; j <= 1000000; j += i * 2) prime[j] = false;
	}
	while(scanf("%d%d", &n, &x), n) {
		for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n);
		for(int i = 0; i < x; i++) dp[i + 1] = false; dp[0] = true;
		for(int i = 1; i <= x; i++) {
			for(int j = 0; j < n && i >= a[j]; j++) {
				if(dp[i - a[j]]) {
					dp[i] = true; break;
				}
			}
		}
		for(int i = x; i >= 0; i--) {
			if(dp[i] && prime[i]) {
				printf(i ? "%d\n" : "NA\n", i); break;
			}
		}
	}
}

C++: #include <iostream>

using namespace std;

bool flag[1000001];
int n, x, m, price[30];

bool sosuu(int k) {
	if (k == 2) return true;
	else if (k < 2) return false;
	else if (k % 2 == 0) return false;
	for (int i = 3; i*i<=k; i += 2)
		if (k % i == 0) return false;
	return true;
}
void next(int p, int k) {
	if (flag[k]) return;
	flag[k] = true;
	if (k > x) return;
	if (k > m) if (sosuu(k)) m = k;
	if (p == n) return;
	for (int i = 0; k + price[p] * i <= x; ++i)
		next(p + 1, k + price[p] * i);
}

int main() {
	while (cin >> n >> x, n || x) {
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		for (int i = 0; i < 1000001; ++i) flag[i] = false;
		m = 0;
		next(0, 0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 1000009
ll m=0; int n=0; int ans=0;
bool flag;
/*void dfs(int num){
 if(flag) return;
 if(ans==num){flag=true;return;}
 if(num>ans){memo[num]=false;return;}
 else rep(i,n){
 cout<<num<<endl;
 if(num+v[i]<=ans&&memo[num+v[i]])dfs(num+v[i]);
 }
 memo[num]=false;
 return;
 }*/
int main(){
    vector<bool>check(MAX+1);  //index number will be checked
    rep(i, MAX+1)check[i] = true;
    vector<int>Primenumber(MAX+1,0); //Primearray
    int counter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(check[i]){
            for(int j = 2;i*j<MAX;j++)
                check[i*j] = false;//??´????°??????Primearray????????´???????????????????°??????¨???false???
            Primenumber[counter] = i;
            counter++;
        }
    }
    while(cin>>n>>m,n+m){
        vi v;
        int temp;
        flag=false;
        rep(i,n){cin>>temp; v.push_back(temp);}
        for(int i=(int)m;i>=0;i--){
            if(check[i]){
                ans=i;break;}
        }
        bool array[MAX]; rep(i,MAX)array[i]=false;
        rep(i,n)array[v[i]]=true;
        for(int i=1;i<=ans;i++) {
            if(array[i])rep(j,n)if(i+v[j]<=ans)array[i+v[j]]=true;
            if(array[ans])break;
        }
        flag=array[ans];
        if(flag) cout<<ans<<endl;
        else cout<<"NA"<<endl;
    }
}

C++: #include<iostream>
using namespace std;
#define MAX_N 1000000

int prime[MAX_N];
bool is_prime[MAX_N + 1];

int main(){

  //素数判定
  int p = 0;
  for(int i=0;i<=MAX_N;i++) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for(int i=2;i<=MAX_N;i++){
    if(is_prime[i]){
      prime[p++] = i;
      for(int j=2*i;j<=MAX_N;j+=i) is_prime[j] = false;
    }
  }

  int n,x;
  while(cin >> n >> x){
    if(n==0&&x==0) break;
    int v[n];
    bool dp[x+1];
    for(int i=0;i<=x;i++)
      dp[i] = false;

    for(int i=0;i<n;i++)
      cin >> v[i];
    dp[v[0]] = true;

    for(int i=0;i<n;i++){
      for(int j=0;j<=x-v[i];j++){
        if(dp[j]) dp[j+v[i]] = true;
      }
    }
    for(int i=x;i>=0;i--){
      if(i==0) cout << "NA" << endl;
      if(dp[i]&&is_prime[i]){
        cout << i << endl;
        break;
      }
    }
  }
}

C++: #include<iostream>
#include<vector>
#define MAX 1000010
using namespace std;
int prime[MAX];
bool is_prime[MAX];
void prime_make(int n){
	int p=0;
	for(int i=0;i<=n;i++)
	is_prime[i]=true;
	is_prime[0]=is_prime[1]=false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++]=i;
			for(int j=i*2;j<=n;j+=i)
			is_prime[j]=false;
		}
	}
}
int main(){
	int n,m;
	prime_make(MAX);
	while(cin>>n>>m,n||m){
		bool dp[MAX];
		int p[40];
		vector<int> box;
		for(int i=0;i<=m;i++)
		dp[i]=0;
		dp[0]=1;
		for(int i=0;i<n;i++)
			cin>>p[i];
		for(int i=0;i<=m;i++){
			for(int j=0;j<n;j++){
				if(i-p[j]>=0&&dp[i-p[j]]){
					//cout<<i<<endl;
					dp[i]=1;
					box.push_back(i);
					break;
				}
			}
		}
		int ans=-1;
		for(int i=box.size()-1;i>=0;i--){
			int j;
			for(j=0;prime[j]<box[i];j++){
				if(box[i]%prime[j]==0){
			//		cout<<box[i]<<endl;
					break;
				}
			}
			if(prime[j]>=box[i]){
				ans=box[i];
				break;
			}
		}
		if(ans==-1)
		cout<<"NA"<<endl;
		else
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int prime[1000010];
int dp[1000010];

int main() {
  fill(prime, prime+1000010, 1);

  for (int i = 2; i <= sqrt(1000000)+1; i++) {
    if (prime[i]) {
      for (int j = 2*i; j <= 1000000; j+=i) {
	prime[j] = 0;
      }
    }
  }
  prime[0] = prime[1] = 0;

  while (1) {
    int N, X;
    cin >> N >> X;
    if (N == 0 && X == 0) break;
    int price[40];
    for (int i = 0; i < N; i++) cin >> price[i];

    fill(dp, dp+1000010, 0);
    dp[0] = 1;
    for (int i = 0; i < X; i++) {
      if (!dp[i]) continue;
      for (int j = 0; j < N; j++) {
	if (i + price[j] <= X) dp[i+price[j]] = 1;
      }
    }

    bool flag = true;
    for (int i = X; i > 0; i--) {
      if (dp[i] && prime[i]) {
	cout << i << endl;
	flag = false;
	break;
      }
    }
    if (flag) cout << "NA" << endl;
  }
  return 0;
}