qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n, m = map(int, input().split())
lis = list(map(int, input().split()))
su = []

for i in range(n):
    su.append(lis[i] - lis[0])

print(su)
res = 10**9
for i in range(n - m + 1):
    res = min(res, su[i + m - 1] - su[i] + min(abs(lis[i]), abs(lis[i + m - 1])))
print(res)


Fixed code:
n, m = map(int, input().split())
lis = list(map(int, input().split()))
su = []

for i in range(n):
    su.append(lis[i] - lis[0])

res = 10**9
for i in range(n - m + 1):
    res = min(res, su[i + m - 1] - su[i] + min(abs(lis[i]), abs(lis[i + m - 1])))
print(res)
Buggy code:
N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
dis.append(Lright[K-1])
dis.append(Lleft[K-1])


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    d += Lleft[remain-1]
    dis.append(d)
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    d += Lright[remain-1]
    dis.append(d)
print(min(dis))


Fixed code:
N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
try :
    dis.append(Lright[K-1])
    dis.append(Lleft[K-1])
except:
    pass


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    try :
        d += Lleft[remain-1]
        dis.append(d)
    except:
        pass
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    try:
        d += Lright[remain-1]
        dis.append(d)
    except:
        pass
print(min(dis))
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

dist = []
for i in range(N-K+1): 
    l, r = x[i], x[i+k-1]
    t = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    dist.append(t)

print(min(dist))

Fixed code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

dist = []
for i in range(N-K+1): 
    l, r = x[i], x[i+K-1]
    t = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    dist.append(t)

print(min(dist))
Buggy code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

for j in range(N - K):
    L = j
    R = j + K - 1
    sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
    sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
    sum = min(sumL, sumR)
    if sum < minMin:
        minMin = sum

print(minMin)

Fixed code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

if N == 1:
    minMin = listMap[0]

for j in range(N - (K - 1)):
    L = j
    R = j + (K - 1)
    sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
    sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
    sum = min(sumL, sumR)
    if sum < minMin:
        minMin = sum

print(minMin)
Buggy code:
num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

    print(time)


Fixed code:
num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

print(time)
Buggy code:
n, k = map(int, input().split())
x = [int(i) for i in input().split()]

# c = combinations(x, k)
c = []
for i in range(n - k):
    c.append(x[i : i + k])


# print(list(c))

ans = 10 ** 10

for g in c:
    a = max(0, max(g))
    b = min(0, min(g))
    # print(a, b)
    if (a * b) < 0:
        tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
    else:
        tmp = abs(a) + abs(b)
    ans = min(ans, tmp)

print(ans)


Fixed code:
n, k = map(int, input().split())
x = list(map(int, input().split()))

# c = combinations(x, k)
c = []
for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    c.append([l, r])


# print(list(c))

ans = 10 ** 10

for l, r in c:
    tmp = min(abs(l) + abs(r - l), abs(r) + abs(r - l))
    ans = min(ans, tmp)

# for g in c:
#     a = max(0, max(g))
#     b = min(0, min(g))
#     # print(a, b)
#     if (a * b) < 0:
#         tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
#     else:
#         tmp = abs(a) + abs(b)
#     ans = min(ans, tmp)

print(ans)
Buggy code:
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
mini = 1000000000
for i in range(N - K):
    if min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i]) < mini:
        mini = min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i])

print(mini)

Fixed code:
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
mini = 1000000000
for i in range(N - K + 1):
    if min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i]) < mini:
        mini = min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i])

print(mini)
Buggy code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=float('inf')

for i in range(0,n-k+1):
  b=a[i]
  c=a[i+k-1]
  if c<=0:
    ans=min(ans,abs(b)-abs(c))
  elif b>=0:
    ans=min(ans,abs(c)-abs(b))
  else:
    ans=min(ans,abs(c)+abs(b)*2,abs(c)*2+abs(b))
    
print(ans)

Fixed code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=float('inf')

