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()))

for center in range(n):
    if x[center] >= 0:
        break

if center == n or (center == n - 1 and x[n - 1] == 0):
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - 1]))

print(ans)

Submission status: Runtime Error

Solution code:
N,K,*x=map(int,open(0).read().split());print(min(min(abs(x[i]),abs(x[i+k-1]))+x[i+k-1]-x[i]for i in range(n-k+1)))

Submission status: Runtime Error

Solution code:

near = abs(xlist[lpos]) if (abs(xlist[lpos]) < abs(xlist[rpos])) else abs(xlist[rpos])
bet = abs(xlist[lpos] - xlist[rpos])

ans = bet + near

for i in range(N-K):
    lpos += 1
    rpos += 1

    near = abs(xlist[lpos]) if (abs(xlist[lpos]) < abs(xlist[rpos])) else abs(xlist[rpos])
    bet = abs(xlist[lpos] - xlist[rpos])

    tmp = bet+near

    if tmp < ans:
        ans = tmp


print(ans)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().strip().split(" ")))
candles = input().strip()
min_cost_index = candles.count("-")
candles = list(map(int, candles.split(" ")))

def update():
    start_index = max(0, min_cost_index - K - 1)
    end_index = min(N, min_cost_index + K + 1)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 
   
start_time = time.perf_counter()
candles = update()

N = len(candles)
min_cost = None
patterns = N - K + 1


for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Runtime Error

Solution code:
def update():
    cost = list(map(abs, candles))
    min_cost_index = cost.index(min(cost))
    start_index = max(0, min_cost_index - K)
    end_index = min(N, min_cost_index + K)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 

N, K = list(map(int, input().strip().split(" ")))
candles = list(map(int, input().strip().split(" ")))



candles = update()

N = len(candles)
min_cost = None
patterns = N - K + 1


for start_index in range(patterns):
    before_flag = True if start_index == 0 else candles[start_index-1] * candles[start_index] < 0
    after_flag = True if start_index == N - K - 1 else candles[start_index] * candles[start_index+1] < 0
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Runtime Error

Solution code:
def main(init, lcandle, rcandle, ifzero):
	target = init[1]
	
	if ifzero:
		target -= 1
	
	cost = sum(lcandles) + sum(rcandles)
	if len(lcandles) == 0 and len(rcandles)>0:
		cost = min(rcandles[target-1], cost)
	elif len(rcandles) == 0 and len(lcandles)>0:
		cost = min(lcandles[-(target-1)],cost)
	else:
		if len(rcandles)>= target:
			cost = min(rcandles[target-1], cost)
		if len(lcandles) >= target:
			cost = min(lcandles[-(target)],cost)
		for j in range(len(lcandles)):
			if target-j <= len(rcandles):
				cost = min(lcandles[-(j)]*2 + rcandles[target-j-1], cost)
		for j in range(len(rcandles)):
			if target-j <= len(lcandles):
				cost = min(rcandles[j]*2 + lcandles[-(target-j)], cost)	
		
	return cost
	
	
init = input().split(" ")
init = [int(c) for c in init]

candles = input().split(" ")
candles = [int(c) for c in candles]

lcandles = [abs(candle) for candle in candles if candle < 0]
rcandles = [candle for candle in candles if candle > 0]
ifzero = 0 in candles

print(main(init, lcandles, rcandles, ifzero))

Submission status: Runtime Error

Solution code:
def main(init, lcandle, rcandle, ifzero):
	target = init[1]
	
	if ifzero:
		target -= 1
	
	cost = sum(lcandles) + sum(rcandles)
	if len(lcandles) == 0 and len(rcandles)>0:
		cost = min(rcandles[target-1], cost)
	elif len(rcandles) == 0 and len(lcandles)>0:
		cost = min(lcandles[-(target-1)],cost)
	else:
		if len(rcandles)>= target:
			cost = min(rcandles[target-1], cost)
		if len(lcandles) >= target:
			cost = min(lcandles[-(target)],cost)
		for j in range(len(lcandles)):
			if target-j <= len(rcandles):
				cost = min(lcandles[-(j)-1]*2 + rcandles[target-j-1], cost)
		for j in range(len(rcandles)):
			if target-j <= len(lcandles):
				cost = min(rcandles[j]*2 + lcandles[-(target-j)], cost)	
		
	return cost
	
	
