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 <bits/stdc++.h>
using namespace std;

int main() {
    int N, K;

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

    while (cin >> N >> K, N || K) {
        vector<int> v(N);
        for (auto& i : v) cin >> i;

        vector<bool> dp(K+1, false);
        dp[0] = true;
        int ans = -1;
        for (int i = 0; i < K; i++) {
            if (dp[i]) {
                for (auto j : v) {
                    if (i + j <= K) dp[i + j] = true;
                    if (i + j <= K && is_prime[i+j])
                        ans = max(ans, i + j);
                }
            }
        }

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

    return 0;
}

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

int gcd(int m,int n){
  while(m != n){
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main(){
  //ツ素ツ青板青カツ青ャ
  const int NUM = 1000000;
  bool prime[NUM];
  prime[0] = false;
  prime[1] = false;
  for(int i=2;i<NUM;i++)
    prime[i] = true;
  for(int i=2;i*i<NUM;i++)
    for(int k=2;i*k<NUM;k++)
      prime[i*k]=false;

  while(true){
    int n,x;
    cin>>n>>x;
    if(!n&&!x)break;
    int menu[n];
    int g;
    cin>>menu[0];
    g = menu[0];
    for(int i=1;i<n;i++){
      cin>>menu[i];
      g = gcd(g,menu[i]);
    }
    int p = -1;
    if(g==1){
    bool money[x+1];
    money[0] = true;
    for(int i=1;i<x+1;i++){
      money[i] = false;
      for(int j=0;j<n;j++){
	if(i>=menu[j]&&money[i-menu[j]]){
	  money[i] = true;
	  break;
	}
      }
      if(money[i]&&prime[i])p=i;
    }
    }
    if(p==-1)cout<<"NA"<<endl;
    else cout<<p<<endl;
  }
  return 0;
}

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


int main(){
  bool is_prime[N];
  for(int i=0;i<N;i++)
    is_prime[i] = false;

  for(int i=3;i<N;i+=2) is_prime[i] = true;
  is_prime[2] = true;
  for(int i=3;i<=1000;i+=2){
    if(is_prime[i]){
      for(int j=2*i;j<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<queue>
#include<functional>
#include<set>
using namespace std;
int main(){
  //ツ素ツ青板青カツ青ャ
  const int NUM = 1000000;
  bool prime[NUM];
  prime[0] = false;
  prime[1] = false;
  for(int i=2;i<NUM;i++)
    prime[i] = true;
  for(int i=2;i*i<NUM;i++)
    for(int k=2;i*k<NUM;k++)
      prime[i*k]=false;

  while(true){
    int n,x;
    cin>>n>>x;
    if(!n&&!x)break;
    vector<int> menu(n);
    for(int i=0;i<n;i++){
      cin>>menu[i];
    }
    int p = -1;
    set<int> money;
    set<int>::iterator it;
    money.insert(0);
    while(true){
      it = money.begin();
      if(*it>x)break;
      if(prime[*it])p=*it;
      for(int i=0;i<n;i++){
	money.insert(menu[i]+*it);
      }
      money.erase(it);
    }
    if(p==-1) cout<<"NA"<<endl;
    else cout<<p<<endl;
  }
  return 0;
}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[31][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(i=0;i<n;i++){
			for(j=0;j<=x;j++){
				if(j>0){
					dp[i][j]=-1;
				}
				else if(j==0){
					dp[i][j]=0;
				}
			}
		}
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[i][j]!=-1){
					dp[i][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[n][i]==1 && flg[i]==1){
				max=i;
			}
		}
/*		for(i=0;i<=x;i++){
			printf("%d=%d ",i,flg[i]);
		}
		printf("\n");*/
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

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

using namespace std;

const int MAX_N = 1e6 + 1;

bool is_prime[MAX_N + 1];

void SieveOfEratosthenes(int n)
{
    for (int i = 0; i <= n; ++i)
        is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;

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


int main()
{
    int n, x;

    SieveOfEratosthenes(1000000);

    while (cin >> n >> x, n || x) {
        int item[n];
        bool flag[x];

        memset(flag, 0, sizeof(flag));
        for (int i = 0; i < n; ++i)
            cin >> item[i];

        flag[0] = true;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j + item[i] <= x; ++j)
                if (flag[j])
                    flag[j + item[i]] = true;
        }

        int money = x;
        for ( ; money >= 0; --money)
            if (is_prime[money] && flag[money])
                break;

        if (money > 0)
            cout << money << endl;
        else
            cout << "NA\n";
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pb push_back
#define MAX 100005
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;

bool Is_Prime(int n){
  if(n==1) return false;
  for(int i=2;i*i<n;i++){
    if(n%i==0){
      return false;
    }
  }
  return true;
}

signed main(){
    int n,x;
    while(cin>>n>>x,n){
      vector<int> v(n);
      rep(i,n)cin>>v[i];
      vector<int> dp(x+1);
      dp[0]=1;
      rep(j,n){
        loop(i,1,x+1){
          if(i-v[j]>=0&&dp[i-v[j]]==1){
            dp[i]=1;
          }
        }
      }
      for(int i=x;i>0;i--){
        if(dp[i]==1&&Is_Prime(i)){
          cout<<i<<endl;
          goto at;
        }
      }
      cout<<"NA"<<endl;
      at: ;
    }

  return 0;
}