Buggy python submission (verdict: Runtime Error):
n, k = list(map(int, input().split(" ")))
x = list(map(int, input().split(" ")))

if((0 in x) == True):
    state = x.index(0)
    count = 1
else:
    x.append(0)
    x = sorted(x)
    state = x.index(0)
    count = 0

ans = int(1e9)
for left in range(k-count+1):
    right = k - count - left
    if(((state - left) >= 0) and ((state + right) <= (len(x)))):
        if(right > 0): tmp = -2 * x[state-left]
        else: tmp = -1 * x[state-left]
        tmp += x[state+right]
        if(ans > tmp): ans = tmp

print(ans)

Fixed submission:
n, k = list(map(int, input().split(" ")))
x = list(map(int, input().split(" ")))

if((0 in x) == True):
    state = x.index(0)
    k -= 1
else:
    x.append(0)
    x = sorted(x)
    state = x.index(0)

ans = int(1e10)
for left in range(k+1):
    right = k - left
    if(((state - left) >= 0) and ((state + right) <= (len(x) - 1))):
        if(right > 0): tmp = -2 * x[state-left]
        else: tmp = -1 * x[state-left]
        tmp += x[state+right]
        if(ans > tmp): ans = tmp

        tmp = -1 * x[state-left]
        if(left > 0): tmp += 2 * x[state+right]
        else: tmp += x[state+right]
        if(ans > tmp): ans = tmp

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

import copy

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

if((0 in x) == True):
    flag = n*[1]
    flag[x.index(0)] = 0
    state = x.index(0)
    count = 1
else:
    x.append(0)
    x = sorted(x)
    flag = (n+1)*[1]
    flag[x.index(0)] = 0
    state = x.index(0)
    count = 0

def search(x, flag, state, count, time, ans):
    if(count == k):
        if(ans > time): ans = time
        return ans

    if(sum(flag[:state]) != 0):
        tmp_state = state
        tmp_time = time
        while(True):
            state -= 1
            if(flag[state] == 1): break
        flag[state] = 0
        count += 1
        time += x[tmp_state] - x[state]

        ans = search(x, flag, state, count, time, ans)
        
        time = tmp_time
        count -= 1
        flag[state] = 1
        state = tmp_state

    if(sum(flag[state:]) != 0):
        tmp_state = state
        tmp_time = time
        while(True):
            state += 1
            if(flag[state] == 1): break
        flag[state] = 0
        count += 1
        time += x[state] - x[tmp_state]

        ans = search(x, flag, state, count, time, ans)
        
        time = tmp_time
        count -= 1
        flag[state] = 1
        state = tmp_state

    return ans

ans = search(x, flag, state, count, 0, int(1e9))

print(ans)

Fixed submission:
n, k = list(map(int, input().split(" ")))
x = list(map(int, input().split(" ")))

if((0 in x) == True):
    state = x.index(0)
    k -= 1
else:
    x.append(0)
    x = sorted(x)
    state = x.index(0)

ans = int(1e10)
for left in range(k+1):
    right = k - left
    if(((state - left) >= 0) and ((state + right) <= (len(x) - 1))):
        if(right > 0): tmp = -2 * x[state-left]
        else: tmp = -1 * x[state-left]
        tmp += x[state+right]
        if(ans > tmp): ans = tmp

        tmp = -1 * x[state-left]
        if(left > 0): tmp += 2 * x[state+right]
        else: tmp += x[state+right]
        if(ans > tmp): ans = tmp

print(ans)