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 = list(map(int,input().split()))

split_idx = None
for i,x in enumerate(x_list):
    if x >= 0:
        split_idx = i
        break
if split_idx == None:
    lefts = x_list
    rights = []
else:
    lefts = x_list[:split_idx]
    rights = x_list[split_idx:]

if len(rights)>0:
    if rights[0]==0:
        K -= 1
        rights = rights[1:]

if K == 0:
    print(0)
else:
    min_time = 10**9
    for left_cnt in range(K+1):
        right_cnt = K-left_cnt
        if left_cnt > len(lefts) or right_cnt > len(rights):
            continue
        if left_cnt ==0 and right_cnt == 0:
            tmp_time = 0
        elif left_cnt == 0:
            tmp_time = rights[right_cnt-1]
        elif right_cnt == 0:
            tmp_time = -lefts[-left_cnt]
        elif -lefts[-left_cnt] >= rights[right_cnt-1]:
            tmp_time = rights[right_cnt-1]*2 + (-lefts[-left_cnt])
        else:
            tmp_time = rights[right_cnt-1] + (-lefts[-left_cnt]) * 2
        if tmp_time < min_time:
            min_time = tmp_time
    print(min_time)

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

ans = float("inf")
for i in range(n-k+1):
    if i != 0:
        if l[i-1]>=0:
            continue
    if i != n-k:
        if l[i+k]<=0:
            continue

    if l[i]>=0:
        now = l[i+k-1]
    elif l[i+k-1]<=0:
        now = abs(l[i])
    else:
        now = min(abs(l[i])*2+l[i+k-1], abs(l[i])+l[i+k-1]*2)

    ans = min(ans, now)

print(ans)

Solution (Python):
from functools import reduce
import math

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    N, K = (int(_) for _ in input().split())
    x = list(int(_) for _ in input().split())
    ans = INF
    for i in range(N-K+1):
        l = i
        r = i + K-1
        ans_l = abs(x[l]) + abs(x[l]-x[r])
        ans_r = abs(x[r]) + abs(x[l]-x[r])
        ans = min(ans_l,ans_r,ans)
        
    print(ans)
    
if __name__ == '__main__':
    main()

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

Solution (Python):

import sys, math, fractions, itertools


def solve(N: int, K: int, x: "List[int]"):
    mn = 1e9
    for i in range(N-K+1):
        n = abs(x[i])
        a = abs(x[i+K-1])
        if x[i+K-1] * x[i] < 0:
            mn = min([mn, 2*a+n, 2*n+a])
        else:
            mn = min([mn, max(a, n)])
    print(mn)
    return



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) ]  
    solve(N, K, x)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

ans = sys.maxsize

for i in range(N-K+1):
    minNum = candles[i]
    maxNum = candles[i+K-1]
    l = abs(minNum) + abs(minNum - maxNum)
    r = abs(maxNum) + abs(maxNum - minNum)
    ans = min(ans, min(l, r))

print(ans)

Solution (Python):
import bisect

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

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""
ans = 10**100


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

if idx == n:
    ans = -x[-k]

print(ans)

Solution (Python):

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

for i in range(n-k+1):
    l = lst[i]
    r = lst[i+k-1]
    way1 = abs(l) + abs(r-l)
    way2 = abs(r) + abs(l-r)
    m = min(way1,way2)
    if i==0:
        ans = m
    elif m < ans:
        ans = m
    
print(ans)

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

Solution (Python):
n,k=map(int,input().split())
x=list(map(int,input().split()))
min_len=1e9
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)

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

time = []
for i in range(n - k + 1):
    l, r = x[i], x[i + k - 1]
    if l * r < 0:
        time.append(min(abs(l), r) + r - l)
    else:
        time.append(max(abs(l), abs(r)))
