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()))
time = 10**9
x_k = [0, 0]
for i in range(n - k + 1):
    x_k[0] =  x[i] 
    x_k[1] =  x[i+k-1] 
    
    
    if x_k[0] >= 0:
        t = x_k[1]
            
    elif x_k[1] <= 0:
        t = -x_k[0]
    
    else:
        t = min(-x_k[0] + x_k[1] *2,-x_k[0] * 2 + x_k[1])

    time = min(t, time)
    
print(time)

Solution (Python):
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
x = list(map(int, input().split()))
min_d = 10 ** 9
for i in range(n - k + 1):
    max_i = x[i + k - 1]
    min_i = x[i]
    if max_i <= 0:
        min_d_i = - min_i
    elif min_i >= 0:
        min_d_i = max_i
    else:
        min_d_i = min(- min_i + 2 * max_i, -2 * min_i + max_i)
    if min_d_i < min_d:
            min_d = min_d_i
print(min_d)

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

result = []
for i in range(N-K+1):
    s,g = i,i+K-1
    result.append(abs(x[s]) + abs(x[s] - x[g]))
    result.append(abs(x[g]) + abs(x[g] - x[s]))
result.sort()
print(result[0])

Solution (Python):
N, K = [int(item) for item in input().split()]
candles = [int(item) for item in input().split()]
ans = []
for i in range(N-K+1):
  l, r = i, i+K-1
  ans.append(abs(candles[l]) + abs(candles[l] - candles[r]))
  ans.append(abs(candles[r]) + abs(candles[r] - candles[l]))
ans.sort()
print(ans[0])

Solution (Python):
N,K = map(int,input().split(" "))
cdn = list(map(int,input().split(" ")))
n = N - K + 1
neko = 0
ans = []
i = 0
while i < n:
  if abs(cdn[i]) >= abs(cdn[i+K-1]):
    neko = abs(cdn[i+K-1]) + cdn[i+K-1] - cdn[i]
    ans.append(neko)
  else:
     neko = abs(cdn[i]) + cdn[i+K-1] - cdn[i]
     ans.append(neko)
  i = i + 1
ans.sort()
print(ans[0])

Solution (Python):
N, K = [int(_) for _ in input().split()]
xs = [int(_) for _ in input().split()]
L = []
R = [0]
for i in range(N):
    if xs[i] < 0:
        L += [-xs[i]]
    elif xs[i] > 0:
        R += [xs[i]]
    else:
        K -= 1
L = [0] + L[::-1]

ans = 10 ** 9
for i in range(0, len(L)):
    if 0 <= K - i and K - i < len(R):
        ans = min(ans, 2 * L[i] + R[K - i], L[i] + 2 * R[K - i])
print(ans)

Solution (Python):
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
x = list(map(int, input().split()))
min_d = 10 ** 9
for i in range(n - k + 1):
    max_i = x[i + k - 1]
    min_i = x[i]
    if max_i <= 0:
        min_d_i = - min_i
    elif min_i >= 0:
        min_d_i = max_i
    else:
        min_d_i = min(- min_i + 2 * max_i, -2 * min_i + max_i)
    if min_d_i < min_d:
            min_d = min_d_i
print(min_d)

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

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

min_time = 2 * abs(candles[0] - candles[-1])

for i in range(N):
    end_index = i + K - 1
    if end_index >= N:
        break
    left_end = candles[i]
    right_end = candles[end_index]
    d = abs(right_end - left_end)

    time = min(abs(left_end) + d, abs(right_end) + d)
    min_time = min(time, min_time)

print(min_time)

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

Solution (Python):
iN,iK = [int(_) for _ in input().split()]
aX = [int(_) for _ in input().split()]
ans = 10**9
iDiff = iN-iK
for i in range(iDiff + 1):
    iL = aX[i]
    iR = aX[iN-iDiff+i-1]
    ans = min(ans ,abs(iL) + abs(iL-iR),abs(iR) + abs(iR-iL))
