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):
#coding:utf-8


# ls = [-30,-10,10,20,50]

# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

#n,k=8,5
#ls=[-9,-7,-4,-3,1,2,3,4]

first = -1
for i,point in enumerate(ls):
    if point >= 0:
        first = i
        break
size=k+1
ans = []
for i in range(size):
    if first-size+i+1>=0 and first+i-1< len(ls):
        temp = [0,ls[first-size+i+1],ls[first+i-1]]
        temp = [abs(min(temp)),abs(max(temp))]
        ans.append((2*min(temp)+max(temp)))
print (min(ans))


#coding:utf-8

n,k = map(int,input().split())
ls = list(map(int,input().split()))


# ls = [-30,-10,10,20,50]

# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]

def set_max(ls):
    try:
        return set([max(ls)])
    except:
        return set()
def set_min(ls):
    try:
        return set([min(ls)])
    except:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

print (neighbor)
answer = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        print (ls[i:i+k])
        if sign(ls[i]*ls[i+k-1])<0:
            temp = [abs(ls[i]),abs(ls[i+k-1])]
            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(2*max(temp))
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []

#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))


#coding:utf-8
import numpy as np
# n,k = map(int,input().split())
# ls = list(map(int,input().split()))


# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

n,k=8,5
ls=[-9,-7,-4,-3,1,2,3,4]



def set_max(ls):
    if len(ls)>0:
        return set([max(ls)])
    else:
        return set()
def set_min(ls):
    if len(ls)>0:
        return set([min(ls)])
    else:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
cnt = 0
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:

        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
        cnt += 1
        if cnt == k + 1:
            break
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []

#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))


first = -1
for i,point in enumerate(ls):
    if point >= 0:
        first = i
        break
size=k+1
ans = []
for i in range(size):
    if first-size+i+1>=0 and first+i-1< len(ls):
        temp = [0,ls[first-size+i+1],ls[first+i-1]]
        temp = [abs(min(temp)),abs(max(temp))]
        ans.append((2*min(temp)+max(temp)))
print (min(ans))

#coding:utf-8

n,k = map(int,input().split())
ls = list(map(int,input().split()))


# ls = [-30,-10,10,20,50]

# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]

def set_max(ls):
    try:
        return set([max(ls)])
    except:
        return set()
def set_min(ls):
    try:
        return set([min(ls)])
    except:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

print (neighbor)
answer = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        print (ls[i:i+k])
        temp = [0,ls[i],ls[i+k-1]]
        temp = [abs(min(temp)),abs(max(temp))]
        answer.append((2*min(temp)+max(temp)))
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []

#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))


#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))


# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]

def set_max(ls):
    if len(ls)>0:
        return set([max(ls)])
    else:
        return set()
def set_min(ls):
    if len(ls)>0
        return set([min(ls)])
    else:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []

#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))


#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))


# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]
if not (0 in ls):
    ls.append(0)
    k+=1
#print (ls)

ls = np.array(ls)
ls.sort()
#print (ls)

idx = np.where(ls==0)[0][0]
#print (idx,k)
#print (idx+k)
start = max(0,idx-k)
goal = min(idx+k,len(ls)-1)

#print (start,goal)
answer = []
for i in range(goal-(k-1)+1):
    #print (i,i+k-1)
    mi = min(abs(ls[i]),abs(ls[i+k-1]))
    ma = max(abs(ls[i]),abs(ls[i+k-1]))
    #print (mi,ma)
    answer.append(2*mi+ma)
print (min(answer))

#     if len(ls)>0:
#         return set([max(ls)])
#     else:
#         return set()

#     if len(ls)>0:
#         return set([min(ls)])
#     else:
#         return set()
#
#

# minus = list(filter(lambda n:n<=0, ls))
#

#
# #print (neighbor)
# answer = []
# cnt = 0

#     if len(set(ls[i:i+k])&neighbor)>0:
#
#         #print (ls[i:i+k])
#         #print (np.sign(ls[i]*ls[i+k-1]))
#         temp = [abs(ls[i]),abs(ls[i+k-1])]
#         if np.sign(ls[i]*ls[i+k-1])<0:
#
#             answer.append(2*min(temp)+max(temp))
#         else:
#             answer.append(max(temp))
#         cnt += 1

#             break
# print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []

#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Accepted