Problem:


Score : 300 points

Problem Statement

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.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
# for j in range(1, y):
#     #if count_m-j>0 and count_m+y-1-j<N-1:
#     if True:

#         print(j)

#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2 for j in range(1, y) if count_m-j>=0  and count_m+y-1-j<N)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] for j in range(1, y) if count_m-j>=0 and count_m+y-1-j<N)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0

elif count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x = x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
for j in range(1, y):
    #if count_m-j>0 and count_m+y-1-j<N-1:
    if True:

        print(j)

        print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2 for j in range(1, y) if count_m-j>=0  and count_m+y-1-j<N)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] for j in range(1, y) if count_m-j>=0 and count_m+y-1-j<N)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0

elif count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
for j in range(1, y):
    #if count_m-j>0 and count_m+y-1-j<N-1:
    if True:

        print(j)

        print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2 for j in range(1, y) if count_m-j>=0  and count_m+y-1-j<N)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] for j in range(1, y) if count_m-j>=0 and count_m+y-1-j<N)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0

elif count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
for j in range(1, y):
    #if count_m-j>0 and count_m+y-1-j<N-1:
    if True:

        print(j)

        print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1 and N-K>0:

    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j]
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0

elif count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
# for j in range(1, y):
#     if count_m-j>0 and count_m+y-1-j<N-1:

#         print(j)

#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:

    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j]
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0

elif count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
# for j in range(1, y):
#     if count_m-j>0 and count_m+y-1-j<N-1:

#         print(j)

#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:

    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j]
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000

if count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
elif N==0:
    ans = 1
else:
    # print("san")
    ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
# for j in range(1, y):
#     if count_m-j>0 and count_m+y-1-j<N-1:

#         print(j)

#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:

    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j]
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000

if count_p>=K:
        # print(y, "1chi")
        ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
        #print("ni")
        ans = min(tmp, tmp2, -x[count_m-K])
else:
        # print("san")
        ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
                                                                        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y
# for j in range(1, y):
#     if count_m-j>0 and count_m+y-1-j<N-1:

#         print(j)

#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])



if count_m >= 1:

    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j]
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000

if count_p>=K:
        # print(y, "1chi")
        ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
        #print("ni")
        ans = min(tmp, tmp2, -x[count_m-K])
else:
        # print("san")
        ans = min(tmp, tmp2)


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0

y = K
z = K - y

# for j in range(1, y):
#     if count_m-j>0 and count_m+y-1-j<N-1:
#         print(j)
#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])

if count_m >= 1:
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] 
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000
#print(tmp, tmp2, x[count_m:y], -x[count_m-min(y, count_m)])

#print("-----------------")
#print(count_m, count_p)
if count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)
       

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0

y = K
z = K - y

# for j in range(1, y):
#     if count_m-j>0 and count_m+y-1-j<N-1:
#         print(j)
#         print(x[count_m+y-1-j],x[count_m-j] ,x[count_m-1])

if count_m >= 1:
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2
              for j in range(1, y) if count_m-j>0  and count_m+y-1-j<N-1)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] 
               for j in range(1, y) if count_m-j>0 and count_m+y-1-j<N-1)
else:
    tmp = tmp2 = 1000000000
#print(tmp, tmp2, x[count_m:y], -x[count_m-min(y, count_m)])

#print("-----------------")
#print(count_m, count_p)
if count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    # print("san")
    ans = min(tmp, tmp2)
       

print(ans)

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
x = list(map(int,input().split()))
bindex = bisect.bisect_left(x,0)
mainasu = x[:bindex] #[-30,-20,-10]
purasu = x[bindex:] #[10,20,30]
"""
for i in range(len(mainasu)-2,-1,-1): #0は対象ではない
  mainasu[i] = mainasu[i+1]+mainasu[i]
#[-60,-30,-10]
for i in range(1,len(purasu)):
  purasu[i] = purasu[i-1]+purasu[i]
#[10,30,60]
"""
ans = 10**9
if len(mainasu) >= k:
  ans = min(ans,abs(mainasu[-k]))
if len(purasu) >= k:
  ans = min(ans,purasu[k-1])
if mainasu != []:
  for i in range(len(mainasu)-k,len(mainasu)): #-2~2
    if i >= len(purasu):break
    a = abs(mainasu[i])
    b = purasu[i]
    ans = min(ans,a*2+b,a+b*2)
print(ans)

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
x = list(map(int,input().split()))
bindex = bisect.bisect_left(x,0)
mainasu = x[:bindex] #[-30,-20,-10]
purasu = x[bindex:] #[10,20,30]
"""
for i in range(len(mainasu)-2,-1,-1): #0は対象ではない
  mainasu[i] = mainasu[i+1]+mainasu[i]
#[-60,-30,-10]
for i in range(1,len(purasu)):
  purasu[i] = purasu[i-1]+purasu[i]
#[10,30,60]
"""
ans = 10**9
if len(mainasu) >= k:
  ans = min(ans,abs(mainasu[-k]))
if len(purasu) >= k:
  ans = min(ans,purasu[k-1])

for i in range(len(mainasu)-k,len(mainasu)): #-2~2
  if i >= len(purasu):break
  a = abs(mainasu[i])
  b = purasu[i]
  ans = min(ans,a*2+b,a+b*2)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
arr = list(map(int,input().split()))
left = []
right = []
for i in arr:
    if i < 0:
        left.append(abs(i))
    else:
        right.append(i)
left.sort()
tmp = 0
count = 0
if len(right) == 0:
    print(left[K-1])
    exit()
if len(right) >= K:
    tmp += right[K-1]
    i = 1
    while True:
        if len(left) < i or K-i-1 < 0:
            break
        tmp2 = right[K-i-1] + left[i-1] + min(right[K-i-1],left[i-1])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
else:
    tmp += right[-1]+left[K-len(right)-1]+min(right[-1],left[K-len(right)-1])
    i = 1
    while True:
        if K-len(right)-1+i >= K or -1-i < 1-len(right):
            break
        tmp2 = right[-1-i] + left[K-len(right)-1+i] + min(right[-1-i],left[K-len(right)-1+i])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
print(tmp)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
arr = list(map(int,input().split()))
left = []
right = []
for i in arr:
    if i < 0:
        left.append(abs(i))
    else:
        right.append(i)
left.sort()
tmp = 0
count = 0
if len(right) >= K:
    tmp += right[K-1]
    i = 1
    while True:
        if len(left) < i or K-i-1 < 0:
            break
        tmp2 = right[K-i-1] + left[i-1] + min(right[K-i-1],left[i-1])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
else:
    tmp += right[-1]+left[K-len(right)-1]+min(right[-1],left[K-len(right)-1])
    i = 1
    while True:
        if K-len(right)-1+i >= K or -1-i < 1-K:
            break
        tmp2 = right[-1-i] + left[K-len(right)-1+i] + min(right[-1-i],left[K-len(right)-1+i])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
print(tmp)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(' '))
a= list(map(int, input().split(' ')))

INF = float('inf')
ans = INF

if N == K:
    ans = min(ans, abs(a[N]-a[0])+min(abs(a[0]), abs(a[N])))
else:
    for i in range(N-K):
        ans = min(ans, abs(a[i+K-1]-a[i])+min(abs(a[i+K-1]), abs(a[i])))

print(ans)

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.buffer.readline


def main():
    N, K = map(int, readline().split())
    X = list(map(int, readline().split()))

    if N == 1:
        print(abs(X[0]))
        sys.exit()

    dm = X[K] - X[0]
    for i in range(0, N-K+1):
        d = X[i+K-1] - X[i] + min(abs(X[i+K-1]), abs(X[i]))
        if d < dm:
            dm = d

    print(dm)


main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()

le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not left: 
    sm=0 
    l=[abs(i) for i in l]
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
k=k-l.count(0)
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
k=k-l.count(0)
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
#print(rr)
for a in range(1,len(left)):
    b=k-a 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
l=[abs(i) for i in l]
sm=sum(l[:k])
mini=min(mini,sm)
for i in range(k,n):
    sm=sm-l[i-k]
    sm+=l[i]
    mini=min(mini,sm)
print(mini)

Submission status: Runtime Error

Solution code:
import sys
import bisect
inputs = sys.stdin.readlines()

N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))

if N == 1:
    print(abs(X[0]))
else:
    ans = 2 * 10 ** 8
    idx = bisect.bisect_left(X, 0)
    Y = X[max(0, idx-K):min(N, idx+K)]
    for i in range(N-K+1):
        y1, y2 = Y[i], Y[i+K-1]
        if y1 < 0 and y2 <= 0:
            ans = min(ans, -y1)
        elif y1 < 0 and 0 < y2:
            if -y1 < y2:
                ans = min(ans, -2*y1 + y2)
            else:
                ans = min(ans, -y1 + 2*y2)
        else:
            ans = min(ans, y2)
            break
    print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import bisect

