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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <string>
using namespace std;

#define PI 3.14159265358979

#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
#define repd(i,s,e) for(int i=s; i>=e; i--)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

int n,x;
int d[31];
const int MAX_C = 1000001;
bool primes[MAX_C];
bool dp[MAX_C];

void eratosthenes() {
	fill(primes, primes+MAX_C, true);
	primes[0] = primes[1] = false;
	repa(i,2,MAX_C-1) {
		if(primes[i]) {
			int j = i<<1;
			while(j < MAX_C) {
				primes[j] = false;
				j += i;
			}
		}
	}
}

void solve() {
	memset(dp, 0, sizeof(dp));
	dp[0] = true;
	repa(i,1,x) {
		if(!dp[i]) {
			rep(j,n) {
				if(i-d[j] >= 0 && dp[i-d[j]]) {
					dp[i] = true;
					break;
				}
			}
		}
	}

	repd(i,x,0) {
		if(dp[i] && primes[i]) {
			cout << i << endl;
			return;
		}
	}
	cout << "NA" << endl;
}

int main() {
	eratosthenes();
	while(cin>>n>>x, n|x) {
		rep(i,n) cin>>d[i];
		solve();
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

#define MAX 1000010
#define INF (1<<29)

int N,X;
int a[30],dp[MAX];
bool isprime[MAX];

void sieve(){
  int i,j;

  rep(i,MAX) isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for(i = 2; i*i <= MAX; i++) if(isprime[i]){
    for(j = 2*i; j <= MAX; j += i) isprime[j] = false;
  }
}

int main(){
  int i,j,k;

  sieve();

  for(;;){
    scanf("%d %d",&N,&X); if(N == 0 && X == 0) break;
    rep(i,N) scanf("%d",&a[i]);

    rep(i,MAX) dp[i] = INF;
    dp[0] = 0;

    int res = 0;
    rep(i,N){
      for(j = 0; j <= X; j++) if(j - a[i] >= 0 && dp[j] > dp[j - a[i]] + 1){
        dp[j] = dp[j - a[i]] + 1;
        if(isprime[j]) res = max(res, j);
      }
    }

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

  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>

#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int n,m;
int sina[30];
char memo[1000001];
char sosu[1000001];

int N(int val){
	if(val<0){
		return 0;
	}
	
	if(val==0){
		return 1;
	}
	
	if(memo[val]!=-1){
		return memo[val];
	}
	
	int ga=0;
	rep(i,n){
		if(N(val-sina[i])==1){
			ga=1;
			break;
		}
	}
	
	return memo[val]=ga;
}

int main(){
	sosu[0]=1;sosu[1]=1;
	for(int i=2;i<1000001;i++){
		if(sosu[i]==1){
			continue;
		}
		for(int j=i*2;j<1000001;j+=i){
			sosu[j]=1;
		}
	}
	
	while(1){
		sintt(n,m);
		if(n==0)break;
		rep(i,n){
			sint(sina[i]);
		}
		
		rep(i,1000001){
			memo[i]=-1;
		}
		
		int flg=1;
		int ans=0;
		
		
		for(int i=m;i>0;i--){
			if(sosu[i]!=0){
				continue;
			}
			
			if(N(i)==1){
				ans=i;
				flg=0;
				break;
			}
		}
		
		/*
		rep(i,m){
			if(memo[i]==0){
				printf("%d\n",i);
			}
		}*/
		
		if(flg==1){
			puts("NA");
		}else{
			printf("%d\n",ans);
		}
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
#define INF (1111111)
int N,X;
int r[31];
bool sosu[1000001];
int dp[1000001];
int solve(int sum){
  if(sum>X) return 0; 
  if(dp[sum]!=-1) return dp[sum];
  int ret=0;
  for(int i=0;i<N;i++){
    ret=max(ret,solve(sum+r[i]));
  }
  if(!sosu[sum]){
    return dp[sum]=max(sum,ret);
  }else{
    return dp[sum]=ret;
  }  
}
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;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<N;i++){
      scanf("%d",&r[i]);
    }
    int res = solve(0);
    if(res==0) puts("NA");
    else printf("%d\n",res);
  }
}

C++: #include <vector>
#include <iostream>

double sqrt(const int &n) {
	double a = n, b = 1;
	while ((a - b) * (a - b) < 0.1) {
		double min = (a + b) / 2;
		if (min * min > n) {
			a = min;
		}
		else {
			b = min;
		}
	}
	return a;
}
std::vector<bool> cul_prime(const int &n) {
	std::vector<bool> prime(n + 1, true);
	prime.at(0) = false;
	prime.at(1) = false;
	double root = sqrt(n) + 1;
	for (auto i = 2; i <= n >> 1; ++i) {
		prime.at(i << 1) = false;
	}
	for (auto i = 3; i < root; i += 2) {
		if (prime.at(i)) {
			for (auto j = 2; j <= n / i; ++j) {
				prime.at(i * j) = false;
			}
		}
	}
	return prime;
}
int main() {
	int n, x;
	std::cin >> n >> x;
	while (n != 0) {
		std::vector<bool> num(x + 1, false);
		std::vector<int> menu;
		for (auto i = 0; i < n; ++i) {
			int price;
			std::cin >> price;
			menu.push_back(price);
		}
		int max = 0;
		auto prime = cul_prime(x);
		num.at(0) = true;
		for (const auto &i : menu) {
			for (auto j = 0; j <= x - i; ++j) {
				if (num.at(j)) {
					if (j + i <= x) {
						num.at(j + i) = true;
						if (max < j + i && prime.at(j + i)) {
							max = j + i;
						}
					}
				}
			}
		}
		if (max == 0) {
			std::cout << "NA\n";
		}
		else {
			std::cout << max << "\n";
		}
		std::cin >> n >> x;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool e[1000001],dp[1001110];
int main(){
  e[0]=e[1]=1;
  for(int i=2;i<1000001;i++)
    for(int j=i*2;j<1000001;j+=i)
      e[j]=1;
  int n,m;
  while(cin>>n>>m,n){
    for(int i=0;i<m+110;i++)dp[i]=0;
    int a[n],c=1,p;
    for(int i=0;i<n;i++)cin>>a[i];
    /*for(int i=0;i<n;i++)
      for(int j=1;j<=m;j++){
        if(dp[j]&&a[i]+j<=m)dp[a[i]+j]=1;
        if(a[i]*j<=m)dp[a[i]*j]=1;}*/
    for(p=m;p>=0;p--)if(dp[p]&&!e[p]){c=0;break;}
    if(c)cout<<"NA"<<endl;
  else cout<<p<<endl;
  }
}