Problem p03276 (python)

Fixed submission:
N,K=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(abs, a))
i=b.index(min(b))
c=list(filter(lambda x: x>=0, a))
e=sorted(c)
d=list(map(abs, filter(lambda x: x<0, a)))
f=sorted(d)

lp=len(c)
lm=len(d)
s=[]
if 0 in a:
    K -=1

if K==0:
    print(0)
elif lp==0:
    print(f[K-1])
elif lm==0:
    print(e[K-1])
elif K==N:
    print(min(2*e[lp-1]+f[lm-1],2*f[lm-1]+e[lp-1]))
else:
    for i in range(lp):
        if K-i<=lm:
            if i==K:
                u=e[i-1]
            else:
                u=2*e[i-1]+f[K-i-1]
            s.append(u)
    for t in range(lm):
        if K-t<=lp:
            if t==K:
                q=f[t-1]
            else:
                q=2*f[t-1]+e[K-t-1]
            s.append(q)

    print(min(s))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(abs, a))
i=b.index(min(b))
c=list(filter(lambda x: x>=0, a))
e=sorted(c)
d=list(map(abs, filter(lambda x: x<0, a)))
f=sorted(d)

lp=len(c)
lm=len(d)
s=[]
if 0 in a:
    K -=1
if K==0:
    print(0)
elif K==N:
    print(min(2*e[lp-1]+f[lm-1],2*f[lm-1]+e[lp-1]))
elif lp==0:
    print(f[lm-1])
elif lm==0:
    print(e[lp-1])
else:
    for i in range(lp):
        if K-i<=lm:
            if i==K:
                u=e[i-1]
            else:
                u=2*e[i-1]+f[K-i-1]
            s.append(u)
    for t in range(lm):
        if K-t<=lp:
            if t==K:
                q=f[t-1]
            else:
                q=2*f[t-1]+e[K-t-1]
            s.append(q)

    print(min(s))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(abs, a))
i=b.index(min(b))
c=list(filter(lambda x: x>=0, a))
e=sorted(c)
d=list(map(abs, filter(lambda x: x<0, a)))
f=sorted(d)

lp=len(c)
lm=len(d)
s=[]
if 0 in a:
    K -=1
if K==0:
    print(0)
elif K==N:
    print(min(2*e[lp-1]+f[lm-1],2*f[lm-1]+e[lp-1]))
elif lp==0:
    print(f[lm-1])
elif lm==0:
    print(e[lp-1])
else:
    for i in range(lp):
        if K-i<=lm:
            if i==K:
                u=e[i-1]
            else:
                u=2*e[i-1]+f[K-i-1]
            s.append(u)
    for t in range(lm):
        if K-t<=lp:
            if t==K:
                q=f[t-1]
            else:
                q=2*f[t-1]+e[K-t-1]
            s.append(q)

    print(min(s))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=list(map(int, input().split()))
if 0 in a:
    K -=1
if K==0:
    print(0)
else:
    b=list(map(abs, a))
    i=b.index(min(b))
    c=list(filter(lambda x: x>=0, a))
    e=sorted(c)
    d=list(map(abs, filter(lambda x: x<0, a)))
    f=sorted(d)

    lp=len(c)
    lm=len(d)
    s=[]
    v=[]
    for i in range(lp):
        if K-i<=lm:
            if i==K:
                u=e[i-1]
            else:
                u=2*e[i-1]+f[K-i-1]
            s.append(u)
    for t in range(lm):
        if K-t<=lp:
            if t==K:
                q=f[t-1]
            else:
                q=2*f[t-1]+e[K-t-1]
            s.append(q)

    print(min(s))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=list(map(int, input().split()))
if 0 in a:
  K -=1
b=list(map(abs, a))
i=b.index(min(b))
c=list(filter(lambda x: x>=0, a))
e=sorted(c)
d=list(map(abs, filter(lambda x: x<0, a)))
f=sorted(d)

lp=len(c)
lm=len(d)
s=[]
v=[]
for i in range(lp):
    if K-i<=lm:
        if i==K:
            u=e[i-1]
        else:
            u=2*e[i-1]+f[K-i-1]
        s.append(u)
for t in range(lm):
    if K-t<=lp:
        if t==K:
            q=f[t-1]
        else:
            q=2*f[t-1]+e[K-t-1]
        s.append(q)

print(min(s))