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()))
if K==1 and 0 in X:
    print(0)
    exit()
if 0 in X:
    K-=1
left=sorted([i for i in X if i<0],reverse=True)
right=[i for i in X if i>0]

lst=set()
for l,L in enumerate(left):
    i=K-(l+1)-1
    if i>=0:
        if i<len(right):
            lst.add(right[i]-L*2)
    else:
        lst.add(-L)

for l,R in enumerate(right):
    i=K-(l+1)-1
    if i>=0:
        if i<len(left):
            lst.add(R*2-left[i])
    else:
        lst.add(R)

print(min(list(lst)))

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

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

print(ans)

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = 10**9
for x1, x2 in zip(X, X[K-1:]):
    if x2 < 0 or 0 < x1:
        ans = min(ans, max(abs(x1), abs(x2)))
    else:
        xm = min(abs(x1), abs(x2))
        ans = min(ans, xm + abs(x1) + abs(x2))
print(ans)

Solution (Python):
import sys

n, k, *x = map(int, sys.stdin.read().split())

def main():
    l = 0; r = k - 1
    ans = float('inf')
    while r < n:
        if x[r] <= 0:
            res = abs(x[l])
        elif x[l] < 0:
            res = min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r])
        else:
            res = x[r]
        ans = min(ans, res)
        l += 1; r += 1
    
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

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

L=bisect.bisect_left(X,0)
Z=1 if L<N and X[L]==0 else 0
R=N-L-Z

l=max(0,L-(K-Z))
r=l+K-1
ans=10**8*4
while r<N:
    if X[l]<0<X[r]:
        tmp=min(abs(X[l]),abs(X[r]))*2 + max(abs(X[l]),abs(X[r]))
    else:
        tmp=max(abs(X[l]),abs(X[r]))
    ans=min(ans,tmp)
    l+=1
    r+=1

print(ans)

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


def min_for_interval(left, right):
    if left < 0:
        if right < 0:
            return abs(left)
        else:
            l_r = 2 * abs(left) + abs(right)
            r_l = 2 * abs(right) + abs(left)
            return min(l_r, r_l)
    else:
        return right


min_time = 1e9
for i in range(0, n - k + 1):
    left = xs[i]
    right = xs[i + k - 1]
    min_time = min(min_for_interval(left, right), min_time)

print(min_time)

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

for i in range(n-k+1):
    if x[i] * x[i+k-1] < 0:
        t = min(a[i],a[i+k-1])*2 + max(a[i],a[i+k-1])
    else:
        t = max(a[i],a[i+k-1])
    ans = min(t, ans)

print(ans)

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

answer = []
for i in range(N):
  if i + K > N:
    break
  Ys, Yt = X[i], X[i+K-1]
  if Ys > 0:
    answer.append(Yt)
  elif Yt < 0:
    answer.append(-Ys)
  else:
    answer.append(min(Ys*(-2)+Yt, Yt*2-Ys))

print(min(answer))

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
K = gi()
xs = [0] * N

for i in range(N):
    xs[i] = gi()

ans = abs(xs[0]) + abs(xs[0] - xs[N - 1])

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

    l = xs[i]
    r = xs[i + K - 1]
    ans = min(ans, abs(r - l) + abs(r))
    ans = min(ans, abs(r - l) + abs(l))

print(ans)

Solution (Python):
def main():
    N, K, *xn = map(int, open(0).read().split())
    ans = int(1e9 + 7)
    for xl, xr in zip(xn, xn[K - 1:]):
        t = xr - xl + min(abs(xl), abs(xr))
        ans = t if t < ans else ans
    print(ans)
    return


main()

Solution (Python):
def main():
    N, K, *xn = map(int, open(0).read().split())
    ans = int(1e9 + 7)
    for xl, xr in zip(xn[:], xn[K - 1:]):
        t = min(ans, xr - xl + min(abs(xr), abs(xl)))
        ans = t if t < ans else ans
    print(ans)
    return


