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):
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))
 
    def solve(self):
        candidates = []
        for i in range(self.n - self.k + 1):  
            candidates.append((i, i + self.k - 1))
            
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        
        print(best_distance)
 
    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]]
        
        if right <= 0:
            return abs(left)
        
        elif left >= 0:
            return abs(right)
        
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        
        else:
            return abs(right) + abs(right) + abs(left)
 
    def filter(self):
        
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)
 
        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]
 
 
if __name__ == "__main__":
    s = Solver()
    s.solve()

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

    n = []
    p = []
    ans = []
    for xi in x:
        if xi < 0:
            n.append(xi)
        else:
            p.append(xi)

    

    
    
    for i in range(k+1):
        
        if i > len(n):
            continue
        if k-i > len(p):
            continue

        if i == 0:
            ans.append(p[k-1])
        elif k - i == 0:
            ans.append(-n[-k])
        else:
            nd = -n[-i]
            pd = p[k-i-1]
            ans.append(nd + pd + min(nd, pd))

    
    print(min(ans))


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = abs(array[array<0])
ne_array.sort()

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        dist = 10**10
        if i == k-1:
            dist = short_array[i]
        elif k-i-1 <= len(long_array): 
            dist = min(short_array[i]*2 + long_array[k-i-2], short_array[i] + long_array[k-i-2]*2)
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Solution (Python):
def solve(N, K, Xs):
    plus = sorted([x for x in Xs if x >= 0])
    minus = sorted([abs(x) for x in Xs if x < 0])
    ans = 10 ** 9
    for i in range(0, K + 1):
        j = K - i
        if i <= len(plus) and i == K:
            p = plus[i-1]
            ans = min(ans, p)
        elif j <= len(minus) and j == K:
            m = minus[j-1]
            ans = min(ans, m)
        elif i <= len(plus) and j <= len(minus):
            p = plus[i - 1]
            m = minus[j - 1]
            ans = min(ans, p * 2 + m, m * 2 + p)

    return ans
  
if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    Xs = list(map(int, input().split(" ")))
    print(solve(N, K, Xs))

Solution (Python):

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

x_minus = filter(lambda x: x<0, x)
x_plus  = filter(lambda x: x>=0, x)

x_minus = sorted(x_minus, reverse=True)
x_plus  = sorted(x_plus , reverse=False)

n_minus = len(x_minus)
n_plus = len(x_plus)


ans = 10**9


if n_minus >= K:
    
    ans = min(ans, abs(x_minus[K-1]))


if n_plus  >= K:
    
    ans = min(ans, x_plus[K-1])


for i in range( min(K-1, n_minus) ):
    remain = K - i - 1
    try:
        ans = min(ans, 2*abs(x_minus[i]) + x_plus[K-i-1-1])
    except IndexError:
        continue
    

for i in range( min(K-1, n_plus) ):
    remain = K - i - 1
    try: 
        ans = min(ans, 2*x_plus[i] + abs(x_minus[K-i-1-1]))
    except IndexError:
        continue

print(ans)

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

if len(x) == 1 and x[0] == 0:
    print(0)
    exit()

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

ans = float('inf')
if len(m) >= k:
    ans = min(ans,abs(m[-k]))
    m = m[-k+1:]
if len(p) >= k:
    ans = min(ans,p[k-1])
    p = p[:k-1]

x = m+p
i = 0
for i in range(len(x)-k+1):
    ans = min(ans, min(abs(x[i])*2+abs(x[i+k-1]), abs(x[i])+abs(x[i+k-1])*2))

print(ans)

Solution (Python):
n,k = (int(i) for i in input().split())
x =list(int(i) for i in input().split())
a = [999999999999999]*(n-k+1)
for i in range(n-k+1):
  left =x[i]
  right =x[i+k-1]
  leftdistance = abs(left)+abs(right-left)
  rightdistance = abs(right)+abs(left-right)
  a[i] = min(leftdistance,rightdistance)
print(min(a))

Solution (Python):
import numpy as np

