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):
n, k = map(int, input().split())
x = list(map(int, input().split()))
 
ans = []
 
for _ in range(n - k + 1):
    a = abs(x[k + _ - 1]) + abs(x[k + _ - 1] - x[_])
    b = abs(x[_]) + abs(x[_] - x[k + _ - 1])
    
    ans.append(min(a, b))
 
print(min(ans))

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
 
ans = []
 
for _ in range(n - k + 1):
    ans.append(abs(x[k + _ - 1]) + abs(x[k + _ - 1] - x[_]))
    ans.append(abs(x[_]) + abs(x[_] - x[k + _ - 1]))
    
 
print(min(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 r<=0:
    d=-l
  elif l<=0:
    d=min((-l)*2+r,(-l)+r*2)
  else:
    d=r
  ans=min(ans,d)
print(ans)

Solution (Python):



import sys

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

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


ans = 10 ** 18

for i in range(N - K + 1):
    ans = min(ans, min(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):
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        if num_list != [0]:
            k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k <= len(positive_list):
    ans_list.append(positive_list[k - 1])

if k <= len(negative_list):
    ans_list.append(-1 * negative_list[k - 1])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

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

time = 10 ** 10
for i in range(0, n-k+1):
  p = a[i]
  q = a[i+k-1]
  if q < 0:
    t = abs(p)
  elif p > 0:
    t = q
  else:
    t = abs(p) + q + min(abs(p), q)
  if t < time:
    time = t

print(time)

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

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

Solution (Python):
import sys
def input():
  return sys.stdin.readline().rstrip()

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

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

print(ans)

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

ans = 10**9

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
m = 3*10**8

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

Solution (Python):
n,k = map(int, input().split())
if n==1:
  print(abs(int(input())))
  exit()
a = list(map(int, input().split()))
g = []
for i in range(n-k+1):
  b = a[i]
  c = a[i+k-1]
  if c<=0:
    g.append(-b)
  elif b>=0:
    g.append(c)
  else:
    g.append(c-b+min(abs(b),abs(c)))
print(min(g))

Solution (Python):
N,K =(int(i) for i in input().split())
x = list((int(i) for i in input().split()))
i = 0
t_min = float("inf")
while i+K-1 <= N-1 :
  left = abs(x[i])+abs(x[i+K-1]-x[i])
  right = abs(x[i+K-1])+abs(x[i+K-1]-x[i])
  if min(left,right)<t_min:
    t_min = min(left,right)
  i += 1
print(t_min)

Solution (Python):
from collections import deque 
N, K = map(int,input().split())
Xlist = list(map(int,input().split()))
Ans = 10**13
for i in range(len(Xlist)-K+1):
    Xleft = Xlist[i]
    Xright = Xlist[i+K-1]
    Dis = min(abs(Xleft),abs(Xright))+(Xright-Xleft)
    Ans = min(Dis,Ans)
print(Ans)

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
XN, XP = [], []
for x in X:
    if x < 0: XN.append(-x)
    else: XP.append(x)
XN.reverse()
T = [0]
S = 0
for i, x in enumerate(XN):
    if i==K-1:
        T.append(x)
        break
    T.append(2*x)
res = float("inf")
for i in range(K):
    if K-1-i >= len(XP) or i>=len(T):
        continue
    res = min(T[i] + XP[K-1-i], res)

T = [0]
S = 0
for i, x in enumerate(XP):
    if i==K-1:
        T.append(x)
        break
    T.append(2*x)
for i in range(K):
    if K-1-i >= len(XN) or i>=len(T):
        continue
    res = min(T[i] + XN[K-1-i], res)

print(res)

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

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

xlm = []
xlp = []
for x in xl:
    if x < 0: xlm.append(x*(-1))
    elif x == 0: k-=1
    else: xlp.append(x)

xlm.sort()
ans = 10**9
for i in range(min(k,len(xlm))+1):
    if i == 0: lv = 0
    else: lv = xlm[i-1]
    
    rem = k-i
    if rem > len(xlp): continue
    if rem == 0: rv = 0
    else: rv = xlp[rem-1]
    v = lv*2+rv
    ans = min(ans,v)

for i in range(min(k,len(xlp))+1):
    if i == 0: rv = 0
    else: rv = xlp[i-1]
    
    rem = k-i
    if rem > len(xlm): continue
    if rem == 0: lv = 0
    else: lv = xlm[rem-1]
    v = lv+rv*2
    ans = min(ans,v)

print(ans)

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

deq = deque()
r_init = 0
for i in range(n):
  if len(deq) == k and x[i] > 0:
    break
  deq.append(x[i])
  r_init = i
  if len(deq) > k:
    deq.popleft()

left = min(deq[0], 0)
right = max(deq[-1], 0)
ans = 0
if right <= 0:
  ans = abs(left)
elif left >= 0:
  ans = abs(right)
else:
  ans = min(abs(left),abs(right))+abs(left)+abs(right)


for i in range(r_init+1, n):
  m = deq.popleft()
  deq.append(x[i])
  if m >= 0:
    break
  left = abs(deq[0])
  right = abs(x[i])
  ans = min(ans, min(left,right)+left+right)

print(ans)

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

x=[int(x) for x in input().rstrip().split()]


ans=float('inf')
for i in range(n-k+1):
  ans=min(abs(x[i])+abs(x[i+k-1]-x[i]),ans)
  ans=min(abs(x[i+k-1])+abs(x[i+k-1]-x[i]),ans)
  
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):
    x1, xk = x[i], x[i + k - 1]
    if x1 * xk >= 0:
        t = max(abs(x1),abs(xk))
    elif x1 * xk < 0:
        t = min(abs(x1), abs(xk)) + abs(x1 - xk)
    ans = min(ans, t)
print(ans)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)
INF =  float("inf")
import bisect

N, K = list(map(int, input().split()))
x = [-INF] + list(map(int, input().split()))

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

print(ans)

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

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1001001001
for xi, xj in zip(x, x[k-1:]):
    ans = min(ans, min(abs(xi), abs(xj)) + abs(xi - xj))
print(ans)

Solution (Python):
import bisect

def main():
  n, k, *lst = map(int, open(0).read().split())
  z = bisect.bisect_left(lst, 0)
  if not z:
    print(lst[k - 1])
  elif z >= n - 1:
    print(-lst[-k])
  else:
    idx = max(z - k + 1, 0)
    print(min(min(abs(i), j) + j - i for i, j in zip(lst[idx:], lst[idx + k - 1:])))

if __name__ == '__main__':
  main()

Solution (Python):
import bisect

def main():
  n, k, *lst = map(int, open(0).read().split())
  z = bisect.bisect_left(lst, 0)
  if not z:
    print(lst[k - 1])
  elif z >= n - 1:
    print(-lst[-k])
  else:
    res = float('inf')
    for i in range(max(z - k + 1, 0), min(z, n - k) + 1):
      tmp = lst[i + k - 1] - lst[i] + min(abs(lst[i]), lst[i + k - 1])
      if tmp < res:
        res = tmp
    print(res)

if __name__ == '__main__':
  main()

Solution (Python):
import bisect

n, k, *lst = map(int, open(0).read().split())
z = bisect.bisect_left(lst, 0)
if not z:
  print(lst[k - 1])
  quit()
if z >= n - 1:
  print(-lst[-k])
  quit()
res = float('inf')
for i in range(max(z - k + 1, 0), min(z, n - k) + 1):
  tmp = lst[i + k - 1] - lst[i] + min(abs(lst[i]), lst[i + k - 1])
  if tmp < res:
    res = tmp
print(res)

Solution (Python):

import sys


def solve(N: int, K: int, x: "List[int]"):
    from bisect import bisect
    if 0 in x:
        x.remove(0)
        K -= 1
    if K == 0:
        return 0
    ans = float('inf')
    for l, r in zip(x, x[K-1:]):
        ans = min(ans, abs(r)+r-l)
        ans = min(ans, abs(l)+r-l)
    return ans


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    x = [int(next(tokens)) for _ in range(N)]  
    print(solve(N, K, x))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

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

X_dif = []
for i in range(N-1):
  X_dif.append(X[i+1]-X[i])

S = [0]
for i in range(N-1):
  S.append(S[i]+X_dif[i])



def f(x, y):
  return min(abs(x), abs(y))

for i in range(N-K+1):
  
  ans = min(ans, S[i+K-1] - S[i] + f(X[i], X[i+K-1]))

print(ans)

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)






