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<iostream>
#include<vector>

using namespace std;

int main()
{
	char pri[1000000];
	int i,j;
	
	for(i=0;i<1000000;i++)
	{
		pri[i]='1';
	}
	pri[0]=pri[1]='0';
	
	for(i=0;i<1000000;i++)
	{
		if(pri[i]=='1')
		{
			for(j=2;j*i<1000000;j++)
			{
				pri[i*j]='0';
			}
		}
	}
	
	int n,max,menu[100];
	vector<char> sum;
	int ans=0;
	
	
	for(;;)
	{
		cin >> n >> max ;
		if(n==0 && max==0)
			break;
		sum.clear();
		for(i=0;i<1000000;i++)
			sum.push_back(0);
		
		for(i=0;i<n;i++)
		{
			cin >> menu[i];
			sum[menu[i]]='1';
		}
		for(i=0;i<=max;i++)
		{
//			cout << i << endl;
			if(sum[i]!='1')
			{
				for(j=0;j<i;j++)
				{
					if(sum[j]=='1' && sum[i-j]=='1')
					{
						sum[i]='1';
						if(pri[i]=='1')
							ans=i;
						break;
					}
				}
			}
		}
		
		if(ans==0)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, x, a[30]; bool dp[1000001], prime[1000001];
int main() {
	prime[0] = true, prime[2] = true;
	for(int i = 3; i <= 1000000; i += 2) prime[i] = true;
	for(int i = 3; i <= 1000; i++) {
		if(!prime[i]) continue;
		for(int j = i; i * j <= 1000000; j++) prime[i * j] = false;
	}
	while(scanf("%d%d", &n, &x), n) {
		for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n);
		for(int i = 0; i < x; i++) dp[i + 1] = false; dp[0] = true;
		for(int i = 1; i <= x; i++) {
			for(int j = 0; j < n && i >= a[j]; j++) {
				if(dp[i - a[j]]) {
					dp[i] = true; break;
				}
			}
		}
		for(int i = x; i >= 0; i--) {
			if(dp[i] && prime[i]) {
				printf(i ? "%d\n" : "NA\n", i); break;
			}
		}
	}
}

C++: #include<cstdio>
#include<iostream>
using namespace std;
int so[1000001],m[1000001];
int main(void){
	int i,j,n,x,v[30],flg;
	while(1){
		cin>>n>>x;
		if(n==0) break;
		for(i=0;i<n;i++) cin>>v[i];
		for(i=0;i<=x;i++) so[i]=1;
		for(i=2;i*i<=x;i++){
			if(so[i]!=0){
				for(j=i*2;j<=x;j+=i) so[j]=0;
			}
		}
		for(i=0;i<=x;i++) m[i]=0;
		m[0]=1;
		for(i=0;i<n;i++){
			for(j=0;j<=x;j++){
				if(m[j]==1) m[j+v[i]]=1;
			}
		}
		flg=0;
		for(i=x;i>0;i--){
			if(so[i]==1&&m[i]==1){
				cout<<i<<endl;
				flg=1;
				break;
			}
		}
		if(flg==0) cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[1000050],flg[1000050];
int main(void){
	scanf("%d %d",&n,&x);
	for(i=1;i<=1000001;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=x;i++){
		if(flg[i]==1){
			for(j=2;j*i<=x;j++){
				flg[i*j]=0;
			}
		}
	}
	while(n!=0 && x!=0){
		for(j=0;j<=x;j++){
			dp[j]=-1;
		}
		dp[0]=0;
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[j]!=-1 && j+kin<=x){
					dp[j+kin]=1;
				}
			}
		}
		int max=0;
		for(i=0;i<=x;i++){
			if(dp[i]==1 && flg[i]==1){
				max=i;
			}
		}
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

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

#define reep(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

#define MAX 1000000 

bool prime[MAX+1];
void setPrime(){
	reep(i,2,MAX+1){
		if(prime[i])
			continue;
		bool flg=true;
		reep(k,2,sqrt(i)+1){
			if(i%k==0){
				flg = false;
				break;
			}
		}
		if(flg){
			for(int k=i+i;k<MAX+1;k+=i){
				prime[k] = true;
			}
		}
	}	
}

int main(){
	int n,x;
	setPrime();
	while(cin >> n >> x,n){

		bool dp[MAX+1] = {0};
	
		rep(i,n){
			int v;
			cin >> v;
			dp[v] = true;
			reep(k,v,x+1){
				if(dp[k-v]){
					dp[k] = true;
				}	
			}	
		}

		bool flg = true;
		for(int i=x;i>1;i--){
			if(!prime[i] && dp[i]){
				flg = false;
				printf("%d\n",i);
				break;
			}
		}
		if(flg){
			puts("NA");
		}

	}
	return 0;
}

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

int Wm, N, W;
int w[1000];
bool dp[1000001];
deque< int > vc, newvc;
void rec( int mul){ // mul: 残りの重さ
  if(dp[mul]++) return;
  for(int i = 0; i < N; i++){
    if(mul + w[i] > W){
      newvc.push_back(mul + w[i]);
      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(i){
    if(dp[i] && !isprime(i)) return i;
    i--;
  }
  return 0;
}
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.empty() ; i++){
        rec(vc.front());
        vc.pop_front();
      }
      vc = deque<int>(newvc.begin(),newvc.end());
      newvc.clear();
    }
    W = Wm;
    for(int i = 0; !vc.empty() ; i++){
      rec(vc.front());
      vc.pop_front();
    }
    newvc.clear();
    int ret = solve(W);
    if(ret == 0) cout << "NA" << endl;
    else cout << solve(W) << endl;
    vc.clear();
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
bool DP[31][1145149];
int cost[31];
bool prime(int N){
	if(N==0)return false;
	for(int i=2;i*i<=N;i++){
		if(N%i==0)return false;
	}
	return true;
}
int main()
{
	while(true){
		int N,X;
		scanf("%d%d",&N,&X);
		if(N==0&&X==0)return 0;
		for(int i=0;i<N;i++)scanf("%d",&cost[i]);
		for(int i=0;i<=N;i++){
			for(int j=0;j<=X;j++){
				DP[i][j]=false;
			}
		}
		DP[0][0]=true;
		for(int i=1;i<=N;i++){
			for(int j=0;j<=X;j++){
				if(DP[i-1][j])DP[i][j]=true;
				if(j>=cost[i-1]&&DP[i-1][j-cost[i-1]])DP[i][j]=true;
				if(j>=cost[i-1]&&DP[i][j-cost[i-1]])DP[i][j]=true;
				
			}
		}
		bool F=true;
		for(int i=X;i>=1;i--){
			if(F&&DP[N][i]&&prime(i)){
				printf("%d\n",i);
				F=false;
			}
		}
		if(F)printf("NA\n");
	}
}

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;
}