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])
		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<stdio.h>
int hideo[1000001] = { 0 }, ruio[31] = { 0 };
int main()
{
	int  n, x, temp = 1,noyori=0,debu=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 > 0) {
			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<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int main(){

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

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

  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<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];
int oprime[PLEN];
bool data[PLEN];

int main () {

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

	oprime[2] = 2;

	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 ( (oprime[j] % i) == 0 ) {
						oprime[j] = 0;
				}
			}
		}
	}

	oprime[1] = 0;
	/*
	for ( int i=0; i<PLEN; i++ ) {
		if ( oprime[i] != 0 )
			cout << oprime[i] << endl;
	}

	int count = 0;
	for ( int i=0; i<PLEN; i++ ) {
		if ( oprime[i] != 0 ) {
			prime[count++] = oprime[i];
		}
	}
	
	free(oprime);
	*/

	//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 ( !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] != 0 ) {
					//cout << "S" << i << endl;
					if ( answer < i )
						answer = i;
				}
			}
		}
		
		if ( answer != 0 )
			cout << answer << endl;
		else
			cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include <array>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_PAY = 1e6 + 10;
array<bool, MAX_PAY> PrimeTable;

void makePrimeTable()
{
    fill(PrimeTable.begin(), PrimeTable.end(), true);

    for (int i = 2; i * i < MAX_PAY; ++i) {
        for (int j = 2; j * i <= MAX_PAY; ++j) {
            PrimeTable.at(j * i) = false;
        }
    }
}


int main()
{
    makePrimeTable();

    int cook_num, max_pay;

    while (cin >> cook_num >> max_pay) {
        // input
        if (cook_num == 0 && max_pay == 0) break;

        vector<int> dish(cook_num);
        vector<bool> dp(max_pay);

        for (auto& it: dish) {
            cin >> it;
            dp.at(it) = true;
        }

        // solve
        for (int i = 0; i < cook_num; ++i) {
            for (int j = 0; j < max_pay; ++j) {
                if (dp.at(j) && j + dish.at(i) <= max_pay) {
                    dp.at(j + dish.at(i)) = true;
                }
            }
        }

        // return max_ans
        int max_ans = 0;
        for (int temp = 0; temp <= max_pay; ++temp) {
            if (dp.at(temp) && PrimeTable.at(temp)) max_ans = temp;
        }

        switch (max_ans) {
            case 0:
                cout << "NA" << endl;
                break;
            defalut:
                cout << max_ans << endl;
        }

    }

    return 0;
}

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
//typedef pair<int, int> P;
/* struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
}; */

/** Problem0202 : At Boss's Expense **/
const int MAX = 1000001;
int main()
{
	int N, P;
	bool isPrime[MAX];
	memset(isPrime, 1, sizeof(isPrime));
	isPrime[0]=false, isPrime[1]=false;
	for (int i=2; i<MAX; i++) {
		if (isPrime[i]) {
			for (int j=2; i*j<MAX; j++) {
				isPrime[i*j]=false;
			}
		}
	}
	
	while (cin>>N>>P, N||P) {
		vector<int> price(N);
		bool can[MAX]={0};
		
		rep(i, N) {
			cin>>price[i];
			can[price[i]] = true;
		}
		
		for (int i=0; i<N; i++) {
			for (int j=0; j<MAX; j++) {
				if (can[j] && j+price[i]<=P) {
					can[j+price[i]] = true;
				}
			}
		}
		
		int ans=0;
		for (int i=0; i<MAX; i++) {
			if (can[i] && isPrime[i])
				ans = i;
		}
		
		if (ans>0) {
			cout << ans << endl;
		} else {
			cout << "NA" << endl;
		}
	}
}