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 <cmath>
#include <algorithm>
#include <stack>

#define NUM 1100000

using namespace std;

int table[NUM];

void func(int n,int budget){

	int tmp,max_prime = 0,food[n],index = 0;

	bool checkTable[budget+1],FLG;

	for(int i = 0; i < budget+1;i++)checkTable[i] = false;

	checkTable[0] = true;

	for(int i = 0; i < n; i++){
		scanf("%d",&tmp);
		FLG = true;

		//??¢?????????????????°???????¢??????????
		for(int loop = 0; loop < index;loop++){
			if(food[loop] == tmp){
				FLG = false;
				break;
			}
		}

		if(FLG == true && tmp <= budget){
			food[index++] = tmp;

			for(int k = 1; k <= budget; k++){
				if((k%tmp == 0) || (k >= tmp && checkTable[k-tmp] == true)){
					checkTable[k] = true;
					if(table[k] == 1){
						max_prime = max(max_prime,k);
					}
				}
			}
		}
	}

	if(max_prime == 0)printf("NA\n");
	else{
		printf("%d\n",max_prime);
	}

}

int main(){
	int limit;

	for(int i=0; i < NUM;i++)table[i] = 1;
	table[0] = 0;
	table[1] = 0;

	limit = sqrt(NUM);

	for(int i=2;i<=limit;i++){
		if(table[i] == 1){
			for(int k=2*i;k < NUM; k += i){
				table[k] = 0;
			}
		}
	}

	int n,budget;

	while(true){
		scanf("%d %d",&n,&budget);
		if(n == 0 && budget == 0)break;

		func(n,budget);
	}
	return 0;
}

C++: #include<stdio.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(n!=0&&p!=0){
		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;
		for(cnt=0;cnt<=p;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %d",&n,&p);
	}
	return 0;
}

C++: #include<stdio.h>
	long p,cnt,cnt2,food;
	int n,d=0,g,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++){
					youso[cnt+kakaku[cnt2]]=1;
					if(cnt+kakaku[cnt2]>p){
						break;
					}
				}
			}
		}
		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 <cmath>
#include <iostream>

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

bool is_prime(int n) {
    if (n % 2 == 0) return false;
    for (int i = 3; i <= std::sqrt(n); i += 2)
        if (n % i == 0) return false;
    return true;
}

int main() {
    int n, x;
    while (std::cin >> n >> x && n > 0 && x > 0) {
        auto course = new bool[x + 1]();
        rep(i, x + 1) course[i] = false;

        int prices[n];
        rep(i, n) { std::cin >> prices[i]; }
        course[0] = true;

        int max = -1;
        rep(i, x + 1) rep(j, n) {
            if (i - prices[j] >= 0 && course[i - prices[j]]) {
                if (is_prime(i)) max = i;
                course[i] = true;
                break;
            }
        }
        if (max < 0)
            std::cout << "NA" << std::endl;
        else
            std::cout << max << std::endl;
    }
}

C++: #include<iostream>

using namespace std;

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

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

	while (cin >> n >> x, n * x) {
		for (int i = 0; i < n; i++) {
			cin >> price[i];
			isprice[price[i]] = true;
		}
		for (int i = 2; i <= x; i++) {
			if (!isprime[i]) {
				for (int j = i * 2; j <= x; j += i) {
					isprime[j] = 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<iostream>
#include<cmath>
#define PLEN 1000010
using namespace std;

int gcm ( int a, int b ) {
	int temp;

	while ( a != 0 ) {
		
		temp = a-b;
		a = b;
		b = temp;
	}
	return b;
}
		

int dish[30];
int oprime[PLEN];
bool data[PLEN];

int main () {

	for ( int i=0; i<PLEN; i++ ) {
		oprime[i++] = 0;
		oprime[i] = i;
	}

	oprime[2] = 2;

	int limit;
	limit = (int)(sqrt((double)PLEN));

	for ( int i=3; i<limit; i+=2 ) {
		for ( int j=1; j<PLEN; j+=2 ) {
				if ( j != i ) {
				if ( (oprime[j] % i) == 0 ) {
						oprime[j] = 0;
				}
			}
		}
	}

	oprime[1] = 0;
	
	while(true) {
		int dishNum;
		cin >> dishNum;


		if ( dishNum == 0 )
			break;


		for ( int i=0; i<=PLEN; i++ ) {
			data[i] = false;
		}


		for ( int i=0; i<dishNum; i++ ) {
			cin >> dish[i];
			data[dish[i]] = true;
		}

		int budget;
		cin >> budget;


		int count = 1;

		while ( true ) {
			bool done = false;

			for ( int i=0; i<=budget; i++ ) {
				if ( data[i] ) {
					for ( int j=0; j<dishNum; j++ ) {
						if ( !data[i+dish[j]] ) {
							data[i+dish[j]] = true;
							done = true;
						}
					}
				}
			}

			//cout << "Pass" << count++ << endl;

			if ( !done )
				break;
		}
		/*
		for ( int i=0;i<=budget; i++ ) {
			if ( data[i] )
				{
					if ( i%10 )
						//cout << i << endl;
				}
		}
		*/
		int answer = 0;

		for ( int i=0; i<=budget; i++ ) {
			if ( data[i] ) {
				if ( oprime[i] != 0 ) {
					//cout << "S" << i << endl;
					if ( answer < i )
						answer = i;
				}
			}
		}
		
		if ( answer != 0 )
			cout << answer << endl;
		else
			cout << "NA" << endl;
	}
}