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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


vector<bool> getPrimeTable(const int& max)
{
	vector<bool> table(max + 1);
    
	table[2] = true;
    
	int i, j;
	for(i = 3; i <= max; i += 2)
	{
		table[i] = i % 2 == 1;
		for(j = 3; j*j <= i; j += 2)
		{
			if(!(i % j))
			{
				table[i] = false;
				break;
			}
		}
	}
    
	return table;
}


int main()
{
	int n, x;
	vector<bool> table = getPrimeTable(1000000);

	while(cin >> n >> x && n)
	{
		vector<bool> v(x + 1, 0);
		v[0] = 1;

		rep(i, n)
		{
			int in;
			cin >> in;

			rep(i, x - in + 1)
				v[i + in] = v[i + in] || v[i];
		}

		for(int i = x; 0 < i; i--)
		{
			if(table[i] && v[i])
			{
				cout << i << endl;
				goto ng;
			}
		}

		cout << "NA" << endl;
ng:
		;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define xMAX 1000001
#define nMAX 31
bool prime[xMAX];

int dp[nMAX][xMAX];

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

bool isPrime(int n){
  return prime[n];
}

void makedp(){
  rep(i,nMAX)
    dp[i][0]=0;
  rep(i,xMAX)
    dp[0][i]=0;
}

void printdp(int n, int x){
  putchar('\n');
  rep(i,n+1){
    printf("%d :",i+1);
    rep(j,x+1){
      printf("%2d ",dp[i][j]);
    }
    putchar('\n'); 
  }
}

void solve(int n, int x, const int *v){
  int max=0;
  for(int i=1;i<n+1;i++)
    for(int j=1;j<x+1;j++){
      int Do =  dp[i][j-v[i]]+v[i];
      int Not = dp[i-1][j];
      if(j<v[i-1])
	dp[i][j] = Not;
      else{
	if (Do > Not){
	  dp[i][j] = Do;
	  if(max<Do && isPrime(Do) && Do<=x)
	    max=Do;
	}
	else
	  dp[i][j] = Not;
      }
    }

  //printdp(n,x);

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

int main(){
  sieve();
  makedp();
  int v[nMAX];
  v[0]=0;
  for(;;){
    int n,x;
    cin >> n >> x;
    if(!(n+x)) break;
    for(int i=1;i<n+1;i++)
      cin >> v[i];
    solve(n,x,v);
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std ;
long long dp[ 1000000 + 1 ] ;
void ans( int cnt ,long long dp[] )
{
	int a = cnt + 1 ;
	LOOK :
	a-- ;
	while( dp[ a ] != 1 )
	{
		a-- ;
		if( a < 2 )
		{
			END :
			cout << "NA" << endl ;
			return ;
		}
	}
	if( a < 2 )
	{
		goto END ;
	}
	if( a % 2 == 0 )
	{
		goto LOOK ;
	}
	if( a == 2 )
	{
		cout << a << endl ;
		return ;
	}
	for( int i = 3 ; i * i <= a ; i += 2 )
	{
		if( a % i == 0 )
		{
			goto LOOK ;
		}
	}
	cout << a << endl ;
	return ;
}
int main()
{
	int n ,m ;
	int dmon[ 30 ] ;
	int j ;
	while( cin >> n >> m )
	{
		if( n== 0 && m == 0 )
		{
			break ;
		}
		int cnt = m ;
		fill_n( dp ,1000001 ,0 ) ;
		for( int i = 0 ; i < n ; i++ )
		{
			cin >> dmon[ i ] ;
		}
		for( int i = 0 ; i + dmon[ 0 ] <= m ; )
		{
			for( j = 0 ; j < n && i + dmon[ j ] <= m ; j++ )
			{
				dp[ i + dmon[ j ] ] = 1 ;
			}
			i++ ;
			while( dp[ i ] != 1 )
			{
				i++ ;
			}
		}
		ans( cnt , dp ) ; 
	}
	return 0 ;
}

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


int n, x, v[30];
bool cost[1000001], prime[1000001];
int main(){
    std::fill(prime, prime + 1000001, true);
    prime[1] = false;
    for(int i = 2; i * i <= 1000001; i++){
        for(int j = 2; i * j <= 1000001; j++){
            if(!prime[i * j]) continue;
            prime[i * j] = false;
        }
    }

    while(scanf("%d %d", &n, &x) != EOF){
        if(n == 0 && x == 0) break;


        std::fill(cost, cost + 1000001, false);
        cost[0] = true;
        for(int i = 0; i < n; i++){
            scanf("%d", &v[i]);

            for(int j = v[i]; j <= x; j += v[i]) cost[j] = true;
        }

        int ans = 0;
        for(int i = 1; i <= x; i++){
            if(!cost[i]){
                for(int j = 0; j < n; j++){
                    if(i - v[j] >= 0 && cost[i - v[j]]){
                        cost[i] = true;
                        break;
                    }
                }
            }
            if(cost[i] && prime[i]){
                ans = i;
            }
        }

        if(ans == 0) printf("NA\n");
        else printf("%d\n", ans);
    }

    return 0;
}

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

using namespace std;

int dp[1000001];
int kind[31];


const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;


int prime[MAX_PRIME+1];
bool isPrime[MAX_PRIME+1];
int p;

void erats(){
	// primeXgðú»
	fill(isPrime,isPrime+MAX_PRIME,true);
	p = 0;
	isPrime[0] = isPrime[1] = false;

	for(int i = 2; i <= MAX_PRIME; i++){
		if(isPrime[i]){
			prime[p++] = i;
		}
		else
			continue;

		// Óé¢É©¯é
		for(int j = 2*i; j <= MAX_PRIME; j+=i){
			isPrime[j] = false;
		}
	}
}


int main(){

	int n;
	int x;
	erats();
	while(cin>>n>>x&&!(n==0&&x==0)){
		for(int i = 0; i < n; i++){
			cin>>kind[i];
		}
		// eàzÌ¥¢ûª½íÞ é©ðßé
		//fill(dp,dp+1000001,0);
		dp[0]=1;
		int maxCost=-1;

		//for(int j = 0; j < n; j++){
		//	for(int i = kind[j]; i <= x; i++){
		//		dp[i]+=dp[i-kind[j]];
		//	}
		//}

		for(int i = 1; i <= x; i++){
			dp[i]=0;
			for(int j = 0; j < n; j++){
				if(i-kind[j]>=0){
					dp[i]+=dp[i-kind[j]];
				}
			}
			if(dp[i]&&(isPrime[i])){
				maxCost=i;
			}
		}
		//for(int i = 0; i <= x; i++){
		//	if(dp[i]&&isPrime[i])
		//		maxCost=i;
		//}


		if(maxCost==-1)
			cout<<"NA"<<endl;
		else
			cout<<maxCost<<endl;
	}

	return 0;
}