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 <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;
 
typedef ostringstream OSS;
typedef istringstream ISS;
 
typedef vector<int> VI;
typedef vector< VI > VVI;
 
typedef long long LL;
 
typedef pair<int, int> PII;
typedef vector<PII> VPII;
 
#define X first
#define Y second

vector<bool> Prime;
void initPrime(long num) {
	Prime = vector<bool>(num + 1, true);
	Prime[1] = Prime[0] = false;
	for (long i = 2; i * i <= num; i++) {
		if (Prime[i]) {
			for (long j = i + i; j <= num; j += i) {
				Prime[j] = false;
			}
		}
	}
}
 
int main(void) {
	initPrime(1000000);
	int N, X;
	while (cin >> N >> X, N) {
		VI ps(N);
		for (auto &p : ps) cin >> p;
		
		vector<bool> dp(X + 1);
		dp[0] = true;

		for (auto p : ps) {
			for (int i = 0; i <= X; i++) {
				if (!dp[i]) continue;

				int np = i + p;
				if (np <= X) {
					dp[np] = true;
				}
			}
		}

		int ans = 0;
		for (int i = 0; i <= X; i++) {
			if (dp[i] && Prime[i]) ans = i;
		}

		if (ans) 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;
      }
    }
  }
  return 0;
}

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

using namespace std;

int main() {
	int n, x;
	vector<int> furui(1000000+1, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i <= 1000000; i++) {
		if (furui[i] == 0) {
			for (int j = 2; i*j <= x; j++) {
				furui[i*j] = 1;
			}
		}
	}
	while (cin >> n >> x) {
		if (n == 0 && x == 0) {
			break;
		}
		vector<int> en(n);
		vector< vector<int> > dp(n+1, vector<int>(x+1, 0));
		for (int i = 0; i < n; i++) {
			cin >> en[i];
			dp[i+1][en[i]] = 1;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= x; j++) {
				if (j < en[i]) {
					dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
				} else {
					dp[i+1][j] = max(max(dp[i][j], dp[i+1][j-en[i]]), dp[i+1][j]);
				}
			}
		}
		bool hantei = true;
		for (int i = x; i >= 0; i--) {
			if (dp[n][i] > 0 && furui[i] == 0) {
				cout << i << endl;
				hantei = false;
				break;
			}
		}
		if (hantei) {
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

	int K[KMAX];
	char T[KMAX][VMAX];

void eratos(int n,bool prime[]){
	for(int i=0;i<=n;i++)prime[i]=false;
	for(int i=3;i<=n;i+=2)prime[i]=true;
	prime[2]=true;
	int lim=(int)sqrt((double)n)+1;
	for(int i=3;i<=lim;i+=2){
		if(!prime[i])continue;
		for(int j=i+i;j<=n;j+=i)prime[j]=false;
	}
}	

int main(){
	bool prime[VMAX+1];
	eratos(VMAX,prime);
	
	int k,v;

	while(cin>>k>>v && !(k==0 && v==0)){
		for(int i=0;i<k;i++)cin>>K[i];
		
		for(int j=0;j<=v;j++)T[0][j]=0;
		for(int j=K[0];j<=v;j+=K[0])T[0][j]=1;
		for(int i=0;i<k;i++)T[i][0]=1;
		
		for(int i=1;i<k;i++){
			for(int j=1;j<K[i];j++)T[i][j]=T[i-1][j];
			for(int j=K[i];j<=v;j++){
				T[i][j]=(T[i-1][j] | T[i][j-K[i]]);
			}
		}
		int ans=-1;
		for(int i=v;i>=2;i--){
			if(prime[i] && T[k-1][i]){
				ans=i;
				break;
			}
		}
		if(ans==-1)cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}

}

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

vector<int> prime_table(int n) {
    vector<int> is_prime(n + 1);
    if (n) iota(is_prime.begin() + 2, is_prime.end(), 2);
    for (int i = 2; i * i <= n; i++) if (is_prime[i]) {
        for (int j = i + i; j <= n; j += i) is_prime[j] = 0;
    }
    return is_prime;
}

int main() {
    auto is_prime = prime_table(1000000);
    int n, x;
    while (cin >> n >> x, n) {
        vector<int> vs(n);
        for (int &v: vs) cin >> v;
        vector<int> dp(x + 1, false); dp[0] = true;
        for (int i = 0; i < n; i++) for (int j = vs[i]; j <= x; j++) {
            dp[j] |= dp[j - vs[i]];
        }
        int ans = -1;
        for (int j = 0; j <= x; j++) if (dp[j]) {
            if (is_prime[j]) ans = j;
        }
        if (ans == -1) cout << "NA" << endl;
        else cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[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]= 1;

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

C++: #include<iostream>
#include<climits>
#include<math.h>
#include<vector>
#include<algorithm>
#include<cstdio>
#include <string>
#include <complex>
#include <functional>
using namespace std;
typedef pair<int,int> P;
double dat[100][100];
int dp[1000001];//動的計画法
int prime[1000001];
int main(){
    prime[0]=prime[1]=-1;//作る
    for(int i=2;i<=1000000;i++){
        if(prime[i]!=-1){
            prime[i]=1;
            for(int j=i+i;j<=1000000;j+=i)prime[j]=-1;
        }
    }
    int n,x;
    int p[31];
    while(cin>>n>>x,n){
        int ans=0,cnt=0;
        for(int i=0;i<n;i++){
            cin>>p[i];
            if(p[i]%2==0) cnt++;
        }
        if(cnt==n){
            cout<<"NA"<<endl;
            continue;
        }
        dp[0]=1;
        for(int i=1;i<=x;i++) dp[i]=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<=x;j++){
                if(p[i]<=j&&dp[j-p[i]])dp[j]=1;
            }
        }
        int ch=0;
        for(int j=x;j>=0;j--){
            if(prime[j]==1&&dp[j]==1){
                ch++;
                cout<<j<<endl;
                break;
            }
        }
        if(ch==0)cout<<"NA"<<endl;
    }
    return 0;
}