N,K = map(int,input().split())
x = list(map(int,input().split()))
min_t = None
for i in range(N-K+1):
  t = min(abs(x[i])+abs(x[i]-x[i+K-1]), abs(x[i+K-1])+abs(x[i]-x[i+K-1]))
  if min_t == None:
    min_t = t
  if min_t > t:
    min_t = t
print(min_t)

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

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,k = map(int,input().split())
x = list(map(int,input().split()))
 
kyori=10**9
for i in range(n-k+1):

  if x[k+i-1]<=0:
    kyori = min(kyori,abs(x[i]))
  elif x[i]<0 and 0<x[k+i-1]:
    kyori = min(kyori,abs(x[i]*2)+abs(x[k+i-1]),abs(x[i])+abs(x[k+i-1]*2))
  elif 0<=x[i]:
    kyori = min(kyori,abs(x[k+i-1]))
print(kyori)

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

x.append(0)
x = sorted(x)
check = []
for i in range(n-k+1):
    a = abs(x[i])
    b = abs(x[i+k])
    check.append(max(a,b) + min(a,b) * 2)
print(min(check))

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

ans = []
for i in range(n-k+1):
    if x[i] * 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):
def main():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))

    i = 0
    ans = 10 ** 8 * n
    while i < n - k + 1:
        lr = abs(x[i]) + abs(x[i] - x[i + k - 1])
        rl = abs(x[i + k - 1]) + abs(x[i] - x[i + k - 1])
        i += 1
        ans = min(ans, lr, rl)
    print(ans)


main()

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
minval = 10000000000
for i in range(N-K+1):
    tmpval = abs(X[i]) + X[i+K-1] - X[i]
    if minval > tmpval:
        minval = tmpval
X = [-x for x in X[::-1]]
for i in range(N-K+1):
    tmpval = abs(X[i]) + X[i+K-1] - X[i]
    if minval > tmpval:
        minval = tmpval
print(minval)

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




print(min_space)

Solution (Python):

import bisect

def main():
    na = list(map(int, input().split()))
    N, K = na[0], na[1]
    x = list(map(int, input().split()))

    l = bisect.bisect_left(x, 0)

    r = 3 * (10 ** 8)

    for i in range(min(l, K), -1, -1):
        if N < l + K - i:
            break
        if i == K:
            r = min(r, abs(x[l - K]))
        elif i == 0:
            r = min(r, abs(x[l + K - 1]))
        else:
            r = min(r, abs(x[l - i]) * 2 + x[l + K - i - 1], abs(x[l - i]) + x[l + K - i - 1] * 2)


    print(r)


if __name__ == '__main__':
    main()

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

ans = 10 ** 9
for i in range(n - k + 1):
    L = A[i]
    R = A[i + k - 1]
    r1 = abs(L) - L + R
    r2 = abs(R) - L + R
    temp = min(r1, r2)
    ans = min(ans, temp)
    
print(ans)

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

cost_min = 999999999

for i in range(N-K+1):
    if X[i] < 0 and X[i+K-1] < 0:
        cost_tmp = abs(X[i])
    elif X[i] > 0 and X[i+K-1] > 0:
        cost_tmp = X[i+K-1]
    else:
        cost_tmp = min(X[i+K-1]-X[i]*2, X[i+K-1]*2- X[i])
    if cost_tmp <= cost_min:
        cost_min = cost_tmp

print(cost_min)

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):
    ans = min(ans, min(abs(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()))
y = []

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

print(min(y))

Solution (Python):
def a():return input()
def ai():return int(input())
def ma():return map(int, input().split())
def ms():return map(str, input().split())
def lma():return list(map(int, input().split()))
def lms():return list(map(str, input().split()))
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,abs(a0))
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,ab)
print(ans)

Solution (Python):
from collections import Counter
from math import *
N, K = [int(x) for x in input().strip().split()]
x = [int(a) for a in input().strip().split()]
xpos = [0] + [a for a in x if a >= 0]
xneg = [0] + [abs(a) for a in x if a < 0]
xneg.sort()