print(min(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 = abs(x[i]) + abs(x[i+k-1] - x[i])
    r = abs(x[i+k-1]) + abs(x[i+k-1] - x[i])

    if l < r:
        ans = min(ans, l)
    else:
        ans = min(ans, r)

print(ans)

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

lengthlist=[]

for i in range(N-K+1):
  
  if x[i+K-1]*x[i]>=0:
    lengthlist.append(min(abs(x[i+K-1]),abs(x[i]))+abs(x[i+K-1]-x[i]))
  elif abs(x[i+K-1])>abs(x[i]):
    lengthlist.append(abs(x[i+K-1])+abs(x[i])*2)
  else:
    lengthlist.append(abs(x[i+K-1])*2+abs(x[i]))


print(min(lengthlist))

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

def fire(a,b):
    if a * b >= 0:
        r = max(abs(a),abs(b))
    else:
        r = abs(a) + abs(b) + min(abs(a),abs(b))
    return r

ans = 10**14
for i in range(n-k+1):
    ans = min(fire(coordinate_list[i],coordinate_list[i+k-1]),ans)
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):
    temp_ans1 = abs(x[i]) + abs(x[i+K-1]-x[i])
    temp_ans2 = abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    
    
    ans = min(ans,temp_ans1,temp_ans2)
    
print(ans)

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

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

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

if __name__ == "__main__":
    main()

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

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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    n, i = LI()
    print(n-i+1)
    return


def B():
    h, w = LI()
    a = SR(h)
    dy = defaultdict(int)
    dx = defaultdict(int)
    for y in range(h):
        for x in range(w):
            if a[y][x] == "
                dx[x] = 1
                dy[y] = 1
    dy = list(sorted(dy.items(), key=lambda x: x[0]))
    dx = list(sorted(dx.items(), key=lambda x: x[0]))
    for y,_ in dy:
        for x,_ in dx:
            print(a[y][x], end="")
        print()
    return


def C():
    n, k = LI()
    x = LI()
    ans = inf
    for i in range(n - k + 1):
        if x[i] * x[i + k - 1] > 0:
            ans = min(ans, max(abs(x[i]), abs(x[i + k - 1])))
            continue
        ans = min(ans, min(abs(x[i]), abs(x[i + k - 1])) * 2 + max(abs(x[i]), abs(x[i + k - 1])))
    print(ans)
    return


def D():
    return


if __name__ == '__main__':
    C()

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

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

ans = 10**10
for i in range(N-K+1):
	time = min(abs(X[i])+abs(X[i+K-1]-X[i])\
		,abs(X[i+K-1])+abs(X[i+K-1]-X[i]))
	ans = min(ans,time)

print(ans)

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

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

Solution (Python):
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
def main(): 
    N,K = map(int,input().split())
    x = list(map(int,input().split()))
    ans = float("inf")
    flag = False
    for i in range(N-K+1):
        flag = True
        l = x[i]
        r = x[K+i-1]
        p = abs(l)+abs(r-l)
        q = abs(r)+abs(r-l)
        s = min(p,q)
        if ans > s:
            ans = s
    print(ans if flag else abs(x[0]))
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)


if __name__ == "__main__":
    main()

Solution (Python):

n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k-1]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n-k+1):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam == near_zero_big:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

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

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

a = []

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


print(min(a))

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

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

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

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

minusx = []
plusx = []
for x in l:
    if x < 0:
        minusx.append(x)
    else:
        plusx.append(x)

m = len(minusx)
a = len(plusx)
res = float("inf")
if m == 0:
    res = plusx[k-1]
elif a == 0:
    res = abs(minusx[-k])
else:
    for i in range(1,k+1):
        if  i <= a and  k-i <= m:
            p = plusx[i-1]
            q = abs(minusx[-(k-i)])
            t = max(p,q)+min(p,q)*2
            if t < res:
                res = t
print(res)

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

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