print(ans)

Solution (Python):


iN,iK = [int(_) for _ in input().split()]
aX = [int(_) for _ in input().split()]
aP = [_ for _ in aX if  _ >= 0]
aM = [-1*_ for _ in aX if  _ < 0][::-1]
iP = len(aP)
iM = len(aM)
if iP == 0:
    print(aM[iK-1])
    exit()
elif iM == 0:
    print(aP[iK-1])
    exit()
iD = 2*aP[-1]+2*aM[-1]
for i in range(min(iP-1,iK-1),-1,-1):
    if iK - 2 -i < 0:
        iD = min(iD,aP[i])
    elif iM > iK -2 - i :
        iD = min(iD,aP[i] + 2*aM[iK-i-2])
for i in range(min(iM-1,iK-1),-1,-1):
    if iK - 2 - i < 0:
        iD = min(iD,aM[i])
    elif iP > iK -2 - i :
        iD =  min(iD,aM[i] + 2*aP[iK-i-2])
print(iD)

Solution (Python):
N,K = map(int,input().split())
L = list(map(int,input().split()))
ANS = []
for i in range(len(L)-K+1) :
    p = L[i]
    q = L[i+K-1]
    if p*q == 0 :
        r = abs(p-q)
    elif p*q < 0 :
        r = 2*min(abs(p),q) + max(abs(p),q)
    else :
        if p > 0 :
            r = q
        else :
            r = abs(p)
    ANS.append(r)
ans = min(ANS)
print(ans)

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

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


for i in range(n):
    if l[i] >= 0:
        ind = i

left = min(n-1,i+k)

ans = 1e10

while left >= k-1:
    if l[left]*l[left-k+1]>=0:
        d = max(abs(l[left]),abs(l[left-k+1]))
    else:
        a = abs(l[left])
        b = abs(l[left-k+1])
        d = min(2*a+b,a+2*b)
    if d < ans:
        ans=d
    left -=1

print(ans)

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

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

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

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

sum = 10 ** 8 * 3 + 1
for i in range(n - k + 1):
    cur = arr[i + k - 1] - arr[i] + min(abs(arr[i + k - 1]), abs(arr[i]))
    if sum > cur:
        sum = cur
print sum

Solution (Python):
global x
def get(left,right):
    if(x[left]*x[right]<0):
        return min([2*abs(x[left]) +x[right], 2*abs(x[right]) + abs(x[left])])

    else:
        return max([abs(x[left]),x[right]])


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


tmp_min=get(0,k-1)
for i in range(1,n):
    if(i+k-1 >=n):
        break
    tmp_min=min([tmp_min ,get(i,i+k-1)])
    if(x[i]>=0):
        break
    

print(tmp_min)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

    n, p = [], []
    for x in sorted(X):
        if x == 0:
            K -= 1
        elif x < 0:
            n.append(x)
        else:
            p.append(x)

    ans = INF
    n = n[::-1]
    for i in range(K + 1):
        j = K - i
        if i <= len(n) and j <= len(p):
            a, b = 0, 0
            if i > 0:
                a = abs(n[i - 1])
            if j > 0:
                b = abs(p[j - 1])
            ans = min(ans, a + b + min(a, b))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
x = [int(i) for i in input().split()]
y = list(map(abs,x))
xl=0
xr=0
loop=n-k+1
ans=100000000000

for i in range(loop):
    if(x[i]*x[i+k-1]>=0):
        tmp=max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
    else:
        tmp=2*min(y[i],y[i+k-1])+max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
print(ans)

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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

mn = 0xFFFFFFFF
for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    a = abs(l) + abs(r - l)
    b = abs(r) + abs(r - l)
    mn = min(a, b, mn)
print(mn)

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

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

print(ans)

Solution (Python):
N,K=(int(i) for i in input().split(' '))
x=[int(i) for i in input().split(' ')]
time=None
index=0
l=len(x)
while True:
  if x[index]==0:
    K-=1
    x.remove(index)
  elif x[index]>0:
    break
  index+=1
  if index==l:
    break
