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<iostream>
using namespace std;
#define N 1000001


int main(){
  bool is_prime[N];
  for(int i=0;i<N;i++)
    is_prime[i] = false;

  for(int i=3;i<N;i+=2) is_prime[i] = true;
  is_prime[2] = true;
  for(int i=3;i<=1000;i+=2){
    if(is_prime[i]){
      for(int j=2*i;j<N;j+=i) is_prime[j] = false;
    }
  }

  int n,x;
  while(cin >> n >> x){
    if(n==0&&x==0) break;
    int v;
    bool dp[x+1];
    for(int i=0;i<=x;i++)
      dp[i] = false;

    for(int i=0;i<n;i++){
      cin >> v;
      dp[v] = true;
      for(int j=0;j<=x-v;j++){
        if(dp[j]) dp[j+v] = true;
      }
    }
    for(int i=x;i>=0;i--){
      if(i==0) cout << "NA" << endl;
      if(dp[i]&&is_prime[i]){
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

C++: #include <stdio.h>

int n,m[50];
bool prime[1000000],c[1000000];

void saiki(int total){
	int i;
	
	c[total]=1;
	
	for( i=total ; i>=0 ; i--){
		if(c[i]==1){
			for(int j=0;j<n;j++){
				if(i-m[j]>=0){
					c[i-m[j]]=1;
				}
			}
		}
	}
}


int main(void)
{
	int i,j;
	//f
	prime[0]=1;
	prime[1]=1;
	for(i=3;i<1000000;i++){
		if(i%2==0){
			prime[i]=1;
		}
	}
	for(i=3;i<1000000;i+=2){
		if(prime[i]==0){
			for(j=(i+i);j<1000000;j+=i){
				prime[j]=1;
			}
		}
	}
	//
	while(1){
		int x;
		int flg=0;
		scanf("%d%d",&n,&x);
		if(n==0)break;
		
		for(i=0;i<n;i++){
			scanf("%d",&m[i]);
		}
		saiki(x);
		//
		for(i=0;i<x;i++){
			if(c[i]==1 && prime[x-i]==0){
				flg=1;
				break;
			}
		}
		if(flg==1){
			printf("%d\n",x-i);
		}
		else puts("NA");
		for(i=0;i<1000000;i++){
			c[i]=0;
		}
		for(i=0;i<50;i++){
			m[i]=0;
		}
		//
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX_N 1000000

int prime[MAX_N];
bool is_prime[MAX_N + 1];

int main(){

  //素数判定
  int p = 0;
  for(int i=0;i<=MAX_N;i++) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for(int i=2;i<=MAX_N;i++){
    if(is_prime[i]){
      prime[p++] = i;
      for(int j=2*i;j<=MAX_N;j+=i) is_prime[j] = false;
    }
  }

  int n,x;
  while(cin >> n >> x){
    if(n==0&&x==0) break;
    int v[n];
    bool dp[x+1];
    for(int i=0;i<=x;i++)
      dp[i] = false;

    for(int i=0;i<n;i++)
      cin >> v[i];
    dp[v[0]] = true;

    for(int i=0;i<n;i++){
      for(int j=0;j<=x-v[i];j++){
        if(dp[j]) dp[j+v[i]] = true;
      }
    }
    for(int i=x;i>=0;i--){
      if(i==0) cout << "NA" << endl;
      if(dp[i]&&is_prime[i]){
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e10,EPS=1e-9;

int coin[]={500,100,50,10,5,1};
int p,n[6];

int pay(int a)
{
	int ret=0;
	rep(i,6)
	{
		int t=min(n[i],a/coin[i]);
		a-=coin[i]*t; ret+=t;
	}
	return a?inf:ret;
}
int change(int a)
{
	int ret=0;
	rep(i,6)ret+=a/coin[i],a%=coin[i];
	return ret;
}

int main()
{
	while(cin>>p,p)
	{
		rep(i,6)cin>>n[5-i];
		int ans=inf;
		for(int i=p;i<=p+1000;i++)ans=min(ans,pay(i)+change(i-p));
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <cmath>

using namespace std;

vector<int> price;
list<int> primes;
int memo[1000000][30];

void mk_primes(int x)
{
	for( int i = 3; i <= x; i+=2 ) {
		for( int j = 3; j <= sqrt(i); j++ ) {
			if( i % j == 0 ) {
				goto NEXT;
			}
		}
		primes.push_front(i);
	NEXT:;		
	}
}

int solve(int x, int pos)
{
	int& result = memo[x][pos];

	if( -1 < result )
		return result;

	result = 0;

	int p = price[pos++];
	int s = x / p;

	if( price.size() == pos ) {

		if( s*p == x ) {
			result = 1;
			// cout << "(" << p << "*" << s << ")";
		}
		
		
	} else {

		for( int i = 0; i <= s; i++ ) {

			int t = i * p;
			result = solve(x-t, pos);
			if( result ) {
				// cout << "(" << p << "*" << i << ")";
				break;
			}
		}
	}

	return result;
}

int main(int argc, char* argv[])
{
	while(1) {

		int n; cin >> n;
		int x; cin >> x;

		if( n == 0 && x == 0)
			break;
	
		for( int i = 0; i < n; i++ ) {
			int p; cin >> p;
			price.push_back(p);
		}

		mk_primes(x);

		for( int i = 0; i < 1000000; i++ ) {
			for( int j = 0; j < 30; j++ ) {			
				memo[i][j] = -1;
			}
		}

		for( auto p : primes ) {
			if( solve(p, 0) ) {
				cout << p << endl;
				goto LAST;
			}
		}

		cout << "NA" << endl;

	LAST:;

		price.clear();

	}

    return 0;
}

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

using namespace std;

int n,x;
int v[30];
//vector<int> prime;
bool is_prime[1000001];

void sieve(int n){
  //  int p = 0;
  for(int i = 0;i<=n;i++){
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;
  for(int i = 2;i<=n;i++){
    if(is_prime[i]){
      for(int j = 2*i;j<=n;j+=i)
	is_prime[j] = false;
    }
  }
}
  
  

int main(){
  sieve(1000000);
  while(cin>>n>>x){
    if(n == 0 && x == 0)
      return 0;
    bool can[1000001];
    for(int i = 0;i<=x;i++)
      can[i] = false;
    for(int i = 0;i<n;i++){
      cin>>v[i];
    }
    sort(v,v+n);
    for(int i = 0;i<n;i++){
      can[v[i]]=true;
      for(int j = 0;v[i]+j<=x;j++){
	if(can[j])
	  can[j+v[i]] = true;
      }
    }
    int ans = 0;
    for(int i = 0;i<=x;i++){
      if(can[i] && is_prime[i])
	ans = i;
    }
    if(ans != 0)
      cout<<ans<<endl;
    else
      cout<<"NA"<<endl;
  }


  return 0;
}

C++: #include <stdio.h>
#include <map>

using namespace std;


int *ms;
int n, m;

bool *dp;
bool *data;

bool search(int m){
	if(m == 0) return true;
	else if(m < 0) return false;
	else if(dp[m]) return data[m];

	for(int i=0;i<n;i++){
		if(search(m-ms[i])){
			return dp[m] = data[m] = true; 
		}
	}
	
	dp[m] = true;
	return data[m]=false;
}

int isprime(int p){
	if(p%2==0) return 0;
	for(int i=3;i*i<p;i++){
		if(p%i==0) return 0;
	}
	return 1;
}

int main(){
	while(1){
		scanf("%d%d", &n,&m);
		
		if(n==0) break;
		
		int i;
		dp = new bool[m+1];
		data = new bool[m+1];
		for(i=0;i<=m;i++) dp[i] = false;
		
		ms = new int[n];
		for(i=0;i<n;i++) scanf("%d", ms+i);
		
		int flag = 1;
		for(i=m;i>=0;i--){
			if(search(i) && isprime(i)){
				printf("%d\n", i);
				flag = 0;
				break;
			}
		}
		if(flag) puts("NA");
		
		delete dp;
		delete data;
	}
}