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 = float("inf")
for i in range(N - K + 1):
    if ans > abs(x[i]) + (x[i + K - 1] - x[i]):
        ans = abs(x[i]) + (x[i + K - 1] - x[i])
    if ans > abs(x[i + K - 1]) + (x[i + K - 1] - x[i]):
        ans = abs(x[i + K - 1]) + (x[i + K - 1] - x[i])
print(ans)

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

minv = 10**18
for i in range(N-K+1):
    l, r = X[i], X[i+K-1]

    minv = min(minv, r-l+abs(l))
    minv = min(minv, r-l+abs(r))

print(minv)

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








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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()

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







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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()

Solution (Python):
n,k,*a=map(int,open(0).read().split());print(min([r-l+min(abs(l),abs(r))for l,r in zip(a,a[k-1:])]))

Solution (Python):
n,k,*a=map(int,open(0).read().split());print(min([a[i+k-1]-a[i]+min(abs(a[i+k-1]),abs(a[i]))for i in range(n-k+1)]))

Solution (Python):
n, k, *a = map(int, open(0).read().split())
ans = 1e11
for i in range(n-k+1):
    ans = min(ans, a[i+k-1]-a[i]+min(abs(a[i+k-1]),abs(a[i])))
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:
    dup = min(abs(X[i]), abs(X[i + k - 1])) * 2
    sgl = max(abs(X[i]), abs(X[i + k - 1]))
    dist = dup + sgl
  else:
    dist = max(abs(X[i]), abs(X[i + k - 1]))
  ans = min(ans, dist)
print(ans)

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

print(ans)

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

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

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

ans=float('inf')
for i in range(n-k+1):
    temp=x[i+k-1]-x[i]+min(abs(x[i]),abs(x[i+k-1]))
    if ans>temp:ans=temp
print(ans)

Solution (Python):

import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_right, bisect_left

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
A = LIST()

zero = A.count(0)

l = bisect_left(A, 0)
r = bisect(A, 0)
zero = r - l
C = []
for a in A[:l]:
    if a == 0:
        continue
    C.append(a)
    
B = C + A[r:]


w = K - zero
ans = INF
B.insert(0, -INF)
B.append(INF)
if zero == K:
    print(0)
    exit()

for i in range(len(B) - K + 1):
    r = B[i + K - zero - 1]
    l = B[i]
    tmp = INF
    if l < 0 and r > 0:
        tmp = min(abs(l), r) + r - l
    
    elif l > 0 and B[i-1] < 0:
        
        tmp = r

    elif r < 0 and B[i + K - zero - 1 + 1] > 0:
        
        tmp = abs(l)
    ans = min(ans, tmp)
print(ans)

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

    x.sort()
    xmin = x[0]
    xmax = x[k-1]

    ans = 0
    if xmin < 0 and xmax >= 0:
        ans = min([abs(xmin), abs(xmax)]) * 2 + max([abs(xmin), abs(xmax)])
    else:
        ans = max([abs(xmin), abs(xmax)])

    for i in range(n - k + 1):
        xmin = x[i]
        xmax = x[i + k - 1]
        if xmin < 0 and xmax >= 0:
            tmp = min([abs(xmin), abs(xmax)]) * 2 + max([abs(xmin), abs(xmax)])
        else:
            tmp = max([abs(xmin), abs(xmax)])
        if tmp < ans:
            ans = tmp

    print(ans)

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

ans = float("inf")
for left in range(N - K + 1):
    right = left + K - 1

    if X[left] <= 0 and X[right] <= 0:
        tmp = -X[left]
    elif X[left] >= 0 and X[right] >= 0:
        tmp = X[right]
    else:
        if abs(X[left]) >= abs(X[right]):
            tmp = abs(X[right]) * 2 + abs(X[left])
        else:
            tmp = abs(X[left]) * 2 + abs(X[right])
    ans = min(ans, tmp)