x.insert(index,0)
if K==0:
  time=0
for i in range(max(0,K-len(x)+index+1),min(K+1,index+1)):
  if -x[index-i]>x[index+K-i]:
    time_tmp=-x[index-i]+2*x[index+K-i]
  else:
    time_tmp=-2*x[index-i]+x[index+K-i]
  if time==None:
    time=time_tmp
  if time_tmp<time:
    time=time_tmp
print(time)

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float('inf')
for i in range(N-K+1):
    l,r = 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)

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
 
ans = 3 * (10 ** 8) + 1
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)

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

ans = 10000000000000000
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)

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

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

Solution (Python):
ANS=[]
N,K=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
for i in range(N-K+1):
    a=x[i]
    b=x[i+K-1]
    A=abs(a)+abs(a-b)
    B=abs(b)+abs(b-a)
    c=min(A,B)
    ANS.append(c)
print(min(ANS))

Solution (Python):
import bisect


def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 99999999999
    for i in range(n-k+1):
        s = 0
        if left >= i+k-1:
            s = b[i]
        elif right <= i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())

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

shortest = float("inf")
for i in range(N - K + 1):
    left = x[i]
    right = x[i + K - 1]
    left_first = abs(left) + abs(left - right)
    right_first = abs(right) + abs(left - right)
    shortest = min(shortest, left_first, right_first)
print(shortest)

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

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

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

ans = float('inf')

for i in range(n - k + 1):
	if arr[i] * arr[k + i -1] >= 0:
		tmp = max(abs(arr[i]), abs(arr[k + i -1]))
	else:
		tmp = abs(arr[i]) + abs(arr[k + i -1]) + min(abs(arr[i]), abs(arr[k + i -1]))
	ans = min(ans, tmp)

print(ans)

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

m = np.sum(np.array(a) < 0) - 1

cs = list(np.abs(a))
result = []
for s, e in zip(range(0, n - k + 1), range(k - 1, n + 1)):
    if(s <= m and e > m):
        left = cs[s]
        right = cs[e]

        result.append(left * 2 + right)
        result.append(left + right * 2)
    elif s <= m:
        result.append(cs[s])
    else:
        result.append(cs[e])
print(min(result))

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

Solution (Python):

def inpl(): return [int(i) for i in input().split()]

n,k = inpl()
C =inpl()

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

print(ans)

Solution (Python):
R=[]
N,K=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
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+K-1]-x[i])
    c=min(a,b)
    R.append(c)
print(min(R))

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
  a.remove(0)
  n -= 1
  k -= 1
if k == 0:
  print(0)
  exit()
m = 300000000
for i in range(n+1-k):
  if a[i+k-1] < 0:
    x = -a[i]
  elif a[i] > 0:
    x = a[i+k-1]
  else:
    x = (a[i+k-1]-a[i]) + min(-a[i],a[i+k-1])
  m = min(m,x)
print(m)

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

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

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

for i in range(N - K + 1):
    l = i
    r = i + K - 1

    Xl, Xr = X[l], X[r]
    if i == 0:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr))
    else:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr) , ans)


print(ans)

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

Solution (Python):
N, K = list(map(int, input().split()))
candles = list(map(int, input().split()))
abs_c = [abs(c) for c in candles]
result = []
for n in range(N-K+1):
    r1 = abs(candles[n]) + abs(candles[n+K-1]-candles[n])
    r2 = abs(candles[n+K-1]) + abs(candles[n]-candles[n+K-1])
    result.append(min(r1, r2))

print(min(result))

Solution (Python):

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

shortest = float("inf")
for i in range(N - K + 1):
    left = x[i]
    right = x[i + K - 1]
    left_first = abs(left) + abs(left - right)
    right_first =  abs(right) + abs(left - right)
    shortest = min(shortest, left_first, right_first)