main()

Solution (Python):
def main():
    N, K, *xn = map(int, open(0).read().split())
    ans = int(1e9+7)
    for xl, xr in zip(xn[:], xn[K - 1:]):
        ans = min(ans, xr - xl + min(abs(xr), abs(xl)))
    print(ans)
    return


main()

Solution (Python):
MAX_D = 300000001


def main():
    N, K, *xn = map(int, open(0).read().split())
    if 0 < xn[0]:
        print(xn[K - 1])
        return
    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return
    ans = MAX_D
    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]:
            if 0 != i:
                if xn[i - 1] < 0:
                    ans = min(ans, abs(xn[i + K - 1]))
                else:
                    break
        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):
N,K = map(int,input().split())
X = list(map(int,input().split()))

ans = 10**9
for i in range(N-K+1):
    xl = X[i]
    xr = X[i+K-1]
    if xl*xr < 0: tmp = min(abs(xl),abs(xr))*2 + max(abs(xl),abs(xr))
    elif xr <= 0: tmp = abs(xl)
    else: tmp = xr
    if tmp < ans: ans = tmp
print(ans)

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

Solution (Python):
import bisect
import cmath
import heapq
import itertools
import math
import operator
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, mul, add, xor

import numpy as np

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

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


N, K = list(map(int, sys.stdin.buffer.readline().split()))
X = list(map(int, sys.stdin.buffer.readline().split()))

li = 0
ri = K - 1
ans = INF
while ri < N:
    l = X[li]
    r = X[ri]
    if r <= 0:
        a = abs(l)
        ans = min(a, ans)
    elif l >= 0:
        a = abs(r)
        ans = min(a, ans)
    else:
        a = max(abs(l), abs(r)) + min(abs(l), abs(r)) * 2
        ans = min(a, ans)
    li += 1
    ri += 1
print(ans)

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

ans = float('inf')
for i in range(N+1-K):
    if arr[i]<= 0 and arr[i+K-1]>0:
        preans = min(abs(arr[i]*2) + arr[i+K-1], abs(arr[i]) + arr[i+K-1]*2)
    elif arr[i]<=0 and arr[i+K-1]<=0:
        preans = abs(arr[i])
    elif arr[i]>=0 and arr[i+K-1]>=0:
        preans = arr[i+K-1]
    ans = min(ans,preans)

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

print(ans)

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

ans = 3*10**8 + 1
for i in range(N-K+1):
  l = A[i]
  r = A[i+K-1]
  a = abs(l) + r-l
  b = abs(r) + r-l
  ans = min(ans, min(a,b))
print(ans)

Solution (Python):
N, K = (int(i) for i in input().split())  
X = [int(i) for i in input().split()] 
ans = 10 ** 18
for i in range(N):
  if i + K > N:
    break
  l = X[i]
  r = X[i + K - 1]
  
  if l >= 0:
    ans = min(ans, r)
  elif r <= 0:
    ans = min(ans, -l)
  else:  
    if r < -l:
      ans = min(ans, r * 2 + -l)
    else:
      ans = min(ans, -l * 2 + r)
    
print(ans)

Solution (Python):
H,K = map(int,input().split())
x=list(map(int,input().split()))
m = sum([1 for i in x if i < 0])
xm = [-x[i] for i in range(m)]
xm.reverse()

if 0 in x:
  K = K-1
  xp = x[m+1:]
else:
  xp = x[m:]
xm = [0]+xm
xp = [0]+xp

if K == 0:
  ans = 0
else:
  ans = 3 * 10**8
  for i in range(min(K+1,len(xm))):
    if K-i < len(xp):
      x = xm[i]*2+xp[K-i]
      ans = min(x,ans) 
  for i in range(min(K+1,len(xp))):
    if K-i < len(xm):
      x = xp[i]*2+xm[K-i]
      ans = min(x,ans) 
print(ans)

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

ans = float('inf')

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