init = input().split(" ")
init = [int(c) for c in init]

candles = input().split(" ")
candles = [int(c) for c in candles]

lcandles = [abs(candle) for candle in candles if candle < 0]
rcandles = [candle for candle in candles if candle > 0]
ifzero = 0 in candles

print(main(init, lcandles, rcandles, ifzero))

Submission status: Runtime Error

Solution code:
# encoding utf-8

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

x = list(int(i) for i in input().split())


for i in range(N):
    if 0 <= x[i]:
        border = i
        break

len_run = 100000

if border == 0:
    len_run = min(abs(x[K-1]), len_run)

if border == N-1:
    len_run = min(abs(x[N-K]), len_run)

for i in range(K):
    if border-K+i >= 0 and border+i <= N:
        len_run = min(abs(2*x[border-K+i])+abs(x[border+i-1]), len_run)
        len_run = min(abs(x[border-K+i])+abs(2*x[border+i-1]), len_run)

print(len_run)













# #最終的に右に進む
# def right(K):
#     runlen = 0
#     y = x_pos
#     for i in range(len(x_neg)):
#         y.append(x_neg[i]*(-2))
#     y.sort()
#     max = y[K-1]
#     if max in x_pos:
#         runlen += max
#         y_neg = [i for i in x_neg if i >= (-max)/2]
#         runlen += y_neg[-1]*-2
#     else:
#         runlen += max/2
#         y_pos = [i for i in x_pos if i <= max]
#         runlen += y_pos[-1]
#     return runlen
#
#
# def left(K):
#     runlen = 0
#     z = []
#     for j in range(len(x_neg)):
#         z.append(x_neg[j]*-1)
#     for i in range(len(x_pos)):
#         z.append(x_pos[i]*(2))
#     z.sort()
#     max = -z[K-1]
#     if max in x_neg:
#         runlen -= max
#         z_pos = [i for i in x_pos if i <= (-max)/2]
#         runlen += z_pos[-1]*2
#     else:
#         runlen -= max/2
#         z_neg = [i for i in x_neg if i <= -max]
#         runlen += z_neg[-1]
#     return runlen
#
# if len(x_neg) == 0:
#     runlen = x[K-1]
# elif len(x_pos) == 0:
#     x.sort().reverse()
#     runlen = x[K-1]
# else:
#     runlen = min(right(K),left(K))
#     print(right(K),left(K))
#
# print(runlen)

Submission status: Runtime Error

Solution code:
def main(init, lcandle, rcandle, ifzero):
	target = init[1]
	
	if ifzero:
		target -= 1
		
	
	cost = sum(lcandles) + sum(rcandles)
	if len(lcandles) == 0 and len(rcandles)>0:
		cost = rcandles[target-1]
	elif len(rcandles) == 0 and len(lcandles)>0:
		cost = lcandles[-(target-1)]
	else:
		for j in range(len(lcandles)):
			if target-j-1 <= len(rcandles):
				cost = min(lcandles[-(j)]*2 + rcandles[target-j-1], cost)
		for j in range(len(rcandles)):
			if target-j-1 <= len(lcandles):
				cost = min(rcandles[j]*2 + lcandles[-(target-j-1)], cost)	
		
	return cost
	
	
init = input().split(" ")
init = [int(c) for c in init]

candles = input().split(" ")
candles = [int(c) for c in candles]

lcandles = [abs(candle) for candle in candles if candle < 0]
rcandles = [candle for candle in candles if candle > 0]
ifzero = 0 in candles

print(main(init, lcandles, rcandles, ifzero))

Submission status: Runtime Error

Solution code:
def main(init, lcandle, rcandle, ifzero):
	target = init[1]
	
	if ifzero:
		target -= 1
		
	
	cost = sum(lcandles) + sum(rcandles)
	for j in range(len(lcandles)):
		if target-j-1 <= len(rcandles):
			cost = min(max(lcandles[-(j+1):])*2 + max(rcandles[:target-j-1]), cost)
	
	for j in range(len(rcandles)):
		if target-j-1 <= len(lcandles):
			cost = min(max(rcandles[:j+1])*2 + max(lcandles[-(target-j-1):]), cost)	
		
	return cost
	
	