print(shortest)

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

Solution (Python):



if __name__ == "__main__":
    N,K = map(int,input().split(" "))
    place = list(map(int,input().split(" ")))
    time = []
    for i in range(N-K+1):
        left = place[i]
        right = place[i+K-1]
        n1 = abs(left) + abs(right - left)
        n2 = abs(right) + abs(left - right)
        time.append(min(n1,n2))
    print(min(time))

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

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

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

ans = 10 ** 9
for i in range(n - k + 1):
    left = i
    right = i + k - 1
    if x[right] <= 0:
        ans = min(ans, abs(x[left]))
    elif 0 <= x[left]:
        ans = min(ans, abs(x[right]))
    else:
        diff = abs(x[right] - x[left])
        ans = min(ans, diff + abs(x[right]), diff + abs(x[left])) 

print(ans)

Solution (Python):
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
          (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

for i in range(1, len(x) - K + 1):
    left = i
    right = i + K - 1

    letf_to_right = abs(x[left]) + abs(x[right] - x[left])
    right_to_left = abs(x[right]) + abs(x[left] - x[right])

    tmp = min(letf_to_right, right_to_left)

    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):
def solution(a, k):
    n = len(a)
    p, q = [0], []
    for x in a:
        if x < 0:
            q.append(-x)
        else:
            p.append(x)
    q = [0] + q[::-1]
    foo = min(p[-1], q[-1]) * 2 + max(p[-1], q[-1])
    for x in xrange(max(0, k - len(p) + 1), min(k, len(q) - 1) + 1):
        y = k - x
        foo = min(min(q[x], p[y]) * 2 + max(q[x], p[y]), foo)
    return foo

def main():
    n, k = [int(i) for i in raw_input().strip().split()]
    a = [int(i) for i in raw_input().strip().split()]
    print(solution(a, k))

if __name__ == '__main__':
    main()

Solution (Python):
N,K=map(int,input().split())
x=list(map(int,input().split()))
T=10**9
for i in range(N-K+1):
    xr=x[i]
    xl=x[i+K-1]
    d1=abs(xr)+xl-xr
    d2=abs(xl)+xl-xr
    if T>d1:
        T=d1
    if T>d2:
        T=d2   
print(T)

Solution (Python):
def binarySearch(list_a, key):
    left = 0
    right = len(list_a)
    while left < right:
        mid = (left + right) // 2
        if list_a[mid] == key:
            return mid
            break
        elif key < list_a[mid]:
            right = mid
        else:
            left = mid+1
    else:
        return mid

n,k = map(int, input().split())
list_x = list((map(int, input().split())))
ans = 10**9
key_point = binarySearch(list_x,0)
for i in range(key_point-k,key_point+1):
    if i < 0:
        continue
    try:
        dist = min(abs(list_x[i]),abs(list_x[i+k-1])) + (list_x[i+k-1] - list_x[i])
        if ans > dist:
            ans = dist
    except:
        pass
print(ans)

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

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

if N == 1:
    min_d = abs(x[0])
else:
    for i ,x_i in enumerate(x[:-(K-1)]):
        d = min(abs(x_i), abs(x[i+(K-1)])) + abs(x_i - x[i+(K-1)])
        if d < min_d:
            min_d = d

print(min_d)

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

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

l = int(1e9)
idx = 0
for i in range(n-k+1):
    if x[i] < 0 and x[i+k-1] > 0:
        lx = x[i+k-1] - x[i] + min(abs(x[i]), x[i+k-1])
    else:
        lx = max(abs(x[i]), x[i+k-1])
    if l >= lx:
        l = lx
        idx = i

print(l)

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


res = min(min(abs(l), abs(r)) + r - l for l, r in zip(cp, cp[K-1:]))

print(res)

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

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

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

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

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

Solution (Python):
N, K = map(int, input().split(" "))
Xs = [int(a) for a in input().split()]
 
 
min = 3*(10**8)
 
