Problem p03274 (python)

Fixed submission:
N, K = map(int, input().split())
arr = list(map(int, input().split()))

ans =[]
if arr[0]>0:
    ans.append(arr[K-1])

if arr[-1]<0:
    ans.append(abs(arr[-1*(K)]))

for i in range(N-K+1):
    if arr[i]*arr[i+K-1]<=0:
        left =2*abs(arr[i])+abs(arr[i+K-1])
        right=2*abs(arr[i+K-1])+abs(arr[i])
        ans.append(min(left,right))

print(min(ans))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
arr = list(map(int, input().split()))

if 0 in arr:
    K -= 1

if K==0:
    print(0)
    exit()

left =[]
right= []
for i in arr:
    if i<0:
        left.append(i*(-1))
    if i>0:
        right.append(i)

left = left[::-1]
if len(left)==0:
    print(right[K-1])
    exit()

if len(right)==0:
    print(left[K-1])
    exit()


if len(left)>=K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(left)<K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)>=K:
    ans =[]
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)<K:
    ans =[]
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
arr = list(map(int, input().split()))

if 0 in arr:
    K -= 1

if K==0:
    print(0)
    exit()

left =[]
right= []
for i in arr:
    if i<0:
        left.append(i*(-1))
    if i>0:
        right.append(i)

left = left[::-1]
if len(left)==0:
    print(right[K-1])
    exit()

if len(right)==0:
    print(left[K-1])
    exit()


if len(left)>=K and len(right)>=K:
    print(min(abs(left[K-1]),abs(right[K-1])))
    exit()

if len(left)<K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)>=K:
    ans =[]
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)<K:
    ans =[]
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
arr = list(map(int, input().split()))

if 0 in arr:
    K -= 1

if K==0:
    print(0)
    exit()

left =[]
right= []
for i in arr:
    if i<0:
        left.append(i*(-1))
    if i>0:
        right.append(i)

left = left[::-1]
if len(left)==0:
    print(right[K-1])
    exit()

if len(right)==0:
    print(left[K-1])
    exit()


if len(left)>=K and len(right)>=K:
    print(min(abs(left[K-1]),abs(right[K-1])))
    exit()

if len(left)<K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(len(left)-1):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)>=K:
    ans =[]
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)-1):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)<K:
    ans =[]
    for i in range(len(left)-1):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)-1):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()