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()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if 0 in x:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1

if k==0:
    print(0)
    exit()
ans=10**10
if na>=k:
    ans=-a[-k]

if nb>=k:
    ans=min(ans,b[k-1])

if nb==0:
    print(-a[-k])
    exit()

if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

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

lis = sorted(list(map(int, input().split())))

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

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):
    if x[i] < 0 and x[i+K-1] > 0:
        ans += abs(x[i]) + abs(x[i+K-1]) + min(abs(x[i]),abs(x[i+K-1])),
    elif x[i] < 0:
        ans += -x[i],
    else:
        ans += x[i+K-1],
print(min(ans))

Solution (Python):
def main():
  N, K = [int(n) for n in input().split(" ")]
  X = [int(x) for x in input().split(" ")]
  X.sort()
  
  minus = []
  plus = []
  for i in range(len(X)):
    if X[i] < 0:
      minus.append(-X[i])
    else:
      plus += X[i:]
      break

  minus.reverse()

  T = []
  
  if len(plus) >= K:
    T.append(plus[K - 1])
  if len(minus) >= K:
    T.append(minus[K - 1])
  for i in range(K):
    if i < len(plus) and 0 <= K - i - 2 < len(minus):
      T.append(2 * plus[i] + minus[K - i - 2])
      T.append(2 * minus[K - i - 2] + plus[i])
    if i < len(minus) and 0 <= K - i - 2 < len(plus):
      T.append(2 * minus[i] + plus[K - i - 2])
      T.append(2 * plus[K - i - 2] + minus[i])
  
  print(min(T))

main()

Solution (Python):

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

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

print(ans)

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

s = 10**9

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

    s = min(s, t)

print(s)

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

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

x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')

if count1 == 0:
    print(x1[k-1])
elif count0 == 0:
    print(-x2[k-1])
else:
    if 0 in x1:
        x1.remove(0)
        k -= 1
        count0 -= 1
    for i in range(min(count0, k)):
        if count1 >= k-i-1:
            ans = min(ans, x1[i]*2-x2[k-i-2])
    for i in range(min(count1, k)):
        if count0 >= k-i-1:
            ans = min(ans, -x2[i]*2+x1[k-i-2])
    print(ans)

Solution (Python):
n,k = map(int,input().split())
l = list(map(int,input().split()))
ans = 10**13
for i in range(n-k+1):
    left = l[i]
    right = l[k+i-1]
    if right <= 0:
        time = abs(left)
    elif 0 <= left:
        time = abs(right)
    else:
        time = min(abs(left),abs(right))*2 + max(abs(left),abs(right))
    ans = min(ans,time)
print(ans)

Solution (Python):
import sys
n,k = map(int,input().split())
x_ls = list(map(int, input().split()))
x_ls.sort()
if x_ls[0] >= 0:
    print(x_ls[k-1])
    sys.exit()
elif x_ls[-1] <= 0:
    x_ls.sort(reverse=True)
    print(abs(x_ls[k-1]))
    sys.exit()
l = 0
r = k - 1
ans = float('inf')
while r < n:
    cost_1 = 2*abs(x_ls[l]) + abs(x_ls[r])
    cost_2 = abs(x_ls[l]) + 2*abs(x_ls[r])
    cost = min(cost_1,cost_2)
    ans = min(cost, ans)
    r += 1
    l += 1
print(ans)

Solution (Python):
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
  if x[i] == 0:
    p, m = i, i+1
    break
  elif x[i] > 0:
    p, m = i, i
    break
    
if (x[0]<0) and (x[-1]>0):
  pls = x[p:]
  mns = x[:m]
  ans = math.inf

  for i in range(len(pls)):
    if i < K-1:
      if pls[0] == 0:
        n = K - i
      else:
        n = K - i - 1
      if len(mns) >= n:
        tmp = 2*abs(mns[-n]) + pls[i]
        ans = min(ans, tmp)
    if i == K-1:
      ans = min(ans, pls[i])
      break
    
  for i in range(len(mns)):
    m = len(mns)-i
    if m < K:
      if pls[0] == 0:
        n = K - m
      else:
        n = K - m - 1
      if len(pls) > n:
        tmp = 2*pls[n] + abs(mns[-m])
        ans = min(ans, tmp)
    if m == K:
      ans = min(ans, abs(mns[i]))
    
