Problem p03274 (python)

Fixed submission:
n,k=map(int,input().split())
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi==0:
        k-=1
    else:
        x.append(xi)
x.sort()
ans=float("INF")
zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi==0:
        k-=1
    else:
        x.append(xi)
x.sort(x)
ans=float("INF")
zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0+1:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k==0:
    print(0)
    exit()
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k==0:
    print(0)
    exit()
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k<len(a):
    ans=min(ans,a[k-1])
if k<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)