qyliang/CodeNanoFix huggingface dataset:
Buggy code:
# Q3
N, K = map(int, input().split())
x = [int(_) for _ in input().split()]

def distance(left, right):
    if right < 0:
        return abs(left)
    elif left > 0:
        return right
    else:
        return -left + right + min(-left, right)

min_dist = float('inf')
for left in range(N-K):
    right = left + K - 1
    dist = distance(x[left], x[right])
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Fixed code:
# Q3
N, K = map(int, input().split())
x = [int(_) for _ in input().split()]

def distance(left, right):
    if right < 0:
        return abs(left)
    elif left > 0:
        return right
    else:
        return -left + right + min(-left, right)

min_dist = float('inf')
for left in range(N):
    right = left + K - 1
    if right >= N:
        break
    dist = distance(x[left], x[right])
    if dist < min_dist:
        min_dist = dist

print(min_dist)
Buggy code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        dis = X[n:n + K]
        l = abs(min(dis)) * 2 + abs(max(dis))
        r = abs(max(dis)) * 2 + abs(min(dis))
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()


Fixed code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        l = abs(X[n]) + abs(X[n]-X[n+K-1])
        r = abs(X[n+K-1]) + abs(X[n+K-1] - X[n])
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()
Buggy code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
INF=10**9+7
if 0 in l:k=k-1
lm=[0]+[i for i in l if i<0][::-1]
if len(lm)<k+1:
    lm=lm+[-INF]*(k+1-len(lm))
lp=[0]+[i for i in l if i>0]
if len(lp)<k+1:
    lp=lp+[INF]*(k+1-len(lp))
ans=INF*10
for i in range(k+1):
    tmp=min(2*lp[i]-lm[k-i],lp[i]-2*lm[k-i])
    print(tmp)
    if tmp<ans:ans=tmp
print(ans)

Fixed code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
INF=10**9+7
if 0 in l:k=k-1
lm=[0]+[i for i in l if i<0][::-1]
if len(lm)<k+1:
    lm=lm+[-INF]*(k+1-len(lm))
lp=[0]+[i for i in l if i>0]
if len(lp)<k+1:
    lp=lp+[INF]*(k+1-len(lp))
ans=INF*10
for i in range(k+1):
    tmp=min(2*lp[i]-lm[k-i],lp[i]-2*lm[k-i])
    if tmp<ans:ans=tmp
print(ans)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 10**9
for i in range(N-K+1):
  if A[i]*A[i+K-1] <= 0:
    ans = min(ans, A[i+K-1]-A[i]+min(abs(A[i]), A[i+K-1]))
  else:
    ans = min(ans, min(abs(A[i]), A[i+K-1]))
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):
  if A[i]*A[i+K-1] <= 0:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, (abs(A[i]))*2+A[i+K-1])
    else:
      ans = min(ans, abs(A[i])+A[i+K-1]*2)
  else:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, abs(A[i+K-1]))
    else:
      ans = min(ans, abs(A[i]))
print(ans)
Buggy code:
n, k = list(map(int, input().split()))
x = [int(inout()) for i in range(n)]
ans = [min(abs(x[i]), abs(x[i+k-1]))+x[i+k-1]-x[i] for i in range(n-k+1)]
print(min(ans))

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

ans = 1e9
for l, r in zip(X, X[K:]):
  ans = min(ans, r-l+min(abs(l),abs(r)))
print(ans)

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

ans = 1e9
for l, r in zip(X, X[K-1:]):
  ans = min(ans, r-l+min(abs(l),abs(r)))
print(ans)
Buggy code:
n, k = map(int, input().split())
x = list(map(int, input().split()))

ans = []
for i in range(n - k + 1):
    left = x[i]
    right = x[i + k - 1]
    elif abs(left) > abs(right):
        ans.append(abs(right) + abs(left - right))
    else:
        ans.append(abs(left) + abs(right - left))

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):
    left = x[i]
    right = x[i + k - 1]
    ans.append(min(abs(left) + abs(right - left),
                   abs(right) + abs(left - right)))

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


Fixed code:
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    s = 5 * (10 ** 8)
    for i in range(N-K+1):
        if x[i] * x[K - 1 + i] < 0:
            s = min(s, min(x[K - 1 + i], abs(x[i])) * 2 + max(x[K - 1 + i], abs(x[i])))
        else:
            s = min(s, max(abs(x[K - 1 + i]), abs(x[i])))
    print(s)
main()
Buggy code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
l1=[]
for i in range(n-m+1):
  if l[i]>=0:
    l1.append(l[i+m]-l[i])
  elif l[i]<=0<=l[i+m]:
    l1.append(l[i+m]-l[i]+min(l[i],l[i+m]))
  else:
    l1.append(l[i+m]-l[i])
print(min(l1))    

