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<stdio.h>
int g[1000001],k[30],i,n;

int prime(int);
int goukei(int);
int saiki(int);

int saiki(int y){
	if(prime(y) && goukei(y))return y;
	if(y>2)return saiki(y-1);
	else return 0;
}

int goukei(int z){
	if(g[z]==2)return 1;
	else if(g[z]==1)return 0;
	for(i=0;i<n;i++){
		if(z-k[i]>0) goukei(z-k[i]);
		else if(z-k[i]==0){
			g[z]=2;
			return 1;
		}
	}
	return 0;
}

int prime(int n){
	int i=3;
	if(n<2)return 0;
	else if(n==2)return 1;
    if(n%2==0)return 0;
    for(;i*i<=n;i+=2)if(n%i==0)return 0;
	return 1;
}

int main(){
	int x,a;
	while(1){
		scanf("%d%d",&n,&x);
		if(!n&&!x)break;
		for(i=0;i<n;i++) scanf("%d",&k[i]);
		a=saiki(x);
		if(a)printf("%d\n",saiki(x));
		else puts("NA");
	}
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<double> vd;
typedef std::vector<std::string> vs;
typedef std::vector<pii> vpii;
typedef std::vector<vi> vvi;
typedef std::vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { rep(i,v.size()) is >> v[i]; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

bool isPrime(const int &n) {
  if (n == 1) return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

main {
  int n, x;
  while (cin >> n >> x, n) {
    vi v(n);
    cin >> v;
    vi dp(x + 1);
    dp[0] = true;
    rep(i, v.size()) rep(j, x + 1 - v[i]) dp[j + v[i]] |= dp[j];
    for (int i = x; i > 0; i--) {
      if (dp[i] && isPrime(i)) {
        cout << i << endl;
        goto next;
      }
    }
    cout << "NA" << endl;
  next:;
  }
}

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

#define REP(i,n) for(int i=0;i<(int)(n);++i)

const int MAX_N = 1000 * 1000 + 10;
bool isPrime[MAX_N];

void makePrimes() {
  REP(i, MAX_N) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i < MAX_N; i++)
      if (isPrime[i])
          for (int j = i * i; j < MAX_N; j += i)
              isPrime[j] = false;
}

bool can[MAX_N];

int main() {
  makePrimes();

  for (int N, X; cin >> N >> X, (N || X); ) {
      memset(can, 0, sizeof(can));
      can[0] = true;
      REP(i, N) {
          int a; cin >> a;
          for (int p = 0; p + a <= X; p++)
              if (can[p]) can[p + a] = true;
      }

      int ans = -1;
      for (int i = 0; i <= X; i++)
          if (isPrime[i] && can[i])
              ans = i;
      if (ans == -1)
          cout << "NA" << endl;
      else
          cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

#define MAX 1000001
int main(){
  vector<bool> prime(1000001, true);
  prime[0] = prime[1] = false;
  for(int i=2; i<MAX; i++)
    if(prime[i])
      for(int j=i+i; j<MAX; j+=i)
	prime[j] = false;

  int n, x;
  while(scanf("%d%d",&n,&x),n+x){
    int cost[n];
    int ans = 0;
    REP(i,n) scanf("%d",cost+i);
    vector<bool> f(x+1, false);
    f[0] = true;
    REP(i,x+1){
      if(f[i]){
	if(prime[i]) ans = i;
	REP(j,n){
	  if(i+cost[j] <= x)
	    f[i+cost[j]] = true;
	}
      }
    }
    if(ans == 0) printf("NA\n");
    else printf("%d\n",ans);
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<functional>
#include<set>
using namespace std;
int main(){
  //ツ素ツ青板青カツ青ャ
  const int NUM = 1000000;
  bool prime[NUM];
  prime[0] = false;
  prime[1] = false;
  for(int i=2;i<NUM;i++)
    prime[i] = true;
  for(int i=2;i*i<NUM;i++)
    for(int k=2;i*k<NUM;k++)
      prime[i*k]=false;

  while(true){
    int n,x;
    cin>>n>>x;
    if(!n&&!x)break;
    int menu[n];
    for(int i=0;i<n;i++){
      cin>>menu[i];
    }
    int p = -1;
    bool money[x+1];
    money[0] = true;
    for(int i=1;i<x+1;i++){
      money[i] = false;
      for(int j=0;j<n;j++){
	if(i>=menu[j]&&money[i-menu[j]]){
	  money[i] = true;
	  break;
	}
      }
      if(money[i]&&prime[i])p=i;
    }
    if(p==-1)cout<<"NA"<<endl;
    else cout<<p<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std ;
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 ;
		long long dp[ 1000000 + 1 ] = { 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 ;
}