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 <cstdio>
#include<string>
using namespace std;
int ha(string);
int saga(int);
int q,m,n=1000000,List[1000];
bool  slist[1000001];
bool  klist[1000001];

int main()
{	
	int i,j,k,b=0,s,a=2,f=0,c=0;
	for( i = 0; i <n;i++ ){
		slist[i]=true;
		klist[i]=false;
	}

	i=0;
	while(i<sqrt((double)n)){
		for(i=a+a;i<n;i=i+a){
			slist[i]=false;
		}
		for(i=a+1;i<sqrt((double)n);i++){
			if(slist[i]==true){
				a=i;
				break;
			}
		}
	}
	cin>>m>>n;
	while(m!=0||n!=0){
	for( i = 0; i <n;i++ ){
		klist[i]=false;
	}
	for(i=0;i<m;i++){
		cin>>List[i];
		klist[List[i]]=true;
	}
		for(i=0;i<n;i++){
			if(klist[i]){
			for(j=0;j<m;j++){				
				klist[i+List[j]]=true;
			}
		}
	}
		for(i=n;i>0;i--){
			if(klist[i]&&slist[i]){
			break;
		}
	}
		if(i==0){
			printf("NA\n");
		}
		else{
		printf("%d\n",i);
		}
		cin>>m>>n;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
bool dp[1000001];
bool a[1000001];
int main() {
	long long n, x;
	fill(a, a + 1000001, true);
	a[0] = false;
	a[1] = false;
	for (int i = 0; i <= 1000000; i++) {
		if (a[i]) {
			int j = 2;
			while (i*j <= 1000000) {
				a[i*j] = false;
				j++;
			}
		}
	}
	while (cin >> n >> x&&n != 0 && x != 0) {
		fill(dp, dp + x + 1, false);
		int y;
		dp[0] = true;
		for (int i = 0; i < n; i++) {
			cin >> y;
			for (int i = y; i <= x; i++) {
				if (dp[i - y]) {
					dp[i] = true;
				}
			}
		}
		for (int i = x; i >= 2; i--) {
			if (a[i]&&dp[i]) {
				cout << i << endl;
				goto stop;
			}
		}
		cout << "NA" << endl;
	stop:;
	}
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
#define sosu 1000001
bool so[sosu];
int n,x,c[31];
bool his[sosu];
int ans;

void hantei() {
  so[0] = so[1] = 1;
  for(int i=2;i*i<=sosu;i++) 
    if(so[i] == 0) 
      for(int j=2;j<=sosu/i;j++)so[i*j] = 1;
}


void saiki(int sum) {
  his[sum] = 1;
  if(so[sum] == 0 && ans < sum) ans = sum;
  
  for(int i=0;i<n;i++) 
    if(c[i] + sum <= x && his[c[i]+sum] == 0) 
      saiki(c[i]+sum);
}


int main() {
  hantei();
  
  while(1){
    cin >> n >> x;
    if(n == 0 && x == 0) break;
    
    for(int i=0;i<n;i++) cin >> c[i];
    for(int i=0;i<=x;i++) his[i] = 0;
    
    ans = -1;
    saiki(0);
    
    if(ans == -1) cout <<"NA"<<endl;
    else cout << ans << endl;
    
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

// 1ÌÆ«trueÉÈÁÄàCÉµÈ¢
bool isPrime(int x){
	for(int i = 2, lim = sqrt(x); i <= lim; ++i){
		if(x % i == 0){ return false; }
	}
	return true;
}

bool table[1000001];

int main(){
	while(true){
		int n, x;
		cin >> n >> x;
		if(n == 0 && x == 0){ break; }
		memset(table, 0, sizeof(bool) * 1000001);
		table[0] = true;
		while(n--){
			int price;
			cin >> price;
			for(int i = 0; i <= x - price; ++i){
				if(table[i]){ table[i + price] = true; }
			}
		}
		int sum = 0;
		for(int i = x; i > 0; --i){
			if(table[i] && isPrime(i)){
				sum = i;
				break;
			}
		}
		if(sum == 0){
			cout << "NA" << endl;
		}else{
			cout << sum << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
int N,X;
int r[31];
bool sosu[1000001];
bool dp[1000001];
int solve(){
  int res=0;
  memset(dp,false,sizeof(dp));
  for(int i=0;i<N;i++) dp[r[i]]=true;
  for(int i=1;i<=X;i++){
    if(!dp[i]) continue;
    if(!sosu[i]) res=i;
    for(int j=0;j<N;j++){
      if(i+r[j]>X) continue;
      dp[i+r[j]]=true;
    }
  }
  return res;
}

main(){
  sosu[0]=true;
  sosu[1]=true;
  for(int i=2;i<1000001;i++){
    if(!sosu[i])
      for(int j=i*2;j<1000001;j+=i){
	if(j>1000000) break;
	sosu[j]=true;
      }
  }
  while(1){
    scanf("%d %d",&N,&X);
    if(N==0 && X==0) break;
    for(int i=0;i<N;i++){
      scanf("%d",&r[i]);
    }
    int res = solve();
    if(res==0) puts("NA");
    else printf("%d\n",res);
  }
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;
bool dp[1000001];
bool pn[1000001];

void eratos(bool f[]){
	for(int i=0; i<1000001; ++i){
		f[i]=true;
	}
	pn[0]=false;
	pn[1]=false;
	for(int i=2; i*i<1000001; ++i){
		if(f[i]){
			for(int j=2; i*j<1000001; ++j){
				f[i*j]=false;
			}
		}
	}
}
int main(){
	int n,x;
	eratos(pn);
	while(cin >> n >> x){
		if(n==0&&x==0) break;
		vector<int> cost(n);
		for(int i=0; i<=x; ++i){
			dp[i]=false;
		}
		for(int i=0; i<n; ++i){
			cin >> cost[i];
			dp[cost[i]]=true;
		}
		for(int i=1; i<n; ++i){
			for(int j=0; j<=x; ++j){
				if(dp[j]&&j+cost[i]<=x){
					dp[j+cost[i]]=true;
				}
			}
		}
		bool flag=false;
		int ans;
		for(int i=x; i>=0; --i){
			if(dp[i]&&pn[i]){
				flag=true;
				ans=i;
				break;
			}
		}
		if(flag) {
			cout << ans << "\n";
		} else {
			cout << "NA\n";
		}
	}
	return 0;
}

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

#define pb push_back
#define MAX 1000000

int dp[MAX];

using namespace std;

int main(void)
{
	int n,x;
	int i1,i2;

	while(1){
		cin>>n>>x;
		if(n+x==0) break;

		vector<int> v(n);
		memset(dp,0,sizeof(dp));

		for(i1=0;i1<n;i1++){
			cin>>v[i1];
			dp[v[i1]]=1;
		}

		for(i1=0;i1<=x;i1++){
			if(dp[i1]==1) {
				for(i2=0;i2<n;i2++){
					if((i1+v[i2])<MAX) dp[i1+v[i2]]=1;
				}
			}
		}

		for(i1=x;i1>0;i1--){
			if(dp[i1]==1) {

				for(i2=2;i2*i2<=i1;i2++){
					if(i1%i2==0) break;
				}
				if(i2*i2>i1) break;
			}
		}

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


	return 0;

}