for i in range(0,n-k+1):
  b=a[i]
  c=a[i+k-1]
  if c<=0:
    ans=min(ans,abs(b))
  elif b>=0:
    ans=min(ans,abs(c))
  else:
    ans=min(ans,abs(c)+abs(b)*2,abs(c)*2+abs(b))
    
print(ans)
Buggy code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(N-K+1):
  	l=i
    r=l+K-1
    ans=min(ans,min(abs(X[l]),abs(X[r]))+X[r]-X[l])
print(ans)

Fixed code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
ans=float('inf')
for l in range(N-K+1):
    r=l+K-1
    ans=min(ans,min(abs(X[l]),abs(X[r]))+X[r]-X[l])
print(ans)
Buggy code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
lis=[]
for i in range(n-k+1):
  if x[i]<=0 and x[i+k-1]>=0:
    lis.append(min(x[i+k-1]-2*x[i],abs(x[i])-2*x[i+k-1]))
  elif x[i]>0 and x[i+k-1]<0:
    lis.append(min(x[i]-2*x[i+k-1],abs(x[i+k-1])-2*x[i]))
  else:
    lis.append(max(abs(x[i]),abs(x[i+k-1])))
print(min(lis))


Fixed code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
lis=[]
for i in range(n-k+1):
  if x[i]<=0 and x[i+k-1]>=0:
    lis.append(min(x[i+k-1]-2*x[i],abs(x[i])+2*x[i+k-1]))
  elif x[i]>0 and x[i+k-1]<0:
    lis.append(min(x[i]-2*x[i+k-1],abs(x[i+k-1])+2*x[i]))
  else:
    lis.append(max(abs(x[i]),abs(x[i+k-1])))
print(min(lis))
Buggy code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9 + 7
for i in range(N - K + 1):
	print(i)
	print(x[i],x[i+K-1])
	if ans > abs(x[i]) + abs(x[i+K-1]) :
		ans   = x[K-1+i] - x[i] + min( abs(x[K-1+i]) , abs(x[i]) )
print(ans)


Fixed code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9 + 7
for i in range(N - K + 1):
	ans = min(ans, x[K-1+i] - x[i] + min( abs(x[K-1+i]) , abs(x[i]) ))
print(ans)
Buggy code:
N, K = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]
pos_i = -1
for i in range(len(x)):
    if x[i] >= 0:
        pos_i = i
        break

min_t = 200000000
if pos_i == -1:
    min_t = -x[-K]
else:
    for i in range(N-K+1):
        if i+K < pos_i:
            min_t = -x[pos_i-K]
        elif pos_i <= i:
            min_t = x[pos_i+K-1]
        else:
            min_t = min(min_t, x[K+i-1]-2*x[i], x[K+i-1]*2-x[i])

print(min_t)

Fixed code:
N, K = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]
pos_i = -1
for i in range(len(x)):
    if x[i] > 0:
        pos_i = i
        break

min_t = 300000000
if pos_i == -1:
    min_t = -x[-K]
else:
    for i in range(N-K+1):
        if i+K-1 < pos_i:
            min_t = min(min_t, -x[pos_i-K])
        elif pos_i <= i:
            min_t = min(min_t, x[pos_i+K-1])
        else:
            min_t = min(min_t, x[K+i-1]-x[i]*2, x[K+i-1]*2-x[i])

print(min_t)
Buggy code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
time = 10**9

for i in range(n - k + 1):
    x_k = [ x[i], x_k[i+k-1] ]
    #print(x_k)
    
    if x_k[0] >= 0:
        t = x_k[1]
            
    elif x_k[1] <= 0:
        t = -x_k[0]
    
    else:
        t = min(-x_k[0] + x_k[1] *2,-x_k[0] * 2 + x_k[1])

    time = min(t, time)
    
print(time)


