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<bits/stdc++.h>
using namespace std;

int Wm, N, W;
int w[1000];
bool dp[1000001];
vector< int > vc;
void rec( int mul){ // mul: 残りの重さ
  if(dp[mul]++) return;
  for(int i = 0; i < N; i++){
    if(mul + w[i] >= W){
      vc.push_back(mul);
      continue;
    }    
    rec(mul + w[i]);
  }
}
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(){

  while(cin >> N >> Wm, N){
    fill_n( dp, Wm + 1, false);
    for(int i = 0; i < N; i++){
      cin >> w[i];
    }
    vc.push_back(0);
    for(W = 100000 ; W < Wm; W += 100000){
      for(int i = 0; i < vc.size() ; i++){
        rec(vc[0]);
        vc.pop_back();
      }
    }
    W = Wm;
    for(int i = 0; i < vc.size() ; i++){
      rec(vc[0]);
      vc.pop_back();
    }
    cout << solve(W) << endl;
    vc.clear();
  }
}

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=1;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>
using namespace std;
#define MAX 1000000
int main()
{
  bool b[MAX+1]={0};
  int i,j;
  b[0]=b[1]=1;
  for(i=4;i<=MAX;i+=2)b[i]=1;
  for(i=3;i*i<=MAX;i+=2)
    if(!b[i])for(j=i+i;j<=MAX;j+=i)b[j]=1;
  int n,x,price[30],dp[MAX+1];
  bool f;
  while(cin>>n>>x,n|x){
    for(i=0;i<n;i++)cin>>price[i];
    for(i=0;i<x+1;i++)dp[i]=(i==0);
    for(i=0;i<x+1;i++){
      if(!dp[i])continue;
      for(j=0;j<n;j++)if(i+price[j]<x+1)dp[i+price[j]]=true;
    }
    f=false;
    for(i=x;i>0;i--)if(dp[i]&!b[i]){f=true;break;}
    if(f)cout<<i<<endl;
    else puts("NA");
  }
}

C++: #include<iostream>
#include<cstring>
using namespace std;
#define MAX_N 1000000
#define MAX_C 32
bool dp[MAX_N * 2];
bool prime[MAX_N]; int n, m, a[MAX_C];
int main() {
	for (int i = 2; i < MAX_N; i++) { prime[i] = true; }
	for (int i = 2; i*i < MAX_N; i++) {
		for (int j = i*i; j < MAX_N; j += i) {
			prime[j] = false;
		}
	}
	while (true) {
		cin >> n >> m;
		if (n == 0 && m == 0) { break; }
		for (int i = 0; i <= m; i++) {
			dp[i] = false;
		}
		dp[0] = true;
		for (int i = 0; i < n; i++) { cin >> a[i]; }
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (dp[j] == true) {
					dp[j] = true;
					dp[j + a[i]] = true;
				}
			}
		}
		for (int i = m; i >= 0; i--) {
			if (dp[i] == true && prime[i] == true) {
				cout << i << endl; goto E;
			}
		}
		cout << "NA" << endl;
	E:;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
	bool p[1000001];
	int n, x;

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

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

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

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

		vector< bool > dp(x + 1, false);
		int money;
		dp[0] = true;

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


		int ans = 0;
		for(int i = x; i >= 0; --i) {
			if(p[i] && dp[i]) {
				cout << i << endl;
				break;
			} else if(i == 0) {
				cout << "NA" << endl;
			}
		}
	}
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int prime[1000001];
int pricecheck[2000001];
int main(){
    for(int i=2;i<=1000000;i++)prime[i]=1;
    for(int i=2;i<=1000;i++){
        if(!prime[i])continue;
        for(int j=2;;j++){
            if(1000000<i*j)break;
            prime[i*j]=0;
        }
    }
    while(1){
        int N,X;
        cin>>N>>X;
        if(N==0&&X==0)break;
        int price[30];
        for(int i=0;i<N;i++)cin>>price[i];
        for(int i=0;i<=1000000;i++)pricecheck[i]=0;
        pricecheck[0]=1;
        for(int i=0;i<1000000;i++){
            if(pricecheck[i]){
                for(int j=0;j<N;j++){
                    pricecheck[i+price[j]]=1;
                }
            }
        }
        for(int i=X;;i--){
            if(i==1){
                puts("NA");
                break;
            }
            if(pricecheck[i]&&prime[i]){
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}