Problem:
 Score : 300 points    Problem Statement There are N candles placed on a number line.
The i -th candle from the left is placed on coordinate x_i .
Here, x_1 < x_2 < ... < x_N holds.  Initially, no candles are burning.
Snuke decides to light K of the N candles.  Now, he is at coordinate 0 .
He can move left and right along the line with speed 1 .
He can also light a candle when he is at the same position as the candle, in negligible time.  Find the minimum time required to light K candles.      Constraints  1 \leq N \leq 10^5  1 \leq K \leq N  x_i is an integer.  |x_i| \leq 10^8  x_1 < x_2 < ... < x_N         Input Input is given from Standard Input in the following format:  N  K  x_1  x_2  ...  x_N       Output Print the minimum time required to light K candles.        Sample Input 1 5 3
-30 -10 10 20 50      Sample Output 1 40  He should move and light candles as follows:   Move from coordinate 0 to -10 .  Light the second candle from the left.  Move from coordinate -10 to 10 .  Light the third candle from the left.  Move from coordinate 10 to 20 .  Light the fourth candle from the left.        Sample Input 2 3 2
10 20 30      Sample Output 2 20       Sample Input 3 1 1
0      Sample Output 3 0   There may be a candle placed at coordinate 0 .        Sample Input 4 8 5
-9 -7 -4 -3 1 2 3 4      Sample Output 4 10

Solution (Python):
def main():
    import numpy as np
    n,k=map(int,input().split())
    X=list(map(int,input().split()))
    X=np.array(X)
    Xp,Xz,Xm=X[X>0],X[X==0],X[X<0]
    lp,lm=len(Xp),len(Xm)

    if 0 in Xz:
        k-=1
    if k==0:
      print(0)
      exit()
      
    ans=10**9
    if k<=lp:
        ans=min(ans,Xp[k-1])
    if k<=lm:
        ans=min(ans,-Xm[-k])

    for i in range(lp):
        j=k-i-1
        if 0<j<=lm:
            ans=min(ans,Xp[i]*2-Xm[-j])
        else:
            continue
        
    for i in range(lm,0,-1):
        j=k-i-1
        if 0<=j<lp:
            ans=min(ans,Xm[-i]*(-2)+Xp[j])
        else:
            continue
    print(ans)
   
if __name__=='__main__':
    main()

Solution (Python):
N, K = [int(x) for x in input().split()]
xn = [int(x) for x in input().split()]

diffs = []
ranges = []
for i in range(N - K + 1):
  diffs.append(xn[i+K -1] - xn[i])
  ranges.append((i, i + K - 1))
  
sums = []
for diff, range_ in zip(diffs, ranges):
  r = min(abs(xn[range_[0]]), abs(xn[range_[1]]))
  sums.append(r+diff)
print(min(sums))

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

ans = float('inf')
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if l < 0 and r < 0:
        ans = min(ans, max(abs(l), abs(r)))
    elif l <= 0 and r >= 0:
        ans = min(ans, min(abs(l), r)*2 + max(abs(l), r))
    elif l > 0 and r > 0:
        ans = min(ans, max(l, r))
print(ans)

Solution (Python):
n,k=map(int,input().split())
x=list(map(int,input().split()))
x.append(0)
x.sort()
x_0=x.index(0)

if x_0 == 0:
    print(x[k])
elif x_0==n:
    print(-x[-k-1])
else:
    r = min(n-x_0, k)
    l = min(x_0, k)
    ans1=float("inf")
    ans2=float("inf")
    
    for i in range(1,r+1):
        if  k-i <= l:
            s = 2*x[x_0 + i] - x[x_0 - (k-i)]
            ans1=min(ans1, s)
    
    for i in range(1,l+1):
        if k-i <= r:
            s = -2*x[x_0 - i] + x[x_0 + (k-i)]
            ans2=min(ans2, s)
    print(min(ans1, ans2))