Fixed code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
time = 10**9
x_k = [0, 0]
for i in range(n - k + 1):
    x_k[0] =  x[i] 
    x_k[1] =  x[i+k-1] 
    #print(x_k)
    
    if x_k[0] >= 0:
        t = x_k[1]
            
    elif x_k[1] <= 0:
        t = -x_k[0]
    
    else:
        t = min(-x_k[0] + x_k[1] *2,-x_k[0] * 2 + x_k[1])

    time = min(t, time)
    
print(time)
Buggy code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
ans = float('inf')

for i in range(N - K + 1):
    ans = min(ans, min(abs(x[i]), abs(x[i+K-1])) * 2 + max(abs(x[i]), abs(x[i+K-1])))
print(ans)

Fixed code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
ans = float('inf')

for i in range(N - K + 1):
    ans = min(ans, min(abs(x[i]), abs(x[i+K-1])) + abs(x[i] - x[i+K-1]))
print(ans)
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 3*10**8

for i in range(N-K+1):
    ans1 = abs(x[i])+abs(x[i]-x[i+N-K])
    ans2 = abs(x[i+N-K])+abs(x[i]-x[i+N-K])
    ans = min(ans, ans1, ans2)
print(ans)

Fixed code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 3*10**8

for i in range(N-K+1):
    ans1 = abs(x[i])+abs(x[i]-x[i+K-1])
    ans2 = abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans = min(ans, ans1, ans2)
print(ans)
Buggy code:
N,K=map(int, input().split())
Candles=list(map(int, input().split()))

Distance=[]
for i in range(N-K):
        Distance.append(abs(Candles[i]-Candles[i+K-1])+min(abs(Candles[i]),abs(Candles[i+K-1])))
print(min(Distance))

Fixed code:
N,K=map(int, input().split())
Candles=list(map(int, input().split()))

Distance=[]
for i in range(N-K+1):
        Distance.append(abs(Candles[i]-Candles[i+K-1])+min(abs(Candles[i]),abs(Candles[i+K-1])))
print(min(Distance))
Buggy code:
n,k=map(int,input().split())
X=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
  ans.append(X[i+k-1]-X[i]+min(abs(X[i+k+1]),abs(X[i])))
print(min(ans))

Fixed code:
n,k=map(int,input().split())
X=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
  ans.append(X[i+k-1]-X[i]+min(abs(X[i+k-1]),abs(X[i])))
print(min(ans))
Buggy code:
N, K = map(int, input().split())
arr = list(map(int, input().split()))

ans = float('inf')
for i in range(N+1-K):
    print(arr[i],arr[i+K-1])
    if arr[i]<= 0 and arr[i+K-1]>0:
        preans = min(abs(arr[i]*2) + arr[i+K-1], abs(arr[i]) + arr[i+K-1]*2)
    elif arr[i]<=0 and arr[i+K-1]<=0:
        preans = abs(arr[i])
    elif arr[i]>=0 and arr[i+K-1]>=0:
        preans = arr[i+K-1]
    ans = min(ans,preans)

print(ans)

Fixed code:
N, K = map(int, input().split())
arr = list(map(int, input().split()))

ans = float('inf')
for i in range(N+1-K):
    if arr[i]<= 0 and arr[i+K-1]>0:
        preans = min(abs(arr[i]*2) + arr[i+K-1], abs(arr[i]) + arr[i+K-1]*2)
    elif arr[i]<=0 and arr[i+K-1]<=0:
        preans = abs(arr[i])
    elif arr[i]>=0 and arr[i+K-1]>=0:
        preans = arr[i+K-1]
    ans = min(ans,preans)

print(ans)
Buggy code:
n,k=map(int, input().split(" "))
X=list(map(int, input().split(" ")))
import bisect
maxvalue=100000000000000000000000
place=bisect.bisect_left(X,0)

#if k<place:
#    X=X[place-k:]
#elif k<n-place-1:
#    X=X[:]

