Problem p03274 (python)

Fixed submission:
# -*- coding: utf-8 -*-

n, k = map(int, input().split())
X = list(map(int, input().split()))
ans = 10**10
for i in range(n-k+1):
    left = X[i]
    right = X[i+k-1]
    ans = min(ans, abs(left)+abs(left-right), abs(right)+abs(right-left))
print(ans)

Buggy submission (verdict: Runtime Error):
for i in range(n-k+1):
    xtmp = x[i:i+k]
#     print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

print(MIN)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
x = list(map(int, input().split()))

for i in range(n-k+1):
    xtmp = x[i:i+k]
    print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

    i+=1

print(MIN)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
x = list(map(int, input().split()))

# if 0 in x:
#     x.remove(0)
#     k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
    i=0
    if x[0]>=0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
            xtmp = x[i:i+k]
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
            if i<n-k:
                if x[i+k]>0 and x[i+k-1]<0:
                    flag02=1

            if flag02==1:
                MIN = min(MIN, abs(xtmp[0]))             

            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))

            i+=1

        print(MIN)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
x = list(map(int, input().split()))

if 0 in x:
    x.remove(0)
    k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
    i=0
    if x[0]>=0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
            xtmp = x[i:i+k]
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
            if i<n-k:
                if x[i+k]>0 and x[i+k-1]<0:
                    flag02=1

            if flag02==1:
                MIN = min(MIN, abs(xtmp[0]))             

            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))

            i+=1

        print(MIN)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
x = list(map(int, input().split()))

if 0 in x:
    x.remove(0)
    k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
#     MIN = 2*abs(x[0]+x[-1])
    
    i=0
    if x[0]>0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
    #     for i in range(n-k+1):
#             print(i, x[i])
            xtmp = x[i:i+k]
#             print(xtmp)
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
            if i<n-k:
                if x[i+k]>0 and x[i+k-1]<0:
                    flag02=1

            if flag02==1:
                MIN = min(MIN, abs(xtmp[0]))             

    #         if xtmp[0] != 0 and xtmp[-1]!=0:
            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))
    #         elif xtmp[0]==0:
    #             MIN = min(MIN, abs(xtmp[-1]))
    #         else:
    #             MIN = min(MIN, abs(xtmp[0]))       


            i+=1

        print(MIN)