Problem:


Problem B: Matsuzaki Number

Matsuzaki 教授は，宇宙の真理を研究している科学者である．人生，宇宙，すべての答えは 42 であると言われているが，Matsuzaki 教授はこれだけでは宇宙の真理を解明するには不十分であると考えている．Matsuzaki 教授は，宇宙の真理は 2 つのパラメータからなる関数で表されると考えており，42 はその 1 つに過ぎないというのである．

Matsuzaki 教授の定義した関数 M(N, P) は，N より大きい素数を 2 つ選んで（同じ数を 2 つでも構わない）和をとることで得られる数の全体を，小さいほうから順番に並べたときに，P 番目に現れる数を表す．ここで，2 通り以上の和で表されるような数も存在するが，そういった数は和の組み合わせの数と同じ個数だけ並べられる．

例として N = 0 の場合を考えよう．このときは素数全体から 2 つを選んで和をとることになる．そういった和のうちで最小の数を考えると，同じ数を 2 回選ぶことも許されていることから，2 + 2 = 4 であることがわかる．すなわち M(0, 1) = 4 である．次に小さい数は 2 + 3 = 5 であるから M(0, 2) = 5 となる．同様にして考えると，和を並べたものは 4, 5, 6, 7, 8, 9, 10, 10, 12, 13, 14, 14, 16, ... のようになることがわかる．すなわち，たとえば M(0, 9) = 12 である．

同じようにして N = 10 の場合を考えると，このときは 10 より大きい素数 {11, 13, 17, 19, ...} から 2 つを選ぶことになり，得られる和を小さいほうから並べると 22, 24, 26, 28, 30, 30, 32, ... のようになる．

あなたの仕事は，N と P が与えられた時に M(N, P) を計算するプログラムを書くことである．

Input

入力は複数のデータセットからなる．データセットは 1 行であり，2 つの整数 N (0 ≤ N ≤ 100,000) と P (1 ≤ P ≤ 100) が 1 つの空白で区切られて与えられる．

入力の終わりは，空白で区切られた 2 つの -1 を含む 1 行で示される．

Output

各データセットに対して，M(N, P) の値を 1 行に出力せよ．出力に余計な空白や改行を含めてはならない．

Sample Input

0 55
0 1
0 2
0 3
10 1
10 2
10 3
10 4
10 5
10 6
11 1
11 2
11 3
100000 100
-1 -1

Output for the Sample Input

42
4
5
6
22
24
26
28
30
30
26
30
32
200274



Sample input:
0 55
0 1
0 2
0 3
10 1
10 2
10 3
10 4
10 5
10 6
11 1
11 2
11 3
100000 100
-1 -1


Sample output:
42
4
5
6
22
24
26
28
30
30
26
30
32
200274

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
import itertools

def sieve_of_erastosthenes(num):
    input_list = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)]
    input_list[0] = input_list[1] = False
    input_list[2] = input_list[3] = input_list[5] = True
    sqrt = math.sqrt(num)

    for serial in range(3, num, 2):
        if serial >= sqrt:
            return input_list
        for s in range(serial ** 2, num, serial): 
            input_list[s] = False

primeTable = sieve_of_erastosthenes(10**6)

while True:
    N,P = map(int,input().split(" "))
    if N == -1 and P == -1:
        break
    start = N
    while primeTable[start] is False:
        start += 1
    prime_list = []
    count = 0
    i = 0
    while count != P:
        if primeTable[start + i]:
            prime_list.append(start+i)
            count += 1
        i += 1
    print(sorted([sum(t) for t in itertools.combinations_with_replacement(prime_list,2)])[P-1])

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = []
	while len(prime) < 20:
		if p[i-1] == 1:
			prime.append(i)
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*25
	j = 0
	while j < 24:
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*21
	j = 0
	while j < 21:
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*(P/5 + 4)
	j = 0
	while j < (P/5 + 4):
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*(P/5 + 5)
	j = 0
	while j < (P/5 + 5):
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*(P/5 + 6)
	j = 0
	while j < (P/5 + 6):
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*(6 + P/6)
	j = 0
	while j < (6 + P/6):
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Wrong Answer

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*(7 + P/6)
	j = 0
	while j < (7 + P/6):
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
from itertools import combinations_with_replacement as cwr

MAX = 110000
ROOT = 340
tf = [True] * MAX
tf[0] = tf[1] = False
for i in range(2, ROOT + 1):
  if tf[i]:
    for j in range(i * i, MAX, i):
      tf[j] = False
primes = [i for i in range(MAX) if tf[i]]

