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 <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;
      }
    }
    if(flag1)
      cout <<ans<<endl;
    else
      cout <<"NA"<<endl;
  }

  return 0;
}

C++: #include <stdio.h>
#include <bitset>
#include <set>
using namespace std;
const int N=1000000;
int main(){
    int n,x,temp;
    bitset<N>b;
    set<int> prime;
    bool f;
    b.reset();
    prime.insert(2);
    for(long long int i=3;i<1000000;i+=2)if(!b[i]){
        prime.insert(static_cast<int>(i));
        for(long long int j=i*i;j<1000000;j+=i*2)b[j]=true;
    }
    while(true){
        scanf("%d%d",&n,&x);
        if(n==0&&x==0)return 0;
        b.reset();
        b[0]=true;
        while(n--){
            scanf("%d",&temp);
            for(int i=temp;i<=x;i+=temp)b|=b<<temp;
        }
        f=true;
        for(set<int>::iterator u=prime.upper_bound(x);f&&u!=prime.begin();){
            u--;
            if(b[*u]){
                f=false;
                printf("%d\n",*u);
            }
        }
        if(f)printf("NA\n");
    }
}

C++: #include <iostream>
#include <cmath>
#define VMAX  1000000
#define KMAX  30
using namespace std;

int main(){
  int k, v;
  int K[KMAX];
  char T[KMAX][VMAX+1];
  bool prime[VMAX+1];
  for (int i=0;i<=VMAX;i++) prime[i] = false;
  for (int i=3;i<=VMAX;i+=2) prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)VMAX) + 1;
  for (int i=3;i<=limit;i+=2){
    if (!prime[i]) continue;
    for (int j=i+i;j<=VMAX;j+=i)prime[j]=false;
  }

  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>1;i--)
      if (prime[i] && T[k-1][i]){
        ans = i;
        break;
      }
    if (ans==-1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string.h>

using namespace std;
bool IsPrime[1000001];
vector<int> Prime;
bool DP[1000001];
void calc()
{
	memset(IsPrime, 1, sizeof(IsPrime));
	for(int i = 2; i < 1000001; ++i)
	{
		if(IsPrime[i])
		{
			Prime.push_back(i);
			for(int j = 2 * i; j < 1000001; j += i)
			{
				IsPrime[j] = false;
			}
		}
	}
}

void solve()
{
	calc();
	int n , x;
	while(cin >> n >> x)
	{
		if(!n && !x)
		{
			break;
		}
		vector<int> Price(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Price[i];
		}
		
		memset(DP, 0, sizeof(DP));
		DP[0] = true;
		for(int i = 0; i < x; ++i)
		{
			if(DP[i])
			{
				for(int j = 0; j < n; ++j)
				{
					if(i + Price[j] <= x)
					{
						DP[i + Price[j]] = true;
					}
				}
			}
		}
		int pos = 0;
		while(Prime[pos] < x)
		{
			pos++;
		}
		if(Prime[pos] > x)
		{
			pos--;
		}
		while(DP[Prime[pos]] == false)
		{
			pos--;
			if(pos < 0)
			{
				cout << "NA" << endl;
				goto NEXT;
			}
		}
		cout << Prime[pos] << endl;
NEXT:
		;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

vector<char> ch(1000001, 2);

char check(int m){
	FOR(i, 2, sqrt(m)){
		if(m%i==0) return 1;
	}
	
	return 2;
}

int main(){
	ch[0]=ch[1]=1;
	FOR(i, 2, 1000001){
		if(ch[i]==1) continue; 
		
		for(int j=2*i; j<=1000000; j+=i){
			ch[j]=1;
		}
	}
	
	while(1){
		int n, x;
		scanf("%d%d", &n, &x);
		if(n==0) return 0;
		
		vector<int> v(n);
		REP(i, n) scanf("%d", &v[i]);
		
		vector<vector<char> > dp(n+1, vector<char>(x+1, 0));
		dp[0][0]=1;
		REP(i, n){
			REP(j, x+1){
				if(dp[i][j]!=0){
					dp[i+1][j]=dp[i][j];
				}
				else {
					if(j>=v[i] && dp[i+1][j-v[i]]!=0){
						dp[i+1][j]=ch[j];
					}
				}
			}
		}
		
		int ans=-1;
		REP(i, x+1) if(dp[n][i]==2) ans=i;
		
		if(ans!=-1) cout << ans << endl;
		else cout << "NA" << endl;
	}
}