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>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);

using namespace std;

typedef long long ll;
typedef long double ld;


int n;
ll V[31], x;
ll dp[32][1LL<<32];
ll maxn;

bool sosu(ll t){
  if(t == 1) return false;
  if(t == 2) return true;
  if(t%2 == 0) return false;

  for(ll i=3; i*i<=t; i+=2){
    if(t%i == 0) return false;
  }

  return true;
}

void solve(ll now) {
  if(now > x) return;
  if(sosu(now) && now > maxn)
    maxn = now;
  rep(i, n) {
    solve(now + V[i]);
  }
  return;
}

int main() {
  while(cin >> n >> x, n) {
    maxn = 0;
    rep(i, n) {
      cin >> V[i];
    }
    solve(0);
    if(maxn == 0) {
      cout << "NA" << endl;
    } else {
      cout << maxn << endl;
    }
  }

  return 0;
}

C++: #include<iostream>
#include<cmath>
#define PLEN 1000010
using namespace std;

int gcm ( int a, int b ) {
	int temp;

	while ( a != 0 ) {
		
		temp = a-b;
		a = b;
		b = temp;
	}
	return b;
}
		

int dish[30];
bool oprime[PLEN];
bool data[PLEN];

int main () {

	for ( int i=0; i<PLEN; i++ ) {
		oprime[i++] = false;
		oprime[i] = true;
	}

	oprime[2] = true;

	int limit;
	limit = (int)(sqrt((double)PLEN));

	for ( int i=3; i<limit; i+=2 ) {
		for ( int j=1; j<PLEN; j+=2 ) {
				if ( j != i ) {
				if ( (j % i) == 0 ) {
						oprime[j] = false;
				}
			}
		}
	}

	oprime[1] = false;

	//cout << "Start" << endl;
	
	while(true) {
		int dishNum;
		cin >> dishNum;

		int budget;
		cin >> budget;

		if ( dishNum == 0 )
			break;


		for ( int i=0; i<=budget; i++ ) {
			data[i] = false;
		}


		for ( int i=0; i<dishNum; i++ ) {
			cin >> dish[i];
			data[dish[i]] = true;
		}

		int count = 1;

		while ( true ) {
			bool done = false;

			for ( int i=0; i<=budget; i++ ) {
				if ( data[i] ) {
					for ( int j=0; j<dishNum; j++ ) {
						if ( i + dish[j] <= budget ) {
							if ( !data[i+dish[j]] ) {
								data[i+dish[j]] = true;
								done = true;
							}
						}
					}
				}
			}

			//cout << "Pass" << count++ << endl;

			if ( !done )
				break;
		}
		/*
		for ( int i=0;i<=budget; i++ ) {
			if ( data[i] )
				{
					if ( i%10 )
						//cout << i << endl;
				}
		}
		*/
		int answer = 0;

		for ( int i=0; i<=budget; i++ ) {
			if ( data[i] ) {
				if ( oprime[i] ) {
					//cout << "S" << i << endl;
					if ( answer < i )
						answer = i;
				}
			}
		}
		
		if ( answer != 0 )
			cout << answer << endl;
		else
			cout << "NA" << endl;
	}
}

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

#define MAX_X 1500001

int n;
int x;
vector<int> p;

bool table[MAX_X];
vector<int> prime;

void init() {
    memset(table, 1, sizeof(table));
    table[0] = table[1] = false;
    for (int i = 2; i < MAX_X; i++) {
        if (table[i]) {
            prime.push_back(i);
            for (int j = i + i; j < MAX_X; j += i) {
                table[j] = false;
            }
        }
    }
}

bool cache[MAX_X];
bool cached[MAX_X];

bool can_make(int t) {
//    cout << t << endl;
    if (t == 0) return true;
    else if (t < 0) return false;
    if (cached[t]) return cache[t];

    bool can = false;
    for (int i = 0; i < p.size(); i++) {
        if (can_make(t-p[i])) {
            can = true;
            break;
        }
    }
    cache[t] = can;
    cached[t] = true;
    return can;
}

int main() {
    init();
//    for (int i = 0; i < prime.size(); i++) {
//        cout << prime[i] << ' ';
//    }

    while (cin >> n >> x, n || x) {
        memset(cache, 0, sizeof(cache));
        memset(cached, 0, sizeof(cached));

        p.clear();
        p.resize(n);
        for (int i = 0; i < n; i++) {
            cin >> p[i];
        }
        sort(p.begin(), p.end(), greater<int>());

        bool exist = false;

        int ub = prime.size()-1;
        int lb = 0;
        int mid;

        while (lb != ub - 1) {
            mid = (lb + ub) / 2;
            if (prime[mid] < x) {
                lb = mid;
            } else {
                ub = mid;
            }
        }

//        cout << "ub: " << ub << endl;
        for (int i = mid; 0 <= i; i--) {
            if (can_make(prime[i])) {
                cout << prime[i] << endl;
                exist = true;
                break;
            }
        }

        if (!exist) {
            cout << "NA" << endl;
        }

    }
    return 0;
}