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>
#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 ;
}

C++: #include<iostream>
#include<string>
using namespace std;
int prime[200];
bool ans[1000001];
int main(){
	int count=0;
	for(int i=2;i<1000;i++){
		bool t=true;
		for(int j=0;j<count;j++){
			if(i%prime[j]==0)
				t=false;
		}
		if(t==true){
			prime[count]=i;
			count++;
		}
	}
	int n,x,m;
	while(true){
	cin>>n>>x;
	if(n==0&&x==0)
		break;
	ans[0]=true;
	for(int i=1;i<x;i++)
		ans[i]=false;
	for(int u=0;u<n;u++){
		cin>>m;
		for(int i=0;i<m;i++){
			bool t=false;
			for(int j=i;j<=x;j+=m){
				if(ans[j]==true)
					t=true;
				if(t==true)
					ans[j]=true;
			}
		}
	}
	int best=x;
	while(true){
		if(ans[best]==true){
			bool t=true;
			for(int i=0;i<count;i++){
				if(best%prime[i]==0)
					t=false;
			}
			if(t==true)
				break;
		}
		best--;
		if(best==1)
			break;
	}
	if(best==1)
		cout<<"NA"<<endl;
	else
		cout<<best<<endl;
	}
	return 0;
}

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

int main() {

	while(true)
	{
		int N,X;
		cin >> N >> X;
		if(N==0 && X==0)break;
		
		vector<int> food;
		for(int i=0;i<N;i++){
			int m;
			cin >> m;
			food.push_back(m);
		}
		
		vector<int> num(X+1);
		vector<int> prim(X+1);
		fill(num.begin(),num.end(),0);
		fill(prim.begin(),prim.end(),1);
		for(int i=0;i<N;i++){
			num[ food[i] ]=1;
		}
		for(int i=0;i<X+1;i++){
			if( num[i]!=0){
				for(int j=0;j<N;j++){
					if( i+food[j]>=num.size())break;
					num[i+food[j]]=1;
				}
			}
		}
		prim[0]=0;
		prim[1]=0;
		for(int i=2;i<X+1;i++){
			if( prim[i]!=0 ){
				int x=i+i;
				while(x<X+1){
					prim[x]=0;
					x+=i;
				}
			}
		}
		int ans=-1;
		for(int x=X;x>=0;x--){
			if( prim[x]==1 && num[x]!=0){
				ans = x;
				break;
			}			
		}
		if(ans>0){
			cout << ans << endl;
		}else{
			cout << "NA" << endl;
		}
	}
	


	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]||i==1)){
				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;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool isPrime[1000001];
bool dp[1000001];
int main()
{
	fill(isPrime, isPrime+1000001, true);
	isPrime[0] = isPrime[1] = false;
	for (int i = 2; i*i <= 1000000; ++i)
	{
		if (isPrime[i])
		{
			for (int j = 2*i; j <= 1000000; j += i)
				isPrime[j] = false;
		}
	}

	int n, x;
	while (scanf("%d %d", &n, &x), n | x)
	{
		int v[30];
		for (int i = 0; i < n; ++i)
		{
			int t;
			scanf("%d", &t);
			v[i] = t;
		}

		fill(dp, dp+x+1, false);
		dp[0] = true;
		for (int i = 0; i <= x; ++i)
			if (dp[i])
				for (int j = 0; j < n; ++j)
					if (i + v[j] <= x)
						dp[i + v[j]] = true;

		int ans;
		for (ans = x; ans > 0; --ans)
			if (dp[ans] && isPrime[ans])
				break;
		if (ans)
			printf("%d\n", ans);
		else
			puts("NA");
	}

	return 0;
}