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 <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 1000000+1
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
vector<bool>Isprime(MAX+1,true);
vvi dp(MAX,vi(30,0));
int main(){
    int n;
    int Primecounter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(Isprime[i]){
            for(int j = 2;i*j<MAX;j++)
                Isprime[i*j] = false;//テァツエツ?ヲツ閉ーテ」ツ?ェテ」ツつ臼rimearrayテ」ツ?ォテヲツ?シテァツエツ催」ツ?療」ツ?ヲテ」ツ?敕」ツ?ョテ・ツ?催ヲツ閉ーテ」ツつ津・ツ?ィテ」ツ?ヲfalseテ」ツ?ォ
            Primecounter++;
        }
    }
    int x;
    while(cin>>n>>x,n+x){
        rep(i,x+1)rep(j,n+1)dp[i][j]=0;
        vi tiya;
        rep(i,n){
            int gotiusa_is_a_god_anime;
            cin>>gotiusa_is_a_god_anime;
            tiya.push_back(gotiusa_is_a_god_anime);
            dp[gotiusa_is_a_god_anime][i]=1;
        }
        rep(i,x+1)rep(j,n){
            if(dp[i][j]){
                dp[i][j+1]=dp[i][j];
               if(i+tiya[j]<=x) dp[i+tiya[j]][j]=1;
            }
        }
        int maxi=-INF;
        for(int i=1; i<=x;i++)if(Isprime[i]&&dp[i][n])cmax(maxi,i);
        if(maxi!=-INF&&maxi!=1)cout<<maxi<<endl;
        else cout<<"NA"<<endl;
    }
}

C++: #include<iostream>

using namespace std;

bool prime[1000001];
bool dp[1000001];
int menu[31];
void seive(int s){
	for(int i=0;i<=s;i++)prime[i]=true;
	prime[0]=prime[1]=false;
	for(int i=2;i<=s;i++){
		if(prime[2]==true){
			for(int j=i*2;j<=s;j+=i){
				prime[j]=false;
			}
		}
	}
}

int main(void){
	seive(1000000);
	int n,x;
	while(1){
		int max=0;
		memset(dp,false,sizeof(dp));
		scanf("%d%d",&n,&x);
		if(n==0 && x==0)break;
		for(int i=0;i<n;i++){
			scanf("%d",&menu[i]);
		}
		dp[0]=true;
		for(int i=0;i<n;i++){
			for(int j=menu[i];j<=x;j++){
				if(dp[j-menu[i]]==true)dp[j]=true;
			}
		}
		for(int i=0;i<=x;i++){
			if(dp[i]==true && prime[i]==true)max=i;
		}
		if(max>0)printf("%d\n",max);
		else printf("NA\n");
	}
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#define MAX 2000000
using namespace std;

const int INF = 1<<29;
bool isPrime[MAX+5],dp[MAX+5];

void Init(){
  for(int i=0;i<=MAX;i++)isPrime[i] = false;
  for(int i=3;i<=MAX;i+=2)isPrime[i] = true;
  isPrime[2] = true;
  int limit = (int)sqrt((double)MAX)+1;
  for(int i=3;i<=limit;i+=2){
    if(!isPrime[i])continue;
    for(int j=i+i;j<=MAX;j+=i)isPrime[j] = false;
  }
}


int main(){
  int n,x,val;
  vector<int> The_rock;
  Init();
  while(cin >> n >> x && n && x){
    The_rock.clear();
    for(int i=0;i<n;i++){
      cin >> val;
      The_rock.push_back(val);
    }
    //int dp[x+10];
  
    for(int i=0;i<=MAX;i++)dp[i] = false;
    
    for(int i=0;i<The_rock.size();i++)dp[The_rock[i]] = true;
    dp[0] = true;
    for(int i=0;i<The_rock.size();i++){
      for(int j=The_rock[i];j<=MAX;j++){
	if(dp[j-The_rock[i]])dp[j] = true;
      }
    }

    bool fin = false;
    for(int i=x;i>=2;i--){
      if(isPrime[i] && dp[i]){
	cout << i << endl;
	fin = true;
	break;
      }
    }
    if(!fin)cout << "NA" << endl;


  }
  return 0;
}

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

#define MAX 1000001

using namespace std;

int main(){
	
	char sosu[MAX]={0};
	sosu[0]=1;
	sosu[1]=1;
	for(int i=2;i<MAX;i++){
		if(sosu[i]==0){
			for(int j=i+i;j<MAX;j+=i){
				sosu[j]=1;
			}
		}
	}
	while(1){
		int n,m;
		scanf("%d%d",&n,&m);
		if(n==0 && m==0){
			break;
		}
		int item[30];
		for(int i=0;i<n;i++){
			scanf("%d",&item[i]);
		}
		bool dp[MAX]={0};
		for(int i=0;i<MAX;i++){
			dp[i]=0;
		}
		dp[0]=1;
		for(int i=0;i<=m;i++){
			for(int j=0;j<n;j++){
				if(item[j]<=i){
					if(dp[i-item[j]]==1){
						dp[i]=1;
						break;
					}
				}
			}
		}
		
		
		for(int i=m;;i--){
			if(i==0){
				printf("NA\n");
				break;
			}
			if(dp[i]==1 && sosu[i]==0){
				printf("%d\n",i);
				break;
			}
		}
	}
}

C++: #include<cstdio>
bool dp[1000001];
bool isprime(int& x){
  for(int i = 2 ; i * i <= x ; i++ ) if( x % i == 0) return true;
  return x == 1;
}
int solve(int& i){
  while(true){
    if(dp[i] && !isprime(i)) return i;
    i--;
  }
}
int main(){
  int n,x,data,ans;
  dp[0] = true;
  while(scanf("%d %d",&n,&x) , n){
    for(int i = 1 ; i <= x ; i++ ) dp[i] = false;
    for(int i = 0 ; i < n ; i++ ){
      scanf("%d",&data);
      for(int i = data ; i <= x ; i++ ) if(dp[i-data]) dp[i] = true;
    }
    if(ans = solve(x)) printf("%d\n",ans);
    else printf("NA\n");
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
bool a[1000001]{};
a[0]=a[1]=true;
for(int b=2;b<1000001;b++){
	if(!a[b]){
		for(int c=b*2;c<1000001;c+=b)a[c]=true;
	}
}
int x,y;
while(scanf("%d%d",&x,&y),x|y){
	bool u[1000000]{};
	u[0]=true;
	int p;
	for(int g=0;g<x;g++){
		scanf("%d",&p);
		for(int q=p;q<=y;q++)if(u[q-p])u[q]=true;
	}
	for(int h=y;h!=-1;h--){
		if(u[h]&&a[h]==false){printf("%d\n",h);break;}
	}
}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool dp[1000001];
bool isprime(int& x){
  for(int i = 2 ; i * i <= x ; i++ ) if( x % i == 0) return true;
  return x == 1;
}
int solve(int i){
  while(true){ if(dp[i] && !isprime(i)) return i; i--; }
}
int main(){
  int n,x,data[30],ans; dp[0] = true;
  while(cin >> n >> x , n){
    for(int i = 1 ; i <= x ; i++ ) dp[i] = false;
    for(int i = 0 ; i < n ; i++ ) cin >> data[i];
    for(int i = 0 ; i <= x ; i++ ){
      if(dp[i]) for(int j = 0 ; j < n ; j++ )
        if(i + data[j] <= x) dp[i+data[j]] = true;
    }
    if(ans = solve(x)) cout << ans << endl;
    else cout << "NA" << endl;
  }
}