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<cstring>
#include<cstdio>
using namespace std;

int main(){

  int n, x;
  int p[1111111];
  int data[33];
  bool dp[1111111];

  memset(p, -1, sizeof(p));
  p[0] = p[1] = 0;
  for(int i=2;i*i<1111111;i++) if(p[i]) for(int j=i+i;j<1111111;j+=i) p[j] = 0;

  while(scanf("%d %d", &n, &x), n){
    
    memset(dp, 0, sizeof(dp));
    dp[0] = true;
    for(int i=0;i<n;i++){
      scanf("%d", &data[i]);
      for(int j=data[i];j<=x;j++) if(dp[j-data[i]]) dp[j] = true;
    }
    
  int ans = 0;
  for(int i=0;i<=x;i++) if(p[i] & dp[i]) ans = i;

  if(!ans) cout << "NA\n";
  else cout << ans << endl;

  }

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  int i,j,n=1000000,x;
  vector<int> a(1000010,1);
  for(i=2;i<=n;i++){
    if(a[i]==0){
      continue;
    }
    for(j=i*2;j<=n;j=j+i){
      a[j]=0;
    }
  }
  vector<int> y;
  for(i=2;i<=n;i++){
    if(a[i]==1){
      y.push_back(i);
    }
  }
  while(1){
    cin >> n >> x;
    if(n==0 && x==0){
      return 0;
    }
    int b[35];
    vector<int> dp(1000100,0);
    for(i=0;i<n;i++){
      cin >> b[i];
    }
    dp[0]=1;
    for(i=1;i<=n;i++){
      for(j=0;j<=x;j++){
        if(j-b[i-1]>=0){
          dp[j]=max(dp[j],dp[j-b[i-1]]);
        }
        else{
          dp[j]=dp[j];
        }
      }
    }
    int ans=-1;
    for(i=0;i<y.size();i++){
      if(y[i]>x){
        break;
      }
      if(dp[y[i]]==1){
        ans=y[i];
      }
    }
    if(ans==-1){
      cout << "NA" << endl;
    }
    else{
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <windows.h>
int n,m[30];

bool prime[1000000];
bool c[1000000];

void saiki(int total){
	int i;
	if(total==0){
		return;
	}
	
	if(total<0){
		return;
	}
	
	for( i=0 ; i<n ; i++){
		total-=m[i];
		if(c[total]==1)continue;
		if(total>=0){
			c[total]=1;
		}
		saiki(total);
	}
}

int main(void)
{
	int i,j;
	//f
	prime[0]=1;
	prime[1]=1;
	for(i=3;i<1000000;i++){
		if(i%2==0){
			prime[i]=1;
		}
	}
	for(i=3;i<1000000;i++){
		if(prime[i]==0){
			for(j=(i+i);j<1000000;j+=i){
				prime[j]=1;
			}
		}
	}
	//
	while(1){
		int x;
		int flg=0;
		scanf("%d%d",&n,&x);
		if(n==0)break;
		
		for(i=0;i<n;i++){
			scanf("%d",&m[i]);
		}
		saiki(x);
		//
		for(i=0;i<x;i++){
			
			if(c[i]==1 && prime[x-i]==0){
				flg=1;
				break;
			}
		}
		if(flg==1){
			printf("%d\n",x-i);
		}
		else puts("NA");
		for(i=0;i<1000000;i++){
			c[i]=0;
		}
		//
	}
	
	
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  bool prime[1000005]={};//true   
  int n,x,i,j,k;

  rep(i,1000005)
    prime[i]=true;
  for(i=2;i<1000005;i++){
    if(prime[i])
      for(j=2;i*j<1000005;j++)
	prime[i*j]=false;
  }

  while(1){
    int price[30]={};
    int cost[2000005]={};
    cin>>n>>x;
    if(n==0 && x==0)break;
    rep(i,n){
      cin>>price[i];
      cost[price[i]]++;
    }

    rep(i,x)
      if(cost[i])
	rep(j,n)
	  cost[i+price[j]]++;


    for(i=x;i>0;i--)
      if(cost[i]>0 && prime[i])break;

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

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 31;
const int MAX_C = 1e6;
int n,x,cost[MAX];
bool dp[MAX_C+1],prime[MAX_C+1];

void is_prime(){
  fill(prime,prime+MAX_C,true);
  prime[0] = prime[1] = false;

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

int main(){
  is_prime();
  while(cin >> n >> x ,n + x){
    fill(dp,dp+MAX_C,false); 
    fill(cost,cost+MAX,0);
    for(int i = 0 ; i < n ; i++){
      cin >> cost[i];
      dp[cost[i]] = true;
    }
    for(int i = 0 ; i < n ; i++){
      for(int j = 0 ; j <= x ; j++){
	if(dp[j] && j+cost[i] <= x){
	  dp[j+cost[i]] = true;
	}
      }
    }
    int ans = 0;
    for(int i = x ; i >= 0 ; i--){
      if(prime[i]&&dp[i]){
	ans = i;
	break;
      }
    }
    if(ans) cout << ans << endl;
    else cout << "NA" << endl;
  }

  return 0;
}

C++: #include <cmath>
#include <iostream>

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

bool is_prime(int n) {
    if (n % 2 == 0) return false;
    for (int i = 3; i <= std::sqrt(n); i += 2)
        if (n % i == 0) return false;
    return true;
}

int main() {
    int n, x;
    while (std::cin >> n >> x && n > 0 && x > 0) {
        bool course[x + 1];
        rep(i, x + 1) course[i] = false;

        int prices[n];
        rep(i, n) {
            std::cin >> prices[i];
            course[prices[i]] = true;
        }
        course[0] = true;

        int max = -1;
        rep(i, x + 1) rep(j, n) {
            if (i - prices[j] >= 0 && course[i - prices[j]]) {
                if (is_prime(i)) max = i;
                course[i] = true;
                std::cout << i << ' ';
                break;
            }
        }
        std::cout << std::endl;
        if (max < 0)
            std::cout << "NA" << std::endl;
        else
            std::cout << max << std::endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int n, x, ans;
int in[31], in2[31];
bool dp[1000001];
void solve(int a, int sum){
	for (int i = a; i <= n; i++){
		if (sum + in[i] > x&&!in2[i]){
			in[i] /= 1000;
			in2[i] = 1;
		}
		if (sum + in[i] <= x){
			if (dp[sum + in[i]])
				continue;
			if (sum + in[i] % 2){
				dp[sum + in[i]] = 1;
			}
			solve(i, sum + in[i]);
		}
	}
}
int main(){
	while (cin >> n >> x&&n&&x){
		ans = 0;
		fill(dp, dp + 1000001, 0);
		for (int i = 1; i <= n; i++){
			cin >> in[i];
			in[i] *= 1000;
			in2[i] = 0;
		}
		solve(1, 0);
		for (int i = x; i > 0; i--){
			if (dp[i]){
				for (int j = 5; j <= x; j++){
					if (j == i)
						ans = j;
					if (i % j == 0)
						break;
				}
				if (ans)
					break;
			}
		}
		if (ans)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}