qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N, K = list(map(int,input().split()))
xlist = list(map(int,input().split()))

def f(list1):
  min1 = list1[0]
  max1 = list1[-1]
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<0 and max1 <0:
    return abs(min1)
  else:
    return total+min([abs(min1),abs(max1)])

ans = 2*10**8 + 2
for i in range(N-K+1):
  dist =f (xlist[i:i+K]) 
  if dist < ans :
    ans = dist

print(ans)



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

def f(min1,max1):
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<=0 and max1 <=0:
    return abs(min1)
  else:
    return min([-2*min1 + max1,-min1 + 2*max1])

ans = 2*10**9
for i in range(N-K+1):
  min1 = xlist[i]
  max1 = xlist[i+K-1]
  dist =f (min1,max1) 
  if dist < ans :
    ans = dist

print(ans)
Buggy code:
n, k = map(int, input().split())

x = list(map(int, input().split()))

min_dist = 10**20

dist = 0

for i in range(n-k):
    dist = (x[i+k-1] - x[i])
    dist += min(abs(x[i+k-1]), abs(x[i]))
    if dist < min_dist:
        min_dist = dist

print(min_dist)

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

x = list(map(int, input().split()))

if k == 1:
    min_num = 10**20
    for i in x:
        if abs(i) < min_num:
            min_num = abs(i)
    print(min_num)
    exit()

min_dist = 10**20

dist = 0

for i in range(n-k+1):
    dist = (x[i+k-1] - x[i])
    dist += min(abs(x[i+k-1]), abs(x[i]))
    if dist < min_dist:
        min_dist = dist

print(min_dist)
Buggy code:
N,K = map(int,input().split())
x = list(map(int,input().split()))

result = []
for i in range(N-K+1):
    s,g = i,i+K-1
    result.append(2*abs(x[s]) + abs(x[g]))
    result.append(abs(x[s]) + 2*abs(x[g]))
result.sort()
print(result[0])

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

result = []
for i in range(N-K+1):
    s,g = i,i+K-1
    result.append(abs(x[s]) + abs(x[s] - x[g]))
    result.append(abs(x[g]) + abs(x[g] - x[s]))
result.sort()
print(result[0])
Buggy code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
for i in range(n-k+1):
    if i==0:
        a=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
    else:
        a=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
print(a)

Fixed code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
for i in range(n-k+1):
    if i==0:
        a=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
    else:
        a=min(a,min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1])))
print(a)
Buggy code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
dis = N-K+1
for i in range(dis):
  b.append(min(abs(a[i])+abs(a[i]-a[i+dis-1]),abs(a[i+dis-1])+abs(a[i+dis-1]-a[i])))
print(min(b))
    

Fixed code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
dis = N-K+1
for i in range(dis):
  b.append(min(abs(a[i])+abs(a[i]-a[i+K-1]),abs(a[i+K-1])+abs(a[i+K-1]-a[i])))
print(min(b))
Buggy code:
INF = 10**9

N, K = map(int, input().split())
x_list = [int(i) for i in input().split()]
for i in range(N):
    if x_list[i] >= 0:
        T = i
        break
