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] != -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<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))
#define X 1000001
int ans;
int n,x,v[30];
bool p[X];
int dp[31][X];

int main(){
	p[0]=1;
	reps(i,2,X)if(!p[i])for(int j=i+i;j<X;j+=i)p[j]=1;
	for(;;){
		scanf("%d%d",&n,&x);
		if(!n)break;
		x++;
		rep(i,n){
			scanf("%d",v+i);
		}
		fill_n(dp[0],31*X,0);
		dp[0][0]=1;
		rep(i,n){
			rep(j,x){
				if(dp[i][j])for(int k=0;j+k*v[i]<x;k++){
					dp[i+1][j+k*v[i]] = 1;
				}
			}
		}
		ans=0;
		rep(j,x){
			if(dp[n][j] && !p[j])ans = max(ans,j);
		}
		if(ans)printf("%d\n",ans);
		else puts("NA");

	}
	return 0;
}

C++: #include<stdio.h>
#include <string.h>
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
int p,cnt,cnt2,food;
int n,d=0,g,kakaku[31],flag,youso[1000001]={0};
int main()
{
	scanf("%d %d",&n,&p);
	while(1){
		memset(youso, 0, sizeof(youso));
		if(n==0&&p==0){
			break;
		}
		for(cnt=1;cnt<=n;cnt++){
			scanf("%d",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=0;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					if(cnt+kakaku[cnt2]<=p){
						youso[cnt+kakaku[cnt2]]=1;
					}
				}
			}
		}
		flag=0;
		for(cnt=p;cnt>=0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					flag=1;
					d=cnt;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		d=0;
		flag=0;
		
		scanf("%d %d",&n,&p);
	}
	return 0;
}

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

using namespace std;

int main(){
	bool sosuu[1000011];
	bool dp[1000011];
	
	while(1){
		for(int i=0;i<=1000000;i++){
			sosuu[i]=0;
			dp[i]=0;
		}
		for(int i=2;i<=500000;i++){
			if(sosuu[i]==0){
				for(int j=i;j<=1000000;j+=i){
					sosuu[j]=1;
				}
				sosuu[i]=0;
			}
		}
		sosuu[0]=1;
		sosuu[1]=1;
		
		int item[30];
		
		int n,m;
		scanf("%d%d",&n,&m);
		
		if(n==0 && m==0){
			break;
		}
		for(int i=0;i<n;i++){
			scanf("%d",&item[i]);
		}
		dp[0]=1;
		for(int i=0;i<=m;i++){
			for(int j=0;j<n;j++){
				if(i<item[j]){
					continue;
				}
				if(dp[i-item[j]]==1){
					dp[i]=1;
				}
			}
		}
		int num=0;
		for(int i=m;i>=0;i--){
			if(dp[i]==1&&sosuu[i]==0){
				num=i;
				break;
			}
		}
		if(num==0){
			puts("NA");
		}else{
			printf("%d\n",num);
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;
const int MAX_N = 1000000;
bool prime[MAX_N+1];

void make_prime (void )
{
	fill (prime, prime + MAX_N, true );

	prime[0] = false;
	prime[1] = false;

	for (int i = 2; i < MAX_N + 1; ++i ){
		if (prime[i] ){
			for (int j = 2*i; j < MAX_N + 1; j += i ){
				prime[j] = false;
			} // end for
		} // end if
	} // end for
}

bool dp[31][MAX_N+1];

int main()
{
//	cut here before submit 
//	freopen ("testcase.be", "r", stdin );
	int n, x;

	make_prime ();

	while (cin >> n >> x && n && x ){
		vector <int> cost (n, 0 );
		for (int i = 0; i < n; ++i ){
			cin >> cost[i];
		} // end for
		memset (dp, false , sizeof (dp ) );

		for (int j = cost[0]; j <= x; j += cost[0] )
			dp[0][j] = true;

		for (int i = 0; i < n; ++i ){
			for (int j = 0; j <= x; ++j ){
				if (j < cost[i] ){
					dp[i+1][j] = dp[i][j];
				}else{
					dp[i+1][j] = dp[i][j] || dp[i+1][j - cost[i]];
				} // end if
			} // end for
		} // end for 

		int res = 0;
		for (int j = MAX_N; j >= 0; --j ){
			if (dp[n][j] && prime[j] ){
				res = j;
				break;
			} // end if
		} // end for

		if (res == 0 ){
			cout << "NA" << endl;
		}else{
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define MAX 10000000
bool sosu[MAX];
int main(){
	memset(sosu,true,MAX);
	for(int i=4;i<MAX;i+=2)sosu[i]=false;
	for(int i=3;i<MAX;i+=2)
	for(int j=3*i;j<MAX;j+=2*i)sosu[j]=false;
	int n,b;
	while(cin>>n>>b,n||b){
		vector<int>in(n);
		rep(i,n)cin>>in[i];
		vector<bool>h(b+10);
		h[0]=true;
		rep(i,n){
			rep(j,b-in[i]+1)if(h[j])h[j+in[i]]=true;
		}
		for(int i=b;i>=0;i--){
			if(!i)cout<<"NA"<<endl;
			else if(sosu[i]&&h[i]){
				cout<<i<<endl;
				break;
			}
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <bitset>

using namespace std;
const int MAX = 1000010;
bitset<MAX> isPrime;

void sieve(){
    bitset<MAX> find;
    int i;
    for(i=2;i*i < MAX;i++){
        if(find[i]) continue;
        isPrime.set(i);
        for(int j=i;j<MAX;j++){
            if(j%i == 0){
                find.set(j);
            }
        }
    }
    for(;i<MAX;i++){
        if(!find[i]) {
            isPrime.set(i);
        }
    }
}

int main(){
    sieve();

    while(true){
        int n,x;
        cin >> n >> x;
        if(n==0 && x==0) return 0;
        vector<int> R(n);
        for(int i=0;i<n;i++){
            cin >> R[i];
        }

        bitset<MAX> S;
        S.set(0);
        for(int i=0;i<R.size();i++){
            for(int j=0;j<=x;j++){
                if(!S[j]) continue;
                int newe = j + R[i];
                if(newe > x) continue;
                S.set(newe);
            }
        }
        int maxn = -1;
        for(int i=0;i<=x;i++){
            if(isPrime[i] && S[i]) maxn = i;
        }
        if(maxn == -1) cout << "NA" << endl;
        else cout << maxn << endl;
    }
    return 0;
}