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

  
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<(n);i++)
#define REP2(i,d,n) for(int i=(d);i<(n);i++)
#define RREP(i,n) for(int i=(n);i>=0;i--)
#define CLR(a)      memset((a),0,sizeof(a))
#define MCLR(a)     memset((a),-1,sizeof(a))
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI > VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int,int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

bool dp[1000000];

const int MAX_N = 1000000;

vector<int> prime;
bool is_prime[MAX_N+1];

// n以下の素数を生成する
void create_prime(int n){
	// 初期化
	is_prime[0] = is_prime[1] = false;
	for(int i=2;i<=n;i++){
		is_prime[i] = true;
	}
	
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime.push_back(i);
			for(int j=2*i;j<=n;j+=i){
				is_prime[j] = false;
			}
		}
	}
}

int main(){
	int n,x;
	create_prime(MAX_N);
	while(true){
		cin >> n >> x;
		if(n==0 && x==0) break;
		REP(i,x+1){
			dp[i] = false;
		}
		VI data;
		int buf;
		REP(i, n){
			cin >> buf;
			data.PB(buf);
		}

		dp[0] = true;
		REP(i,x){
			if(dp[i]){
				REP(j, n){
					if(i+data[j] <= x) dp[i+data[j]] = true;
				}
			}
		}

		int ans = 0;
		REP(i, prime.size()){
			if(x < prime[i]) break;
			if(dp[prime[i]]){
				ans = prime[i];
			}
		}

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool dp[1000001];
int main(){
    bool latte[1000001];
    fill(latte,latte+1000001,true);
    latte[0]=latte[1]=false;
    for(int i=2;i<=1000000;i+=2)latte[i]=false;
    for(int i=3;i<=1000000;i+=2){
        if(latte[i])
            for(int j=i+i;j<=1000000;j+=i)
                latte[j]=false;

    }
    int n,l;
    while(cin>>n>>l,n||l){
        int v[30];
        for(int i=0;i<n;i++)cin>>v[i];
        fill(dp,dp+l,false);
        dp[0]=true;
        for(int i=0;i<=l;i++){
            if(!dp[i])continue;
            for(int j=0;j<n;j++){
                int to=i+v[j];
                if(to>l)continue;
                dp[to]=true;
            }
        }
        bool NAFlag=true;
        for(int i=l;i>=0;i--){
            if(dp[i]&&latte[i]){
                cout<<i<<endl;
                NAFlag=false;
                break;
            }
        }
        if(NAFlag)cout<<"NA"<<endl;
    }

    return 0;
}