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 <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define MAX 1000001

bool prime[MAX];

void sieve(){
  prime[0] = prime[1] = false;
  for(int i=2;i<MAX+1;i++)
    prime[i] = true;
  for(int i=2;i<MAX+1;i++)
    if(prime[i])
      for(int j=i*2;j<MAX+1;j+=i)
	prime[j]=false;
}

bool isPrime(int n){
  return prime[n];
}

void solve(int n, int x, const int *v){
  int dp[n+1][x+1];
  rep(i,n+1)
    dp[i][0]=0;
  rep(i,x+1)
    dp[0][i]=0;

  int max=0;
  for(int i=1;i<n+1;i++)
    for(int j=1;j<x+1;j++){
      int Do =  dp[i][j-v[i]]+v[i];
      int Not = dp[i-1][j];
      if(j<v[i-1])
	dp[i][j] = Not;
      else{
	if (Do > Not){
	  dp[i][j] = Do;
	  if(max<Do && isPrime(Do) && Do<=x)
	    max=Do;
	}
	else
	  dp[i][j] = Not;
      }
    }

  /*
    putchar('\n');
    rep(i,n+1){
    printf("%d :",i+1);
    rep(j,x){
    printf("%2d ",dp[i][j]);
    }
    putchar('\n');
  */

   if(max)
    cout << max << endl;
  else
    cout << "NA" << endl;
}

int main(){
  sieve();
  for(;;){
    int n,x;
    cin >> n >> x;
    if(!(n+x)) break;
    int v[n+1];
    v[0]=0;
    for(int i=1;i<n+1;i++)
      cin >> v[i];
    solve(n,x,v);
  }
  return 0;
}

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

int n,x,ans;
int t[30];
char dp[1000001];
char p[1000001];

void mkPrime(void){
	memset(p,1,sizeof(p));
	p[0] = p[1] = 0;

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

void solve(int sum){
	if(dp[sum]) return;

	dp[sum] = 1;
	if(p[sum] && ans<sum) ans = sum;

	for(int i=0;i<n;i++){
		int nsum = sum + t[i];
		if(nsum > x) return;
		solve(nsum);
	}
}

int main(void){
	mkPrime();

	while(cin>>n>>x && (n||x)){
		for(int i=0;i<n;i++) cin>>t[i];

		memset(dp,0,sizeof(dp));
		ans = 0;

		solve(0);

		if(ans == 0) cout<<"NA\n";
		else cout<<ans<<endl;
	}

	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

#define M 1000001

int main() {
	static char gosei[M] = {};

	//????????°???1???
	for (int i = 2; i <= sqrt(M); i++)
		if (gosei[i] == 0) {
			for (int j = 2, k = 0; (k = i*j) <= M; j++)
				gosei[k] = 1;
		}

	static char dp[M];
	for (int n, x; cin >> n >> x&&n&&x;) {
		memset(dp, 0, sizeof(dp));
		dp[0] = 1;

		for (int i = 0; i < n; i++) {
			int b; cin >> b;
			for (int j = 0; j + b <= x; j++)
				if (dp[j]) dp[j + b] = 1;
		}

		bool NA = true;
		for (int i = x; i >= 1; i--) {
			if (dp[i] && !gosei[i]) {
				cout << i << endl;
				NA = false;
				break;
			}
		}
		if (NA)cout << "NA" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define MP 10000001
using namespace std;
bool p[MP]; int n,x,c[35],ans;
bool rec(int a,int s){
	int f=0;
	for(int i=s;i<n&&a>=c[i];i++)if(i==0||c[i]!=c[i-1]){
		if(a==c[i])return 1;
		else{
			f=rec(a-c[i],i);
			if(f) return 1;
		}
	}
	return f;
}
int main(){
	for(int i=2;i*i<MP;i++)if(!p[i])for(int j=i+i;j<MP;j+=i)p[j]=1;
	p[0]=p[1]=1;
	while(cin>>n>>x,n){
		rep(i,n)cin>>c[i]; sort(c,c+n);
		for(ans=x;ans>=c[0];ans--)if(!p[ans]&&rec(ans,0))break;
		if(ans<c[0])cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

C++: // 2012/11/19 Tazoe

#include <iostream>
using namespace std;

int main()
{
	bool is_prime[1000001];

	is_prime[0] = is_prime[1] = false;
	for(int i=2; i<=1000000; i++){
		is_prime[i] = true;
	}

	for(int i=2; i*i<=1000000; i++){
		if(is_prime[i]){
			for(int j=i*i; j<=1000000; j+=i){
				is_prime[j] = false;
			}
		}
	}

	while(true){
		int n, x;
		cin >> n >> x;

		if(n==0&&x==0)
			break;

		int K[30];
		for(int i=0; i<n; i++){
			cin >> K[i];
		}

		bool dp[1000001];

		dp[0] = true;
		for(int i=1; i<=x; i++){
			dp[i] = false;
		}

		for(int i=0; i<n; i++){
			for(int j=K[i]; j<=x; j++){
				if(!dp[j]&&dp[j-K[i]]){
					dp[j] = true;
				}
			}
		}

		int ans = -1;
		for(int i=x; i>=2; i--){
			if(dp[i]&&is_prime[i]){
				ans = i;
				break;
			}
		}

		if(ans!=-1){
			cout << ans << endl;
		}
		else{
			cout << "NA" << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <cstring>
#define MAX 1000000
using namespace std;

int n,x,d[31];
bool p[MAX+1],dp[MAX+1];

void isPrime(){
   memset(p,1,sizeof(p));
   p[0] = p[1] = false;
   for(int i=2;i*i<=MAX;i++){
      for(int j=i+i;j<=MAX;j+=i)
         p[j] = false;
   }
}

int main(){
   isPrime();
   while(cin >> n >> x,n||x){
      memset(dp,0,sizeof(dp));
      for(int i=0;i<n;i++) cin >> d[i];
      
      dp[0] = true;
      for(int i=0;i<=x;i++){
         if(dp[i]){
            for(int j=0;j<n;j++){
               if(i + d[j] > x) continue;
               dp[i + d[j]] = true;
            }
         }
      }
      
      bool ju = true;
      for(int i=x;i>0;i--){
         if(p[i] && dp[i]){ cout << i << endl; ju = false; break; }
      }
      if(ju) cout << "NA" << endl;
   }
}

C++: #include<iostream>
using namespace std;
#define MAX_N 1000010
bool prime[MAX_N], dp[MAX_N];
int main(){
	for(int i=0;i<=MAX_N;i++){
		prime[i] = true;
	}
	prime[0] = prime[1] = false;
	for(int i=2;i*i<MAX_N;i++){
		if(prime[i]){
			for(int j=i*2;j<=MAX_N;j+=i)prime[j] = false;
		}
	}

	int n, x, menu[30];
	while(1){
                cin >> n >> x;
                if(n==0&&x==0)break;
		for(int i=0;i<=x;i++){
			dp[i] = false;
		}

		for(int i=0;i<n;i++){
			cin >> menu[i];
		}

		int ans = 0;

		dp[0] = true;
		for(int i=1;i<=x;i++){
			for(int j=0;j<n;j++){
				if(i-menu[j] >= 0 && dp[i-menu[j]]){
					dp[i] = true;
				}
			}
			if(dp[i] && prime[i]){
				ans = i;
			}
		}

		if(ans != 0){
			cout << ans << endl;
		}else{
			cout << "NA" << endl;
		}
	}
}