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 <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <complex>
#include <stack>
#include <set>
#include <map>
#

#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i,n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i,s,n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c,i,n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

typedef complex<int> POINT;
template <class T>
class vevector : public vector<vector<T>>
{
public:
	using vector<vector<T>>::operator []; 
	T& operator [](const POINT &index) { return (*this)[index.real()][index.imag()]; }
};

template <class T> T read() { T t; cin >> t; return t; }
template <> POINT read<POINT>() { int x, y; cin >> x >> y; return POINT(x, y); }
template <class T> vector<T> read(int n) { vector<T> v; REP(i,n) { v.push_back(read<T>()); } return v; }
template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i,n) v.push_back(read<T>(m)); return v; }
template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) { cout << t << ' ' << t2 << endl; }

template <class T> void write(const vector<T> &v)
{
	ostringstream ss; 
	for (auto x : v) ss << x << ' ';
	auto s = ss.str();
	cout << s.substr(0, s.length() - 1) << endl;
}

template <class T> string toString(const T &t) { stringstream ss; ss << t; return ss.str(); }

struct _Reader { template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } };
#define READ(t,...) t __VA_ARGS__; _Reader(), __VA_ARGS__
#define countof(a) (sizeof(a)/sizeof(a[0]))

void solve();
int main() { solve(); return 0; }

vector<unsigned char> isPrime;
vector<int> primes;
void initPrimes(int n)
{
	isPrime = vector<unsigned char>(n + 1, true);
	isPrime[0] = isPrime[1] = false;
	FOR(i, 2, n)
	{
		if (!isPrime[i]) continue;
		primes.push_back(i);
		for (int j = i * 2; j <= n; j += i)
			isPrime[j] = false;
	}
}

/// template end ///

void solve()
{
	initPrimes(200000);
	REP (testcase, INF)
	{
		READ(int, N, P);
		if (N < 0) { break; }

		vector<int> candidates;
		int startIndex = upper_bound(ALLOF(primes), N) - primes.begin();

		REP(i,P) REP(j,P) candidates.push_back( primes[i + startIndex] + primes[i + j + startIndex]);
		partial_sort(candidates.begin(), candidates.begin() + P, candidates.end());
		write(candidates[P - 1]);
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
int main(void){
    int prime[300010]={};
    int b=0;
    for(int i=2;i<=200274;i++){
        if(prime[i]==0){
            prime[i]=1;
            b++;
            for(int j=2*i;j<=200274;j+=i){
                prime[j]=-1;
            }
        }
    }
    vector<int> p(b);
    int a=0;
    for(int i=0;i<200274;i++){
        if(prime[i]==1){
            p[a]=i;
            a++;
        }
    }
    int s[200275];
    while(1){
        int n,m;
        cin>>n>>m;
        if(n==-1&&m==-1)break;
        for(int i=0;i<200275;i++)s[i]=0;
        int c;
        for(int i=0;i<200275;i++){
            if(p[i]>n){
                c=i;
                break;
            }
        }
        for(int i=c;i<=c+m;i++){
            for(int j=i;j<=c+m;j++){
                int sum=p[i]+p[j];
                if(sum<=200274)s[sum]++;
            }
        }
        int t=0;
        for(int i=4;i<200275;i++){
            t+=s[i];
            if(t>=m){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  int a[100000]={2,3},i,j,k,c=2,b[100002][200],q;
  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+13;j++){
      for(c=k;c<k+13;c++){
        b[i][q++]=a[j]+a[c];
      }
    }
    sort(b[i],b[i]+169);
  }
  while(cin>>i>>j){
    if(i<0)break;
    cout<<b[i][j]<<endl;
  }
}

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

vector<bool> prime(150000, true);

int main(){
  prime[0] = prime[1] = false;
  for(int i=2; i*i<150000; i++){
    if(prime[i]){
      for(int j=2; j*i<150000; j++){
        prime[i*j]=false;
      }
    }
  }
  
  while(1){
    int n, p;
    cin >> n >> p;
    if(n==-1) break;

    vector<int> prm_btn(50);
    int cnt=0;
    for(int i=n+1; cnt<50; i++){
      if(prime[i]){
        prm_btn[cnt]=i;
        cnt++;
      }
    }

    vector<int> matsuzaki;
    for(int i=0; i<50; i++){
      for(int j=i; j>=0; j--){
        matsuzaki.push_back(prm_btn[i]+prm_btn[j]);
      }
    }
    sort(matsuzaki.begin(), matsuzaki.end());

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