Problem p03276 (python)

Fixed submission:
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        if num_list != [0]:
            k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k <= len(positive_list):
    ans_list.append(positive_list[k - 1])

if k <= len(negative_list):
    ans_list.append(-1 * negative_list[k - 1])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

Buggy submission (verdict: Runtime Error):
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        if num_list != [0]:
        k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k <= len(positive_list):
    ans_list.append(positive_list[k - 1])

if k <= len(negative_list):
    ans_list.append(-1 * negative_list[k - 1])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

Buggy submission (verdict: Runtime Error):
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k <= len(positive_list):
    ans_list.append(positive_list[k - 1])

if k <= len(negative_list):
    ans_list.append(-1 * negative_list[k - 1])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

Buggy submission (verdict: Runtime Error):
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

ans_list.append(min(ans_positive))
ans_list.append(min(ans_negative))
if k < len(positive_list):
    ans_list.append(positive_list[k])

if k < len(negative_list):
    ans_list.append(-1 * negative_list[k])

print(min(ans_list))