Problem p03274 (python)

Fixed submission:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

N, K = map(int, input().split())
X = list(map(int, input().split()))

has_zero = 0 in X
negs = [val for val in X if val<0]
poss = [val for val in X if val>0]
lenn = len(negs)
lenp = len(poss)
negs.append(0)
poss = [0] + poss

# print(negs)
# print(poss)

minval = 10**9
if has_zero: K-=1
for left in range(0, K+1):
    right = K-left
    if lenn<left or lenp<right: continue
    # print(left, right)
    negv = abs(negs[lenn-left])
    posv = abs(poss[right])
    val = max(negv, posv) + 2*min(negv, posv)
    if val < minval: minval = val
print(minval)

Buggy submission (verdict: Runtime Error):
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

N, K = map(int, input().split())
x = list(map(int, input().split()))
MAX = 10**9

has0 = 1 if 0 in x else 0
if has0 and K==1:
    print(0)
    exit()
if not(has0):
    x.append(0)
    x.sort()

# minusC = len([val for val in x if val<0])
# plusC = len([val for val in x if val>0])
ind0 = x.index(0)

res = MAX
for i, xi in enu(x):
    if i < ind0:
        min_flg = True
    elif i == ind0:
        continue
    else:
        min_flg = False
    rousoku_cnt = abs(i - ind0) + has0

    if K < abs(rousoku_cnt):
        continue
    elif K == abs(rousoku_cnt):
        res = min(res, abs(xi))
    else:
        if min_flg:
            val = abs(2*x[ind0+(K-rousoku_cnt)])
            res = min(res, abs(xi) + val)
        else:
            val = abs(2*x[ind0-(K-rousoku_cnt)])
            res = min(res, abs(xi) + val)
        # print(abs(xi) + val)
print(res)

Buggy submission (verdict: Runtime Error):
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

N, K = map(int, input().split())
x = list(map(int, input().split()))
MAX = 10**9

has0 = 1 if 0 in x else 0
if has0 and K==1:
    print(0)
    exit()
if not(has0):
    x.append(0)
    x.sort()

# minusC = len([val for val in x if val<0])
# plusC = len([val for val in x if val>0])
ind0 = x.index(0)

res = MAX
for i, xi in enu(x):
    if i < ind0:
        min_flg = True
    elif i == ind0:
        continue
    else:
        min_flg = False
    rousoku_cnt = abs(i - ind0) + has0

    if K < abs(rousoku_cnt):
        continue
    elif K == abs(rousoku_cnt):
        res = min(res, abs(xi))
    else:
        if min_flg:
            val = abs(2*x[ind0+(K-rousoku_cnt)])
            res = min(res, abs(xi) + val)
        else:
            val = abs(2*x[ind0-(K-rousoku_cnt)])
            res = min(res, abs(xi) + val)
        # print(abs(xi) + val)
print(res)