#print(place)
for i in range(n-k+1):
    low=X[i]
    high=X[i+k-1]
    if high*low<0:
        low=abs(low)
        high=abs(high)
        value=min(low,high)*2+max(low,high)
    else:
        low=abs(low)
        high=abs(high)
        value=low+high
    #print(value)
    if value<maxvalue:
        maxvalue=value
print(maxvalue)


Fixed code:
n,k=map(int, input().split(" "))
X=list(map(int, input().split(" ")))
#import bisect
maxvalue=100000000000000000000000
#place=bisect.bisect_left(X,0)

#if k<place:
#    X=X[place-k:]
#if k<n-place:
#    X=X[:-(n-place-k)]

#print(X)
#print(place)
for i in range(n-k+1):
    low=X[i]
    high=X[i+k-1]
    if high*low<0:
        low=abs(low)
        high=abs(high)
        value=min(low,high)*2+max(low,high)
    else:
        low=abs(low)
        high=abs(high)
        value=max(low,high)
    #print(value)
    if value<maxvalue:
        maxvalue=value
print(maxvalue)
Buggy code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=1e20
for i in range(n-k):
  r=i+k-1
  p1=abs(l[i])+abs(l[r]-l[i]);p2=abs(l[r])+abs(l[i]-l[r])
  ans=min(ans,p1,p2)
print(ans)

Fixed code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=1e20
for i in range(n-k+1):
  r=i+k-1
  p1=abs(l[i])+abs(l[r]-l[i]);p2=abs(l[r])+abs(l[i]-l[r])
  ans=min(ans,p1,p2)
print(ans)
Buggy code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(a) - K):
  l = a[i]
  r = a[i+K-1]
  ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(ans)

Fixed code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(a) - K + 1):
  l = a[i]
  r = a[i+K-1]
  ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(ans)
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = pow(10, 10)
for i in range(K, len(x)):
  ans = min(ans, abs(x[i-1]) + abs(x[i-1] - x[i-K]))
print(ans)

Fixed code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = pow(10, 10)
for i in range(N-K+1):
    temp_l = abs(x[i]) + abs(x[i]-x[i+K-1])
    temp_r = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans, temp_l, temp_r)
print(ans)
Buggy code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
flag = True
for i in range(n+1):
    if i == n or x[i] > 0:
        x.insert(i,0)
        zahyou = i
        break
ans = 10**18
print(x)
for i in range(k+1):
    mainasu = 0
    purasu = 0
    flag = True
    for j in range(k):
        a = zahyou-k+i+j
        if  a >= 0 and a < n:
            if x[a] < 0:
                mainasu += x[a+1]-x[a]
            else:
                purasu += x[a+1]-x[a]
        else:
            flag = False
        print(a,purasu,mainasu,flag)
    if mainasu != 0 and purasu != 0:
        purasu += min(mainasu,purasu)
    if flag:
        ans = min(ans,mainasu+purasu)
print(ans)

Fixed code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
flag = True
for i in range(n+1):
    if i == n or x[i] > 0:
        x.insert(i,0)
        zahyou = i
        break
ans = 10**18
for i in range(k+1):
    flag = True
    if zahyou + i < n+1 and zahyou -k+i >= 0:
        mainasu = x[zahyou]-x[zahyou-k+i]
        purasu = x[zahyou+i]-x[zahyou]
        if mainasu != 0 and purasu != 0:
            purasu += min(mainasu,purasu)
        ans = min(ans,mainasu+purasu)
print(ans)
Buggy code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ls = []
rs = []
for x in X:
    if x < 0:
        ls.append(-x)
    else:
        rs.append(x)
ls.reverse()

ans = float('inf')
if len(ls) >= K:
    ans = min(ans, ls[K-1])
if len(rs) >= K:
    ans = min(ans, rs[K-1])

for l in range(1,K):
    r = K-l
    if l >= len(ls) or r >= len(rs): break    
    ans = min(ans, ls[l-1]*2 + rs[r-1])
    ans = min(ans, rs[r-1]*2 + ls[l-1])
