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 <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int psize;
int prime[200000];
bool bprime[200000];
int cand[200000];

int main() {
  MEMSET(bprime, true);
  bprime[0] = bprime[1] = false;
  REP(i, 200000) {
    if (!bprime[i]) { continue; }
    prime[psize++] = i;
    for (int j = i + i; j < 200000; j += i) {
      bprime[j] = false;
    }
  }
  int n, p;
  while (scanf("%d %d", &n, &p), n != -1 && p != -1) {
    int s = 0;
    REP(i, psize) {
      if (n < prime[i]) { s = i; break; }
    }
    int m = 0;
    REP(i, 100) REP(j, i + 1) {
      cand[m++] = prime[s + i] + prime[s + j];
    }
    sort(cand, cand + m);
    printf("%d\n", cand[p - 1]);
  }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <climits>
#include <bitset>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define fin(ans) cout<<(ans)<<endl
#define P 1000000007
#define STI(s) atoi(s.c_str())//string to int
#define mp(p,q) make_pair(p,q)
#define Sort(a) sort(a.begin(),a.end())
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
const int INF = INT_MAX;
const long long LLINF = 1LL<<60;
//g++ -std=c++1z temp.cpp
//./a.out
vector<bool> eratosmake(int n,vector<bool> num){
    //整数を列挙して素数の候補とする
    for(int i = 0; i < n; i++) num[i] = true;
    //0と1を消す
    num[0] = num[1] = false;
    //iを残してiの倍数を消していく
    for(int i = 2; i < n; i++){
        if(num[i]){
            for(int j = i + i; j < n; j += i){
                num[j] = false;
            }
        }
    }
    return num;
}

int main(void){ios::sync_with_stdio(false);cin.tie(0);
//////////////////////////////////////////////////////
    int n,q;
    vector<bool> prime(200000,false);
    prime=eratosmake(200000,prime);
    while(1){
        cin>>n>>q;
        if(n==-1&&q==-1)break;
        int co=110,p=n+1;
        vector<int> primelist;
        while(co>0){
            if(prime[p]){
                primelist.push_back(p);
                co--;
            }
            p++;
        }
        // rep(i,primelist.size())cout<<primelist[i]<<" ";
        // cout<<endl;
        vector<int> m;
        for(int i=0;i<primelist.size();i++){
            for(int j=i;j<primelist.size();j++){
                m.push_back(primelist[i]+primelist[j]);
                // if(primelist[i]+primelist[j]==5) cout<<primelist[i]<<" "<<primelist[j]<<endl;
            }
        }
        sort(m.begin(),m.end());

        // rep(i,10)cout<<m[i]<<" ";
        // cout<<endl;

        cout<<m[q-1]<<endl;
    }
//////////////////////////////////////////////////////
return 0;}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#define F(i,n) for(int i=0;i<(int)(n);i++)
#define INF 100000000
#define ll long long

using namespace std;
typedef pair<int, int> P;

int prime[1000010];
bool is_prime[1000010];

void sieve(int n) {
	int p = 0;
	for (int i = 0; i <= n; i++) is_prime[i] = true;
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i <= n; i++) {
		if (is_prime[i]) {
			prime[p++] = i;
			for (int j = 2 * i; j <= n; j += i) {
				is_prime[j] = false;
			}
		}
	}
}
int main() {
	ll  b[10000],x,a1[50],a2[50],r;
	int y,q,counter;
	sieve(200000);
	while (1) {
		cin >> x >> y;
		
		if (x == -1 && y == -1)break;
		for (int i = 0; i < 50; i++) {
			a1[i] = 0;
			a2[i] = 0;
		}
		for (int i = 0; i < 10000; i++) {
			b[i] = INF;
		}
		r = 0;
		for (int i = 0;; i++) {
			if (r >= 50)break;
			if (prime[i] > x) {
				a1[r] = prime[i];
				a2[r] = prime[i];
				r++;
			}
		}
		r = 0;
		for (int i = 0; i < 50; i++) {
			for (int j = i; j < 50; j++) {
				b[r] = a1[i] + a2[j];
				r++;
			}
		}
		sort(b, b+10000);
		
		cout << b[y-1] << endl;
	}
	return 0;
}