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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#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 CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 1000009
/*void dfs(int num){
    if(flag) return;
    if(ans==num){flag=true;return;}
    if(num>ans){memo[num]=false;return;}
    else rep(i,n){
        cout<<num<<endl;
      if(num+v[i]<=ans&&memo[num+v[i]])dfs(num+v[i]);
    }
    memo[num]=false;
    return;
}*/
int main(){
    vector<bool>check(MAX+1);  //index number will be checked
    rep(i, MAX+1)check[i] = true;
    vector<int>Primenumber(MAX+1,0); //Primearray
    int counter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(check[i]){
            for(int j = 2;i*j<MAX;j++)
                check[i*j] = false;//?´???°??????Primearray???????´??????????????????°?????¨???false???
            Primenumber[counter] = i;
            counter++;
        }
    }
    int m,n,ans; bool flag;
    while(cin>>n>>m,n+m){
        vi v;
        int temp;
        flag=false;
        rep(i,n){cin>>temp; v.push_back(temp);}
        for(int i=m;i>=0;i--){
            if(check[i]){
                ans=i;break;}
        }
        bool array[MAX]; rep(i,MAX)array[i]=false;
        rep(i,n)array[v[i]]=true;
        for(int i=2;i<=ans;i++) {
            if(array[i])rep(j,n)if(i+v[j]<=ans)array[i+v[j]]=true;
            if(array[ans])break;
        }
        if(array[ans]) cout<<ans<<endl;
        else cout<<"NA"<<endl;
    }
}

C++: #include<iostream>
#include<cmath>
#define PLEN 1000000
using namespace std;

int gcm ( int a, int b ) {
	int temp;

	while ( a != 0 ) {
		
		temp = a-b;
		a = b;
		b = temp;
	}
	return b;
}
		

int dish[30];
int oprime[PLEN];
int prime[PLEN];
bool data[PLEN];

int main () {

	for ( int i=0; i<PLEN; i++ ) {
		oprime[i++] = 0;
		oprime[i] = i;
	}

	oprime[2] = 2;

	int limit;
	limit = (int)(sqrt((double)PLEN));

	for ( int i=3; i<limit; i+=2 ) {
		for ( int j=1; j<PLEN; j+=2 ) {
				if ( j != i ) {
				if ( (oprime[j] % i) == 0 ) {
						oprime[j] = 0;
				}
			}
		}
	}

	oprime[1] = 0;
	/*
	for ( int i=0; i<PLEN; i++ ) {
		if ( oprime[i] != 0 )
			cout << oprime[i] << endl;
	}

	int count = 0;
	for ( int i=0; i<PLEN; i++ ) {
		if ( oprime[i] != 0 ) {
			prime[count++] = oprime[i];
		}
	}
	
	free(oprime);
	*/

	//cout << "Start" << endl;
	
	while(true) {
		int dishNum;
		cin >> dishNum;

		int budget;
		cin >> budget;

		if ( dishNum == 0 )
			break;


		for ( int i=0; i<=budget; i++ ) {
			data[i] = false;
		}


		for ( int i=0; i<dishNum; i++ ) {
			cin >> dish[i];
			data[dish[i]] = true;
		}

		int count = 1;

		while ( true ) {
			bool done = false;

			for ( int i=0; i<=budget; i++ ) {
				if ( data[i] ) {
					for ( int j=0; j<dishNum; j++ ) {
						if ( !data[i+dish[j]] ) {
							data[i+dish[j]] = true;
							done = true;
						}
					}
				}
			}

			//cout << "Pass" << count++ << endl;

			if ( !done )
				break;
		}
		/*
		for ( int i=0;i<=budget; i++ ) {
			if ( data[i] )
				{
					if ( i%10 )
						//cout << i << endl;
				}
		}
		*/
		int answer = 0;

		for ( int i=0; i<=budget; i++ ) {
			if ( data[i] ) {
				if ( oprime[i] != 0 ) {
					//cout << "S" << i << endl;
					if ( answer < i )
						answer = i;
				}
			}
		}
		
		if ( answer != 0 )
			cout << answer << endl;
		else
			cout << "NA" << endl;
	}
}

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

const int INF = 1<<30;
bool isPrime[2000000],dp[2000000];

void Init(){
  for(int i=0;i<=2000000;i++)isPrime[i] = false;
  for(int i=3;i<=2000000;i+=2)isPrime[i] = true;
  isPrime[2] = true;
  int limit = (int)sqrt((double)2000000)+1;
  for(int i=3;i<=limit;i+=2){
    if(!isPrime[i])continue;
    for(int j=i+i;j<=2000000;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<=2000000;i++){
      //dp[i] = INF;
      dp[i] = false;
    }
    for(int i=0;i<The_rock.size();i++)dp[The_rock[i]] = true;
    //dp[0] = 0;
    dp[0] = true;
    for(int i=0;i<The_rock.size();i++){
      for(int j=The_rock[i];j+The_rock[i]<=2000000;j++){
	if(dp[j-The_rock[i]])dp[j] = true;
      }
    }
    bool fin = false;
    for(int i=x-1;i>=2;i--){
      if(i == 1)continue;
      if(isPrime[i] && dp[i]){
	cout << i << endl;
	fin = true;
	break;
      }
    }
    if(!fin)cout << "NA" << endl;


  }
  return 0;
}

C++: #include <iostream>

using namespace std;

bool flag[1000001];
int n, x, m, price[30];

bool sosuu(int k) {
	if (k == 2) return true;
	else if (k < 2) return false;
	else if (k % 2 == 0) return false;
	for (int i = 3; i*i<=k; i += 2)
		if (k % i == 0) return false;
	return true;
}
void next(int p, int k) {
	if (flag[k]) return;
	flag[k] = true;
	if (k > x) return;
	if (k > m) if (sosuu(k)) m = k;
	if (p == n) return;
	for (int i = 0; k + price[p] * i < x; ++i)
		next(p + 1, k + price[p] * i);
}

int main() {
	while (cin >> n >> x, n || x) {
		for (int i = 0; i < 1000001; ++i) flag[i] = false;
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		m = 0;
		next(0, 0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
}

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

using namespace std;

bool prime[1000001],dp[1000001];

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

int n,x;
vector<int> price;

int main(){
  make_prime();

  while(cin>>n && n){
    cin>>x;
    for(int i=0;i<1000001;i++){
      dp[i]=false;
    }
    for(int i=0;i<n;i++){
      int tmp;
      cin>>tmp;
      price.push_back(tmp);
      dp[tmp]=true;
    }

    sort(price.begin(),price.end());

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

    int ans=0;

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

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