N,K=map(int,input().split())
X=list(map(int,input().split()))

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    m=min(0,X[i])
    M=max(0,X[i+K-1])
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import bisect

N,K=map(int,input().split())
X=list(map(int,input().split()))

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    if i==start:
        temp=X[i:i+K]
    else:
        temp.pop(0)
        temp.append(X[i+K-1])
    #print(temp)
    m=min(0,min(temp))
    M=max(0,max(temp))
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    main=main[:]
    sub=list(map(lambda x:x*2,sub))
    
    ans=0
    for _ in range(K):
        if len(main)==0:
            ans+=sub.pop(-1)
        elif len(sub)==0:
            ans+=main.pop(-1)
        elif main[-1]<=sub[-1]:
            ans+=main.pop(-1)
        else:
            ans+=sub.pop(-1)
    
    return ans
    
i=0
while X[i]<0:
    i+=1

neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]
d_pos=[pos[i+1]-pos[i] for i in range(len(pos)-1)][::-1]#popしたいのでひっくり返す
d_neg=[neg[i+1]-neg[i] for i in range(len(neg)-1)][::-1]

a=(journey(d_pos,d_neg))
b=(journey(d_neg,d_pos))

print(min(a,b))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    if len(main)==1:
        main.append(10**18)
    elif len(sub)==1:
        sub.append(10**18)
    dp=0
    nm=1
    ns=1
    ans=0
    for _ in range(K):
        dism=(main[nm]-main[nm-1])
        diss=(sub[ns]-sub[ns-1])
        if nm==len(main)-1:
            ans+=2*diss
            ns+=1
        elif ns==len(sub)-1:
            ans+=dism
            nm+=1
        elif dism>2*diss:
            ans+=diss*2
            ns+=1
        else:
            ans+=dism
            nm+=1
    return ans

i=0
while X[i]<0:
    i+=1

neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]


print(min(journey(pos,neg),journey(neg,pos)))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    if len(main)==1:
        main.append(10**18)
    elif len(sub)==1:
        sub.append(10**18)
    dp=0
    nm=1
    ns=1
    ans=0
    for _ in range(K):
        dism=(main[nm]-main[nm-1])
        diss=(sub[ns]-sub[ns-1])
        print(nm,ns)
        if nm==len(main)-1:
            ans+=2*diss
            ns+=1
        elif ns==len(sub)-1:
            ans+=dism
            nm+=1
        elif dism>2*diss:
            ans+=diss*2
            ns+=1
        else:
            ans+=dism
            nm+=1
    return ans

i=0
while X[i]<0:
    i+=1


neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]

print(pos)
print(neg)

print(min(journey(pos,neg),journey(neg,pos)))

Submission status: Runtime Error

Solution code:
import sys
n,k=map(int,input().split())
x=list(map(int,input().split()))
left=[]
right=[]
origin=0
for u in x:
    if u<0:
        left.append(abs(u))
    elif u==0:
        origin+=1
    else:
        right.append(u)
left.sort()
ans=float("inf")
k-=origin
if k==0:
    print(0)
    sys.exit()
r=len(right)
l=len(left)
for i in range(k):
    if i<r and k-i-2<l:
        y=right[i]+left[k-i-2]+min(right[i],left[k-i-2])
        ans=min(ans,y)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        try:
            k = K - abs(i - a)
            d = abs(ary[i])
        except:print(1)
        if k > 0:
                d += abs(ary[i] - ary[k + a])
                if ans > d:
                    ans = d

print(0 if ans == float("Inf") else ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N+1):
        if i == a:continue
        k = abs(i - a)
        d = abs(ary[i])
        if k >= K and ans > d:
            ans = d
        if i <= a:
            k = K - abs(i-a)
            d = abs(ary[i])
            if k > 0:
                d += abs(ary[i] - ary[k+a])
                if ans > d:
                    ans = d
print(0 if ans == float("Inf") else ans)

Submission status: Runtime Error

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = input().split()
mc = []
pc = []
for i in s:
  c = int(i)
  if c==0:
    K -= 1
  if c<0:
    mc += [-c]
  elif c>0:
    pc += [c]

mc.sort()
pc.sort()
mc += [INF]
pc += [INF]

m = K-1
xi = 0
yi = -1
while m>0:
  if pc[xi+1]>2*mc[yi+1]:
    yi += 1
  else:
    xi += 1
  m -= 1
if yi!=-1:
  c1 = pc[xi] + 2*mc[yi]
else:
  c1 = pc[xi]

