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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    vector<int> primes;
    primes.PB(2);
    for(int i=3;i<=1000001;i++){
	bool flg = true;
	for(int j=0;primes[j]*primes[j]<=i;j++){
	    if(i % primes[j] == 0) flg = false;
	}
	if(flg) primes.PB(i);
    }
    while(true){
	int ans = 0;
	int n,x;
	cin >> n >> x;
	if(n==0&&x==0) break;
	bool d[1000001] = {false};
	d[0] = true;
	REP(i,n){
	    int a;
	    cin >> a;
	    for(int i=0;i+a<=1e6;i++){
		if(d[i])
		    d[i+a] = d[i];
	    }
	}
	int s = upper_bound(ALL(primes),x) - primes.begin();
	RREP(i,s){
	    if(d[primes[i]]){
		ans = primes[i];
		break;
	    }
	}
	if(ans==0) cout << "NA" << endl;
	else cout << ans << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

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

const int INF = 1001001001;

int main()
{
	int n;
	while(scanf("%d", &n), n){
		int x;
		scanf("%d", &x);
		
		int p[30];
		rep(i, n)
			scanf("%d", &p[i]);
		
		bool dp[1000001] = {false};
		dp[0] = true;
		reps(i, 1, x+1){
			rep(j, n)
				dp[i] |= i>=p[j] && dp[i-p[j]];
		}
		
		bool isprime[1000001];
		fill(isprime, isprime+x+1, true);
		isprime[0] = isprime[1] = false;
		reps(i, 2, x){
			if(isprime[i]){
				for(int j=i*2; j<=x; j+=i)
					isprime[j] = false;
			}
		}
		
		int ans = -1;
		for(int i=x+1; i>=0; --i){
			if(dp[i] && isprime[i]){
				ans = i;
				break;
			}
		}
		if(ans == -1)
			puts("NA");
		else
			printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)
#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 sintttt(i,j,k,m) scanf("%d%d%d%d",&i,&j,&k,&m);

#define INF 1010000000
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};


char sosu[1000001];
char can[1000001];

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

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>

using namespace std;

int main() {
	int n, x;
	vector<int> furui(1000000+1, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i <= 1000000; i++) {
		if (furui[i] == 0) {
			for (int j = 2; i*j <= 1000000; j++) {
				furui[i*j] = 1;
			}
		}
	}
	while (cin >> n >> x) {
		if (n == 0 && x == 0) {
			break;
		}
		vector<int> en;
		priority_queue<int, vector<int>, greater<int> > pq;
		vector<int> dp(x+1, 0);
		for (int i = 0; i < n; i++) {
			int k;
			cin >> k;/*
			if (k > x || k == 0) {
				continue;
			}
			dp[k] = 1;
			pq.push(k);*/
		}
		en.push_back(pq.top());
		pq.pop();
		/*
		while (!pq.empty()) {
			int k = pq.top();
			pq.pop();
			bool hantei2 = true;
			for (int i = 0; i < en.size(); i++) {
				if (k % en[i] == 0) {
					hantei2 = false;
					break;
				}
			}
			if (hantei2) {
				en.push_back(k);
			}
		}
		for (int i = 0; i < en.size(); i++) {
			for (int j = 0; j <= x; j++) {
				if (j < en[i]) {
				} else {
					dp[j] = max(dp[j], dp[j-en[i]]);
				}
			}
		}
		bool hantei = true;
		for (int i = x; i >= 0; i--) {
			if (dp[i] > 0 && furui[i] == 0) {
				cout << i << endl;
				hantei = false;
				break;
			}
		}
		if (hantei) {
			cout << "NA" << endl;
		}*/
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999

bool era[10000001]={false};


void makeERA(){
    for(int i=2;i<10000001;i++){
        if(era[i]==true){
            int x=10000000/i;
            for(int j=2;j<=x;j++){
                era[i*j]=false;
            }
        }
    }
    era[2]=true;
}


int main(){
    for(int i=0;i<10000001;i++)era[i]=true;
    era[0]=false;era[1]=false;
    makeERA();

    int n,x;
    while(cin>>n>>x){
        if(n==0&&x==0)break;
        vector<int> data(n);
        rep(i,n) cin>>data[i];
        
        static bool dp[1000100];
        rep(i,1000100)dp[i] = false;
        dp[0] = true;
        
        rep(i,n){
            int val = data[i];
            rep(j,x+1){
                if(j-val>=0) dp[j] |= dp[j-val];
            }
        }
        for(int i=x;i>=0;i--){
            if(era[i]&&dp[i]){
                cout<<i<<endl;
                goto next;
            }
        }
        cout<<"NA"<<endl;
    next:;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int n, x, ans;
int in[310];
bool dp[10000001];
void solve(int a, int sum){
	for (int i = a; i <= n; i++){
		if (sum + in[i] <= x){
			if (dp[sum + in[i]])
				continue;
			if (sum + in[i] % 2 && sum + in[i] % 3){
				dp[sum + in[i]] = 1;
			}
			solve(i, sum + in[i]);
		}
	}
}
int main(){
	while (cin >> n >> x&&n&&x){
		ans = 0;
		fill(dp, dp + 1000001, 0);
		for (int i = 1; i <= n; i++)
			cin >> in[i];
		solve(1, 0);
		for (int i = x; i > 0; i--){
			if (dp[i]){
				for (int j = 5; j <= x; j++){
					if (j == i)
						ans = j;
					if (i % j == 0)
						break;
				}
				if (ans)
					break;
			}
		}
		if (ans)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}