elif x[0] >= 0:
  ans = x[K-1]
  
else:
  ans = abs(x[-K])
  
print(ans)

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

neg_to_pos = n
for i in range(n):
    if x[i] > 0:
        neg_to_pos = i
        break

res = 10 ** 14
for i in range(n - k + 1):
    l = min(x[i], 0)
    r = max(0, x[i + k - 1])
    if i <= neg_to_pos and neg_to_pos - 1 <= i + k - 1:
        tmp = min(-2 * l + r, -l + 2 * r)
        res = min(res, tmp) 
print(res)

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

for i in range(n):
    if x[i] > 0:
        neg_num = i
        break
pos_num = n - neg_num

l = min(k, neg_num)
res = 10 ** 13
for i in range(l, -1, -1):
    r = k - i
    if r <= pos_num:
        l_val = min(0, x[neg_num - i])
        r_val = max(0, x[neg_num + k - i - 1])
        tmp = min( -l_val * 2 + r_val, -l_val + r_val * 2)
        res = min(tmp, res)
print(res)

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

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)
import math

import bisect
import numpy as np




INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<=0:
            if right <= 0:
                best_time = min(best_time, abs(left))
            else:
                best_time = min(abs(left) + right + min(abs(left), right), best_time)
        else:
            best_time = min(best_time, right)
            break

    print(best_time)


if __name__ == "__main__":
    run()

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

ans = []
for i in range(N - K + 1):
    left = i
    right = left + K - 1
    if x[left] < 0 and x[right] >= 0:
        q = min(abs(x[left]) * 2 + x[right], x[right] * 2 + abs(x[left]))
        ans.append(q)
        continue
    elif x[right] < 0:
        q = abs(x[left])
        ans.append(q)
        continue
    elif x[left] > 0:
        q = abs(x[right])
        ans.append(q)
        continue
if len(ans) == 0:
    print(0)
    exit()

print(min(ans))

Solution (Python):
import sys

input = sys.stdin.readline


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

    X.sort()
    ans = float("inf")
    for l in range(N - K + 1):
        r = l + K - 1
        l2r = abs(X[l]) + abs(X[l] - X[r])
        r2l = abs(X[r]) + abs(X[r] - X[l])
        ans = min(ans, l2r, r2l)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
    
N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10**9
    
l=inf
dp=deque(x[N-K:N])
i=0
while True:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if abs(dp[K-1])>=abs(dp[0]):
            tmp=abs(dp[0])*2+abs(dp[K-1])
        else:
            tmp=abs(dp[K-1])*2+abs(dp[0])
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)

Solution (Python):
import sys

input = sys.stdin.readline


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

    INF = float("inf")
    M = [INF] * N
    P = [INF] * N
    i_m, i_p = 0, 0
    for x in X:
        if x == 0:
            K -= 1
        elif x > 0:
            P[i_p] = x
            i_p += 1
        else:
            M[i_m] = -x
            i_m += 1

    if K <= 0:
        print(0)
        exit()

    M.sort()
    P.sort()
    ans = min(M[K - 1], P[K - 1])
    for n_M in range(1, K + 1):
        dist = 2 * M[n_M - 1] + P[K - n_M - 1]
        if ans > dist:
            ans = dist
    for n_P in range(1, K + 1):
        dist = 2 * P[n_P - 1] + M[K - n_P - 1]
        if ans > dist:
            ans = dist

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n,K=(int(i) for i in input().strip().split(" "))
x=[int(i) for i in input().strip().split(" ")]
m=1000000000000000000000
for i in range(n-K+1):
    k=-x[i]+x[i+K-1]+min(abs(x[i]),abs(x[i+K-1]))
    if k<m:
        m=k
print(m)

Solution (Python):
N, K = map(int, input().split())
D = [int(n) for n in input().split()]
ans = 10000000000

for i in range(N-K+1):
    L = D[i]
    R = D[i+K-1]
    LR = abs(L) + abs(L - R)
    RL = abs(R) + abs(R - L)
    ans = min(ans, min(LR, RL))

print(ans)

Solution (Python):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
flag = True
if 0 not in ls:
    ls.append(0)
    ls.sort()
    flag = False
