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] != 0 {
				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>
#define N 1000001
using namespace std;
int main(){
  int p[N]={0};
  for(int i=3;i<N;i+=2) p[i] = 1;
  p[2] = 1;
  for(int i=3;i<=1000;i+=2){
    if(p[i]==1){
      for(int j=2*i;j<N;j+=i) p[j] = 0;
    }
  }
  int n,x;
  while(cin >> n >> x){
    if(n==0&&x==0) break;
    int t[x+1];
    for(int i=0;i<=x;i++) t[i] = 0;
    for(int i=0;i<n;i++){
      int k;
      cin >> k;
      if(k<=x) t[k] = 1;
      for(int j=1;j<=x;j++){
	if(t[j]==1&&j+k<=x) t[j+k] = 1;
      }
    }
    bool ok = false;
    for(int i=x;i>=0;i--){
      if(t[i]==1&&p[i]==1){
	cout << i << endl;
	ok = true;
	break;
      }
    }
    if(!ok) cout << "NA" << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n)  FOR(i,0,n)
#define INF 1e9


int main(){
    int N = 10e6+1;
    vector<bool> isPrime(N, true);
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i*i < N; i++) {
        if (!isPrime[i]) continue;
        for (int j = i*i; j < N; j += i) isPrime[j] = false;
    }
    while(true){
        int n,x;
        cin >> n >> x ;
        if(n == 0 && x == 0){
            break;
        }

        vector<int> dishes(n,0);

        FOR(i,0,n){
            cin >> dishes[i];
        }
        int ans = 0;
        vector<bool> dp(x+1,false);
        dp[0] = true;

        for(auto dish:dishes){
            FOR(i,0,x+1){
                if((i+dish < x+1) && dp[i]){
                    if(isPrime[i+dish])
                       ans = i + dish;
                    dp[i+dish] = true;
                }
            }
        }
        if(ans == 0)
            cout << "NA" << endl;
        else
            cout << ans << endl;
    }
    return 0;
}

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

const int MAX_X = 1000000;
bool sosu[MAX_X+1];
bool sum[MAX_X+1];
int costs[30];

int main(){
	for(int i = 2; i <= MAX_X; i++) sosu[i] = true;
	for(int i = 2; i * i <= MAX_X; i++){
		if(sosu[i] == true)
			for(int j = 2; i * j <= MAX_X; j++)
				sosu[i*j] = false;
	}
	sosu[0] = sosu[1] = false;
	int n, x;
	while(cin >> n >> x){
		if(n == 0 && x == 0) break;
		for(int i = 1; i <= x; i++){
			sum[i] = false;
		}
		sum[0] = true;
		for(int i = 0; i < n; i++){
			cin >> costs[i];
		}
		for(int i = 0; i < x; i++){
			if(sum[i])
				for(int j = 0; j < n; j++){
					if(i + costs[j] <= x)
						sum[i+costs[j]] = true;
				}
		}
		int m = x;
		while(m > 1){
			if(sum[m] && sosu[m]) break;
			m--;
		}
		if(m == 1) cout << "NA" << endl;
		else cout << m << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
#define MAX_X 1000100
using namespace std;

int n;
int x;
int d[100];
bool p[MAX_X];
bool dp[MAX_X];

int main(){
  
  memset(p,true,sizeof(p));
  p[0] = false;
  for(int i=2;i*i<MAX_X;i++){
    if(p[i]) for(int j=i+i;j<MAX_X;j+=i) p[j] = false;
  }
  
  while(true){
    cin >> n >> x;
    if(!n && !x) break;
    
    memset(dp,false,sizeof(dp));
    dp[0] = true;
    for(int i=0;i<n;i++) cin >> d[i];
    
     for(int i=0;i<MAX_X;i++){
      if(dp[i]){
	for(int j=0;j<n;j++){
	  if(i+d[j] <= x) dp[i+d[j]] = true;
	}
      }
    }
    
    int ans = -1;
    for(int i=0;i<=x;i++){
      if(dp[i] && p[i]) ans = i;
    }

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

C++: #include<stdio.h>
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
int main()
{
	long x,cnt,cnt2,food;
	int n,d=0,g,kakaku[31],youso[1000001]={0};
	scanf("%d %d",&n,&x);
	while(n!=0&&x!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%ld",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=1;cnt<=x;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					youso[cnt+kakaku[cnt2]]=1;
					if(cnt+kakaku[cnt2]>x){
						break;
					}
				}
			}
		}
		for(cnt=x;cnt>0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					d=cnt;
					flag=1;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		flag=0;
		d=0;
		for(cnt=1;cnt<=x;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

int dt[40];
bool prime[100000];
bool okapon[1000000];
/*
long long dfs(int i,int lm){
	long long res=0;
	if(lm < 0)return 0;
	
}
*/
int main(void){
	int n, x,lm;
	rep(i,100000){
		prime[i]=true;
	}
	for(int i=2;i<100000;i++){
		for(int j=i*2;j<100000;j+=i){
			prime[j]=false;
		}
	}

	while(cin>>n>>lm,n||lm){
		int ma = 1;
		bool find = false;
		rep(i,100000){
			okapon[i]=false;
		}
		okapon[1] = 1;
		loop(i,0,n){
			cin>>dt[i];
			rep(k,ma)
			if(okapon[k])
			for(int j = k ; j < lm ; j += dt[i]){
				okapon[k+j] = true;
			}
			ma = max(ma,dt[i]);
		}
		//rep(i,sizeof(okapon))if(okapon[i])cout<<i<<endl;
		for(int i = lm ; i >= 0 ; i --){
			if(i==2){cout<<"NA"<<endl;break;}
			if(okapon[i] && prime[i]){cout<<i<<endl;break;}
		}
	}
}

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

int main()
{
    int n, m;
    while(cin >> n >> m && (n || m)) {
        int cnt=0;vector<int> primes; bool num[m];
        memset(num, false, sizeof(num));
        num[0] = true;

        for(int i=0;i < n;++i) {int hoge; cin>>hoge; if(!(hoge%2)){++cnt;}}
        if (cnt == n) {puts("NA"); continue;}

        for(int i=2,len=m/2;i<=len;++i) {
            if (num[i-1]) continue;
            for(int j=i+i;j <= m;j+=i) {
                num[j-1] = true;
            }
        }

        for(int i=0;i < m;++i) {
            if (!num[i]) {
                primes.push_back(i+1);
            }
        }

        cout << primes.back() << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#include <bitset>
#include <set>
using namespace std;
const int N=1000000;
int main(){
    int n,x,temp;
    bitset<N>b;
    set<int> prime;
    bool f;
    b.reset();
    prime.insert(2);
    for(long long int i=3;i<1000000;i+=2)if(!b[i]){
        prime.insert(static_cast<int>(i));
        for(long long int j=i*i;j<1000000;j+=i*2)b[j]=true;
    }
    while(true){
        scanf("%d%d",&n,&x);
        if(n==0&&x==0)return 0;
        b.reset();
        b[0]=true;
        while(n--){
            scanf("%d",&temp);
            while(temp<=x){
                b|=b<<temp;
                temp<<=1;
            }
        }
        f=true;
        for(set<int>::iterator u=prime.upper_bound(x);f&&u!=prime.begin();){
            u--;
            if(b[*u]){
                f=false;
                printf("%d\n",*u);
            }
        }
        if(f)printf("NA\n");
    }
}