else:
    print(abs(x_list[-K])
    exit()
min_take_time = INF
for i in range(K+1):
    if T-K+i < 0 or T+i>N:
        continue
    if x_list[T-K+i] *  x_list[T+i-1] <= 0:
        take_time = min(abs(x_list[T-K+i]), abs(x_list[T+i-1])) * 2 + max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    else:
        take_time = max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    min_take_time = min(min_take_time, take_time)


print(min_take_time)

Fixed code:
INF = 10**9

N, K = map(int, input().split())
x_list = [int(i) for i in input().split()]
for i in range(N):
    if x_list[i] >= 0:
        T = i
        break
else:
    print(abs(x_list[-K]))
    exit()
min_take_time = INF
for i in range(K+1):
    if T-K+i < 0 or T+i>N:
        continue
    if x_list[T-K+i] *  x_list[T+i-1] <= 0:
        take_time = min(abs(x_list[T-K+i]), abs(x_list[T+i-1])) * 2 + max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    else:
        take_time = max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    min_take_time = min(min_take_time, take_time)


print(min_take_time)
Buggy code:
N, K = map(int, input().split())
x_list = list(map(int, input().split()))

result = []
for i in range(N-K):
    start = i
    end = i + K -1
    result.append(abs(x_list[start] + abs(x_list[end] - x_list[start])))
    result.append(abs(x_list[end]) +  abs(x_list[end] - x_list[start] ))
print(min(result))

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

result = []
for i in range(N-(K-1)):
    start = i
    end = i + (K -1)
    result.append(abs(x_list[start]) + abs(x_list[end] - x_list[start]))
    result.append(abs(x_list[end]) + abs(x_list[end] - x_list[start]))
print(min(result))
Buggy code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float("inf")
for i in range(N - K + 1):
    left, right = X[i], X[i + K - 1]
    dist = right - left
    ans = min(abs(left) + dist, abs(right) + dist)
print(ans)


Fixed code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float("inf")
for i in range(N - K + 1):
    left, right = X[i], X[i + K - 1]
    dist = right - left
    ans = min(ans, abs(left) + dist, abs(right) + dist)
print(ans)
Buggy code:
n,k=map(int,input().split())
add=list(map(int,input().split()))

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    a=min(ans,abs(add[l])+abs(add[r]-add[l]),abs(add[r]+abs(add[l]-add[r])))
    
print(ans)

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

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[r]-add[l]),
            abs(add[r])+abs(add[r]-add[l])
            )

print(ans)
Buggy code:
N,K=list(map(int,input().split()))
x=list(map(int,input().split()))
t_min=10**9+7
for i in range(N-K+1):
    t=x[i+K-1]-x[i]+min(np.abs(x[i]),np.abs(x[i+K-1]))
    if t<t_min:
        t_min=t
print(t_min)

Fixed code:
import numpy as np

N,K=list(map(int,input().split()))
x=list(map(int,input().split()))
t_min=10**9+7
for i in range(N-K+1):
    t=x[i+K-1]-x[i]+min(np.abs(x[i]),np.abs(x[i+K-1]))
    if t<t_min:
        t_min=t