Solution (Python):
n, k = map(int, input().split())
X = list(map(int, input().split()))
l, r = -1, n
while r - l > 1:
    p = (l + r) // 2
    if X[p] < 0:
        l = p
    else:
        r = p

if l == -1:
    print(X[k - 1])
elif r == n:
    print(abs(X[n - k]))
elif n == k:
    print(min(2 * abs(X[0]) + X[-1], abs(X[0]) + 2 * X[-1]))
else:
    best_score = 10 ** 9
    for i in range(max(l - k + 1, 0), n - k):
        score = 0
        if i > r:
            break
        if X[i + k] < 0:
            score = abs(X[i])
        elif X[i] > 0:
            score = X[i + k - 1]
        elif abs(X[i]) < abs(X[i + k - 1]):
            score = 2 * abs(X[i]) + X[i + k - 1]
        else:
            score = abs(X[i]) + 2 * X[i + k - 1]
        if score < best_score:
            best_score = score
    print(best_score)

Solution (Python):

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


Ans=10**9
for i in range(N-K+1):
    XL,XR=X[i],X[i+K-1]
    if XL*XR>0:
        Ans=min(Ans,max(abs(XR),abs(XL)))
    else:
        Ans=min(Ans,abs(XR)*2+abs(XL),abs(XL)*2+abs(XR))
print(Ans)

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

min_time = 10**10
for i in range(n-k+1):
    time = abs(x[i] - x[i+k-1]) + min(abs(x[i]), abs(x[i+k-1]))
    min_time = min(time, min_time)

print(min_time)

Solution (Python):
n,k=map(int,input().split())
x=sorted(list(map(int,input().split())))
a=[]
for i in range(n-k+1):
    l=x[i]
    r=x[i+k-1]
    a.append(min(abs(l)+abs(r-l),abs(r)+abs(l-r)))
print(min(a))

Solution (Python):
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+K-1]<=0)&(arr[i]<=0):
    path = abs(arr[i])
  elif (arr[i+K-1]>0)&(arr[i]<=0):
    path  = arr[i+K-1]-arr[i] + min(abs(arr[i+K-1]),abs(arr[i]))
  else:
    path  = abs(arr[i+K-1])
  
  ans = min(path,ans)

print(ans)

Solution (Python):
f = lambda:map(int,input().split())
n,k = f()
x = list(f())
d = 10**9
for i in range(n-k+1):
  l,r = x[i],x[i+k-1]
  d = [min(d,max(-l,r)),min(d,(r-l)+min(-l,r))][l<0<r]
print(d)

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))

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

print(ans)

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

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

ans = 1000000000

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

print(ans)

Solution (Python):
n,k=map(int,input().split())

x=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
    ans.append(min(abs(x[i])+x[i+k-1]-x[i] , abs(x[i+k-1])+(x[i+k-1]-x[i])))
    
print(min(ans))

Solution (Python):
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()

Solution (Python):
def examA():
    N, i = LI()
    ans = N - i + 1
    print(ans)
    return

def examB():
    H, W = LI()
    A = [SI() for _ in range(H)]
    h = [[]for _ in range(H)]
    for i in range(W):
        flag = False
        for j in range(H):
            if A[j][i]=="
                flag = True
                break
        if flag:
            for j in range(H):
                h[j].append(A[j][i])
    
    ans = []
    for i,a in enumerate(h):
        if "
            ans.append(i)
    for v in ans:
        print("".join(map(str,h[v])))
    return

def examC():
    N, K = LI()
    X = LI()
    ans = inf
    for i in range(N-K+1):
        if X[i]<0 and X[i+K-1]>0:
            cur = (-X[i] + X[i+K-1]) + min(-X[i], X[i+K-1])
        else:
            cur = max(-X[i], X[i+K-1])
        ans = min(ans,cur)
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

"""

"""

Solution (Python):
n, k = map(int, input().split())
X = list(map(int, input().split()))

