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<vector>
#define MAX 1000010
using namespace std;
int prime[MAX];
bool is_prime[MAX];
void prime_make(int n){
	int p=0;
	for(int i=0;i<=n;i++)
	is_prime[i]=true;
	is_prime[0]=is_prime[1]=false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++]=i;
			for(int j=i*2;j<=n;j+=i)
			is_prime[j]=false;
		}
	}
}
int main(){
	int n,m;
	prime_make(MAX);
	while(cin>>n>>m,n||m){
		bool dp[MAX];
		int p[40];
		vector<int> box;
		for(int i=0;i<=m;i++)
		dp[i]=0;
		dp[0]=1;
		for(int i=0;i<n;i++)
			cin>>p[i];
		for(int i=0;i<=m;i++){
			for(int j=0;j<n;j++){
				if(i-p[j]>=0&&dp[i-p[j]]){
					//cout<<i<<endl;
					dp[i]=1;
					box.push_back(i);
					break;
				}
			}
		}
		int ans=-1;
		for(int i=box.size()-1;i>=0;i--){
			int j;
			for(j=0;prime[j]<box[i];j++){
				if(box[i]%prime[j]==0){
			//		cout<<box[i]<<endl;
					break;
				}
			}
			if(prime[j]>=box[i]){
				ans=box[i];
				break;
			}
		}
		if(ans==-1)
		cout<<"NA"<<endl;
		else
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int prime[1000010];
int dp[1000010];

int main() {
  fill(prime, prime+1000010, 1);

  for (int i = 2; i <= sqrt(1000000)+1; i++) {
    if (prime[i]) {
      for (int j = 2*i; j <= 1000000; j+=i) {
	prime[j] = 0;
      }
    }
  }
  prime[0] = prime[1] = 0;

  while (1) {
    int N, X;
    cin >> N >> X;
    if (N == 0 && X == 0) break;
    int price[40];
    for (int i = 0; i < N; i++) cin >> price[i];

    fill(dp, dp+1000010, 0);
    dp[0] = 1;
    for (int i = 0; i < X; i++) {
      if (!dp[i]) continue;
      for (int j = 0; j < N; j++) {
	if (i + price[j] <= X) dp[i+price[j]] = 1;
      }
    }

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

C++: #include <stdio.h>

int n,m[30];

bool prime[1000000];
bool c[1000000];

void saiki(int total){
	int i;
	if(total==0){
		return;
	}
	
	if(total<0){
		return;
	}
	
	for( i=0 ; i<n ; i++){
		total-=m[i];
		if(c[total]==1)continue;
		if(total>=0){
			c[total]=1;
		}
		saiki(total);
	}
}

int main(void)
{
	int i,j;
	//f
	prime[0]=1;
	prime[1]=1;
	for(i=3;i<1000000;i++){
		if(i%2==0){
			prime[i]=1;
		}
	}
	for(i=3;i<1000000;i++){
		if(prime[i]==0){
			for(j=(i+i);j<1000000;j+=i){
				prime[j]=1;
			}
		}
	}
	//
	while(1){
		int x;
		int flg=0;
		scanf("%d%d",&n,&x);
		if(n==0)break;
		
		for(i=0;i<n;i++){
			scanf("%d",&m[i]);
		}
		saiki(x);
		//
		for(i=0;i<x;i++){
			
			if(c[i]==1 && prime[x-i]==0){
				flg=1;
				break;
			}
		}
		if(flg==1){
			printf("%d\n",x-i);
		}
		else puts("NA");
		for(i=0;i<1000000;i++){
			c[i]=0;
		}
		//
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);

using namespace std;

typedef long long ll;
typedef long double ld;


int n;
ll V[31], x;
ll dp[32][1LL<<32];
ll maxn;

bool sosu(ll t){
  if(t == 1) return false;
  if(t == 2) return true;
  if(t%2 == 0) return false;

  for(ll i=3; i*i<=t; i+=2){
    if(t%i == 0) return false;
  }

  return true;
}

void solve(ll now) {
  if(now > x) return;
  if(sosu(now) && now > maxn)
    maxn = now;
  rep(i, n) {
    solve(now + V[i]);
  }
  return;
}

int main() {
  while(cin >> n >> x, n) {
    maxn = 0;
    rep(i, n) {
      cin >> V[i];
    }
    solve(0);
    if(maxn == 0) {
      cout << "NA" << endl;
    } else {
      cout << maxn << endl;
    }
  }

  return 0;
}

C++: #include<stdio.h>
int hideo[200001] = { 0 }, ruio[20000] = { 0 };
int main()
{
	int  n, x, temp = 1,noyori=0,debu=0 ;
	while (scanf("%d %d", &n, &x)) {
		if (n == 0 && x == 0)return 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &ruio[i]);
			hideo[ruio[i]] = 1;
		}
		while (temp != x) {
			if (hideo[temp] == 1) {
				for (int j = 1; j <= n; j++) {
					if (hideo[temp + ruio[j]] == 0) {
						hideo[temp + ruio[j]] += 1;
					}
				}
			}
			temp++;
		}
		for (int i = x; i >= 1; i--) {
			if (hideo[i] == 1) {
				for (int j = 2; j < i - 1; j++) {
					if (i%j == 0) {
						debu++;
					}
				}
				if (debu == 0) {
					noyori = i;
					break;
				}
			}
			debu = 0;
		}
		if (noyori > 0) {
			printf("%d\n", noyori);
		}
		else {
			printf("NA\n");
		}
		temp = 1;
		noyori = debu = 0;
		for (int i = 1; i <= n; i++) {
			ruio[i] = 0;
		}
		for (int i = 1; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}

C++: #include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000000];
int m[1000000];

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


int main(){
	int n; int x;
	cin >> n >> x;
	for (int i = 0; i < n; i++){
		cin >> a[i];
		m[a[i]] = 1;
	}
	for (int i = 0; i <= 15000; i++){
		if (m[i] > 0){
			for (int j = 0; j < n; j++){
				m[i + a[j]] = 1;
			}
		}
	}
	bool flag = false;
	for (int i = x; i >= 0; i--){
		if (m[i] > 0) {
			if (isPrime(i) == 1) {
				cout << i << endl;
				flag = true;
				break;
			}
		}
	}
	if (flag == false){
		cout << "NA" << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
int main()
{
	long cnt,cnt2;
	int n,x,d=0,g,food,kakaku[1000],youso[1000001]={0};
	int flag=0;
	scanf("%d %d",&n,&x);
	while(n!=0&&x!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%d",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=1;cnt<=x;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					youso[cnt+kakaku[cnt2]]=1;
					if(cnt+kakaku[cnt2]>x){
						break;
					}
				}
			}
		}
		for(cnt=x;cnt>0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					d=cnt;
					flag=1;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		flag=0;
		d=0;
		for(cnt=1;cnt<=x;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}