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<stdio.h>
#include<bitset>
int main()
{
	const int N=1000001;
	static std::bitset<N>p,c;
	int i,j,n,x,a[30],f;
	p.set();
	p[0]=p[1]=0;
	for(i=2;i<N/2;++i)if(p[i])for(j=2*i;j<N;j+=i)p[j]=0;

	while(scanf("%d%d",&n,&x),n)
	{
		for(i=0;i<n;++i)scanf("%d",&a[i]);
		c.reset();
		c[0]=1;
		for(i=0;i<n;++i)
		{
			for(j=0;j+a[i]<=x;++j)
			{
				if(c[j]) c[ j+a[i] ]=1;
			}
		}
		f=0;
		for(i=x;i>=0;--i)
			if(c[i]&&p[i])
			{
				f=1,x=i;
				break;
			}
		if(f) printf("%d\n",x);
		else puts("NA");
	}
	return 0;
}

C++: #include<stdio.h>
long p,cnt,cnt2,food,g;
int n,d=0,kakaku[31],flag,youso[1000001]={0};
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 main()
{
	scanf("%d %ld",&n,&p);
	while(n!=0&&p!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%ld",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=1;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					if(cnt+kakaku[cnt2]<=p){
						youso[cnt+kakaku[cnt2]]=1;
					}
				}
			}
		}
		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;
		for(cnt=1;cnt<=p;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %ld",&n,&p);
	}
	return 0;
}

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;
	
	
	for(;;)
	{
		cin >> n >> max ;
		if(n==0 && max==0)
			break;
		int ans=0;
		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<iostream>
#include<vector>

using namespace std;

const int MAX=1000001;

void sieve(int MAX,bool prime[]){
	for(int i=0; i<=MAX; i++)	prime[i]=true;
	prime[0]=false;
	prime[1]=false;
	for(int i=2; i*i<=MAX; i++){
		if(prime[i])for(int j=i+i; j<=MAX; j+=i)prime[j]=false;
	}
}

int main(){
	bool prime[MAX+1];
	sieve(MAX,prime);
	int N,X;
	while(cin >> N >> X && N && X){
		int a[N];
		for(int i=0; i<N; i++)	cin>>a[i];
		vector<int> dp=vector<int>(X+1, 0);
		int ans=-1;
		for(int i=0; i<N; i++){
			for(int j=1; j<=X; j++){
				if(j>=a[i]){
					dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
					if(prime[dp[j-a[i]]+a[i]])	ans=max(ans,dp[j-a[i]]+a[i]);
				}
			}
		}
		if(ans>0)	cout << ans << endl;
		else		cout << "NA" << endl;
	}
}

C++: #include<stdio.h>
int hideo[1000001] = { 0 }, ruio[31] = { 0 };
int main()
{
	int  n, x, temp = 1,noyori=0,debu=0 ;
	while (scanf("%d %d", &n, &x)) {
		if (n == 0 && x == 0)return 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &ruio[i]);
			hideo[ruio[i]] = 1;
		}
		while (temp != x) {
			if (hideo[temp] == 1) {
				for (int j = 1; j <= n; j++) {
					if (hideo[temp + ruio[j]] == 0) {
						hideo[temp + ruio[j]] += 1;
					}
				}
			}
			temp++;
		}
		for (int i = x; i >= 1; i--) {
			if (hideo[i] == 1) {
				for (int j = 2; j < i - 1; j++) {
					if (i%j == 0) {
						debu++;
					}
				}
				if (debu == 0) {
					noyori = i;
					break;
				}
			}
			debu = 0;
		}
		if (noyori > 0) {
			printf("%d\n", noyori);
		}
		else {
			printf("NA\n");
		}
		temp = 1;
		noyori = debu = 0;
		for (int i = 1; i <= n; i++) {
			ruio[i] = 0;
		}
		for (int i = 1; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
vector<bool>prime_t(int n){
  vector<bool>prime(n + 1,true);
  if(n >= 0)prime[0] = false;
  if(n >= 1)prime[1] = false;
  for(int i = 2; i * i <= n; i++){
    if(!prime[i])continue;
    for(int j = i + i; j <= n; j += i)prime[j] = false;
  }
  return prime;
}
int main(){
  while(true){
    int n,x;
    cin >> n >> x;
    if(n == 0 && x == 0)return 0;
    vector<int>v(n);
    rep(i,n)cin >> v[i];
    auto P = prime_t(x);
    vector<bool>dp(x + 1,false);
    dp[0] = true;
    int ans = -1;
    rep(i,x + 1){
      if(!dp[i])continue;
      if(P[i])ans = max(ans,i);
      rep(j,n){
        if(i + v[j] > x)continue;
        dp[i + v[j]] = true;
      }
    }
    if(ans == -1)cout << "NA" << endl;
    else cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <array>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_PAY = 1e6 + 10;
array<bool, MAX_PAY> PrimeTable;

void makePrimeTable()
{
    fill(PrimeTable.begin(), PrimeTable.end(), true);

    PrimeTable.at(0) = PrimeTable.at(1) = false;

    for (int i = 2; i * i <= MAX_PAY; ++i) {
        for (int j = 2; j * i < MAX_PAY; ++j) {
            PrimeTable.at(j * i) = false;
        }
    }
}


int main()
{
    makePrimeTable();

    int cook_num, max_pay;

    while (cin >> cook_num >> max_pay) {
        // input
        if (cook_num == 0 && max_pay == 0) break;

        vector<int> dish(cook_num);
        vector<bool> dp(MAX_PAY);

        for (auto& it: dish) {
            cin >> it;
            dp.at(it) = true;
        }

        // solve
        for (int i = 0; i < cook_num; ++i) {
            for (int j = 0; j <= max_pay; ++j) {
                if (dp.at(j) && j + dish.at(i) <= max_pay) {
                    try {
                        dp.at(j + dish.at(i)) = true;
                    } catch(...){
                        cout << j + dish.at(i) << endl;
                    }
                }
            }
        }

        // return max_ans
        int max_ans = 0;
        for (int temp = 0; temp <= max_pay; ++temp) {
            if (dp.at(temp) && PrimeTable.at(temp)) max_ans = temp;
        }

        // output
        if (max_ans) {
            cout << max_ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }

    return 0;
}