print(ans)

Solution (Python):
import sys
import itertools
import numpy
input = sys.stdin.readline
N,K = [int(i) for i in input().split()]
li = [int(i) for i in input().split()]
mina = 10000000001
for i in range(N-K+1) :
    ll = abs(li[i])
    lr = abs(li[i+K-1])
    if li[i] * li[i+K-1] > 0 :
        l = max(ll,lr)
    else :
        if ll >= lr :
            l = lr * 2 + ll
        else :
            l = ll * 2 + lr
    mina = min(mina,l)
print(mina)

Solution (Python):
n,k=map(int,input().split())
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi==0:
        k-=1
    else:
        x.append(xi)
x.sort()
ans=float("INF")
zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

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



time = float('inf')
for n_i in range(n-k+1):
  r_side = X[n_i+k-1]
  l_side = X[n_i]
  time = min(time, min(abs(r_side), abs(l_side)) + abs(r_side-l_side))
  
print(time)

Solution (Python):

import sys
from itertools import accumulate

def solve(N: int, K: int, x: "List[int]"):
    minus = []
    plus = []
    answer = 10**9
    for i in range(N):
        if x[i] >= 0:
            plus.append(x[i])
        else:
            minus.append(abs(x[i]))
    minus = minus[::-1]

    for plus_count in range(0,K+1):
        minus_count = K- plus_count

    
        if 0 <= plus_count <= len(plus) and 0 <= minus_count <= len(minus):
            if plus_count == 0:
                answer = min(answer,minus[minus_count-1])
            elif minus_count ==0:
                answer = min(answer,plus[plus_count-1])
            else:
                answer = min(answer,plus[plus_count-1]+minus[minus_count-1]*2,plus[plus_count-1]*2+minus[minus_count-1])   

    print(answer)    
    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 numpy as np
N, K = map(int, input().split())
x = np.asarray(list(map(int,input().split())))
ans = 10**9
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]-x[i+K-1]))
print(ans)

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

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

    right = left+K

    minus = 0
    plus = 0

    if X[left] < 0:
        minus = min(minus, X[left])
    if X[right-1] > 0:
        plus = max(plus, X[right-1])

    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)

print(ans)

Solution (Python):
N, K = map(int, input().split())
xlist = list(map(int, input().split()))
ans = []
for i in range(N - K + 1):
    left = xlist[i]
    right = xlist[i + K - 1]
    if left < 0 and right > 0 or left > 0 and right < 0:
        ans.append(min(abs(left), abs(right)) * 2 + max(abs(left), abs(right)))
    elif left == 0 or right == 0:
        ans.append(abs(left) + abs(right))
    else:
        ans.append(max(abs(left), abs(right)))
print(min(ans))

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

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

Solution (Python):

import bisect
n,k = map(int, input().split( ))
x = list(map(int, input().split( )))
inf = 10**15
x.append(inf)
start = bisect.bisect_left(x,0)

if x[start] == 0:
    k -= 1
    n -= 1
    x = [-inf]*k + x + [inf]*k
else:
    x = [-inf]*k + x[:start] + [0] + x[start:] + [inf]*k

ans = inf
start = bisect.bisect_left(x,0)
for i in range(k+1):
    tmp1 = -x[start-i]
    tmp2 = x[start+k-i]
    
    ans = min(ans, tmp1*2 + tmp2, tmp1 + tmp2*2)

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,k = inpl()
a = inpl()
res = INF
for i in range(n-k+1):
    if a[i]*a[i+k-1] >= 0:
        res = min(res, max(abs(a[i]), abs(a[i+k-1])))
    else:
        c = min(abs(a[i]), abs(a[i+k-1]))
        d = max(abs(a[i]), abs(a[i+k-1]))
        res = min(res, c*2+d)
print(res)

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

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

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

a = []
b = []
for i in x:
    if i == 0:
        k -= 1
    elif i < 0:
        a.append(-i)
    else:
        b.append(i)
