qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
ans = 10**5

for i in range(N-K+1):
    S = X[i:i+K]
    #print(S,S[0],S[-1])
    mi = S[0]
    ma = S[-1]
    if mi<0 and ma<=0:
        ans = min(ans ,abs(mi))
    elif  mi>=0 and ma>0:
        ans = min(ans ,ma)
    elif  mi<0 and ma>0:
        if abs(mi) > ma :
            ans = min(ans,abs(mi) + 2*ma)
        elif abs(mi) < ma :
            ans = min(ans,abs(mi)*2 + ma)

print(ans)

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

for i in range(N-K+1):
    #S = X[i:i+K]
    #print(S,S[0],S[-1])
    mi = X[i]
    ma = X[i+K-1]
    #print(mi,ma)
    if ma<=0:
        ans = min(ans ,abs(mi))
    elif  mi>0:
        ans = min(ans ,ma)
    elif  mi<0 and ma>0:
        if abs(mi) > ma :
            ans = min(ans,abs(mi) + 2*ma)
        else :
            ans = min(ans,abs(mi)*2 + ma)

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 = i + K - 1
  if X[l] * X[r] < 0:
    ans = min(ans, 2 * min(abs(X[l]), abs(X[r])) + max(abs(X[l]), abs(X[r])))
  else:
    ans = max(ans, min(abs(X[l]), abs(X[r])))
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):
  l = i
  r = i + K - 1
  if X[l] * X[r] < 0:
    ans = min(ans, 2 * min(abs(X[l]), abs(X[r])) + max(abs(X[l]), abs(X[r])))
  else:
    ans = min(ans, abs(X[r]) if X[r] > 0 else abs(X[l]))
print(ans)
Buggy code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
Y = []
Z = []
zc = 0
for i in range(N):
    if X[i] > 0:
        Y.append(X[i])
    if X[i] == 0:
        zc = 1
for i in range(N-1, -1, -1):
    if X[i] < 0:
        Z.append(-X[i])

cs = []
K -= zc
if K == 0:
    print(0)
else:
    if len(Y) >= K:
        cs.append(Y[K-1])
    if len(Z) >= K:
        cs.append(Z[K-1])
    for i in range(K-1):
        cs.append(2*Y[i]+Z[K-i-2])
    for i in range(K-1):
        cs.append(2*Z[i]+Y[K-i-2])
print(min(cs))


Fixed code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
Y = []
Z = []
zc = 0
for i in range(N):
    if X[i] > 0:
        Y.append(X[i])
    if X[i] == 0:
        zc = 1
for i in range(N-1, -1, -1):
    if X[i] < 0:
        Z.append(-X[i])

cs = []
K -= zc
if K == 0:
    print(0)
else:
    if len(Y) >= K:
        cs.append(Y[K-1])
    if len(Z) >= K:
        cs.append(Z[K-1])
    for i in range(min(K-1, len(Y))):
        if K-i-2 < len(Z):
            cs.append(2*Y[i]+Z[K-i-2])
    for i in range(min(K-1, len(Z))):
        if K-i-2 < len(Y):
            cs.append(2*Z[i]+Y[K-i-2])
    print(min(cs))
Buggy code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans=float("inf")

for i in range(K):
    if i + K > N+1:
        break
    else:
        a = min(abs(x[K+i-1]),abs(x[i])) 
        ans = min(ans,x[K+i-1]-x[i]+a)
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):
    if i + K == N+1:
        break
    else:
        a = min(abs(x[K+i-1]),abs(x[i])) 
        ans = min(ans,x[K+i-1]-x[i]+a)
print(ans)
Buggy code:
n, k = map(int,input().split())

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

ans = float('inf')

for i in range(n - k):
	if arr[i] * arr[k + i -1] >= 0:
		tmp = max(abs(arr[i]), abs(arr[k + i -1]))
	else:
		tmp = abs(arr[i]) + abs(arr[k + i -1]) + min(abs(arr[i]), abs(arr[k + i -1]))
	ans = min(ans, tmp)

print(ans)
		

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

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

ans = float('inf')

for i in range(n - k + 1):
	if arr[i] * arr[k + i -1] >= 0:
		tmp = max(abs(arr[i]), abs(arr[k + i -1]))
	else:
		tmp = abs(arr[i]) + abs(arr[k + i -1]) + min(abs(arr[i]), abs(arr[k + i -1]))
	ans = min(ans, tmp)