timemin = 2**64
for i in range(0, min(len(xpos), K + 1)):
    posnum = i
    negnum = K - i
    if negnum >= len(xneg):
        continue
    time = xpos[posnum] * 2 + xneg[negnum]
    if time < timemin:
        timemin = time
for i in range(0, min(len(xneg), K + 1)):
    posnum = K - i
    negnum = i
    if posnum >= len(xpos):
        continue
    time = xneg[negnum] * 2 + xpos[posnum]
    if time < timemin:
        timemin = time

print(timemin)

Solution (Python):
N,K = (int(i) for i in input().split())
x = [int(i) for i in input().split()]
time=[]
for i in range(N-K+1):
    minSign='minus' if x[i]<0 else 'plus'
    maxSign='minus' if x[i+K-1]<0 else 'plus'
    if minSign=='minus' and maxSign=='minus':
        time.append(abs(x[i]))
    elif minSign=='plus' and maxSign=='plus':
        time.append(abs(x[i+K-1]))
    else:
        time.append(abs(x[i])+abs(x[i+K-1])+min(abs(x[i]),abs(x[i+K-1])))
print(min(time))

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

ans = 10**18
for i in range(N-K+1):
    y0 = x[i]
    yk = x[i+K-1]
    if yk < 0:
        ans = min(ans, abs(y0))
    elif y0 <= 0 and 0 <= yk:
        ans = min(ans, yk-2*y0, 2*yk-y0)
    else:
        ans = min(ans, yk)

print(ans)

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

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

Solution (Python):
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
elif len(Xpositive) == 0:
    sub = Xnegative[K-1]
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and -i-2 >= -len(Xpositive):
        distance = min(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

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

ans = 10 ** 5 * n
for i in range(n-k+1):
    l = A[i]
    r = A[i+k-1]
    route1 = abs(l) - l + r
    route2 = abs(r) + r - l
    temp = min(route1, route2)
    ans = min(ans, temp)
    
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):
    ans = min(ans, A[i + K - 1] - A[i] + min(abs(A[i + K - 1]), abs(A[i])))
print(ans)

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

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

Solution (Python):

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




D = []
for i in range(n - k + 1):
    t1 = abs(X[i]) + (X[i + k - 1] - X[i])
    t2 = abs(X[i + k - 1]) + (X[i + k - 1] - X[i])
    D.append(min(t1, t2))

print(min(D))

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

for i in range(n - k + 1):
    L = A[i]
    R = A[i + k -1]
    dist = min(abs(L) - L + R, abs(R) + R - L)
    D.append(dist)
    
print(min(D))

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

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

ans = INF
for li in range(n - k + 1):
    ri = k + li - 1
    t = min(abs(x[li]) + abs(x[ri] - x[li]), abs(x[ri]) + abs(x[li] - x[ri]))
    ans = min(t, ans)

print(ans)

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

def sign(x):
    return int(x >= 0)

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

print(ans)

Solution (Python):
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**15
 
for i in range(x):
    tmp = i + b[1] - 1
    if res > (a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp]))):
	    res = a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp]))
        
print(res)

Solution (Python):
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**15
 
for i in range(x):
    tmp = i + b[1] - 1
    if res > (a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp]))):
	    res = a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp]))
        
print(res)

Solution (Python):
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**100
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min( a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp])),res)
        
print(res)

Solution (Python):

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


a = input().split()

x = [int(m) for m in a]
lx = []
mx = []
rx = []
for i in range(n):
    if x[i] < 0:
        lx.append(x[i])
    elif x[i] == 0:
        mx.append(x[i])
    else:
        rx.append(x[i])

lx.sort(reverse=True)
rx.sort()
lmx = lx + mx
lmx.sort(reverse=True)
ans1 = float("INF")
if len(lmx) >= k:
    ans1 = abs(lmx[k - 1])

mrx = mx + rx
mrx.sort()
ans2 = float("INF")
if len(mrx) >= k:
    ans2 = abs(mrx[k - 1])

