Problem p03274 (python)

Fixed submission:
# N, K = 3, 2
# X = [int(i) for i in '10 20 30'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)

res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])
# print(res)

if len_plus != 0:
    for i_minus in range(len_minus):
        if K-i_minus-1 <= len_plus:
            res.append(abs(-X_minus[-i_minus-1])*2 + X_plus[K-i_minus-2])

if len_minus >= K:
    if len_plus > 0:
        if X_plus[0]==0:
            res.append(-X_minus[-K+1])
    else:
        res.append(-X_minus[-K])
# print(res)
print(min(res))

Buggy submission (verdict: Runtime Error):
# N, K = 9, 5
# X = [int(i) for i in '-9 -7 -4 -3 0 1 2 3 4'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)
res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])

if len_plus != 0:
    for i_minus in range(len_minus):
        if K-i_minus <= len_plus:
            res.append(abs(-X_minus[-i_minus-1])*2 + X_plus[K-i_minus-2])
if len_minus >= K:
    if len_plus > 0:
        if X_plus[0]==0
            res.append(-X_minus[-K+1])
    else:
        res.append(-X_minus[-K])
print(min(res))

Buggy submission (verdict: Runtime Error):
# N, K = 9, 5
# X = [int(i) for i in '-9 -7 -4 -3 0 1 2 3 4'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)


res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])

if len_plus != 0:
    for i_minus in range(len_minus):
        if K-i_minus <= len_plus:
            res.append(abs(-X_minus[-i_minus-1])*2 + X_plus[K-i_minus-2])
if len_minus >= K:
    if X_plus[0] == 0:
        res.append(-X_minus[-K+1])
    else:
        res.append(-X_minus[-K])
print(min(res))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
X = [int(i) for i in input().split()]

X_plus = [0]
X_minus = [0]
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])

n_minus = len(X_minus)-1
n_plus = len(X_plus)-1
res = []
for i in range(1, n_minus+1):
    if n_plus > K-i-1:
        res.append(abs(X_minus[-i])*2 + abs(X_plus[K-i])) 

for i in range(1, n_plus+1):
    if n_minus >= K-i-1:
        res.append(abs(X_plus[i])*2 + abs(X_minus[min(-(K-i), 0)])) 
if n_minus >= K:
    res.append(abs(X_minus[-K]))
if n_plus >= K:
    res.append(X_plus[K])
        
print(min(res))