from bisect import bisect_left,bisect_right

n,k=n1()
X=n1()

ans=float("inf")
for i in range(n-k+1):
    a=X[i]
    b=X[i+k-1]
    if a*b<0:
        tmp=min(abs(a)*2+abs(b),abs(a)+abs(b)*2)
    else:
        tmp=max(abs(a),abs(b))
    ans=min(ans,tmp)
print(ans)

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)






from bisect import bisect_left,bisect_right

n,k=n1()
X=n1()

x0=bisect_left(X,0)

if x0<n and X[x0]==0:
    k=k-1
    P=X[x0+1:]
    M=X[:x0]
else:
    P=X[x0:]
    M=X[:x0]

if k==0:
    print(0)
else:
    ans1=ans2=ans3=float("inf")
    if len(P)>=k:
        ans1=P[k-1]
    if len(M)>=k:
        ans2=-M[-k]

    for i in range(1,k):
        j=k-i
        if len(P)>=i and len(M)>=j:
            ans3=min(ans3,P[i-1]*2-M[-j],P[i-1]-2*M[-j])
    print(min(ans1,ans2,ans3))

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)


from collections import Counter,deque,defaultdict



from bisect import bisect_left,bisect_right

n,k=n1()
X=n1()

x0=bisect_left(X,0)
if x0<n and X[x0]==0:
    k=k-1
    P=X[x0+1:]
    M=X[:x0]