p = 1
ans3 = float("INF")
while p <= len(rx):
    if len(rx) >= p and len(lx) >= k - p - len(mx) and k - p - len(mx) > 0:
        if ans3 > abs(2 * rx[p - 1]) + abs(lx[k - p - len(mx) - 1]):
            ans3 = abs(2 * rx[p - 1]) + abs(lx[k - p - len(mx) - 1])
    p += 1

q = 1
ans4 = float("INF")
while q <= len(lx):
    if len(lx) >= q and len(rx) >= k - q - len(mx) and k - q - len(mx) > 0:
        if ans4 > abs(2 * lx[q - 1]) + abs(rx[k - q - len(mx) - 1]):
            ans4 = abs(2 * lx[q - 1]) + abs(rx[k - q - len(mx) - 1])
    q += 1

print(min(ans1,ans2,ans3,ans4))

Solution (Python):
INF = 10**15
N,K = map(int,input().split())
x = list(map(int,input().split()))
num = 0
positive = []
negative = []
for i in range(N):
    if x[i] == 0:
        N  -= 1
    elif x[i] < 0:
        negative.append(x[i])
    else:
        positive.append(x[i])
negative = negative[::-1]
dist = INF
if len(positive) >= K :
    dist = positive[K-1]
    
for i in range(len(negative)):
    if len(positive) > 0 and len(positive) >= K - (i+1) :
        tmp = - 2*negative[i] + positive[K-1-i-1]
        if dist > tmp:
            dist = tmp
            

if len(negative) >= K and dist > -negative[K-1]:
    dist  = -negative[K-1]

for i in range(len(positive)):
    if len(negative) > 0 and len(negative) >= K - (i+1) :
        tmp = -negative[K-1-i-1] + 2*positive[i]
        if dist > tmp:
            dist = tmp
if N == 0:
    print(x[0])
else:
    print(dist)

Solution (Python):
def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    res = 1 << 30
    for i in range(k - 1, n):
        d = xs[i] - xs[i - k + 1] + min(abs(xs[i]), abs(xs[i - k + 1]))
        res = min(res, d)
    print(res)


if __name__ == '__main__':
    main()

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

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

d = 10**10
for i in range(n-k+1):
    x_l = l[i]
    x_r = l[i+k-1]

    if x_l >= 0:
        tmp = x_r
    elif x_r <= 0:
        tmp = -x_l
    else:
        tmp = min(abs(x_r), abs(x_l)) + abs(x_l-x_r)

    d = min(d, tmp)

print(d)

Solution (Python):
n,k=map(int,input().split())
x=list(map(int,input().split()))
if (0 in x):
    k-=1
else:
    x.append(0)
    x.sort()

zeroPos=x.index(0)
leftLen=zeroPos
rightLen=len(x)-zeroPos-1


left=[]
right=[]
for turn in range(leftLen+1):
    if(turn>k):
        break
    if(k-turn<=rightLen):
        left.append(x[zeroPos-turn]*-2 + x[zeroPos+k-turn])
for turn in range(rightLen+1):
    if(turn>k):
        break
    if(k-turn<=leftLen):
        right.append(2*x[zeroPos+turn] + -1*x[zeroPos-k+turn])
minL=min(left)
minR=min(right)

print(minL if minL<minR else minR)

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

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

print(time)

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

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

if 0 in x:
  K -= 1
  x.remove(0)
  
if K <= 0:
  print(0)
  exit()
if K == 1:
  print(min(abs(i) for i in x))
  exit()
  
px = [i for i in x if i > 0]
mx = [i for i in x if i < 0]


  
lp = len(px)
lm = len(mx)

mn = (abs(x[0]) + abs(x[len(x)-1])) * 2
if lp >= K:
  t = px[K-1]
  if t < mn:
    mn = t
if lm >= K:
  t = -mx[-K]
  if t < mn:
    mn = t
for i in range(max(lm-K+1, 0), min(lm,len(x)-K+1)):
  tm = -x[i]
  tp = x[i+K-1]
  t = min(tm, tp) * 2 + max(tm, tp)
  if t < mn:
    mn = t
  