ans = 1e9

for i in range(n-k+1):
    ans = min(ans, abs(X[i]) + abs(X[i+k-1]-X[i]),
              abs(X[i+k-1]) + abs(X[i+k-1]-X[i]))

print(ans)

Solution (Python):
def main():
    INF = 10**18
    N,K = map(int,input().split())
    x = list(map(int,input().split()))
    ans = INF
    for i in range(K-1,N):
        ans = min(ans,abs(x[i]-x[i-K+1])+min(abs(0-x[i]),abs(0-x[i-K+1])))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
  N, K = map(int, input().split(" "))
  X = list(map(int, input().split(" ")))
  answer = 10 ** 9 + 7
  for i in range(0, N - K + 1):
    time = X[i + K - 1] - X[i] + min(abs(X[i]), abs(X[i + K - 1]))
    answer = min(answer, time)
  print(answer)


if __name__ == "__main__":
  main()

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'

    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()

input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a
 
show_flg=False
show_flg=True
 
ans=0

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

print(ans)

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float("inf")
for i in range(N-K+1):
    l,r = x[i],x[i+K-1]
    if l*r>=0:
        ans = min(ans,max(abs(l),abs(r)))
    else:
        ans = min(ans,min(abs(l)*2+r,abs(l)+r*2))
print(ans)

Solution (Python):
N,K=map(int, input().split())
x = list(map(int, input().split()))

xp = []
xm = []
for i in range(N):
  if x[i] > 0:
    xp.append(x[i])
  elif x[i] < 0:
    xm.append(-x[i])
  else:
    K -= 1
xm.reverse()

ans = 1000000000
n = max(0, K - len(xp) )
for i in range(n, min(len(xm)+1,min(len(xp),K))):
  if i == 0:  m = 0
  else:       m = xm[i-1]
  ans = min(ans, m * 2 + xp[K - i - 1] )


n = max(0, K - len(xm) )
for i in range(n, min(len(xp)+1,min(len(xm),K))):
  if i == 0:  p = 0
  else:       p = xp[i-1]
  ans = min(ans, p * 2 + xm[K - i - 1] )

if ans == 1000000000: ans = 0
print(ans)

Solution (Python):
def main():
    
    N, K = map(int, input().split(" "))
    coordinates = list(map(int, input().split(" ")))
    result = 10 ** 9 + 7
    for i in range(N - K + 1):
        result = min(result, abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i]),
                     abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i + K - 1]))
    print(result)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

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

x0=bisect.bisect_right(x,0)

ans=10**9
for i in range(K):
    if x0+i>=N:
        break
    r=x[x0+i]
    if x0-K+i+1>=0 and x0-K+i+1 <x0:
        l=-1*x[x0-K+i+1]
        ans=min(ans,r*2+l,l*2+r)
    
if x0+K-1<N:
    ans=min(ans,x[x0+K-1])

if x0-K>=0:
    ans=min(ans,-1*x[x0-K])
    
print(ans)

Solution (Python):
n, k = map(int, input().split())
xxx = list(map(int, input().split()))
print(min(min(abs(l), abs(r)) + r - l for l, r in zip(xxx, xxx[k - 1:])))

Solution (Python):
N, K = map(int, input().split())
li = list(map(int, input().split()))

ans = 10**10

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

print(ans)

Solution (Python):
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)

Solution (Python):
N,K=[int(i) for i in input().split()]
X=[int(i) for i in input().split()]
m=10**9
for i in range(N-K+1):
    m=min(m,X[K-1+i]-X[i]+min(abs(X[K-1+i]),abs(X[i])))

print(m)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
ans = []

for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    ans.append(abs(r-l)+min(abs(l),abs(r)))
print(min(ans))

