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

if x[n-1]<=0 :
    t=x[-k+1]
    print(t)

elif x[0]>=0:
    t=x[k-1]
    print(t)

else:
    for i in range(n):
        if x[i]<0 and x[i+1]>=0 :
            i0=i+1
            break
    print(i0)
    tmin=10**9
    for i in range(i0,k):
        kt=i
        t=x[kt]*2-x[kt-k+1]
        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
        if t<tmin:
            tmin=t
    for i in range(i0-1,i0+1-k-1,-1):
        kt=i
        t=-x[kt]*2+x[kt+k-1]
        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
        if t<tmin:
            tmin=t
    print(tmin)

from bisect import bisect_left

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

iz=bisect_left(x,0)
 f x[iz+1]==0:
    k=k-1
if x[n-1]<=0 :
    t=-x[-k]
    print(t)
elif x[0]>=0:
    t=x[k-1]
    print(t)

else:
    tmin=10**9
    for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
        kt=i
        t1=-x[kt]+x[kt+(k-1)]*2
        t2=-x[kt]*2+x[kt+(k-1)]
        if t1<tmin:
            tmin=t1
        if t2<tmin:
            tmin=t2
    print(tmin)
    


from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        for i in range(iz+2,min(iz+k,k)):
            kt=i
            t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
            if t<tmin:
                tmin=t
        for i in range(max(iz+2-k,0),iz):
            kt=i
            t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
            if t<tmin:
                tmin=t
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1))+1):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])

            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000        
    

from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
#    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
#    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
#    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
#    print(4)
    print(t)

else:
    iz=bisect_left(x,0)-1
    if x[iz+1]==0:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)-1]*2
            t2=-x[kt]*2+x[kt+(k-1)-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])

            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])

            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000        
    


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

xmin=(x[n-1]-x[0])*2

for i in range(n-k+1):
    x1=abs(x[i])+abs(x[i+k-1]-x[i])
    x2=abs(x[i+k-1])+abs(x[i+k-1]-x[i])
    xmin=min(xmin,x1,x2)

print(xmin)


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Accepted