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<algorithm>
#include<cstring>
#define MAX_X 1000000+100
using namespace std;

int n;
int x;
int d[100];
bool p[MAX_X];
bool memo[MAX_X];

int solve(int a,int k){
  
  if(a > x) return 0;
  if(memo[a]) return memo[a];
  
  memo[a] = true;
  
  for(int i=k;i<n;i++) solve(a+d[i],k);
  
  return memo[a];
}

int main(){
  
  memset(p,true,sizeof(p));
  p[0] = false;
  for(int i=2;i*i<MAX_X;i++){
    if(p[i]) for(int j=i+i;j<MAX_X;j+=i) p[j] = false;
  }
  
  while(true){
    cin >> n >> x;
    if(!n && !x) break;
    
    memset(memo,false,sizeof(memo));
    for(int i=0;i<n;i++) cin >> d[i];
    
    solve(0,0);
    
    int ans = -1;
    for(int i=0;i<=x;i++){
      if(memo[i] && p[i]) ans = i;
    }

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

C++: using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ
{
	class Program
	{
		public static void Main(string[] args)
		{
			var pmax = 1000000;
			var primes = new bool[pmax+1];
			var maxsq = (int)Math.Sqrt(pmax);
			for(var i=2;i<=pmax;i++)
				primes[i]=true;
			for(var i=2;i<=maxsq;i++)
				if(primes[i])
					for(var j=i*2;j<pmax;j+=i)
						primes[j]=false;
			List<int> num;
			while((num = Console.ReadLine().Split(' ').Select(x=>int.Parse(x)).ToList())[0]>0){
				var menu = new List<int>();
				var order = new bool[num[1]+1];
				var max = 0;
				for(var i=0;i<num[0];i++)
					menu.Add(int.Parse(Console.ReadLine()));
				order[0]=true;
				for(var i=0;i<=num[1];i++)
					if(order[i]){
						menu.ForEach(x=>{if(i+x<=num[1])order[i+x]=true;});
						if(primes[i])
							max=i;
					}
				Console.WriteLine(max<1?"NA":"{0}",max);
			}
			
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <climits>
#include <algorithm>
#include <math.h>

using namespace std;

int main() {
	int n, x;

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

	while( true ) {
		cin >> n >> x;

		if(n == 0 && x == 0) {
			break;
		}

		vector< int > num(n);

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

		vector< vector< int > > ans(n + 1, vector< int >(x + 1, 0));

		for(int i = 0; i < n - 1; i++) {
			for(int j = 0; j <= x; j++) {
				if(j < num[i]) {
					ans[i + 1][j] = ans[i][j];
				} else {
					ans[i + 1][j] = max(ans[i][j], ans[i + 1][j - num[i]] + num[i]);
				}
			}
		}

		int money = 0;
		for(int j = 0; j <= x; j++) {
			if(p[ans[n - 1][j]] == 1) {
				money = max(money, ans[n - 1][j]);
			}
		}
		if(money != 0) {
			cout << money << endl;
		} else {
			cout << "NA" << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, -1, 0, 1 , 1,  1, -1, -1 };

static bool is_pm[ 10000001 ];
static bool dp[ 10000001 ];
static int ans = 0, m;
static char n;
static int ar[ 30 ];

static void solve( const int how_much ) {
	if ( how_much > m  ) {
		return;
	}
	if ( ( how_much % 2 == 1 || how_much == 2 ) && is_pm[ how_much ] ) {
		ans = max( ans, how_much );
	}
	if ( how_much == m ) {
		return;
	}
	if ( dp[ how_much ] ) {
		return;
	}
	
	dp[ how_much ] = true;
	for ( int i = 0; i < n; i++ ) {
		solve( how_much + ar[ i ] );
	}
}

int main() {
	memset( is_pm, 1, sizeof( is_pm ) );
	for ( int i = 3; i * i <= 10000000; i += 2 ) {
		if ( is_pm[ i ] ) {
			for ( int j = i * i; j < 10000000; j += i ) {
				is_pm[ j ] = false;
			}
		}
	}
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d", &ar[ i ] );
		}
		sort( ar, ar + n );
		reverse( ar, ar + n );
		memset( dp, false, sizeof( dp ) );
		ans = 0;
		solve( 0 );
		if ( ans == 0 ) {
			printf( "NA\n" );
		}
		else {
			printf( "%d\n", ans );
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>

using namespace std;

int main() {
	int n, x;
	vector<int> furui(1000000+1, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i <= 1000000; i++) {
		if (furui[i] == 0) {
			for (int j = 2; i*j <= 1000000; j++) {
				furui[i*j] = 1;
			}
		}
	}
	while (cin >> n >> x) {
		if (n == 0 && x == 0) {
			break;
		}/*
		vector<int> en;
		priority_queue<int, vector<int>, greater<int> > pq;
		vector<int> dp(x+1, 0);
		for (int i = 0; i < n; i++) {
			int k;
			cin >> k;
			if (k > x || k == 0) {
				continue;
			}
			dp[k] = 1;
			pq.push(k);
		}
		en.push_back(pq.top());
		pq.pop();
		while (!pq.empty()) {
			int k = pq.top();
			pq.pop();
			bool hantei2 = true;
			for (int i = 0; i < en.size(); i++) {
				if (k % en[i] == 0) {
					hantei2 = false;
					break;
				}
			}
			if (hantei2) {
				en.push_back(k);
			}
		}
		for (int i = 0; i < en.size(); i++) {
			for (int j = 0; j <= x; j++) {
				if (j < en[i]) {
				} else {
					dp[j] = max(dp[j], dp[j-en[i]]);
				}
			}
		}
		bool hantei = true;
		for (int i = x; i >= 0; i--) {
			if (dp[i] > 0 && furui[i] == 0) {
				cout << i << endl;
				hantei = false;
				break;
			}
		}
		if (hantei) {
			cout << "NA" << endl;
		}*/
	}
	return 0;
}