print(ans)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k = map(int,input().split())
X = list(map(int,input().split()))
for i in range(n-k+1):
  fi = X[i]
  en = X[i+k-1]
  if fi <= 0 and en >= 0:
    mans = min(mans,min(-fi,en)-fi+en)
  else:
    mans=min(mans,max(abs(fi),abs(en)))
print(mans)

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

print(ret)

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

Solution (Python):
n, k = map(int, input().split())
X = list(map(int, input().split()))
P = []
N = []
for x in X:
    if x >= 0:
        P.append(x)
    else:
        N.append(x)

P.sort()
N.sort(reverse=True)
P = [0]+P
N = [0]+N
ans = 10**16
for a in range(k+1):
    b = k-a
    if a > len(P)-1 or b > len(N)-1:
        continue
    else:
        ans = min(ans, min(2*P[a]+(-1)*N[b], P[a]+2*(-1)*N[b]))
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n, k = MAP()
x = LIST()
idx = -1
for i in range(n - 1):
    if x[i] < 0 and x[i + 1] >= 0:
        idx = i + 1
if idx == -1 and x[-1] < 0:
    print(-x[len(x) - k])
elif idx == -1 and x[0] >= 0:
    print(x[k - 1])
elif idx != -1:
    ans = inf
    for i in range(n - k + 1):
        tmp = abs(x[i]) + x[i + k - 1] - x[i]
        ans = min(ans, tmp)
    for i in range(k - 1, n):
        tmp = abs(x[i]) + x[i] - x[i - k + 1]
        ans = min(ans, tmp)
    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):
    t = min(abs(x[i]), abs(x[i+k-1]))
    ans = min(ans, t + abs(x[i]-x[i+k-1]))
print(ans)

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

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

print(ans)

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

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

cost = float("inf")
for i in range(n-k+1):
  j = i + k - 1
  cost = min(
    cost,
    abs(xx[i] - 0) + abs(xx[j] - xx[i]),
    abs(xx[j] - 0) + abs(xx[i] - xx[j])
  )
print(cost)

Solution (Python):
from sys import maxsize

n, k = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

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

print(time)

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

ans = float("inf")

for left in range(N-K+1):
    right = left + K - 1

    if X[left] <=0 and X[right] <= 0:
        cur = abs(X[left])
    elif X[left] >= 0 and X[right] >= 0:
        cur = abs(X[right])
    else:
        if abs(X[left]) >= abs(X[right]):
            cur = abs(X[right]) * 2 + abs(X[left])
        else:
            cur = abs(X[left]) * 2 + abs(X[right])
    ans = min(ans, cur)

print(ans)

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

ans = float('inf')
for l in range(N + 1 - K):
    r = l + K - 1
    ans = min(ans, min(abs(X[l]), abs(X[r])) + X[r] - X[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):
  a = abs(x[i+k-1] - x[i])
  ans = min(ans,min(abs(x[i+k-1])+a,abs(x[i])+a))
print(ans)

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

if x[K-1]*x[0]>0:
    tmp_min = max(abs(x[0]),abs(x[K-1]))
else:
    tmp_min = abs(x[0]-x[K-1])+min(abs(x[0]),abs(x[K-1]))

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

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

ans = 10**20
for i in range(N-K+1):
    fst = i
    lst = i+K-1

    if X[lst]<=0:
        ans = min(ans,abs(X[fst]))
    elif X[fst]>=0:
        ans = min(ans,X[lst])
    else:
        dis = max(abs(X[fst]),X[lst])+min(abs(X[fst]),X[lst])*2
        ans = min(ans,dis)

print(ans)

Solution (Python):
n,k=map(int,input().split())
x=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())
X = list(map(int,input().split()))
ans = 10**N+1
distance = 0
for i in range(N-K+1):
  distance = X[i+K-1]-X[i]
  ans = min(abs(X[i])+distance,abs(X[i+K-1])+distance,ans)