init = input().split(" ")
init = [int(c) for c in init]

candles = input().split(" ")
candles = [int(c) for c in candles]

lcandles = [abs(candle) for candle in candles if candle < 0]
rcandles = [candle for candle in candles if candle > 0]
ifzero = 0 in candles

print(main(init, lcandles, rcandles, ifzero))

Submission status: Runtime Error

Solution code:
def main():
    N, K = list(map(int, input().split(" ")))
    X = list(map(int, input().split(" ")))
    output = solve(N, K, X)
    print(output)
    return

#________________________________________
#  Solve
#________________________________________
def solve(N, K, X):
    """
    解答のコード
    """
    oab_min = X[-1] - X[0]

    a_list = [ i for i in range(len(X)) if X[i] >= 0 ]  # O(N)
    b_list = [ i for i in range(len(X)) if X[i] < 0 ]  # O(N)

    if not a_list:
        # if a_list is empty
        ob = - X[b_list[K-1]]
        oab_min = min( ob, oab_min)
    if not b_list:
        oa = X[a_list[K-1]]
        oab_min = min( ob, oab_min)
    left = max(0, K - len(b_list))
    right = min(len(a_list), K)
    for i in range(left, right):
        # if a_list is empty, skip this process
        if (i==0) and (a_list[0] != 0):
            ob = - X[b_list[K-1]]
            oab_min = min( ob, oab_min )
        oa = X[a_list[i]]
        ob = abs(X[a_list[i] - K + 1])
        oab = min( oa, ob ) + oa + ob
        oab_min = min(oab, oab_min)
    return oab_min
#________________________________________


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        print(x[bottom + i:bottom + i + K])
        print(x[bottom + i + K - 1])
        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            best = min(best, 2 * abs(x[bottom + i]) + x[bottom + i + K - 1])
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
for i in range(n-k+1):
    xtmp = x[i:i+k]
#     print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

print(MIN)

Submission status: Runtime Error

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

for i in range(n-k+1):
    xtmp = x[i:i+k]
    print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

    i+=1

print(MIN)

Submission status: Runtime Error

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

# if 0 in x:
#     x.remove(0)
#     k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
    i=0
    if x[0]>=0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
            xtmp = x[i:i+k]
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
            if i<n-k:
                if x[i+k]>0 and x[i+k-1]<0:
                    flag02=1

            if flag02==1:
                MIN = min(MIN, abs(xtmp[0]))             

            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))

            i+=1

        print(MIN)

Submission status: Runtime Error

Solution code:
N,K =map(int,input().split())
arr = [int(x) for x in input().split()]
ps = []
ms = []
for i in range(N):
    if arr[i] == 0:
        K -= 1
    elif arr[i] < 0:
        ms.append(abs(arr[i]))
    elif arr[i] > 0:
        ps.append(arr[i])
ms = ms[::-1]
if K <= 0:
    ans = 0
else:
    ans = ps[K-1] if K <= len(ps) else 10**18
    ans = min(ans,ms[K-1]) if K <= len(ms) else ans
if len(ps) > 0 and len(ms) > 0:
    for i in range(K):
        if i == len(ms):
            break
        ans = min(ans,ms[i]*2 + ps[K-(i+1)-1])
    for i in range(K):
        if i == len(ps):
            break
        ans = min(ans,ps[i]*2 + ms[K-(i+1)-1])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

result = np.inf
for i in range(len(x) - k):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))

Submission status: Runtime Error

Solution code:
from itertools import accumulate
N,K =map(int,input().split())
arr = [int(x) for x in input().split()]
ps = []
ms = []
for i in range(N):
    if arr[i] == 0:
        K -= 1
    elif arr[i] < 0:
        ms.append(abs(arr[i]))
    elif arr[i] > 0:
        ps.append(arr[i])
ms = ms[::-1]
if K == 0:
    ans = 0
