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 <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

vector<int> eratosthenes(int n) {
  vector<int> v(n);
  for (int i=2; i<n; i++) v[i] = i;
  for (int i=2; i*i<n; i++)
    if (v[i])
      for (int j=i*i; j<n; j+=i)
        v[j] = 0;
//  v.erase(remove(ALL(v),0),v.end()); // ツつアツつアツづーツづつつッツづェツづ篠素ツ青板δ環スツト
  return v;
}

int main() {
  vector<int> prime =  eratosthenes(1000000);
  int n,x;
  while(cin >> n >> x, n||x) {
    int a[n];
    REP(i,n) {
      cin >> a[i];
    }
    bool dp[x+1];
    memset(dp,0,sizeof(dp));
    dp[0] = true;
    for (int i=1; i<=x; ++i) {
      REP(j,n) {
        if (i-a[j] >= 0 &&dp[i-a[j]]) {
          dp[i] = true;
          break;
        }
      }
    }
    int i;
    for (i = x; i>=0; --i) {
      if (prime[i] &&dp[i]) {
        cout << i << endl;
        break;
      }
    }
    if (i<0) {
      cout << "NA" << endl;
    }
  }
}

C++: #include<iostream>
#include <cstdio>
#include<string>
#include<math.h>
using namespace std;
//int ha(string);
//int saga(int);
int q,m,n=1000000,List[1000];
bool  slist[1000001];
bool  klist[1000001];

int main()
{	
	int i,j,k,b=0,s,a=2,f=0,c=0;
	for( i = 0; i <n;i++ ){
		slist[i]=true;
		klist[i]=false;
	}

	i=0;
	while(i<sqrt((double)n)){
		for(i=a+a;i<n;i=i+a){
			slist[i]=false;
		}
		for(i=a+1;i<sqrt((double)n);i++){
			if(slist[i]==true){
				a=i;
				break;
			}
		}
	}
	cin>>m>>n;
	while(m!=0||n!=0){
	for( i = 0; i <=n;i++ ){
		klist[i]=false;
	}
	for(i=0;i<m;i++){
		cin>>List[i];
		klist[List[i]]=true;
	}
		for(i=0;i<=n;i++){
			if(klist[i]){
			for(j=0;j<m;j++){
				if(i+List[j]<n){
				klist[i+List[j]]=true;
				}
			}
		}
	}
		for(i=n;i>0;i--){
			if(klist[i]&&slist[i]){
			break;
		}
	}
		if(i==0){
			printf("NA\n");
		}
		else{
		printf("%d\n",i);
		}
		cin>>m>>n;
	}
	return 0;
}

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

int n,x,ans;
int t[30];
char dp[1000001];
char p[1000001];

void mkPrime(void){
	memset(p,1,sizeof(p));
	p[0] = p[1] = 0;

	for(int i=2;i<=1000000;i++){
		if(p[i]){
			for(int j=i*2;j<=1000000;j+=i){
				p[j] = 0;
			}
		}
	}
}

void solve(void){
	dp[0] = 1;
	for(int i=0;i<x;i++){
		if(dp[i]){
			for(int j=0;j<n;j++){
				int sum = i + t[j];
				if(sum <= x){
					dp[sum] = 1;
				}
			}
		}
	}

	for(int i=0;i<=x;i++){
		if(dp[i] && p[i] && i > ans) ans = i;
	}
}

int main(void){
	mkPrime();

	while(cin>>n>>x && (n||x)){
		for(int i=0;i<n;i++) cin>>t[i];

		memset(dp,0,sizeof(dp));
		ans = 0;

		solve();

		if(ans == 0) cout<<"NA\n";
		else cout<<ans<<endl;
	}

	return 0;
}

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
//typedef pair<int, int> P;
/* struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
}; */

/** Problem0202 : At Boss's Expense **/
const int MAX = 1000010;
int main()
{
	int N, P;
	bool isPrime[MAX];
	memset(isPrime, 1, sizeof(isPrime));
	isPrime[0]=false, isPrime[1]=false;
	for (int i=2; i<MAX; i++) {
		if (isPrime[i]) {
			for (int j=2; i*j<MAX; j++) {
				isPrime[i*j]=false;
			}
		}
	}
	
	while (cin>>N>>P, N||P) {
		vector<int> price(N);
		bool can[MAX]={0};
		
		rep(i, N) {
			cin>>price[i];
			can[price[i]] = true;
		}
		
		for (int i=0; i<N; i++) {
			for (int j=0; j<MAX; j++) {
				if (can[j] && j+price[i]<=MAX) {
					can[j+price[i]] = true;
				}
			}
		}
		
		int ans=0;
		for (int i=0; i<=P; i++) {
			if (can[i] && isPrime[i])
				ans = i;
		}
		
		if (ans>0) {
			cout << ans << endl;
		} else {
			cout << "NA" << endl;
		}
	}
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

bool isp(int n){
    if(n==2)return true;
    if(n==1)return false;
    if(n==0)return false;

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

int main(){
    int n,x;
    while(cin>>n>>x){
        if(n==0&&x==0)break;
        
        int data[40];
        rep(i,n){
            cin>>data[i];
        }
        
        bool dp[1000001]={};
        rep(i,1000001)dp[i]=false;
        
        dp[0]=true;
        

        for(int i=1;i<=n;i++){
            for(int j=0;j<=x;j++){
//                dp[i][j] = dp[i-1][j];

                dp[j] |= dp[j-data[i-1]];

            }
        }
        int ans = -1;
        for(int i=x;i>=0;i--){
            if(dp[i]==true){
                if(isp(i)){
                    ans=i;
                    break;
                }
            }
        }
        if(ans==-1)cout<<"NA"<<endl;
        else cout<<ans<<endl;
        
    }
}