Solution (Python):
import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
x=[-10**10]*k+x+[10**10]*k
s=bisect.bisect_left(x,0)
if x[s]==0:
    m=min(-x[s-(k-1)],x[s+(k-1)])
    for j in range(k):
        m=min(m,2*(x[s+j]-x[s-(k-1)+j])-max(-x[s-(k-1)+j],x[s+j]))
else:
    m = min(-x[s-1 - (k - 1)], x[s + (k - 1)])
    for j in range(k-1):
        m = min(m, 2 * (x[s + j] - x[s - (k - 1) + j]) - max(-x[s - (k - 1) + j], x[s + j]))
print(m)

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
l = 0

ans = 10 ** 18
for r in range(k-1, len(x)):
    ans = min(ans, abs(x[l]) + abs(x[r] - x[l]))
    ans = min(ans, abs(x[r]) + abs(x[r] - x[l]))
    l += 1
print(ans)

Solution (Python):
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)


cnt = 10000000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    
    
    
    
    s = max(0,K-len(M)+1)
    e = min(K+1, len(P))
    for i in range(s, e):
        cnt =min(cnt,P[i]*2 + M[K-i])
    
    s1 = max(0,K-len(P)+1)
    e1 = min(K+1, len(M))
    for i in range(s1, e1):
        cnt =min(cnt,M[i]*2 + P[K-i])

print(cnt)

Solution (Python):
N,K = map(int,input().split())
candle = list(map(int,input().split()))

min_value = 10**9

for i in range(N-K+1):
    left = abs(candle[i])+abs(candle[i]-candle[i+K-1])
    right = abs(candle[i+K-1])+abs(candle[i]-candle[i+K-1])
    if min_value > min(left,right):
        min_value = min(left,right)
        
        
print(min_value)

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

s = 10**9
for i in range(n-k+1) :
  m = min(abs(x[i]) +abs(x[i+k-1] - x[i]) , abs(x[i+k-1]) + abs(x[i+k-1] - x[i]))
  if s >m :
    s = m
print(s)

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

s = 10**9
for i in range(n-k+1) :
  f1 = x[i]
  f2 = x[i+k-1]
  m = min(abs(f1) +abs(f2 - f1) , abs(f2) + abs(f2 - f1))
  if s >m :
    s = m
print(s)

Solution (Python):
n,k = map(int, input().split())
X = list(map(int, input().split()))
ans = []


for i in range(n-k+1):
  l = X[i]
  r = X[i+k-1]
  
  ans.append(abs(r-l)+min(abs(l), abs(r)))
  
print(min(ans))

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter

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

X = list(map(int, input().split()))
 
ans = 10**18
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:
        ans = min(ans, 2*abs(X[i])+abs(X[i+K-1]), abs(X[i])+2*abs(X[i+K-1]))
 
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

X = []
K = 0
def fullsearch():
    for l, r in zip(X, X[K-1:]):
        distance = abs(r - l)
        yield distance + abs(r)
        yield distance + abs(l)

def main():
    global X, K
    _, K = map(int, input().split())
    X = list(map(int, input().split()))

    print(min(fullsearch()))

main()

Solution (Python):
import sys
input = sys.stdin.readline

def fullsearch(x, k):
    for l, r in zip(x, x[k-1:]):
        distance = abs(r - l)
        yield distance + abs(r)
        yield distance + abs(l)

def main():
    _, K = map(int, input().split())
    X = list(map(int, input().split()))



    print(min(fullsearch(X, K)))

main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    _, K = map(int, input().split())
    X = list(map(int, input().split()))

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print(min(fullsearch()))

main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    _, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print(min(fullsearch()))

main()

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = [0] * (N-K+1)

for i in range(N-K+1):
    xr = x[i+K-1]
    xl = x[i]
    lr = abs(xl) + abs(xr-xl)
    rl = abs(xr) + abs(xr-xl)
    ans[i] = min(lr,rl)
a = min(ans)
print(a)

Solution (Python):
n,k = map(int,input().split())
tmp_x = list(map(int, input().split()))
import bisect