else:
    ans = ps[K-1] if K <= len(ps) else 10**18
    ans = min(ans,ms[K-1]) if K <= len(ms) else ans
if len(ps) > 0 and len(ms) > 0:
    for i in range(K):
        if i == len(ms):
            break
        ans = min(ans,ms[i]*2 + ps[K-(i+1)-1])
    for i in range(K):
        if i == len(ps):
            break
        ans = min(ans,ps[i]*2 + ms[K-(i+1)-1])
print(ans)

Submission status: Runtime Error

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

result = []
for i in range(N-K):
    start = i
    end = i + K 
    result.append(abs(x_list[start] + abs(x_list[end] - x_list[start])))
    result.append(abs(x_list[end]) +  abs(x_list[end] - x_list[start] ))
print(min(result))

Submission status: Runtime Error

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

result = []
for i in range(N-K):
    start = i
    end = i + K -1
    result.append(abs(x_list[start] + abs(x_list[end] - x_list[start])))
    result.append(abs(x_list[end]) +  abs(x_list[end] - x_list[start] ))
print(min(result))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
x = [int(input()) for i in range(n)]
ans = [min(abs(x[i]), abs(x[i+k-1]))+x[i+k-1]-x[i] for i in range(n-k+1)]
print(min(ans))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
x = [int(inout()) for i in range(n)]
ans = [min(abs(x[i]), abs(x[i+k-1]))+x[i+k-1]-x[i] for i in range(n-k+1)]
print(min(ans))

Submission status: Runtime Error

Solution code:
import sys

N, K = [int(_) for _ in input().split()]
list = [int(_) for _ in input().split()]

l1 = []
l2 = []
t = 0
for i in list:
    if i < 0:
        l1.append(i)
    elif i == 0:
        t = 1
    else:
        l2.append(i)
l1 = [abs(_) for _ in l1]
l1.sort()

if t == 1 and K == 1:
    print(0)
    sys.exit()

K = K - t
a = float('inf')
b = float('inf')
if len(l1) >= K:
    a = l1[K-1]
if len(l2) >= K:
    b = l2[K-1]

c = float('inf')
d = float('inf')
for i in range(len(l2)):
    if K-i-2 >= 0:
        c_new = l2[i] * 2 + l1[K-i-2]
        c = min(c, c_new)
for j in range(len(l1)):
    if K-j-2 >= 0:
        d_new = l1[j] * 2 + l2[K-j-2]
        d = min(d, d_new)

print(min(a, b, c, d))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
xl=list(map(int,input().split()))
for i in range(n-k+1):
    a=xl[s+i,s+k+i]
print(min(abs(a[-1]-a[0])+abs(a[-1]),abs(a[-1]-a[0])+abs[0]))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
x = [int(i) for i in input().split()]

left = sorted([abs(i) for i in x if i < 0])
right = [i for i in x if i >= 0]

ans = 9999999999999999

l = 0

if len(right) >= k:
    ans = right[k-1]


while len(left) != l:

    distance = 0

    nokori = k - l - 1


    distance += left[l] * 2
    l += 1


    if nokori > len(right):
        continue

    if nokori == 0:
        ans = min(ans,distance)
    else:
        ans = min(ans,distance + right[nokori-1])
        

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
a = []
a2 = []
for i in x:
    if i < 0:
        a2.append(2 * abs(i))
    else:
        a.append(i)

if len(a2) == 0:
    print(a[K-1])
    exit()
a2 = sorted(a2)
c1 = float('inf')

for i, v in enumerate(a):
    if K - i > len(a2):
        continue
    c1 = min(c1, v + a2[K - i])

b = []
b2 = []
for i in x:
    if i > 0:
        b2.append(2 * abs(i))
    else:
        b.append(-i)
b = sorted(b)

if len(b2) == 0:
    print(b[K-1])
    exit()

c2 = float('inf')
for i, v in enumerate(b):
    if K - i > len(b2):
        continue
    c2 = min(c2, v + b2[K - i])

print(min(c1,c2))

Submission status: Runtime Error

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

MIN = float("inf")
for i in range(N-K+1):
	l = i
	r = i + K - 1
		cur1 = 2*abs(x[l]) + abs(x[r]-x[l])
		cur2 = abs(x[l]) + 2*abs(x[r]-x[l])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