List = []
for j in range(N-K+1):
    if X[j]<=0 and X[j+K-1]>=0:
        List.append(X[j+K-1]-X[j]+min(abs(X[j]),X[j+K-1]))
    else:
        List.append(max(abs(X[j]),abs(X[j+K-1])))

print(min(List))

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

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

ans = 2*(X[N-1] - X[0])
for i in range(N-K+1):
    zerodim = min(abs(X[i]), abs(X[i+K-1]))
    dis = Xs[K+i-1] - Xs[i]
    
    
    
    ans = min(dis + zerodim, 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):
    ans = min(ans, X[i+K-1] - X[i] + min(abs(X[i]), abs(X[i+K-1])))
print(ans)

Solution (Python):
N,K = map(int, input().split())
X = []
idx = 0
for i,x in enumerate(input().split()):
    x = int(x)
    X.append(x)
    if x < 0:
        idx = i + 1
if idx == N:
    idx = N-1
ans = 10**9
for i in range(max(0,idx-K), min(N-K+1,idx+K)):
    ans = min(ans, X[i+K-1] - X[i] + min(abs(X[i]), abs(X[i+K-1])))
print(ans)

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

X = []
idx = 0
for i,x in enumerate(input().split()):
    x = int(x)
    X.append(x)
    if x < 0:
        idx = i + 1
if idx == N:
    idx = N-1
ans = 10**9
for i in range(max(0,idx-K), min(N-K+1,idx+K)):
    tmp = 0
    if i <= idx <= i+K-1 or i >= idx or i+K-1 <= idx:
        tmp = min(abs(X[i]), abs(X[i+K-1]))
    
    ans = min(ans, X[i+K-1] - X[i] + tmp)
print(ans)

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

ans = 9999999999999

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

Solution (Python):
n,c=map(int,input().split())
can=list(map(int,input().split()))

res=[0]*(n-c+1)

for i in range(n-c+1):
	s=can[i]
	e=can[i+c-1]
	if s>=0 and e>=0:
		res[i]=e
	else:
		if s<=0 and e<=0:
			res[i]=-s
		else:
			if s<=0 and e>=0:
				if abs(s)>=e:
					res[i]+=-s+e*2
				else:
					res[i]+=-s*2+e
print(min(res))

Solution (Python):
n, k = map(int, input().split())
dat_x = list(map(int, input().split()))
l = 0
r = k - 1
total = 0
for i in range(1, k):
    
    
    total += abs(dat_x[i] - dat_x[i - 1])

total += min(abs(dat_x[l]), abs(dat_x[r]))

m = total



r += 1
l += 1

while r < n:
    
    
    
    total -= min(abs(dat_x[l-1]), abs(dat_x[r-1]))
    
    total -= abs(dat_x[l] - dat_x[l - 1])
    
    total += abs(dat_x[r] - dat_x[r - 1])
    
    total += min(abs(dat_x[l]), abs(dat_x[r]))

    m = min(m, total)
    r += 1
    l += 1

print(m)

Solution (Python):
n, k = map(int, input().split())
dat_x = list(map(int, input().split()))
l = 0
r = k - 1
total = 0
for i in range(1, k):
    
    
    total += abs(dat_x[i] - dat_x[i - 1])

total += min(abs(dat_x[l]), abs(dat_x[r]))

m = total



r += 1
l += 1

while r < n:
    
    
    
    total -= min(abs(dat_x[l-1]), abs(dat_x[r-1]))
    
    total -= abs(dat_x[l] - dat_x[l - 1])
    
    total += abs(dat_x[r] - dat_x[r - 1])
    
    total += min(abs(dat_x[l]), abs(dat_x[r]))

    m = min(m, total)
    r += 1
    l += 1

print(m)

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

def calc_costs(l, r):
    axl = abs(x[l])
    axr = abs(x[r])
    if 0 <= x[l] and 0 <= x[r]:
        return x[r]
    elif x[l] <= 0 and x[r] <= 0:
        return abs(x[l])
    elif axl < axr:
        return axl * 2 + axr
    else:
        return axl + axr * 2