a.reverse()
ans = float("inf")
if len(b) > len(a):
    a, b = b, a
if k == 0:
    ans = 0
else:
    for i in range(k):
        if i < len(a):
            if i == k - 1:
                ans = min(ans, a[i])
            else:
                ta = a[i]
                if 0 <= k - i - 2 < len(b):
                    tb = b[k - i - 2]
                    ans = min(ans, ta * 2 + tb, ta + tb * 2)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
res = float("inf")
for i in range(N - K + 1):
  if a[i] * a[i + K - 1] < 0:
    mn = min(abs(a[i]), abs(a[i + K - 1]))
    mx = max(abs(a[i]), abs(a[i + K - 1]))
    res = min(res, mn * 2 + mx)
  else:
    res = min(res, max(abs(a[i]), abs(a[i + K - 1])))
print(res)

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

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

print(ans)

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

if N == K:
    start,end = x[0],x[-1]
    if end <= 0:
        ans = abs(start)
    elif start <= 0:
        ans = 2*min(abs(start), end)+max(abs(start), end)
    else:
        ans = end

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

print(ans)

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

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

def solve(A, K):
  ans = dist(A[0], A[K-1])
  for i in range(len(A)-K+1):
    ans = min(ans, dist(A[i], A[i+K-1]))
  return ans
               
N, K, *A = map(int, open(0).read().split())
print(solve(A, K))

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

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

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(l, r):
    
    if r <= 0:
        return -l
    
    elif l >= 0:
        return r
    
    else:
        
        return min(-l, r) * 2 + max(-l, r)

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

ans = INF
for i in range(N):
    if i+K > N:
        break
    l = A[i]
    r = A[i+K-1]
    ans = min(ans, check(l, r))
print(ans)

Solution (Python):
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
x = x[np.nonzero(x)]
if K == 0:
    print(0)
    sys.exit()
ans = []
for i in range(len(x)-K+1):
    if (x[i] < 0) != (x[i+K-1] < 0):
        ans.append(abs(x[i])+abs(x[i+K-1])+min(abs(x[i]),abs(x[i+K-1])))
    else:
        ans.append(max(abs(x[i]),abs(x[i+K-1])))
print(min(ans))

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
if N==1:
  print(0);exit()

ans=float("Inf")
for i in range(N-K+1):
  ans=min(ans, X[i+K-1]-X[i]+ min(abs(X[i+K-1]),abs(X[i]))  )

print(ans)

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


c = []
for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    c.append([l, r])




ans = 10 ** 10

for l, r in c:
    tmp = min(abs(l) + abs(r - l), abs(r) + abs(r - l))
    ans = min(ans, tmp)











print(ans)

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

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y


if count_m >= 1 and count_p>=1:
    
    
    
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2 for j in range(1, y) if count_m-j>=0  and count_m+y-1-j<N)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] for j in range(1, y) if count_m-j>=0 and count_m+y-1-j<N)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0


elif count_p>=K:
    
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    
    ans = min(tmp, tmp2)


print(ans)

Solution (Python):
n, k = map(int, input().split())
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
    min_l = min(0, l[i])
    max_l = max(0, l[i+k-1])
    min_abs = min(abs(min_l), abs(max_l))
    max_abs = max(abs(min_l), abs(max_l))
    ans = min(ans, min_abs * 2 + max_abs)
print(ans)

Solution (Python):
n, k = map(int, input().split())
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
    min_l = min(0, l[i])
    max_l = max(0, l[i+k-1])
    min_abs = min(abs(min_l), abs(max_l))
    max_abs = max(abs(min_l), abs(max_l))
    ans = min(ans, min_abs * 2 + max_abs)
print(ans)

Solution (Python):
import sys
import bisect
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

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

if min(A) > 0:
	print(A[K-1])
	sys.exit()
elif max(A) < 0:
	print(abs(list(reversed(A))[K-1]))
	sys.exit()