x = [int(i) for i in input().split()]

left = sorted([abs(i) for i in x if i < 0])
right = [i for i in x if i >= 0]

ans = 9999999999999999

l = 0

if len(right) >= k:
    ans = right[k-1]


while len(left) != l:

    distance = 0

    nokori = k - l - 1


    distance += left[l] * 2
    l += 1


    if nokori > len(right):
        continue
    
    ans = min(ans,distance + right[nokori-1])
        

print(ans)

Submission status: Runtime Error

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

o = 0
if x[N-1] <= 0:
	print(x[N-K])
	exit()
if x[0] >= 0:
	print(x[K-1])
	exit()

MIN = float("inf")
for i in range(N-K+1):
	l = i
	r = i + K
		cur1 = 2*abs(x[l]) + abs(x[r]-x[l])
		cur2 = abs(x[l]) + 2*abs(x[r]-x[l])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)

Submission status: Runtime Error

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

o = 0
if x[N-1] <= 0:
	print(x[N-K])
	exit()
if x[0] >= 0:
	print(x[K-1])
	exit()
if 0 in x:
	i = x.index(0)
	del x[i]
	N,K = N-1,K=1

while x[o] < 0:
	o += 1
	if o == N-1:
		break

MIN = float("inf")
for i in range(-K-1,K+1,1):
	l = o - i+1
	r = o + K-i
	if not(l < 0 or l >= N or r < 0 or r >= N):
		cur1 = 2* abs(x[l]) + abs(x[r])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
xl=list(map(int,input().split()))
if xl==[0]:
    print(0)
if 0 in xl:
    xl.remove(0)
    n=n-1
    k=k-1
mi=0
pl=0
for i in xl:
    if i<0:
        mi+=1
    else:
        pl+=1
if len(xl)>0:    
    if mi==0:
        print(xl[k-1])
    if pl==0:
        print(abs(xl[-k]))
proans=[]
s=0
a=xl[s:s+k]
for i in range(n-k+1):
    a=xl[s+i:s+k+i]
    if a[-1]<0:
        proans.append(abs(a[0]))
    else:
        proans.append(2*min(abs(a[0]),abs(a[-1]))+max(abs(a[0]),abs(a[-1])))
print(min(proans))

Submission status: Runtime Error

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

minus=[]
for i in range (N):
    if a[i]<0:
        minus.append(a[i])
plus=a[len(minus):]

if len(minus)==0:
    print(plus[K-1])
    sys.exit()
if len(plus)==0:
    minus=minus[::-1]
    print(minus[K-1])
    sys.exit()

if K==1:
    minus=minus[::-1]
    if len(plus)==0:
        plus.append(10**18)
    if len(minus)==0:
        minus.append(10**18)
    print(min(plus[0],abs(minus[0])))
    sys.exit()
     