l = 0
r = K-1
min_costs = calc_costs(l, r)
for i in range(1, N-K+1):
    l += 1
    r += 1
    costs = calc_costs(l, r)
    if costs < min_costs:
        min_costs = costs
print(min_costs)

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

Solution (Python):
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

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

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = list(map(int, sys.stdin.readline().split()))
X = list(map(int, sys.stdin.readline().split()))
ans = INF
for i in range(N - K + 1):
    L = X[i]
    r = X[i + K - 1]
    if r <= 0:
        ans = min(ans, abs(L))
    elif 0 <= L:
        ans = min(ans, abs(r))
    else:
        al = abs(L)
        ar = abs(r)
        ans = min(ans, min(al, ar) * 2 + max(al, ar))
print(ans)

Solution (Python):


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

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

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

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

Solution (Python):
N, M = list(map(int, input().split()))
data = list(map(int, input().split()))
result = 10**9
for i in range(N-M+1):
  if data[i] < 0 and data[i+M-1] >= 0:
    result = min([data[i+M-1]-data[i]*2, 
                  data[i+M-1]*2-data[i],
                  result])
  elif data[i+M-1] >= 0:
    result = min([data[i+M-1], result])
  else:
    result = min([-data[i], result])
print(result)

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

time = 10 ** 10
for i in range(N - K + 1):
    if candle[i] < 0 and candle[i + K-1] > 0:
        if abs(candle[i]) < candle[i + K-1]:
            kouho = 2 * abs(candle[i]) + candle[i + K-1]
        else:
            kouho = abs(candle[i]) + 2 * candle[i + K-1]
    else:
        kouho = max(abs(candle[i]), abs(candle[i + K-1]))

    if kouho < time:
        time = kouho
print(time)

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


def comp(k, lst):
    temp =10**9
    for i in range(N-k+1):
        temp = min(temp, min(abs(lst[i]-lst[i+k-1])+abs(lst[i]), abs(lst[i]-lst[i+k-1])+abs(lst[i+k-1])))
    return temp

print(comp(K, X))

Solution (Python):
inf = float('inf')

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

m, p = [], []
for xx in x:
    if xx > 0:
        p.append(xx)
    elif xx < 0:
        m.append(-xx)
    else:
        K -= 1
        if K == 0:
            print(0)
            exit()
m.sort()
lp = len(p)
lm = len(m)

ans = inf
for from_minus in range(min(lm, K) + 1):
    from_plus = K - from_minus
    if from_plus > lp:
        continue
    if from_minus == 0:
        ans = min(ans, p[K - 1])
    elif from_minus == K:
        ans = min(ans, m[K - 1])
    else:
        ans = min(ans, p[from_plus - 1] * 2 + m[from_minus - 1])
        ans = min(ans, m[from_minus - 1] * 2 + p[from_plus - 1])
print(ans)

Solution (Python):
from bisect import bisect_left

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

si = bisect_left(x, 0)

ans = (x[-1] - x[0]) * 2
if si + k - 1 < len(x):
    ans = min(ans, x[si + k - 1])
if si - k >= 0:
    ans = min(ans, abs(x[si - k]))

for i in range(n):
    tmp_ans = 0
    if i < si and i + k -1 < len(x):
        tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
        ans = min(ans, tmp_ans)
    elif i >= si and i - k + 1 >= 0:
        tmp_ans = x[i] * 2 + abs(x[i - k + 1])
        ans = min(ans, tmp_ans)

print(ans)

Solution (Python):
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
if 0 in x:
    x.remove(0)
    k -= 1
    n -= 1

if n == 0:
    print(0)
