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 <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define REP(i,n) for (int i=0;i<(n);i++)
int main() {
	bool prime[1000001];
	for(int i=0;i<=1000000;i++)prime[i]=true;
	prime[0]=prime[1]=false;
	for(int i=2;i<=1000000;i++){
		if(prime[i]){
			for(int j=i*2;j<=1000000;j+=i)prime[j]=false;
		}
	}
	int n,x;
	while(cin>>n>>x&&n&&x){
		bool data[2000001];
		memset(data,false,sizeof(data));
		data[0]=true;
		for(int i=0;i<n;i++){
			int a;
			cin>>a;
			for(int i=0;i<=x;i++){
				if(data[i])data[i+a]=true;
			}
		}
		int ans=-1;
		for(int i=x;i>=0;i--){
			if(data[i]&&prime[i]){
				ans=i;
				break;
			}
		}
		if(ans==-1)cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}
		
}

C++: #include<iostream>

using namespace std;

bool isprime[1000001];//false????´???°

int main() {
	isprime[1] = true;
	int n, x;
	int price[30];

	for (int i = 2; i <= 1000000; i++) {
		if (!isprime[i]) {
			for (int j = i * 2; j <= 1000000; j += i) {
				isprime[j] = true;
			}
		}
	}

	while (cin >> n >> x, n * x) {
		bool isprice[1000001] = {};//??????????????????????????????true
		for (int i = 0; i < n; i++) {
			cin >> price[i];
			isprice[price[i]] = true;
		}
		for (int i = 1; i <= x; i++) {
			if (isprice[i]) {
				for (int j = 0; j < n; j++) {
					if (i + price[j] <= x) {
						isprice[i + price[j]] = true;
					}
				}
			}
		}
		int ans = 99999999;
		for (int i = x;i >= 1; i--) {
			if (!isprime[i] && isprice[i]) {
				ans = i;
				break;
			}
		}
		if (ans == 99999999)cout << "NA" << endl;
		else cout << ans << endl;
	}
	return 0;
}

C++: #include<stdio.h>

bool prime[1000001];
bool money[1000001];
int mono[31];
int n,x;

void func(int now){
	for(int i = 0; i < n; i++){
		if(now+mono[i] <= x){
			money[now+mono[i]] = 1;
		}
	}
}

int main(void){
	prime[0] = prime[1] = 1;
	for(int i = 2; i < 1000000; i++){
		if(prime[i])continue;
		for(int j = i * 2; j < 1000000; j+=i){
			prime[j] = 1;
		}
	}
	while(scanf("%d%d", &n, &x) && n && x){
		int ans = -1; 
		for(int i = 0; i < n; i++){
			scanf("%d", &mono[i]);
		}
		money[0] = 1;
		for(int i = 0; i <= x; i++){
			if(money[i])func(i);
		}
		for(int i = x; i >= 0; i--){
			if(!prime[i] && money[i]){
				ans = i;
				break;
			}
		}
		if(ans == -1){
			puts("NA");
		}else {
			printf("%d\n",ans);
		}
		
		/*ツ渉可甘コツ可サ*/
		for(int i = 0; i < 1000001; i++){
			money[i] = 0;
		}
		for(int i = 0; i < 31; i++){
			mono[i] = 0;
		}
	}
}

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

int main(){
  int n,x,i,j;
  int d[30];
  bool can[1000001];

  for(;;){
    cin >> n >> x;
    if(!n && !x)break;

    for(i=0;i<n;i++)cin >> d[i];
    for(i=1;i<=x;i++)can[i] = false;
    can[0] = true;

    for(i=0;i<n;i++){
      for(j=0;j+d[i]<=x;j++){
	if(can[j])can[j+d[i]] = true;
      }
    }

    for(i=x;i>=0;i--)
      if(can[i] && i&1){
	bool f=true;
	for(j=3;j*j<=i;j+=2)
	  if(!(i%j)){
	    f = false;
	    break;
	  }
	if(f)break;
      }
    if(i<0)cout << "NA" << endl;
    else cout << i << endl;
  }
}

C++: #include <cstdio>

