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 <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 ++)

const int N = 1000000;
int dt[40];
bool prime[N];
bool dp[N];

int main(void){
	int n, x,lm;
	rep(i,N){
		prime[i]=true;
	}
	for(int i=2;i<N;i++){
		for(int j=i*2;j<N;j+=i){
			prime[j]=false;
		}
	}

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

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool sosuu[1111111];
vector<int> v;
int a[33],x,N;
bool dp[33][1111111];
bool used[33][1111111];
bool solve(int n,int m){
  if(m == 0) return true;
  if(n == N) return false;
  if(used[n][m]) return dp[n][m];
  used[n][m] = true;

  bool res = solve(n+1,m);
  if(!res && m >= a[n]) res |= solve(n,m-a[n]);
  return dp[n][m] = res;
}
int main(void){
  sosuu[0] = sosuu[1] = true;
  for(int i = 0; i*i <= 1000000; i++){
    if(sosuu[i]) continue;
    for(int j = i+i; j <= 1000000; j += i){
      sosuu[j] = true;
    }
  }
  for(int i = 0; i <= 1000000; i++){
    if(!sosuu[i]){
      v.push_back(i);
    }
  }

  while(cin >> N >> x){
    if(!N)break;
    memset(used,false,sizeof(used));
    for(int i = 0; i < N; i++){
      cin >> a[i];
    }
    int i;
    for(i = (int)v.size()-1;i >= 0; i--){
      if(x < v[i]) continue;
      if(solve(0,v[i]))break;
    }
    if(i+1){
      cout << v[i] << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

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

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){
		bool find = false;
		rep(i,100000){
			dp[i]=false;
		}
		dp[0] = 1;
		loop(i,0,n){
			cin>>dt[i];
			rep(j,lm){
				if(j + dt[i] > lm)continue;
				dp[j + dt[i]] |= dp[j];
			}
			
		}
		//rep(i,sizeof(okapon))if(okapon[i])cout<<i<<endl;
		for(int i = lm ; i >= 0 ; i --){
			if(i==1){cout<<"NA"<<endl;break;}
			if(dp[i] && prime[i]){cout<<i<<endl;break;}
		}
	}
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,x;
  while(cin>>n>>x,n+x){
    int ans=-1;
    bool dp[x+1]={};
    vector<int>v(n);
    for(int i=0;i<n;i++){
      cin>>v[i];
      dp[v[i]]=true;
    }
    sort(v.begin(),v.end());
    for(int i=1;i<x;i++){
      for(int j=0;j<v.size();j++){
	if(dp[i])
	if(i+v[j]>x){
	  v.pop_back();
	  j--;
	}else{
	  dp[i+v[j]]=true;
	}
      }
    }
    int num=x;
    if(num%2==0)num--;
    for(int i=num;i>1;i-=2){
      bool flag=true;
      if(dp[i]==true){
	for(int j=2;j<=sqrt(i);j++){
	  if(i%j==0){
	    flag=false;
	    break;
	  }
	}
	if(flag){
	  ans=i;break;
	}
      }
    }
    if(ans==-1&&dp[2]==false)
      cout<<"NA"<<endl;
    else if(ans!=-1)
      cout<<ans<<endl;
    else
      cout<<2<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>

using namespace std;

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

const int N = 1000001;
bool isP[N], dp[N];
int main(){
	FOR(i, 2, N) isP[i] = 1;
	FOR(i, 2, N){
		if(isP[i]) for(int j = i*2; j < N; j += i) isP[j] = false;
	}
	int n, x;
	while(cin >> n >> x, n|x){
		memset(dp, 0, sizeof(dp));
		int ans = 0;
		dp[0] = 1;
		rep(i, n){
			int val;
			cin >> val;
			rep(j, x+1-val){
				if(dp[j]){
					dp[j+val] = 1;
					if(isP[j+val]) ans = max(ans, j+val);
				}
			}
		}
		if(ans) cout << ans << endl;
		else cout << "NA" << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define MC 10000001
using namespace std;
bool p[MC],V[MC]; int n,x,c[35],ans;
void rec(int a){
	rep(i,n)if(a+c[i]<x&&!V[a+c[i]])V[a+c[i]]=1,rec(a+c[i]);
}
int main(){
	for(int i=2;i*i<MC;i++)if(!p[i])for(int j=i+i;j<MC;j+=i)p[j]=1;
	p[0]=p[1]=1;
	while(cin>>n>>x,n){
		rep(i,n)cin>>c[i]; sort(c,c+n);
		fill(V,V+x,0); rec(0);
		for(ans=x;ans>=c[0];ans--)if(!p[ans]&&V[ans])break;
		if(ans<c[0])cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
bool so[1000000]={};
int yomax,N;
int nmax;
int ryo[30];

void bfs(){
  queue <int> Q;
  bool visited[1000000]={};
  nmax=0;
  Q.push(0);
  while(Q.empty()==0){
    int u=Q.front();
    Q.pop();
    visited[u] = 1;
    if(so[u]==0 && nmax<u)
      nmax=u;
    for(int i=0;i<N;i++)
      if(u+ryo[i]<=yomax && visited[u+ryo[i]]==0){
	visited[u+ryo[i]] =1;
	Q.push(u+ryo[i]);
      }
  }
}


int main(){
  so[0] = 1;
  so[1] = 1;
  for(int i=2;i*i<=1000000;i++)
    if(so[i] == 0)
      for(int j=2;j*i<=1000000;j++)
	so[i*j] =1;

  while(cin>>N>>yomax){
    if(N==0 && yomax==0) break;
    for(int i=0;i<N;i++)
      cin>>ryo[i];
    bfs();
    if(so[nmax]==0) cout<<nmax<<endl;
    else cout<<"NA"<<endl;
  }

  return 0;
}