Fixed code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
l1=[]
for i in range(n-m+1):
  if l[i]>=0:
    l1.append(l[i+m-1])
  elif l[i]<=0<=l[i+m-1]:
    l1.append(l[i+m-1]-l[i]+min(abs(l[i]),abs(l[i+m-1])))
  else:
    l1.append(abs(l[i]))
print(min(l1))
Buggy code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
mini=100000000000000
for i in range(N-K):
    if A[i]<0 and A[i+K-1]>=0:
        if abs(A[i])>abs(A[i+K-1]):
            if mini>abs(A[i+K-1])*2+abs(A[i]):
                mini=abs(A[i+K-1])*2+abs(A[i])
        else:
            if mini > abs(A[i]) * 2 + abs(A[i+K-1]):
                mini = abs(A[i]) * 2 + abs(A[i+K-1])
    else:
       if mini>max(abs(A[i]),abs(A[i+K-1])):
           mini=max(abs(A[i]),abs(A[i+K-1]))
print(mini)



Fixed code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
mini=100000000000000
for i in range(N-K+1):
    if A[i]<0 and A[i+K-1]>=0:
        if abs(A[i])>abs(A[i+K-1]):
            if mini>abs(A[i+K-1])*2+abs(A[i]):
                mini=abs(A[i+K-1])*2+abs(A[i])
        else:
            if mini > abs(A[i]) * 2 + abs(A[i+K-1]):
                mini = abs(A[i]) * 2 + abs(A[i+K-1])
    else:
       if mini>max(abs(A[i]),abs(A[i+K-1])):
           mini=max(abs(A[i]),abs(A[i+K-1]))
print(mini)
Buggy code:
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,ab)
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,a0)
print(ans)

Fixed code:
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,abs(a0))
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,ab)
print(ans)
Buggy code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(N-K+1):
    b.append(min(abs(a[i])+abs(a[i+K-1]-a[i]),abs(a[i+K-1])+abs(a[i]-a[i+K-1]))
print(min(b))


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

ans = 10**6
for i in range(n-k):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
print(ans)




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

ans = 10**10
for i in range(n-k+1):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
if ans == 10**10:
  print(0)
else:
  print(ans)
Buggy code:
import sys
N,K = map(int,input().split())
lsx = list(map(int,input().split()))
if 0 in lsx:
    K = K-1
    lsx.remove(0)
if bool(lsx) == False or K == 0:
    print(0)
    sys.exit()
lshu = []
for i in range(len(lsx)):
    if lsx[i] < 0:
        lshu.append(lsx[i])
    else:
        sei = i
        break
lssei = lsx[sei:]
lenhu = len(lshu)
lensei = len(lssei)
lshu2 = [abs(lshu[i]) for i in range(lenhu-1,-1,-1)]
dishu = [0] + lshu2
dissei = [0] + lssei
print(dissei,dishu)
ans = 10*10**8
for i in range(K):
    if i <= lenhu and K-i <= lensei:
        ans = min(ans,2*dishu[i]+dissei[K-i],dishu[i]+2*dissei[K-i])
print(ans)

Fixed code:
import sys
N,K = map(int,input().split())
lsx = list(map(int,input().split()))
if 0 in lsx:
    K = K-1
    lsx.remove(0)
if bool(lsx) == False or K == 0:
    print(0)
    sys.exit()
lshu = []
sei = len(lsx)
for i in range(len(lsx)):
    if lsx[i] < 0:
        lshu.append(lsx[i])
    else:
        sei = i
        break
lssei = lsx[sei:]
lenhu = len(lshu)
lensei = len(lssei)
lshu2 = [abs(lshu[i]) for i in range(lenhu-1,-1,-1)]
dishu = [0] + lshu2
dissei = [0] + lssei
ans = 10*10**8
for i in range(K+1):
    if i <= lenhu and K-i <= lensei:
        ans = min(ans,2*dishu[i]+dissei[K-i],dishu[i]+2*dissei[K-i])
print(ans)
Buggy code:
N, K = map(int, input().split(' '))
a= list(map(int, input().split(' ')))

INF = float('inf')
ans = INF

for i in range(N-K):
    ans = min(ans, abs(a[i+K-1]-a[i])+min(abs(a[i+K-1]), abs(a[i])))

print(ans)

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

INF = float('inf')
ans = INF

if N == K:
    ans = abs(a[N-1]-a[0])+min(abs(a[0]), abs(a[N-1]))
else:
    for i in range(N-K+1):
        ans = min(ans, abs(a[i+K-1]-a[i])+min(abs(a[i+K-1]), abs(a[i])))

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

Fixed code:
N,K = map(int,input().split())
x = list(map(int, input().split()))
ans = 10**10
for i in range(N-K+1):
    temp_ans1 = abs(x[i]) + abs(x[i+K-1]-x[i])
    temp_ans2 = abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    # temp_ans2 = abs(x[N-1-i]) + abs(x[N-1-i-K]-x[N-1-i])
    # temp_ans2 = abs(x[-(i+1)]) +abs(x[-(i+K)] - x[i+1])
    ans = min(ans,temp_ans1,temp_ans2)
    # ans = min(ans,temp_ans1    ans = min(ans,temp_ans1,temp_ans2)
print(ans)
Buggy code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1e13
for i in range(n-k+1):
    l = x[i]
    r = x[i+k]
    ans = min(ans, abs(l)+abs(r-l))
print(ans)


Fixed code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1e13
for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    ans = min(ans, abs(r)+abs(l-r))
    ans = min(ans, abs(l)+abs(r-l))
print(ans)
Buggy code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k):
    c=min(c,a[i]+a[i+k-1]-abs(min(a[i],a[i+k-1])))
