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 <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int XMAX = 1000000;
const int SQRTXMAX = sqrt(XMAX);

bool is_prime[XMAX + 1];
bool dp[XMAX + 1];
int cost[30];

int main() {
  // prime
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false
  for (int i = 2; i <= SQRTXMAX) {
    if (is_prime[i]) {
      int current = i * i;
      for (int j = i * i; j <= XMAX; j += i) {
        is_prime[j] = false;
      }
    }
  }

  int n, x;
  while (cin >> n >> x, n) {
    for (int i = 0; i < n; i++) cin >> cost[i];

    // dp
    r = 0;
    dp[0] = true;
    for (int i = 1; i <= x; i++) {
      for (int j = 0; j < n; j++) {
        int k = i - cost[j];
        if (k >= 0 && dp[k]) {
          dp[i] = true;
          if (is_prime[i]) r = i;
          break;
        }
      }
    }

    if (r == 0) {
      cout << "NA" << endl;
    } else {
      cout << r << endl;
    }
  }
  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();
   }
   return 0;
}

void initialize(){
    memset(is_prime_number, true, MAX_X);
    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 <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;

const int MAX_N = 1000000;
bool prime[MAX_N+1];

void make_prime (void )
{
	fill (prime, prime + MAX_N, true );

	prime[0] = false;
	prime[1] = false;

	for (int i = 2; i < MAX_N + 1; ++i ){
		if (prime[i] ){
			for (int j = 2*i; j < MAX_N + 1; j += i ){
				prime[j] = false;
			} // end for
		} // end if
	} // end for
}

bool dp[30][MAX_N+1];

int main()
{
//	cut here before submit 
//	freopen ("testcase.be", "r", stdin );
	int n, x;

	make_prime ();

	while (cin >> n >> x && n && x ){
		vector <int> cost (n, 0 );
		for (int i = 0; i < n; ++i ){
			cin >> cost[i];
		} // end for
		memset (dp, false , sizeof (dp ) );

		for (int j = cost[0]; j <= x; j += cost[0] )
			dp[0][j] = true;

		for (int i = 1; i < n; ++i ){
			for (int j = 0; j <= x; ++j ){
				if (j < cost[i] ){
					dp[i][j] = dp[i-1][j];
				}else{
					dp[i][j] = dp[i-1][j] | dp[i][j - cost[i]];
				} // end if
			} // end for
		} // end for 

		int res = 0;
		for (int j = MAX_N; j >= 0; --j ){
			if (dp[n-1][j] && prime[j] ){
				res = j;
				break;
			} // end if
		} // end for

		if (res == 0 ){
			cout << "NA" << endl;
		}else{
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))
#define X 1000001
int ans;
int n,x,v[30];
bool p[X];
char dp[31][X];

int main(){
	p[0]=1;
	reps(i,2,X)if(!p[i])for(int j=i+i;j<X;j+=i)p[j]=1;
	for(;;){
		scanf("%d%d",&n,&x);
		if(!n)break;
		x++;
		rep(i,n){
			scanf("%d",v+i);
		}
		fill_n(dp[0],31*X,0);
		dp[0][0]=1;
		rep(i,n){
			rep(j,x){
				if(dp[i][j])for(int k=0;j+k*v[i]<x;k++){
					dp[i+1][j+k*v[i]] = 1;
				}
			}
		}
		ans=0;
		rep(j,x){
			if(dp[n][j] && !p[j])ans = max(ans,j);
		}
		if(ans)printf("%d\n",ans);
		else puts("NA");

	}
	return 0;
}

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


int main()
{
	vector<bool> totalValue;
	totalValue.reserve(1000000);

	while(1)
	{
		
		for(int i = 0; i < 10000001; i++)
		{
			totalValue[i] = false;
		}
		totalValue[0] = true;
		int n;
		int x;
		cin >> n >> x;
		if(n == 0 && x == 0)
		{
			break;
		}
		for(int i = 0; i < n; i++)
		{
			int value;
			cin >> value;

			for(int j = 0; j <= x; j++)
			{
				if(totalValue[j] == true && j + value <= x + 1)
				{
					totalValue[j + value] = true;
				}
			}
		}
		bool NA = true;
		for(int i = x; i >= 0; i--)
		{
			if(totalValue[i] == false)
			{
				cout << i << endl;
				NA = false;
				break;
			}
		}
		if(NA)
		{
			cout << "NA" << endl;
		}


	}


}