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++: //============================================================================
// Name        : TopCoderCompetition.cpp
// Author      : taguchi
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <bitset>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <numeric>
#include <list>
using namespace std;
//#define P pair<int,int>
#define max (ull)1000005
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,n) for(ull i = 0;i<n;i++)
#define pb(n) push_back(n)

typedef vector<int> vi;
typedef vector<string> vs;
struct edge{int cost,to;};

vector<string> split(string &in,char delimiter){
	vector<string> re; uint cur = 0,next;
	while((next = in.find_first_of(delimiter,cur)) != string::npos){
		re.pb(string(in,cur,next-cur));
		cur = next + 1;
	}
	re.pb(string(in,cur,in.size()-cur));
	return re;
}
vector<int> split_int(string &in, char delimiter){
	vector<string> str = split(in,delimiter);
	vector<int> re;
	rep(i,str.size()){
		re.pb(strtol(str[i].c_str(),NULL,10));
	}
	return re;
}

class Range{
public:
	int a,b;
	Range(int an,int bn) : a(an),b(bn){}

	bool operator()(int n){
		return a<=n&&n<=b;
	}
	bool isCross(Range x){
		return x(a) || x(b);
	}
	bool contains(Range x){
		return this->a <= x.a && x.b <= this->b;
	}
};

class RangeHandler{
public:
	list<Range> Ranges;

	bool insert(Range x){
		if(Ranges.empty()){
			Ranges.push_back(x);
			return true;
		}

		if(x.b <= Ranges.front().a){
			Ranges.push_front(x);
			return true;
		}
		if(Ranges.back().b <= x.a){
			Ranges.push_back(x);
			return true;
		}
		for(list<Range>::iterator i = Ranges.begin();i != Ranges.end();i++){
			list<Range>::iterator l = i;
			l++;
			//cout << "dbg" << i->b << " " << i->a << endl;
			if(Range(i->b,l->a).contains(x)){
				Ranges.insert(l,x);
				return true;
			}
		}
		return false;
	}
};
int main(){
	bitset<max> primeTable;
	primeTable.set();
	for(uint i = 2;i<max;i++){
		if(primeTable[i])
		for(uint j = i;j*i<max;j++){
			primeTable[i*j] = false;
		}
	}
	while(true){
		ull n,x;
		ull foods[30];
		cin >> n >> x;
		if(n == 0 && x == 0) break;
		rep(i,n){
			cin >> foods[i];
		}

		bitset<max> makeable;
		makeable.reset();
		makeable[0] = true;
		rep(k,n)
		rep(i,max){
			if(makeable[i] && i + foods[k] <= x){
				makeable[i + foods[k]] = true;
			}
		}
		int result = 0;
		for(uint i = x;i>=0;i--){
			if(makeable[i] && primeTable[i]){
				result = i;break;
			}
		}
		if(result != 0)
			cout << result << endl;
		else cout << "NA" << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
int prime(int);
int p[1000001]={};
int main(){
	int n,x,v[30],i,j,o,a;
	while(1){
		memset(p,0,sizeof(p));
		memset(v,0,sizeof(v));
		scanf("%d %d",&n,&x);
		if(n==0&&x==0){
			break;
		}
		for(i=0;i<n;i++){
			scanf("%d",&v[i]);
			p[v[i]]=1;
		}
		for(i=0;i<=x;i++){
			if(p[i]){
				for(j=0;j<n;j++){
					if(i+v[j]<=x){
						p[i+v[j]]=1;
					}
				}
			}
		}
		o=0;
		for(i=x;i>=0;i--){
			if(p[i]){
				if(prime(i)){
					o=1;
					break;
				}
				
			}
		}
		if(o){
			printf("%d\n",i);
		}
		else{
			printf("NA\n");
		}
	}
	return 0;
}
int prime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}

C++: #include<iostream>

using namespace std;

#define XMAX 1000000
#define NMAX 30
#define NO -1
#define YES 1

int main(void){

	int erato[XMAX+1];

	for(int i=0;i<=XMAX;i++) erato[i]=YES;
	erato[0]=NO,erato[1]=NO;

	for(int i=2;i<=XMAX;i++){
		if(erato[i]==YES){
			for(int j=i+i;j<XMAX;j+=i){
				erato[j]=NO;
			}
		}
	}

	int n,x;
	int dp[XMAX+1];
	int price[NMAX];
	bool f;
	int ans;

	while(1){

		cin>>n>>x;
		if(n==0&&x==0) break;

		for(int i=0;i<n;i++){
			cin>>price[i];
		}

		for(int i=0;i<=x;i++) dp[i]=NO;
		dp[0]=YES;

		while(1){
			f=false;
			for(int i=0;i<x;i++){
				if(dp[i]==YES){
					for(int j=0;j<n;j++){
						if(i+price[j]<=x){
							if(dp[i+price[j]]==NO){
								f=true;
								dp[i+price[j]]=YES;
							}
						}
					}
				}
			}
			if(!f) break;
		}
		ans=-1;
		for(int i=x;i>=0;i--){
			if(dp[i]==YES&&erato[i]==YES){
				ans=i;
				break;
			}
		}

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

}

C++: #include <iostream>
#include <cstring>
#include <vector>
using namespace std;

int n,x,ans;
int t[30];
char dp[1000001];
char p[1000001];

void mkPrime(void){
	memset(p,1,sizeof(p));
	p[0] = p[1] = 0;

	for(int i=2;i<=1000000;i++){
		if(p[i]){
			for(int j=i*2;j<=1000000;j+=i){
				p[j] = 0;
			}
		}
	}
}

void solve(int sum,int idx){
	if(dp[sum]) return;
	if(idx == n) return;

	dp[sum] = 1;
	if(p[sum] && ans<sum) ans = sum;

	for(int i=0;;i++){
		int nsum = sum + i * t[idx];
		if(nsum > x) return;
		solve(nsum,idx+1);
	}
}

int main(void){
	mkPrime();

	while(cin>>n>>x && (n||x)){
		for(int i=0;i<n;i++) cin>>t[i];

		memset(dp,0,sizeof(dp));
		ans = 0;

		solve(0,0);

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

	return 0;
}

C++: #include <iostream>
#include <cmath>
#define VMAX  1000000
#define KMAX  1000000
using namespace std;

int main(){
  int k, v;
  int K[KMAX];
  char T[KMAX][VMAX+1];
  bool prime[VMAX+1];
  for (int i=0;i<=VMAX;i++) prime[i] = false;
  for (int i=3;i<=VMAX;i+=2) prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)VMAX) + 1;
  for (int i=3;i<=limit;i+=2){
    if (!prime[i]) continue;
    for (int j=i+i;j<=VMAX;j+=i)prime[j]=false;
  }

  while (cin >> k >> v && !(k ==0 && v ==0)){
    for (int i=0;i<k;i++) cin >> K[i];
    for (int j=0;j<=v;j++) T[0][j]=0;
    for (int j=K[0];j<=v;j+=K[0]) T[0][j]=1;
    for (int i=0;i<k;i++) T[i][0]=1;
    for (int i=1;i<k;i++){
      for (int j=1;j<K[i];j++) T[i][j] = T[i-1][j];
      for (int j=K[i];j<=v;j++){
        T[i][j] = (T[i-1][j] | T[i][j-K[i]]);
      }
    }
    int ans = -1;
    for (int i=v;i>1;i--)
      if (prime[i] && T[k-1][i]){
        ans = i;
        break;
      }
    if (ans==-1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}