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 <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <climits>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

vector<bool> sieve;
vector<int> primes;

void make_primes(int n) {
	sieve = vector<bool>(n, true);
	sieve[1] = false;
	for (int i = 4; i < n; i += 2)
		sieve[i] = false;
	for (int i = 9; i < n; i += 6)
		sieve[i] = false;
	for (int i = 6, e = sqrt(n); i <= e; i += 6) {
		for (int j : {i - 1, i + 1})
			if (sieve[j])
				for (int k = j * j; k < n; k += j)
					sieve[k] = false;
	}
	primes = {2, 3};
	for (int i = 6 ; i < n; i += 6)
		for (int j : {i - 1, i + 1})
			if (sieve[j])
				primes.push_back(j);
}

struct min_list {
	int max_size, size = 0;
	multiset<int> s;
	min_list(int s) {
		max_size = s;
	}
	bool insert(int n) {
		if (size < max_size) {
			s.insert(n);
			size++;
			return true;
		}
		auto r = max_element(s.begin(), s.end());
		if (n <= *r) {
			s.erase(r);
			s.insert(n);
			return true;
		} else
			return false;
	}
	int max() {
		return s.empty() ? INT_MAX : *s.rbegin();
	}
};

int main() {
	make_primes(110000);
	for (int n, p; n = in(), p = in(), n != -1;) {
		auto s = upper_bound(primes.begin(), primes.end(), n);
		min_list l(p);
		for (auto i = s; l.insert(*i + *i); i++)
			for (auto j = i + 1; l.insert(*i + *j) ;j++)
				;
		outl(l.max());
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {

    int n, p;
    bool is_prime[1000001];
    vector<int> prime;

    fill(is_prime, is_prime + 1000001, true);

    for(int i = 2; i <= sqrt(1000000); ++i) {
        if(is_prime[i]) {
            for(int j = i * 2; j <= 1000000; j += i) {
                is_prime[j] = false;
            }
        }
    }

    for(int i = 2; i <= 1000000; ++i) {
        if(is_prime[i]) {
            prime.push_back(i);
        }
    }

    while(cin >> n >> p && n != -1 && p != -1) {

        priority_queue<int, vector<int>, greater<int> > pq;
        vector<int>::iterator it = upper_bound(prime.begin(), prime.end(), n);

        for(int i = 0; i < p; ++i) {
            for(int j = i; j < p; ++j) {
                pq.push(*(it + i) + *(it + j));
            }
        }

        for(int i = 0; i < p - 1; ++i) {
            pq.pop();
        }

        cout << pq.top() << endl;

    }

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main ()
{
    int n, p;
    vector<bool> num(1000001, true);
    vector<int> prim;
    num[0] = num[1] = false;
    for (int i = 2; i <= 1000000; i++) {
        for (int j = 2; num[i] && i * j <= 1000000; j++) {
            num[i * j] = false;
        }
        if (num[i]) prim.push_back(i);
    }

    while (cin >> n >> p, !(n < 0 || p < 0)) {
        vector<int> v;
        int s = upper_bound(prim.begin(), prim.end(), n) - prim.begin();
        if (prim[s] <= n) s++;
        for (int i = 0; i <= p; i++) {
            for (int j = i; j <= p; j++) {
                v.push_back((prim[s + i] + prim[s + j]));
            }
        }

        sort(v.begin(), v.end());
        cout << v[p-1] << endl;
    }

    return 0;
}