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 <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define EPS 1e-12

int main(void) {
  bool prime[1000000];
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j <= 1000000; j += i) {
        prime[j] = false;
      }
    }
  }

  while (true) {
    int n, p;
    cin >> n >> p;
    if (n == -1 && p == -1)
      break;

    int ii = 0;
    VI pp, ppp;
    pp.clear();
    ppp.clear();
    for (int i = n + 1; ii < p; i++) {
      if (prime[i]) {
        pp.push_back(i);
        ii++;
      }
    }

    REP(i, p) FOR(j, i, p) { ppp.push_back(pp[i] + pp[j]); }
    sort(ALL(ppp));

    /*REP(i, 100) {
      if(i != 0 && i % 10 == 0) cout << endl;
      cout << ppp[i] << " ";
    }*/

    cout << ppp[p - 1] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>
 
using namespace std;
  
const double eps=1e-10;
 
const int dy[]={-1,-1,0,1,1, 1, 0,-1};
const int dx[]={ 0, 1,1,1,0,-1,-1,-1};
 
int main()
{
 
	vector<long> prime;
	prime.push_back(2);
	prime.push_back(3);
	prime.push_back(5);
 
	for(int i=7;prime.back()<=100000+100*100;i+=2){
		bool ok=true;
		for(int j=0;prime[j]*prime[j]<=i && ok;j++){
			if(i%prime[j]==0) ok=false;
		}
		if(ok) prime.push_back(i);
//	  if(ok) cout<<i<<endl;
	}
 
	long n;
	int p;
	while(cin>>n>>p,n!=-1){
		vector<long> v;

		long pos=lower_bound(prime.begin(),prime.end(),n)-prime.begin();
		if(prime[pos]==n) pos++;

		for(long i=pos;i<pos+p;i++){
			for(long j=i;j<pos+p;j++) v.push_back(prime[i]+prime[j]);
		}

		sort(v.begin(),v.end());
//		for(int i=0;i<v.size();i++) cout<<i<<" "<<v[i]<<endl;
//		cout<<endl;

		cout<<v[p-1]<<endl;
	}
 
 
	return 0;
}

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

using namespace std;

bool prime[500005];
vector<int> pvec, vec;
int n, k;


int main(void)
{
	for(int i = 2; i < 800; i++){
		if(prime[i]) continue;
		for(int j = 2*i; j < 500005; j+=i) prime[j] = true;
	}
	for(int i = 2; i < 500005; i++) if(!prime[i]) pvec.push_back(i);
	
	while(1){
		cin >> n >> k;
		if(n == -1 && k == -1) break;
		
		int p = upper_bound(pvec.begin(), pvec.end(), n) - pvec.begin();
		vec.clear();
		for(int i = p; i < p+105; i++){
			for(int j = p; j < p+105; j++){
				if(i > j) continue;
				vec.push_back(pvec[i] + pvec[j]);
			}
		}
		sort(vec.begin(), vec.end());
		cout << vec[k-1] << 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[450];
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+20;j++){
      for(c=j;c<j+20;c++){
        w[q++]=a[j]+a[c];
      }
    }
    sort(w,w+q-1);
    for(c=0;c<=100;c++)b[i][c]=w[i];
  }
  while(cin>>i>>j){
    if(i<0)break;
    cout<<b[i][j-1]<<endl;
  }
}