print(c)

Fixed code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,-a[i]+a[i+k-1]\
          +min(abs(a[i]),abs(a[i+k-1])))
print(c)
Buggy code:
#-*-coding:utf-8-*-

def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))
    ans = float('inf')

    if 0 in x_list:
        k -= 1

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

if __name__ == '__main__':
    main()

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

def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))
    ans = float('inf')

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

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

max = 10000000000000000000000000
for i in range(N - K + 1):
    # i = 0
    t_1 = abs(X[i])
    t_1 += X[i + K - 1] - X[i]

    t_2 = abs(X[i + K - 1])
    t_2 = X[i] - X[i + K - 1]

    if t_1 < max:
        max = t_1
    if t_2 < max:
        max = t_2

print(max)


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


max = 10000000000000000000000000
for i in range(N - K + 1):
    # i = 0
    diff = X[i + K - 1] - X[i]
    t_1 = abs(X[i])
    t_1 += diff
    t_2 = abs(X[i + K - 1])
    t_2 += diff

    if t_1 < max:
        max = t_1
    if t_2 < max:
        max = t_2

print(max)
Buggy code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]
res = 10*100

for i in range(x):
    tmp = i + b[1] - 1
    if a[i] <= 0 and a[tmp] >= 0:
        res = min(abs(a[i] - a[tmp]) + min(abs(a[i]),abs(a[tmp])),res)

print(res)

Fixed code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**15
 
for i in range(x):
    tmp = i + b[1] - 1
    if res > (a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp]))):
	    res = a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp]))
        
print(res)
Buggy code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]
res = 10*11

for i in range(x):
    tmp = i + b[1] - 1
    res = min(abs(a[i] - a[tmp]) + min(abs(0-a[i]),abs(0-a[tmp])),res)

print(res)

Fixed code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**100
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min( a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp])),res)
        
print(res)
Buggy code:
N, K = map(int, input().split())
x = [int(x) for x in input().split()]

x1 = []
x2 = []
for i in range(N):
    if x[i] >= 0:
        x1.append(x[i])
    else:
        x2.append(-1 * x[i])
x2 = x2[::-1]

N1 = len(x1)
N2 = len(x2)

ans = float('inf')

if N1 >= K:
    ans = min(ans, x1[-1])
if N2 >= K:
    ans = min(ans, x2[-1])

for i in range(K - 1):
    if i < N1 and K - i - 2 < N2:
        d1 = 2 * x1[i] + x2[K - i - 2]
        d2 = x1[i] + 2 * x2[K - i - 2]
        ans = min(ans, d1, d2)
print(ans)


Fixed code:
N, K = map(int, input().split())
x = [int(x) for x in input().split()]

x1 = []
x2 = []
for i in range(N):
    if x[i] >= 0:
        x1.append(x[i])
    else:
        x2.append(-1 * x[i])
x2 = x2[::-1]

N1 = len(x1)
N2 = len(x2)

ans = float('inf')

if N1 >= K:
    ans = min(ans, x1[K - 1])
if N2 >= K:
    ans = min(ans, x2[K - 1])

for i in range(K - 1):
    if i < N1 and K - i - 2 < N2:
        d1 = 2 * x1[i] + x2[K - i - 2]
        d2 = x1[i] + 2 * x2[K - i - 2]
        ans = min(ans, d1, d2)
print(ans)
Buggy code:
n,k = map(int,input().split())
x = [int(x) for x in input().split()]

mini = 10**9
for i in range(n-k+1):
    ls = x[i:i+k]
    flg1 = abs(ls[0])+abs(ls[0]-ls[k-1])
    flg2 = abs(ls[k-2])+abs(ls[0]-ls[k-1])
    flg = min(flg1,flg2)
    mini = min(flg,mini)
print(mini)

Fixed code:
n,k = map(int,input().split())
ls = [int(x) for x in input().split()]