print(ans)

Fixed code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ls = []
rs = []
for x in X:
    if x < 0:
        ls.append(-x)
    else:
        rs.append(x)
ls.reverse()

ans = float('inf')
if len(ls) >= K:
    ans = min(ans, ls[K-1])
if len(rs) >= K:
    ans = min(ans, rs[K-1])

for l in range(1,K):
    if l > len(ls): break
    r = K-l
    if r > len(rs): continue
    ans = min(ans, ls[l-1]*2 + rs[r-1])
    ans = min(ans, rs[r-1]*2 + ls[l-1])
print(ans)
Buggy code:
INF = 10**9
N, K = map(int, input().split())
s = [int(c) for c in input().split()]

ans = INF
for i in range(N-K):
  l = i
  r = l+K-1
  ans = min(ans, abs(s[l])+abs(s[r]-s[l]), abs(s[r])+abs(s[r]-s[l]))

print(ans)


Fixed code:
INF = 10**9
N, K = map(int, input().split())
s = [int(c) for c in input().split()]

ans = INF
for i in range(N-K+1):
  l = i
  r = l+K-1
  ans = min(ans, abs(s[l])+abs(s[r]-s[l]), abs(s[r])+abs(s[r]-s[l]))

print(ans)
Buggy code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000

for i in range(N - K):
    part = problem[i:i+K]
    left = part[0]
    right = part[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left) 

print(ans)

Fixed code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000000000000000

if N == K:
    left = problem[0]
    right = problem[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left)
else:
    for i in range(N - K + 1):
        left = problem[i]
        right = problem[i+K-1]
        if left * right >= 0:
            if abs(right) > abs(left):
                if ans > abs(right):
                    ans = abs(right)
            else:
                if ans > abs(left):
                    ans = abs(left)
        else:
            if abs(right) > abs(left):
                if ans > (2 * abs(left) + abs(right)):
                    ans = (2 * abs(left) + abs(right))
            else:
                if ans > 2*abs(right)+abs(left):
                    ans = 2*abs(right)+abs(left)

print(ans)
Buggy code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)

Fixed code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k-1
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)
Buggy code:
def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    D = [0]*(N - 1)
    for i in range(1, N):
        D[i-1] = X[i] - X[i - 1]
    ans = 0
    t = 0
    T = [0]*(N - K + 1)
    for i in range(1, N):
        t += D[i-1]
        if i < K:
            if i == K - 1:
                T[0] = t
            continue
        t -= D[i - K]
        T[i - K + 1] = t
    # print(D, T)
    ans = K * 10**8
    for i in range(N - K + 1):
        a = T[i] + min(abs(X[i]), abs[X[i + K - 1])]
        ans = min(a, ans)
    print(ans)

if __name__ == '__main__':
    main()


Fixed code:
def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    D = [0]*(N - 1)
    for i in range(1, N):
        D[i-1] = X[i] - X[i - 1]
    ans = 0
    t = 0
    T = [0]*(N - K + 1)
    for i in range(1, N):
        t += D[i-1]
        if i < K:
            if i == K - 1:
                T[0] = t
            continue
        t -= D[i - K]
        T[i - K + 1] = t
    # print(D, T)
    ans = K * 10**8
    for i in range(N - K + 1):
        a = T[i] + min(abs(X[i]), abs(X[i + K - 1]))
        ans = min(a, ans)
    print(ans)

if __name__ == '__main__':
    main()
Buggy code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<0:
            best_time = -left + right +min(-left, right)
        else:
            break

    print(best_time)


if __name__ == "__main__":
    run()

Fixed code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<=0:
            if right <= 0:
                best_time = min(best_time, abs(left))
            else:
                best_time = min(abs(left) + right + min(abs(left), right), best_time)
        else:
            best_time = min(best_time, right)
            break

    print(best_time)


if __name__ == "__main__":
    run()