ind = ls.index(0)
if flag == True:
    if ind >= k-1:
        l = ind - k + 1
    else:
        l = 0
    if ind + k - 1 <= n - 1:
        r = ind + k - 1
    else:
        r = n - 1 
    mn = float("inf")
    while True:
        if l+k-1 > r:
            break
        a = abs(ls[l]) + ls[l+k-1] + min(abs(ls[l]),ls[l+k-1])
        if a < mn:
            mn = a
        l += 1
else:
    if ind >= k:
        l = ind - k
    else:
        l = 0
    if ind + k <= n:
        r = ind + k
    else:
        r = n
    mn = float("inf")
    while True:
        if l+k > r:
            break
        a = abs(ls[l]) + ls[l+k] + min(abs(ls[l]),ls[l+k])
        if a < mn:
            mn = a
        l += 1
print(mn)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *X = map(int, read().split())

    A = [abs(x - X[0]) for x in X]

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def solve(N, K, X):
    A = [abs(x - X[0]) for x in X]

    ans = INF
    for i in range(N - K + 1):
        if ans > abs(X[i]) + A[i + K - 1] - A[i]:
            ans = abs(X[i]) + A[i + K - 1] - A[i]

    return ans


def main():
    N, K, *X = map(int, read().split())

    ans = solve(N, K, X)
    ans = min(ans, solve(N, K, X[::-1]))

    print(ans)
    return


if __name__ == '__main__':
    main()

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

print(ans)

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

ans = 10 ** 10
nega = sorted(-x for x in X if x < 0)
posi = sorted(x for x in X if 0 < x)
if 0 in X:
    K -= 1

if 0 < K <= len(posi):
    t = posi[K - 1]
    ans = min(ans, t)
if 0 < K <= len(nega):
    t = nega[K - 1]
    ans = min(ans, t)

if 0 < K:
    for np in range(1, len(posi) + 1):
        if 0 <= K - np - 1 < len(nega):
            t = posi[np - 1] * 2 + nega[K - np - 1]
            ans = min(ans, t)
    for nn in range(1, len(nega) + 1):
        if 0 <= K - nn - 1 < len(posi):
            t = nega[nn - 1] * 2 + posi[K - nn - 1]
            ans = min(ans, t)
else:
    ans = 0
print(ans)

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

Solution (Python):

n, k, *x = map(int, open(0).read().split())
ans = 10**18
neg = [i for i in x if i<0]+[0]
pos = [0]+[i for i in x if i>=0]
l = len(neg) -max(0, k + 1 - len(pos))
r = min(k+1, len(pos))
while l > 0 and r > 0:
    l -= 1
    r -= 1
    ans = min(ans, -neg[l] * 2 + pos[r], -neg[l] + pos[r] * 2)
print(ans)

Solution (Python):

n, k, *x = map(int, open(0).read().split())
ans = 10**18
neg = []
pos = [0]
for i in x:
    if i < 0:
        neg.append(i)
    else:
        pos.append(i)
neg.append(0)
k += 1
c = len(neg)
c -= k -min(k, len(pos))
k = min(k, len(pos))
while c > 0 and k > 0:
    c -= 1
    k -= 1
    ans = min(ans, -neg[c] * 2 + pos[k], -neg[c] + pos[k] * 2)
print(ans)

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

mt=5*10**8

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

if x[0]>0:mt=min(mt,x[k-1])
elif x[-1]<0:mt=min(mt,abs(x[-1-k+1]))
print(mt)

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

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k+1):
  if i+k<c:continue
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))
  if i+k-1 ==c-1:mt=min(mt,abs(x[i]))
  if i==c:mt=min(mt,x[i+k-1])
if n==k:
  if c!=n and c!=0:
    mt=min(2*abs(x[0])+x[-1],2*x[-1]+abs(x[0]))
  else:
    mt=max(abs(x[0]),abs(x[-1]))
print(mt)

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

ans=10**10

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,right-left+min(abs(right),abs(left)))

print(ans)

Solution (Python):






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





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

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

M=float("inf")
for i in range(N-K+1):
    a,b=X[i],X[i+K-1]
    if 0<=a or b<=0:
        M=min(M,max(abs(a),abs(b)))
    else:
        c=min(abs(a),abs(b))
        M=min(M,abs(a)+abs(b)+abs(c))