print(mn)

Solution (Python):
n, k = map(int, input().split())
C = list(map(int, input().split()))
D = []

for i in range(n - k + 1):
    l = C[i]
    r = C[i + k -1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    dist = min(route1, route2)
    D.append(dist)
    
print(min(D))

Solution (Python):
import bisect

N,K = list(map(int,input().strip().split()))
xlist = list(map(int,input().strip().split()))
ans = int(10e10)

if 0 not in xlist:
    bisect.insort_left(xlist,0)
else:
    K -= 1

if (bisect.bisect_left(xlist,0)+1) > K:
    del xlist[0:(bisect.bisect_left(xlist,0) - K)]
if len(xlist) < (bisect.bisect_left(xlist,0) + K):
    del xlist[(bisect.bisect_left(xlist,0)+K +1):]

for i in range(len(xlist)-K):
    low = min(abs(xlist[i]),abs(xlist[i+K]))
    high = max(abs(xlist[i]),abs(xlist[i+K]))
    ans = min(ans,low*2+high)

print(ans)

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

ans=10**11
for i in range(0,len(x)-K+1):
    if x[i]*x[K-1+i] <0:
        anss=abs(x[i])+ abs(x[K-1+i])
        anss += min(abs(x[i]),abs(x[K-1+i]))
    else:
        anss=max(abs(x[i]), abs(x[K-1+i]))
    ans=min( ans, anss)

print(ans)

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

for i in range(n - k + 1):
    l = X[i]
    r = X[i + k - 1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    temp = min(route1, route2)
    T.append(temp)
    
print(min(T))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
    ans.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(ans))

Solution (Python):
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
cand = []

for i in range(n - k + 1):
    l = x_list[i]
    r = x_list[i + k - 1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    cand.append(min(route1, route2))
    
print(min(cand))

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

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

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

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

x.append(0)
x.sort()



min = 10**10

zeroIdx = x.index(0)

for i in range(zeroIdx-k,zeroIdx+1):
    if i < 0 : 
        continue
    elif len(x) <= i + k :
        break

    plsIdx = i + k 
    misIdx = i

    

    if abs(x[plsIdx]) > abs(x[misIdx]) :
        tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
    else:
        tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])

    if tmp < min :
        min = tmp

print(min)

Solution (Python):
N,K = map(int,input().split())
X = list(map(int,input().split()))
min_time = 10**14
for i in range(N-K+1):
    l_edge = X[i]
    r_edge = X[i+K-1]
    route1 = abs(l_edge) + r_edge - l_edge
    route2 = abs(r_edge) + r_edge - l_edge
    time = min(route1,route2)
    if time < min_time:
        min_time = time
print(min_time)

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

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

def dist(i):
    a = abs(X[K - 1 + i]) + X[K - 1 + i] - X[i]
    b = abs(X[i]) + X[K - 1 + i] - X[i]
    return min(a, b)

ans = dist(0)
for i in range(N):
    if i + K > N:
        break
    ans = min(ans, dist(i))
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
mini = 1000000000
for i in range(N - K + 1):
    if min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i]) < mini:
        mini = min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i])

print(mini)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
pos = []
neg = []
for i in x:
    if i < 0:
        neg.append(-i)
    else:
        pos.append(i)
neg = neg[::-1]

pos = [0] + pos
neg = [0] + neg





res = float('inf')
for i in range(min(len(pos),k)):
    
    j = k - i
    if j >= len(neg):
        continue
    tmp = pos[i] * 2 + neg[j]
    res = min(res, tmp)


for i in range(min(len(neg),k)):
    j = k - i
    if j >= len(pos):
        continue
    tmp = neg[i] * 2 + pos[j]
    res = min(res, tmp)

print(res)

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

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


if __name__ == "__main__":
    main()

Solution (Python):
m,k=map(int,input().split())
lst=list(map(int,input().split()))
if 0 in lst:
    k-=1
    if k==0:
        print(0)
        exit()
