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:
import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = abs(negatives[-K])
        for i in range(K+1):
            if i == 0:
                continue
            elif i > 0 and i < K:
                ne = abs(negatives[-K+i])
                po = positives[i-1]
                min = calc(ne, po, min)
            elif i == K:
                tmp = positives[K-1]
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[K+i]
            min = calc(ne, po, min)

        return min

    if nega_size < K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min
        return min

    if posi_size < K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-i-posi_size-1])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())

Submission status: Runtime Error

Solution code:
import bisect

N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9
#print(bisect.bisect(x, 0))

for i in range(len(x) - K + 1):
  if x[i] <= 0 and x[i+K-1] >= 0:
    tmp = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
  elif i == bisect.bisect(x, 0):
    tmp = x[i+K-1]
  elif i+K-1 == bisect.bisect(x, 0):
    tmp = abs[x[i]]
#  print("i:{}, i+K-1:{}, tmp:{}".format(i, i+K-1, tmp))
  ans = min(tmp, ans)
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import numpy as np
 
N, K = map(int, input().rstrip().split(' '))
X = np.array(list(map(int, input().rstrip().split(' '))))

min_cost = int(1e10)
for i in range(K, N+1):
    min_cost = min(min_cost, X[i-1] - X[i-K] + min(abs(X[i-1]), abs(X[i-K])))
    
print(min_cost)

Submission status: Runtime Error

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

min_t = 10 ** 9

import bisect

snuke = bisect.bisect_left(x, 0)
if x[snuke] == 0:
    k -= 1
    n -= 1
    del x[snuke]

for i in range(k+1):
    if i == 0:
        left = 0
    elif snuke - i >= 0:
        left = abs(x[snuke - i])
    else:
        left = 10 ** 9

    if k - i == 0:
        right = 0
    elif snuke + k - i - 1 <= n - 1:
        right = abs(x[snuke + k - i - 1])
    else:
        right = 10 ** 9

    min_t = min(min_t, min(left, right) * 2 + max(left, right))

print(min_t)

Submission status: Runtime Error

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

min_t = 10 ** 9

import bisect

snuke = bisect.bisect_left(x, 0)
if x[snuke] == 0:
    k -= 1
    del x[snuke]

for i in range(k+1):
    if i == 0:
        left = 0
    elif snuke - i >= 0:
        left = abs(x[snuke - i])
    else:
        left = 10 ** 9

    if k - i == 0:
        right = 0
    elif snuke + k - i - 1 <= n - 1:
        right = abs(x[snuke + k - i - 1])
    else:
        right = 10 ** 9

    min_t = min(min_t, min(left, right) * 2 + max(left, right))

print(min_t)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
minus = []
minus.append(0)
plus = []
plus.append(0)

for n in x:
    if n < 0:
        minus.append(n)
    else:
        plus.append(n)
minus.sort(reverse=True)
totals = []

for i in range(len(minus)):
    if k-i < len(plus):
        if i == 0:
            totals.append(plus[k-i])
        else:
            totals.append(2 * abs(minus[i]) + plus[k-i])
            # print(i)
            # print(abs(minus[i]))
            # print(plus[k-i])
for i in range(len(plus)):
    if k-i < len(minus):
        if i == 0:
            totals.append(abs(minus[k-i]))
        else:
            totals.append(2 * plus[i] + abs(minus[k-i]))
# print(totals)
print(min(totals))

Submission status: Runtime Error

Solution code:
import sys

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

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
all_p = 10 ** 9
all_m = 10 ** 9
if K <= lastx:
    all_p = lst_p[K-1]
if K <= lasty:
    all_m = lst_m[lasty - K]

#print (all_p, all_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m), all_p, all_m))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] < abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1

Submission status: Runtime Error

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

distance = max([X[-1] - X[0], -X[i], X[-1]])
for i in range(N-K+1):
    if X[i+K-1] < 0:
        distance = min(distance, -X[i])
    elif X[i] > 0:
        distance = min(distance, X[i+K-1])
    else:
        tmp_distance = X[i+K-1]-X[i]
        tmp_distance += min(X[i+K-1], -X[i])
        distance = min(distance, tmp_distance)
print(distance)

Submission status: Runtime Error