else:
    P=X[x0:]
    M=X[:x0]

if k==0:
    print(0)
else:
    ans1=ans2=ans3=float("inf")
    if len(P)>=k:
        ans1=P[k-1]
    if len(M)>=k:
        ans2=-M[-k]

    for i in range(1,k):
        j=k-i
        if len(P)>=i and len(M)>=j:
            ans3=min(ans3,P[i-1]*2-M[-j],P[i-1]-2*M[-j])
    print(min(ans1,ans2,ans3))

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

Solution (Python):
import sys
input = sys.stdin.buffer.readline
n,k = map(int,input().split())
x = [int(i) for i in input().split()]
l = []
r = []
for i in x:
  if i < 0:
    l.append(-i)
  else:
    r.append(i)
    
l = l[::-1]

if l == []:
  print(r[k-1])
elif r == []:
  print(l[k-1])
elif k == 1:
  print(min(r[0],l[0]))
else:
  L = len(l)
  R = len(r)
  ans = 3*10**8
  if L > k-1:
    ans = l[k-1]
  if R > k-1:
    ans = min(ans,r[k-1])
  for i in range(max(L,R)):
    if i < R and 0 <= k-2-i <= L-1 :
      ans = min(ans,r[i]*2+l[k-2-i])
    if i < L and 0 <= k-2-i <= R-1:
      ans = min(ans,l[i]*2+r[k-2-i])
  print(ans)

Solution (Python):
N, K = map( int, input().split() )
x = list( map( int, input().split() ))
 
ret = 10 ** 18
for i in range( N - K + 1 ):
  l, r = x[i], x[ K + i - 1 ]
  ret = min( ret, abs( l - r ) + min( abs( r ), abs( l ) ) )
print( ret )

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

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

Solution (Python):
from collections import defaultdict

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

result=10**9


if xn[-1]<=0:
    result=-xn[-K]
elif xn[0]>=0:
    result=xn[K-1]
else:
    
    for i in range(N):
        if xn[i]>=0:
            posi=i
            break
    
    for j in range(K-min(posi,K),min(K,N-posi)+1):
        if j==0:
            hantei=-xn[posi-K]

        elif j==K:
            hantei=xn[posi+K-1]
        else:
            hantei=min(xn[posi+j-1]*2-xn[posi-(K-j)],(xn[posi+j-1]-2*xn[posi-(K-j)]))
        result=min(hantei,result)

print(result)

Solution (Python):
N,M=list(map(int,input().split()))
n_l=list(map(int,input().split()))
ans=float("INF")
for i in range(N-M+1):
   ans=min(ans,abs(0-n_l[i])+abs(n_l[i]-n_l[i+M-1]),abs(0-n_l[i+M-1])+abs(n_l[i]-n_l[i+M-1]))
print(ans)

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

ans = float('inf')

for i in range(N-K+1):
    start = A[i]
    end = A[i+K-1]

    if start*end>0:
        dist = max(abs(start), abs(end))
    else:
        dist = abs(start)+abs(end)+min(abs(start), abs(end))

    ans = min(ans, dist)

print(ans)

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

timelist=[]
for i in range(n-(k-1)):
	bpoint=xlist[i+k-1]
	spoint=xlist[i]
	timelist.append(bpoint-spoint+min(abs(bpoint),abs(spoint)))

print(min(timelist))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
dist=[]
for i in range(n-k+1):
  if a[i+k-1]<=0:
    dist.append(-a[i])
  elif a[i]>=0:
    dist.append(a[i+k-1])
  else:
    dist.append(min(2*a[i+k-1]-a[i],a[i+k-1]-2*a[i]))

print(min(dist))

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

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

print(ans)

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

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

print(ans)

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
N_minus = len([m for m in x if m < 0])
ans = float('inf')