print(ans)

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

ans = float("inf")
for i in range(len(x)-(k-1)):
    xl = x[i]
    xr = x[i+k-1]
    candidate = min(abs(xl) + abs(xr-xl), abs(xr) + abs(xl-xr))
    ans = min(ans, candidate)

print(ans)

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

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

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

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

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

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

Solution (Python):


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


cnt_time = (10**8)*K
for l in range(N-K+1):
    r=l+K-1

    route1 = abs(x_list[l]) + abs(x_list[r] - x_list[l])
    route2 = abs(x_list[r]) + abs(x_list[r] - x_list[l])
    cnt_time = min( cnt_time, route1, route2 )

print(cnt_time)

Solution (Python):
n,k=map(int,input().split())
x=list(map(int,input().split()))
ans=10**9
for i in range(n-k+1):
    l=x[i]
    r=x[i+k-1]
    a=abs(l)+abs(l-r)
    b=abs(r)+abs(l-r)
    c=min(a,b)
    ans=min(c,ans)
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):
    l,r=i,i+k-1
    ans=min(ans,abs(A[r]-A[l])+abs(A[r]),abs(A[l])+abs(A[l]-A[r]))
print(ans)

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

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

Solution (Python):
import sys
import math
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
from collections import Counter
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache




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


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

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

    min_cost = float('inf')
    for i in range(n - k + 1):
        st = i
        ed = i + k - 1
        
        to_start = min(abs(xlist[st]), abs(xlist[ed]))
        start_end = xlist[ed] - xlist[st]

        if min_cost > to_start + start_end:
            min_cost = to_start + start_end

    print(min_cost)
        

if __name__ == "__main__":
    submit()

Solution (Python):
N, K = map(int, input().split())
m = float("inf")
x = sorted(list(map(int, input().split())))
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]-x[i+K-1]), m)
print(m)

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

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

Solution (Python):
def main():
  n,k=map(int,input().split())
  x=list(map(int,input().split()))
  ans=pow(10,10)
  for i in range(0,n-k+1):
    if x[i]*x[i+k-1]>=0:
      ans=min(ans,max(abs(x[i+k-1]),abs(x[i])))
    else:
      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)
if __name__=='__main__':
  main()

Solution (Python):
N, K = map(int, input().split())
lst = [int(x) for x in input().split()]
ret = 100 ** 100
for i in range(N - K + 1):
    if lst[i] >= 0 and lst[i + K - 1] >= 0:
        t = lst[i + K - 1]
    elif lst[i] < 0 and lst[i + K - 1] < 0:
        t = abs(lst[i])
    else:
        t = lst[i + K - 1] - lst[i] + min(abs(lst[i]), abs(lst[i + K - 1]))
    ret = min(ret, t)
print(ret)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def get_cost(min_v,max_v):
	if max_v<0:
		return abs(min_v)
	elif min_v >=0:
		return max_v
	else:
		ret = min(max_v+abs(min_v)*2, abs(min_v)+ max_v*2)
		return ret

def main():
	n, k = IN()
	al=list(IN())
	ret=min([get_cost(a,b) for a,b in zip(al,al[k-1:])])
	return ret
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

answer = 10 ** 18
for i in range(N - K + 1):
    left, right = i, i + K - 1
    al, ar = abs(X[left]), abs(X[right])
    if X[left] * X[right] >= 0:
        answer = min(answer, max(al, ar))
    else:
        answer = min(answer, al * 2 + ar, al + ar * 2)
        
print(answer)

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

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

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):
    l = a[i]
    r = a[i+k-1]
    time = min(abs(l) + abs(l-r), abs(r) + abs(l-r))
    
    ans = min(ans, time)

print(ans)

Solution (Python):
N,K = map(int,input().split())
X = list(map(int,input().split()))
print(min(X[i+K-1]-X[i]+min(abs(X[i]),abs(X[i+K-1])) for i in range(N-K+1)))

