Problem p03274 (python)

Fixed submission:
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
elif len(Xpositive) == 0:
    sub = Xnegative[K-1]
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and -i-2 >= -len(Xpositive):
        distance = min(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

Buggy submission (verdict: Runtime Error):
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
elif len(Xpositive) == 0:
    sub = Xnegative[K-1]
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and i-2 < len(Xpositive):
        distance = max(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

Buggy submission (verdict: Runtime Error):
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
elif len(Xpositive) == 0:
    sub = Xnegative[K-1]
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and i < len(Xpositive):
        distance = max(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

Buggy submission (verdict: Runtime Error):
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and i < len(Xpositive):
        distance = max(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)