p=0
z=0
m=0
Y=[]
for x in Xs:
    if x > 0:
        p += 1
        Y.append(x)
    elif x < 0:
        m += 1
    else:
        z +=1
 
    
K -= z
Z=[]
for i in range(m-1,-1,-1):
    Z.append(-Xs[i])
    
 
if K == 0:
    min = 0
if K-1 < p and K-1 >= 0:
    if Y[K-1] < min:
        min = Y[K-1]
 
if K-1 < m and K-1 >= 0:
    if Z[K-1] < min:
        min = Z[K-1]
 
for i in range(p):
    if i < K-1:
        if K-i-2 < m:
            if Y[i] * 2 + Z[K-i-2] < min:
                min = Y[i] * 2 + Z[K-i-2]
    
for i in range(m):
    if i < K-1:
        if K-i-2 < p:
            if Z[i] * 2 + Y[K-i-2] < min:
                min = Z[i] * 2 + Y[K-i-2]
 
               
print (min)

Solution (Python):
n,k = map(int, raw_input().split())
l = map(int, raw_input().split())
mn = 10**9
for i in range(n-k+1):
    if l[i] * l[i+k-1] < 0:
        p = max(abs(l[i]), abs(l[i+k-1])) + 2 * min(abs(l[i]), abs(l[i+k-1]))
    else:
        p = max(abs(l[i]), abs(l[i+k-1]))
    mn = min(mn, p)
print mn

Solution (Python):

import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))












if not (0 in ls):
    ls.append(0)
    k+=1


ls = np.array(ls)
ls.sort()


idx = np.where(ls==0)[0][0]


start = max(0,idx-k)
goal = min(idx+k,len(ls)-1)


answer = []
for i in range(goal-(k-1)+1):
    
    mi = min(abs(ls[i]),abs(ls[i+k-1]))
    ma = max(abs(ls[i]),abs(ls[i+k-1]))
    
    answer.append(2*mi+ma)
print (min(answer))

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

m = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))
print(m)

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

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

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

Solution (Python):
"""
C - Candles
X ろうそく間の距離を出して、K本目まで距離を足せば良い
O 右にいくか　左に行くか　どっちが良いか
"""

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

candles = []
for part in input().split():
    candles.append(int(part))

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

    
    r_dist = abs(candles[r]) + abs(candles[l] - candles[r])
    
    l_dist = abs(candles[l]) + abs(candles[r] - candles[l])

    min_dist = min(min_dist, r_dist, l_dist)

print(min_dist)

Solution (Python):
def nl(): return list(map(int, input().split()))

n, k = nl()
x = nl()

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

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

score = []
for i in range(n-k+1):
    l = abs(x[i]) + (x[i+k-1] - x[i])
    r = abs(x[i+k-1]) + (x[i+k-1] - x[i])
    score.append(l)
    score.append(r)
score.sort()
print(score[0])

Solution (Python):
N, k = [ int(i) for i in input().split()]
cPos = [ int(i) for i in input().split()]
time = 10**10
for i in range(N-k+1):
    if time > min( abs(cPos[i])+ abs(cPos[i]- cPos[i+k-1]), abs(cPos[i+k-1])+ abs(cPos[i]- cPos[i+k-1])):
        time = min( abs(cPos[i])+ abs(cPos[i]- cPos[i+k-1]), abs(cPos[i+k-1])+ abs(cPos[i]- cPos[i+k-1]))
print(time)

Solution (Python):
import bisect

n,k = map(int, input().split())
x = list(map(int, input().split()))
j = bisect.bisect_right(x,0)
if x[-1] > 0:
    j += 1
ans = float("INF")
for i in range(max(j-1-k,0), max(min(j,n-k+1),0)):
    key = x[i+k-1]-x[i]
    ans = min(ans, min(abs(x[i])+key, abs(x[i+k-1])+key))
print(ans)

Solution (Python):
import sys

