AtCoder Beginner Contest 107 - Candles

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-----
5 3
-30 -10 10 20 50

-----Sample Output-----
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.

Difficulty: EASY

Starter code:

Solutions:
(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)]))


(N, K) = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = []
for i in range(N - K + 1):
	left = i
	right = left + K - 1
	if x[left] < 0 and x[right] >= 0:
		q = min(abs(x[left]) * 2 + x[right], x[right] * 2 + abs(x[left]))
		ans.append(q)
		continue
	elif x[right] < 0:
		q = abs(x[left])
		ans.append(q)
		continue
	elif x[left] > 0:
		q = abs(x[right])
		ans.append(q)
		continue
if len(ans) == 0:
	print(0)
	return
print(min(ans))


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
ans = float('INF')
for i in range(N - K + 1):
	lr = abs(X[i]) + abs(X[i] - X[i + K - 1])
	rl = abs(X[i + K - 1]) + abs(X[i + K - 1] - X[i])
	ans = min(ans, min([lr, rl]))
print(ans)


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


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


(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)


(n, k) = map(int, input().split())
al = list(map(int, input().split()))
res = 10 ** 10
for i in range(n - k + 1):
	st = al[i]
	en = al[i + k - 1]
	if st * en < 0:
		temp = min(abs(st), abs(en)) * 2 + max(abs(st), abs(en))
	else:
		temp = max(abs(st), abs(en))
	res = min(temp, res)
print(res)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
d = 10 ** 9
for i in range(b - 1, a):
	e = abs(c[i] - c[i - b + 1])
	e += min(abs(c[i]), abs(c[i - b + 1]))
	d = min(d, e)
print(d)


from bisect import bisect_left
import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
x = list(map(int, input().split()))
x_shift = [i + abs(x[0]) for i in x]
dist = []
for i in range(N - K + 1):
	d_end = x_shift[i + K - 1] - x_shift[i]
	d_left = abs(x[i])
	d_right = abs(x[i + K - 1])
	dist.append(d_end + min(d_left, d_right))
print(min(dist))


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
tp = N
for i in range(N):
	if x[i] >= 0:
		tp = i
		break
	x[i] *= -1
s = max(0, tp - K + 1)
g = min(tp - 1, N - K)
ans = float('INF')
if K <= tp:
	ans = min(ans, x[tp - K])
if K <= N - tp:
	ans = min(ans, x[tp + K - 1])
for i in range(s, g + 1):
	ans = min(ans, x[i] + x[i + K - 1] + min(x[i], x[i + K - 1]))
print(ans)


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


(n, k) = map(int, input().split())
lst = list(map(int, input().split()))
ne_lst = []
po_lst = []
for i in lst:
	if i < 0:
		ne_lst.append(-i)
	else:
		po_lst.append(i)
ne_lst.reverse()
res = 10 ** 10
if len(ne_lst) >= k:
	res = min(res, ne_lst[k - 1])
if len(po_lst) >= k:
	res = min(res, po_lst[k - 1])
for (i, j) in enumerate(ne_lst):
	if k - (i + 1) > 0 and len(po_lst) > k - 1 - (i + 1):
		res = min(res, ne_lst[i] * 2 + po_lst[k - 1 - (i + 1)])
for (i, j) in enumerate(po_lst):
	if k - (i + 1) > 0 and len(ne_lst) > k - 1 - (i + 1):
		res = min(res, po_lst[i] * 2 + ne_lst[k - 1 - (i + 1)])
print(res)


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


(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)


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


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


(N, K) = map(int, input().split())
(*X,) = map(int, input().split())
i = 0
ans = []
while i + K - 1 < N:
	(a, b) = (X[i], X[i + K - 1])
	if b < 0:
		ans.append(abs(a))
	elif a > 0:
		ans.append(b)
	else:
		a = abs(a)
		ans.append(a + b + min(a, b))
	i += 1
print(min(ans))


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
ans = float('inf')
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)