x=[]
for tmp in tmp_x:
    if tmp==0:
        k-=1
    else:
        x.append(tmp)
if k==0:
    print(0)
    exit()

z_ind = bisect.bisect_left(x,0)

mn_res=10**9
for i in range(1,k+2):
    l_ind = z_ind-i+1
    r_ind = z_ind-i+k

    if l_ind < 0:
        continue
    if r_ind >= len(x):
        continue

    l = x[l_ind]
    r = x[r_ind]

    if l > 0:
        result = r
    elif r< 0:
        result = -l
    else:
        result = max(abs(l),abs(r)) + 2 * min(abs(l),abs(r))

    if mn_res > result:
        mn_res=result
print(mn_res)

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K+1):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            yield min(distance + abs(r), distance + abs(l))

    print(min(fullsearch()))

main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K+1):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print(min(fullsearch()))

main()

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

ans = 10**10

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

print(ans)

Solution (Python):
import sys
 
stdin = sys.stdin

mod = 1000000007
inf = 1 << 60
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
x = na()

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

print(ans)

Solution (Python):





import sys
from bisect import bisect_left
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


N, K = read_ints()
X = read_ints()



if X[0] >= 0:
    print(X[K - 1])
    exit()

if X[-1] <= 0:
    print(abs(X[-K]))
    exit()


zeroidx = bisect_left(X, 0)
if X[zeroidx] == 0:
    K -= 1

l = max(zeroidx - K, 0)


ans = 10**16

for i in range(K):
    ll = l + i
    rr = ll + K - 1
    if rr >= len(X):
        break
    
    ans = min(ans, 2 * abs(X[ll]) + abs(X[rr]),
              abs(X[ll]) + 2 * abs(X[rr]))


print(ans)

Solution (Python):
n, k = map(int, input().split())
nums = [int(i) for i in input().split()]



m = 10 ** 10

for i in range(n-k+1):
    m = min(m, abs(nums[i]) + nums[i+k-1] - nums[i])


nums = nums[::-1]
nums = [num * -1 for num in nums]


for i in range(n-k+1):
    m = min(m, abs(nums[i]) + nums[i+k-1] - nums[i])
print(m)

Solution (Python):
def main():
    _, k = map(int, input().split())
    x = tuple(map(int, input().split()))
    print(min(r - l + min(abs(l), abs(r)) for l, r in zip(x, x[k - 1 :])))
 
 
if __name__ == "__main__":
    main()

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

a = 10**10
for left,x in enumerate(X):
  right = left + K - 1
  if right == N:
    break
  left_to_right = abs(X[left])+abs(X[right]-X[left])
  right_to_left = abs(X[right])+abs(X[right]-X[left])
  m = min(left_to_right, right_to_left)
  if a > m:
    a = m
    
print(a)

Solution (Python):
import sys
from bisect import bisect_left,bisect_right
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    x=list(map(int,input().split()))
    ans=INF
    zero=bisect_left(x,0)
    for i in range(K+1):
        if zero<i or N-zero<K-i:
            continue
        if i==K:
            tmp=abs(x[zero-i])
        elif i==0:
            tmp=x[zero+K-i-1]
        else:
            tmp=min(abs(x[zero-i])*2+x[zero+K-i-1],abs(x[zero-i])+x[zero+K-i-1]*2)
        ans=min(ans,tmp)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
lst = list(map(int, input().split()))

res = float('inf')
for i in range(n-k+1):
    l = lst[i]
    r = lst[i+k-1]
    distlr = abs(l) + abs(l-r)
    distrl = abs(r) + abs(r-l)
    min_ = min(distlr, distrl)
    if min_ < res:
        res = min_
print(res)

Solution (Python):
import sys
from bisect import bisect_left

N, K, *X = map(int, open(0).read().split())


X = [x for x in X if x != 0]

zero_index = bisect_left(X, 0)

K -= N - len(X)


