Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		n, p := io.NextInt(), io.NextInt()
		if n == -1 {
			break
		}
		isPrime := SieveOpt(200000)
		prime := make([]int, p)
		cnt := 0
		if n < 2 {
			prime[0] = 2
			cnt++
		}
		for i := (n + 1) / 2; i < len(isPrime) && cnt < p; i++ {
			if isPrime[i] == true {
				prime[cnt] = 2*i + 1
				cnt++
			}
		}
		comb := make([]int, 0, p*p)
		for i := 0; i < p; i++ {
			for j := i; j < p; j++ {
				comb = append(comb, prime[i]+prime[j])
			}
		}
		sort.Ints(comb)
		io.Println(comb[p-1])
	}
}

func SieveOpt(x int) []bool {
	flags := make([]bool, x/2) // flags[i] := 2*j+1 is prime
	for i := 1; i < x/2; i++ {
		flags[i] = true
	}
	sqrtXi := int(math.Ceil(math.Sqrt(float64(x)))) / 2
	for i := 1; i < sqrtXi; i ++ {
		if !flags[i] {
			continue
		}
		for j := 2 * i * (i + 1); j < x/2; j += 2*i + 1 {
			flags[j] = false
		}
	}
	return flags
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>
using namespace std;

void print(vector<int> V){
  for(vector<int>::iterator p = V.begin(); p != V.end(); p++){
    cout << *p << " ";
  }
  cout << endl;
}

int sosu(int n){
  if(n==2) return 1;
  if(n==1||n==0||n%2==0) return 0;
  for(int i = 3; i <= sqrt(n); i+=2){
    if(n%i == 0) return 0;
  }
  return 1;
}

bool a[300000] = {};

int main(){

  for(int i = 0; i < 300000; i++){
    if(sosu(i)) a[i] = true;
  }

  vector<int> V;
  for(int i = 2; i < 300000; i++){
    if(a[i]) V.push_back(i);
  }

  while(1){
    int N,P;
    cin >> N >> P; if(N==-1&&P==-1) break;
    //print(V);
    int p;
    for(int i = 0; i < V.size(); i++){
      if(N < V[i]){p = i; break;}
    }
    //cout << p << endl;

    vector<int> V2;
    for(int i = p; i < p+100; i++){
      for(int j = p; j <= i; j++){
        V2.push_back(V[i] + V[j]);
      }
    }
    sort(V2.begin(),V2.end());
    //print(V2);
    cout << V2[P-1] << endl;

  }

    return 0;
}

C++: // 2016-12-12
#include <iostream>
#include <algorithm>
#include <cmath>
#include <array>
#include <map>
#include <vector>
using namespace std;

int main() {
	int n, p;
	bool a[100275];
	fill(a, a + 100275, true);
	a[0] = a[1] = false;
	vector<int> v;
	for (int i = 2; i < 100275; i++) {
		if (a[i]) {
			v.push_back(i);
			for (int j = i + i; j < 100275; j += i) {
				a[j] = false;
			}
		}
	}
	vector<pair<int, int>> w;
	for (int i = 0; i < v.size(); i++) {
		for (int j = i, l = min((int)v.size(), i + 50); j < l; j++) {
			if (v[i] + v[j] > 200274) {
				break;
			} else {
				w.push_back(make_pair(v[i] + v[j], min(v[i], v[j])));
			}
		}
	}
	sort(begin(w), end(w));
	while (cin >> n >> p, n >= 0) {
		int i, m;
		for (i = 0, m = 0; i < w.size(); i++) {
			if (w[i].second > n) {
				m++;
				if (m >= p) {
					break;
				}
			}
		}
		cout << w[i].first << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int prime[3000001];

int M( int n, int p ){
	priority_queue<int, vector<int>, greater<int> > list;
	int k = 0;
	int list2[101];
	for( int i = n + 1; k < p; i++ ){
		if( prime[i] == 1 ){
			list2[k++] = i;
		}
	}
	for( int i = 0; i < p; i++ ){
		for( int j = i; j < p; j++){
			list.push( list2[i] + list2[j] );
		}
	}
	int result;
	for(int i = 0 ; i < p ; i++ ){
		result = list.top(); list.pop();
	}
	return result;
}

void P(){
	for( int i = 2; i <= 300000; i++ ){
		prime[i] = 1;
	}
	for( int i = 2; i <= 300000; i++ ){
		if( prime[i] == 1 ){
			for( int j = i * 2; j <= 300000; j += i ){
				prime[j] = 0;
			}
		}
	}
}

int main(){
	P();
	int n, p;
	while( cin >> n >> p && n != -1 ){
		cout << M( n, p ) << endl;
	}
}

C++: #include <cstdio>
#include <algorithm>

using namespace std;

int main(){
	
	int n,p;
	
	bool is_prime[103000];
	int prime_list[10000];
	int sum_list[10000];
	int num_of_prime = 0;
	for(int i=0;i<sizeof(is_prime)/sizeof(bool);i++){
		if(i<2){
			is_prime[i] = false;
			continue;
		}
		is_prime[i] = true;
		int j;
		for(j=0;j<num_of_prime;j++){
			if(i % prime_list[j] == 0){
				break;
			}
		}
		if(j!= num_of_prime){
			is_prime[i] = false;
		}else{
			prime_list[num_of_prime] = i;
			num_of_prime ++;
		}
	}
	
	while(1){
		scanf(" %d %d", &n, &p);
		
		if(n ==-1 && p == -1){
			break;
		}
		int i;
		for(i=0;i<num_of_prime;i++){
			if(prime_list[i] > n){
				break;
			}
		}
		
		int cnt = 0;
		for(int j=0;j<100;j++){
			for(int k=j;k<100;k++){
				sum_list[cnt] = prime_list[i + j] + prime_list[i + k];
				cnt ++;
			}
		}
		sort(sum_list,sum_list+cnt);
		printf("%d\n", sum_list[p-1]);
		
	}
	
	return 0;
}

C++: //Matsuzaki Number
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<ll> prime;

int main(){
  prime.push_back(2);
  for(ll i=3; i<1000000; i+=2){
    bool flag=true;
    for(ll j=0; j<prime.size()&&i>=prime[j]*prime[j]; j++){
      if(i%prime[j]==0){flag=false; break;}
    }
    if(flag)prime.push_back(i);
  }
  int N, P;
  while(true){
    cin>>N>>P;
    if(N==-1&&P==-1)break;
    vector<ll> prime2;
    int to;
    for(to=0; to<prime.size()&&N>=prime[to]; to++);
    for(int i=to; i<to+P; i++)
      for(int j=i; j<to+P; j++)
        prime2.push_back(prime[i]+prime[j]);
    sort(prime2.begin(), prime2.end());
    cout<<prime2[P-1]<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 300000

int main(void){
	int n, p, i, j, count1, count2, flag[MAXNUM + 1], tmp[MAXNUM + 1];
	for(i = 0;i <= MAXNUM;i++)
		flag[i] = 1;
	flag[0] = flag[1] = 0;
	for(i = 2;i <= sqrt((double)MAXNUM);i++){
		for(j = 2 * i;j <= MAXNUM;j += i)
			flag[j] = 0;
	}
	while(1){
		scanf("%d%d",&n,&p);
		if(n == -1 && p == -1)
			break;
		count1 = 0;
		memset(tmp,0,sizeof(tmp));
		for(i = n + 1;count1 < p;i++){
			if(flag[i]){
				count1++;
				count2 = 0;
				for(j = i;count2 < p;j++){
					if(flag[j]){
						count2++;
						tmp[i + j]++;
					}
				}
			}
		}
		count1 = 0;
		for(i = n + 1;count1 < p;i++){
			if(tmp[i])
				count1 += tmp[i];
		}
		printf("%d\n",i - 1);
	}
	return 0;
}