for i in range(N):
    left = i
    right = i + K - 1
    if right >= N:
        break

    if x[right] <= 0:
        ans = min(x[left] * (-1), ans)
    elif x[left] < 0:
        ans = min(x[left] * (-1) * 2 + x[right], ans)
        ans = min(x[left] * (-1) + x[right] * 2, ans)
    else:
        ans = min(x[right], ans)

print(ans)

Solution (Python):
from bisect import *
n,k=map(int,input().split())
c=list(map(int,input().split()))
a=bisect_left(c,0)
insort_left(c,0)
j=a
while 1:
  if j+1<=n:
    if c[j+1]==0:
      k-=1
      j+=1
    else:
      break
  else:
    break
ans=10**18
for i in range(k+1):
  if a+i<=n and a-k+i>=0:
    ans=min(ans,min(c[a+i],-c[a-k+i])*2+max(c[a+i],-c[a-k+i]))
if ans==10**18:
  print(0)
else:
  print(ans)

Solution (Python):
def resolve():
    from bisect import bisect
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    g=bisect(x,0)
    if x[n-1]<=0:
        print(abs(x[n-k]))
    elif x[0]>=0:
        print(x[k-1])
    else:
        if g>=k:
            s=g-k
        else:
            s=0
        t=0
        while s<=n-k:
            if x[s]<0 and x[s+k-1]>0:
                if t==0:
                    t=min(2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
                else:
                    t=min(t,2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
            else:
                t=min(t,max(abs(x[s]),abs(x[s+k-1])))
            s+=1
        print(t)
resolve()

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

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

if(N > 1):        
    print(ans)
else:
    print(a[0])

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
dist=[]
for i in range(n-k+1):
  if a[i+k-1]<=0:
    dist.append(-a[i])
  elif a[i]>=0:
    dist.append(a[i+k-1])
  else:
    dist.append(min(2*a[i+k-1]-a[i],a[i+k-1]-2*a[i]))

print(min(dist))

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

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

    if tmp < ans:
        ans = tmp
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):
  left = X[i]; right = X[i+K-1]
  if left >= 0:
    ans = min(ans, right)
    break
  elif right <= 0:
    ans = min(ans, -left)
  else:
    t = min(-left, right)*2 + max(-left, right)
    ans = min(ans, t)

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):
    if x[i] <= 0 and x[i+k-1] >= 0:
        ans = min(abs(x[i])+abs(x[i+k-1]) + min(abs(x[i]),abs(x[i+k-1])),ans)
    if x[i] >= 0 and x[i+k-1] >= 0:
        ans = min(x[i+k-1],ans)
    if x[i] <= 0 and x[i+k-1] <= 0:
        ans = min(abs(x[i]),ans)
print(ans)

Solution (Python):
import sys
N, K = map(int, input().split())
Xs = [int(s) for s in input().split()]

result = sys.maxsize
for i in range(len(Xs) - K + 1):
  a = abs(0 - Xs[i]) + Xs[i + K - 1] - Xs[i]
  b = abs(0 - Xs[i + K - 1]) + Xs[i + K - 1] - Xs[i]
  shortest = min(a, b)
  if shortest < result:
    result = shortest
    
print(result)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
mint = 10000000000
for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    if l > 0:
        t = r
    elif r < 0:
        t = -l
    else:
        t = min((2*r-l),r-2*l)
    mint = min(t,mint)
print(mint)

Solution (Python):
N,K=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
ans=[]
for i in range(0,N-K+1):
    start=x[i]
    goal=x[i+K-1]
    if goal<0:
        ans.append(abs(start))
    elif start<0:
        test=min(2*abs(start)+goal,2*goal+abs(start))
        ans.append(test)
    else:
        ans.append(goal)
print(min(ans))

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

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
a = A[0]
b = A[K-1]
ans = 0
if a*b < 0:
    ans = abs(min(-a, b)*2)+abs(max(-a, b))
else:
    ans = max(abs(a), abs(b))
for i in range(1, N-K+1):
    tmp = 0
    a = A[i]
    b = A[i+K-1]
    if a*b < 0:
        tmp = abs(min(-a, b)*2)+abs(max(-a, b))
    else:
        tmp = max(abs(a), abs(b))
    ans = min(ans, tmp)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


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

k -= x.count(0)

if k <= 0:
  print(0)
  exit()

n = [0]
for e in reversed(x):
  if e < 0:
    n.append(-e)

p = [0]
for e in x:
  if e > 0:
    p.append(e)


dprint(n, p)

anses = []

for i in range(k + 1):
  j = k - i
  if 0 <= i < len(p) and 0 <= j < len(n):
    anses.append(p[i] * 2 + n[j])
    anses.append(p[i] + n[j] * 2)

dprint(anses)

print(min(anses))

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7


