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 <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <stack>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <sstream>
using namespace std;

#define REP(i,a,n) for(i=a; i<n; i++)
#define rep(i,n) REP(i,0,n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define foreach(it,x) for(typeof(x.begin()) it=x.begin(); it!=x.end(); it++)


bool primes[1000010]; // false is prime.
bool ok[1000010];

void build_prime(){
    int i,j;

    primes[0] = primes[1] = true;
    for(i=2; i*i<=1000000; i++){
        if( !primes[i] ){
            for(j=i*2; j<=1000000; j+=i) primes[j] = true;
        }
    }
}

int main(){
    int n,x,i,j,k;
    int dish[33];

    build_prime();

    while( scanf("%d%d",&n,&x), n|x ){

        rep(i,n) scanf("%d",dish+i);
        sort(dish, dish+n);

        int res = 0;
        memset(ok, false, sizeof(ok));
        ok[0] = true;
        for(i=0; i<n; i++){
            for(j=dish[i]; j<=x; j++){
                ok[j] |= ok[j-dish[i]];
                if( !primes[j] && ok[j] ) res = max(res, j);
            }
        }

        if( res == 0 ){ puts("NA"); }
        else{ printf("%d\n",res); }
    }

    return 0;
}