print(M)

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

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

import bisect

b=bisect.bisect_left(x,0)

r=x[b:]
l=list(reversed(x[:b]))


R=len(r)
L=len(l)

ans=float('inf')

if k<=R:
    ans=min(ans,r[k-1])

if k<=L:
    ans=min(ans,abs(l[k-1]))


for i in range(R):
    if i<k and k-i-2<=L-1 and k-i-2>=0:
        tmp=2*r[i] + abs(l[k-i-2])
        ans=min(ans,tmp)

for j in range(L):
    if j<k and k-j-2<=R-1 and k-j-2>=0:
        tmp = 2*abs(l[j]) +r[k-j-2]
        ans=min(ans,tmp)


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):
  b=a[i]
  c=a[i+k-1]
  if c<=0:
    ans=min(ans,abs(b))
  elif b>=0:
    ans=min(ans,abs(c))
  else:
    ans=min(ans,abs(c)+abs(b)*2,abs(c)*2+abs(b))
    
print(ans)

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

if len(lis) == 1:
    print(abs(lis[0]))
    exit()

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

print(ans)

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


l = []

for i in range(N - K + 1):
    left = i
    right = i + K - 1
    
    ans1 = abs(x[left]) + abs(x[right] - x[left])
    
    ans2 = abs(x[right]) + abs(x[right] - x[left])
    tmp_ans = min(ans1, ans2)
    l.append(tmp_ans)

ans = min(l)
print(ans)

Solution (Python):
import numpy as np
N,K = map(int,input().split())
X = np.array(input().split(), dtype=np.int64)

INF = 10 ** 18
neg = np.concatenate([-X[X <= 0][::-1], np.full(N+1,INF,dtype=np.int64)])[:K]
pos = np.concatenate([X[X > 0], np.full(N+1,INF,dtype=np.int64)])[:K]

answer = INF

answer = min(answer, neg[-1])

answer = min(answer, pos[-1])
if K >= 2:
  
  answer = min(answer, (neg[:K-1] * 2 + pos[K-2::-1]).min())
  
  answer = min(answer, (pos[:K-1] * 2 + neg[K-2::-1]).min())

print(answer)

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















def cost(i):
    l = i
    r = i+K
    if x[l] >= 0:
        return abs(x[r-1])
    elif x[r-1] <= 0:
        return abs(x[l])
    else:
        if abs(x[l]) < abs(x[r-1]):
            return 2*abs(x[l]) + abs(x[r-1])
        else:
            return 2*abs(x[r-1]) + abs(x[l])
ans = 1e12
for i in range(0, N-K+1):
    ans = min(ans, cost(i))
print(ans)

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

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

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


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

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

Solution (Python):

import math
import bisect
import numpy as np
N, k = map(int,input().split())
X = list(map(int,input().split()))

 
ans = float('inf')
for i in range(N-k+1):
    a = X[i]
    b = X[i+k-1]
    
    if np.sign(a)*np.sign(b) >= 0:
        ans = min(ans, max(abs(a), abs(b)))
        
    else:
        ans = min(ans, abs(a)+abs(b)+min(abs(a), abs(b)))
        
print(ans)

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

ans = 10 ** 9
for l in range(0, n - k + 1):
  r = l + k - 1
  a = abs(x[r]) + abs(x[r] - x[l])
  b = abs(x[l]) + abs(x[r] - x[l])
  ans = min(ans, min(a, b))

print(ans)

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

inf = 10**12
z = len(x)

if n == 1 and k == 1:
    print(0)
    exit()

for i in range(len(x)):
    if x[i] >= 0:
        z = i
        break

r = [inf] * (n - z)
l = [inf] * z

for i in range(n-z):
    r[i] = x[z+i]

for i in range(z):
    l[i] = abs(x[z-i-1])


tl = inf
if len(l) < k:
    tl = inf
else:
    tl = abs(l[k-1])

if z == n-1:
    print(tl)
    exit()


tr = inf
if len(r) < k:
    tr = inf
else:
    tr = abs(r[k-1])
    