(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
s = 10 ** 9
for i in range(n - k + 1):
	if x[i] * x[i + k - 1] >= 0:
		t = max(abs(x[i]), abs(x[i + k - 1]))
	else:
		t = abs(x[i]) + abs(x[i + k - 1]) + min(abs(x[i]), abs(x[i + k - 1]))
	s = min(s, t)
print(s)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
ans = []
for j in range(n - k + 1):
	if x[j] > 0:
		ans.append(x[j + k - 1])
	elif x[j] <= 0 <= x[j + k - 1]:
		ans.append(min(2 * abs(x[j]) + x[j + k - 1], abs(x[j]) + 2 * x[j + k - 1]))
	else:
		ans.append(-x[j])
print(min(ans))


(N, K) = map(int, input().split())
x = [int(i) for i in input().split()]
ans = float('inf')
for i in range(N - K + 1):
	n = x[i + K - 1] - x[i]
	ans = min(ans, n + abs(x[i]), n + abs(x[i + K - 1]))
print(ans)


(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
import bisect
b = bisect.bisect_left(x, 0)
r = x[b:]
l = list(reversed(x[:b]))
R = len(r)
L = len(l)
ans = float('inf')
if k <= R:
	ans = min(ans, r[k - 1])
if k <= L:
	ans = min(ans, abs(l[k - 1]))
for i in range(R):
	if i < k and k - i - 2 <= L - 1 and (k - i - 2 >= 0):
		tmp = 2 * r[i] + abs(l[k - i - 2])
		ans = min(ans, tmp)
for j in range(L):
	if j < k and k - j - 2 <= R - 1 and (k - j - 2 >= 0):
		tmp = 2 * abs(l[j]) + r[k - j - 2]
		ans = min(ans, tmp)
print(ans)


(N, K) = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(min((r - l + min(abs(l), abs(r)) for (l, r) in zip(X, X[K - 1:]))))


(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)


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
p = True
for i in range(N):
	if X[i] < 0:
		continue
	elif X[i] == 0:
		K -= 1
		if i < K:
			mX = [0] + [-x for x in X[:i]][::-1]
		else:
			mX = [0] + [-x for x in X[i - K:i]][::-1]
		pX = [0] + X[i + 1:i + 1 + K]
		p = False
		break
	else:
		if i < K:
			mX = [0] + [-x for x in X[:i]][::-1]
		else:
			mX = [0] + [-x for x in X[i - K:i]][::-1]
		pX = [0] + X[i:i + K]
		p = False
		break
if p:
	mX = [0] + [-x for x in X[::-1]]
	pX = [0]
ans = 10 ** 9
l = K - (len(pX) - 1)
n = len(mX)
for i in range(len(pX) - 1, -1, -1):
	if l < n:
		ans = min(ans, pX[i] + mX[l] * 2, pX[i] * 2 + mX[l])
	else:
		break
	l += 1
print(ans)


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


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


url = 'https://atcoder.jp//contests/abc107/tasks/arc101_a'

def main():
	(n, k) = list(map(int, input().split()))
	xs = list(map(int, input().split()))
	ans = 100000000000000
	se = [0, k - 1]
	while se[1] < n:
		if xs[se[0]] < 0 <= xs[se[1]]:
			tmp = min(abs(xs[se[0]]), xs[se[1]]) * 2 + max(abs(xs[se[0]]), xs[se[1]])
			ans = min(ans, tmp)
		elif xs[se[0]] < 0:
			ans = min(ans, abs(xs[se[0]]))
		elif xs[se[1]] >= 0:
			ans = min(ans, xs[se[1]])
		se = [v + 1 for v in se]
	print(ans)

def __starting_point():
	main()
__starting_point()


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


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


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
res = []
for i in range(N - K + 1):
	tmp = min(abs(x[i]), abs(x[i + K - 1])) + x[i + K - 1] - x[i]
	res.append(tmp)
print(min(res))


(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]
	tmp = min(abs(l) + abs(l - r), abs(r) + abs(l - r))
	ans = min(tmp, ans)
print(ans)


(N, K) = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = 10 ** 9
for i in range(N - K + 1):
	if X[i] >= 0:
		ans = min(ans, X[K + i - 1])
	elif X[K + i - 1] <= 0:
		ans = min(ans, -X[i])
	else:
		ans = min(ans, 2 * X[K + i - 1] - X[i], X[K + i - 1] - 2 * X[i])
print(ans)


def resolve():
	(N, K) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	ans = 1 << 60
	for i in range(N - K + 1):
		l = A[i]
		r = A[i + K - 1]
		l_to_r = abs(l) + abs(l - r)
		r_to_l = abs(r) + abs(r - l)
		res = min(l_to_r, r_to_l)
		ans = min(ans, res)
	print(ans)

def __starting_point():
	resolve()
__starting_point()


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


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, K, *X) = list(map(int, read().split()))
	A = [abs(x - X[0]) for x in X]
	ans = INF
	for i in range(N - K + 1):
		if ans > min(abs(X[i]), abs(X[i + K - 1])) + A[i + K - 1] - A[i]:
			ans = min(abs(X[i]), abs(X[i + K - 1])) + A[i + K - 1] - A[i]
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


(n, k, *a) = map(int, open(0).read().split())
ans = 10 ** 9
for i in range(n - k + 1):
	(b, c) = (a[i], a[i + k - 1])
	ans = min(ans, min(abs(b), abs(c)) + abs(b - c))
print(ans)


(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)


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


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


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


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


(n, k, *x) = list(map(int, open(0).read().split()))
ans = 10 ** 18
neg = [i for i in x if i < 0] + [0]
pos = [0] + [i for i in x if i >= 0]
l = len(neg) - max(0, k + 1 - len(pos))
r = min(k + 1, len(pos))
while l > 0 and r > 0:
	l -= 1
	r -= 1
	ans = min(ans, -neg[l] * 2 + pos[r], -neg[l] + pos[r] * 2)
print(ans)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
ans = float('inf')
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)


(N, K) = map(int, input().split())
(*X,) = map(int, input().split())
import bisect
i = bisect.bisect_left(X, 0)
L = max(0, bisect.bisect_right(X, 0) - K)
R = min(N - 1, bisect.bisect_left(X, 0) + K - 1)
ans = float('inf')
for i in range(L, R - K + 2):
	t = 0
	l = X[i]
	r = X[i + K - 1]
	if r <= 0:
		t = abs(l)
	elif 0 <= l:
		t = r
	else:
		t = min(abs(l), r) * 2 + max(abs(l), r)
	ans = min(ans, t)
print(ans)


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


(N, K, *x) = map(int, open(0).read().split())
ans = 1000000000.0
for i in range(N - K + 1):
	l = x[i]
	r = x[i + K - 1]
	ans = min(ans, abs(l) + (r - l), abs(r) + (r - l))
print(ans)


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


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


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


(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 - K + 1):
	left = X[i]
	right = X[i + K - 1]
	if left * right >= 0:
		ans = min(ans, max(abs(left), right))
	else:
		ans = min(ans, right - left + min(abs(left), right))
print(ans)


import math
from math import gcd, pi, sqrt
INF = float('inf')
import sys
sys.setrecursionlimit(10 ** 6)
import itertools
from collections import Counter, deque

def i_input():
	return int(input())

def i_map():
	return list(map(int, input().split()))

def i_list():
	return list(i_map())

def i_row(N):
	return [i_input() for _ in range(N)]

def i_row_list(N):
	return [i_list() for _ in range(N)]

def s_input():
	return input()

def s_map():
	return input().split()

def s_list():
	return list(s_map())

def s_row(N):
	return [s_input for _ in range(N)]

def s_row_str(N):
	return [s_list() for _ in range(N)]

def s_row_list(N):
	return [list(s_input()) for _ in range(N)]

def main():
	(n, k) = i_map()
	x = i_list()
	l = []
	r = []
	for i in x:
		if i < 0:
			l.append(-i)
		else:
			r.append(i)
	l.reverse()
	R_len = len(r)
	L_len = len(l)
	ans = INF
	for (i, j) in enumerate(l):
		if R_len >= k - i - 1:
			if R_len == 0:
				ans = min(ans, j)
			else:
				ans = min(ans, 2 * j + r[k - i - 2])
	for (i, j) in enumerate(r):
		if L_len >= k - i - 1:
			if L_len == 0:
				ans = min(ans, j)
			else:
				ans = min(ans, 2 * j + l[k - i - 2])
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
lis = list(map(int, input().split()))
if len(lis) == 1:
	print(abs(lis[0]))
	return
ans = 10 ** 9 + 7
for i in range(n - k + 1):
	l = abs(lis[i]) + abs(lis[i + k - 1] - lis[i])
	r = abs(lis[i + k - 1]) + abs(lis[i + k - 1] - lis[i])
	ans = min(ans, l, r)
print(ans)


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


(n, k) = list(map(int, input().split()))
ls = list(map(int, input().split()))
flag = True
if 0 not in ls:
	ls.append(0)
	ls.sort()
	flag = False
ind = ls.index(0)
if flag == True:
	if ind >= k - 1:
		l = ind - k + 1
	else:
		l = 0
	if ind + k - 1 <= n - 1:
		r = ind + k - 1
	else:
		r = n - 1
	mn = float('inf')
	while True:
		if l + k - 1 > r:
			break
		a = abs(ls[l]) + ls[l + k - 1] + min(abs(ls[l]), ls[l + k - 1])
		if a < mn:
			mn = a
		l += 1
