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 <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;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct team{
	  int id;
	  int ac,time;
	  vector<int> wa;
	  team(int i, int a, int t, int P){
			id = i; ac = a; time = t;
			wa = vector<int>(P+1,0);
	  }
	  bool operator < (const team& tgt) const {
			if( ac != tgt.ac )
				  return ac < tgt.ac;
			if( time != tgt.time )
				  return time > tgt.time;
			return id < tgt.id;
	  }

};

int main(){
	  while(1){
			int M,T,P,R; cin >> M >> T >> P >> R;
			if(!M) break;
			vector<team> teams;
			for(int i=0; i<T; i++)
				  teams.push_back( team(i+1,0,0,P) );
	  
			for(int i=0; i<R; i++){
				  int m,t,p,j;
				  cin >> m >> t >> p >> j;
				  if( j == 0 ){
						teams[ t-1 ].ac ++;
						teams[ t-1 ].time += m;
						teams[ t-1 ].time += teams[ t-1 ].wa[ p ]*20 ;
						
				  }
				  else{
						teams[ t-1 ].wa[ p ]++;
				  }
			}
			sort(teams.begin(),teams.end());
			reverse(teams.begin(),teams.end());
			
			team pre = team(-1,-1,-1,P);
			for(int i=0; i<T; i++){
				  team corr = teams[i];
				  if( pre.ac == corr.ac and pre.time == corr.time )
						cout << "=" << corr.id;
				  else
						if(i!=0)
							  cout << "," << corr.id;
						else
							  cout << corr.id;
				  pre = corr;
			}
			cout << endl;
	  
	  }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
typedef long long ll;
typedef pair<int, int> P;

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

int main(){
  int N, p;
  while(cin >> N >> p && (N != -1 || p != -1)){
    vector<int> primes;
    vector<int> nums;
    while(primes.size() < 20){
      if(isprime(++N)) primes.push_back(N);
    }
    rep(i, (int)primes.size())rep(j, i + 1){
      nums.push_back(primes[i] + primes[j]);
    }
    sort(nums.begin(), nums.end());
    cout << nums[p-1] << endl;
  }
  return 0;
}

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

using namespace std;

vector<int> primes;

void init_primes() {
    for (int n=2; n<=200000; n++) {
        bool is_prime = true;
        for (vector<int>::iterator iter=primes.begin(); iter!=primes.end(); iter++) {
            if (n%(*iter) == 0) {
                is_prime = false;
                break;
            }
            if ((*iter)*(*iter) > n) {
                break;
            }
        }
        if (is_prime) {
            primes.push_back(n);
        }
    }
}

int m(int n, int p) {
    vector<int>::iterator base = lower_bound(primes.begin(), primes.end(), n+1);
    vector<int> sum_;
    for (int i=0; i<p; i++) {
        for (int j=i; i+j<p; j++) {
            sum_.push_back(*(base+i)+*(base+j));
        }
    }
    sort(sum_.begin(), sum_.end());
    return *(sum_.begin()+p-1);
}

int main() {
    int n, p;
    init_primes();
    while (true) {
        cin >> n >> p;
        if (n < 0) {
            break;
        }
        cout << m(n, p) << endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
#define N 105000
int main() {
	vector<bool>f(N, true);
	f[0] = false;
	f[1] = false;
	vector<int>prime;
	for (int i = 2; i <= sqrt(N); i++) {
		if (f[i]) {
			for (int j = 2; i*j < N; j++) {
				f[i*j] = false;
			}
		}
	}
	for (int i = 0; i < N; i++) {
		if (f[i]) {
			prime.push_back(i);
		}
	}

	int n, p;
	while (cin >> n >> p, p != -1) {
		auto it = upper_bound(prime.begin(), prime.end(), n);
		vector<int>ans(10000,100000000);

		for (int cnt = 0, i = 0; i < 100; i++) {
			for (int j = i; j < 100; j++) {
				ans[cnt++] = *(it + i) + *(it + j);
			}
		}
		sort(ans.begin(), ans.end());

		cout << ans[p - 1] << endl;


	}
	return 0;
}