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])
		if n == 0 && x == 0 {
			break
		}
		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 <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <limits>
#include <cstring>
#include <cmath>
#include <cfloat>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

#define LIMIT 1000010
using namespace std;

bool isnotprime[LIMIT] = {0};
bool dp[31][LIMIT] = {0};
int n, x;
int dishes[31];

void erathos()
{
	CLR(isnotprime);
	
	FOR(i, 2, LIMIT)
	{
		if( !isnotprime[i] )
		{
			for(int j = 2; i*j < LIMIT; j++)
			{
				isnotprime[i*j] = true;
			}
		}
	}
	
	return;
}

int solve()
{
	int res = 0;
	
	CLR(dp);
	
	FOR(j, 0, n)
	{
		for(int i = dishes[j]; i <= x; i += dishes[j])
		{
			dp[j][i] = true;
		}
		
		if( j > 0 )
			for(int i = 1; i <= x; i++)
			{
				if(  ( dp[j-1][i] ) || ((i - dishes[j] >= 0) && dp[j][i - dishes[j]]) )
				{
					dp[j][i] = true;
				}
			}
	}
	
	RFOR(i, 0, x+1)
	{
		if( !isnotprime[i] && dp[n-1][i] )
		{
			res = i;
			break;
		}
	}
	
	return res;
}

int main()
{
	erathos();
	
	while(cin >> n >> x, (n||x))
	{
		FOR(i, 0, n)
		{
			cin >> dishes[i];
		}
		
		int r = solve();
		
		if(r != 0)
			cout << r << endl;
		else
			cout << "NA" << endl;
	}
		
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;
bool dp[30][1000001];
bool pn[1000001];

void eratos(bool f[]){
	for(int i=0; i<1000001; ++i){
		f[i]=true;
	}
	pn[0]=false;
	pn[1]=false;
	for(int i=2; i*i<1000001; ++i){
		if(f[i]){
			for(int j=2; i*j<1000001; ++j){
				f[i*j]=false;
			}
		}
	}
}
int main(){
	int n,x;
	eratos(pn);
	while(cin >> n >> x){
		if(n==0&&x==0) break;
		vector<int> cost(n);
		for(int i=0; i<n; ++i){
			cin >> cost[i];
		}
		for(int i=0; i<n; ++i){
			for(int j=0; j<=x; ++j){
				dp[i][j]=false;
			}
		}
		for(int i=0; i*cost[0]<=x; ++i){
			dp[0][cost[0]*i]=true;
		}
		for(int i=1; i<n; ++i){
			for(int j=0; j<=x; ++j){
				if(dp[i-1][j]){
					for(int k=0;j+cost[i]*k<=x ; ++k){
						dp[i][j+cost[i]*k]=true;
					}
				}
			}
		}
		bool flag=false;
		int ans;
		for(int i=x; i>=0; --i){
			if(dp[n-1][i]&&pn[i]){
				flag=true;
				ans=i;
				break;
			}
		}
		if(flag) {
			cout << ans << "\n";
		} else {
			cout << "NA\n";
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <limits>
#include <cstring>

constexpr int MAX_X = 1000001;
constexpr int MAX_N = 31;

int n, x, values[MAX_N];
bool is_prime_number[MAX_X] = {true};

void initialize();

void solve();

int main(){
    initialize();
    while(true){
        memset(values, 0, MAX_N);

        std::cin >> n >> x;
        
        if(n + x == 0) break;
        
        for(int i = 0; i < n; ++i)
            std::cin >> values[i];

        solve();
    }
}

void initialize(){
    is_prime_number[0] = false, is_prime_number[1] = false;
    for(int i = 2; i * i <= MAX_X; ++i){
        if(is_prime_number[i]){
            for(int j = i + 1; j < MAX_X; ++j){
                if(j % i == 0)
                    is_prime_number[j] = false;
            }
        }
    }
}

void solve(){
    int ans = 0;
    int dp[MAX_X];
    memset(dp, 0, MAX_X);

    /*
    std::cout << "x : " << x << std::endl << "n : " << n << std::endl;
    for(int i = 0; i < (x > 1000 ? 1000 : x); ++i){
        std::cout << i << " dp : " << dp[i] << std::endl;
    }
    */

    for(int i = 1; i <= x; ++i){
        int res = 0;
        for(int j = 0; j < n; ++j){
            if(values[j] <= i){
                res = std::max(dp[i - values[j]] + values[j], res);
            }
        }
        dp[i] = std::max(dp[i], res);
        if(is_prime_number[dp[i]]){
            ans = std::max(dp[i], ans);
        }
    }

    if(ans != 0){
        std::cout << ans << std::endl;
    }
    else{
        std::cout << "NA" << std::endl;
    }

}

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(1){
		if(n==0&&p==0){
			break;
		}
		for(cnt=0;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 <iostream>
#include <cstdio>
using namespace std;
#define sosu 1000001
bool so[sosu];
int n,x,c[31];
bool his[sosu];
int ans;

void hantei() {
  so[0] = so[1] = 1;
  for(int i=2;i*i<sosu;i++) 
    if(so[i] == 0) 
      for(int j=2;j<sosu/i;j++)so[i*j] = 1;
}


void saiki(int sum) {
  his[sum] = 1;
  if(so[sum] == 0 && ans < sum) ans = sum;
  
  for(int i=0;i<n;i++) 
    if(c[i] + sum <= x && his[c[i]+sum] == 0) 
      saiki(c[i]+sum);
}


int main() {
  hantei();

  while(1){
    cin >> n >> x;
    if(n == 0 && x == 0) break;
    
    for(int i=0;i<n;i++) cin >> c[i];
    for(int i=0;i<=x;i++) his[i] = 0;
    
    ans = -1;
    saiki(0);
    
    if(ans == -1) cout <<"NA"<<endl;
    else cout << ans << endl;
    
  }
  
  return 0;
}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[1000001];
int flg[1000001];
int main(void){
	for(i=1;i<=1000000;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(j=2;j*i<=1000000;j++){
				flg[i*j]=0;
			}
		}
	}
	scanf("%d %d",&n,&x);
	while(n!=0 && x!=0){
		for(j=0;j<=x;j++){
			if(j>0){
				dp[j]=-1;
			}
			else if(j==0){
				dp[j]=0;
			}
		}
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[j]!=-1){
					dp[j+kin]=1;
				}
			}
			/*for(j=0;j<=x;j++){
				dp[i+1][j]=dp[i][j];
			}*/
		}
		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;
}