if K <= 0:
  print(0)
  sys.exit()

ret = float("inf")



if zero_index > K:
  head_start = zero_index - K
  tail_start = zero_index - 1
else:
  head_start = 0
  tail_start = K - 1
  


for head, tail in zip(X[head_start:], X[tail_start:]):
  if head < 0 and tail < 0:
    ret = min(ret, -head)
  elif head < 0 and tail > 0:
    tmp = min(2 * tail - head, tail - 2 * head) 
    ret = min(ret, tmp)
  else: 
    ret = min(ret, tail)

print(ret)

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

Xp = [0]+[+x for x in X if x>=0]
Xm = [0]+[-x for x in reversed(X) if x< 0]

Np = len(Xp)-1
Nm = len(Xm)-1

ans = 10**10
for np in range(min(Np, K), max(0, K-Nm)-1, -1):
    nm = K-np
    time = min(Xp[np], Xm[nm]) + Xp[np] + Xm[nm]
    ans = min(ans, time)

print(ans)

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))

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

Solution (Python):
from bisect import bisect_left

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

idx = bisect_left(x, 0)

l = x[:idx][::-1]
r = x[idx:]

if K-1 <= len(l):
    s = 0
else:
    s = K-1-len(l)
g = min(len(r), K) 
ans = float("inf")    
for i in range(s, g):
    if i == K-1:
        ans = min(ans, r[i])
    else:
        ans = min(ans, 2 * r[i] + abs(l[K-i-2]), 2 * abs(l[K-i-2]) + r[i])

if len(l) >= K:
    ans = min(ans, abs(l[K-1]))

print(ans)