else:
	if ind >= k:
		l = ind - k
	else:
		l = 0
	if ind + k <= n:
		r = ind + k
	else:
		r = n
	mn = float('inf')
	while True:
		if l + k > r:
			break
		a = abs(ls[l]) + ls[l + k] + min(abs(ls[l]), ls[l + k])
		if a < mn:
			mn = a
		l += 1
print(mn)


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


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
assert len(x) == N
m = min((x[i] - x[i - K + 1] + min(abs(x[i]), abs(x[i - K + 1])) for i in range(K - 1, N)))
print(m)


(N, K) = list(map(int, input().split()))
x = list(map(int, input().split()))
l = []
for i in range(N - K + 1):
	left = i
	right = i + K - 1
	ans1 = abs(x[left]) + abs(x[right] - x[left])
	ans2 = abs(x[right]) + abs(x[right] - x[left])
	tmp_ans = min(ans1, ans2)
	l.append(tmp_ans)
ans = min(l)
print(ans)


import sys
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.141592653589793

def read_str():
	return sys.stdin.readline().strip()

def read_int():
	return int(sys.stdin.readline().strip())

def read_ints():
	return map(int, sys.stdin.readline().strip().split())

def read_str_list():
	return list(sys.stdin.readline().strip().split())

def read_int_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def lcm(a: int, b: int) -> int:
	return a * b // math.gcd(a, b)

def Main():
	(n, k) = read_ints()
	x = read_int_list()
	ans = INF
	for i in range(n - k + 1):
		left = x[i]
		right = x[i + k - 1]
		if left < 0 and right > 0:
			ans = min(ans, min(abs(left), abs(right)) * 2 + max(abs(left), abs(right)))
		else:
			ans = min(ans, max(abs(left), abs(right)))
	print(ans)

def __starting_point():
	Main()
__starting_point()


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


(n, k) = list(map(int, input().split()))
xlst = list(map(int, input().split()))
minus = []
plus = []
for x in xlst:
	if x < 0:
		minus.append(-1 * x)
	elif x > 0:
		plus.append(x)
	else:
		k -= 1
if k == 0:
	print(0)
	return
minus.append(0)
minus = minus[::-1]
plus = [0] + plus
ans = 10 ** 20
ml = len(minus)
pl = len(plus)
pos = min(k, ml - 1)
while 1:
	mm = minus[pos]
	pp = plus[k - pos]
	ans_tmp = max(mm, pp) + min(mm, pp) * 2
	ans = min(ans, ans_tmp)
	pos -= 1
	if pos == -1 or k - pos == pl:
		break
print(ans)


def __starting_point():
	(n, k) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	ans = float('inf')
	for i in range(k + 1):
		if i + k > n:
			break
		left = A[i]
		right = A[i + k - 1]
		tmp2 = abs(left) + abs(right - left)
		tmp3 = abs(right) + abs(left - right)
		ans = min(ans, min(tmp2, tmp3))
	print(ans)
__starting_point()


from bisect import bisect_left
(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
pos = bisect_left(x, 0)
if pos < len(x) and x[pos] == 0:
	k -= 1
else:
	x.insert(pos, 0)
l = pos
r = pos + k
if r >= len(x):
	diff = r - len(x) + 1
	l -= diff
	r -= diff
ans = min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r])
while r >= pos:
	if l < 0:
		break
	dist = min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r])
	if dist < ans:
		ans = dist
	l -= 1
	r -= 1
print(ans)


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


(n, k) = map(int, input().split())
c = list(map(int, input().split()))
ans = float('Inf')
for i in range(n - k + 1):
	(l, r) = (c[i], c[i + k - 1])
	ans = min(abs(l) + abs(r - l), abs(r) + abs(r - l), ans)
print(ans)


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
ans = 1000000000.0
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)


(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)


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


import math
(N, K) = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
	if x[i] == 0:
		(p, m) = (i, i + 1)
		break
	elif x[i] > 0:
		(p, m) = (i, i)
		break
if x[0] < 0 and x[-1] > 0:
	pls = x[p:]
	mns = x[:m]
	ans = math.inf
	for i in range(len(pls)):
		if i < K - 1:
			if pls[0] == 0:
				n = K - i
			else:
				n = K - i - 1
			if len(mns) >= n:
				tmp = 2 * abs(mns[-n]) + pls[i]
				ans = min(ans, tmp)
		if i == K - 1:
			ans = min(ans, pls[i])
			break
	for i in range(len(mns)):
		m = len(mns) - i
		if m < K:
			if pls[0] == 0:
				n = K - m
			else:
				n = K - m - 1
			if len(pls) > n:
				tmp = 2 * pls[n] + abs(mns[-m])
				ans = min(ans, tmp)
		if m == K:
			ans = min(ans, abs(mns[i]))
elif x[0] >= 0:
	ans = x[K - 1]
else:
	ans = abs(x[-K])
print(ans)


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
T = 10 ** 18
for i in range(N - K + 1):
	if X[i + K - 1] <= 0:
		T = min(T, -X[i])
	elif X[i] >= 0:
		T = min(T, X[i + K - 1])
	else:
		T = min(T, X[i + K - 1] - X[i] + min(-X[i], X[i + K - 1]))
print(T)


(N, K) = list(map(int, input().split()))
X_list = list(map(int, input().split()))
INF = 10 ** 18
ans = INF
import bisect
zero_index = bisect.bisect_left(X_list, 0)
candidate_list = []
ans = INF
for i in range(K + 1):
	head = zero_index - i
	tail = head + K - 1
	if head < 0 or tail > N - 1:
		continue
	if i == 0:
		ans = min(ans, abs(X_list[tail]))
	elif i == K:
		ans = min(ans, abs(X_list[head]))
	else:
		head_abs = abs(X_list[head])
		tail_abs = abs(X_list[tail])
		ans = min(ans, head_abs + tail_abs + min(head_abs, tail_abs))
print(ans)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(abs, x))
ans = float('inf')
for i in range(n - k + 1):
	if 0 < x[i] * x[i + k - 1]:
		cost = max(y[i], y[i + k - 1])
	else:
		cost = y[i] + y[i + k - 1] + min(-x[i], x[i + k - 1])
	ans = min(ans, cost)
print(ans)


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


(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)


(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))


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
m = float('INF')
for i in range(N - K + 1):
	m = min(m, x[i + K - 1] - x[i] + min(abs(x[i + K - 1]), abs(x[i])))
print(m)


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


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
k = float('INF')
for i in range(N - K + 1):
	l = X[i]
	r = X[i + K - 1]
	k = min(k, abs(l) + abs(r - l), abs(r) + abs(r - l))
print(k)


(N, K) = list(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(r - l)))
print(min(a))