p=list(filter(lambda x:x>0,lst))
n=list(map(abs,filter(lambda x:x<0,lst)))
n.sort()
ans=[]
if len(n)>=k:
    ans.append(n[k-1])
i=min(len(n),k-1)-1
while i>=0 and 0<=k-i-2<len(p):
    ans.append(n[i]*2+p[k-i-2])
    i-=1
i=min(len(p),k-1)-1
if len(p)>=k:
    ans.append(p[k-1])
while i>=0 and 0<=k-i-2<len(n):
    ans.append(p[i]*2+n[k-i-2])
    i-=1
print(min(ans))

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

ans = float('inf')
for left in range(n-k+1):
    right = left + k - 1
    if x[right] <= 0:
        ans = min(ans, abs(x[left]))
    elif x[left] >= 0:
        ans = min(ans, x[right])
    else:
        ans = min(ans, abs(x[left]) * 2 + x[right])
        ans = min(ans, abs(x[left]) + x[right] * 2)

print(ans)

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

ans = 10**18
for l in range(N-K+1):
    r = l+K-1
    if X[l]*X[r] >= 0:
        tmp = max(abs(X[l]), abs(X[r]))
    else:
        tmp1 = abs(X[l])*2 + abs(X[r])
        tmp2 = abs(X[r])*2 + abs(X[l])
        tmp = min(tmp1, tmp2)
    ans = min(ans, tmp)
print(ans)

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

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
x = list(li())

ans = 10**18

for st in range(n-k+1):
    ed = st+k-1
    cand = max(abs(x[st]), abs(x[ed]))
    if x[st] * x[ed] < 0:
        cand += 2 * min(abs(x[st]), abs(x[ed]))
        
    ans = min(ans,cand)
    
print(ans)

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

l=l[::-1]
l=l[:k]
r=r[:k]

ll=len(l)
rr=len(r)
lx=sum(l)
rx=sum(r)

if ll<k: lx=lx*2+sum(r[:(k-ll)])
if rr<k: rx=rx*2+sum(l[:(k-rr)])
ans=min(lx,rx)
o=[ll,[0,k-ll][ll<k]]
p=[rr,[0,k-rr][rr<k]]


for i in range(1,k):
    if o[1]==0: lx*=2
    if p[1]==0: rx*=2
    if o[1]+1<=rr and o[0]>0:
        lx-=l[o[0]-1]*2
        lx+=r[o[1]]
        o[0]-=1
        o[1]+=1
        ans=min(ans,lx)
    if p[1]+1<=ll and p[0]>0:
        rx-=r[p[0]-1]*2
        rx+=l[p[1]]
        p[0]-=1
        p[1]+=1
        ans=min(ans,rx)
print(ans)

Solution (Python):
def main():
    n, k = list(map(int, input().split()))
    x = list(map(int, input().split()))
    ans = float('inf')
 
    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]
 
        if left * right >= 0:
            ans = min(ans, max(abs(left), abs(right)))
        else:
            ans = min(ans, 2 * abs(left) + abs(right), abs(left) + 2 * abs(right))
 
    print(ans)
 
 
if __name__ == '__main__':
    main()

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

Solution (Python):
from bisect import bisect_left
N, K = map(int, input().split())
x = [int(s) for s in input().split()]

izero = bisect_left(x, 0)
zpos = x[izero:]
neg = [-v for v in reversed(x[:izero])]

ans = 1000000000
if len(neg) >= K and ans > neg[K - 1]:
    ans = neg[K - 1]
if len(zpos) >= K and ans > zpos[K - 1]:
    ans = zpos[K - 1]
for lenneg in range(max(1, K - len(zpos)), min(K - 1, len(neg)) + 1):
    cand = 2 * neg[lenneg - 1] + zpos[K - lenneg - 1]
    if ans > cand:
        ans = cand
    cand = neg[lenneg - 1] + 2 * zpos[K - lenneg - 1]
    if ans > cand:
        ans = cand
print(ans)