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>
using namespace std;
#define sosu 1000001
bool so[sosu];
int n,x,c[31];
bool his[sosu];
int ans;

void hantei() {
  so[0] = so[1] = 1;
  for(int i=2;i*i<sosu;i++) 
    if(so[i] == 0) 
      for(int j=2;j<sosu/i;j++)so[i*j] = 1;
}


void saiki(int sum) {
  his[sum] = 1;
  if(so[sum] == 0 && ans < sum) ans = sum;
  
  for(int i=0;i<n;i++) 
    if(c[i] + sum <= x && his[c[i]+sum] == 0) 
      saiki(c[i]+sum);
}


int main() {
  hantei();

  while(1){
    cin >> n >> x;
    if(n == 0 && x == 0) break;
    
    for(int i=0;i<n;i++) cin >> c[i];
    for(int i=0;i<=x;i++) his[i] = 0;
    
    ans = -1;
    saiki(0);
    
    if(ans == -1) cout <<"NA"<<endl;
    else cout << ans << endl;
    
  }
  
  return 0;
}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[1000001];
int flg[1000001];
int main(void){
	for(i=1;i<=1000000;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(j=2;j*i<=1000000;j++){
				flg[i*j]=0;
			}
		}
	}
	scanf("%d %d",&n,&x);
	while(n!=0 && x!=0){
		for(j=0;j<=x;j++){
			if(j>0){
				dp[j]=-1;
			}
			else if(j==0){
				dp[j]=0;
			}
		}
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[j]!=-1){
					dp[j+kin]=1;
				}
			}
			/*for(j=0;j<=x;j++){
				dp[i+1][j]=dp[i][j];
			}*/
		}
		int max=0;
		for(i=0;i<=x;i++){
			if(dp[i]==1 && flg[i]==1){
				max=i;
			}
		}
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

C++: // 0202_AtBosssExpense.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int MAX_X = 1000001;
const int MAX_N =30;
bool prime[MAX_X], dp[MAX_X];
int v[MAX_N];
int n, x;
void table() {
  fill(prime,prime+MAX_X,1);

  for(int i=2;i*i<MAX_X;++i)
    if(prime[i])
      for(int j=i*i;j<MAX_X;j+=i)
        prime[j]=0;
}
void solve() {
  fill(dp,dp+MAX_X,0);
  dp[0]=1;
  for(int i=0;i<=x;++i)
    for(int j=0;j<n;++j)
      if(v[j]<=i&&dp[i-v[j]]) dp[i]=1;

  for(int i=x;i>0;--i) {
    if(prime[i]&&dp[i]) {
      cout << i << endl;
      return;
    }
  }
  cout << "NA" << endl;
}
int main() {
  table();
  while(cin >> n >> x && n && x) {
    for(int i=0;i<n;++i) cin >> v[i];
    solve();
  }
  return 0;
}

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

constexpr int MAX_N = 30, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
int dp[MAX_C];
bool prime_num[MAX_C];