import math
import bisect
import numpy as np
(N, k) = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float('inf')
for i in range(N - k + 1):
	a = X[i]
	b = X[i + k - 1]
	if np.sign(a) * np.sign(b) >= 0:
		ans = min(ans, max(abs(a), abs(b)))
	else:
		ans = min(ans, abs(a) + abs(b) + min(abs(a), abs(b)))
print(ans)


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
m = min((b - a + min(abs(a), abs(b)) for (a, b) in zip(x, x[K - 1:])))
print(m)


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


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
ans = 10 ** 10
for i in range(N - K + 1):
	R = X[i + K - 1]
	L = X[i]
	A = [ans, abs(L) + abs(R - L), abs(R) + abs(R - L)]
	ans = min(A)
print(ans)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
minus = [0]
plus = [0]
for i in x:
	if x:
		plus.append(x)
	else:
		minus.append(-x)
k -= 1
ans = 1000000000000000
for i in range(n - k):
	if x[i + k] < 0:
		ans = min(ans, -x[i])
	elif 0 < x[i + k] and x[i] < 0:
		if -x[i] < x[i + k]:
			ans = min(ans, abs(2 * x[i]) + x[i + k])
		else:
			ans = min(ans, 2 * x[i + k] + abs(x[i]))
	else:
		ans = min(ans, x[i + k])
print(ans)


(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:]))))


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
c = 10 ** 9
for i in range(n - k + 1):
	a = x[i + k - 1]
	b = x[i]
	if a < 0:
		c = min(c, abs(b))
	elif b < 0:
		c = min(c, min(a, abs(b)) * 2 + max(a, abs(b)))
	else:
		c = min(c, a)
print(c)


def main():
	(n, k) = list(map(int, input().split()))
	x = list(map(int, input().split()))
	(m, p) = ([], [])
	for i in range(n):
		if x[i] < 0:
			m.append(x[i])
		else:
			p.append(x[i])
	(M, P) = (len(m), len(p))
	ans = float('inf')
	for i in range(min(M + 1, k + 1)):
		t = 0
		if i > 0:
			t += 2 * abs(m[M - i])
		r = k - i
		if r == 0:
			if t // 2 < ans:
				ans = t // 2
		elif r <= P:
			t += p[r - 1]
			if t < ans:
				ans = t
	for i in range(min(P + 1, k + 1)):
		t = 0
		if i > 0:
			t += 2 * p[i - 1]
		r = k - i
		if r == 0:
			if t // 2 < ans:
				ans = t // 2
		elif r <= M:
			t += abs(m[M - r])
			if t < ans:
				ans = t
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
v = 1000000000.0
for l in range(n - k + 1):
	r = l + k - 1
	a = abs(x[l]) + abs(x[l] - x[r])
	b = abs(x[r]) + abs(x[l] - x[r])
	v = min(v, min(a, b))
print(v)


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


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


(N, K) = list(map(int, input().split()))
x = list(map(int, input().split()))
minTime = 10000000000
for i in range(N - K + 1):
	time = 0
	minValue = x[i]
	maxValue = x[i + K - 1]
	if minValue < 0 and maxValue < 0:
		time = minValue * -1
	elif minValue >= 0 and maxValue >= 0:
		time = maxValue
	elif minValue < 0 and maxValue >= 0:
		leftTime = minValue * -1
		rightTime = maxValue
		if leftTime < rightTime:
			time = leftTime * 2 + rightTime
		else:
			time = leftTime + rightTime * 2
	if time < minTime:
		minTime = time
print(minTime)


(n, k) = map(int, input().split())
x = [int(i) for i in input().split()]
m = 4 * 10 ** 8
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)


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


import math
(N, K) = map(int, input().split())
cd = list(map(int, input().split()))
(cd_plus, cd_minus) = ([], [])
res = math.inf
for i in range(N):
	if cd[i] == 0:
		N -= 1
		K -= 1
		res = 0
	elif cd[i] > 0:
		cd_plus.append(cd[i])
	else:
		cd_minus.append(-cd[i])
cd_minus.sort()
for i in range(len(cd_plus)):
	if i + len(cd_minus) + 1 >= K:
		if K >= i + 2:
			res = min(res, cd_plus[i] * 2 + cd_minus[K - i - 2])
		else:
			res = min(res, cd_plus[i])
for i in range(len(cd_minus)):
	if i + len(cd_plus) + 1 >= K:
		if K >= i + 2:
			res = min(res, cd_minus[i] * 2 + cd_plus[K - i - 2])
		else:
			res = min(res, cd_minus[i])
print(res)


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


(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)


(n, k, *l) = map(int, open(0).read().split())
m = 10000000000.0
for (i, j) in zip(l, l[k - 1:]):
	if i * j < 0:
		m = min(m, -i * 2 + j, -i + j * 2)
	else:
		m = min(m, max(abs(i), abs(j)))
print(m)


(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)


(n, k, *a) = map(int, open(0).read().split())
m = INF = float('inf')
for i in range(n - k + 1):
	(l, r) = (a[i], a[i + k - 1])
	c = abs(r - l)
	m = min(m, min(abs(l) + c, abs(r) + c))
print(m)


(N, K) = map(int, input().split())
X = [int(x) for x in input().split()]
ans = 10 ** 10
for i in range(N - K + 1):
	tmp = X[i + K - 1] - X[i] + min(abs(X[i + K - 1]), abs(X[i]))
	if tmp < ans:
		ans = tmp
print(ans)


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


(n, k) = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
import math
import sys
sys.setrecursionlimit(10 ** 9)

def binary_research(start, end, function):
	if start == end:
		return start
	middle = math.ceil((start + end) / 2)
	if function(middle):
		start = middle
	else:
		end = middle - 1
	return binary_research(start, end, function)
positive = []
negative = []
zero = False
for i in x:
	if i < 0:
		negative.append(abs(i))
	elif i > 0:
		positive.append(i)
	else:
		zero = True
positive.sort()
negative.sort()

def function(time):
	if time == -1:
		return True
	ans_list = []
	ans = 0
	if zero:
		ans += 1
	for i in positive:
		if i <= time:
			ans_list.append(i)
			ans += 1
		else:
			break
	negative_list = []
	for i in negative:
		if i <= time:
			negative_list.append(i)
		else:
			break
	negative_list = negative_list[::-1]
	newans = ans
	while negative_list:
		(newans, ans_list, negative_list) = function2(ans_list, negative_list, newans, time)
		ans = max(ans, newans)
		if ans_list == []:
			break
		else:
			ans_list.pop()
			newans -= 1
	if ans >= k:
		return False
	return True

