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<stdio.h>
int hideo[1000001] = { 0 };
int main(void)
{
	int  n, x, temp = 1,noyori=0,debu=0 , ruio[31] = { 0 };
	while (scanf("%d %d", &n, &x)) {
		if (n == 0 && x == 0) {
			return 0;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &ruio[i]);
			hideo[ruio[i]] = 1;
		}
		while (temp != x) {
			if (hideo[temp] == 1) {
				for (int j = 1; j <= n; j++) {
					if (temp + ruio[j] > x)continue;
					if (hideo[temp + ruio[j]] == 0) {
						hideo[temp + ruio[j]] = 1;
					}
				}
			}
			temp++;
		}
		for (int i = x; i >= 1; i--) {
			if (hideo[i] == 1) {
				for (int j = 2; j < i - 1; j++) {
					if (i%j == 0) {
						debu=1;
					}
				}
				if (debu == 0) {
					noyori = i;
					break;
				}
			}
			debu = 0;
		}
		if (noyori > 1 ) {
			printf("%d\n", noyori);
		}
		else {
			printf("NA\n");
		}
		temp = 1;
		noyori = 0;
		debu = 0;
		for (int i = 1; i <= n; i++) {
			ruio[i] = 0;
		}
		for (int i = 1; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}

C++: #include <iostream>
#define MAX 1000001

using namespace std;

bool dp[MAX];
int n,est;
int value[31];


int isPrime(int N){

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

}

void solve(void){

  dp[0] = true;
  for( int i = 0 ; i < n ; i++ ){
    for( int j = value[i] ; j <= est ; j++ ){
      if( dp[j - value[i]] ) dp[j] = true;
    }
  }
}

int main(void){


  while( cin >> n >> est , n + est ){

    fill( dp , dp + MAX , false );

    for( int i = 0 ; i < n ; i++ )
      cin >> value[i];

    solve();

    int ans = 0;
    for( int i = est ; i >= 2 ; i-- ){
      if( dp[i] ){
	ans = isPrime(i);
	if( ans ) break;
      }
    }
    if( ans ) cout << ans << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool e[1000001],dp[1011110];
int main(){
  e[0]=e[1]=1;
  for(int i=2;i<1000001;i++)
    for(int j=i*2;j<1000001;j+=i)
      e[j]=1;
  int n,m;
  while(cin>>n>>m,n){
    for(int i=0;i<m+1110;i++)dp[i]=0;
    int a[n],c=1,p;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++)
      for(int j=1;j<=m;j++){
        if(dp[j]&&a[i]+j<=m)dp[a[i]+j]=1;
        if(a[i]*j<=m)dp[a[i]*j]=1;}
    for(p=m;p>=0;p--)if(dp[p]&&!e[p]){c=0;break;}
    if(c)cout<<"NA"<<endl;
  else cout<<p<<endl;
  }
}

C++: #include<iostream>
#include<string>

using namespace std;

bool dp[1000001];

bool isPrime(int x){
  int i, flag=0;
  for(i=2; i*i<=x; i++){
    if(x%i==0){
      flag += 1;
    }
  }
  if(flag==0){
    return true;
  }else{
    return false;
  }
}

int solve(int x){
  int temp = x;
  while(1){
    if(dp[temp] && isPrime(temp)){
      return temp;
    }
    temp--;
  }
}
int main(){
  int dish, budget, dishPrice[30], i, j;
  while(cin >> dish >> budget, dish+budget){
    for(i=1; i<=budget; i++){
      dp[i] = false;
    }
    dp[0] = true;
    for(i=0; i<dish; i++){
      cin >> dishPrice[i];
      for(j=dishPrice[i]; j<=budget; j++){
        if(dp[j-dishPrice[i]]){
          dp[j] = true;
        }
      }
    }
    int ans = solve(budget);
    if(ans){
      cout << ans << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

C++: #include <iostream>

using namespace std;

bool memo[1000001];
int n, x, m, price[30];

bool sosuu(int k) { 
	if (k == 2) return true;
	else if (k < 2) return false;
	else if (k % 2 == 0) return false;
	for (int i = 3; i*i<=k; i += 2)
		if (k % i == 0) return false;
	return true;
}
int next(){
	memo[0] = true;
	for(int i = 0 ; i < n ; i++){
		for(int j = price[i] ; j <= x ; j++)
			memo[j] |= memo[j-price[i]];
	}
}

int main() {
	while (cin >> n >> x, n || x) {
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		for (int i = 0; i < 1000001; ++i) memo[i] = 0;
		m = 0;
		memo[0] = true;
		for(int i = 0 ; i < n ; i++)
			for(int j = price[i] ; j <= x ; j++)
				memo[j] |= memo[j-price[i]];
		
		for (int i = x; i; --i)
			if (memo[i] && sosuu(i)) {
				m = i;
				break;
			}

		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
	return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
int n;//料理の種類
int x;
int sum;
bool prime[1000001];
bool check[1000001];
int main()
{

  for(int i = 0;i <1000001;i++){
    prime[i] = true;
    check[i] = false;
  }
  prime[0] = false;
  prime[1] = false;
  for(int i = 2;i <1000001;i++){
    if(prime[i]){
      for(int j = 2*i;j <1000001;j+=i){
	prime[j]= false;
      }
    }
  }
  while(cin>>n>>x,n||x){
    bool flag1=false;
    int dish[40];
   
    for(int i = 0;i < n;i++){
      cin>>dish[i];
    }
    check[0] =true;//0はどの料理からでも作れる
    for(int i = 0;i <=x;i++){
      if(check[i]){
	for(int j = 0;j <n;j++){
	  if(i + dish[j]<=x)
	    check[i+dish[j]] = true;
	}
      }
    }
    int ans = 0;

    for(int i =x;i >0;i--){//上から調べる
      if(check[i]&&prime[i]){
	flag1 = true;
	ans = i;
	break;
      }
    }
    if(flag1)
      cout <<ans<<endl;
    else
      cout <<"NA"<<endl;
  }

  return 0;
}

C++: #include <stdio.h>

int ans = -1;
char visited[1000001], isprime[1000001];
int price[30];
int n, x;

void eratos(int m)
{
	int i, j;
	for(i=2; i<=m; ++i)
		isprime[i] = 1;
	isprime[0] = isprime[1] = 0;
	
	for(i=2; i*i<=m; ++i){
		if(isprime[i]){
			for(j=i*2; j<=m; j+=i)
				isprime[j] = 0;
		}
	}
}

void search(int c)
{
	int i;
	if(visited[c])
		return;
	visited[c] = 1;
	
	if(ans < c && isprime[c])
		ans = c;
	
	for(i=0; i<n; ++i){
		if(c+price[i] <= x)
			search(c+price[i]);
	}
}

int main(void)
{
	eratos(1000000);
	while(scanf("%d%d", &n, &x), n){
		int i;
		for(i=0; i<n; ++i)
			scanf("%d", &price[i]);
		
		ans = -1;
		for(i=0; i<=x; ++i)
			visited[i] = 0;

		search(0);
		if(ans == -1)
			puts("NA");
		else
			printf("%d\n", ans);
	}
	return 0;
}