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++: /* 0-1 knapsack problem (DP) */

#include<vector>
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	vector<bool> era(1000000,true);
	era[0]=false;
	for(int i=2;i<1000;i++){
		if(era[i])	for(int j=i*i;j<1000000;j+=i)	era[j]=false;
	}

	int menu[30];
	static bool knap[1000001];
	for(int n,bud;cin>>n>>bud,n;){
		for(int i=0;i<n;i++)	cin>>menu[i];

		knap[0]=true;
		for(int i=1;i<=bud;i++)	knap[i]=false;
		for(int i=0;i<n;i++){
			for(int j=0;j<bud;j++){
				if(knap[j] && j+menu[i]<=bud)
					knap[j+menu[i]]=true;
			}
		}

		bool b=false;
		for(int i=bud;i>=0;i--){
			if(knap[i] && era[i]){
				cout<<i<<endl;
				b=true;
				break;
			}
		}
		if(!b)	cout<<"NA"<<endl;
	}

	return 0;
}

C++: #include<stdio.h>
int hideo[2000001] = { 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 (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 ||noyori==x) {
			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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define xMAX 1000001
#define nMAX 31
#define Do dp[i][j-v[i]]+v[i]
#define Not dp[i-1][j]

int v[nMAX];

int dp[nMAX][xMAX];

bool prime[xMAX];

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

bool isPrime(int n){
  return prime[n];
}

void makedp(){
  rep(i,nMAX)
    dp[i][0]=0;
  rep(i,xMAX)
    dp[0][i]=0;
}

void printdp(int n, int x){
  putchar('\n');
  rep(i,n+1){
    printf("%d :",i+1);
    rep(j,x+1){
      printf("%2d ",dp[i][j]);
    }
    putchar('\n'); 
  }
}

void solve(int n, int x){
  int max=0;
  int i,j;
  for(i=1;i<n+1;i++)
    for(j=1;j<x+1;j++){
      if( Do >x)
	break;
      if(j<v[i-1])
	dp[i][j] = Not;
      else{
	if (Do > Not){
	  dp[i][j] = Do;
	  if(max<Do && Do<=x && isPrime(Do))
	    max=Do;
	}
	else
	  dp[i][j] = Not;
      }
    }

  //printdp(n,x);

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

int main(){
  sieve();
  makedp();
  v[0]=0;
  for(;;){
    int n,x;
    cin >> n >> x;
    if(!(n+x)) break;
    for(int i=1;i<n+1;i++)
      cin >> v[i];
    solve(n,x);
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout<<x<<endl
#define debug(x) cout<<#x<<": "<<x
const int MAX_INT = 1000000;
bool er[1000001];
int n,x,ans;
int v[31];
int solve(int k,int m);
int memo[31][1000001];
int main(){
  memset(er,true,sizeof(er));
  for(int i=2;i<sqrt(MAX_INT);i++){
    if(er[i]){
      for(int j=0;i*(j+2)<MAX_INT;j++){
	er[i*(j+2)]=false;
      }
    }
  }
  while(1){
    cin >> n >> x;
    if(!n && !x)break;
    memset(memo,-1,sizeof(memo));
    for(int i=0;i<n;i++){
      cin >> v[i];
    }
    
    ans = solve(0,0);
    if(!ans)cout << "NA\n";
    else cout << ans << endl;
  }
}
int solve(int k,int m){
  if(memo[k][m] != -1)return memo[k][m];
  if(m > x)return 0;
  else if(k == n){
    if(m == 0)return 0;
    if(er[m])return m;
    else return 0;
  }
  return memo[k][m] = max(solve(k,m+v[k]),solve(k+1,m));
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
bool SOSU[1000001];
bool ARU[1000001];
void solve(){
	SOSU[0]=true,SOSU[1]=true;
	for(int i=2;i<1000001;i++){
		if(SOSU[i]==false){
			for(int j=2;i*j<1000001;j++){
				SOSU[i*j]=true;
			}
		}
	}
}
int main(){
	fill(SOSU,SOSU+1000000,false);
	solve();
	int n,x,sum,a[30];
	while(1){
	fill(ARU,ARU+1000000,false);
	cin >> n >> x;
	if(n==0&&x==0)break;
	sum=0;
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	ARU[0]=true;
	for(int i=0;i<=x;i++){
		if(ARU[i]==true){
				for(int j=0;j<n;j++){
					if(i+a[j]<=x)
					ARU[i+a[j]]=true;
				}
		}
	}
	int flg=0;
	for(int i=x;i>=1;i--){
		if(ARU[i]==true && SOSU[i]==false){
			cout  << i << endl;
			flg=1;
			break;
		}
	}
	if(flg==0)printf("NA\n");
}
}

C++: #include <iostream>
using namespace std;
int n,budget,price[30];
int dp[1000001];
bool is_prime[1000001];

void sieve() {
  for(int i=0;i<1000001;i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;

  for(int i=0;i*i<1000001;i++) {
    if(is_prime[i]) {
      for(int j=2*i;j<1000001;j+=i) {
        is_prime[j] = false;
      }
    }
  }
}

void solve() {
  for(int j=0;j<=budget;j++) {
    dp[j] = 0;
  }

  for(int i=0;i<n;i++) {
    for(int j=0;j<=budget;j++) {
      if(j-price[i]>=0) {
        dp[j] = max(dp[j], dp[j-price[i]] + price[i]);
      }
      else {
        dp[j] = dp[j];
      }
    }
  }
  for(int i=0;i<budget;i++) {
    if(is_prime[dp[budget-i]]) {
      cout << dp[budget-i] << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  sieve();
  while(cin >> n >> budget) {
    if(n==0&&budget==0) break;
    for(int i=0;i<n;i++) {
      cin >> price[i];
    }
    solve();
  }
}

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

int main() {

	while(true)
	{
		int N,X;
		cin >> N >> X;
		if(N==0 && X==0)break;
		
		vector<int> food;
		for(int i=0;i<N;i++){
			int m;
			cin >> m;
			food.push_back(m);
		}
		
		vector<int> num(X+1);
		vector<int> prim(X+1);
		fill(num.begin(),num.end(),0);
		fill(prim.begin(),prim.end(),1);
		for(int i=0;i<N;i++){
			num[ food[i] ]=1;
		}
		for(int i=0;i<X+1;i++){
			if( num[i]!=0){
				for(int j=0;j<N;j++){
					if( i+food[j]>=num.size())break;
					num[i+food[j]]=1;
				}
			}
		}
		prim[0]=0;
		prim[1]=0;
		for(int i=2;i<X+1;i++){
			if( prim[i]!=0 ){
				int x=i+i;
				while(x<X+1){
					prim[x]=0;
					x+=i;
				}
			}
		}
		int ans=-1;
		for(int x=X;x>=0;x--){
			if( prim[x]==1 && num[x]!=0){
				ans = x;
				break;
			}			
		}
		if(ans>0){
			cout << ans << endl;
		}else{
			cout << "NA" << endl;
		}
	}
	


	return 0;
}