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):
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    if 0 < xn[0]:
        print(xn[K - 1])
        return

    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return

    ans = MAX

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

    print(ans)
    return


main()

Solution (Python):
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    if 0 <= xn[0]:
        print(xn[K - 1])
        return

    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return

    ans = MAX

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

    print(ans)
    return


main()

Solution (Python):
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    if 0 <= xn[0]:
        print(xn[K - 1])
        return

    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return

    ans = MAX

    for i in range(N - K + 1):
        if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 < xn[i + K]:
            ans = min(ans, abs(xn[i]))

        elif 0 <= xn[i + K - 1]:
            ans = min(ans,
                      2 * abs(xn[i]) + abs(xn[i + K - 1]),
                      abs(xn[i]) + 2 * abs(xn[i + K - 1]))

    print(ans)
    return


main()

Solution (Python):
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    if 0 <= xn[0]:
        print(xn[K - 1])
        return

    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return

    ans = MAX

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] < 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 < xn[i + K]:
                ans = min(ans, abs(xn[i]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

    print(ans)
    return


main()

Solution (Python):
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    if 0 <= xn[0]:
        print(xn[K - 1])
        return

    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return

    ans = MAX

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] < 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 < xn[i + K]:
                ans = min(ans, abs(xn[i]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

        elif 0 != i and xn[i - 1] < 0:
            ans = min(ans, xn[i + K - 1])

    print(ans)
    return


main()

Solution (Python):
N, K = map(int, input().split())
xs = list(map(int, input().split()))
ans = 1e20
for i in range(N-K+1):
    xl = xs[i]
    xr = xs[i+K-1]
    ltor = abs(xl) + abs(xl-xr)
    rtol = abs(xr) + abs(xl-xr)
    t = min(ltor, rtol)
    if t < ans:
        ans = t
print(ans)

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

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

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

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

Solution (Python):
def candles(N: int, K: int, X: int)->int:
    min_d = float('inf')
    for i in range(N-K+1):
        xl, xr = X[i], X[i+K-1]
        min_d = min(min_d, abs(xl) + abs(xr - xl), abs(xr) + abs(xr - xl))

    return min_d


if __name__ == "__main__":
    N, K = map(int, input().split())
    X = [int(s) for s in input().split()]
    ans = candles(N, K, X)
    print(ans)

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

res = 1e9+7
for l, r in zip(x,x[K-1:]):
  tmp = r - l
  if(abs(l) < abs(r)):
    tmp += abs(l)
  else:
    tmp += abs(r)
  if(res > tmp):
    res = tmp
print(res)

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

Solution (Python):
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
t = 2 * (x[-1] - x[0])
i = bisect.bisect_right(x, 0)
for j in range(0, N):
    if N <= j + K -1 :
        break
    if (j - (i - 0.1)) * (j + K- 1 - (i - 0.1)) >= 0:
        t = min(t, max(abs(x[j]), abs(x[j+K-1])))
    else:
        t = min(t, abs(2 * x[j]) + x[j+K-1])
        t = min(t, abs(x[j]) + 2 * x[j+K-1])
print(t)

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

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 numpy as np

N,K=list(map(int,input().split()))
x=list(map(int,input().split()))
t_min=10**9+7
for i in range(N-K+1):
    t=x[i+K-1]-x[i]+min(np.abs(x[i]),np.abs(x[i+K-1]))
    if t<t_min:
        t_min=t
print(t_min)

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

ans = 10**18
for i in range(n-k+1):
    x_s, x_e = x[i], x[i+k-1]
    if x_e <= 0:
        ans_tmp = -x_s
    elif x_s < 0 and 0 < x_e:
        ans_tmp = min(x_e-2*x_s, 2*x_e-x_s)
    else:
        ans_tmp = x_e
    ans = min(ans, ans_tmp)
print(ans)

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

Solution (Python):


















import math
import numpy as np
import copy
from collections import defaultdict, Counter
from itertools import product
from bisect import bisect_left, bisect_right
def inpl(): return list(map(int, input().split()))


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

all_time = []
for ind in range(N-K+1):
    l = x[ind]
    r = x[ind+K-1]
    all_time.append(min(abs(l)+abs(r-l), abs(r)+abs(l-r)))

print(min(all_time))

Solution (Python):
n,k=map(int,input().split())
lst1=input()
lst2=lst1.split(" ")
lst=[]
for i in lst2:
    lst.append(int(i))

minr=[]
minl=[]

for i in range(n-k+1):
    l=i
    r=i+k-1
    minr.append(abs(lst[r])+abs(lst[r]-lst[l]))
    minl.append(abs(lst[l])+abs(lst[r]-lst[l]))

minr.sort()
minl.sort()

m=min(minr[0],minl[0])
print(m)

Solution (Python):
import math
import sys

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

mn = sys.maxsize

for i in range(n-k+1):
	mn = min(mn, abs(x[i]-x[i+k-1])+abs(x[i+k-1]))
for i in range(n-1, k-2, -1):
	mn = min(mn, abs(x[i]-x[i-k+1])+abs(x[i-k+1]))

print(mn)

Solution (Python):
import bisect

def solve():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    if K == 0:
        return 0
    if 0 in x:
        x.remove(0)
        K -= 1
    if K == 0:
        return 0

    x.sort()

    mid = bisect.bisect_left(x, 0)
    p = [0]
    m = [0]
    for i in range(mid, N):
        p.append(x[i])
    for i in range(mid - 1, -1, -1):
        m.append(-x[i])
    pcnt = len(p) - 1
    mcnt = len(m) - 1
    inf = 1 << 60
    min_sc = inf
    for i in range(K+1):
        if mcnt < K - i:
            continue
        if pcnt < i:
            continue
        if m[K - i] > p[i]:
            sc = m[K - i] + p[i] * 2
        else:
            sc = m[K - i] * 2 + p[i]
        min_sc = min(min_sc, sc)
    return min_sc

ans = solve()
print(ans)

Solution (Python):


import itertools
import collections
import bisect

def main():
    
    INF = int(1e15)

    N, K = map(int, input().split())
    X = [-INF] + list(map(int, input().split())) + [INF]
    pos = bisect.bisect(X, 0) 
    

    right_num = N - pos + 1
    left_num = pos - 1

    ans = INF
    if right_num >= K:  
        ans = min(ans, abs(X[pos + K - 1]))
    

    for i in range(1, K):  
        if i > left_num:  
            continue
        if K - i > right_num:  
            continue

        left_distance = abs(X[pos - i])
        right_distance = abs(X[pos + K - i - 1])
        
        ans = min(ans, left_distance * 2 + right_distance, right_distance * 2 + left_distance)

    if left_num >= K:  
        ans = min(ans, abs(X[pos - K]))
    

    print(ans)


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import bisect

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

if 0 in X:
  K=K-1
ans=[]
Right=[]
Left=[]
  
for x in X:
  if x>0:
    Right.append(x)
  else:
    Left.append(abs(x))

Left.append(0)
Left.sort()
Right=[0]+Right
    
for i in range(K+1):
  if i<=len(Right)-1 and K-i<=len(Left)-1:
    ans.append(min(Right[i],Left[K-i])*2+max(Right[i],Left[K-i]))

if K==0:
  ans.append(0)

print(min(ans))

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

Solution (Python):


"""
Created on 2019/3/1
Solved on 2019/3/  WA
@author: shinjisu
"""



def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**12


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def probC_WA():
    N, K = getIntList()
    X = getIntList()
    dmp((N, K, X))
    X = [-INF] + X + [INF]
    dmp(X)
    for i in range(N+2):
        if X[i] >= 0:
            break
    zeroIdx = i
    dmp(zeroIdx, 'zeroIdx')
    fromIdx = max(1, zeroIdx-K+1)
    dmp(fromIdx, 'fromIdx')
    
    minTime = INF
    
    
    for i in range(fromIdx, N+2-K):
        if X[i] >= 0:
            moveTime = X[i+K-1]
        elif X[i+K-1] <= 0:
            moveTime = abs(X[i])
        else:
            plDist = X[i+K-1]
            mnDist = abs(X[i])
            moveTime = min(plDist, mnDist)*2 + max(plDist, mnDist)
        dmp((i, minTime, moveTime), 'moveTime in loop')
        minTime = min(minTime, moveTime)
    return minTime


def probC():
    N, K = getIntList()
    X = getIntList()
    dmp((N, K, X))
    
    
    
    for i in range(N):
        if X[i] >= 0:
            break
    zeroIdx = i
    dmp(zeroIdx, 'zeroIdx')
    fromIdx = max(0, zeroIdx-K+1)
    dmp(fromIdx, 'fromIdx')
    
    minTime = INF
    
    
    for i in range(fromIdx, N-K+1):
        if X[i] >= 0:
            moveTime = X[i+K-1]
        elif X[i+K-1] <= 0:
            moveTime = abs(X[i])
        else:
            plDist = X[i+K-1]
            mnDist = abs(X[i])
            moveTime = min(plDist, mnDist)*2 + max(plDist, mnDist)
        dmp((i, minTime, moveTime), 'moveTime in loop')
        minTime = min(minTime, moveTime)
    return minTime


debug = False  
ans = probC()
print(ans)

Solution (Python):
def solve(string):
    n, k, *x = map(int, string.split())
    x = sorted(x)
    diff = [
        _x1 - _x0 + min(abs(_x0), abs(_x1)) if _x0 <= 0 <= _x1 else max(abs(_x0), abs(_x1))
        for _x0, _x1 in zip(x, x[k - 1:])
    ]
    return str(min(diff))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

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

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

time = []

for i in range(N-K+1):
  t = min(abs(lst[i]) + abs(lst[i] - lst[i + K - 1]),abs(lst[i + K -1]) + abs(lst[i] - lst[i + K -1]))
  time.append(t)
                           
print(min(time))

Solution (Python):

import sys


def solve(N: int, K: int, x: "List[int]"):
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            right_end = x[i+window_size-1]
            left_end = x[i]
            dist = abs(right_end-left_end)
            costs.append(min(abs(right_end)+dist, abs(left_end)+dist))
        return min(costs)
    print(search(N, K, x))
    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 math
import sys

import math
import sys


n,k = map(int, sys.stdin.readline().rstrip().split())
xs = list(map(int, sys.stdin.readline().rstrip().split()))

mn = sys.maxsize
for i in range(n-k+1):
  mn = min(mn, abs(xs[i]-xs[i+k-1])+abs(xs[i+k-1]))

for i in range(n-1,k-2,-1):
  mn = min(mn, abs(xs[i]-xs[i-k+1])+abs(xs[i-k+1]))
  
print(mn)

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
mintime = 10**20
for i in range(n-k+1):
    left = x[i]
    right = x[i+k-1]
    abss = (abs(left), abs(right))
    if left * right >= 0:
        mintime = min(mintime, max(abss))
    else:
        mintime = min(mintime, min(abss) * 2 + max(abss))
print(mintime)

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

min_cost = float('inf')

for i in range(N-K+1):
    l = X[i]
    r = X[i+K-1]
    tmp_l = abs(l) + abs(r-l)
    tmp_r = abs(r) + abs(l-r)
    tmp = min(tmp_l,tmp_r)

    if min_cost > tmp:
        min_cost = tmp

print(min_cost)

Solution (Python):
n,k = map(int,input().split())
locations = list(map(int,input().split()))
tmp = 10 ** 18

for left in range(n-k+1):
    right = left + k - 1
    if locations[left] * locations[right] > 0:
        answer = max(abs(locations[left]),abs(locations[right]))
    else:
        answer1 = abs(locations[left]) * 2 + locations[right]
        answer2 = locations[right]* 2 + abs(locations[left])
        answer = min(answer1,answer2)
    tmp = min(tmp,answer)
    
print(tmp)

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

l = 0
r = 0

for li in reversed(range(0, n)):
    if x[li] < 0:
        l += 1
        if l == k:
            break

for ri in range(0, n):
    if x[ri] > 0:
        r += 1
        if r == k:
            break
    
c = 10**9

def t(a, b):
    if a * b < 0:
        return min(abs(a) * 2 + abs(b), abs(a) + abs(b) * 2)
    else:
        return max(abs(a), abs(b))

for i in range(li, ri + 2 - k):
    ci = t(x[i], x[i + k - 1])
    
    if ci < c:
        c = ci
            
print(c)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False



dp = None


def main():
    N, K = li_input()
    A = li_input()
    D = []

    for i in range(N - K + 1):
        l,r = A[i],A[i+K-1]
        D.append(min(abs(l) + abs(r - l), abs(r) + abs(r - l)))

    print(min(D))


main()

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False



dp = None


def main():
    N, K = li_input()
    A = li_input()

    Ps, Ng = [], []

    for a in A:
        if a > 0:
            Ps.append(a)
        else:
            Ng.append((-1) * a)

    Ps.sort()
    Ng.sort()

    ans = 10**20

    
    if len(Ps):
        for turn in range(K-len(Ng)-1, min(K, len(Ps))):
            dist = Ps[turn]

            if turn != K - 1:
                dist += Ps[turn] + Ng[K - turn - 2]

            if dist < ans:
                ans = dist

    
    if len(Ng):
        for turn in range(K-len(Ps)-1, min(K, len(Ng))):
            dist = Ng[turn]

            if turn != K - 1:
                dist += Ng[turn] + Ps[K - turn - 2]

            if dist < ans:
                ans = dist

    print(ans)





main()

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

N = int(N)
K = int(K)

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

cache = 99999999999
for a, b in zip(xList, xList[K-1:]):
    if min(a,b) < 0 and 0 <= max(a,b):
        if (max(a,b) - min(a,b) + min(abs(a),abs(b))) < cache:
            cache = (max(a,b) - min(a,b) + min(abs(a),abs(b)))
    elif a < 0 and b < 0:
        if abs(a) < cache:
            cache = abs(a)
    else:
        if abs(b) < cache:
            cache = abs(b)

print(cache)

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

L = N-K+1
Y = [10**9]*(L)
for i in range(L):
    a = X[i]
    b = X[i+K-1]
    if a*b<0:
        Y[i] = 2*min(abs(a), abs(b)) + max(abs(a), abs(b))
    else:
        Y[i] = max(abs(a), abs(b))

print(min(Y))

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
time=[]
zero=0
if 0 in X:
    zero=X.index(0)
    for i in range(N):
        if zero+i<N and zero-K+1+i>=0:
            time.append(min(abs(X[zero-K+1+i]),abs(X[zero+i]))+abs(X[zero-K+1+i])+abs(X[zero+i]))
else:
    g=1
    for i in range(N-1):
        if X[i]<0 and X[i+1]>0:
            g=0
            zero=i
            break
    if g==1:
        if X[0]<0:
            time.append(-X[-K])
        else:
            time.append(X[K-1])
        exit
    for i in range(N):
        if zero+i<N and zero-K+1+i>=0:
            time.append(min(abs(X[zero-K+1+i]),abs(X[zero+i]))+abs(X[zero-K+1+i])+abs(X[zero+i]))
print(min(time))

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

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

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

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())



x,y= (int(i) for i in input().split())      


a = [int(i) for i in input().split()]        







s = []
if x == 1:
    print(a[0])
    exit(0)
if x == y:
    c = a[0]
    d = a[x-1]
    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d <= 0:
        s.append(abs(c))
    else: s.append(d)
for i in range(x-y+1):
    c = a[i]
    d = a[i+y-1]
    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d <= 0:
        s.append(abs(c))
    else: s.append(d)
print(min(s))

Solution (Python):
import sys

def main():
  input = sys.stdin.readline
  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]
    if L * R < 0:
      t = min(abs(L) + (R - L), R + (R - L))
    else:
      t = max(abs(L), abs(R))
    ans = min(ans, t)
  
  print(ans)


if __name__ == '__main__':
  main()

Solution (Python):

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

Solution (Python):
n=list(map(int,input().split()))
Y=list(map(int,input().split()))
P=[i for i in Y if i>=0]
M=[i*-1 for i in Y if i<0]

if P==[]:
    P.append(max(M)*1000)
if M==[]:
    M.append(max(P)*1000)
p=sorted(P)
m=sorted(M)
L1=[]
L2=[]

for i in range(n[1]):
    if n[1]-i-2<len(m) and i<len(p):
        L1.append(p[i]+m[n[1]-i-2]*2)
        L2.append(p[i]*2+m[n[1]-i-2])
if len(p)>=n[1]:
    L1.append(p[n[1]-1])
if len(m)>=n[1]:
    L2.append(m[n[1]-1])
print(min(min(L1),min(L2)))

Solution (Python):
n=list(map(int,input().split()))
Y=list(map(int,input().split()))
P=[i for i in Y if i>=0]
M=[i*-1 for i in Y if i<0]
if P==[]:
    P.append(max(M)*1000)
if M==[]:
    M.append(max(P)*1000)
p=sorted(P)
m=sorted(M)
L1=[]
L2=[]
for i in range(n[1]):
    if n[1]-i-2<len(m) and i<len(p):
        L1.append(p[i]+m[n[1]-i-2]*2)
        L2.append(p[i]*2+m[n[1]-i-2])
if len(p)>=n[1]:
    L1.append(p[n[1]-1])
if len(m)>=n[1]:
    L2.append(m[n[1]-1])
print(min(min(L1),min(L2)))

Solution (Python):
N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
try :
    dis.append(Lright[K-1])
    dis.append(Lleft[K-1])
except:
    pass


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    try :
        d += Lleft[remain-1]
        dis.append(d)
    except:
        pass
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    try:
        d += Lright[remain-1]
        dis.append(d)
    except:
        pass
print(min(dis))

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

Solution (Python):
n,k=map(int,input().split())
X=list(map(int,input().split()))
cost=float('inf')
for i in range(n-k+1):
    l=X[i]
    r=X[i+k-1]
    if l<=0:
        if r<=0:
            tmp=-l
        else:
            tmp_l=-l*2+r
            tmp_r=2*r-l
            tmp=min(tmp_l,tmp_r)
    else:
        tmp=r
    cost=min(cost,tmp)
print(cost)