o = bisect.bisect_left(A, 0)


ans = float('INF')
for i in range(K+1):
	j = K - i
	
	if j==0 and o+K <= N-1:
		
		ans = min(ans, abs(A[o+K]))
	elif i==0 and o-K >= 0:
		
		ans = min(ans, abs(A[o-K]))
	elif o+i-1 <= N-1 and o-j >=0:
		if i<j:
			
			ans = min(ans, abs(A[o+i-1]*2) + abs(A[o-j]))
		elif i>j:
			
			ans = min(ans, abs(A[o+i-1]) + abs(A[o-j]*2))
		else:
			ans = min(ans, abs(A[o+i-1])+abs(A[o-j])*2, abs(A[o+i-1])*2+abs(A[o-j]))

print(ans)

Solution (Python):
import numpy as np
import bisect
N,K=map(int,input().split())
X=[int(i) for i in input().split()]
min_=10**10
for i in range(N-K+1):
    
    temp=min(abs(X[i])+abs(X[i]-X[i+K-1]),abs(X[i+K-1])+abs(X[i]-X[i+K-1]))
    min_=min(temp,min_)
print(min_)

Solution (Python):
n,k=map(int,input().split())
x=list(map(int,input().split()))
if k==1:
    ans=1000000000
    for i in range(n):
        now=abs(x[i])
        if now<ans:
            ans=now
    print(ans)
else:
    ans=1000000000
    for i in range((n-1)-k+1+1):
        l=x[i]
        r=x[i+(k-1)]
        if l<0 and r<0:
            check=abs(l)
            if check<ans:
                ans=check
        if 0<=l and 0<=r:
            check=abs(r)
            if check<ans:
                ans=check
        if l<0 and 0<=r:
            check_l=abs(l)
            check_r=abs(r)
            min_c=min(check_l,check_r)
            max_c=max(check_l,check_r)
            check=min_c*2+max_c
            if check<ans:
                ans=check
    print(ans)

Solution (Python):
N,  K = map(int, input().split())
candles = list(map(int, input().split()))
ans = float('inf')
for i in range(N - K + 1):
    if candles[i] * candles[i + K - 1] >= 0:
        t = max(abs(candles[i]), abs(candles[i + K - 1]))
    else:
        t = candles[i + K - 1] - candles[i] + min(abs(candles[i]), abs(candles[i + K - 1]))
    ans = min(ans, t)
print(ans)

Solution (Python):
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
if 0 not in x:
    bisect.insort(x, 0)
    N += 1
    K += 1
ans = 10 ** 9
for i in range(N-K+1):
    lo, hi = x[i], x[i+K-1]
    if lo <= 0 <= hi:
        ans = min(ans, abs(lo) + hi + min(abs(lo), hi))
print(ans)

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

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

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

l_x=sorted([abs(i) for i in x if i<0])
r_x=[i for i in x if i>=0]

ans=10**12
for r_num in range(k+1):
  l_num=k-r_num
  if l_num>len(l_x) or r_num>len(r_x):
    continue
  if l_num==0: 
    ans=min(ans,r_x[r_num-1])
  elif r_num==0: 
    ans=min(ans,l_x[l_num-1])
  elif l_x[l_num-1]<=r_x[r_num-1]: 
    ans=min(ans,l_x[l_num-1]*2+r_x[r_num-1])
  else: 
    ans=min(ans,l_x[l_num-1]+(r_x[r_num-1])*2)

print(ans)

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

l_x=[i for i in x if i<0]
r_x=[i for i in x if i>=0]

ans=10**12
for r_num in range(k+1):
  l_num=k-r_num
  
  if l_num>len(l_x) or r_num>len(r_x):
    continue
  if l_num==0: 
    ans=min(ans,r_x[r_num-1])
  elif r_num==0: 
    ans=min(ans,abs(l_x[-1*l_num]))
  elif abs(l_x[-1*l_num])<=r_x[r_num-1]: 
    ans=min(ans,abs(l_x[-1*l_num])*2+r_x[r_num-1])
  else: 
    ans=min(ans,abs(l_x[-1*l_num])+(r_x[r_num-1])*2)