def main():
    
    N, K = Scanner.map_int()
    X = Scanner.map_int()
    left = 0
    right = K - 1
    ans = int(1e09)
    while right < N:
        if Math.sign(X[left]) == Math.sign(X[right]):
            ans = min(ans, max(abs(X[left]), abs(X[right])))
        else:
            ans = min(ans, abs(X[left]) * 2 + abs(X[right]))
            ans = min(ans, abs(X[left]) + abs(X[right]) * 2)
        left += 1
        right += 1
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

i = 0
ans = float("inf")
while  i+k - 1 < n:
    l = x[i]
    r = x[i+k-1]
    ans = min(ans, min(abs(l), abs(r)) + r - l)
    i += 1

print(ans)

Solution (Python):
N, K = map(int, input().split())
PO, NE = [0], []
for a in input().split():
    a = int(a)
    if a >= 0: PO.append(a)
    else: NE.append(-a)

NE = [0] + NE[::-1]
ans = 1<<99

for i in range(K+1):
    j = K - i
    if i < len(PO) and j < len(NE):
        ans = min(ans, PO[i] * 2 + NE[j], PO[i] + NE[j] * 2)
print(ans)

Solution (Python):
def solve():
    N, K, *x = map(int, open(0).read().split())
    
    ans = 10 ** 9
    for l in range(N - K + 1):
        r = l + K - 1
        ans = min(ans, abs(x[l]) + abs(x[r] - x[l]), abs(x[r]) + abs(x[r] - x[l]))
    print(ans)


if __name__ == '__main__':
    solve()

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

for i in range(N - K + 1):
    x1 = abs(X[i]) + abs(X[K - 1 + i] - X[i])
    x2 = abs(X[K - 1 + i]) + abs(X[i] - X[K - 1 + i])
    
    x_tmp = min(x1, x2)
    x = min(x, x_tmp)

print(x)

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+k-1]-x[i]))
    ans=min(ans,abs(x[i+k-1])+abs(x[i+k-1]-x[i]))
print(ans)

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

md = 10**10
for i in range(N - K + 1):
    xl, xr = X[i], X[i + K - 1]
    if xl >= 0:
        d = xr
    elif xr <= 0:
        d = -xl
    else:
        d = min(-xl, xr) * 2 + max(-xl, xr)

    if d < md:
        md = d
print(md)

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

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

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+k-1]<=0:
        ans=min(ans,-x[i])
    elif x[i]>=0:
        ans=min(ans,x[i+k-1])
    else:
        ans=min(ans,x[i+k-1]-x[i]+min(x[i+k-1],-x[i]))
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
N,K= map(int,input().split())
x= list(map(int,input().split()))

cand = []
for i in range(N-K+1):
    l = i
    r = i+K-1
    if x[r]<0:
        cand.append(abs(x[l]))
    elif x[l] > 0:
        cand.append(abs(x[r]))
    else:
        cand.append(2*abs(x[l]) + abs(x[r]))
        cand.append(abs(x[l]) + 2*abs(x[r]))
print(min(cand))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = float('inf')
for i in range(n-k+1):
    l = i
    r = i + k - 1
    ldis = abs(a[l])+abs(a[r]-a[l])
    rdis = abs(a[r])+abs(a[r]-a[l])
    ans = min(ans, ldis, rdis)
print(ans)

Solution (Python):

import sys


def append_zero(x):
    if 0 in x:
        return
    if x[0] > 0:
        x.insert(0, 0)
        return
    if x[len(x)-1] < 0:
        x.append(0)
        return
    for i in range(1, len(x)):
        if x[i-1] < 0 and x[i] > 0:
            x.insert(i, 0)
            return


def solve(N, K, x):
    y = [0]
    append_zero(x)
    n = len(x)
    z = 0
    for i in range(1, n):
        y.append(y[i-1] + x[i] - x[i-1])
        if x[i] == 0:
            z = i
    
    
    if n == K:
        if n-1 == z or z == 0:
            return y[n-1]
        else:
            l = y[z] + y[n-1]
            r = y[n-1] - y[z] + y[n-1]
            return min(l, r)
    ans = 10e20
    for i in range(n - K):
        k = i + K
        if k <= z:
            ans = min(ans, y[z] - y[i])
        if z <= i:
            ans = min(ans, y[k] - y[z])
        if i <= z and z < k:
            l = y[z] - y[i] + y[k] - y[i]
            r = y[k] - y[z] + y[k] - y[i]
            ans = min(ans, l, r)
    return ans


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    x = [int(next(tokens)) for _ in range(N)]  
    print(solve(N, K, x))


if __name__ == '__main__':
    main()