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 <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#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 loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 1000000+1
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
vector<bool>Isprime(MAX+1,true);
vi dp(MAX+1,0);
int main(){
    int n;
    int Primecounter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(Isprime[i]){
            for(int j = 2;i*j<MAX;j++)
                Isprime[i*j] = false;//テァツエツ?ヲツ閉ーテ」ツ?ェテ」ツつ臼rimearrayテ」ツ?ォテヲツ?シテァツエツ催」ツ?療」ツ?ヲテ」ツ?敕」ツ?ョテ・ツ?催ヲツ閉ーテ」ツつ津・ツ?ィテ」ツ?ヲfalseテ」ツ?ォ
            Primecounter++;
        }
    }
    int x;
    while(cin>>n>>x,n+x){
        rep(i,x+1)dp[i]=0;
        vi tiya;
        rep(i,n){
            int gotiusa_is_a_god_anime;
            cin>>gotiusa_is_a_god_anime;
            tiya.push_back(gotiusa_is_a_god_anime);
            dp[gotiusa_is_a_god_anime]=1;
        }
        rep(j,n)rep(i,x+1){
            if(dp[i]){
                if(i+tiya[j]<=x) dp[i+tiya[j]]=1;
            }
        }
        int maxi=-INF;
        for(int i=1; i<=x;i++)if(Isprime[i]&&dp[i])cmax(maxi,i);
        if(maxi!=-INF&&maxi!=1)cout<<maxi<<endl;
        else cout<<"NA"<<endl;
    }
}

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

using namespace std;

bool flag[1000001] = {0};
int add[30], n, a = 0, 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 k) {
	if (flag[k]) return;
	flag[k] = true;
	if (k > x) return;
	if (k > m) if (sosuu(k)) m = k;
	for (int i = 0; i < n; ++i)
		next(k + price[i]);
}

int main() {
	int pattern[30];
	while (cin >> n >> x, n || x) {
		memset(flag,0,sizeof(flag));
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		int p = 0, a = 0;
		m = 0;
		next(0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#define MAX 2000000
using namespace std;

const int INF = 1<<29;
bool isPrime[MAX+5],dp[MAX+5];

void Init(){
  for(int i=0;i<=MAX;i++)isPrime[i] = false;
  for(int i=3;i<=MAX;i+=2)isPrime[i] = true;
  isPrime[2] = true;
  int limit = (int)sqrt((double)MAX)+1;
  for(int i=3;i<=limit;i+=2){
    if(!isPrime[i])continue;
    for(int j=i+i;j<=MAX;j+=i)isPrime[j] = false;
  }
}


int main(){
  int n,x,val;
  vector<int> The_rock;
  Init();
  while(cin >> n >> x && n && x){
    The_rock.clear();
    for(int i=0;i<n;i++){
      cin >> val;
      The_rock.push_back(val);
    }
    //int dp[x+10];
  
    for(int i=0;i<=MAX;i++)dp[i] = false;
    
    for(int i=0;i<The_rock.size();i++)dp[The_rock[i]] = true;
    dp[0] = true;
    for(int i=0;i<The_rock.size();i++){
      for(int j=The_rock[i];j<=MAX;j++){
	if(dp[j-The_rock[i]])dp[j] = true;
      }
    }

    bool fin = false;
    for(int i=x-1;i>=2;i--){
      if(i == 1)continue;
      if(isPrime[i] && dp[i]){
	cout << i << endl;
	fin = true;
	break;
      }
    }
    if(!fin)cout << "NA" << endl;


  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,x;
  while(cin>>n>>x,n+x){
    int ans=-1;
    bool dp[x+1000]={};
    vector<int>v(n);
    for(int i=0;i<n;i++){
      cin>>v[i];
      if(v[i]<=x)
      dp[v[i]]=true;
    }
    sort(v.begin(),v.end());
    for(int i=1;i<x;i++){
      for(int j=0;j<v.size();j++){
	if(dp[i]==true){
	  if(i+v[j]>x){
	    v.pop_back();
	    j--;
	  }else{
	    dp[i+v[j]]=true;
	  }
	}
      }
    }
    int num=x;
    if(num%2==0)num--;
    for(int i=num;i>1;i-=2){
      bool flag=true;
      if(dp[i]==true){
	for(int j=2;j<=sqrt(i);j++){
	  if(i%j==0){
	    flag=false;
	    break;
	  }
	}
	if(flag){
	  ans=i;break;
	}
      }
    }
    if(ans==-1&&dp[2]==false&&dp[1]==false)
      cout<<"NA"<<endl;
    else if(ans!=-1)
      cout<<ans<<endl;
    else{
      if(dp[2]==true)
      cout<<2<<endl;
      else
	cout<<1<<endl;
    }
  }
  return 0;
}

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

#define N 1000001

using namespace std;

bool prm[N];
void mkPrm()
{
  fill(prm, prm+N, true);
  prm[0] = prm[1] = false;

  for(int i = 2; i < N; i++)
    if(prm[i])
      for(int j = i+i; j < N; j+=i) prm[j] = false;

  return ;
}

int main()
{
  int n, x, a[30];
  bool val[N];

  mkPrm();
  while(cin>>n>>x && (n+x)){
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    fill(val, val+N, false);
    val[0] = true;

    for(int i = 0; i <= x; i++)
      if(val[i])
	for(int j = 0; j < n; j++)
	  if(i+a[j] <= x) val[i+a[j]] = true;

    n = 0;
    for(int i = x; i > 1; i--)
      if(val[i] && prm[i]){
	n = i;
	break;
      }

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

  return 0;
}

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 < 1000001; ++i) flag[i] = false;
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		m = 0;
		next(0, 0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
}