print(ans)
Buggy code:
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
  if x[i] == 0:
    p, m = i, i+1
    break
  elif x[i] > 0:
    p, m = i, i
    break
    
if (x[0]<0) and (x[-1]>0):
  pls = x[p:]
  mns = x[:m]
  ans = math.inf

  for i in range(len(pls)):
    if i < K-1:
      if pls[0] == 0:
        n = K - i
      else:
        n = K - i - 1
      if len(mns) >= n:
        tmp = 2*abs(mns[-n]) + pls[i]
        ans = min(ans, tmp)
    if i == K-1:
      ans = min(ans, pls[i])
      break
    
  for i in range(len(mns)):
    m = len(mns)-i
    if m < K:
      if pls[0] == 0:
        n = K - m
      else:
        n = K - m - 1
      if len(pls) > n:
        tmp = 2*pls[n] + abs(mns[-m])
        print(i, tmp)
        ans = min(ans, tmp)
    if m == K:
      ans = min(ans, abs(mns[i]))
    
elif x[0] >= 0:
  ans = x[K-1]
  
else:
  ans = abs(x[-K])
  
print(ans)

Fixed code:
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
  if x[i] == 0:
    p, m = i, i+1
    break
  elif x[i] > 0:
    p, m = i, i
    break
    
if (x[0]<0) and (x[-1]>0):
  pls = x[p:]
  mns = x[:m]
  ans = math.inf

  for i in range(len(pls)):
    if i < K-1:
      if pls[0] == 0:
        n = K - i
      else:
        n = K - i - 1
      if len(mns) >= n:
        tmp = 2*abs(mns[-n]) + pls[i]
        ans = min(ans, tmp)
    if i == K-1:
      ans = min(ans, pls[i])
      break
    
  for i in range(len(mns)):
    m = len(mns)-i
    if m < K:
      if pls[0] == 0:
        n = K - m
      else:
        n = K - m - 1
      if len(pls) > n:
        tmp = 2*pls[n] + abs(mns[-m])
        ans = min(ans, tmp)
    if m == K:
      ans = min(ans, abs(mns[i]))
    
elif x[0] >= 0:
  ans = x[K-1]
  
else:
  ans = abs(x[-K])
  
print(ans)
Buggy code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
p = n
for i in range(n):
  if x[i] > 0:
    p = i
    break
result = float('inf')
for i in range(max(p - k, 0), min(k - p, k + 1)):
  if i == 0:
    result = min(result, x[p + k - 1])
  elif i == k:
    result = min(result, -x[p - k])
  else:
    l = x[p - i]
    r = x[p + (k - i) - 1]
    result = min(result, r - 2 * l, r * 2 - l)
print(result)


Fixed code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
p = n
for i in range(n):
  if x[i] > 0:
    p = i
    break
result = float('inf')
for i in range(k + 1):
  if p - i < 0 or p + k - i > n:
    continue
  if i == 0:
    result = min(result, x[p + k - 1])
  elif i == k:
    result = min(result, -x[p - k])
  else:
    l = x[p - i]
    r = x[p + (k - i) - 1]
    result = min(result, r - 2 * l, r * 2 - l)
print(result)
Buggy code:
n,k = map(int,input().split())
x = list(map(int,input().split()))

l = 0
r = k-1
mnm = 1000100010
while r<n:
    print(r)
    a = abs(x[l])+abs(x[l]-x[r])
    b = abs(x[r])+abs(x[r]-x[l])
    if a<=b:
        if a < mnm:
            mnm = a
    else:
        if b < mnm:
            mnm = b
    l += 1
    r += 1
print(mnm)

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

l = 0
r = k-1
mnm = 1000100010
while r<n:
    a = abs(x[l])+abs(x[l]-x[r])
    b = abs(x[r])+abs(x[r]-x[l])
    if a<=b:
        if a < mnm:
            mnm = a
    else:
        if b < mnm:
            mnm = b
    l += 1
    r += 1
