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 <iostream>
#define MAX 1100000

using namespace std;

bool dp[MAX];
int dish[30];

bool isPrime(int n){

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

}

int main(void){

  int n, value;
  while(cin >> n >> value, n + value){

    for(int i = 0 ; i < MAX ; i++)
      dp[i] = false;

    for(int i = 0 ; i < n ; i++)
      cin >> dish[i];

    dp[0] = true;
    for(int i = 0 ; i <= value ; i++){
      if(dp[i]){
	for(int j = 0 ; j < n ; j++){
	  dp[i + dish[j]] = true;
	}
      }
    }

    bool flag = false;
    for(int i = value ; i > 0 ; i--){
      if(dp[i] && isPrime(i)){
	cout << i <<endl;
	flag = true;
	break;
      }
    }
    if(!flag) cout << "NA" << endl;
  }
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, x, a[30]; bool dp[1000001], prime[1000001];
int main() {
	prime[0] = true, prime[2] = true;
	for(int i = 3; i <= 1000000; i += 2) prime[i] = true;
	for(int i = 3; i <= 1000; i++) {
		if(!prime[i]) continue;
		for(int j = i * i; j <= 1000000; j += i * 2) prime[j] = false;
	}
	while(scanf("%d%d", &n, &x), n) {
		for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n);
		for(int i = 0; i < x; i++) dp[i + 1] = false; dp[0] = true;
		for(int i = 1; i <= x; i++) {
			for(int j = 0; j < n && i >= a[j]; j++) {
				if(dp[i - a[j]]) {
					dp[i] = true; break;
				}
			}
		}
		for(int i = x; i >= 0; i--) {
			if(dp[i] && prime[i]) {
				printf(i ? "%d\n" : "NA\n", i); break;
			}
		}
	}
}

C++: #include<iostream>
using namespace std ;
void ans( int cnt ,int 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 , n )
	{
		int cnt = m ;
		int dp[ 1000000 ] = { 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<iostream>
using namespace std;
int main(){
	bool prime[1000000];
	prime[1] = true;
	for(int i = 2; i * i <= 1000000; i++){
		if(!prime[i]){
			for(int j = i * i; j <= 1000000; j += i){
				prime[j] = true;
			}
		}
	}
	int n,w;
	while(cin >> n >> w){
		if(n == 0 && w == 0) break;
		bool dp[1000001] = {0};
		int data[31];
		for(int i = 0; i < n; i++){
			cin >> data[i];
		}
		dp[0] = true;
		for(int i = 0; i < n; i++){
			for(int j = data[i]; j <= w; j++){
				if(dp[j - data[i]]){
					dp[j] = true;
				}
			}
		}
		int ans = -1;
		for(int i = w; i >= 2; i--){
			if(!prime[i] && dp[i]){
				ans = i;
				break;
			}
		}
		if(ans == -1)puts("NA");
		else printf("%d\n",ans);
	}
	return 0;
}

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

const int INF = 1<<30;
bool isPrime[2000000],dp[2000000];

void Init(){
  for(int i=0;i<=2000000;i++)isPrime[i] = false;
  for(int i=3;i<=2000000;i+=2)isPrime[i] = true;
  isPrime[2] = true;
  int limit = (int)sqrt((double)2000000)+1;
  for(int i=3;i<=limit;i+=2){
    if(!isPrime[i])continue;
    for(int j=i+i;j<=2000000;j+=i)isPrime[j] = false;
  }
}


int main(){
  int n,x,val;
  vector<int> The_rock;
  Init();
  while(cin >> n >> x && n && x){
    The_rock.clear();
    for(int i=0;i<n;i++){
      cin >> val;
      The_rock.push_back(val);
    }
    //int dp[x+10];
  
    for(int i=0;i<=1000000;i++){
      //dp[i] = INF;
      dp[i] = false;
    }
    for(int i=0;i<The_rock.size();i++)dp[The_rock[i]] = true;
    //dp[0] = 0;
    dp[0] = true;
    for(int i=0;i<The_rock.size();i++){
      for(int j=The_rock[i];j+The_rock[i]<=1000000;j++){
	if(dp[j-The_rock[i]])dp[j] = true;
      }
    }
    bool fin = false;
    for(int i=x+9;i>=0;i--){
      if(isPrime[i] && dp[i]){
	cout << i << endl;
	fin = true;
	break;
      }
    }
    if(!fin)cout << "NA" << endl;


  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n){
    if(n <= 1) return vector<int>();
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; ++i) {
        if(is_prime[i]) prime.emplace_back(i);
        for (auto &&j : prime){
            if(i*j > n) break;
            is_prime[i*j] = false;
            if(i % j == 0) break;
        }
    }
    return prime;
}

int main() {
    int M = 1000000;
    vector<bool> is_prime(M+1, true);
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= M; ++i) {
        if(!is_prime[i]) continue;
        for (int j = i*2; j <= M; j += i) {
            is_prime[j] = false;
        }
    }
    int n, x;
    while(cin >> n >> x, n){
        vector<char> dp(x+1);
        dp[0] = 1;
        for (int i = 0; i < n; ++i) {
            int a; cin >> a;
            for (int j = 0; j+a <= x; ++j) {
                dp[j+a] |= dp[j];
            }
        }
        int ok = 0;
        for (int i = x; i >= 2; --i) {
            if(is_prime[i] && dp[i]){
                cout << i << "\n";
                ok = 1;
                break;
            }
        }
        if(!ok) puts("NA");

    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
#define MAX_X (1000005)
 
void calcPrime(bool prime[])
{
    for ( int i = 2; i < MAX_X; i++ ) {
        prime[i] = true;
    }
 
    for ( int i = 2; i * i < MAX_X; i++ ) {
        if ( prime[i] ) {
            for ( int j = i * i; j < MAX_X; j += i ) {
                prime[j] = false;
            }
        }
    }
}
 
int main()
{
    int N, X;
    bool prime[MAX_X];
 
    calcPrime(prime);
 
    while ( cin >> N >> X, N || X ) {
        bool dp[MAX_X] = {};
 
        dp[0] = true;
        for ( int i = 0; i < N; i++ ) {
            int v;
            cin >> v;
            for ( int j = 0; j <= X; j++ ) {
                if ( dp[j] && j + v <= X ) {
                    dp[j + v] = true;
                }
            }
        }
        int k = X;
        while ( k > 0 && (!prime[k] || !dp[k]) ) {
            k--;
        }
        if ( k > 0 ) {
            cout << k << endl;
        } else {
            cout << "NA" << endl;
        }
    }
 
    return (0);
}