mini = 10**9
for i in range(n-k+1):
    flg1 = abs(ls[i])+abs(ls[i]-ls[i+k-1])
    flg2 = abs(ls[i+k-1])+abs(ls[i]-ls[i+k-1])
    mini = min(flg1,flg2,mini)
print(mini)
Buggy code:
def dist(a, b):
  if a * b < 0:
    return 2 * min(abs(a), abs(b)) + max(abs(a), abs(b))
  else:
    return max(abs(a), abs(b))

def solve(A, K):
  ans = dist(A[0], A[K])
  for i in range(len(A)-K+1):
    ans = min(ans, dist(A[i], A[i+K-1]))
  return ans
               
N, K, *A = map(int, open(0).read().split())
print(solve(A, K))

Fixed code:
def dist(a, b):
  if a * b < 0:
    return 2 * min(abs(a), abs(b)) + max(abs(a), abs(b))
  else:
    return max(abs(a), abs(b))

def solve(A, K):
  ans = dist(A[0], A[K-1])
  for i in range(len(A)-K+1):
    ans = min(ans, dist(A[i], A[i+K-1]))
  return ans
               
N, K, *A = map(int, open(0).read().split())
print(solve(A, K))
Buggy code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
dis = []
for i in range(N - K + 1):
    dis.append(abs(x[i] - x[i + K - 1]) + abs(x[i]))
    dis.append(abs(x[i] - x[i + K - 1]) + abs(x[i + k - 1]))
print(min(dis))


Fixed code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
dis = []
for i in range(N - K + 1):
    dis.append(abs(x[i] - x[i + K - 1]) + abs(x[i]))
    dis.append(abs(x[i] - x[i + K - 1]) + abs(x[i + K - 1]))
print(min(dis))
Buggy code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
Xs = list(mapint())

from bisect import bisect_left
pos = []
neg = []

for x in Xs:
    if x>=0:
        pos.append(x)
    else:
        neg.append(-x)

neg = neg[::-1]

ans = 10**18
if len(pos)>=K:
    ans = min(ans, pos[K-1])
if len(neg)>=K:
    ans = min(ans, neg[K-1])
lenp = len(pos)
lenn = len(neg)
for i in range(min(lenp, K)):
    if K-(i+1)<=lenn:
        ans = min(ans, pos[i]*2+neg[K-i-2])
for i in range(min(lenn, K)):
    if K-(i+1)<=lenp:
        ans = min(ans, neg[i]*2+pos[K-i-2])
print(ans)

Fixed code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
Xs = list(mapint())

pos = []
neg = []

for x in Xs:
    if x>=0:
        pos.append(x)
    else:
        neg.append(-x)

neg = neg[::-1]

ans = 10**18
if len(pos)>=K:
    ans = min(ans, pos[K-1])
if len(neg)>=K:
    ans = min(ans, neg[K-1])
lenp = len(pos)
lenn = len(neg)
if lenn>0:
    for i in range(min(lenp, K)):
        if K-(i+1)<=lenn:
            ans = min(ans, pos[i]*2+neg[K-i-2])
if lenp>0:
    for i in range(min(lenn, K)):
        if K-(i+1)<=lenp:
            ans = min(ans, neg[i]*2+pos[K-i-2])
print(ans)
Buggy code:
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)
    N += 1

MIN = 2 * 10**8
for i in range(K+1):
    if s - K + i >= 0 and s + i <N:
        f = X[s -K+i]
        l = X[s+i]
        print(f,l)
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        print(ans)
        MIN =min(MIN,ans)

print(MIN)

Fixed code:
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)
    N += 1

MIN = 2 * 10**9
for i in range(K+1):
    if s - K + i >= 0 and s + i <N:
        f = X[s -K+i]
        l = X[s+i]
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        MIN =min(MIN,ans)

print(MIN)
Buggy code:
import sys

def main(lines):
  N, K = map(int, lines[0].split())
  a = list(map(int, lines[1].split()))
  ans = 1000000000000
  for i in range(N):
    if i + K - 1 >= N:
      break
    b = a[i]
    c = a[i + K - 1]
    if b < 0 and c >= 0:
      ans = min(ans, max(abs(b), abs(c)) + 2*min(abs(b), abs(c)))
    else:
      ans = min(ans, min(abs(b), abs(c)))
  print(ans)


if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Fixed code:
import sys

def main(lines):
  N, K = map(int, lines[0].split())
  a = list(map(int, lines[1].split()))
  ans = 1000000000000
  for i in range(N):
    if i + K - 1 >= N:
      break
    b = a[i]
    c = a[i + K - 1]
    if b < 0 and c >= 0:
      ans = min(ans, max(abs(b), abs(c)) + 2*min(abs(b), abs(c)))
    else:
      ans = min(ans, max(abs(b), abs(c)))
  print(ans)


if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)