Solution code:
N, K = map(int, input())
x = list(map(int, input()))
ret = 10 ** 9
for i in range(N-k+1):
    init = abs(x[i])
    dist = abs(x[i]-x[i+k-1])
    l = init + dist
    init = abs(x[i+k-1])
    r = init + dist
    ret = min(l, r)
print(ret)

Submission status: Runtime Error

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-(index-1)) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index+1)):
        tmp = array[i]
        tmp += array[i] - array[k -(i - index + 1)]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        tmp += -array[i] + array[k + i - 1]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

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

print(ans(index, x, k))

Submission status: Runtime Error

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-(index-1)) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index+1)):
        tmp = array[i]
        tmp += array[i] - array[k -(i - index + 1)]
        count = count if count < tmp else tmp

    if n > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        tmp += -array[i] + array[k + i - 1]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

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

print(ans(index, x, k))

Submission status: Runtime Error

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-(index-1)) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index+1)):
        tmp = array[i]
        tmp += array[i] - array[k -(i - index + 1)]
        count = count if count < tmp else tmp

    if n > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        tmp += -array[i] + array[k -(start - index)]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

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

print(ans(index, x, k))

Submission status: Runtime Error

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

ans = sum([abs(i) for i in x])
for i in range(N - K + 1):
    t = min(abs(x[i])+abs(x[i+K-1] - x[i]),abs(x[i+K-1]+abs(x[i+K-1]-x[i]))
    if t < ans:
        ans = t:

print(ans)

Submission status: Runtime Error

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

xp = []
xn = []
for i in x:
    if i >= 0:
        xp += [i]
    else:
        xn += [abs(i)]
    
xn.sort()
pl = len(xp)
pn = len(xn)

ans = 2 ** 30

for i in range(pl):
    if K-2-i < pn:
        if K-2-i >= 0:
            tmp = xp[i] * 2 + xn[K-2-i]
        else:
            tmp = xp[i] 
    ans = min(ans,tmp)


for i in range(pn):
    if K-2-i < pl:        
        if K-2-i >= 0:
            tmp = xn[i] * 2 + xp[K-2-i]
        else:
            tmp = xn[i]
        
    ans = min(ans,tmp)

print(ans)

Submission status: Runtime Error

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

xp = []
xn = []
for i in x:
    if i >= 0:
        xp += [i]
    else:
        xn += [abs(i)]
    
xn.sort()
pl = len(xp)
pn = len(xn)

ans = 2 ** 30

for i in range(K):
    if i < pl and K-2-i < pn :
        if K-2-i >= 0:
            tmp = xp[i] * 2 + xn[K-2-i]
        else:
            tmp = xp[i] 
    ans = min(ans,tmp)


for i in range(K):
    if i < pn and K-2-i < pl and K-2-i >= 0:        
        if K-2-i >= 0:
            tmp = xn[i] * 2 + xp[K-2-i]
        else:
            tmp = xn[i]
        
    ans = min(ans,tmp)

print(ans)

Submission status: Runtime Error

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

xp = []
xn = []
for i in x:
    if i >= 0:
        xp += [i]
    else:
        xn += [abs(i)]
    
xn.sort()
pl = len(xp)
pn = len(xn)

ans = 2 ** 31 - 1

for i in range(K):
    if i < pl and K-2-i < pn :
        if K-2-i >= 0:
            tmp = xp[i] * 2 + xn[K-2-i]
        else:
            tmp = xp[i] 
    ans = min(ans,tmp)


for i in range(K):
    if i < pn and K-2-i < pl and K-2-i >= 0:        
        if K-2-i >= 0:
            tmp = xn[i] * 2 + xp[K-2-i]
        else:
            tmp = xn[i]
        
    ans = min(ans,tmp)

print(ans)

Submission status: Runtime Error

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

infty = 10**9
c = bisect.bisect_left(x,0)
if x[c] > 0:
    K += 1
    N += 1
    x.insert(c,0)

if c >= K-1:
    turn = c-(K-1)
    ans = -x[turn]
    turn += 1
else:
    turn = 0
    ans = infty

while turn <= c and turn + K <= N:
    L = -x[turn]
    R = x[turn + K - 1]
    cur = L + R + min(L,R)
    if cur < ans:
        ans = cur
    turn += 1

print(ans)

Submission status: Runtime Error

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

import numpy as np 


flag = 0
x_plus = [i for i in X if i>=0]
x_minus = [i for i in X if i<0]
x_minus.reverse()

len_plus = len(x_plus)
len_minus = len(x_minus)

use_x = []
answer = 0

i = 0
j = 0
if len_plus==0:
    answer = abs(x_minus[K-1])
    
if len_minus==0:
    answer = abs(x_plus[K-1])

if len_plus!=0 and len_minus!=0:
    while(1):
        if len(use_x)==K: break
        if x_plus[i] > abs(x_minus[j]):
            use_x.append(x_minus[j])
            if j+1>=len_minus:
                i += 1
            else:
                j += 1
        else:
            use_x.append(x_plus[i])
            if i+1>=len_minus:
                j += 1
            else:
                i += 1
    use_x.sort()
    # print(use_x)
    answer = use_x[-1]-use_x[0]+min(use_x[-1],abs(use_x[0]))

print(answer)

Submission status: Runtime Error

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

for i in range(N-K):
    L.append(abs(x[i]) + x[K+i-1] - x[i])
    L.append(abs(x[K+i-1]) + x[K+i-1] - x[i])
    
if N != 1:
    print(min(L))
else:
    print(abs(x[0]))

Submission status: Runtime Error

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

neg_x = []
for i in range(n):
    if x[i] < 0:
        neg_x.append(x[i])
    if x[i] >= 0:
        break
pos_x = x[len(neg_x):]
neg_x = neg_x[::-1]

if len(neg_x) == 0:
    print(pos_x[k - 1])
elif len(pos_x) == 0:
    print(-1 * neg_x[k - 1])
else:
    total_all = []
    for i in range(max(0, k - len(pos_x)), min(k, len(neg_x))):
        if i == 0:
            total1 = 0
        else:
            total1 = -1 * neg_x[i - 1]
        if k - i == 0:
            total2 = 0
        else:
            total2 = pos_x[k - i - 1]
        total = min(total1 * 2 + total2, total2 * 2 + total1)
        total_all.append(total)
    print(min(total_all))

Submission status: Runtime Error

Solution code:
"""
map(int,input().split())

X = input().split()
x = []
for i in range(0,len(X)):
    x.append(X[i])

"""
N,K = map(int,input().split())
X = input().split()
xPlus = []
xMinus = []
for i in range(0,len(X)):
    r = int(X[i])
    if r >= 0:
        xPlus.append(r)
    else:
        xMinus.append(abs(r))

if len(xMinus) != 0:
    xMinus.sort()

if len(xPlus) == 0:
    print(xMinus[K-1])
elif len(xMinus) == 0:
    print(xPlus[K-1])
else:
    mn = 10**10
    for k in range(0,K):
        notK = K-k
        if k >= len(xPlus):
            None
        if notK >= len(xMinus):
            None
        else:
            retp = xPlus[k-1]
            retm = xMinus[notK-1]
            ret = min(retp,retm)*2 + max(retp,retm)
            mn = min(mn,ret)
            #print(k,mn,retp,retm)
    print(mn)

Submission status: Runtime Error

Solution code:
"""
map(int,input().split())

X = input().split()
x = []
for i in range(0,len(X)):
    x.append(X[i])

"""
N,K = map(int,input().split())
X = input().split()
xPlus = []
xMinus = []
for i in range(0,len(X)):
    r = int(X[i])
    if r >= 0:
        xPlus.append(r)
    else:
        xMinus.append(abs(r))

if len(xMinus) != 0:
    xMinus.sort()

if len(xPlus) == 0:
    print(xMinus[K-1])
elif len(xMinus) == 0:
    print(xPlus[K-1])
else:
    mn = 10**10
    for k in range(0,K):
        notK = K-k
        if k > len(xPlus):
            None
        if notK > len(xMinus):
            None
        else:
            retp = xPlus[k-1]
            retm = xMinus[notK-1]
            ret = min(retp,retm)*2 + max(retp,retm)
            mn = min(mn,ret)
            #print(k,mn,retp,retm)
    print(mn)

Submission status: Runtime Error

Solution code:
"""
map(int,input().split())

X = input().split()
x = []
for i in range(0,len(X)):
    x.append(X[i])

"""
N,K = map(int,input().split())
X = input().split()
xPlus = []
xMinus = []
for i in range(0,len(X)):
    r = int(X[i])
    if r >= 0:
        xPlus.append(r)
    else:
        xMinus.append(abs(r))

if len(xMinus) != 0:
    xMinus.sort()

if len(xPlus) == 0:
    print(xMinus[K-1])
elif len(xMinus) == 0:
    print(xPlus[K-1])
else:
    mn = 10**10
    for k in range(0,K+1):
        notK = K-k
        if k > len(xPlus):
            None
        if notK > len(xMinus):
            None
        else:
            retp = xPlus[k-1]
            retm = xMinus[notK-1]
            ret = min(retp,retm)*2 + max(retp,retm)
            mn = min(mn,ret)
            #print(k,mn,retp,retm)
    print(mn)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
import copy

INF = 2*(10**8)+1
memo = INF  # メモ化用変数

def solve():
    N, K = list(map(int, input().split(" ")))
    X = [0]
    X.extend(list(map(int, input().split(" "))))
    X.sort()


    def dfs(depth, cost, i, Xs):
        global memo
        if depth == K:
            memo = min(cost, memo)
            return cost
        
        if cost >= memo:
            # 探索を打ち切る
            return INF
        
        ret1, ret2 = INF, INF
        if i+1 < len(Xs):
            # 右に移動
            tmpCost = abs(Xs[i] - Xs[i+1])
            tmpXs = copy.deepcopy(Xs)
            del tmpXs[i]
            ret1 = dfs(depth+1, cost+tmpCost, i, tmpXs)
        if i-1 >= 0:
            # 左に移動
            costCost = abs(Xs[i] - Xs[i-1])
            tmpXs = copy.deepcopy(Xs)
            del tmpXs[i]
            ret2 = dfs(depth+1, cost+tmpCost, i-1, tmpXs)
        
        return min(ret1, ret2)

    # 0座標のインデックスは？
    for i in range(len(X)):
        if X[i] == 0:
            break
    ans = dfs(0, 0, i, X)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
import copy

INF = 2*(10**8)+1
memo = INF  # メモ化用変数

def solve():
    N, K = list(map(int, input().split(" ")))
    X = [0]
    X.extend(list(map(int, input().split(" "))))
    X.sort()


    def dfs(depth, cost, i, Xs):
        global memo
        if depth == K:
            memo = min(cost, memo)
            return cost
        
        if cost > memo:
            # 探索を打ち切る
            return INF
        
        ret1, ret2 = INF, INF
        if i+1 < len(Xs):
            # 右に移動
            tmpCost = abs(Xs[i] - Xs[i+1])
            tmpXs = copy.deepcopy(Xs)
            del tmpXs[i]
            ret1 = dfs(depth+1, cost+tmpCost, i, tmpXs)
        if i-1 >= 0:
            # 左に移動
            costCost = abs(Xs[i] - Xs[i-1])
            tmpXs = copy.deepcopy(Xs)
            del tmpXs[i]
            ret2 = dfs(depth+1, cost+tmpCost, i-1, tmpXs)
        
        return min(ret1, ret2)

    # 0座標のインデックスは？
    for i in range(len(X)):
        if X[i] == 0:
            break
    ans = dfs(0, 0, i, X)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
x,neg,pos=[],[],[]
for i in input().split():
	i = int(i)
	x.append(i)
	if i < 0:
		neg.append(i)
	else:
		pos.append(i)
neg.reverse()

ans=[]

if len(neg) >= k:
	ans.append(-neg[k-1])
if len(pos) >= k:
	ans.append(pos[k-1])

for i in range(1, k):
	j = k - i
	if i >= len(neg) or j >= len(pos):
		continue
	ans.append(pos[j-1] - neg[i-1] + min(pos[j-1], -neg[i-1]))

print(min(ans))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = []
y = []
for i in input().split():
    n = int(i)
    if n < 0:
        x.append(-n)
    elif n == 0:
        K -= 1
    else:
        y.append(n)

x = x[::-1]

len_x = len(x)
len_y = len(y)
now = 10 ** 18

if len_x == 0:
    if len_y == 0:
        print(0)
    else:
        print(y[K-1])
elif len_y == 0:
    print(x[K-1])
else:
    for i in range(max(K - len_x, 0), K+1):
        if len_x < i:
            break
        if i == 0:
            now = min(x[K-1], now)
        elif i == K:
            now = min(y[K-1], now)
        else:
            now = min(min(x[i-1], y[K-i-1]) + x[i-1] + y[K-i-1], now)

    print(now)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = []
y = []
for i in input().split():
    n = int(i)
    if n < 0:
        x.append(-n)
    elif n == 0:
        K -= 1
    else:
        y.append(n)

x = x[::-1]

len_x = len(x)
len_y = len(y)
now = 10 ** 18

if len_x == 0:
    if len_y == 0:
        print(0)
    else:
        print(y[K-1])
elif len_y == 0:
    print(x[K-1])
else:
    for i in range(max(K - len_x, 0), K+1):
        if len_x < i:
            break
        print('x',i, K-i)
        if i == 0:
            now = min(x[K-1], now)
        elif i == K:
            now = min(y[K-1], now)
        else:
            now = min(min(x[i-1], y[K-i-1]) + x[i-1] + y[K-i-1], now)

    print(now)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = []
y = []
for i in input().split():
    n = int(i)
    if n < 0:
        x.append(-n)
    elif n == 0:
        K -= 1
    else:
        y.append(n)

x = x[::-1]

now = 10 ** 20
len_x = len(x)
len_y = len(y)

if len_x == 0:
    if len_y == 0:
        print(0)
    else:
        print(y[K-1])
elif len_y == 0:
    print(x[K-1])
else:
    for i in range(max(K - len_x, 0), K):
        if len_x < i:
            break
        now = min(min(x[i-1], y[K-i-1]) + x[i-1] + y[K-i-1], now)

    print(now)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    if A[i]>0:
        min_plus=i
        break
t=10**9
for k in range(min(K,N-K+1)):
    if min_plus > k+K-1:
        t=min(t,-A[k])
    elif min_plus > k:
        t0=min(-A[k],A[k+K-1])
        t=min(t,A[k+K-1]-A[k]+t0)
    else:
        t=min(t,A[k+K-1])
print(t)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    if A[i]>0:
        min_plus=i
        break
t=10**9
for k in range(min(K,N-K+1)):
    if min_plus > k+K-1:
        t=min(t,-A[k])
    elif min_plus > k:
        t0=min(-A[k],A[k+K-1])
        t=min(t,A[k+K-1]-A[k]+t0)
    else:
        t=min(t,A[k+K-1])

Submission status: Runtime Error

Solution code:
INF = 10**9

N, K = map(int, input().split())
x_list = [int(i) for i in input().split()]
for i in range(N):
    if x_list[i] >= 0:
        T = i
        break
else:
    print(abs(x_list[-K])
    exit()
min_take_time = INF
for i in range(K+1):
    if T-K+i < 0 or T+i>N:
        continue
    if x_list[T-K+i] *  x_list[T+i-1] <= 0:
        take_time = min(abs(x_list[T-K+i]), abs(x_list[T+i-1])) * 2 + max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    else:
        take_time = max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    min_take_time = min(min_take_time, take_time)


print(min_take_time)

Submission status: Runtime Error

Solution code:
INF = 10**9

N, K = map(int, input().split())
x_list = [int(i) for i in input().split()]
for i in range(N):
    if x_list[i] >= 0:
        T = i
        break
min_take_time = INF
for i in range(K+1):
    if T-K+i < 0 or T+i>N:
        continue
    if x_list[T-K+i] *  x_list[T+i-1] <= 0:
        take_time = min(abs(x_list[T-K+i]), abs(x_list[T+i-1])) * 2 + max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    else:
        take_time = max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    min_take_time = min(min_take_time, take_time)


print(min_take_time)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    c = s.count("0")
    print(2*min(c,len(s)-c))
    return

#B
def B():
    n = I()
    h = LI()
    ans = 0
    m = min(h)
    while m:
        ans += m
        k = max(h)
        for i in range(n):
            if h[i] >= m:
                h[i] -= m
                if h[i]:k = min(k,h[i])
            else:break
        m = k
    print(ans)
    return

#C
def C():
    n,k = LI()
    x = LI()
    ans = float("inf")
    j = bisect.bisect_left(x,0)
    if n == 1:
        print(abs(x[0]))
        quit()
    if x[j] == 0:
        k -= 1
        x.pop(j)
        n -= 1
    for i in range(n):
        if i < j:
            if abs(i-j) > k:continue
            elif abs(i-j) == k:ans = min(ans,abs(x[i]))
            elif k+i-1 < n:
                ans = min(ans,abs(x[i])+x[k+i-1]-x[i])
        else:
            if abs(i-j)+1 > k:continue
            elif abs(i-j)+1 == k:ans = min(ans,x[i])
            elif i-k+1 >= 0:
                ans = min(ans,abs(x[i])+x[i]-x[i-k+1])

    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import itertools
import collections
from queue import Queue
import functools
import math
INF = 10**9


def solve(N: int, K: int, x: "List[int]"):
    x_neg = ([-xx for xx in x if xx < 0]+[0])[::-1]
    x_pos = [0] + [xx for xx in x if xx >= 0]

    # 左先を考える
    # print(x_neg)
    # print(x_pos)
    left = [x_neg[i]*2 + x_pos[K-i] for i in range(len(x_neg))
            if K-i < len(x_pos)]
    if len(left) > 0:
        ansleft = min(left)
    else:
        ansleft = INF

    # 右先
    right = [x_pos[i]*2 + x_neg[K-i] for i in range(len(x_pos))
             if K-i < len(x_neg)]
    if len(right) > 0:
        ansright = min(right)
    else:
        ansright = INF
    # print(left)
    # print(right)
    print(min(ansright, ansleft))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, x)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

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

x_minus = []
x_plus = []

for i in range(n):
    if x[i] >= 0:
        x_plus.append(x[i])
    else:
        x_minus.append(x[i])
x_minus.reverse()

ans = 100000000000
for i in range(len(x_minus)):
    if len(x_plus) + i >= k:
        length = abs(x_minus[i-1]) * 2 + x_plus[k-i-1]
        ans = min(ans,length)

if ans == 100000000000:
    ans = x_plus[k-1]

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

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

x_minus = []
x_plus = []

for i in range(n):
    if x[i] >= 0:
        x_plus.append(x[i])
    else:
        x_minus.append(x[i])
x_minus.reverse()

ans = 10000000000
for i in range(len(x_minus)):
    if len(x_plus) + i >= k:
        length = abs(x_minus[i-1]) * 2 + x_plus[k-i-1]
        ans = min(ans,length)

if ans == 10000000000:
    ans = x_plus[k-1]

print(ans)

Submission status: Runtime Error

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

for i in range(n-k+1):
    r = x[i+k-1]
    l = x[i]
    length = abs(r - l) + min(abs(l), abs(r))
    if length < ans:
        ans = length

print(ans)

Submission status: Runtime Error

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

X=list(map(int,input().split()))
V=[]
for i in range(n-k):
    Y=X[i+k-1]-X[i]   
    if X[i+k-1]<0:
        V.append(Y-X[i+k-1])
    elif X[i+k-1]>=0 and X[i]<0:
        V.append(Y+min(abs(X[i]),abs(X[i+k-1])))
    else:
        V.append(Y+X[i])
if X==[0]:
    print(0)
else:
    print(min(V))

Submission status: Runtime Error

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

X=list(map(int,input().split()))
V=[]
for i in range(n-k):
    Y=X[i+k-1]-X[i]   
    if X[i+k-1]<0:
        V.append(Y-X[i+k-1])
    elif X[i+k-1]>=0 and X[i]<0:
        V.append(Y+min(abs(X[i]),abs(X[i+k-1])))
    else:
        V.append(Y+X[i])
print(min(V))

Submission status: Runtime Error

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

left = []
right = []
for x in X:
    if x<0:
        left.append(abs(x))
    else:
        right.append(x)
left.reverse()

# from math import inf
ans = 10**18

#i=>right
for i in range(K+1):
    if right[0]==0:
        if i==0:
            continue;
    temp = 0
    if len(left)==0:
        temp = right[K-1]
    elif len(right)==0:
        temp = left[K-1]
    elif len(left)>=(K-i):
        if len(right)>=i:
            temp0 = left[K-i-1] if not i==K else 0
            temp1 = right[i-1] if not i==0 else 0
            temp  = temp0+temp1 + min(temp0, temp1)
        else:
            temp0 = right[-1]
            temp1 = left[K-len(right)-1]
            temp  = temp0+temp1 + min(temp0, temp1)
    else:
        temp0 = left[-1]
        temp1 = right[K-len(left)-1]
        temp  = temp0+temp1 + min(temp0, temp1)
    ans = min(ans, temp)

print(ans)