t_arr = []
for i in range(1,k):
    if i-1 >= len(r):
        pass
    else:
        if k-i-1 >= len(l):
            pass
        else:
            t1 = r[i-1]
            t2 = l[k-i-1]
            t_arr.append(t1*2 + t2*2 - max(t1,t2))
tc = inf
if len(t_arr) != 0:
    tc = min(t_arr)
else:
    tc = inf
print(min(tl,tr,tc))

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

ans =[]
if arr[0]>0:
    ans.append(arr[K-1])

if arr[-1]<0:
    ans.append(abs(arr[-1*(K)]))

for i in range(N-K+1):
    if arr[i]*arr[i+K-1]<=0:
        left =2*abs(arr[i])+abs(arr[i+K-1])
        right=2*abs(arr[i+K-1])+abs(arr[i])
        ans.append(min(left,right))

print(min(ans))

Solution (Python):
def main():
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    m,p = [],[]
    for i in range(n):
        if x[i]<0:
            m.append(x[i])
        else:
            p.append(x[i])
    M,P = len(m),len(p)
    ans = float('inf')
    for i in range(min(M+1,k+1)):
        t = 0
        if i>0:
            t += 2*abs(m[M-i])
        r = k-i
        if r==0:
            if t//2<ans:
                ans = t//2
        elif r<=P:
            t += p[r-1]
            if t<ans:
                ans = t
    for i in range(min(P+1,k+1)):
        t = 0
        if i>0:
            t += 2*p[i-1]
        r = k-i
        if r==0:
            if t//2<ans:
                ans = t//2
        elif r<=M:
            t += abs(m[M-r])
            if t<ans:
                ans = t
    print(ans)

if __name__ == "__main__":
    main()

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

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

Solution (Python):

from bisect import bisect_left
import sys
input = sys.stdin.readline  
N,K = map(int, input().split())
x = list(map(int, input().split()))
x_shift =[i +abs(x[0]) for i in x]
 
dist =[]
for i in range(N-K+1):
    d_end =x_shift[i+K-1] -x_shift[i] 
    d_left =abs(x[i])
    d_right =abs(x[i+K-1])
    dist.append(d_end +min(d_left,d_right))
print(min(dist))

Solution (Python):

from bisect import bisect_left
import sys
input = sys.stdin.readline  
N,K = map(int, input().split())
x = list(map(int, input().split()))
x_shift =[i +abs(x[0]) for i in x]
bis_0 =bisect_left(x,0)
 
d_end =[0]*(N-K+1)
d_left =0
d_right =0
dist =[]
for i in range(N-K+1):
    d_end =x_shift[i+K-1] -x_shift[i] 
    d_left =abs(x[i])
    d_right =abs(x[i+K-1])
    dist.append(d_end +min(d_left,d_right))
print(min(dist))

Solution (Python):
if __name__ == '__main__':

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

	ans = float("inf")

	for i in range(k+1):
		if i+k > n:
			break

		left = A[i]
		right = A[i+k-1]

		
		tmp2 = abs(left) + abs(right - left)

		
		tmp3 = abs(right) + abs(left - right)

		ans = min(ans,min(tmp2,tmp3))

	print(ans)

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

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

Solution (Python):



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











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

    
    ans = min(ans, ans_tmp)

print(ans)

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

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

Solution (Python):
import bisect
import math

N,K = map(int, input().split())
X = list(map(int, input().split()))
a = bisect.bisect_left(X,0)
X.insert(a,0)

ans = math.inf

for i in range(max(0,a+K-N), min(a+1,K+1)):
    ans = min(ans, -X[a-i]*2+X[a+K-i], -X[a-i]+X[a+K-i]*2)

print(ans)

Solution (Python):
N,K = list(map(int,input().split()))
x = list(map(int,input().split()))
minTime = 10000000000
for i in range(N-K+1):
    time = 0
    minValue = x[i]
    maxValue = x[i+K-1]
    if minValue < 0 and maxValue < 0:
        time = minValue * -1
    elif minValue >= 0 and maxValue >= 0:
        time = maxValue
    elif minValue < 0 and maxValue >= 0:
        leftTime = minValue * -1
        rightTime = maxValue
        if leftTime < rightTime:
            time = leftTime * 2 + rightTime
        else:
            time = leftTime + rightTime * 2
    if time < minTime:
        minTime = time