def function2(ans_list, negative_list, ans, time):
	if ans_list == []:
		max_num = 0
	else:
		max_num = ans_list[-1]
	remain_time = time - max_num * 2
	while negative_list:
		i = negative_list.pop()
		if max_num == 0:
			if i <= remain_time:
				ans += 1
		elif max_num + i + min(i, max_num) <= time:
			ans += 1
		else:
			negative_list.append(i)
			break
	return (ans, ans_list, negative_list)
print(binary_research(-1, 3 * 10 ** 8 + 1, function) + 1)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
neg_num = n
for i in range(n):
	if x[i] > 0:
		neg_num = i
		break
pos_num = n - neg_num
l = min(k, neg_num)
res = 10 ** 13
for i in range(l, -1, -1):
	r = k - i
	if r <= pos_num:
		l_val = min(0, x[neg_num - i])
		r_val = max(0, x[neg_num + k - i - 1])
		tmp = min(-l_val * 2 + r_val, -l_val + r_val * 2)
		res = min(tmp, res)
print(res)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
ans = 1000000000.0
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)


(N, K) = map(int, input().split())
x = [int(i) for i in input().split()]
cost = []
for i in range(N - K + 1):
	length = abs(x[i] - x[i + K - 1])
	cost.append(length + abs(x[i]))
	cost.append(length + abs(x[i + K - 1]))
print(min(cost))


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


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
tmp = []
for (l, r) in zip(X, X[K - 1:]):
	tmp.append(r - l + min(abs(l), abs(r)))
print(min(tmp))


(n, k) = list(map(int, input().split()))
x0 = list(map(int, input().split()))
x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')
if count1 == 0:
	print(x1[k - 1])
elif count0 == 0:
	print(-x2[k - 1])
else:
	if 0 in x1:
		x1.remove(0)
		k -= 1
		count0 -= 1
	for i in range(min(count0, k)):
		if count1 >= k - i - 1:
			ans = min(ans, x1[i] * 2 - x2[k - i - 2])
	for i in range(min(count1, k)):
		if count0 >= k - i - 1:
			ans = min(ans, -x2[i] * 2 + x1[k - i - 2])
	print(ans)


(n, k) = map(int, input().split())
c = list(map(int, input().split()))
ans = 10 ** 100
for i in range(n - k + 1):
	(l, r) = (i, i + k - 1)
	d = min(abs(c[l]), abs(c[r])) + abs(c[l] - c[r])
	if d < ans:
		ans = d
print(ans)


(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
import bisect
c = bisect.bisect_left(x, 0)
mt = 2 * 10 ** 8
for i in range(n - k + 1):
	if i + k < c:
		continue
	mt = min(mt, 2 * abs(x[i]) + abs(x[i + k - 1]), abs(x[i]) + 2 * abs(x[i + k - 1]))
	if i + k - 1 == c - 1:
		mt = min(mt, abs(x[i]))
	if i == c:
		mt = min(mt, x[i + k - 1])
if n == k:
	if c != n and c != 0:
		mt = min(2 * abs(x[0]) + x[-1], 2 * x[-1] + abs(x[0]))
	else:
		mt = max(abs(x[0]), abs(x[-1]))
print(mt)


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:]))))
main()


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


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


import sys
from decimal import Decimal
import math
from numba import njit, i8, u1, b1
import bisect
from itertools import combinations, product
import numpy as np
from collections import Counter, deque, defaultdict
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.141592653589793

def read_str():
	return sys.stdin.readline().strip()

def read_int():
	return int(sys.stdin.readline().strip())

def read_ints():
	return map(int, sys.stdin.readline().strip().split())

def read_str_list():
	return list(sys.stdin.readline().strip().split())

def read_int_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def lcm(a: int, b: int) -> int:
	return a * b // math.gcd(a, b)

def Main():
	(n, k) = read_ints()
	x = read_int_list()
	ans = INF
	for i in range(n - k + 1):
		left = x[i]
		right = x[i + k - 1]
		if left < 0 and right > 0:
			ans = min(ans, min(abs(left), abs(right)) * 2 + max(abs(left), abs(right)))
		else:
			ans = min(ans, max(abs(left), abs(right)))
	print(ans)

def __starting_point():
	Main()
__starting_point()


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


from sys import stdin

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

def main(n, k, x):
	idx = k - 1
	ans = 0
	if x[0] >= 0:
		ans = x[idx]
	elif x[idx] <= 0:
		ans = abs(x[0])
	else:
		ans = min(abs(x[0]), abs(x[idx])) * 2 + max(abs(x[0]), abs(x[idx]))
	for i in range(k, n):
		temp = 0
		if x[i - k + 1] >= 0:
			temp = x[i]
		elif x[i] <= 0:
			temp = abs(x[i - k + 1])
		else:
			temp = min(abs(x[i]), abs(x[i - k + 1])) * 2 + max(abs(x[i]), abs(x[i - k + 1]))
		ans = min(ans, temp)
	print(ans)

def __starting_point():
	(n, k, x) = getval()
	main(n, k, x)
__starting_point()


