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 <cmath>

using namespace std;

int main() {
	int n, x;
	while (cin >> n >> x) {
		if (n == 0 && x == 0) {
			break;
		}
		vector<int> en(n);
		vector< vector<int> > dp(n+1, vector<int>(x+1, 0));
		for (int i = 0; i < n; i++) {
			cin >> en[i];
			dp[i+1][en[i]] = 1;
		}
		vector<int> furui(x+1, 0);
		furui[0] = furui[1] = 1;
		for (int i = 2; i*i <= x; i++) {
			if (furui[i] == 0) {
				for (int j = 2; i*j <= x; j++) {
					furui[i*j] = 1;
				}
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= x; j++) {
				if (j < en[i]) {
					dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
				} else {
					dp[i+1][j] = max(max(dp[i][j], dp[i+1][j-en[i]]), dp[i+1][j]);
				}
			}
		}
		bool hantei = true;
		for (int i = x; i >= 0; i--) {
			if (dp[n][i] > 0 && furui[i] == 0) {
				cout << i << endl;
				hantei = false;
				break;
			}
		}
		if (hantei) {
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
#define rep(i,j) for(int i=0;i<j;i++)
#define repi(i,j,k) for(int i=j;i<k;i++)
#define Max_N 1000010
bool prime[Max_N];
int menu[30];
bool dp[Max_N];
void eratos()
{
	repi(i,2,Max_N)prime[i]=true;
	prime[0]=false;
	prime[1]=false;
	for(int i=2;i*i<=Max_N;i++)
	{
		if(prime[i])
		{
			for(int j=2*i;j<Max_N;j+=i)
			{
				prime[j]=false;
			}
		}
	}
	return;
}
int main()
{
	rep(i,Max_N)dp[i]=false;
	eratos();
	while(1)
	{
		int n,x;
		cin >> n >> x;
		if(n==0&&x==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> menu[i];
		}
		dp[0]=true;
		int ans=0;
		for(int i=1;i<=x;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(i-menu[j]>=0&&dp[i-menu[j]])
				{
					dp[i]=true;
				}
			}
			if(prime[i]&&dp[i])
			{
				ans=i;
			}	
		}
		if(ans!=0)
		{
			cout << ans << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}

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

using namespace std;

bool isPrime( int n ) {
  if ( n >= 2 ) {
	int lim = (int)sqrt( n );
	for ( int i = 2; i <= lim; i++ ) {
	  if ( n % i == 0 ) {
		return false;
	  }
	}
	return true;
  } else {
	return false;
  }	
}

int main() {

  while( true ) {
	int n, x;
	cin >> n >> x;
	if ( n == 0 and x == 0 ) break;
	vector<int> vs( n );
	for ( int i = 0; i < n; i++ ) {
	  cin >> vs[ i ];
	}
	sort( vs.begin(), vs.end() );
	vector<int> cs;
	cs.push_back( 0 );
	vector<int> indices( n );
	fill( indices.begin(), indices.end(), 0 );
	while( cs.back() <= x ) {
	  int mv = 100000000;
	  int mi = -1;
	  for ( int i = 0; i < n; i++ ) {
		if ( mv > cs[ indices[ i ] ] + vs[ i ] ) {
		  mi = i;
		  mv = cs[ indices[ i ] ] + vs[ i ];
		}
	  }
	  if ( cs.back() < mv ) {
		cs.push_back( mv );
	  } 
	  indices[ mi ]++;
	}
	reverse( cs.begin(), cs.end() );
	bool s = false;
	for ( int v : cs ) {
	  if ( isPrime( v ) ) {
		s = true;
		cout << v << endl;
		break;
	  }
	}
	if ( not s ) {
	  cout << "NA" << endl;
	}
  }
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<cctype>
#include<string>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define INF 2100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define sz(x) ((int)(x).size())
#define SORT(x) sort((x).begin(), (x).end())
#define RSORT(x) sort((x).begin(), (x).end(), greater<int>() )
#define clr(a,b) memset((a),(b),sizeof(a))
int kin[4545],flg[4545454],dp[4545454];	
void sosu()
{
	for(int i=2;i<=1000000;i++) flg[i]=1;
	for(int i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(int j=2;j*i<1000000;j++) flg[i*j]=0;
		}
	}
}
int main(void)
{
	sosu();
	int n,x,ff;
	while(cin>>n>>x) {
		if(!n&&!x) break;
		rep(i,0,n) cin>>kin[i];
		rep(i,0,x+1) dp[i]=0;
		dp[0]=1;
		rep(i,0,n) {
			rep(j,0,x) {
				if(dp[j]==1) {
					if(j+kin[i]<=x) dp[j+kin[i]]=1;
				}
			}
		}
		ff=0;
		for(int i=x;i>=2;i--) {
			if(dp[i]&&flg[i]) {cout<<i<<endl,ff=1;break;}
		}
		if(ff==0) cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int Wm, N, W;
int w[1000];
bool dp[1000001];
vector< int > vc;
void rec( int mul){ // mul: 残りの重さ
  if(dp[mul]++) return;
  for(int i = 0; i < N; i++){
    if(mul + w[i] >= W){
      vc.push_back(mul);
      continue;
    }    
    rec(mul + w[i]);
  }
}
bool isprime(int& x){
  for(int i = 2 ; i * i <= x ; i++ ) if( x % i == 0) return true;
  return x == 1;
}
int solve(int i){
  while(true){
    if(dp[i] && !isprime(i)) return i;
    i--;
  }
}
int main(){

  while(cin >> N >> Wm, N){
    fill_n( dp, Wm + 1, false);
    for(int i = 0; i < N; i++){
      cin >> w[i];
    }
    vc.push_back(0);
    for(W = 100000 ; W < Wm; W += 100000){
      for(int i = 0; i < vc.size() ; i++){
        rec(vc[0]);
        vc.pop_back();
      }
    }
    W = Wm;
    for(int i = 0; i < vc.size() ; i++){
      rec(vc[0]);
      vc.pop_back();
    }
    int ret = solve(W);
    if(ret == 0) cout << "NA" << endl;
    else cout << solve(W) << endl;
    vc.clear();
  }
}

C++: #include <iostream>
#include <cstdio>
#define MAX 1000000
using namespace std;

bool prime[MAX+1];

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


int main(){
  hurui();
  
  for(;;){
    int n,x;
    cin >> n >> x;
    if(n==0)return 0;
    
    int kane[n];
    for(int i=0;i<n;i++)
      cin >> kane[i];
    
    bool dp[x+1];
    for(int i=0;i<=x;i++)dp[i]=false;
    dp[0]=true;
    
    for(int i=0;i<=x;i++){
      if(dp[i]){
	for(int j=0;j<n;j++){
	  if(i+kane[j]<=x)dp[i+kane[j]]=true;
	}
      }
    }

    //cout << "aa";
    for(int i=x;i>=0;i--){
      if(i==0){
	cout << "NA" << endl;
	break;
      }
      if(dp[i]&&prime[i]){
	cout << i << endl;
	break;
      }
    }
  } 
  
}