#define MAX 1000001
int n,x,v;
bool prm[MAX]={false};
bool dp[MAX]={false};

int main(){
	for(int i = 2; i < MAX; i++){
		if(!prm[i]){
			for(int j = i*2; j < MAX; j += i){
				prm[j] = true;
			}
		}
	}
	while(scanf("%d%d",&n,&x), n != 0){
		for(int i = 0; i < MAX; i++){
			dp[i] = false;
		}
		dp[0] = true;
		for(int i = 0; i < n; i++){
			scanf("%d",&v);
			for(int j = 0; j <= x; j++){
				if(dp[j] && j + v <= x){
					dp[j+v] = true;
				}
			}
		}
		
		bool no = true;
		for(int i = x; i > 0; i--){
			if(!prm[i] && dp[i]){
				printf("%d\n",i);
				no = false;
				break;
			}
		}
		if(no){
			printf("NA\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
using namespace std;

#define MAX_X 1000001

int n;
int x;
vector<int> p;

bool table[MAX_X];
vector<int> prime;

void init() {
    memset(table, 1, sizeof(table));
    table[0] = table[1] = false;
    for (int i = 2; i < MAX_X; i++) {
        if (table[i]) {
            prime.push_back(i);
            for (int j = i + i; j < MAX_X; j += i) {
                table[j] = false;
            }
        }
    }
}

bool cache[MAX_X];
bool cached[MAX_X];

bool can_make(int t) {
//    cout << t << endl;
    if (t == 0) return true;
    else if (t < 0) return false;
    if (cached[t]) return cache[t];

    bool can = false;
    for (int i = 0; i < p.size(); i++) {
        if (can_make(t-p[i])) {
            can = true;
            break;
        }
    }
    cache[t] = can;
    cached[t] = true;
    return can;
}

int main() {
    init();
//    for (int i = 0; i < prime.size(); i++) {
//        cout << prime[i] << ' ';
//    }

    while (cin >> n >> x, n || x) {
        memset(cache, 0, sizeof(cache));
        memset(cached, 0, sizeof(cached));

        p.clear();
        p.resize(n);
        for (int i = 0; i < n; i++) {
            cin >> p[i];
        }
        sort(p.begin(), p.end(), greater<int>());

        bool exist = false;

        int ub = prime.size()-1;
        int lb = 0;
        int mid = ub;

        if (!(prime[prime.size()-1] <= x)) {
            while (lb != ub - 1) {
                mid = (lb + ub) / 2;
                if (prime[mid] < x) {
                    lb = mid;
                } else {
                    ub = mid;
                }
            }
        }

//        cout << "ub: " << ub << endl;
        for (int i = mid; 0 <= i; i--) {
            if (can_make(prime[i])) {
                cout << prime[i] << endl;
                exist = true;
                break;
            }
        }

        if (!exist) {
            cout << "NA" << endl;
        }

    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
bool prime[1000001];
bool isReach[1000001];
int main() {
	// your code goes here
	
	vector<int> p;
	p.push_back(2);
	prime[2]=true;
	for(int i=3;i<=1000000;i++){
		for(int j=0;j<p.size();j++){
			if(i%p[j]==0)
			break;
			if(p[j]*p[j]>i ||p.size()-1==j){
			p.push_back(i);
			prime[i]=true;
			break;
			}
		}
	}
	int n,budget,tmp;
	while(cin >> n >> budget && n){
		for(int i=0;i<=1000000;i++)
		isReach[i]=false;
		vector<int> cooks;
		queue<int> qu;
		for(int i=0;i<n;i++){
			cin >> tmp;
			cooks.push_back(tmp);
		}
		qu.push(0);
		int maxim=0;
		while(!qu.empty()){
			int f=qu.front();
			qu.pop();
			if(prime[f])
			maxim=max(maxim,f);
			for(int i=0;i<cooks.size();i++)
				if(f+cooks[i]<=budget && (!isReach[f+cooks[i]])){

					qu.push(f+cooks[i]);
					isReach[f+cooks[i]]=true;
				}
		}
		if(maxim)
		cout << maxim << endl;
		else
		cout << "NA" << endl;
	}
	return 0;
}