Problem p03274 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import bisect

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

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    #print(i)
    m=min(0,X[i])
    M=max(0,X[i+K-1])
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    if i+K==N:
        break
    
print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import bisect

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

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    m=min(0,X[i])
    M=max(0,X[i+K-1])
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    
print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import bisect

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

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    if i==start:
        temp=X[i:i+K]
    else:
        temp.pop(0)
        temp.append(X[i+K-1])
    #print(temp)
    m=min(0,min(temp))
    M=max(0,max(temp))
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    
print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    main=main[:]
    sub=list(map(lambda x:x*2,sub))
    
    ans=0
    for _ in range(K):
        if len(main)==0:
            ans+=sub.pop(-1)
        elif len(sub)==0:
            ans+=main.pop(-1)
        elif main[-1]<=sub[-1]:
            ans+=main.pop(-1)
        else:
            ans+=sub.pop(-1)
    
    return ans
    
i=0
while X[i]<0:
    i+=1

neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]
d_pos=[pos[i+1]-pos[i] for i in range(len(pos)-1)][::-1]#popしたいのでひっくり返す
d_neg=[neg[i+1]-neg[i] for i in range(len(neg)-1)][::-1]

a=(journey(d_pos,d_neg))
b=(journey(d_neg,d_pos))

print(min(a,b))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    if len(main)==1:
        main.append(10**18)
    elif len(sub)==1:
        sub.append(10**18)
    dp=0
    nm=1
    ns=1
    ans=0
    for _ in range(K):
        dism=(main[nm]-main[nm-1])
        diss=(sub[ns]-sub[ns-1])
        if nm==len(main)-1:
            ans+=2*diss
            ns+=1
        elif ns==len(sub)-1:
            ans+=dism
            nm+=1
        elif dism>2*diss:
            ans+=diss*2
            ns+=1
        else:
            ans+=dism
            nm+=1
    return ans

i=0
while X[i]<0:
    i+=1

neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]


print(min(journey(pos,neg),journey(neg,pos)))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    if len(main)==1:
        main.append(10**18)
    elif len(sub)==1:
        sub.append(10**18)
    dp=0
    nm=1
    ns=1
    ans=0
    for _ in range(K):
        dism=(main[nm]-main[nm-1])
        diss=(sub[ns]-sub[ns-1])
        print(nm,ns)
        if nm==len(main)-1:
            ans+=2*diss
            ns+=1
        elif ns==len(sub)-1:
            ans+=dism
            nm+=1
        elif dism>2*diss:
            ans+=diss*2
            ns+=1
        else:
            ans+=dism
            nm+=1
    return ans

i=0
while X[i]<0:
    i+=1


neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]

print(pos)
print(neg)

print(min(journey(pos,neg),journey(neg,pos)))