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>
#include <cmath>
#include <algorithm>
#include <stack>

#define NUM 1100000

using namespace std;

int table[NUM];

void func(int n,int budget){

	int tmp,max_prime = 0,food[n],index = 0;

	bool checkTable[budget+1],FLG;

	for(int i = 0; i < budget+1;i++)checkTable[i] = false;

	checkTable[0] = true;

	for(int i = 0; i < n; i++){
		scanf("%d",&tmp);
		FLG = true;

		for(int loop = 0; loop < index;loop++){
			if(food[loop] == tmp){
				FLG = false;
				break;
			}
		}

		if(FLG == true && tmp <= budget){
			food[index++] = tmp;

			for(int k = tmp; k <= budget; k++){
				if(checkTable[k-tmp] == true){
					checkTable[k] = true;
					if(table[k] == 1){
						max_prime = max(max_prime,k);
					}
				}
			}
		}
	}

	if(max_prime == 0)printf("NA\n");
	else{
		printf("%d\n",max_prime);
	}

}

int main(){
	int limit;

	for(int i=0; i < NUM;i++)table[i] = 1;
	table[0] = 0;
	table[1] = 0;

	limit = sqrt(NUM);

	for(int i=2;i<=limit;i++){
		if(table[i] == 1){
			for(int k=2*i;k < NUM; k += i){
				table[k] = 0;
			}
		}
	}

	int n,budget;

	while(true){
		scanf("%d %d",&n,&budget);
		if(n == 0 && budget == 0)break;

		func(n,budget);
	}
	return 0;
}

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

#define MAX 1000100

bool p[MAX];

void eratos(){
  memset(p, true, sizeof(p));
  p[0] = p[1] = false;
  for(int i = 0 ; i * i < MAX ; i++){
    if(p[i]){
      for(int j = 2*i ; j < MAX ; j += i){
	p[j] = false;
      }
    }
  }
}

int main(){
  eratos();
  int n, x;
  while(cin >> n >> x){
    if(n == 0 && x == 0) break;
    vector<int> v(n);
    for(int i = 0 ; i < n ; i++){
      cin >> v[i];
    }
    
    bool budget[MAX];
    memset(budget, false, sizeof(budget));
    budget[0] = true;
    
    for(int i = 0 ; i < x ; i++){
      if(budget[i]){
	for(int j = 0 ; j < n ; j++){
	  if(i + v[j] <= x) budget[i+v[j]] = true;
	}
      }
    }
    
    bool flag = true;
    
    for(int i = x ; i >= 0 ; i--){
      if(budget[i] && p[i]){
	flag = false;
	cout << i << endl;
	break;
      }
    }
    if(flag) 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;
int dish[30];
int prime[1000001];
int check[1000001];//その数字が料理の組み合わせから作れるかどうかの判定
int main()
{
  memset(prime, 1, 1000001);
  prime[0] = 0;
  prime[1] = 0;
  for(int i = 2;i <1000001;i++){
    if(prime[i]){
      for(int j = 2*i;j <1000001;j+=i){
	prime[j]= 0;
      }
    }
  }
  while(cin>>n>>x,n||x){
    bool flag1=false;
    for(int i = 0;i < n;i++){
      cin>>dish[i];
    }
    memset(check, 0, 1000001);
    check[0] =1;//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]] = 1;
	}
      }
    }
    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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    vector<int> primes;
    primes.PB(2);
    for(int i=3;i<=1000001;i++){
	bool flg = true;
	for(int j=0;primes[j]*primes[j]<=i;j++){
	    if(i % primes[j] == 0) flg = false;
	}
	if(flg) primes.PB(i);
    }
    while(true){
	int ans = 0;
	int n,x;
	cin >> n >> x;
	if(n==0&&x==0) break;
	bool d[1000001] = {false};
	d[0] = true;
	REP(i,n){
	    int a;
	    cin >> a;
	    for(int i=0;i+a<=1e6;i++){
		if(d[i])
		    d[i+a] = d[i];
	    }
	}
	int s = upper_bound(ALL(primes),x) - primes.begin();
	RREP(i,s){
	    if(d[primes[i]]){
		ans = primes[i];
		break;
	    }
	}
	if(ans==0) cout << "NA" << endl;
	else cout << ans << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

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

const int INF = 1001001001;

int main()
{
	int n;
	while(scanf("%d", &n), n){
		int x;
		scanf("%d", &x);
		
		int p[30];
		rep(i, n)
			scanf("%d", &p[i]);
		
		bool dp[1000001] = {false};
		dp[0] = true;
		reps(i, 1, x+1){
			rep(j, n)
				dp[i] |= i>=p[j] && dp[i-p[j]];
		}
		
		bool isprime[1000001];
		fill(isprime, isprime+x+1, true);
		isprime[0] = isprime[1] = false;
		reps(i, 2, x){
			if(isprime[i]){
				for(int j=i*2; j<=x; j+=i)
					isprime[j] = false;
			}
		}
		
		int ans = -1;
		for(int i=x+1; i>=0; --i){
			if(dp[i] && isprime[i]){
				ans = i;
				break;
			}
		}
		if(ans == -1)
			puts("NA");
		else
			printf("%d\n", ans);
	}
	
	return 0;
}