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 <iostream>
#include <cstring>
#include <algorithm>

#define MEMZERO(x) memset(x, 0, sizeof(x))
#define MEMSET(x, y) memset(x, y, sizeof(x))

using namespace std;

const int SIZE = 101200;
bool isPrime[SIZE];
int prime[SIZE/2], pSize;
int table[5100];

int memo[100001][100];

void setPrime( void )
{
	pSize = 0;
	for(int i=2; i < SIZE; ++i) isPrime[i] = true;
	isPrime[0] = isPrime[1] = false;
	for(int i=2; i < SIZE; ++i) {
		if(!isPrime[i]) continue;
		prime[pSize++] = i;
		for(int j=i+i; j < SIZE; j+=i) {
			isPrime[j] = false;
		}
	}
}

int search_index( int num )
{
	int l=0, h = pSize;
	while(l < h) {
		int m = (l + h) / 2;
		if(prime[m] < num) l = m + 1;
		else if(prime[m] > num) h = m;
		else return (m+1);
	}
	return l;
}

int main( void )
{
	setPrime();
	for(int i=0; i <= 100001; ++i) for(int j=0; j < 100; ++j) memo[i][j] = -1;
	int m, p;
	while(cin >> m >> p, m != -1 || p != -1) {
		if(memo[m][p-1] != -1) {
			cout << memo[m][p-1] << endl;
			continue;
		}
		MEMZERO(table);
		int ii = search_index(m);
		int count = 0;
		for(int i=0; i < 100; ++i)
			for(int j=i; j < 100; ++j) {
				table[count++] = prime[i+ii] + prime[j+ii];
			}
		sort(table, table+count);
		cout << table[p-1] << endl;
		for(int i=0; i < 100; ++i) memo[m][i] = table[i];
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define BIG 1000000010
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
typedef long long int ll;
#define N 300000

int prime[N];

void eratosthenes(){
  rep(i,N) prime[i]=1;
  prime[0]=prime[1]=0;
  rep(i,N){
    if(prime[i]){
      for(int j=i+i;j<N;j+=i) prime[j]=0;
    }
  }
}

int main(){
  int n,p;
  int a[100000];
  int b1,b2;
  eratosthenes();
  while(1){
    cin>>n>>p;
    if(n==-1 && p==-1) break;
    vector<int> P;
    rep(i,N){
      if(prime[i] && i>n) P.push_back(i);
    }
    int c=0;
    b1=b2=0;
    int f;
    f=P.size();
    rep(i,min(p,f)){
      repi(j,i,min(p,f)+i){
	a[c]=P[i]+P[j];
	c++;
      }
    }
    sort(a,a+c-1);
    cout<<a[p-1]<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::sort;

vector<int> make_prime(int n, int p) {
	int cnt = 0;
	int number = n+1;
	vector<int> res;
	while (cnt < p) {
		bool flag = true;
		if (number == 1) {
			number++;
			continue;
		}
		for (int i = 2; i <= sqrt(number); i++) {
			if (number % i == 0) {
				flag = false;
				break;
			}
		}
		if (flag) {
			res.push_back(number);
			cnt++;
		}
		number++;
	}
	return res;
}

int matsuzaki(int n, int p) {
	int res;
	vector<int> sum;
	vector<int> prime = make_prime(n, p);
	for (int i = 0; i < p; i++) {
		for (int j = i; j < p; j++) {
			sum.push_back(prime[i] + prime[j]);
		}
	}
	sort(sum.begin(), sum.end());
	/*for (auto itr = sum.begin(); itr != sum.end(); ++itr) {
		cout << *itr << ", ";
	}
	cout << endl;
	*/
	res = sum[p-1];
	return res;
}

int main(void) {
	while (1) {
		int N, P;
		cin >> N >> P;
		if (N == -1 and P == -1) {
			break;
		}
		int res = matsuzaki(N, P);
		cout << res << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
#define MAX 200000
int main()
{
  int i,j;
  int b[MAX]={0};
  b[0]=b[1]=1;
  for(i=2;i*i<=MAX;i++)if(!b[i])for(j=i+i;j<=MAX;j+=i)b[j]=1;

  vector<int> p;
  for(i=2;i<=MAX;i++)if(!b[i])p.push_back(i);

  int N,P;
  while(cin>>N>>P,N!=-1&&P!=-1){
    multiset<int> ms;
    multiset<int>::iterator it;
    int n;
    for(n=0;n<p.size();n++)if(p[n]>N)break;
    for(i=n;i<=n+P;i++)for(j=i;j<=n+P;j++)ms.insert(p[i]+p[j]);
    it=ms.begin();
    advance(it,P-1);
    cout<<*it<<endl;
  }
}

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

const int PRIME_MAX = 200000;
bool isPrime[PRIME_MAX];
int main () {
	fill(isPrime, isPrime + PRIME_MAX, true);
	isPrime[0] = isPrime[1] = false;
	for (int i = 2; i * i <= PRIME_MAX; i++) {
		if (isPrime[i]) {
			for (int j = i << 1; j < PRIME_MAX; j += i) {
				isPrime[j] = false;
			}
		}
	}
	int n, p;
	while (cin >> n >> p, n != -1 || p != -1) {
		vector<int> primes;
		for (int i = n + 1; primes.size() <= 100; i++) {
			if (isPrime[i]) primes.push_back(i);
		}
		vector<int> sums;
		for (unsigned i = 0; i < primes.size(); i++) {
			for (unsigned j = i; j < primes.size(); j++) {
				sums.push_back(primes[i] + primes[j]);
			}
		}
		sort(sums.begin(), sums.end());
		cout << sums[p - 1] << endl;
	}
	return 0;
}