Problem: There are N candles placed on a number line. The i-th candle from the left is placed on coordinate x_i. Here, x_1 < x_2 < ... < x_N holds.  Initially, no candles are burning. Snuke decides to light K of the N candles.  Now, he is at coordinate 0. He can move left and right along the line with speed 1. He can also light a candle when he is at the same position as the candle, in negligible time.  Find the minimum time required to light K candles.

Submission attempts (in order):
N,K = map(int,input().split())
L = list(map(int,input().split()))
Lplus = list(filter(lambda x: x>=0, L))
Lminus = list(filter(lambda x: x<0 ,L))[::-1]
ans_table = []
#print(L)
#right_only
if len(Lplus) >= K:
    ans_table.append(Lplus[K-1])
#left_only
if len(Lminus) >= K:
    ans_table.append(Lminus[K-1])
#once_turn_right_to_left
for i in range(len(Lplus)-1):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if remain <= len(Lminus):
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)
#once_turn_left_to_right
Lplus,Lminus = Lminus,Lplus
for i in range(len(Lplus)-1):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if K - i <= len(Lminus):
 
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)

#print(ans_table)
print(min(ans_table))



N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
dis.append(Lright[K-1])
dis.append(Lleft[K-1])


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    d += Lleft[remain-1]
    dis.append(d)
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    d += Lright[remain-1]
    dis.append(d)
print(min(dis))


N, K   = map(int, input().split())
L = list(map(int, input().split()))
L.append(0)
L.sort()
dis = []
for i in range(N-K+1):
    if 0 in L[i:i+K+1]:
        l = L[i:i+K+1]
        print(l)
        d = abs(l[0]) + abs(l[-1])*2
        dis.append(d)
        d = abs(l[0])*2 + abs(l[-1])
        dis.append(d)
print(min(dis))


N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
try :
    dis.append(Lright[K-1])
    dis.append(Lleft[K-1])
except:
    pass


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    try :
        d += Lleft[remain-1]
        dis.append(d)
    except:
        pass
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    try:
        d += Lright[remain-1]
        dis.append(d)
    except:
        pass
print(min(dis))


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted