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 <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
vector<bool>prime_t(int n){
  vector<bool>prime(n + 1,true);
  if(n >= 0)prime[0] = false;
  if(n >= 1)prime[1] = false;
  for(int i = 2; i * i <= n; i++){
    if(!prime[i])continue;
    for(int j = i + i; j <= n; j += i)prime[j] = false;
  }
  return prime;
}
int main(){
  while(true){
    int n,x;
    cin >> n >> x;
    if(n == 0 && x == 0)return 0;
    vector<int>v(n);
    rep(i,n)cin >> v[i];
    auto P = prime_t(x);
    vector<bool>dp(x + 1,false);
    dp[0] = true;
    int ans = -1;
    rep(i,x + 1){
      if(!dp[i])continue;
      if(P[i])ans = max(ans,i);
      rep(j,n){
        if(i + v[j] > x)continue;
        dp[i + v[j]] = true;
      }
    }
    if(ans == -1)cout << "NA" << endl;
    else cout << ans << endl;
  }
  return 0;
}

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);

    PrimeTable.at(0) = PrimeTable.at(1) = false;

    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) {
                    try {
                        dp.at(j + dish.at(i)) = true;
                    } catch(...){
                        cout << j + dish.at(i) << endl;
                    }
                }
            }
        }

        // 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;
        }

        // output
        if (max_ans) {
            cout << max_ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
const int MAX = 1000000;
bool prime[1000001];
bool dp[1000001];

void Init(){
	memset(dp, false, sizeof(dp));
	dp[0] = true;
}

void era(){
	prime[0] = prime[1] = false;
	for(int i = 2; i <= MAX; i++){
		if(prime[i]){
			for(int j = i*2; j <= MAX; j+=i) prime[j] = false;
		}
	}
}

int main(){
	int n, limit;
	memset(prime, true, sizeof(prime));
	era();
	while(cin >> n >> limit && ((n!=0)||(limit!=0))){
		Init();
		int maxc = 0;
		for(int i = 1; i <= n; i++){
			int tmp;
			cin >> tmp;
			for(int j = 0; j+tmp <= limit; j++){
				if(dp[j]){
					dp[j+tmp] = true;
					if(prime[j+tmp]&&maxc<j+tmp) maxc = j+tmp; 
				}
			}
		}
		if(!maxc) cout << "NA" << endl;
		else cout << maxc << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n, x, ord[35];
    bool prime[1000000] = {1,1,0};

    rep(i,1000000){
        if(!prime[i]){
            for(int j = i + i; j < 1000000; j+=i){
                prime[j] = 1;
            }
        }
    }

    while(cin >> n >> x, n||x){
        bool total[1000000] = {1};
        rep(i,n){
            cin >> ord[i];
        }
        rep(i,x){
            if(total[i]){
                rep(j,n){
                    total[i + ord[j]] = 1;
                }
            }
        }
        bool c = true;
        for(int i = x; i >= 0; i--){
            if(prime[i] == 0 && total[i] == 1){
                cout << i << endl;
                c = false;
                break;
            }
        }
        if(c) cout << "NA" << endl;
    }
}

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; 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<algorithm>
#include<cstring>
#define M 1000001
using namespace std;

int n, x;
int data[33];
int memo[31][M];
int prim[M];
int flg[M];

void solve(int i, int p){

  if(i == n || p > x) return;
  if(memo[i][p]) return;
  
  solve(i+1, p);
  solve(i, p+data[i]);
  solve(i+1, p+data[i]);

  memo[i][p] = flg[p] = 1;
}


  
int main(){

  prim[0] = prim[1] = 1;
  for(int i=2;i*i<M;i++)
    if(!prim[i]) for(int j=i+i;j<M;j+=i) prim[j] = 1;

  for( ; ; ){
    cin >> n >> x;

    if(!n && !x) break;
    
    for(int i=0;i<n;i++) cin >> data[i];

    memset(memo, 0, sizeof(memo));
    memset(flg, 0, sizeof(flg));
  
    solve(0, 0);
  
    int ans = -1;
    for(int i=0;i<=x;i++)
      if(!prim[i] && flg[i]) ans = i;

    if(ans == -1) cout << "NA\n";
    else cout << ans << endl;
  }

  
}