Problem p03274 (python)

Fixed submission:
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 1e10

for i in range(N-K+1):
    l,r = x[i],x[i+K-1]
    if l < 0 and r <= 0: temp = -l
    elif l >= 0 and r > 0:   temp = r
    else:
        if -l < r:    temp = 2*(-l) + r
        else:    temp = -l + 2*r  
    
    if ans > temp:  ans = temp

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 0

if  0 in x:
    K-=1
    N-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 0

if  0 in x:
    K-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N-1:
        xp,xm = x[border:],x[:border][::-1]
        if N - border >= K and border >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 0

if  0 in x:
    K-=1
    x.pop(0)

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K+1]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,mmax,i = xn[-1],0,2
            while i <= K and xn[-i] > 0:
                i += 1
            if i <= K:  mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            mmax,i = xn[-1],2
            while i <= K and xn[-i] < 0:
                i += 1
            if i <= K:  pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 0

if  0 in x:
    K-=1
    x.pop(0)

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K+1]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,i = xn[-1],2
            while xn[-i] > 0:
                i += 1
            mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            mmax,i = xn[-1],2
            while xn[-i] < 0:
                i += 1
            pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)