(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
inf = 10 ** 12
z = len(x)
if n == 1 and k == 1:
	print(0)
	return
for i in range(len(x)):
	if x[i] >= 0:
		z = i
		break
r = [inf] * (n - z)
l = [inf] * z
for i in range(n - z):
	r[i] = x[z + i]
for i in range(z):
	l[i] = abs(x[z - i - 1])
tl = inf
if len(l) < k:
	tl = inf
else:
	tl = abs(l[k - 1])
if z == n - 1:
	print(tl)
	return
tr = inf
if len(r) < k:
	tr = inf
else:
	tr = abs(r[k - 1])
t_arr = []
for i in range(1, k):
	if i - 1 >= len(r):
		pass
	elif k - i - 1 >= len(l):
		pass
	else:
		t1 = r[i - 1]
		t2 = l[k - i - 1]
		t_arr.append(t1 * 2 + t2 * 2 - max(t1, t2))
tc = inf
if len(t_arr) != 0:
	tc = min(t_arr)
else:
	tc = inf
print(min(tl, tr, tc))


(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
a = [x[i] for i in range(n) if x[i] < 0]
b = [x[i] for i in range(n) if x[i] >= 0]
na = len(a)
nb = len(b)
if 0 in x:
	k -= 1
	b = [x[i] for i in range(n) if x[i] > 0]
	nb = nb - 1
if k == 0:
	print(0)
	return
ans = 10 ** 10
if na >= k:
	ans = -a[-k]
if nb >= k:
	ans = min(ans, b[k - 1])
if nb == 0:
	print(-a[-k])
	return
if na == 0:
	print(b[k - 1])
	return
for i in range(1, nb + 1):
	m = k - i
	if m > na or m < 0:
		continue
	elif 0 < m <= na:
		ans = min(ans, min(b[i - 1] - 2 * a[-m], 2 * b[i - 1] - a[-m]))
	elif m == 0:
		ans = min(ans, b[k - 1])
print(ans if k != 0 else 0)


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


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


(n, k) = list(map(int, input().split()))
x = list(map(int, input().split()))
mt = 5 * 10 ** 8
for i in range(n - k + 1):
	mt = min(mt, 2 * abs(x[i]) + abs(x[i + k - 1]), abs(x[i]) + 2 * abs(x[i + k - 1]))
if x[0] > 0:
	mt = min(mt, x[k - 1])
elif x[-1] < 0:
	mt = min(mt, abs(x[-1 - k + 1]))
print(mt)


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


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
if lp == 0:
	print(-xn[K - 1])
	return
if ln == 0:
	print(xp[K - 1])
	return
pc = min(K, lp)
pp = xp[pc - 1]
if K > lp:
	nc = K - lp
	np = -xn[nc - 1]
else:
	nc = 0
	np = 0
ans = pp + np + min(pp, np)
for i in range(pc - 1):
	if nc == ln:
		break
	pp = xp[pc - i - 2]
	np = -xn[nc]
	nc += 1
	ans = min(ans, pp + np + min(pp, np))
print(ans)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
c = 10 ** 9
for i in range(n - k + 1):
	h = x[i]
	m = x[i + k - 1]
	H = abs(h)
	M = abs(m)
	if h * m < 0:
		c = min(c, 2 * H + M, 2 * M + H)
	else:
		c = min(c, max(M, H))
print(c)


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


(n, K) = (int(i) for i in input().strip().split(' '))
x = [int(i) for i in input().strip().split(' ')]
ans = min((b - a + min(abs(a), abs(b)) for (a, b) in zip(x, x[K - 1:])))
print(ans)


(n, k, *l) = map(int, open(0).read().split())
print(min(((max(abs(i), abs(j)), min(-i * 2 + j, -i + 2 * j))[i * j < 0] for (i, j) in zip(l, l[k - 1:]))))


(N, K) = map(int, input().split())
x = [int(i) for i in input().split()]
key = []
for (left, right) in zip(x, x[K - 1:]):
	key.append(right - left + min(abs(left), abs(right)))
print(min(key))


(N, K) = list(map(int, input().split()))
D = [int(n) for n in input().split()]
ans = 10000000000
for i in range(N - K + 1):
	L = D[i]
	R = D[i + K - 1]
	LR = abs(L) + abs(L - R)
	RL = abs(R) + abs(R - L)
	ans = min(ans, min(LR, RL))
print(ans)


import bisect
import math
(N, K) = map(int, input().split())
X = list(map(int, input().split()))
a = bisect.bisect_left(X, 0)
X.insert(a, 0)
ans = math.inf
for i in range(max(0, a + K - N), min(a + 1, K + 1)):
	ans = min(ans, -X[a - i] * 2 + X[a + K - i], -X[a - i] + X[a + K - i] * 2)
print(ans)


(n, k, *a) = map(int, open(0).read().split())
ans = 100000000000.0
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)


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


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)


(n, k) = list(map(int, input().split()))
xl = list(map(int, input().split()))
if 0 in xl:
	k -= 1
else:
	xl.append(0)
	xl.sort()
if k == 0:
	ans = 0
else:
	ans = 10 ** 9
	zeropoint = xl.index(0)
	endpoint = len(xl) - 1
	startpoint = max(0, zeropoint - k)
	cur = startpoint
	while cur + k <= endpoint and cur <= zeropoint:
		if cur < zeropoint < cur + k:
			tempdis = abs(xl[cur] - xl[cur + k])
			tempdis += min(abs(xl[cur]), xl[cur + k])
			ans = min(ans, tempdis)
		else:
			ans = min(ans, abs(xl[cur] - xl[cur + k]))
		cur += 1
print(ans)


(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)


import sys
(n, k) = map(int, input().split())
x = list(map(int, input().split()))
left = []
right = []
origin = 0
for u in x:
	if u < 0:
		left.append(abs(u))
	elif u == 0:
		origin += 1
	else:
		right.append(u)
left.sort()
k -= origin
if k == 0:
	print(0)
	return
r = len(right)
l = len(left)
ans = float('inf')
if k <= r:
	ans = right[k - 1]
if k <= l:
	ans = min(ans, left[k - 1])
for i in range(k - 1):
	if i < r and k - i - 2 < l:
		y = right[i] + left[k - i - 2] + min(right[i], left[k - i - 2])
		ans = min(ans, y)
print(ans)


(n, k) = map(int, input().split())
list_X = list(map(int, input().split()))
ans = pow(10, 14)
for i in range(n - k + 1):
	if list_X[i] * list_X[i + k - 1] < 0:
		ans = min(ans, list_X[i + k - 1] - list_X[i] + min(abs(list_X[i]), abs(list_X[i + k - 1])))
	else:
		ans = min(ans, max(abs(list_X[i + k - 1]), abs(list_X[i])))
print(ans)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
(pos, nega) = ([], [])
for v in x:
	if v >= 0:
		pos.append(v)
	else:
		nega.append(-v)
nega.sort()
ans = 10 ** 9
if k <= len(pos):
	ans = min(ans, pos[k - 1])
if k <= len(nega):
	ans = min(ans, nega[k - 1])
for i in range(k):
	if i < len(pos) and 0 <= k - i - 2 < len(nega):
		ans = min(ans, 2 * pos[i] + nega[k - i - 2])
	if i < len(nega) and 0 <= k - i - 2 < len(pos):
		ans = min(ans, 2 * nega[i] + pos[k - i - 2])
print(ans)


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
ans = []
for i in range(N - K + 1):
	if x[i] < 0 and x[i + K - 1] > 0:
		ans += (abs(x[i]) + abs(x[i + K - 1]) + min(abs(x[i]), abs(x[i + K - 1])),)
	elif x[i] < 0:
		ans += (-x[i],)
	else:
		ans += (x[i + K - 1],)
print(min(ans))


(N, K) = map(int, input().split())
x = [int(i) for i in input().split()]
ls = []
for (x, y) in zip(x[:N - K + 1], x[K - 1:]):
	ls.append(min(abs(x) + abs(x - y), abs(y) + abs(x - y)))
print(min(ls))


import numpy as np
(N, K) = list(map(int, input().split()))
X = np.array(input().split(), dtype=np.int64)
INF = 10 ** 18
neg = np.concatenate([-X[X <= 0][::-1], np.full(N + 1, INF, dtype=np.int64)])[:K]
pos = np.concatenate([X[X > 0], np.full(N + 1, INF, dtype=np.int64)])[:K]
answer = INF
answer = min(answer, neg[-1])
answer = min(answer, pos[-1])
if K >= 2:
	answer = min(answer, (neg[:K - 1] * 2 + pos[K - 2::-1]).min())
	answer = min(answer, (pos[:K - 1] * 2 + neg[K - 2::-1]).min())