print(mnm)
Buggy code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
  ans.append(min(abs(l[i])+abs([l[i]-l[i+k-1]),abs(l[i+k-1])+abs(l[i+k-1]-l[i])))
print(min(ans))

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

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

ans=10**8
for i in range(N-K+1):
  L=A[i]
  R=A[i+K-1]
  ans=min(ans, R-L+min(R, L))
  
print(ans)

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

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

ans=10**9
for i in range(N-K+1):
  L=A[i]
  R=A[i+K-1]
  ans=min(ans, R-L+min(abs(R), abs(L)))
  
print(ans)
Buggy code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
xli = na()
li = []
ans = 1000000000
idx = -1
for i in range(n):
    if xli[i] > 0:
        idx = i
        break
for i in range(n - k + 1):
    if i > idx:
        break
    t = xli[i:i + k]
    if t[0] * t[-1] < 0:
        ans = min(ans, 2 * abs(t[0]) + abs(t[-1]), abs(t[0]) + 2 * abs(t[-1]))
    else:
        if t[0] <= 0:
            ans = min(ans, abs(t[0]))
        else:
            ans = min(ans, abs(t[-1]))
print(ans)

Fixed code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
xli = na()
li = []
ans = 1000000000
idx = -1
for i in range(n):
    if xli[i] > 0:
        idx = i
        break
if idx == -1:
    print(abs(xli[-k]))
elif idx == 0:
    print(abs(xli[k-1]))
else:
    for i in range(max(idx - k, 0), min(idx+1, n-k+1)):
        if xli[i] * xli[i + k -1] < 0:
            ans = min(ans, 2 * abs(xli[i]) + xli[i + k -1], abs(xli[i]) + 2 * xli[i + k -1])
        else:
            if xli[i + k -1] <= 0:
                ans = min(ans, abs(xli[i]))
            else:
                ans = min(ans, abs(xli[i + k -1]))
    print(ans)
Buggy code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
x = 0
besttime = 10**9
while x + K - 1< N:
    if lst[x]*lst[x+K-1] < 0:
        time = min(abs(lst[x])*2+lst[x+K-1], abs(lst[x])+2*lst[x+K-1])
    else:
        time = abs(lst[x] + lst[x+K-1])
    if besttime > time:
        besttime = time
    x += 1
print(besttime)

Fixed code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
x = 0
besttime = 10**9
while x + K - 1< N:
    if lst[x]*lst[x+K-1] < 0:
        time = min(abs(lst[x])*2+lst[x+K-1], abs(lst[x])+2*lst[x+K-1])
    else:
        time = max(abs(lst[x]), abs(lst[x+K-1]))
    if besttime > time:
        besttime = time
    x += 1
print(besttime)
Buggy code:
import sys
input=sys.stdin.readline

input = open(sys.argv[1],"r").readline

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()


Fixed code:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()
Buggy code:
N,K = map(int,input().split())
X = list(map(int,input().split()))

for i in range(N - K + 1):
    l = i
    r = i + K - 1

    Xl, Xr = X[l], X[r]
    if i == 0:
        ans = abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr)
    else:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr) , ans)


print(ans)


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

for i in range(N - K + 1):
    l = i
    r = i + K - 1

    Xl, Xr = X[l], X[r]
    if i == 0:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr))
    else:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr) , ans)


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

for center in range(n):
    if x[center] >= 0:
        break

if center == n or (center == n - 1 and x[n - 1] == 0):
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - 1]))

print(ans)

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

for center in range(n):
    if x[center] >= 0:
        break

if center == n - 1:
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - i]))

print(ans)
Buggy code:
def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    if 0 in X:
        N -= 1
        K -= 1
        X.remove(0)
    ans = X[-1]-X[0]+min(X[-1],abs(X[0]))
    for i in range(N-K+1):
        if X[i+K-1]<=0:
            p = -X[i]
        elif X[i]>=0:
            p = X[i+K-1]
        else:
            p = X[i+K-1]-X[i]+min(X[i+K-1],abs(X[i]))
        ans = min(ans,p)
    return ans
print(solve())

Fixed code:
def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    if 0 in X:
        N -= 1
        K -= 1
        if K == 0:
            return 0
        X.remove(0)
    ans = 10**9
    for i in range(N-K+1):
        if X[i+K-1]<=0:
            p = -X[i]
        elif X[i]>=0:
            p = X[i+K-1]
        else:
            p = X[i+K-1]-X[i]+min(X[i+K-1],abs(X[i]))
        ans = min(ans,p)
    return ans
print(solve())
Buggy code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
# if len(a) >= k - cost:
#     ret = min(ret, cost_a[k - cost])
# if len(b) >= k - cost:
#     ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)


