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;

bool so[1000001];

int main(void)
{
	so[0] = so[1] = true;
	// 素数表 true = 素数じゃない
	for(int i = 2; i <= 1000000; i++){
		for(int j = i*2; j <= 1000000; j+=i){
			so[j] = true;
	
		}
	}

	int N,M;

	while(cin >> N >> M, N | M){
		bool d[1000001] = {};
		d[0] = true;

		int mo;
		for(int i = 0; i < N; i++){
			cin >> mo;

			for(int j = 0; j + mo <= M; j++)
				d[j+mo] |= d[j];

		}

		int max = 0;
		for(int i = 0; i <= M; i++)
			if(d[i] && !so[i] && max < i)
				max = i;
		if(max)
			cout << max << endl;
		else
			cout << "NA" << endl;
	}
	

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool e[1000001],dp[1000010];
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+3;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 <cstring>
using namespace std;

#define MAXNUM 1000001

bool prime[MAXNUM];

void eratosthenes()
{
	prime[0]=prime[1]=false;

	for(int i=2; i<MAXNUM; i++)
	{
		prime[i]=true;
	}
	
	for(int i=2; i*i<MAXNUM; i++)
	{
		if(prime[i])
		{
			for(int j=i*i; j<MAXNUM; j+=i)
			{
				prime[j]=false;
			}
		}
	}
}


bool dp[1000001];

int main()
{
	eratosthenes();
	int N,B,c[30];
	while(cin >> N >> B)
	{
		memset(dp,0,sizeof(dp));
		for(int i=0; i<N; i++)
			cin >> c[i];
			
		dp[0]=1;
		
		for(int i=0; i<N; i++)
		for(int j=0; j<B; j++)
		{
			if(j+c[i]>B) break;
			if(dp[j]) dp[j+c[i]]=1;
		}
		
		bool g=false;;
		for(int i=B; i>0; i--)
		{
			if(dp[i]&&prime[i])
			{
				g=true;
				cout << i << endl;
				break;
			}
		}
		if(!g) cout << "NA" << endl;
	}
}

C++: #include<iostream>

bool prime[1000001], dp[1000001];

int main(){
	for(int i=0;i<=1000000;i++){
		prime[i] = true;
	}
	prime[0] = prime[1] = 0;
	for(int i=2;i<=1000000;i++){
		if(prime[i]){
			for(int j=i*2;j<=1000000;j+=i)prime[j] = false;
		}
	}

	int n, x, a[30];
	while(std::cin >> n >> x, n){
		for(int i=0;i<=x;i++){
			dp[i] = false;
		}

		for(int i=0;i<n;i++){
			std::cin >> a[i];
		}

		int res = 0;

		dp[0] = true;
		for(int i=1;i<=x;i++){
			for(int j=0;j<n;j++){
				if(i-a[j] >= 0 && dp[i-a[j]]){
					dp[i] = true;
					break;
				}
			}
			if(dp[i] && prime[i]){
				res = i;
			}
		}

		if(res != 0){
			std::cout << res << std::endl;
		}else{
			std::cout << "NA" << std::endl;
		}
	}
}

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

bool prime[10000001];
bool dp[10000001];
int menu[30];
int n,x;

void make_prime()
{
  memset(prime,true,sizeof(prime));
  prime[0]=prime[1]=false;
  for(int i=2;i<10000001;i++)
    {
      if(prime[i])
	{
	  for(int j=i+i;j<10000001;j+=i)
	    prime[j]=false;
	}
    }
}

int main()
{
  make_prime();
  for(;cin>>n>>x,n;)
    {
      memset(dp,false,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  cin>>menu[i];
	  dp[menu[i]]=true;	  
	}
      int ans=-1;
      for(int i=0;i<=x;i++)
	{
	  if(dp[i])
	    for(int j=0;j<n;j++)
	      if(i+menu[j]<=x)
		dp[i+menu[j]]=true;	  
	}
      for(int i=x;i>0;i--)
	if(prime[i]&&dp[i])
	  {
	    ans=i;
	    break;
	  }
      if(ans!=-1)
	cout<<ans<<endl;
      else
	cout<<"NA"<<endl;
    }
}

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 = 1; k <= budget; k++){
				if((k%tmp == 0) || (k >= tmp && 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<stdio.h>
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
int p,cnt,cnt2,food;
int n,d=0,g,kakaku[31],flag,youso[1000001]={0};
int main()
{
	scanf("%d %d",&n,&p);
	while(n!=0&&p!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%d",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=0;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					if(cnt+kakaku[cnt2]<=p){
						youso[cnt+kakaku[cnt2]]=1;
					}
				}
			}
		}
		flag=0;
		for(cnt=p;cnt>=0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					flag=1;
					d=cnt;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		d=0;
		flag=0;
		for(cnt=0;cnt<=p;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %d",&n,&p);
	}
	return 0;
}

C++: #include<stdio.h>
	long p,cnt,cnt2,food;
	int n,d=0,g,kakaku[31],flag,youso[1000001]={0};
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
int main()
{
	scanf("%d %ld",&n,&p);
	while(n!=0&&p!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%ld",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=1;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					youso[cnt+kakaku[cnt2]]=1;
					if(cnt+kakaku[cnt2]>p){
						break;
					}
				}
			}
		}
		for(cnt=p;cnt>0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					flag=1;
					d=cnt;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		d=0;
		flag=0;
		for(cnt=1;cnt<=p;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %ld",&n,&p);
	}
	return 0;
}