while True:
  n, p = map(int ,input().split())
  if n == -1:break
  init = br(primes, n)
  choice = primes[init:init+100]
  lst = []
  for i, j in cwr(choice, 2):
    lst.append(i + j)
  lst.sort()
  print(lst[p - 1])

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
from itertools import combinations_with_replacement as cwr

MAX = 110000
ROOT = 340
tf = [True] * MAX
tf[0] = tf[1] = False
for i in range(2, ROOT + 1):
  if tf[i]:
    for j in range(i * i, MAX, i):
      tf[j] = False
primes = [i for i in range(MAX) if tf[i]]

while True:
  n, p = map(int ,input().split())
  if n == -1:break
  init = br(primes, n)
  choice = primes[init:init+p]
  lst = []
  for i, j in cwr(choice, 2):
    lst.append(i + j)
  lst.sort()
  print(lst[p - 1])

Submission status: Accepted

Solution code:
prime = [True for i in range(200001)]
prime[0] = False
prime[1] = False
p = 2
while p * p <= 200000:
    if prime[p]:
        for i in range(p * 2, 200001, p):
            prime[i] = False
    p += 1

while True:
    N, P = map(int, input().split())
    if N < 0:
        exit()
    li = []
    for i in range(N + 1, N + 10001):
        if prime[i]:
            li.append(i)
    li.sort()
    ans = []
    for i in range(100):
        for j in range(i, 100):
            ans.append(li[i] + li[j])
    print(sorted(ans)[P - 1])

Submission status: Accepted

Solution code:
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):  # 確かめるのはnの平方根までで大丈夫
        if not is_prime[i]:  # すでにfalseなら次のiへ
            continue
            """
            bool(not False)はTrueになる
            """
        for j in range(i * 2, n + 1, i):  # iずつ大きくなる(つまりiの倍数をみる)
            is_prime[j] = False  # i自身を除くiの倍数を全てFalseにする
    return [i for i in range(n + 1) if is_prime[i]]  # Trueのものだけ列挙して返す


while 1:
    N, P = map(int, input().split())
    if N == -1:
        exit()
    prime_numbers = [i for i in primes(120000) if i > N]

    prime_wa = []
    for i in range(P+1):
        for j in range(i, int(P+1)):
            prime_wa.append(prime_numbers[i]+prime_numbers[j])

    prime_wa.sort()

    print(prime_wa[P-1])

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        prime = self.sieve(110000)
        while True:
            n, p = map(int, raw_input().split())
            if n == -1 and p == -1:
                break
            l = [e for e in prime if e > n]
            s = []
            for i in range(100):
                for j in range(100):
                    if i < j:
                        continue
                    s.append(l[i] + l[j])
            s.sort()
            print s[p-1]
            
        
        return None

    def sieve(self, n):
        f = [True for i in xrange(n+1)]
        f[0] = False
        f[1] = False
        i = 2
        while i * i <= n:
            if f[i]:
                j = 2
                while i * j <= n:
                    f[i * j] = False
                    j += 1
            i += 1
        return [i for i in xrange(n+1) if f[i]]
        

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:
sieve = [True] * 110000
primes = []
for i in range(2, 110000):
    if sieve[i] == True:
        primes.append(i)
        for j in range(2*i, 110000, i):
            sieve[j] = False

while True:
    n, p = map(int, input().split())
    if n == -1 and p == -1: break
    cnt = 0
    for i in primes:
        if n < i: 
            break
        else: 
            cnt += 1
    psum = sorted([primes[i]+primes[j] for j in range(cnt, cnt+p) for i in range(j, cnt+p)])
    print(psum[p-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
import itertools

def sieve_of_erastosthenes(num):
    input_list = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)]
    input_list[0] = input_list[1] = False
    input_list[2] = input_list[3] = input_list[5] = True
    sqrt = math.sqrt(num)

    for serial in range(3, num, 2):
        if serial >= sqrt:
            return input_list
        for s in range(serial ** 2, num, serial): 
            input_list[s] = False

primeTable = sieve_of_erastosthenes(10**6)

while True:
    N,P = map(int,input().split(" "))
    if N == -1 and P == -1:
        break
    start = N+1
    while primeTable[start] is False:
        start += 1
    prime_list = []
    count = 0
    i = 0
    while count != P:
        if primeTable[start + i]:
            prime_list.append(start+i)
            count += 1
        i += 1
    print(sorted([sum(t) for t in itertools.combinations_with_replacement(prime_list,2)])[P-1])

Submission status: Accepted

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(2*10**5)

