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 <algorithm>
#include <set>
#include <iostream>
#include <vector>
using namespace std;

const int LIM = 3000000;

char isprime[LIM];
int primes[LIM];
int PRIME_LIM = 0;

void eratosthenes() {
    fill(isprime, isprime + LIM, 1);
    isprime[0] = isprime[1] = 0;

    for(int x = 2; x * x < LIM; ++x) {
        if(!isprime[x]) continue;

        for(int y = x * x; y < LIM; y += x) {
            isprime[y] = 0;
        }
    }

    for(int x = 0; x < LIM; ++x) {
        if(isprime[x]) {
            primes[PRIME_LIM++] = x;
        }
    }
}

int solve(int N, int P) {
    vector<int> mtseq;
    int* ub = upper_bound(primes, primes + PRIME_LIM, N);

    for(int s = 0; s <= P; ++s) {
        for(int j = 0; j <= s; ++j) {
            int m = ub[s] + ub[j] - 2 * N;
            mtseq.push_back(m);
        }
    }

    sort(begin(mtseq), end(mtseq));

    return mtseq[P - 1];
}

int main() {
    eratosthenes();
    int N, P;
    while(true) {
        cin >> N >> P;
        if(N == -1 && P == -1) break;
        cout << solve(N, P) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int a[100000]={2,3},i,j,k,c=2,b[100002][101],q,w[700];
int main(){
  for(i=4;i<150000;i++){
    for(k=1,j=2;j*j<=i;j++){
      if(i%j==0){k=0;break;}
    }
    if(k)a[c++]=i;
  }k=0;
  for(i=0;i<=100000;i++){
    if(i>=a[k])k++;
    for(q=0,j=k;j<k+25;j++){
      for(c=j;c<j+25;c++){
        w[q++]=a[j]+a[c];
      }
    }
    sort(w,w+q-1);
    for(c=0;c<=100;c++)b[i][c]=w[c];
  }
  while(cin>>i>>j){
    if(i<0)break;
    cout<<b[i][j-1]<<endl;
  }
}

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

int prime[10000],size;

bool isPrime(int x){
	for(int i=0;i<size && prime[i]<=sqrt(x);i++){
		if(x % prime[i] == 0) return false;
	}
	return true;
}

void mkPrime(void){
	prime[0] = 2;
	size = 1;
	for(int i=3;i<101500;i++){
		if(isPrime(i)) prime[size++] = i;
	}
}

int main(void){
	int i,j,n,p,from,to;
	int sum[5100],len;

	mkPrime();

	while(cin>>n>>p && n+1){
		for(from=0;prime[from]<=n;from++);
		to = from+p;
		len = 0;

		for(i=from;i<to;i++){
			for(j=i;j<to;j++){
				sum[len++] = prime[i] + prime[j];
			}
		}
		sort(sum,sum+len);
		cout<<sum[p-1]<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cctype>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>

using namespace std;

int max(int a, int b);
int min(int a, int b);
int is_prime(int n);

int main(int argc, char const* argv[])
{
    int n,p;
    while (cin >> n >> p) 
    {
        if ( n==-1 && p==-1 ) { break; }
        vector<int> arr;

        //素数列を格納
        int count=0;
        int i=n+1;
        while ( count <  p ) 
        {
            if ( is_prime(i) ) 
            {
                //cout << "i = " << i << endl;
                arr.push_back(i);    
                count++;
            }
            i++;
        }

        //
        //for (int i = 0; i < p; i++) 
        //{
        //    cout << arr[i] << endl;
        //}

        //足した素数を格納
        vector<int> sum;
        for (int i = 0; i < p; i++) 
        {
            for (int j = i; j < p; j++) 
            {
                sum.push_back( arr[i] + arr[j] );
            }
        }
        sort(sum.begin(), sum.end());
        cout << sum[p-1] << endl;    
    }
    return 0;
}

int is_prime(int n)
{
    if (n ==1 || n== 0) 
    {
        return 0;
    }
    for (int i = 2; i*i <= n; i++) 
    {
        if (n % i == 0) 
        {
            return 0;
        }
    }
    return 1;

}


int max(int a, int b)
{
    if (a > b) 
    {
        return a;
    }
    else{
        return b;
    }
}
int min(int a, int b)
{
    if (a > b) 
    {
        return b;
    }
    else{
        return a;
    }
}

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

int isprime[150001];
vector<int> primes;

int main() {
    isprime[0] = isprime[1] = 1;
    for(int i=4; i<=150000; i+=2) isprime[i] = 1;
    for(int i=3; i*i<=150000; i+=2)
        if(isprime[i] == 0)
            for(int j=2; i*j<=150000; ++j)
                isprime[i*j] = 1;

    primes.push_back(2);
    for(int i=3; i<=150000; i+=2)
        if(isprime[i] == 0) primes.push_back(i);

    int N,P;
    while(cin>>N>>P) {
        if(N == -1 && P == -1) break;

        vector<int> pv1,pv2;
        for(int i=0,cnt=0; i<=150000; ++i) {
            if(primes[i] <= N) continue;
            pv1.push_back(primes[i]);
            cnt++;
            if(cnt > P) break;
        }

        for(int i=0; i<pv1.size(); ++i) {
            for(int j=i; j<pv1.size(); ++j) {
                pv2.push_back(pv1[i]+pv1[j]);
            }
        }

        sort(pv2.begin(), pv2.end());
        cout<<pv2[P-1]<<endl;
    }
}