else:
    si = bisect_left(x, 0)

    ans = (x[-1] - x[0]) * 2
    if si + k - 1 < len(x):
        ans = min(ans, x[si + k - 1])
    if si - k >= 0:
        ans = min(ans, abs(x[si - k]))

    for i in range(n):
        tmp_ans = 0
        if i < si and i + k -1 < len(x):
            tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
            ans = min(ans, tmp_ans)
        elif i >= si and i - k + 1 >= 0:
            tmp_ans = x[i] * 2 + abs(x[i - k + 1])
            ans = min(ans, tmp_ans)

    print(ans)

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

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

if __name__ == "__main__":
    main()

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

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

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

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

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

ans = 10**9
for i in range(n-k+1):
  l = abs(li[i]) + abs(li[i]-li[i+k-1])
  r = abs(li[i+k-1]) + abs(li[i]-li[i+k-1])
  ans = min(ans,l,r)
print(ans)

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


ans = float("INF")
for i in range(N - K + 1):
    l = lst_x[i]
    r = lst_x[i + K - 1]
    dx = r - l
    d = min(abs(l) + dx, abs(r) + dx)
    ans = min(ans, d)
print(ans)

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

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

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

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

for i in range(n-k+1):
    left = i
    right = i+k-1
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)

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

for i in range(n-k+1):
    left = i
    right = i+k
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)

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

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

print(ans)

Solution (Python):
from sys import stdin
N, K = [int(x) for x in stdin.readline().rstrip().split()]
data = [int(x) for x in stdin.readline().rstrip().split()]
ans = []
for i in range(N - K + 1):
    
    ans.append(data[i+K-1] - data[i] + abs(data[i+K-1]))
    ans.append(data[i+K-1] - data[i] + abs(data[i]))
print(min(ans))

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

point= len(x) - 1
for i, v in enumerate(x):
    if v == 0:
        point = i
        break
    elif v > 0:
        point = i - 1
        break

start = max(0, point - K + 1)
end = start + K - 1

if x[point] != 0:
    point += 1
    
ans = float('inf')

while True:
    if x[start] >= 0:
        time = x[end]
    elif x[end] <= 0:
        time = abs(x[start])
    elif abs(x[start]) <= x[end]:
        time = abs(x[start] * 2) + x[end]
    else:
        time = abs(x[start]) + (x[end] * 2)
    ans = min(ans, time)
    
    start += 1
    end += 1

    if start > point or end == len(x):
        break
    
print(ans)

Solution (Python):

def main():
    n, k = tuple(map(int, input().rstrip().split()))
    x = tuple(map(int, input().rstrip().split()))
    ans = float("inf")
    for l, r in zip(x, x[k - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n, k = tuple(map(int, input().rstrip().split()))
    x = tuple(map(int, input().rstrip().split()))
    ans = float("inf")
    for i in range(n - k + 1):
        a, b = x[i], x[i + k - 1]
        if a < b < 0 or 0 < a < b:
            ans = min(ans, max(abs(a), abs(b)))
        else:
            ans = min(ans, max(abs(a), abs(b)) + min(abs(a), abs(b)) * 2)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = float("inf")
for i in range(N - K + 1):
    l = x[i]
    r = x[i + K - 1]
    ans = min(ans,min(abs(r)+abs(r-l),abs(l)+abs(l-r)))
print(ans)

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

Solution (Python):
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
ans = 10**9
for l in range(N-K+1):
  r = l + K - 1
  tmp = x[r]-x[l]
  if x[r] < 0:
    tmp += abs(x[r])
  elif x[l] < 0 and 0 <= x[r]:
    tmp += min(abs(x[l]), x[r])
  else:
    tmp += x[l]
  ans = min(ans, tmp)
print(ans)

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

    retval = float("inf")
    for i in range(0, N - (K - 1)):
        left = x[i]
        right = x[i + K - 1]
        time = max(abs(left), abs(right))
        if abs(left + right) != abs(left) + abs(right):
            time += min(abs(left), abs(right)) * 2
        retval = min(retval, time)
    print(retval)


if __name__ == "__main__":
    main()