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):
import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
cnt = min(-sum(a[zero-k-1:zero]),sum(a[zero:zero+k+1]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k]:
        cnt = -2*a[zero-i]+a[zero+k]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+k]:
        cnt = -a[zero-k+i]+2*a[zero+k]
    else:
        break
print(cnt)

import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
cnt = 0
a.sort()
zero = bisect.bisect_left(a,0)
for i in range(zero):
    if cnt > -2*sum(a[0:zero])+sum(a[zero:n-zero]):
        cnt = -2*sum(a[0:zero])+sum(a[zero:n-zero])
    else:
        break
for i in range(zero):
    if cnt > -sum(a[0:zero])+2*sum(a[zero:n-zero]):
        cnt = -sum(a[0:zero])+2*sum(a[zero:n-zero])
    else:
        break
print(c)

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,abs(a[i]+a[i+k-1]\
          -abs(min(a[i],a[i+k-1]))))
print(c)


import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
cnt = min(-sum(a[zero-k+1:zero]),sum(a[zero:zero+k+1]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k]:
        cnt = -2*a[zero-i]+a[zero+k]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+k]:
        cnt = -a[zero-k+i]+2*a[zero+k]
    else:
        break
print(cnt)

import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
cnt = (10**8)*(10**5)
cnt = min(sum(a[zero:n]),-sum(a[0:zero]))
for i in range(1,zero):
    if cnt > -2*a[zero-i]+a[n-1-i]:
        cnt = -2*a[zero-i]+a[n-1-i]
    else:
        break
for i in range(1,zero):
    if cnt > -a[i]+2*a[zero+i-1]:
        cnt = -a[i]+2*a[zero+i-1]
    else:
        break
print(cnt)

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k):
    c=min(c,a[i]+a[i+k-1]-abs(min(a[i],a[i+k-1])))
print(c)

import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
cnt = min(-sum(a[zero-k:zero]),sum(a[zero:zero+k+1]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k-i-1]:
        cnt = -2*a[zero-i]+a[zero+k-i-1]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+k-1]:
        cnt = -a[zero-k+i]+2*a[zero+k-1]
    else:
        break
print(cnt)

import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
Z = min(n-zero,zero,k)
cnt = (10**8)*(10**5)
for i in range(Z):
    if cnt > -2*a[zero-i]+a[zero+k-i-1]:
        cnt = -2*a[zero-i]+a[zero+k-i-1]
    else:
        break
for i in range(Z):
    if cnt > -a[zero-k+i]+2*a[zero+i-1]:
        cnt = -a[zero-k+i]+2*a[zero+i-1]
    else:
        break
print(cnt)

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,-abs(a[i]+a[i+k-1]\
          +min(abs(a[i]),abs(a[i+k-1]))))
print(c)

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,abs(a[i]+a[i+k-1]\
          -min(abs(a[i]),abs(a[i+k-1]))))
print(c)


n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,a[i]+a[i+k-1]\
          -abs(min(a[i],a[i+k-1])))
print(c)


n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,abs(a[i]+a[i+k-1]\
          -min(abs(a[i]),abs(a[i+k-1]))))
print(c)


import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
Z = min(n-zero,zero,k)
cnt = min(-sum(a[Z-k:zero]),sum(a[zero:zero+Z]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k-i-1]:
        cnt = -2*a[zero-i]+a[zero+k-i-1]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+i-1]:
        cnt = -a[zero-k+i]+2*a[zero+i-1]
    else:
        break

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k):
    c=min(c,a[i]+a[i+k])
print(c)

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,-a[i]+a[i+k-1]\
          +min(abs(a[i]),abs(a[i+k-1])))
print(c)


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Accepted