while 1:
	N,P = map(int,raw_input().split())
	if (N,P) == (-1,-1):
		break
	prime_ub = P
	prime_list = []
	prime_num = 0
	inc = N + 1
	while prime_num < prime_ub :
		if prime[inc] == 1:
			prime_num += 1
			prime_list.append(inc)
		inc += 1
	sum_list = []
	for i in range(prime_num):
		sum_list += [prime_list[i] + p for p in prime_list[i:]]
	sum_list.sort()
	print sum_list[P-1]

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

used = [0]*110000
used[0] = 1
used[1] = 1
for i in range(2, 110000):
    if used[i] == 0:
        for j in range(i*2, 110000, i):
            used[j] = 1

(n, p) = map(int, raw_input().split())
while n!=-1:
    prime = []
    cnt = 0
    cur = n+1 
    while cnt <= 100:
        if used[cur] == 0:
            cnt += 1
            prime.append(cur)
        cur += 1
    padd = []
    for i in range(len(prime)):
        for j in range(i, len(prime)):
            padd.append(prime[i]+prime[j])
    padd.sort()
    print padd[p-1]

    (n, p) = map(int, raw_input().split())

Submission status: Accepted

Solution code:

primes = [True] * (150000)
primes[0] = primes[1] = False
for i in range(2, int(len(primes)**0.5)+1):
	if primes[i]:
		for j in range(i*2, len(primes), i):
			primes[j] = False

def m(n, p):
	ns = []
	for i in range(n+1, n+p+3000):
		if primes[i]: ns.append(i)
	r = []
	for i in range(len(ns)):
		for j in range(i, len(ns)):
			r.append(ns[i]+ns[j])
	return sorted(r)[p-1]

while True:
	n, p = map(int, input().split())
	if (n, p) == (-1, -1):
		break
	print(m(n, p))

Submission status: Accepted

Solution code:
import bisect
pr=[]
for i in range(2,110000):
    for j in range(2,int(i**0.5)+1):
        if not i%j:break
    else:pr+=[i]
while 1:
    n,p=map(int,input().split())
    if n<0:break
    a=bisect.bisect_right(pr,n)
    print(sorted([pr[i]+pr[j] for i in range(a,a+p) for j in range(i,a+p)])[p-1])

Submission status: Accepted

Solution code:
import bisect
pr=[]
for i in range(2,110000):
    for j in range(2,int(i**0.5)+1):
        if not i%j:break
    else:pr+=[i]
while 1:
    n,p=map(int,input().split())
    if n<0:break
    n=bisect.bisect_right(pr,n)
    print(sorted([pr[i]+pr[j] for i in range(n,n+p) for j in range(i,n+p)])[p-1])

Submission status: Accepted

Solution code:
import sys
from math import sqrt
from bisect import bisect

inf = 1<<30

def solve():
    primes = eratos(2 * 10**5)

    while 1:
        N, P = map(int, sys.stdin.readline().split())

        if N == P == -1:
            return

        j = bisect(primes, N)

        lprimes = primes[j:j + 100]

        made = []

        for i in range(100):
            for j in range(i, 100):
                made.append(lprimes[i] + lprimes[j])

        made.sort()

        ans = made[P - 1]

        print(ans)


def eratos(n):
    s = [True] * n
    s[0] = s[1] = False

    for p in range(2, int(sqrt(n)) + 1):
        if s[p]:
            for q in range(p*p, n, p):
                s[q] = False

    return [i for i in range(2, n) if s[i]]

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import bisect
MAX_N = 120000
sieve = [0,0] + [1] * MAX_N
n = 2
primes = []
while n*n < MAX_N:
    if sieve[n]:
        primes.append(n)
        for m in range(2*n,MAX_N,n):
            sieve[m] = 0
    n += 1
for m in range(n,MAX_N):
    if sieve[m]: primes.append(m)

while True:
    N,P = map(int,input().split())
    if N == -1: break
    i = bisect.bisect_right(primes, N)
    li = []
    for p1 in range(i,i+P):
        for p2 in range(p1,i+P+1):
            li.append(primes[p1] + primes[p2])
    print(sorted(li)[P-1])

Submission status: Accepted

Solution code:
n=[i for i in range(100272)]
n[:2]=[0,0]
for i in range(2, 100272//2):
    if n[i]==i:n[i*2::i]=[0 for e in range(len(n[i*2::i]))]
while True:
    M,P=[int(i) for i in input().split()]
    if M==-1 and P==-1:break
    p=list(filter(lambda x:x!=0,n[M+1:]))[:25]
    l=[]
    for i in range(len(p)):
        for j in range(i+1):
            l.append(p[i]+p[j])
    print(sorted(l)[P-1])

Submission status: Accepted

Solution code:
prime = [True for i in range(160001)]
prime[0] = prime[1] = False
for i in range(2, 401):
    if prime[i]:
        for j in range(2, 160000 // i + 1):
            prime[i * j] = False

while True:
    n, p = map(int, input().split())
    if n == p == -1:
        break

    pli = list()
    for i in range(p):
        while True:
            n += 1
            if prime[n]:
                pli.append(n)
                break

    numli = list()
    for x in range(p):
        for y in range(x, p):
            numli.append(pli[x] + pli[y])

    numli.sort()
    print(numli[p - 1])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, int(math.sqrt(m)) + 1):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False

    def is_prime(self, n):
        return self.A[n]

def main():
    rr = []

    pr = Prime(10**11)

    while True:
        n,p = LI()
        if n < 0 and p < 0:
            break
        t = []
        i = n
        while len(t) < p:
            i += 1
            if pr.is_prime(i):
                t.append(i)

        u = []
        for i in range(p):
            for j in range(i,p):
                u.append(t[i]+t[j])
        u.sort()
        rr.append(u[p-1])

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
import sys
import bisect

MAX = 2000000
prime = [-1] * MAX
prime_list = []
i = 2
while MAX > i:
	if prime[i - 1] == -1:
		prime[i - 1] = 0
		temp = i * 2
		prime_list.append(i)
		while temp < MAX:
			prime[temp - 1] = 1
			temp += i
	if i == 2:
		i += 1
	else:
		i += 2

while True:
    N, P = map(int, sys.stdin.readline().split())
    
    if N == P == -1:
        break
    
    start = bisect.bisect_right(prime_list, N)
    candidate = []
    for i in xrange(start, min(start + 101, MAX)):
        for j in xrange(i, min(start + 101, MAX)):
            candidate.append(prime_list[i] + prime_list[j])
    candidate.sort()
    print candidate[P - 1]

Submission status: Accepted

Solution code:
import math
from bisect import bisect
N=101000
prime=[1]*(N+1)
prime[0]=0
prime[1]=0
for i in range(2,int(math.sqrt(N))):
    if prime[i]==1:
        j=i
        while i*j<=N:
            prime[i*j]=0
            j+=1
prime=[i for i in range(N+1) if prime[i]==1]
while True:
    n,p=map(int,raw_input().split())
    if n==p==-1:break
    idx=bisect(prime,n)
    S=set()
    for i in prime[idx:idx+p+1]:
        for j in prime[idx:idx+p+1]:
            if i>j:
                S.add((j,i))
            else:
                S.add((i,j))
    L=[i+j for i,j in S]
    L.sort()
    print L[p-1]

Submission status: Accepted

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = []
	while len(prime) < 30:
		if p[i-1] == 1:
			prime.append(i)
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*25
	j = 0
	while j < 25:
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*24
	j = 0
	while j < 24:
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*23
	j = 0
	while j < 23:
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*22
	j = 0
	while j < 22:
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
import itertools
r = 110000
s = int(r*0.5)
p = [1] * r
p[0] = 0
for i in range(1,s):
	if p[i]:
		p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)]
		
while 1:
	N,P = map(int,raw_input().split())
	if N == -1: break
	i = N + 1
	prime = [0]*(13 + P/10)
	j = 0
	while j < (13 + P/10):
		if p[i-1] == 1:
			prime[j] = i
			j += 1
		i += 1
	wa = sorted([sum(comb) for comb in itertools.combinations(prime,2)] + [2*a for a in prime])
	print wa[P-1]

Submission status: Accepted

Solution code:
from bisect import bisect_left