print(ans)

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

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

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

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

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

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

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
ans = 10**100
for i in range(n-k+1):
  st=a[i]
  en=a[i+k-1]
  if st*en<0:
    ans = min(ans, min(abs(st),abs(en))+abs(st-en))
  else:
    ans = min(ans, max(abs(st),abs(en)))
print(ans)

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

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

ans = float("inf")

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

print(ans)

Solution (Python):
import sys
import bisect
n, k = [int(i) for i in sys.stdin.readline().split()]
x_ls = [int(i) for i in sys.stdin.readline().split()]
zero_pos = bisect.bisect_left(x_ls, 0)
x_ls = x_ls[:zero_pos] + [0] + x_ls[zero_pos:]
len_x = len(x_ls)
INF = 10**18
best = INF
for left in range(1+min(k, zero_pos)):
    right = k - left
    if zero_pos + right >= len_x:
        continue
    _min = x_ls[zero_pos - left]
    _max = x_ls[zero_pos + right]
    res = abs(_min) + abs(_max) + min(abs(_min), abs(_max))
    best = min(res, best)
print(best)

Solution (Python):

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

n, k = map(int, input().split())
A=[]; B=[]
x=list(map(int,input().split()))
for i in range(n-(k-1)):
    candi = max(abs(x[i+k-1]),abs(x[i]))
    if x[i+k-1]*x[i]<0:
        candi += 2*min(abs(x[i+k-1]), abs(x[i]))

    ans = min(ans, candi)
print(ans)

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

Solution (Python):
N,K=map(int,input().split())
A=[int(x) for x in input().split()]
B=[]
C=[]
for i in range(N):
  if A[i]<0:
    C += [-A[i]]
  else:
    B +=[A[i]]
C=sorted(C)
b=len(B)
c=len(C)
B=[0]+B+[float('inf')]*c
C=[0]+C+[float('inf')]*b
answer=float('inf')

for i in range(K):
  answer=min(answer,2*B[i]+C[K-i],2*C[i]+B[K-i])
print(answer)

Solution (Python):
N, K = map(int, input().split())
arr = list(map(int,input().split()))
left = []
right = []
for i in arr:
    if i < 0:
        left.append(abs(i))
    else:
        right.append(i)
left.sort()
tmp = 0
count = 0
if len(right) == 0:
    print(left[K-1])
    exit()
if len(right) >= K:
    tmp += right[K-1]
    i = 1
    while True:
        if len(left) < i or K-i-1 < 0:
            break
        tmp2 = right[K-i-1] + left[i-1] + min(right[K-i-1],left[i-1])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
else:
    tmp += right[-1]+left[K-len(right)-1]+min(right[-1],left[K-len(right)-1])
    i = 1
    while True:
        if K-len(right)-1+i >= K or i >= len(right) or K-len(right)-1+i >= len(left)-1:
            break
        tmp2 = right[-1-i] + left[K-len(right)-1+i] + min(right[-1-i],left[K-len(right)-1+i])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
print(tmp)

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

X.sort()

ma = max(X)
mi = min(X)

if ma <= 0:
    X.sort(key=lambda x: -x)
    a = 0
    for i in range(K):
        a = X[i]
    print(abs(a))
elif 0 <= mi:
    a = 0
    for i in range(K):
        a = X[i]
    print(abs(a))
else:
    ans = 1000000000000000000
    for i in range(N):
        l = i
        r = i + K - 1
        if r < N and X[l] <= 0 <= X[r]:
            if abs(X[l]) > abs(X[r]):
                ans = min(abs(X[r]) * 2 + abs(X[l]), ans)
            else:
                ans = min(abs(X[l]) * 2 + abs(X[r]), ans)
    print(ans)