int main() {
	while (1) {
		std::cin >> n >> max;
		if (n + max == 0)
			break;
		std::memset(prime_num, false, sizeof(prime_num));
		std::memset(dp, 0, sizeof(dp));
		for (int i = 0; i < n; ++i)
			std::cin >> cost[i];
		for (int i = 2; i * i <= max; ++i) {
			if (!prime_num[i]) {
				for (int j = 2; i*j <= max; ++j)
					prime_num[i*(j + 2)] = true;
			}
		}
		for (int i = 0; i < n; ++i)
			dp[cost[i]] = cost[i];
		int ans = 0;
		for (int i = 1; i <= max; ++i) {
			dp[i] = dp[i - 1];
			for (int j = 0; j < n; ++j) {
				if (i > cost[j])
					dp[i] = std::max(dp[i], dp[i - cost[j]] + cost[j]);
			}
			if (!prime_num[dp[i]])
				ans = dp[i];
		}
		if (!prime_num[max])
			ans = max;
		if (!ans)
			std::cout << "NA" << std::endl;
		else
			std::cout << ans << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
typedef long long ll;

int n, x;
int ps[40];

bool isp[1000010];
void sieve(){
    const int N = 1000000;
    memset(isp, true, sizeof(isp));
    
    isp[0] = false;
    isp[1] = true;
    
    for(int p=2;p*p<=N;p++)if(isp[p]){
        for(int j=p*p;j<=N;j+=p)isp[j] = false;
    }
}

bool can[1000010];
void gen(){
    memset(can, false, sizeof(can));
    can[0] = true;
    
    for(int sum=0;sum<x;sum++){
        for(int i=0;i<n;i++){
            if(sum + ps[i] <= x){
                can[sum + ps[i]] |= can[sum];
            }
        }
    }
}


int main(){
    sieve();
    
    while(cin >> n >> x){
        if(n == 0 && x == 0)break;
        
        for(int i=0;i<n;i++)cin >> ps[i];
        gen();
    
        int res = -1;
        for(int sum=x;sum>=2;sum--)if(can[sum] && isp[sum]){
            res = sum;
            break;
        }
        
        if(res == -1){
            cout << "NA" << endl;
        } else {
            cout << res << endl;
        }
    }
    
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
long long int a,s,d[1000001],f,g[10000001],h[1000001];
int main(void)
{
	int j,i,k;
	for(i=0;i<1000001;i++){
		h[i]=0;
		g[i]=0;
	}
	for(i=2;i*i<=1000000;i++){
		if(h[i]==0){
			for(j=2;j<=500000;j++){
				if(i*j>1000000) break;
				h[i*j]=1;
			}
		}
	}
	while(1){
		scanf("%d %d",&a,&s);
		f=0;
		if(a==0&&s==0) break;
		for(i=0;i<1000001;i++) g[i]=0;
		for(i=0;i<a;i++){
			scanf("%d",&d[i]);
			g[d[i]]=1;
		}
		for(i=0;i<=s;i++){
			if(g[i]==1){
				for(j=0;j<a;j++){
					if(i+d[j]<=s) g[i+d[j]]=1;
				}
			}
		}
		for(i=s;i>1;i--){
			if(h[i]==0&&g[i]==1){
				f=i;
				break;
			}
		}
		if(f==0) printf("NA\n");
		else printf("%d\n",f);
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

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

bool sosuu(int k) {
	if (k == 2) return true;
	else if (k % 2 == 0) return false;
	for (int i = 3; i*i<=k; i += 2)
		if (k % i == 0) return false;
	return true;
}
void next(int p, int k) {
	if (flag[k]) return;
	flag[k] = true;
	if (k > x) return;
	if (k > m) if (sosuu(k)) m = k;
	if (p == n) return;
	for (int i = 0; k + price[p] * i <= x; ++i)
		next(p + 1, k + price[p] * i);
}

int main() {
	while (cin >> n >> x, n || x) {
		for (int i = 0; i < 1000001; ++i) flag[i] = false;
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		m = 0;
		next(0, 0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include <cmath>
#define MAX 1000000

using namespace std;

bool prime[MAX+1];

void eratos(){
  for(int i=0;i<=MAX;i++) prime[i]=false;
  for(int i=3;i<=MAX;i+=2) prime[i]=true;
  prime[2]=true;
  int limit=(int)sqrt((double)MAX)+1;
  for(int i=3;i<=limit;i+=2){
    if(!prime[i]) continue;
    for(int j=i+i;j<=MAX;j+=i){
      prime[j]=false;
    }
  }
}

main(){
  eratos();
  int n, x;
  while(1){
    cin >> n >> x;
    if(n==0 && x==0) break;

    int items[30];
    for(int i=0;i<n;i++){
      cin >> items[i];
    }

    int max=-1;

    bool can[MAX+1];
    for(int i=0;i<=x;i++) can[i]=false;
    can[0]=true;
    for(int i=0;i<=x;i++){
      if(can[i]){
	int tmp;
	for(int j=0;j<n;j++){
	  tmp=i+items[j];
	  if(tmp<=x){
	    can[tmp]=true;
	  }
	}
      }
    }
    for(int i=x;i>=0;i--){
      if(i==0) cout << "NA" << endl;
      if(can[i] && prime[i]){
	cout << i << endl;
	break;
      }
    }
  }
  return 0;
}