if __name__ == '__main__' :
    
    N,K = map(int, input().split())
    x = list(map(int, input().split()))

    min_dis = sys.maxsize
    for i in range(0,N-K+1):
        l = i
        r = i + K - 1 
        
        dis_l = abs(x[l]) + abs(x[r]-x[l])
        
        dis_r = abs(x[r]) + abs(x[r]-x[l])
        min_dis = min(min_dis,dis_l,dis_r)
    print(min_dis)

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

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

ans = 999999999

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

Solution (Python):
N,K=map(int,input().split())
A = list(map(int, input().split()))
i = 0
ans = 1000000000
while i <= N-K:
  d1 = abs(A[i])+(A[i+K-1]-A[i])
  d2 = abs(A[i+K-1])+(A[i+K-1]-A[i])
  ans = min(ans,d1,d2)
  i += 1
print(ans)

Solution (Python):
from collections import deque

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

x_upper = []

for i in reversed(x):
    if i<0:
        break
    if i>=0:
        x_upper.append(i)
        x.pop()
        

x.reverse()
x_upper.reverse()

lenXu = len(x_upper)
lenX = len(x)

if K>lenXu:
    l = K-lenXu-1
    h = lenXu-1
else:
    l = -1
    h = K-1

S = []
while l<K and l<lenX:
    if h<0:
        S.append(x[l]*(-1))
    elif l<0:
        S.append(x_upper[h])
    else:
        if (x[l]*(-1))<x_upper[h]:
            S.append((x[l]*(-1))*2 + x_upper[h])
        else:
            S.append((x[l]*(-1)) + x_upper[h]*2)
    l += 1
    h -= 1

print(min(S))

Solution (Python):


n,k=map(int,raw_input().split())
x=map(int,raw_input().split())
res=float('inf')
for i in xrange(n-k+1):
 l=x[i]
 r=x[i+k-1]
 res=min(res,abs(l)+abs(r-l),abs(r)+abs(l-r))
print res

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

sorted_X = sorted(X, key=abs)

centers = []

if sorted_X[0] == 0:
    
    centers.append(X.index(0))
else:
    for x in sorted_X:
        if x > 0:
            centers.append(X.index(x))
            break

    for x in sorted_X:
        if x < 0:
            centers.append(X.index(x))
            break



lefts = []

for center_index in centers:
    left_edge = center_index - (K - 1)
    if left_edge >= 0:
        start_left_edge_index = left_edge
        start_right_edge_index = center_index
    else:
        start_left_edge_index = 0
        start_right_edge_index = K-1

    for move in range(K):
        left_edge = start_left_edge_index + move
        right_edge = start_right_edge_index + move
        if right_edge >= N:
            break
        
        lefts.append(left_edge)
lefts = list(set(lefts))

start_left = min(lefts)
search_num = len(lefts)

pos_max = 0
neg_max = 0

propose = []
for i in range(start_left, start_left + K):
    target = X[i]
    if target > 0:
        if pos_max < target:
            pos_max = target

    if target < 0:
        if neg_max < abs(target):
            neg_max = abs(target)

propose.append(pos_max + 2 * neg_max)
propose.append(neg_max + 2 * pos_max)


for slide in range(search_num-1):
    start_left += 1
    left = start_left
    right = start_left + K - 1

    left_target = X[left]
    if left_target < 0:
        neg_max = abs(left_target)
    elif target == 0:
        neg_max = 0

    right_target = X[right]
    if right_target > 0:
        pos_max = right_target
    elif right_target == 0:
        pos_max = 0

    propose.append(pos_max + 2 * neg_max)
    propose.append(neg_max + 2 * pos_max)


print(min(propose))

Solution (Python):
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
mintime = 1e9
for i in range(N-K+1):
    time1 = abs(x[i])+x[i+K-1]-x[i]
    time2 = abs(x[i+K-1])+x[i+K-1]-x[i]
    time = min([time1,time2])
    if time < mintime:
        mintime = time
print(mintime)

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