print(t_min)
Buggy code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[k - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()

Fixed code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[K - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()
Buggy code:
n,k=map(int,input().split())
li=list(map(int,input().split()))
q=[]
for i in range(k,n):
  if i<0 or i-k+1>0:
    q.append(li[i-1]-li[i-k])
  else:
    q.append(li[i-1]-li[i-k]+min(li[i-1],abs(li[i-k])))
print(min(q))

    

Fixed code:
n,k=map(int,input().split())
li=list(map(int,input().split()))
q=[]
if k==n:
  q.append(li[n-1]-li[0]+min(abs(li[n-1]),abs(li[0])))
else:
  for i in range(k,n+1):
    if li[i-1]<0:
      q.append(-li[i-k])
    elif li[i-k]>0:
      q.append(li[i-1])
    else:
      q.append(li[i-1]-li[i-k]+min(li[i-1],abs(li[i-k])))
print(min(q))
Buggy code:

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

ns = 999999999

for i in range(n - k + 1):
    a = abs(x[i]) + (x[i + k - 1] - x[i])
    b = abs(x[i + k - 1]) + (x[i + k - 1] - x[i])
    ans = min(ans, a, b)
    
print(ans)

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

ans = 999999999

for i in range(n - k + 1):
    a = abs(x[i]) + (x[i + k - 1] - x[i])
    b = abs(x[i + k - 1]) + (x[i + k - 1] - x[i])
    ans = min(ans, a, b)
    
print(ans)
Buggy code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ans = 10**9
for i in range(N-K+1):
    ans = min(ans,abs(X[i])+abs(X[i+K-1])+min(abs(X[i]),abs(X[i+K-1])))
print(ans)

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

ans = 10**9
for i in range(N-K+1):
    ans = min(ans,X[i+K-1]-X[i]+min(abs(X[i]),abs(X[i+K-1])))
print(ans)
Buggy code:
n,k=map(int,input().split())
lst1=input()
lst2=lst1.split(" ")
lst=[]
for i in lst2:
    lst.append(int(i))

minr=[]
minl=[]

for i in range(n-k+1):
    l=i
    r=i+k-1
    minr.sppend(abs(lst[r])+abs(lst[r]-lst[l]))
    minl.append(abs(lst[l])+abs(lst[r]-lst[l]))

minr.sort()
minl.sort()

m=min(minr[0],minl[0])
print(m)

Fixed code:
n,k=map(int,input().split())
lst1=input()
lst2=lst1.split(" ")
lst=[]
for i in lst2:
    lst.append(int(i))

minr=[]
minl=[]

for i in range(n-k+1):
    l=i
    r=i+k-1
    minr.append(abs(lst[r])+abs(lst[r]-lst[l]))
    minl.append(abs(lst[l])+abs(lst[r]-lst[l]))

minr.sort()
minl.sort()

m=min(minr[0],minl[0])
print(m)
Buggy code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9
for i in range(k-1, n):
  dR = abs(x[i]) + abs(x[i] - x[i-k+1])
  dL = abs(x[i-k+1]) + abs(x[i] - x[i-k+1])
  ans = min(ans, dR, dL)
  print(i, dR, dL)
print(ans)

Fixed code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9
for i in range(k-1, n):
  dR = abs(x[i]) + abs(x[i] - x[i-k+1])
  dL = abs(x[i-k+1]) + abs(x[i] - x[i-k+1])
  ans = min(ans, dR, dL)
print(ans)
Buggy code:
N,K,*x=map(int,open(0).read().split());print(min(min(abs(x[i]),abs(x[i+k-1]))+x[i+k-1]-x[i]for i in range(n-k+1)))

Fixed code:
n,k,*x=map(int,open(0).read().split());print(min(min(abs(x[i]),abs(x[i+k-1]))+x[i+k-1]-x[i]for i in range(n-k+1)))
Buggy code:
from collections import deque

N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10*8

l=inf
dp=x[N-K:N-1]
i=0
while N-K-i>=0:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if dp[K-1]>=dp[0]:
            tmp=dp[0]*2+dp[K-1]
        else:
            tmp=dp[K-1]*2+dp[0]
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)

Fixed code:
from collections import deque
    
N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10**9
    
l=inf
dp=deque(x[N-K:N])
i=0
while True:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if abs(dp[K-1])>=abs(dp[0]):
            tmp=abs(dp[0])*2+abs(dp[K-1])
        else:
            tmp=abs(dp[K-1])*2+abs(dp[0])
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)
Buggy code:
N, K = map(int, input().split())
l = list(map(int, input().split()))

is_zero = l.count(0)
K -= is_zero

if K:
    ans = float('inf')
    for i in range(1, N-K+1):
        lb, rb = l[i], l[i+K-1]
        steps = abs(rb-lb) + min(abs(rb), abs(lb))
        ans = min(ans, steps)

    print(ans)
else:
    print(0)

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

is_zero = l.count(0)
K -= is_zero

if K:
    ans = float('inf')
    for i in range(N-K+1):
        lb, rb = l[i], l[i+K-1]
        steps = abs(rb-lb) + min(abs(rb), abs(lb))
        ans = min(ans, steps)

    print(ans)
else:
    print(0)
Buggy code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
c=10**8
if n==k:
	print(0)
