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 <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;


int prime[MAX_PRIME+1];
bool isPrime[MAX_PRIME+1];
int p;

void erats(){
	// primeツδ環スツトツづーツ渉可甘コツ可サ
	fill(isPrime,isPrime+MAX_PRIME,true);
	p = 0;
	isPrime[0] = isPrime[1] = false;

	for(int i = 2; i <= MAX_PRIME; i++){
		if(isPrime[i]){
			prime[p++] = i;
		}
		else
			continue;
		// ツづ督づゥツつ「ツづ可つゥツつッツづゥ
		for(int j = 2*i; j <= MAX_PRIME; j+=i){
			isPrime[j] = false;
		}
	}
}

int main(){

	int N,P;
	erats();
	while(cin>>N>>P&&!(N==-1&&P==-1)){
		int *it = upper_bound(prime,prime+p,N);
		int prv=it-prime;
		int init=prv;
		vector<int> v;
		for(int i = init; i <= init+100; i++){
			for(int j = i; j <= init+100; j++){
				v.push_back(prime[i]+prime[j]);
			}
		}
		sort(v.begin(),v.end());
		cout<<v[P-1]<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100000

int main(){
        int n, p, sosuu[100],count=0,sum[10000];
        string keymoji;

        cin >> n >> p;
        while(n != -1 && p != -1){
                int i=n;
                count = 0;
                if(i<2)i=2;
                for(; count<p; i++){
                        int m=sqrt(i);
                        int tmp;
                        if(i==2){
                                sosuu[count]=i;
                                count++;
                                continue;
                        }
                        for(int j=2;j<m;j++){
                                if((tmp=i%j) == 0)break;
                        }
                        if(tmp != 0){
                                sosuu[count]=i;
                                count++; 
                        }
                        
                }
                count =0;
                for(i=0; i<p; i++){
                        for(int j=i; j<p; j++){
                                sum[count]=sosuu[i]+sosuu[j];
                                count++;
                        }
                }

                sort(sum, sum+count);
                cout << sum[p-1] << endl;
                cin >> n >> p;
        }
}

C++: #include<cstdio>
#include<algorithm>

using namespace std;

#define A 5011
#define PRI 1000
#define PRIME 1100000

int main(int argc, char *argv[]){

  int N, P;
  int i, j, cnt=0;
  int prime[PRI], a[A];
  bool isprime[PRIME];



    fill(isprime, isprime+PRIME,true);
    isprime[0] = false;
    isprime[1] = false;

    for(i=2;i<PRIME+1;i++){
      isprime[i]=true;
    }
    
    cnt=0;
    for(i=2;i<PRIME+1;i++){
      if(isprime[i]){
	//	printf("%d is prime\n", i);
	for(j=2;j<PRIME/i;j++){
	  isprime[i*j]=false;
	}
      }
    }

  while(true){

    fill(a, a+A, 0);
    fill(prime, prime+PRI, 0);

    scanf("%d %d", &N, &P);
    if(N==-1 && P==-1){break;}

    cnt=0;
    for(i=N+1;i<PRIME;i++){
      if(cnt==100){break;}
      if(cnt<100 && isprime[i]){
	prime[cnt++] = i;
	//	printf("%d is selected prime\n", prime[cnt-1]);
      }
    }

    cnt=0;
    for(i=0;i<100;i++){
      for(j=0;j<i+1;j++){
	a[cnt++] = prime[i]+prime[j];
	//     	printf("%d is %dth sum of two primes\n", a[cnt-1], cnt-1);
      }
    }


    sort(a, a+5010);

    printf("%d\n", a[P-1]);
  }

  return 0;
}

C++: #include <cstdio>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    vector<int> prime;
    int p[120000];
    int i,j;
    for(i=0;i<120000;i++){
        p[i]=i+2;
    }
    for(i=0;i<sqrt(120000);i++){
        if(!p[i]){continue;}
        for(j=i+1;j<120000;j++){
            if(!(p[j]%p[i])){p[j]=0;}
        }
    }
    for(i=0;i<120000;i++){
        if(p[i]){prime.push_back(p[i]);}
    }
    
    int start_val,cnt;
    while(1){
        scanf("%d",&start_val);
        scanf("%d",&cnt);
        if(start_val==-1&&cnt==-1){break;}
        vector<int>::iterator tg=upper_bound(prime.begin(),prime.end(),start_val);
        vector<int> st;

        for(i=0;i<cnt;i++){
            for(j=i;j<cnt&&j>=i;j++){
                st.push_back((*(tg+i))+(*(tg+j)));
            }
        }
        sort(st.begin(),st.end());
        printf("%d\n",st[cnt-1]);
    }
    return 0;
}