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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);

using namespace std;

typedef long long ll;
typedef long double ld;

bool dp[1000002], sosu[1000002];
int n, x;
int v[30];

void sosuu() {
  sosu[0]=false;
  sosu[1]=false;
  for(int i=2; i*i<=x; i++) {
    if (sosu[i]) {
      for (int j=2*i; j<=x; j+=i) {
        sosu[j] = false;
      }
    }
  }
}
  

int main() {
  while(cin >> n >> x, n) {
    rep(i, n)
      cin >> v[i];
    rep(i, x+1) {
      sosu[i] = true;
      dp[i] = false;
    }
    sosuu();
    rep(i, n) {
      dp[v[i]]=true;
      for(int j=1; j<=x; j++)
        if(dp[j] && j+v[i] <= x)
          dp[j+v[i]] = true;
    }
    while(x>0) {
      if(dp[x] && sosu[x])
        break;
      x--;
    }
    if(x==0)
      cout << "NA" << endl;
    else
      cout << x << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)



bool isPrime(int a){	//must be a>1
	int maxI = floor(sqrt(a));
	FOR(i,2,maxI+1){
		if(a%i==0)return false;
	}
	return true;
}

int main(){
	while(true){
		MUNENIKAKETE:
		int dishL,maxYosan;
		cin >> dishL >> maxYosan;			if(!dishL&&!maxYosan) return 0;

		vector<int> dishS(dishL);
		REP(dishI,dishL){
			int dish;
			cin >> dish;
			dishS[dishI] = dish;
		}

		vector<bool> dekiru(maxYosan+1,false);
		dekiru[0] = true;
		REP(yosan,maxYosan+1){
			REP(dishI,dishL){
				int dish = dishS[dishI];
				if(yosan<dish) continue;
				dekiru[yosan] = dekiru[yosan] || dekiru[yosan - dish];
			}
		}

		for(int yosan = maxYosan; yosan>1 ; --yosan){
			if(dekiru[yosan]&&isPrime(yosan)){
				cout << yosan << endl;
				goto MUNENIKAKETE;
			}
		}
		cout << "NA" << endl;

	}
}

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

using namespace std;

const int MAX_PAY = 1e6 + 10;
array<bool, MAX_PAY> PrimeTable;

void makePrimeTable()
{
    fill(PrimeTable.begin(), PrimeTable.end(), true);

    for (int i = 2; i * i < MAX_PAY; ++i) {
        for (int j = 2; j * i <= MAX_PAY; ++j) {
            PrimeTable.at(j * i) = false;
        }
    }
}


int main()
{
    makePrimeTable();

    int cook_num, max_pay;

    while (cin >> cook_num >> max_pay) {
        // input
        if (cook_num == 0 && max_pay == 0) break;

        vector<int> dish(cook_num);
        vector<bool> dp(max_pay);

        for (auto& it: dish) {
            cin >> it;
            dp.at(it) = true;
        }

        // solve
        for (int i = 0; i < cook_num; ++i) {
            for (int j = 0; j < max_pay; ++j) {
                if (dp.at(j) && j + dish.at(i) <= max_pay) {
                    dp.at(j + dish.at(i)) = true;
                }
            }
        }

        // return max_ans
        int max_ans = 0;
        for (int temp = 0; temp <= max_pay; ++temp) {
            if (dp.at(temp) && PrimeTable.at(temp)) max_ans = temp;
        }

        switch (max_ans) {
            case 0:
                cout << "NA" << endl;
                break;
            defalut:
                cout << max_ans << endl;
        }

    }

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;
 
 
 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int dp[1000010];
int p[40];

bool isPrime[1000010];
//vi primes;

int main(){
	fill(isPrime+2, isPrime+1000010, true);
	for(int i= 2; i < 1000010; i++){
		if(isPrime[i]){
			//primes.pb(i);
			for(int j = i+i; j < 1000010; j+=i){
				isPrime[j]=false;
			}
		}
	}

	int n, x;
	while(cin>>n>>x){
		if(n == 0 && x == 0){
			break;
		}
		for(int i = 0; i < n; i++){
			cin>>p[i];
		}
		for(int i= 0; i <= x; i++){
			dp[i] = INF;
		}
		dp[0] = 0;
		for(int i = 0; i < n; i++){
			for(int j = p[i]; j <= x; j++){
				dp[j] = min(dp[j], dp[j-p[i]]+1);
			}
		}
		bool f = false;
		for(int i = x; i > 0; i--){
			if(dp[i]!=INF && isPrime[i]){
				cout<<i<<endl;
				f=true;
				break;
			}
		}
		if(!f){
			cout<<"NA"<<endl;
		}
	}
}