else:
	for i in range(n-k+1):
		h=x[i]
		m=x[i+k-1]
		H=abs(h)
		M=abs(m)
		if h*m<0:
			c=min(c,2*H+M,2*M+H)
	else:
			c=min(c,max(M,H))
	print(c)

Fixed code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
c=10**9
for i in range(n-k+1):
	h=x[i]
	m=x[i+k-1]
	H=abs(h)
	M=abs(m)
	if h*m<0:
		c=min(c,2*H+M,2*M+H)
	else:
		c=min(c,max(M,H))
print(c)
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))

point= len(x) - 1
for i, v in enumerate(x):
    if v >= 0:
        point = i
        break

start = max(0, point - K + 1)
end = start + K - 1

if x[point] != 0:
    start -= 1
    end -= 1
    
ans = float('inf')

while True:
    if x[start] >= 0:
        time = x[end]
    elif x[end] <= 0:
        time = abs(x[start])
    elif abs(x[start]) <= x[end]:
        time = abs(x[start] * 2) + x[end]
    else:
        time = abs(x[start]) + (x[end] * 2)
    ans = min(ans, time)
    
    start += 1
    end += 1

    if start > point or end == len(x):
        break
    
print(ans)

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

point= len(x) - 1
for i, v in enumerate(x):
    if v == 0:
        point = i
        break
    elif v > 0:
        point = i - 1
        break

start = max(0, point - K + 1)
end = start + K - 1

if x[point] != 0:
    point += 1
    
ans = float('inf')

while True:
    if x[start] >= 0:
        time = x[end]
    elif x[end] <= 0:
        time = abs(x[start])
    elif abs(x[start]) <= x[end]:
        time = abs(x[start] * 2) + x[end]
    else:
        time = abs(x[start]) + (x[end] * 2)
    ans = min(ans, time)
    
    start += 1
    end += 1

    if start > point or end == len(x):
        break
    
print(ans)
Buggy code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
res = 10**10
for i in range(N-K+1):
    if X[i]*X[i+K-1] >= 0:
        res = min(res,max(abs(X[i+K-1],X[i])))
    else:
        res = min(res,abs(X[i])*2+X[i+K-1],X[i+K-1]*2+abs(X[i]))
print(res)

Fixed code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
res = 10**10
for i in range(N-K+1):
    if X[i]*X[i+K-1] >= 0:
        res = min(res,max(abs(X[i+K-1]),abs(X[i])))
    else:
        res = min(res,abs(X[i])*2+X[i+K-1],X[i+K-1]*2+abs(X[i]))
print(res)
Buggy code:
N, K = map(int,input().split())
x = list(map(int,input().split()))

MIN = float("inf")
for i in range(N-K+1):
	l = i
	r = i + K - 1
		cur1 = 2*abs(x[l]) + abs(x[r]-x[l])
		cur2 = abs(x[l]) + 2*abs(x[r]-x[l])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)





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

MIN = float("inf")
for i in range(N-K+1):
	l = i
	r = i + K - 1
	cur1 = abs(x[l]) + abs(x[r]-x[l])
	cur2 = abs(x[r]) + abs(x[r]-x[l])
#	print([l,r,cur1,cur2])
	if cur1 < MIN:
		MIN = cur1
	if cur2 < MIN:
		MIN = cur2
print(MIN)
Buggy code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1

ans=10**10
if na>=k and na!=0:
    ans=-a[-k]
if nb==0:
    print(-a[-k])
    exit()
if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Fixed code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if 0 in x:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1

if k==0:
    print(0)
    exit()
ans=10**10
if na>=k:
    ans=-a[-k]

if nb>=k:
    ans=min(ans,b[k-1])

if nb==0:
    print(-a[-k])
    exit()

if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)
Buggy code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [a[i] for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] > 0]

min_ = 10**18

