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 = int(input())
b = list(map(int,input().split()))

#b = list(set(a))
b.sort()
print(b)
 
if len(b) > 1:
    l = len(b)//2
    print(b[l])
else:
    print(b[0])
        

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

x = list(map(int,input().split()))

x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)


if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx + k])
    if tmp < min :
        min = tmp

/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= len(x):
        m = abs(x[plsIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

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

x = list(map(int,input().split()))

x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)


if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx - k])
    if tmp < min :
        min = tmp

/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= 0:
        m = abs(x[misIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

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

x = list(map(int,input().split()))

x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)


if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx + k])
    if tmp < min :
        min = tmp

/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= 0:
        m = abs(x[plsIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

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

x = list(map(int,input().split()))

x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)


if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx - k])
    if tmp < min :
        min = tmp

/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= 0:
        m = abs(x[plsIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

n = int(input())
b = list(map(int,input().split()))

#b = list(set(a))
b.sort()
 
if len(b) > 1:
    l = len(b)//2
    print(b[l])
else:
    print(b[0])
        

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

x.append(0)
x.sort()

#print(x)

min = 10**10

zeroIdx = x.index(0)

for i in range(zeroIdx-k,zeroIdx+1):
    if i < 0 : 
        continue
    elif len(x) <= i + k :
        break

    plsIdx = i + k 
    misIdx = i

    #print(misIdx,plsIdx,x[misIdx],x[plsIdx])

    if abs(x[plsIdx]) > abs(x[misIdx]) :
        tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
    else:
        tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])

    if tmp < min :
        min = tmp

print(min)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted