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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;

vector<ll> p(1000001);
vector<ll> pri;

void era(){
    for (int i=2; i<1000001; i++) {
        if(p[i]==0)continue;
        int j=i;
        while (j<1000001) {
            j+=i;
            if(j>=1000001)break;
            p[j]=0;
        }
    }
}

int main(){
    rep(i, 1000001) p[i]=i;
    era();
    for (int i=2; i<1000001; i++) {
        if(p[i]!=0)pri.push_back(p[i]);
    }
    for(;;){
        int n,p;
        cin >> n >>p;
        if(n==-1 and p==-1)break;
        int ub = 0, ob=pri.size();
        while (ob-ub>1) {
            int mean = (ub+ob)/2;
            if (pri[mean]>=n) {
                ob = mean;
            }
            if (pri[mean]<n) {
                ub = mean;
            }
        }
        while (pri[ob-1]<=n) {
            ob++;
        }
        vector<ll> ans;
        for (int i=ob;i<=ob+p and i<pri.size();i++ ) {
            for (int j=ob; j<=i and j<pri.size(); j++) {
                ans.push_back(pri[i-1]+pri[j-1]);
            }
        }
        sort(ans.begin(), ans.end());
        cout << ans[p-1]<<endl;
    }
}

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

const int MAX_N=101000;
bool prime[MAX_N+1];

void sieve()
{
  prime[0]=prime[1]=false;
  for(int i=2;i<=MAX_N;i++)
    prime[i]=true;
  for(int i=2;i<=MAX_N;i++)
    if(prime[i])
      for(int j=i*2;j<=MAX_N;j+=i)
	prime[j]=false;
}

int is_prime(int n,int p)
{
  vector<int> primeplus;
  vector<int> array;
  int l=0;
  for(int i=n+1;l<=30;i++)
    if(prime[i]){
      array.push_back(i); l++;
    }
  for(int i=0,k=0;k<=250;i++)
    for(int j=0;j<=i;j++){
      primeplus.push_back(array[i]+array[j]); k++;
    }
  sort(primeplus.begin(),primeplus.end());

  return primeplus[p-1];
}

int main()
{
  int n,p;
  sieve(); 
  for(;;){
    cin>>n>>p;
    if(n==-1&&p==-1) break;
    
    cout<<is_prime(n,p)<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool isPrime(int n) {
	if (n == 1) return false;
	for (int i = 2; i*i <= n; ++i) {
		if (n % i == 0) return false;
	}
	return true;
}

//bool f[10000000] = {0};
int main() {
	int N, p;
	while ( cin >> N >> p, N != -1 || p != -1 ) {
//		fill(&f[0], &f[0]+10000000, false);
		int b = -1;
		vector<int> v;
		for (int i = N+1; ; ++i) {
			if (isPrime(i)) {
				v.push_back(i);
				if (v.size() > p+1) break;
			}
		}
		vector<int> a;
		for (int i = 0; i < v.size(); ++i) {
			for (int j = i; j < v.size(); ++j) {
//				if (!f[v[i]+v[j]]) {
					a.push_back(v[i]+v[j]);
//					f[v[i]+v[j]] = true;
//				}
			}
		}
		sort( a.begin(), a.end() );
//		a.erase( unique(a.begin(), a.end()), a.end() );
		cout << a[p-1] << endl;
	}
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define N 20
using namespace std;
int main(){
  int i,j,k,l;
  int a[10000][N+1],b[(N+1)*N/2],n,p;
  a[0][0]=2;
  j=1;
  for(i=3;j<10000;i+=2){
    l=(int)sqrt(i)+1;
    for(k=3;k<l;k+=2){
      if(i%k==0)
	break;
    }
    if(k>=l){
      a[j][0]=i;
      j++;
    }
  }
  for(i=0;i<10000-N;i++){
    for(j=0;j<N;j++){
      a[i][j+1]=a[i][0]+a[i+j][0];
    }
  }
  while(cin>>n>>p&&(n!=-1||p!=-1)){
    for(i=0;a[i][0]<=n;i++);
    l=0;
    for(j=0;j<N;j++){
      for(k=0;k<N-j;k++){
	b[l]=a[i+j][k+1];
	l++;
      }
    }
    sort(b,b+(N+1)*N/2);
    cout<<b[p-1]<<endl;
  }
  return 0;
}