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>
#include<algorithm>
using namespace std;
#define MAX 1000000

bool prime[MAX];
bool order[MAX+1];
int menu[30];
int n,money;

void init(){
	fill(prime,prime+MAX,true);
	prime[0]=prime[1]=false;
	for(int i=2;i<MAX;i++){
		if(prime[i]){
			for(int j=2;j*i<MAX;j++)
				prime[j*i]=false;
		}
	}
}
void search(int sum){
	if(money<sum || order[sum])return;
	order[sum]=true;
	for(int i=0;i<n;i++)search(sum+menu[i]);
}
int main(){
	init();
	while(cin>>n>>money,n|money){
		for(int i=0;i<n;i++)cin>>menu[i];
		fill(order,order+(MAX+1),false);
		search(0);
		int ans=-1;
		for(int i=money;i>1;i--){
			if(prime[i] && order[i]){
				ans=i;
				break;
			}
		}
		if(ans!=-1)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
}

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

int n;
int x;
int d[100];
bool p[1000011];
bool memo[1000011];

int solve(int a,int k){
  
  if(a > x) return 0;
  if(memo[a]) return memo[a];
  
  memo[a] = true;
  
  for(int i=k;i<n;i++) solve(a+d[i],k);
  
  return memo[a];
}

int main(){
  
  memset(p,true,sizeof(p));
  p[0] = false;
  for(int i=2;i*i<100011;i++){
    if(p[i]) for(int j=i+i;j<1000011;j+=i) p[j] = false;
  }
  
  while(true){
    cin >> n >> x;
    if(!n && !x) break;
    
    memset(memo,false,sizeof(memo));
    for(int i=0;i<n;i++) cin >> d[i];
    
    solve(0,0);
    
    int ans = -1;
    for(int i=0;i<=x;i++){
      if(memo[i] && p[i]) ans = i;
    }

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

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

bool p[1000001];

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

int main(){
    int n, x;
    sieve();

    while(1){
        cin>>n>>x;
        if(!n && !x)break;
        bool dp[1000001]= {false};
        int dish[31];
        for(int i=0;i<n;i++){
            cin>>dish[i];
            dp[dish[i]] = true;
        }
    
        for(int i=0;i<n;i++){
            for(int j=0;j<=1000000;j++){
                if(dp[j] && j+dish[i] <= 1000000)
                    dp[j+dish[i]] = true;
            }
        }
    
        int max=0;
        for(int i=0;i<=x;i++){
            if(dp[i] && p[i])max = i;
        }

        if(max) cout<<max<<endl;
        else cout<<"NA"<<endl;
    }

    return 0;
}

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

int prime[MAX_N+1];
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>

using namespace std;

vector<int> budget;
int price[30];
int max_budget;
int n;

void serch(int s_price)
{
	if(s_price>max_budget)
		return ;
	
	if(budget[s_price]==1)
	{
		budget[s_price]=2;
//		cout << "s_prime:" << s_price << endl;
	}
	else
		budget[s_price]=3;
	
	for(int i=0;i<n;i++)
	{
		if(budget[s_price+price[i]]==0 || budget[s_price+price[i]]==1)
			serch(s_price+price[i]);
//		else
//			return ;
	}
}

int main()
{
	int i;
	
	for(;;)
	{
		cin >> n >> max_budget ;
		if(n==0 && max_budget==0)
			break;
		
		int ans=0;
		budget.clear();
		
		for(i=0;i<1000001;i++)
			budget.push_back(1);
		
		budget[0]=budget[1]=0;
		
		for(i=0;i<1000001;i++)
		{
			if(budget[i]==1)
			{
				for(int j=2;i*j<1000001;j++)
				{
					budget[i*j]=0;
				}
			}
		}
		
/*		for(i=0;i<1000001;i++)
		{
			if(budget[i]==1)
				cout << "prime:" << i << endl;
		}
*/		
		for(i=0;i<n;i++)
			cin >> price[i] ;
		
		for(i=0;i<n;i++)
			serch(price[i]);
		
		for(i=max_budget;i>=0;i--)
		{
			if(budget[i]==2)
			{
				ans=1;
				cout << i << endl;
				break;
			}
		}
		
		if(ans==0)
			cout << "NA" << endl;
	}
}

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

int prime[MAX_N+1];
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

C++: #include<iostream>
using namespace std;
bool p[1000001] = { false };
void prime();
int main()
{
	prime();
while (true){
	bool dp[1000001] = { false };
	int a[31];
	int n, v;
	cin >> n >> v;
	if (n == 0){
		break;
	}
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	dp[0] = true;
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 <= v - a[i]; i2++){
			if (dp[i2]){
				dp[i2 + a[i]] = true;
			}
		}
	}

	bool flag = true;
	for (int i = 1000000; i > 0; i--){
		if (dp[i] && !p[i]){
			flag = false;
			cout << i << endl;
			break;
		}
	}
	if (flag)
		cout << "NA" << endl;
}

}
void prime(){
	for (int i = 2; i <= 100000; i++){
		for (int i2 = 2; i2*i <= 100000; i2++){
			if (!p[i2*i]){
				p[i2*i] = true;
			}
		}
	}
}