m = K-1
xi = -1
yi = 0
while m>0:
  if mc[yi+1]>2*pc[xi+1]:
    xi += 1
  else:
    yi += 1
  m -= 1
if xi!=-1:
  c2 = 2*pc[xi] + mc[yi]
else:
  c2 = mc[yi]

if K==0:
  print(0)
else:
  print(min(c1, c2))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))

cost=10**10

Plus=[i for i in X if i>=0]
Minus=[-i for i in X if i<=0]

if len(Plus)>=K:
    cost=Plus[K-1]
if len(Minus)>=K:
    cost=min(cost,Minus[-K])

i=0
while X[i]<0 and i<=K:
    if i+K-1<N:
        cost=min(cost,X[i+K-1]-X[i]+abs(X[i]))
    i+=1

print(cost)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))

cost=10**10
P=X.copy()
i=0
while P[i]<0:
    P.pop(0)
if len(P)>=K:
    cost=P[K-1]    

for i in range(N-K+1):
    if X[i] >= 0:
        break
    else:
        L=[j-X[i] for j in X]
        cost=min(cost,L[i+K-1]+abs(X[i]))

print(cost)

Submission status: Runtime Error

Solution code:
n,k= map(int, input().split())
x= list(map(int, input().split()))
s=0
cnt=0
while x[s]<0:
    s+=1
a=[]
for i in range(s,min(s+k,n)):
    cnt+=1
    if i-k-1>=0:
        a.append(2*x[i]-x[i-k+1])
    if cnt==k:
        a.append(x[i])
s-=1
cnt=0
for i in range(s,max(s-k,-1),-1):
    cnt+=1
    if i+k-1<n:
        a.append(-2*x[i]+x[i+k-1])
    if cnt==k:
        a.append(-x[i])
print(min(a))

Submission status: Runtime Error

Solution code:
import bisect

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

x_negative = x[:bisect.bisect_left(x,0)]
x_positive = x[bisect.bisect_left(x,0):]

# 0以上の地点にロウソクが無い場合
if len(x_positive) == 0:
    print(abs(x_negative[-K]))
    exit()

# 地点0にロウソクがある場合
if x_positive[0] == 0:
    if K == 1:
        print(0)
        exit()
    K -= 1
    del x_positive[0]

t_list = []

# 全くターンしない場合
if len(x_negative) >= K:
    t_list.append(abs(x_negative[-K]))
if len(x_positive) >= K:
    t_list.append(x_positive[K-1])

# 1回だけターンする場合
for i in range(1, K-1):
    t_list.append(2*abs(x_negative[-i]) + x_positive[K-i-1])
    t_list.append(2*x_positive[i-1] + abs(x_negative[-(K-i)]))

print(min(t_list))

Submission status: Runtime Error

Solution code:
import bisect

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

x_negative = x[:bisect.bisect_left(x,0)]
x_positive = x[bisect.bisect_left(x,0):]

# 地点0にロウソクがある場合
if x_positive[0] == 0:
    if K == 1:
        print(0)
        exit()
    K -= 1
    del x_positive[0]

t_list = []

# 全くターンしない場合
if len(x_negative) >= K:
    t_list.append(x_negative[-K])
if len(x_positive) >= K:
    t_list.append(x_positive[K-1])

# 1回だけターンする場合
for i in range(1, K-1):
    t_list.append(2*abs(x_negative[-i]) + x_positive[K-i-1])
    t_list.append(2*x_positive[i-1] + abs(x_negative[-(K-i)]))

print(min(t_list))

Submission status: Runtime Error

Solution code:
N,K=map(int, input().split())
Candles=list(map(int, input().split()))

Distance=[]
for i in range(N-K):
        Distance.append(abs(Candles[i]-Candles[i+K-1])+min(abs(Candles[i]),abs(Candles[i+K-1])))
print(min(Distance))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
ans = (x[-1] - x[0])*2
if len(plus) >= K:
    ans = plus[K-1]
if len(minus) >= K:
    ans = min(ans, minus[-K])
for i in range(1, min(len(plus), len(minus))):
    tmp = plus[K-1-i] - (minus[-i])*2
    ans = min(tmp, ans)

    tmp = plus[K-1-i]*2 - minus[-i]
    ans = min(tmp, ans)

print(ans)

Submission status: Runtime Error

Solution code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k):
    if x[i]*x[i+k-1]<0:
        a = abs(x[i])+x[i+k-1] + min(abs(x[i], x[i+k-1]))
    else:
        a = abs(x[i+k-1])
    
    ans = min(ans, a)

if k==1:
    ans = abs(x[0])
print(ans)