Fixed code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if index < len(xs) and xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
if len(a) >= k - cost:
    ret = min(ret, cost_a[k - cost])
if len(b) >= k - cost:
    ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if 0 <= k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if 0 <= k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)
Buggy code:
n, k = map(int, input().split())

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

d = 10**10
for i in range(n-k):
    x_l = l[i]
    x_r = l[i+k-1]

    if x_l >= 0:
        tmp = x_r
    elif x_r <= 0:
        tmp = -x_l
    else:
        tmp = min(abs(x_r), abs(x_l)) + abs(x_l-x_r)

    d = min(d, tmp)

print(d)


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

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

d = 10**10
for i in range(n-k+1):
    x_l = l[i]
    x_r = l[i+k-1]

    if x_l >= 0:
        tmp = x_r
    elif x_r <= 0:
        tmp = -x_l
    else:
        tmp = min(abs(x_r), abs(x_l)) + abs(x_l-x_r)

    d = min(d, tmp)

print(d)
Buggy code:
import bisect


def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 99999999999
    for i in range(n-k+1):
        s = 0
        if left > i+k:
            s = b[i]
        elif right < i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())

Fixed code:
import bisect


def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 99999999999
    for i in range(n-k+1):
        s = 0
        if left >= i+k-1:
            s = b[i]
        elif right <= i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())
Buggy code:
N,K=map(int,input().split())
S=list(map(int,input().split()))
ans=float("inf")
for i in range(N-K):
	L=S[i]
	R=S[i+K-1]
	ans=min(abs(L)+abs(R-L),abs(R)+abs(L-R),ans)
print(ans)

Fixed code:
N,K=map(int,input().split())
S=list(map(int,input().split()))
ans=float("inf")
if N==1:
	ans=abs(S[0])
for i in range(N-K+1):
	L=S[i]
	R=S[i+K-1]
	ans=min(abs(L)+abs(R-L),abs(R)+abs(L-R),ans)
print(ans)
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
answer = float('inf')
for l, r in zip(x, x[K - 1:]):
    print(l, r)
    cost = r - l + min(abs(l), abs(r))
    answer = cost if cost < answer else answer

print(answer)

Fixed code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
answer = float('inf')
for l, r in zip(x, x[K - 1:]):
    # print(l, r)
    cost = r - l + min(abs(l), abs(r))
    answer = cost if cost < answer else answer

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

answer = []
for i in range(N):
  if i + K > N:
    break
  Y = X[i:i+K]
  if Y[0] > 0:
    answer.append(Y[-1])
  elif Y[-1] < 0:
    answer.append(-Y[0])
  else:
    answer.append(min(Y[0]*(-2)+Y[-1], Y[-1]*2-Y[0]))