print(answer)


import sys
(n, k) = list(map(int, input().split()))
x_ls = list(map(int, input().split()))
x_ls.sort()
if x_ls[0] >= 0:
	print(x_ls[k - 1])
	return
elif x_ls[-1] <= 0:
	x_ls.sort(reverse=True)
	print(abs(x_ls[k - 1]))
	return
l = 0
r = k - 1
ans = float('inf')
while r < n:
	cost_1 = 2 * abs(x_ls[l]) + abs(x_ls[r])
	cost_2 = abs(x_ls[l]) + 2 * abs(x_ls[r])
	cost = min(cost_1, cost_2)
	ans = min(cost, ans)
	r += 1
	l += 1
print(ans)


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


def solve(N, K, x):
	l = [abs(xi) for xi in x if xi < 0] + [0]
	l.reverse()
	r = [0] + [xi for xi in x if xi >= 0]
	ans = 10 ** 18 * 2 + 1
	for i in range(max(K - (len(l) - 1), 0), min(K + 1, len(r))):
		ll = l[K - i]
		rr = r[i]
		ans = min(ans, min(ll, rr) * 2 + max(ll, rr))
	print(ans)

def __starting_point():
	(N, K) = list(map(int, input().split()))
	x = [int(i) for i in input().split()]
	solve(N, K, x)
__starting_point()


(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))


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


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
ans = 10 ** 10
nega = sorted((-x for x in X if x < 0))
posi = sorted((x for x in X if 0 < x))
if 0 in X:
	K -= 1
if 0 < K <= len(posi):
	t = posi[K - 1]
	ans = min(ans, t)
if 0 < K <= len(nega):
	t = nega[K - 1]
	ans = min(ans, t)
if 0 < K:
	for np in range(1, len(posi) + 1):
		if 0 <= K - np - 1 < len(nega):
			t = posi[np - 1] * 2 + nega[K - np - 1]
			ans = min(ans, t)
	for nn in range(1, len(nega) + 1):
		if 0 <= K - nn - 1 < len(posi):
			t = nega[nn - 1] * 2 + posi[K - nn - 1]
			ans = min(ans, t)
else:
	ans = 0
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = float('inf')
for i in range(0, n - k + 1):
	b = a[i]
	c = a[i + k - 1]
	if c <= 0:
		ans = min(ans, abs(b))
	elif b >= 0:
		ans = min(ans, abs(c))
	else:
		ans = min(ans, abs(c) + abs(b) * 2, abs(c) * 2 + abs(b))
print(ans)


(n, k, *x) = list(map(int, open(0).read().split()))
ans = 10 ** 18
neg = []
pos = [0]
for i in x:
	if i < 0:
		neg.append(i)
	else:
		pos.append(i)
neg.append(0)
k += 1
c = len(neg)
c -= k - min(k, len(pos))
k = min(k, len(pos))
while c > 0 and k > 0:
	c -= 1
	k -= 1
	ans = min(ans, -neg[c] * 2 + pos[k], -neg[c] + pos[k] * 2)
print(ans)


(N, K) = map(int, input().split())
X = tuple(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))))


def main():
	(N, K) = [int(n) for n in input().split(' ')]
	X = [int(x) for x in input().split(' ')]
	X.sort()
	minus = []
	plus = []
	for i in range(len(X)):
		if X[i] < 0:
			minus.append(-X[i])
		else:
			plus += X[i:]
			break
	minus.reverse()
	T = []
	if len(plus) >= K:
		T.append(plus[K - 1])
	if len(minus) >= K:
		T.append(minus[K - 1])
	for i in range(K):
		if i < len(plus) and 0 <= K - i - 2 < len(minus):
			T.append(2 * plus[i] + minus[K - i - 2])
			T.append(2 * minus[K - i - 2] + plus[i])
		if i < len(minus) and 0 <= K - i - 2 < len(plus):
			T.append(2 * minus[i] + plus[K - i - 2])
			T.append(2 * plus[K - i - 2] + minus[i])
	print(min(T))
main()


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


(N, K) = map(int, input().split())
(*X,) = map(int, input().split())
A = []
B = []
for x in X:
	if x >= 0:
		A.append(x)
	else:
		B.append(abs(x))
B.sort()
i = min(K, len(A))
j = K - i
ans = []
while i >= 0 and j <= len(B):
	if i == 0:
		ans.append(B[j - 1])
	elif j == 0:
		ans.append(A[i - 1])
	else:
		(a, b) = (A[i - 1], B[j - 1])
		ans.append(a + b + min(a, b))
	i -= 1
	j += 1
print(min(ans))


(N, K) = map(int, input().split())
cans = list(map(int, input().split()))
print(min([min(abs(cans[n]), abs(cans[n + (K - 1)])) + abs(cans[n + (K - 1)] - cans[n]) for n in range(N - K + 1)]))


(n, m) = map(int, input().split())
lis = list(map(int, input().split()))
res = 10 ** 9
for i in range(n - m + 1):
	res = min(res, lis[i + m - 1] - lis[i] + min(abs(lis[i]), abs(lis[i + m - 1])))
print(res)


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


import bisect
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
x = bisect.bisect_left(a, 0)
X = n - x
if a[0] >= 0:
	print(a[k - 1])
	return
elif a[-1] <= 0:
	print(-1 * a[-1 - k + 1])
	return
if X >= k:
	c = 0
	for i in range(x, n):
		c += 1
		if c == k:
			ans = a[i]
			j = i
			r = x
			break
	while j > x and r > 0:
		j -= 1
		r -= 1
		ans = min(ans, a[j] * 2 - a[r])
else:
	c = 0
	ans = 2 * a[-1]
	for i in range(x - 1, -1, -1):
		c += 1
		if c == k - X:
			ans += -1 * a[i]
			j = n - 1
			r = i
			break
	while j > x and r > 0:
		j -= 1
		r -= 1
		ans = min(ans, a[j] * 2 - a[r])
if n - X >= k:
	c = 0
	for i in range(x - 1, -1, -1):
		c += 1
		if c == k:
			ans2 = -1 * a[i]
			j = i
			r = x - 1
			break
	while j < x - 1 and r < n - 1:
		j += 1
		r += 1
		ans2 = min(ans2, -2 * a[j] + a[r])
else:
	c = 0
	ans2 = -2 * a[0]
	for i in range(x, n):
		c += 1
		if c == k - (n - X):
			ans2 += a[i]
			j = 0
			r = i
			break
	while j < x - 1 and r < n - 1:
		j += 1
		r += 1
		ans2 = min(ans2, -2 * a[j] + a[r])