Solution (Python):
"""
0からLまで行き、LからRまで行く = abs(L) + (R-L)
0からRまで行き、RからLまで行く = abs(R) + (R-L)
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 1<<60

for i in range(0, N-K+1):
  left = A[i]
  right = A[i+K-1]
  ans = min(ans, min(abs(left), abs(right)) + right - left)
print(ans)

Solution (Python):
N, K = map(int, input().split())
posX = []
negX = []
for x in map(int, input().split()):
    if 0 <= x:
        posX.append(x)
    else:
        negX.append(-x)

negX.sort()

len_posX = len(posX)
len_negX = len(negX)

ans = float('inf')
for pos_k in range(len_posX + 1):
    neg_k = K - pos_k
    if len_negX <= neg_k - 1:
        continue

    if neg_k == 0 and len_posX != 0:
        ans = min(ans, posX[pos_k - 1])
    elif pos_k == 0 and len_negX != 0:
        ans = min(ans, negX[neg_k - 1])
    elif len_posX != 0 and len_negX != 0:
        ans = min(ans, posX[pos_k - 1] + 2 * negX[neg_k - 1], 2 * posX[pos_k - 1] + negX[neg_k - 1])

print(ans)

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10 ** 9
now = 0
for i in range(n-k+1):
    ltor = abs(now - x[i]) + abs(x[i+k-1] - x[i])
    rtol = abs(now - x[i+k-1]) + abs(x[i+k-1] - x[i])
    tmp = min(ltor, rtol)
    ans = min(ans, tmp)
print(ans)

Solution (Python):
import bisect
n,k = map(int,input().split())
l = list(map(int,input().split()))


ans = 10**10

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

Solution (Python):
N, K = map(int, input().split())
Xn = list(map(int, input().split()))

Ln = [0] * N
for i in range(N-1):
    Ln[i+1] = abs(Xn[i+1] - Xn[i]) + Ln[i]

Rn = [0] * N
for i in range(1, N)[::-1]:
    Rn[i-1] = abs(Xn[i] - Xn[i-1]) + Rn[i]

ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, abs(Xn[i]) + Ln[i+K-1] - Ln[i])

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

print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
k-=1
ans=1<<99
for i in range(n-k):
    ans=min(ans,abs(a[i])+a[k+i]-a[i],abs(a[k+i])+a[k+i]-a[i])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**18

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

print(ans)

Solution (Python):
n,k=map(int,input().split())
X=list(map(int,input().split()))
r=10**9
for i in range(n-k+1):
    a=X[i]
    b=X[i+k-1]
    if a*b>=0:
        t=max(abs(a),abs(b))
    else:
        t=min((-a)*2+b,(-a)+2*b)
    r=min(r,t)
print(r)

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

distans_list = [100000000000] * N
for i in range(N - K + 1):
    if X[i] < 0:
        if X[i+K-1] < 0:
            distans_list[i] = abs(X[i])
        else:
            if abs(X[i]) < abs(X[i+K-1]):
                distans_list[i] = 2 * abs(X[i]) + X[i+K-1]
            else:
                distans_list[i] = abs(X[i]) + 2 * X[i+K-1]
    else:
        distans_list[i] = X[i+K-1]

print(min(distans_list))

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=N-K+1
long=10**9
for i in range(a):
  l=A[i+K-1]-A[i]+min(abs(A[i+K-1]),abs(A[i]))
  long=min(long,l)
print(long)

Solution (Python):
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]
    if left * right < 0:
        result = min(abs(left), abs(right)) * 2 + max(abs(left), abs(right))
    else:
        result = max(abs(left), abs(right))
    ans.append(result)

print(min(ans))

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    ans = float("inf")
    for i in range(N + 1 - K):
        l = x[i]
        r = x[i + K - 1]
        if l < 0 < r:
            ans = min(ans, abs(l) + abs(r) + min(abs(l), abs(r)))
        else:
            ans = min(ans, max(abs(l), abs(r)))

    print(ans)

    


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    plus = [0]
    j = 0
    for i in range(N):
        if x[i] >= 0:
            plus.append(x[i])
            j += 1

    j = 0
    minus = [0]
    for i in range(N - 1, -1, -1):
        if x[i] < 0:
            minus.append(x[i])
            j += 1

    ans = float("inf")
    for i in range(0, K + 1):
        if len(plus) - 1 < i:
            continue

        if len(minus) - 1 < K - i:
            continue


        rsum = plus[i]
        lsum = abs(minus[K - i])

        if rsum >= lsum:
            ans = min(ans, rsum + lsum * 2)
        else:
            ans = min(ans, rsum * 2 + lsum)

    print(ans)

    


if __name__ == '__main__':
    main()

Solution (Python):


n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 10 ** 10
for i in range(n - k + 1):
    if arr[i] < 0 and arr[i + k - 1] < 0:
        ans = min(ans, abs(arr[i]))
    elif arr[i] < 0 and 0 <= arr[i + k - 1]:
        ans = min(
            ans, abs(arr[i]) * 2 + arr[i + k - 1], abs(arr[i]) + arr[i + k - 1] * 2
        )
    elif 0 <= arr[i] <= arr[i + k - 1]:
        ans = min(ans, arr[i + k - 1])

    
print(ans)

Solution (Python):


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


ans = float("inf")
for i in range(N-K+1):
  f = X[i]
  t = X[i+K-1]
  if f <= 0 <= t:
    ans = min(ans, min(abs(f), abs(t)) + abs(f)+abs(t))
  else:
    ans = min(ans, max(abs(f), abs(t)))

print(ans)

Solution (Python):
n, k = map(int, input().split())
*x, = map(int, input().split())
 
ans = float("inf")
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    ans = min(ans, min(abs(l), abs(r)) + r - l)
 
print(ans)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9
for i in range(n-k+1):
    if x[i]*x[i+k-1] < 0:
        left = 2*abs(x[i])+abs(x[i+k-1])
        right = 2*abs(x[i+k-1])+abs(x[i])
        ans= min(ans,min(left,right))
    else:
        ans = min(ans,max(abs(x[i]),abs(x[i+k-1])))
print(ans)