# n >= 6
def sieve_fast(n):
    correction = (n%6>1)
    n = {0:n,1:n-1,2:n+4,3:n+3,4:n+2,5:n+1}[n%6]
    sieve = [True] * (n//3)
    sieve[0] = False
    for i in range(int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      ((k*k)//3)      ::2*k]=[False]*((n//6-(k*k)//6-1)//k+1)
        sieve[(k*k+4*k-2*k*(i&1))//3::2*k]=[False]*((n//6-(k*k+4*k-2*k*(i&1))//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

ps = sieve_fast(200010)
while True:
  N,P = map(int,input().split())
  if N < 0:
    break
  pos = bisect_left(ps,N+1)
  L = []
  for i in range(110):
    for j in range(i,110):
      L.append(ps[pos+i]+ps[pos+j])
  L.sort()
  print(L[P-1])

Submission status: Accepted

Solution code:
def pri(k):
    if k==1:return False
    elif k==2: return True
    for i in range(2,int(k**0.5)+1):
        if k%i==0:
            return False
    return True
while True:
    n,p=map(int,input().split())
    if n==-1:
        exit()
    lprime=[]
    while len(lprime)!=p:
        n+=1
        if pri(n):
            lprime.append(n)
    lans=[]
    le=len(lprime)
    for k1 in range(le):
        for k2 in range(k1,le):
            lans.append(lprime[k1]+lprime[k2])
    lans.sort()
    print(lans[p-1])

Submission status: Accepted

Solution code:
def eratosthenes(n):
    length = 150000
    ret = []
    flag = [True] * length
    for i in range(2, length):
        if flag[i]:
            if n < i:
                ret.append(i)
            for j in range(i, length, i):
                flag[j] = False
        if len(ret) > 50:
            break
    return ret


if __name__ == "__main__":
    while True:
        N, P = map(int, input().split())
        if N == -1 and P == -1:
            break
        ret = eratosthenes(N)
        sosu_add = []
        for i in range(len(ret)):
            for j in range(i, len(ret)):
                sosu_add.append(ret[i] + ret[j])
        sosu_add.sort()
        print(sosu_add[P - 1])

Submission status: Accepted

Solution code:
def prime(n):
    primes = [True] * (n+1)
    primes[0] = False
    primes[1] = False

    for i in range(2, int(n**0.5)+1):
        if not primes[i]:
            continue

        for j in range(i*2, n+1, i):
            primes[j] = False

    return [i for i in range(n+1) if primes[i]]


while True:
    N, P = map(int, input().split())  # Nより大きい素数, 和の小さな方がP番目

    if N == -1:
        break

    prime_numbers = [i for i in prime(120000) if i > N]

    hoge = []
    for i in range(P+1):
        for j in range(i, P+1):
            hoge.append(prime_numbers[i] + prime_numbers[j])

    hoge.sort()

    print(hoge[P-1])

Submission status: Accepted

Solution code:
from itertools import *
N=101198;n=range(N);a=list(n);a[1]=0
for i in range(2,int(N**.5)+1):a[i*2::i]=[0]*len(a[i*2::i])

for e in iter(input, '-1 -1'):
    qN, qP = map(int, e.split())
    p=[x for x in a[qN+1:]if x][:qP+1]
    print(sorted(p[i]+p[j]for i in range(qP)for j in range(i,qP))[qP-1])

Submission status: Accepted

Solution code:
def eratosthenes(n):
    prime_table = [False,False,True]+[False if i%2!=0 else True for i in range(n-2)]
    i=3
    pn=[2]
    while i*i<=n:
        if prime_table[i]:
            j=i*i
            pn.append(i)
            while j<=n:
                prime_table[j]=False
                j+=i
        i+=2
    for j in range(i,n+1):
        if prime_table[j]:
            pn.append(j)
    return pn

prime_numbers=eratosthenes(200000)
while 1:
    n,p=map(int,input().split())
    if n==-1:
        break
    i=0
    while prime_numbers[i]<=n:
        i+=1
    d=[]
    for j in range(i,i+30):
        for k in range(j,i+30):
            d.append(prime_numbers[j]+prime_numbers[k])
    d.sort()
    print(d[p-1])

Submission status: Runtime Error

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(2*10**5)

while 1:
	N,P = map(int,raw_input().split())
	if (N,P) == (0,0):
		break
	prime_ub = P
	prime_list = []
	prime_num = 0
	inc = N + 1
	while prime_num < prime_ub :
		if prime[inc] == 1:
			prime_num += 1
			prime_list.append(inc)
		inc += 1
	sum_list = []
	for i in range(prime_num):
		sum_list += [prime_list[i] + p for p in prime_list[i:]]
	sum_list.sort()
	print sum_list[P-1]

Submission status: Runtime Error

Solution code:
def getLargePrime(n,k):
    primes=[2]
    result=[]
    if n<2: result.append(2)
    i=3
    while True:
        isValid=True
        for e in primes:
            if i%e==0:
                isValid=False
                break
            if i<e**2: break
        if isValid: primes.append(i)
        if isValid and n<i: result.append(i)
        if len(result)>k: break
        i+=2
    return result

while True:
    n,p=map(int,raw_input().split())
    if n==-1: break
    ps=getLargePrime(n,p)
    re=[]
    for i in xrange(len(ps)):
        for j in xrange(i,len(ps)):
            re.append(ps[i]+ps[j])
    re=sorted(re)
    print re[p-1]