print(min(ans, ans2))


(N, K) = map(int, input().split())
x = list(map(int, input().split()))
tmin = 1000000000
for i in range(N - K + 1):
	a = x[i + K - 1]
	b = x[i]
	res = a - b + min([abs(a), abs(b)])
	if tmin > res:
		tmin = res
print(tmin)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
ans = 10 ** 18
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)


(N, K) = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
ans = 10 ** 20
for i in range(N - K + 1):
	ans = min(ans, min(abs(x[i]) + abs(x[i + K - 1] - x[i]), abs(x[i + K - 1]) + abs(x[i] - x[i + K - 1])))
print(ans)


(n, k) = map(int, input().split())
xs = list(map(int, input().split()))
m = float('inf')
for (l, r) in zip(xs, xs[k - 1:]):
	d = min(abs(l), abs(r)) + abs(r - l)
	m = min(m, d)
print(m)


(N, K) = list(map(int, input().split()))
x = list(map(int, input().split()))
positive = []
negative = []
for i in range(N):
	if x[i] < 0:
		negative.append(-x[i])
	else:
		positive.append(x[i])
negative.sort()
n = len(negative)
m = len(positive)
ans = 1000000000
if n >= K:
	ans = min(ans, negative[K - 1])
if m >= K:
	ans = min(ans, positive[K - 1])
for i in range(n):
	j = K - (i + 1)
	if j > m:
		continue
	if j <= 0:
		continue
	ans = min(ans, negative[i] + positive[j - 1] + min(negative[i], positive[j - 1]))
print(ans)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
ans = 1e+20
for i in range(n - k + 1):
	r = i + k - 1
	p1 = abs(l[i]) + abs(l[r] - l[i])
	p2 = abs(l[r]) + abs(l[i] - l[r])
	ans = min(ans, p1, p2)
print(ans)


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


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
fst = 0
las = k - 1
dis = []
while las < n:
	dis.append(x[las] - x[fst] + min(abs(x[las]), abs(x[fst])))
	fst += 1
	las += 1
print(min(dis))


from collections import deque
(N, K) = map(int, input().strip().split())
x = list(map(int, input().strip().split()))
inf = 10 ** 9
l = inf
dp = deque(x[N - K:N])
i = 0
while True:
	if dp[0] >= 0:
		tmp = dp[K - 1]
	elif dp[K - 1] <= 0:
		tmp = -dp[0]
	elif abs(dp[K - 1]) >= abs(dp[0]):
		tmp = abs(dp[0]) * 2 + abs(dp[K - 1])
	else:
		tmp = abs(dp[K - 1]) * 2 + abs(dp[0])
	l = min(l, tmp)
	i += 1
	if N - K - i < 0:
		break
	else:
		dp.appendleft(x[N - K - i])
		dp.pop()
print(l)


def resolve():
	(N, K) = [int(item) for item in input().split()]
	xs = [int(item) for item in input().split()]
	min_lr = 10 ** 9
	min_rl = 10 ** 9
	if N != K:
		for i in range(N - K + 1):
			min_lr = min(min_lr, abs(xs[i]) + abs(xs[i + K - 1] - xs[i]))
			min_rl = min(min_rl, abs(xs[i + K - 1]) + abs(xs[i + K - 1] - xs[i]))
		print(min(min_lr, min_rl))
	elif N == 1:
		print(abs(xs[0]))
	else:
		print(min(abs(xs[0]) + abs(xs[N - 1] - xs[0]), abs(xs[N - 1]) + abs(xs[N - 1] - xs[0])))

def __starting_point():
	resolve()
__starting_point()


(N, K) = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 11
for i in range(N - K + 1):
	ans = min(ans, abs(a[i]) + a[i + K - 1] - a[i], abs(a[i + K - 1]) + a[i + K - 1] - a[i])
print(ans)


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
ans = 1000000000.0
for (l, r) in zip(X, X[K - 1:]):
	ans = min(ans, r - l + min(abs(l), abs(r)))
print(ans)


(N, K) = map(int, input().split())
x = [int(i) for i in input().split()]
cost = 10 ** 13
for i in range(N - K + 1):
	c = x[i + K - 1] - x[i] + min(abs(x[i]), abs(x[i + K - 1]))
	cost = min(cost, c)
print(cost)


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


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


import sys
input = sys.stdin.readline

def main():
	(N, K) = list(map(int, input().split()))
	X = list(map(int, input().split()))
	X.sort()
	ans = float('inf')
	for l in range(N - K + 1):
		r = l + K - 1
		l2r = abs(X[l]) + abs(X[l] - X[r])
		r2l = abs(X[r]) + abs(X[r] - X[l])
		ans = min(ans, l2r, r2l)
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
X = list(map(int, input().split()))
if N == 1:
	print(0)
	exit()
ans = float('Inf')
for i in range(N - K + 1):
	ans = min(ans, X[i + K - 1] - X[i] + min(abs(X[i + K - 1]), abs(X[i])))
print(ans)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
neg_to_pos = n
for i in range(n):
	if x[i] > 0:
		neg_to_pos = i
		break
res = 10 ** 14
for i in range(n - k + 1):
	l = min(x[i], 0)
	r = max(0, x[i + k - 1])
	if i <= neg_to_pos and neg_to_pos - 1 <= i + k - 1:
		tmp = min(-2 * l + r, -l + 2 * r)
		res = min(res, tmp)
print(res)


(N, K) = list(map(int, input().split()))
X = sorted(list(map(int, input().split())))
cnt = []
for i in range(N - K + 1):
	left = X[i]
	right = X[i + K - 1]
	cnt.append(min(abs(right) + abs(left - right), abs(left) + abs(right - left)))
print(min(cnt))


(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)


import sys
input = sys.stdin.readline

def main():
	(N, K) = list(map(int, input().split()))
	X = list(map(int, input().split()))
	INF = float('inf')
	M = [INF] * N
	P = [INF] * N
	(i_m, i_p) = (0, 0)
	for x in X:
		if x == 0:
			K -= 1
		elif x > 0:
			P[i_p] = x
			i_p += 1
		else:
			M[i_m] = -x
			i_m += 1
	if K <= 0:
		print(0)
		return
	M.sort()
	P.sort()
	ans = min(M[K - 1], P[K - 1])
	for n_M in range(1, K + 1):
		dist = 2 * M[n_M - 1] + P[K - n_M - 1]
		if ans > dist:
			ans = dist
	for n_P in range(1, K + 1):
		dist = 2 * P[n_P - 1] + M[K - n_P - 1]
		if ans > dist:
			ans = dist
	print(ans)

def __starting_point():
	main()
__starting_point()