print(minTime)

Solution (Python):
N,K = (int(T) for T in input().split())
X = [int(T) for T in input().split()]
Cost = [0]*(N-K+1)
for T in range(0,N-K+1):
    Left  = X[T]
    Right = X[T+K-1]
    Cost[T] = min(abs(Left)+abs(Left-Right),abs(Right)+abs(Right-Left))
print(min(Cost))

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
Xs = list(mapint())

pos = []
neg = []

for x in Xs:
    if x>=0:
        pos.append(x)
    else:
        neg.append(-x)

neg = neg[::-1]

ans = 10**18
if len(pos)>=K:
    ans = min(ans, pos[K-1])
if len(neg)>=K:
    ans = min(ans, neg[K-1])
lenp = len(pos)
lenn = len(neg)
if lenn>0:
    for i in range(min(lenp, K)):
        if K-(i+1)<=lenn:
            ans = min(ans, pos[i]*2+neg[K-i-2])
if lenp>0:
    for i in range(min(lenn, K)):
        if K-(i+1)<=lenp:
            ans = min(ans, neg[i]*2+pos[K-i-2])
print(ans)

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

ans = float('inf')
for l in range(N - K + 1):
    r = l + K - 1

    if X[l] < 0 and X[r] <~ 0:
        ans = min(ans, abs(X[l]))
    elif X[l] >= 0 and X[r] > 0:
        ans = min(ans, X[r])
    else:
        ans = min(ans, abs(X[l]) + X[r] + min(abs(X[l]), X[r]))


print(ans)

Solution (Python):
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 10**18
    for i in range(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)
        

    
if __name__ == '__main__':
    main()

Solution (Python):




































































def calculation(lines):
    N, K = list(map(int, lines[0].split()))
    values = list(map(int, lines[1].split()))
    
    P = N-K+1
    difs = list()
    for p in range(P):
        LL = values[p]
        r = values[K-1+p]
        rev = 0
        if LL*r < 0:
            rev = min(abs(LL), abs(r))
            dif = (r-LL) + rev
        else:
            dif = max(abs(r), abs(LL))
        difs.append(dif)
    return [min(difs)]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['5 3', '-30 -10 10 20 50']
        lines_export = [40]
    if pattern == 2:
        lines_input = ['3 2', '10 20 30']
        lines_export = [20]
    if pattern == 3:
        lines_input = ['1 1', '0']
        lines_export = [0]
    if pattern == 4:
        lines_input = ['8 5', '-9 -7 -4 -3 1 2 3 4']
        lines_export = [10]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode



def main():
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

for n in range(N-K+1):
  A+=[X[n+K-1]-X[n]+min(abs(X[n]),abs(X[n+K-1]))]

print(min(A))

Solution (Python):

def solve():
    n, k = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    ans = float('inf')
    for i in range(n):
        if i + 1 >= k:
            l = a[i - k + 1]
            r = a[i]
            ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)

solve()

"""
5 6
4 7 9 3 4
"""

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

m = a[0:bisect.bisect_left(a,0)]+[0]
p = [0]+a[bisect.bisect_left(a,0):]






ans = 10**9
for i in range(max(k-len(m)+1,0),min(len(p),k+1)):
    ans = min(ans,2*p[i]+abs(m[-(k-i+1)]))
    
for i in range(max(k-len(p)+1,0),min(len(m),k)):
    ans = min(ans,2*abs(m[-i-1])+abs(p[k-i]))
    
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):
    sm=x[i]
    sp=x[i+k-1]
    if sp<0:
        ans=min(-sm,ans)
    elif 0<sm:
        ans=min(ans,sp)
    else:
        ans=min(ans,min(sm*-2+sp,sp*2-sm))
print(ans)

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

p = []
m = []
for i in a:
    if i >= 0:
        p.append(i)
    else:
        m.insert(0,-i)
p.insert(0,0)
m.insert(0,0)

ans = 10**9
for i in range(k+1):
    if i < len(p) and k-i < len(m):
        d_p = p[i]
        d_m = m[k-i]
        d = min(d_p,d_m)*2 + max(d_p,d_m)
        ans = min(ans,d)

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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