for i in range(min(k, len(plus))):
  min_ = min(2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
print(min_)
  

Fixed code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [0]+[abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [0]+[a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 1:
  print(minus[k])
elif len(minus) == 1:
  print(plus[k])
else:
  for i in range(min(k+1, len(plus))):
    if k-i < len(minus):
      min_ = min(min_, 2*plus[i]+minus[k-i], plus[i]+2*minus[k-i])
  print(min_)
Buggy code:
n,k=map(int,input().split())
k-=1
x=list(map(int,input().split()))
print(min(min(abs(x[i],x[i+k]))+abs(x[i]-x[i+k])for i in range(n-k)))

Fixed code:
n,k=map(int,input().split())
k-=1
x=list(map(int,input().split()))
print(min(min(abs(x[i])+abs(x[i+k]-x[i]),abs(x[i+k])+abs(x[i+k]-x[i])) for i in range(n-k)))
Buggy code:
n,k = list(map(int,input().split()))
d = list(map(int, input().split()))
d_m = []
d_p = [i for i in d if i >= 0 or d_m.append(i)]
ret = -1
for i in range(k):
  if len(d_p) >= i and len(d_m) >= k-i:
    dp_t = d_p[i-1] if i > 0 else 0
    dm_t = -1 * d_m[-(k-i)] if i != 0 else 0
    if dp_t == 0:
      ret_t = dm_t
    elif dm_t == 0:
      ret_t = dp_t
    else:
      ret_t = dp_t + dm_t + min(dp_t, dm_t)
#   print(i, k-i, dp_t, dm_t, ret_t)
    if ret > ret_t or ret < 0:
      ret = ret_t
print(ret)

Fixed code:
n,k = list(map(int,input().split()))
d = list(map(int, input().split()))
d_m = []
d_p = [i for i in d if i >= 0 or d_m.append(i)]
ret = -1
for i in range(k+1):
  if len(d_p) >= i and len(d_m) >= k-i:
    dp_t = d_p[i-1] if i > 0 else 0
    dm_t = -1 * d_m[-(k-i)] if k-i != 0 else 0
    if dp_t == 0:
      ret_t = dm_t
    elif dm_t == 0:
      ret_t = dp_t
    else:
      ret_t = dp_t + dm_t + min(dp_t, dm_t)
#    print(i, k-i, dp_t, dm_t, ret_t)
    if ret > ret_t or ret < 0:
      ret = ret_t
print(ret)
Buggy code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
time=1000000
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  time=min(time,abs(l)+l-r,abs(r)+l-r)
print(time)
  
      
  


Fixed code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
time=100000000000
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  time=min(time,abs(l)+r-l,abs(r)+r-l)
print(time)
Buggy code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
time=float'inf'
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  time=min(time,abs(l)+r-l,abs(r)+r-l)
print(time)
  
      
  


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

ans = float('inf')
for i in range(n-k+1):
    r = x[i+k-1]
    l = x[i]
    ans = min(ans, abs(r - l) + min(abs(l), abs(r)))

print(ans)
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
L = [0]*(N-K+1) 

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

Fixed code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
L = [0]*(N-K+1) 

for i in range(N-K+1):
    L[i] = min((abs(x[i])+abs(x[i+K-1]-x[i])),(abs(x[i+K-1])+abs(x[i+K-1]-x[i])))
        
print(min(L))
Buggy code:
import numpy as np

n, k = list(map(int, input().split()))
x = np.array(list(map(int, input().split())))

result = np.inf
for i in range(len(x) - k):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))

Fixed code:
import numpy as np

n, k = list(map(int, input().split()))
x = np.array(list(map(int, input().split())))

result = np.inf
for i in range(len(x) - k +1):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))
Buggy code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
tmin=1000000000
for i in range(N-K+1):
    a=x[i+K-1]
    b=x[i]
    res=a-b+min([abs(a),abs(b)])
    if tmin>a:
        tmin=a
print(tmin)

Fixed code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
tmin=1000000000
for i in range(N-K+1):
    a=x[i+K-1]
    b=x[i]
    res=a-b+min([abs(a),abs(b)])
    if tmin>res:
        tmin=res
