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<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

template<int m>
class Prime{
  std::vector<bool> p;
public:
  std::vector<int> ps;
  Prime(){
    p = std::vector<bool>(m,true);
    assert(m>1);
    p[0]=p[1]=false;
    for(int i=4;i<m;i+=2)
      p[i]=false;
    ps.push_back(2);
    for(int i=3;i<m;i+=2){
      if(p[i]){
        ps.push_back(i);
        for(int j=i+i;j<m;j+=i)
          p[j]=false;
      }
    }
  }

  bool isPrime(int n){
    assert(m>n);
    return p[n];
  }

  int operator [] (int n){
    if(n<ps.size()) return ps[n];
    else return 0;
  }

  int size(){
    return ps.size();
  }
};

int main(){
  Prime<100000+10000> p;
  int n,m;
  while(scanf("%d%d",&n,&m), n + m != -2){
    vector<int> v;
    int base = lower_bound(ALL(p.ps), n) - p.ps.begin();
    if(p.ps[base] == n) base++;

    for(int i=0; i<m; i++)
      for(int j=i; j<m; j++)
        v.push_back(p.ps[base + i] + p.ps[base + j]);

    sort(v.begin(), v.end());
    printf("%d\n",v[m-1]);
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define inf (1<<26)

#define PRIME_MAX 200000
vector<bool> prime(PRIME_MAX+1,true);
vector<int> lst;

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

int func(int n,int p){
	int bottom = 0;
	rep(i,lst.size()){
		if(lst[i] > n){bottom = i; break;}
	}
	vector<int> ret;
	rep(i,p){
		for(int j = i ; j < p ; j++){
			ret.push_back(lst[bottom+i] + lst[bottom+j]);
		}
	}
	sort(all(ret));
	return ret[p-1];
}
int main(){	
	init_prime();
	rep(i,PRIME_MAX+1)if(prime[i])lst.push_back(i);
	int n,p;
	while(cin >> n >> p, n != -1){
		cout << func(n,p) << endl;
	}
}

C++: #include<iostream>
#include<string>
using namespace std;
long long m[100],c[10000],prime[200];
int main(){
	long long n,p;
	int count=0,i=2;
	while(true){
		bool t=true;
		for(int j=2;j*j<=i;j++){
			if(i%j==0)
				t=false;
		}
		if(t==true){
			prime[count]=i;
			count++;
		}
		if(count==200)
			break;
		i++;
	}
	while(true){
		cin>>n>>p;
		if(n==-1&&p==-1)
			break;
		for(int i=0;i<p*p;i++){
			c[i]=1000000;
		}
		if(n==0)
			n++;
		int i=n+1,count=0;
		while(true){
			bool t=true;
			for(int j=0;j<200;j++){
				if(i!=prime[j]&&i%prime[j]==0)
					t=false;
			}
			if(t==true){
				m[count]=i;
				count++;
			}
			if(count==p)
				break;
			if(i%2==1)
				i+=2;
			else
			i++;
		}
		for(int i=0;i<p;i++){
			for(int j=0;j<=i;j++){
				c[i*p+j]=m[i]+m[j];
			}
		}
		for(int i=0;i<p*p-1;i++){
			for(int j=p*p-1;j>i;j--){
				if(c[j]<c[j-1]){
					int tmp=c[j];
					c[j]=c[j-1];
					c[j-1]=tmp;
				}
			}
		}
		cout<<c[p-1]<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
const int MAXN = 2*1e5;
vector<int> pri(2*(int)1e5,true);

void prim(){
	pri[1] = false;
	for(int i=2;i*i<MAXN;i++){
		if(pri[i])
			for(int j=2*i;j<MAXN;j+=i)
				if(pri[j])pri[j] = false;
	}
}
int main(){
	int n,p;
	prim();
	while(cin >>n>>p,n!=-1){
		vector<int> twop;
		int limitt = n+ n/100 +p*10;
		for(int i=n+1;i<limitt;i++){
			if(pri[i])
				for(int j=i;j<limitt;j++){
					if(pri[j])twop.push_back(i+j);
				}
		}
		sort(twop.begin(),twop.end());
		cout << twop[p-1] << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int prime[1300001];

void isprime(void){
    int i,j,n,c;

    for(i = 4;i<1300001;i += 2)prime[i] = 1;

    for (i = 3;i * i <= 1300001;i += 2){
        if (prime[i] == 0){
            for (j = i + i; j <= 1300001; j += i){
                prime[j] = 1;
            }
        }
    }
}

int main(void){
    int n,i,a=0;
    int flag,p,j,k;
    isprime();
    
    while (scanf("%d%d",&n,&p),n==-1&&p==-1){
a=0;
flag=0;
        for(i = (n+1)*2;;i++){
for(j = n+1;j < i;j++){
 for(k = j;j+k <= i;k++){
  if(j+k == i){
   if(prime[j]*prime[k]){
    a++;
    if(a==p){
     printf("%d\n",i);
flag=1;
break;
    }
   }
  }
if(flag)break;
 }
if(flag)break;
}
if(flag)break;
        }
    }
     
    return 0;
}