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

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    if k-len(m) >= 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while s+i <= len(m) and z+s+i <= n:
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[k-1+s+i]), abs(x[s+i]) + abs(x[k-1+s+i])*2))
        
        if s+i == len(m):
            ans = min(ans, x[k-1+s+i])
        if z+s+i == len(m)-1:
            ans = min(ans, abs(x[s+i]))
        i += 1
    
    print(ans)
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

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

if len(x) == 1 and x[0] == 0:
    print(0)
    exit()

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

ans = float('inf')
if len(m) >= k:
    ans = min(ans,abs(m[-k]))
    m = m[-k+1:]
if len(p) >= k:
    ans = min(ans,p[k-1])
    p = p[:k-1]

x = m+p
i = 0
for i in range(len(x)-k+1):
    ans = min(ans, min(abs(x[i])*2+abs(x[i+k-1]), abs(x[i])+abs(x[i+k-1])*2))

print(ans)

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

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    z = len(m)-1
    if k-len(m) >= 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while s+i <= len(m) and z+s+i <= n:
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[k-1+s+i]), abs(x[s+i]) + abs(x[k-1+s+i])*2))
        
        if s+i == len(m):
            ans = min(ans, x[k-1+s+i])
        if z+s+i == len(m)-1:
            ans = min(ans, abs(x[s+i]))
        i += 1
        
    print(ans)    
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

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

if len(x) == 1 and x[0] == 0:
    print(0)
    exit()

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

ans = float('inf')
if len(m) >= k:
    ans = min(ans,abs(m[-k]))
    m = m[-k+1:]
if len(p) >= k:
    ans = min(ans,p[k-1])
    p = p[:k-1]

x = m+p
i = 0
for i in range(len(x)-k+1):
    ans = min(ans, min(abs(x[i])*2+abs(x[i+k-1]), abs(x[i])+abs(x[i+k-1])*2))

print(ans)

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

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    z = len(m)-1
    if k-len(m) > 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while z+s+i < n and s+i < len(m):
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[k-1+s+i]), abs(x[s+i]) + abs(x[k-1+s+i])*2))
        i += 1
        
    print(ans)
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

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

if len(x) == 1 and x[0] == 0:
    print(0)
    exit()

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

ans = float('inf')
if len(m) >= k:
    ans = min(ans,abs(m[-k]))
    m = m[-k+1:]
if len(p) >= k:
    ans = min(ans,p[k-1])
    p = p[:k-1]

x = m+p
i = 0
for i in range(len(x)-k+1):
    ans = min(ans, min(abs(x[i])*2+abs(x[i+k-1]), abs(x[i])+abs(x[i+k-1])*2))

print(ans)