Solution (Python):
num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

print(time)

Solution (Python):
import numpy as np
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
arr = X[K - 1:] - X[:-K + 1]
arr1 = arr + np.abs(X[:-K + 1])
arr2 = arr + np.abs(X[K - 1:])
if len(arr) == 0:
    print(0)
else:
    ans = min(arr1.min(), arr2.min())
    print(ans)

Solution (Python):
import numpy as np
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
idx = np.searchsorted(X, 0)

ans = 10**10
for i in range(N - K + 1):
    l = i
    r = i + K - 1
    d1 = np.abs(X[l]) + np.abs(X[r] - X[l])
    d2 = np.abs(X[r]) + np.abs(X[r] - X[l])
    d = min(d1, d2)
    ans = min(ans, d)
print(ans)

Solution (Python):
def main():
    n, k = [int(e) for e in input().split()]
    X = [int(e) for e in input().split()]

    ans = float('inf')
    for i in range(n - k + 1):
        d = None
        add = None
        if abs(X[i]) > abs(X[i + k - 1]):
            d = abs(X[i])
            add = abs(X[i + k - 1])*2
        else:
            d = abs(X[i + k - 1])
            add = abs(X[i])*2

        if X[i] * X[i + k - 1] < 0:
            d += add

        ans = min(ans, d)

    print(ans)

if __name__ == "__main__":
    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):
    s,e = x[i],x[i+k-1]
    ans = min(ans,abs(s)+abs(e-s),abs(e)+abs(e-s))
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):
    s,e = x[i],x[i+k-1]
    if e<0:
        ans = min(ans,abs(s))
    elif s>0:
        ans = min(ans,e)
    else:
        ans = min(ans,abs(s)*2+e,abs(s)+e*2)
print(ans)

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

lis = [0]
for i in range(1,n):
    lis.append(lis[i-1] + abs(x[i]-x[i-1]))


ans = int(1e10)
for i in range(n-k+1):
    val = min(abs(x[i]),abs(x[i+k-1])) + (lis[i+k-1]-lis[i])
    
    
    if val < ans:
        ans = val

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    X = LI()
    X = sorted(X)
    pos_q, neg_q = [], []
    cur_i = N - 1
    for x in X:
        if x < 0:
            neg_q.append(x)
        elif x == 0:
            K -= 1
        else:
            pos_q.append(x)
    if K == 0:
        print(0)
        return
    ans = inf
    pos_q = sorted(pos_q)
    neg_q = sorted(neg_q)[::-1]
    
    for ix, p in enumerate(pos_q):
        if K - ix - 1 > 0: 
            nokori = K - ix - 1
            if 0 <= nokori - 1 < len(neg_q):
                ans = min(abs(neg_q[nokori-1] - p - p), ans)
        else:
            ans = min(p, ans)

    for ix, n in enumerate(neg_q):
        if K - ix - 1 > 0:
            nokori = K - ix -1
            if 0 <= nokori - 1 < len(pos_q):
                ans = min(pos_q[nokori-1] + abs(n) + abs(n), ans)
        else:
            ans = min(abs(n), ans)
    print(ans)

main()

Solution (Python):





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

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

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

Solution (Python):
from bisect import bisect_right

def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]
    o = bisect_right(x, 0)
    ans = 10**9
    for i in range(max(o - k, 0), min(o, n - k) + 1):
        a, b = x[i], x[i + k - 1]
        c, d = abs(a), abs(b)
        if a * b > 0:
            ans = min(max(c, d), ans)
        else:
            ans = min(min(c, d) * 2 + max(c, d), ans)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float("inf")
for left in range(0, N-K+1):
    right = left+K
    minus = 0
    plus = 0
    if X[left] < 0:
        minus = min(minus, X[left])
    if X[right-1] > 0:
        plus = max(plus, X[right-1])
    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)
 
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):
  d=x[i+k-1]-x[i]
  d+=min(abs(x[i]),abs(x[i+k-1]))
  ans=min(ans,d)