print(tmin)
Buggy code:
N, K = map(int, input().split())
X = list(map(int, input().split()))

x_minus = [a for a in X if a < 0]
x_plus = [a for a in X if a >= 0]

ans = float("inf")
for i in range(K+1):
    if i == 0:
        if len(x_plus) >= K:
            dis = x_plus[K - 1]
            ans = min(ans, dis)
        else:
            continue
    if i != 0:
        if len(x_minus) > i and len(x_plus) > K - i:
            dis = 2 * abs(x_minus[-i]) + x_plus[K - i - 1]
            ans = min(ans, dis)
        else:
            continue


print(ans)


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

x_minus = [a for a in X if a < 0]
x_plus = [a for a in X if a >= 0]

ans = float("inf")
for i in range(K+1):
    if i == 0:
        if len(x_plus) >= K:
            dis = x_plus[K - 1]
            ans = min(ans, dis)
        else:
            continue
    elif 1 <= i <= K - 1:
        if len(x_minus) >= i and len(x_plus) >= K - i:
            dis = 2 * abs(x_minus[-i]) + x_plus[K - i - 1]
            ans = min(ans, dis)
            dis = 2 * x_plus[K - i - 1] + abs(x_minus[-i])
            ans = min(ans, dis)
        else:
            continue
    elif i == K:
        if len(x_minus) >= K:
            dis = abs(x_minus[-K])
            ans = min(ans, dis)


print(ans)
Buggy code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
first = True
for l, r in zip(range(0, n - k + 1), range(k - 1, n)):
    if  0 >= x[r]:
        if first:
            min_time = -x[l]
        else:
            time =-x[l]
            if time < min_time:
                min_time = time
    elif 0 <= x[l]:
        if first:
            min_time = x[r]
        break
    else:
        if first:
            min_time = 2 * (x[r] - x[l]) - max(-x[l], x[r])
            brk = True
        else:
            time =  2 * (x[r] - x[l]) - max(-x[l], x[r])
            if time < min_time:
                min_time = time
print(min_time)

Fixed code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
first = True
for l, r in zip(range(0, n - k + 1), range(k - 1, n)):
    if  0 >= x[r]:
        if first:
            min_time = -x[l]
            first = False
        else:
            time =-x[l]
            if time < min_time:
                min_time = time
    elif 0 <= x[l]:
        if first:
            min_time = x[r]
            first = False
        else:
            time = x[r]
            if time < min_time:
                min_time = time
    else:
        if first:
            min_time = (x[r] - x[l]) + min(-x[l], x[r])
            first = False
        else:
            time =  2 * (x[r] - x[l]) - max(-x[l], x[r])
            if time < min_time:
                min_time = time
print(min_time)
Buggy code:
a, b = map(int, input().split())
s = list(map(int,input().split( )))
c=a-b
e = 10000000000
if a==b:
  e=s[b]-s[0]+min(abs(s[b]),abs(s[0]))
else:
  for j in range(0,c+1):
    if e>s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j])):
      e=s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j]))
print(e)

Fixed code:
a, b = map(int, input().split())
s = list(map(int,input().split( )))
c=a-b
e = 10000000000
if a==b:
  e=s[b-1]-s[0]+min(abs(s[b-1]),abs(s[0]))
else:
  for j in range(0,c+1):
    if e>s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j])):
      e=s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j]))
print(e)
Buggy code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
ls = []
for i in range(n-k+1):
  a = l[i]
  b = l[i+k-1]
    ls.append(min(abs(a)+abs(a-b), abs(b)+abs(a-b)))
print(min(ls))

Fixed code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
ls = []
for i in range(n-k+1):
  a = l[i]
  b = l[i+k-1]
  ls.append(min(abs(a)+abs(a-b), abs(b)+abs(a-b)))
print(min(ls))