print(min(answer)

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

answer = []
for i in range(N):
  if i + K > N:
    break
  Ys, Yt = X[i], X[i+K-1]
  if Ys > 0:
    answer.append(Yt)
  elif Yt < 0:
    answer.append(-Ys)
  else:
    answer.append(min(Ys*(-2)+Yt, Yt*2-Ys))

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

tmp=10**10
for i in range(N-K):
  if x[i]*x[i+K-1]<0:
    tmp=min(tmp,min(abs(x[i]),abs(x[i+K-1]))*2+max(abs(x[i]),abs(x[i+K-1])))
  else:
    tmp=min(tmp,max(abs(x[i]),abs(x[i+K-1])))
print(tmp)

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

tmp=10**10
for i in range(N-K+1):
  if x[i]*x[i+K-1]<0:
    tmp=min(tmp,min(abs(x[i]),abs(x[i+K-1]))*2+max(abs(x[i]),abs(x[i+K-1])))
  else:
    tmp=min(tmp,max(abs(x[i]),abs(x[i+K-1])))
print(tmp)
Buggy code:
import bisect
n, k = [int(item) for item in input().split()]
x = [int(item) for item in input().split()]

lft_num = bisect.bisect(x, 0)
rgt_num = n - lft_num

print(rgt_num, lft_num)
ans = 10**10
for i in range(min(rgt_num, k)):
    r = x[lft_num + i]
    distance = abs(r)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num - (k-i-1) > 0:
        l = x[lft_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
for i in range(min(lft_num, k)):
    l = x[lft_num - i - 1]
    distance = abs(l)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if rgt_num - (k-i-1) > 0:
        r = x[lft_num + rgt_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
    
print(ans)

Fixed code:
import bisect
n, k = [int(item) for item in input().split()]
x = [int(item) for item in input().split()]

lft_num = bisect.bisect(x, 0)
rgt_num = n - lft_num

ans = 10**10
for i in range(min(rgt_num, k)):
    r = x[lft_num + i]
    distance = abs(r)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num - (k-i-1) >= 0:
        l = x[lft_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
for i in range(min(lft_num, k)):
    l = x[lft_num - i - 1]
    distance = abs(l)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num + (k-i-2) < n:
        r = x[lft_num + (k-i-2)]
        distance += abs(r - l)
        ans = min(ans, distance)
    
print(ans)
Buggy code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
  a.remove(0)
  n -= 1
  k -= 1
m = 300000000
for i in range(n+1-k):
  if a[i+k-1] < 0:
    x = -a[i]
  if a[i] > 0:
    x = a[i+k-1]
  else:
    x = (a[i+k-1]-a[i]) + min(-a[i],a[i+k-1])
  m = min(m,x)
print(m)

Fixed code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
  a.remove(0)
  n -= 1
  k -= 1
if k == 0:
  print(0)
  exit()
m = 300000000
for i in range(n+1-k):
  if a[i+k-1] < 0:
    x = -a[i]
  elif a[i] > 0:
    x = a[i+k-1]
  else:
    x = (a[i+k-1]-a[i]) + min(-a[i],a[i+k-1])
  m = min(m,x)
print(m)
Buggy code:
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, m):
    print(l, m)
    if l < m:
        return l * 2 + m
    else:
        return l + m * 2

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))
    right_x = [i for i in x if i >= 0][:K]
    left_x = [-i for i in x if i < 0][-K:]

    left_len = len(left_x)
    right_len = len(right_x)
    if right_len < K:
        right_x += [10 ** 8 + 1] * (K - right_len)
    if left_len < K:
        left_x = [10 ** 8 + 1] * (K - left_len) + left_x

    right_x = [0] + right_x
    left_x += [0]

    ans = 10 ** 9
    for i in range(K + 1):
        tmp_ans = solve(left_x[i], right_x[i])
        if tmp_ans < ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()


Fixed code:
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, m):
    if l < m:
        return l * 2 + m
    else:
        return l + m * 2

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))
    right_x = [i for i in x if i >= 0][:K]
    left_x = [-i for i in x if i < 0][-K:]

    left_len = len(left_x)
    right_len = len(right_x)
    if right_len < K:
        right_x += [10 ** 9 + 1] * (K - right_len)
    if left_len < K:
        left_x = [10 ** 9 + 1] * (K - left_len) + left_x

    right_x = [0] + right_x
    left_x += [0]

    ans = 10 ** 9
    for i in range(K + 1):
        tmp_ans = solve(left_x[i], right_x[i])
        if tmp_ans < ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()
Buggy code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k):
    if x[i]*x[i+k-1]>=0:
        ans = min(ans, max(abs(x[i]), abs(x[i+k-1])))
    else:
        a = min(abs(x[i]),abs(x[i+k-1]))
        b = max(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans, a*2+b)
print(ans)

Fixed code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k+1):
    if x[i]*x[i+k-1]>=0:
        ans = min(ans, max(abs(x[i]), abs(x[i+k-1])))
    else:
        a = min(abs(x[i]), abs(x[i+k-1]))
        b = max(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans, a*2+b)
print(ans)
Buggy code:
n,k=map(int,input().split())
candle=list(map(int,input().split()))
ans=2000000000
for i in range(n-k):
  if candle[i]<0 and candle[i+k-1]>0:
    temp=min(abs(candle[i]), candle[i+k-1])*2+max(abs(candle[i]), candle[i+k-1])
  else:
    temp=abs(candle[i+k-1]-candle[i])
  ans=min(temp, ans)
print(ans)

Fixed code:
n,k=map(int,input().split())
candle=list(map(int,input().split()))
ans=2000000000
for i in range(n-k+1):
  if candle[i]<0 and candle[i+k-1]>0:
    temp=min(abs(candle[i]), candle[i+k-1])*2+max(abs(candle[i]), candle[i+k-1])
  elif candle[i+k-1]<=0:
    temp=abs(candle[i])
  elif candle[i]>=0:
    temp=candle[i+k-1]
  ans=min(temp, ans)
print(ans)