print(ans)

Solution (Python):
n, k = map(int,input().split())
x = list(map(int,input().split()))
result = []
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if l * r >= 0:
        result.append(max(abs(l), abs(r)))
    else:
        result.append(abs(2*l-r))
        result.append(abs(2*r-l))
print(min(result))

Solution (Python):
import numpy as np
n, k = map(int, input().split())
x = np.array(list(map(int, input().split())))

result = []
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if l*r >= 0:
        result.append(max(abs(l), abs(r)))
    else:
        result.append(abs(2*l-r))
        result.append(abs(2*r-l))
print(min(result))

Solution (Python):
N,K=map(int,input().split(' '))
x = list(map(int,input().split(' ')))
ans = 2*(10**8)
if N==K:
    if x[-1]<0:
        print(-x[0])
    elif x[0]>0:
        print(x[-1])
    else:
        print(min(x[-1]-2*x[0],2*x[-1]-x[0]))
    exit()
for i in range(N-K+1):
    l,r=x[i],x[i+K-1]
    if l>0:
        ans = min(ans,r)
    elif r<0:
        ans = min(ans,-l)
    else:
        ans = min(ans,r-2*l,2*r-l)
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):
    if x[i]<=0 and x[i+K-1]<=0:
        ans=min(ans,abs(x[i]))
    elif x[i]<=0 and x[i+K-1]>=0:
        ans=min(ans,x[i+K-1]-x[i]+min(x[i+K-1],abs(x[i])))
    else:
        ans=min(ans,x[i+K-1])
print(ans)

Solution (Python):
import math
S = input().split(" ")
N = int(S[0])
K = int(S[1])
arr = [int(s) for s in input().split(" ")]


def calculate(n, k, arr):
    minsArr = []
    plusArr = []
    for ar in arr:
        if ar < 0:
            minsArr.append(ar)
        else:
            plusArr.append(ar)

    
    startIndex = -10000

    if len(minsArr) > k:
        startIndex = len(minsArr) - k
    else:
        startIndex = 0

    minValues = []
    while startIndex <= len(minsArr):

        if k == N:
            s1 = arr[0]
            s2 = arr[-1]
            minValue = math.fabs(s2 - s1) + min(math.fabs(s2), math.fabs(s1))
            minValues.append(minValue)
            break

        if startIndex + k > len(arr):
            break

        s1 = arr[startIndex]
        s2 = arr[startIndex + k - 1]

        minValue = 0

        if s1 * s2 < 0:
            minValue = math.fabs(s2 - s1) + min(math.fabs(s2), math.fabs(s1))
        else:
            
            if s1 >= 0:
                minValue = math.fabs(s2)
            
            else:
                minValue = math.fabs(s1)

        minValues.append(minValue)
        startIndex = startIndex + 1

    return int(min(minValues))


result = calculate(N, K, arr)
print(result)

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

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

print(answer)

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

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

ans = 1e9

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

print(ans)

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

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


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_right
from bisect import bisect_left

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





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

if x[n-1]<0 :
    t=-x[-k]

    print(t)
elif x[n-1]==0 :
    t=-x[-k+1]

    print(t)

elif x[0]>0:
    t=x[k-1]

    print(t)
elif x[0]==0:
    t=x[k-1-1]

    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        k=k-1
        tmin=10**9

        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]


            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    else:
        tmin=10**9

        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]


            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)

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

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

ans = INF

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

print(ans)

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

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

Solution (Python):
import numpy as np
n,k=map(int,input().split())
X=list(map(int,input().split()))
X=np.array(X)
m=abs(X[:n-k+1])+abs(X[:n-k+1]-X[k-1:])
p=abs(X[k-1:])+abs(X[k-1:]-X[:n-k+1])
ans=np.min(np.minimum(p,m))
print(ans)