data=[i * 2 for i in minus]+plus
data2=minus+[i * 2 for i in plus]
data_min=10**18
for i in range (N-K+1):
    if data_min >((abs(min(data[i],0)))+((abs(min(data[i+K-1],0))):
        data_min=((abs(min(data[i],0)))+((abs(min(data[i+K-1],0)))
for i in range (N-K+1):
    if data_min >((abs(min(data2[i],0)))+((abs(min(data2[i+K-1],0))):
        data_min=((abs(min(data2[i],0)))+((abs(min(data2[i+K-1],0)))
print(data_min)

Submission status: Runtime Error

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

ans=0
for i in range(N-K+1):
    tans=abs(X[i]-X[i+K])+abs(X[i])

    if i==0 or ans>tans:
        ans=tans
    
print(ans)

Submission status: Runtime Error

Solution code:

N, K = map(int, input().split())
X_ = input().split()

X = []
for i in X_:
    X.append(int(i))


k = N - K + 1
ans = 10 ** 10
qwe = []
for i in range(k):
    x = X[i:i+K]
    a, b = min(x), max(x)
    
    if a*b < 0:
        if len(qwe) == 0:
            qwe.append(i)
        ans_ = min(abs(a), abs(b))*2 + max(abs(a), abs(b))
    else:
        if len(qwe) == 1:
            ans_ = max(abs(a), abs(b))
            if ans_ < ans:
                ans = ans_
            break
        continue
    
    if ans_ < ans:
        ans = ans_

if len(qwe) != 0:
    a = qwe[0] - 1
    x = X[a]
    if x < ans:
        ans = x
else:
    ans = min(X[K], X[-K])
print(ans)

Submission status: Runtime Error

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

if 0 in x:
    x.remove(0)
    k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
    i=0
    if x[0]>=0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
            xtmp = x[i:i+k]
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
            if i<n-k:
                if x[i+k]>0 and x[i+k-1]<0:
                    flag02=1

            if flag02==1:
                MIN = min(MIN, abs(xtmp[0]))             

            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))

            i+=1

        print(MIN)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split(" "))
X=list(map(int, input().split(" ")))
import bisect
maxvalue=100000000000000000000000
place=bisect.bisect_left(X,0)

if k<place:
    X=X[place-k:]
elif k<n-place:
    X=X[:place-n]

#print(place)
for i in range(n-k+1):
    low=X[i]
    high=X[i+k-1]
    if high*low<0:
        low=abs(low)
        high=abs(high)
        value=min(low,high)*2+max(low,high)
    else:
        low=abs(low)
        high=abs(high)
        value=low+high
    #print(value)
    if value<maxvalue:
        maxvalue=value
print(maxvalue)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

N, K = map(int, input().split())
x = list(map(int, input().split()))
if N != 1:
    nof_negative = len([x_i for x_i in x if x_i < 0])
    x_negative = [0] + x[nof_negative-1::-1]
    if 0 in x:
        nof_positive = N- nof_negative - 1
        x_positive = x[nof_negative:]
    else:
        nof_positive = N - nof_negative
        x_positive = [0] + x[nof_negative:]

    dir_positive = [2*x_positive[i] - x_negative[K-i] for i in range(max(0,K-nof_negative),min(K,nof_positive))]
    dir_positive = [x_positive[K-i] -2* x_negative[i] for i in range(max(0,K-nof_positive),min(K,nof_negative))]
    ans = min(min(dir_negative),min(dir_positive))
else:
    ans = x[0]
print(ans)

Submission status: Runtime Error

Solution code:
L = list()
R = list()
N, K = map(int, input().split())
arr = list(map(int, input().split()))
for i in arr:
    if i < 0: L.insert(0, i)
    else: R.append(i)

Min = 10000000000
if len(L) == 0:
    Min = R[K-1]
elif len(R) == 0:
    Min = L[K-1]
else:
    for Left in range(0, len(L)):
        Min = min(L[Left] * 2 + R[K - Left - 1], Min)
    for Right in range(0, len(R)):
        Min = min(R[Right] * 2 + L[K - Right - 1], Min)
print(Min)

Submission status: Runtime Error

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

if 0 in x:
    x.remove(0)
    k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
#     MIN = 2*abs(x[0]+x[-1])
    
    i=0
    if x[0]>0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
    #     for i in range(n-k+1):
#             print(i, x[i])
            xtmp = x[i:i+k]
#             print(xtmp)
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
            if i<n-k:
                if x[i+k]>0 and x[i+k-1]<0:
                    flag02=1

            if flag02==1:
                MIN = min(MIN, abs(xtmp[0]))             

    #         if xtmp[0] != 0 and xtmp[-1]!=0:
            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))
    #         elif xtmp[0]==0:
    #             MIN = min(MIN, abs(xtmp[-1]))
    #         else:
    #             MIN = min(MIN, abs(xtmp[0]))       


            i+=1

        print(MIN)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]

time = 10 ** 20
# 0以上の最小の整数
base = 0
index = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        index = i
        break
else:
    index = -1

if index == 0:
        time = x[K-1]
elif index == -1:
    time = abs(x[N-K])
else:
    for i in range(max(0, index - K), min(index, N)):
        if x[i+K-1] < 0:
            time = min(abs(x[i]), time)
        elif x[i] < 0 and  x[i+K-1] >= 0:
            time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
        else:
            time = min(x[i+K-1], time)

print(time

Submission status: Runtime Error

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

X = []
for i in X_:
    X.append(int(i))


k = N - K + 1
ans = 10 ** 10
a = []
for i in range(k):
    x = X[i:i+K]
    a, b = min(x), max(x)
    
    if a*b < 0:
        if len(a) == 0:
            a.append(i)
        ans_ = min(abs(a), abs(b))*2 + max(abs(a), abs(b))
    else:
        if len(a) == 1:
            ans_ = max(abs(a), abs(b))
            if ans_ < ans:
                ans = ans_
            break
        continue
    
    if ans_ < ans:
        ans = ans_

a = a[0] - 1
x = X[a]
if x < ans:
    ans = x
print(ans)

Submission status: Runtime Error

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

x_abs=list(map(abs, x))
x_abs.sort()
maximum_num =x_abs[K-1]
#print(x)
#print(maximum_num)
x_max=[i for i in x if i <= maximum_num]
x=[i for i in x_max if i > -maximum_num]
if min(x)<0 and max(x)>0:
    print(abs(min(x))*2+max(x))
elif min(x)<0 and max(x)<0:
    print(abs(min(x)))
else:
    print(max(x))

Submission status: Runtime Error

Solution code:
import bisect
N,K=map(  int,raw_input().split() )
X=map(  int,raw_input().split() )
 
zero_idx=bisect.bisect_left(X,0)
 
t=float("inf")
ans=float("inf")
for i in range(zero_idx):
	if zero_idx-i>K:
		continue
	elif zero_idx-i==K:
		t=X[i]
	else:
		nokori_cnt=K-(zero_idx-i)
		t=2*abs(X[i])+X[zero_idx+nokori_cnt-1]
	ans=min(ans,t)	

for i in range(zero_idx,N):
	if i==zero_idx+K-1:
		t=X[i]
	elif i<zero_idx+K-1:
		nokori_cnt=K-(i-zero_idx)-1
		t=2*X[i]+abs(X[zero_idx-nokori_cnt])
	ans=min(ans,t)

print ans

Submission status: Runtime Error

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

x_minus = [a for a in X if a < 0]
x_plus = [a for a in X if a >= 0]

ans = float("inf")
for i in range(K+1):
    if i == 0:
        if len(x_plus) >= K:
            dis = x_plus[K - 1]
            ans = min(ans, dis)
        else:
            continue
    if i != 0:
        if len(x_minus) >= i and len(x_plus) >= K - i:
            dis = 2 * abs(x_minus[-i]) + x_plus[K - i - 1]
            ans = min(ans, dis)
            dis = 2 * x_plus[K - i - 1] + abs(x_minus[-i])
            ans = min(ans, dis)
        else:
            continue
    if i == K:
        if len(x_minus) >= K:
            dis = abs(x_minus[-K])
            ans = min(ans, dis)


print(ans)

Submission status: Runtime Error

Solution code:
import bisect
N,K=map(  int,raw_input().split() )
X=map(  int,raw_input().split() )
 
zero_idx=bisect.bisect_left(X,0)
 
t=float("inf")
ans=float("inf")
for i in range(zero_idx):
	if zero_idx-i==K:
		t=X[i]
	else:
		nokori_cnt=K-(zero_idx-i)
		t=2*abs(X[i])+X[zero_idx+nokori_cnt-1]
	ans=min(ans,t)	

for i in range(zero_idx,N):
	if i==zero_idx+K-1:
		t=X[i]
	elif i<zero_idx+K-1:
		nokori_cnt=K-(i-zero_idx)-1
		t=2*X[i]+abs(X[zero_idx-nokori_cnt])
	ans=min(ans,t)

print ans

Submission status: Runtime Error

Solution code:
L = list()
R = list()
N, K = map(int, input().split())
arr = list(map(int, input().split()))
for i in arr:
    if i < 0: L.insert(0, i)
    else: R.append(i)

Min = 10000000000
for Left in range(0, len(L)):
    Min = min(L[Left] * 2 + R[K - Left - 1], Min)
for Right in range(0, len(R)):
    Min = min(R[Right] * 2 + L[K - Right - 1], Min)
print(Min)