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>


int *ms;
int n, m;

int *dp;

int search(int m){
	if(m == 0) return 1;
	else if(m < 0) return 0;
	else if(dp[m]!=-1) return dp[m];

	for(int i=0;i<n;i++){
		if(search(m-ms[i])) return dp[m] = 1;
	}
	
	return dp[m]=0;
}

int isprime(int p){
	if(p%2==0) return 0;
	for(int i=3;i*i<p;i++){
		if(p%i==0) return 0;
	}
	return 1;
}

int main(){
	while(1){
		scanf("%d%d", &n,&m);
		
		if(n==0) break;
		
		int i;
		dp = new int[m+1];
		for(i=0;i<m+1;i++) dp[i]=-1;
		
		ms = new int[n];
		for(i=0;i<n;i++) scanf("%d", ms+i);
		
		int flag = 1;
		for(i=m;i>=0;i--){
			if(search(i) && isprime(i)){
				printf("%d\n", i);
				flag = 0;
				break;
			}
		}
		if(flag) puts("NA");
	}
}

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

bool prime[1000001];
vector<int> p;

int main(){
	for(int i = 2;i <= 1000000;i++) prime[i] = true;
	for(int i = 4;i <= 1000000;i += 2) prime[i] = false;
	for(int i = 3;i <= 1000;i += 2){
		for(int j = 2 * i;j <= 1000000;j += i) prime[j] = false;
	}
	for(int i = 0;i <= 1000000;i++) if(prime[i]) p.push_back(i);
	
	int n,x;
	while(1){
		cin >> n >> x;
		if(!n) break;
		bool dp[1000001] = {true};
		for(int i = 0;i < n;i++){
			int v;
			cin >> v;
			for(int j = v;j <= x;j++){
				if(dp[j - v]) dp[j] = true;
			}
		}
		int i;
		for(i = x;i >= 0;i--){
			if(prime[i] && dp[i]) {
				cout << i << endl;
				break;
			}
		}
		if(i == -1) cout << "NA" << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, K;

    bool is_prime[1000001];
    for (int i = 0; i <= 1000000; i++) is_prime[i] = true;
    is_prime[0] = is_prime[1] = 0;
    for (int i = 0; i <= 1000000/2; i++) {
        if (is_prime[i])
            for (int j = 2; j * i <= 1000000; j++) {
                is_prime[i * j] = false;
            }
    }

    while (cin >> N >> K, N || K) {
        vector<int> v(N);
        for (auto& i : v) cin >> i;

        vector<bool> dp(K+1, false);
        dp[0] = true;
        int ans = -1;
        for (int i = 0; i < K; i++) {
            if (dp[i]) {
                for (auto j : v) {
                    if (i + j <= K) dp[i + j] = true;
                    if (i + j <= K && is_prime[i+j])
                        ans = max(ans, i + j);
                }
            }
        }

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

    return 0;
}

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

bool prime[10000001];
bool dp[10000001];
int menu[30];
int n,x;

void make_prime()
{
  memset(prime,true,sizeof(prime));
  prime[0]=prime[1]=false;
  for(int i=2;i<10000001;i++)
    {
      if(prime[i])
	{
	  for(int j=i+i;j<10000001;j+=i)
	    prime[j]=false;
	}
    }
}

int main()
{
  make_prime();
  for(;cin>>n>>x,n;)
    {
      memset(dp,false,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  cin>>menu[i];
	  dp[menu[i]]=true;	  
	}
      int ans=-1;
      for(int i=0;i<10000001;i++)
	{
	  if(dp[i])
	    for(int j=0;j<n;j++)
	      if(i+menu[j]<10000001)
		dp[i+menu[j]]=true;	  
	}
      for(int i=x;i>0;i--)
	if(prime[i]&&dp[i])
	  {
	    ans=i;
	    break;
	  }
      if(ans!=-1)
	cout<<ans<<endl;
      else
	cout<<"NA"<<endl;
    }
}

C++: #include<stdio.h>
#include<string.h>
#include<iostream>
int hideo[2000001] = { 0 };
int noyori = 0, debu = 0, n, temp = 1, ruio[31] = { 0 };
unsigned int x;
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) {
			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 = 1; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>

bool dp[1000001];
bool pm[1000001];

int main() {
	int n,x,v[30];
	pm[0]=pm[1]=true;
	for(int i=2;i<=1000000;i++) {
		if(!pm[i]) for(int j=2;i*j<=1000000;j++) pm[i*j]=true;
	}
	while(scanf("%d %d",&n,&x),n||x) {
		for(int i=0;i<n;i++) {
			scanf("%d",&v[i]);
		}
		memset(dp,0,sizeof(dp));
		dp[0]=true;
		for(int i=1;i<=x;i++) {
			for(int j=0;j<n;j++) {
				if(i-v[j]>=0&&dp[i-v[j]]) {
					dp[i]=true;break;
				}
			}
		}
		int ans=-1;
		for(int i=x;i>=2;i--) {
			if(dp[i]&&!pm[i]) {ans=i;break;}
		}
		if(ans!=-1) printf("%d\n",ans); else printf("NA\n");
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;
const int MAX_N = 1000000;
bool prime[MAX_N+1];

void make_prime (void )
{
	fill (prime, prime + MAX_N, true );

	prime[0] = false;
	prime[1] = false;

	for (int i = 2; i < MAX_N + 1; ++i ){
		if (prime[i] ){
			for (int j = 2*i; j < MAX_N + 1; j += i ){
				prime[j] = false;
			} // end for
		} // end if
	} // end for
}

int dp[31][MAX_N+1];

int main()
{
//	cut here before submit 
//	freopen ("testcase.be", "r", stdin );
	int n, x;

	make_prime ();

	while (cin >> n >> x && n && x ){
		vector <int> cost (n, 0 );
		for (int i = 0; i < n; ++i ){
			cin >> cost[i];
		} // end for
		memset (dp, 0 , sizeof (dp ) );

		for (int i = 0; i < n; ++i ){
			for (int j = 0; j <= x; ++j ){
				if (j < cost[i] ){
					dp[i+1][j] = dp[i][j];
				}else{
					dp[i+1][j] = max (dp[i][j], dp[i+1][j - cost[i]] + cost[i] );
				} // end if
			} // end for
		} // end for 

		int res = 0;
		for (int j = 0; j <= MAX_N; ++j )
			if (prime[dp[n][j]] )
				res = max (res, dp[n][j] );

		if (res == 0 ){
			cout << "NA" << endl;
		}else{
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}