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()))
ans = float("inf")
for i in range(N - K + 1):
    

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

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

INF = float('inf')
ans = INF

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

print(ans)

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

def culclen(l,r):
  return abs(X[r]-X[l])+min(abs(X[l]),abs(X[r]))

ans=10**9
for x in range(len(X)-K+1):
  ans=min(ans,culclen(x,x+K-1))
print(ans)

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

def culclen(l,r):
  if X[l]*X[r]>=0:
    return max(abs(X[l]),abs(X[r]))
  else:
    ldis=abs(X[l])
    rdis=abs(X[r])
    return min(ldis,rdis)*2+max(ldis,rdis)

ans=10**9
for x in range(len(X)-K+1):
  ans=min(ans,culclen(x,x+K-1))
print(ans)

Solution (Python):

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

Solution (Python):

import sys
import numpy as np

def solve(N: int, K: int, x: "List[int]"):
    x = np.array(x)
    score = 10**9
    x_s = sorted(x[x < 0], reverse=True)
    x_plus = x[x >= 0]
    x_len_plus = len(x_plus)
    x_len_minus = len(x_s)
    for right in range(min(K + 1, x_len_plus + 1)):
        left = min(K - right, x_len_minus)
        if right + left == K:
            if x_len_plus > 0:
                rpath = x_plus[right - 1]
            else:
                rpath = 0
            if x_len_minus > 0:
                lpath = abs(x_s[left - 1])
            else:
                lpath = 0
            score = min(score, rpath + lpath + min(rpath, lpath))

    print(score)



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

if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial
inf = 10**9

n,k = [int(x) for x in stdin.readline().rstrip().split()]

li = list(map(int,stdin.readline().rstrip().split()))
mi = 10**10
for i in range(n-k+1):
    mi2 = min(abs(li[i+k-1])+abs(li[i+k-1]-li[i]),abs(li[i])+abs(li[i+k-1]-li[i]))
    mi = min(mi,mi2)

print(mi)

Solution (Python):
import sys

readline = sys.stdin.buffer.readline


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

    if N == 1:
        print(abs(X[0]))
        sys.exit()

    dm = 10**9
    for i in range(0, N-K+1):
        d = X[i+K-1] - X[i] + min(abs(X[i+K-1]), abs(X[i]))
        if d < dm:
            dm = d

    print(dm)


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):
    
    l = i
    
    r = i + K - 1
    
    d1 = X[r] - X[l]
    
    d2 = min(abs(X[l]), abs(X[r]))
    ans = min(ans, d1 + d2)

print(ans)

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

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

print(ans)

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

for n in range(N-K+1):
    t = abs(X[n]-X[n+K-1]) + min(abs(X[n]), abs(X[n+K-1]))
    Y.append(t)

if N - K == 0:
    ans = abs(X[0] - X[-1]) + min(abs(X[0]), abs(X[-1]))
    print(ans)
else:
    print(min(Y))

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

ans=float("inf")

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

print(ans)

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

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

ans = float("inf")
for i in range(n - k + 1):
    min_pos = xl[i]
    max_pos = xl[i+k-1]
    if min_pos * max_pos < 0:
        tmp = min(2*abs(min_pos) + abs(max_pos), abs(min_pos) + 2*abs(max_pos))
    else:
        tmp = max(abs(min_pos), abs(max_pos))
    if tmp < ans:
        ans = tmp
        
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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


ans = INF
idx = bisect(x, 0)
for i in range(idx-K, idx+1):
	if 0 <= i <= N-K:
		if i == idx-K:
			ans = min(ans, abs(x[i]))
		elif i == idx:
			ans = min(ans, x[i+K-1])
		else:
			ans = min(ans, 2*abs(x[i])+x[i+K-1], abs(x[i])+2*x[i+K-1])
print(ans)

Solution (Python):
import sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

Solution (Python):
import bisect
n,k = map(int,input().split())
xl = list(map(int,input().split()))
x1 = xl[:bisect.bisect_right(xl,0)][::-1]
x2 = xl[bisect.bisect_right(xl,0):]
m = 10**10
if len(x1) >= k:
  m = min(m,-x1[k-1])
if len(x2) >= k:
  m = min(m,x2[k-1])
for i in range(k-1):
  try:
    m = min(m,x2[i]-2*x1[k-i-2],2*x2[i]-x1[k-i-2])
  except:
    continue
print(m)

Solution (Python):

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

ans = 10**11
for left in range(N-K+1):
    right = left+K-1

    left, right = X[left], X[right]
    if left < 0 and right > 0:
        dist = min(abs(left), abs(right)) * 2 + max(abs(left), abs(right))
    else:
        dist = max(abs(left), abs(right))

    
    ans = min(ans, dist)

print(ans)

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

fst = 0
las = k-1
dis = []
while las < n:
	dis.append(x[las]-x[fst] + min(abs(x[las]), abs(x[fst])))
	fst += 1
	las += 1
print(min(dis))

Solution (Python):




import sys



import bisect










def main():
    n, k, *x = (int(i) for i in sys.stdin.read().split())

    minimums = []
    for i in range(n - k + 1):

        left = x[i]
        right = x[i + k - 1]
        mi = min(abs(left) + abs(right - left), abs(right) + abs(left - right))
        minimums.append(mi)
    
    ans = min(minimums)
    print(ans)


if __name__ == "__main__":
    
    main()

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):
	ans = min(ans, min(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):
N, K = map(int, input().split())
x = list(map(int, input().split()))
import bisect
id = bisect.bisect_left(x,0)
ans=10**10
if id == N:
  print(abs(x[N-K]))
  exit(0)
for i in range(max(0,(K+id)-N),min(id+1,K)):
  right = x[K+id-1-i]
  left=0
  if i>0:
    left =x[id-i]
  tmp=min(abs(left)*2+right,right*2+abs(left)) 
  ans=min(tmp,ans)
print(ans)

Solution (Python):
n, k =  map(int, input().split())
X = list(map(int, input().split()))
c = float("inf")
for i in range(n-k+1):
  if X[i] < 0 and X[i+k-1] >= 0:
    temp = min(2*(-X[i])+X[i+k-1], 2*X[i+k-1]+(-X[i]))
  else:
    temp = max(abs(X[i]), abs(X[i+k-1]))
  c = min(c, temp)
print(c)

Solution (Python):
def main():
    n,k = map(int,input().split()) 
    x = list(map(int,input().split()))
    m=10**11
    for i in range(n-k+1):
        if x[i]>0:
            m=min(x[i+k-1],m)
        elif x[i+k-1]<0:
            m=min(m,-x[i])
        else:
            m=min(m,x[i+k-1]-x[i]+x[i+k-1],x[i+k-1]-2*x[i])
    print(m)
    
if __name__ == "__main__":
    main()

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

Solution (Python):
words = lambda t : list(map(t, input().split()))
n,k = words(int)
a = words(int)
ans = 10 ** 9 
for i in range(n-k+1):
    l = a[i]
    r = a[i+k-1]
    
    if l < 0 and r <= 0:
        ans = min(ans, abs(l))
    elif l < 0 and r > 0:
        ans = min(ans, min(abs(l), r) * 2 + max(abs(l), r))
    else: 
        ans = min(ans, r)

print(ans)

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

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


def cal_num(a, b):
    if x[a] * x[b] < 0:
        return min(abs(x[a]), abs(x[b])) * 2 + max(abs(x[a]), abs(x[b]))
    else:
        return max(abs(x[a]), abs(x[b]))

for i in range(n-k+1):
    c = cal_num(i, i+k-1)
    if num >= c:
        num = c

print(num)

Solution (Python):
import sys


def solve():
    sys.setrecursionlimit(2000)
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if len(x) == 1:
        print(abs(sum(x)))
    else:
        mt = 10 ** 10
        for i in range(k-1, n):
            if x[i] * x[i-k+1] < 0:
                mt = min(mt, abs(x[i]) + abs(x[i-k+1]) + min(abs(x[i]), abs(x[i-k+1])))
            else:
                mt = min(mt, max(abs(x[i]), abs(x[i-k+1])))
        print(mt)


if __name__ == '__main__':
    solve()

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

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

print(m)

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

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

print(ans)

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

print(min(ress))

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
INF = float('inf')
ans = INF
l, r = 0, K-1
for i in range(N-K+1):
    a, b = X[l+i], X[r+i]
    A, B = abs(a), abs(b)
    if a*b > 0:
        ans = min(ans, max(A, B))
    else:
        ans = min(ans, A + B + (A if A < B else B))

print(ans)

Solution (Python):
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    l.sort()
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**20 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])

for a in range(1,len(left)+1):
    b=k-a 
    
    if b>len(rr):
        
        continue 
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

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

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

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

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

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

ans=1000000000
for i in range(N-K+1):
  tmp1=abs(x[i])+abs(x[i+K-1]-x[i])
  tmp2=abs(x[i+K-1])+abs(x[i+K-1]-x[i])
  tmp=min(tmp1,tmp2)
  ans=min(tmp,ans)

print(ans)

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

N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))

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

Solution (Python):
import sys
inputs = sys.stdin.readlines()
 
N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))
 
if N == 1:
    print(X[0])
else:
    ans = 10 ** 9
    for i in range(N-K+1):
        x1, x2 = X[i], X[i+K-1]
        if x1 <= 0 and x2 <= 0:
            ans = min(ans, -x1)
        elif x1 <= 0 and 0 < x2:
            if -x1 < x2:
                ans = min(ans, -2*x1 + x2)
            else:
                ans = min(ans, -x1 + 2*x2)
        else:
            ans = min(ans, x2)
            break
    print(ans)

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

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
k = float("INF")
for i in range(N-K+1):
	l = X[i]
	r = X[i+K-1]
	k = min(k, abs(l)+abs(r-l), abs(r)+abs(r-l))
print(k)

Solution (Python):
import sys
from bisect import bisect_left as bl
input = sys.stdin.readline
N, K = map(int, input().split())
x = list(map(int, input().split()))
dp = [float("inf")] * (K + 2)
s = bl(x, 0)
l = 0
r = K - 1
if r + 1 < s:
  l = s - 1 - r
  r = s - 1
def calc(l, r):
  if x[l] * x[r] < 0:
    return min(abs(x[l]), abs(x[r])) * 2 + max(abs(x[l]), abs(x[r]))
  else:
    return max(abs(x[l]), abs(x[r]))
dp[0] = calc(l, r)
for i in range(1, K + 2):
  l += 1
  r += 1
  if l > s + 1 or r >= N:
    break
  dp[i] = calc(l, r)
print(min(dp))

Solution (Python):


import bisect

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

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    
    m=min(0,X[i])
    M=max(0,X[i+K-1])
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    if i+K==N:
        break
    
print(ans)

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

    ans = 10 ** 10
    for i in range(N-K+1):
        L = x[i]
        R = x[i+K-1]
        if L <= 0 and R <= 0:
            tmp = abs(L)
        elif L >= 0 and R >= 0:
            tmp = R
        else:
            tmp = abs(L) + (R-L)
            if tmp > 2 * R + abs(L):
                tmp = 2 * R + abs(L)
        if tmp < ans:
            ans = tmp
    print(ans)

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



ans = 10 ** 13
if K == 1:
    print(candle[0])
    exit()


for i in range(N-K+1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i+K-1])
    
    if tmp < ans:
        ans = tmp


for i in range(N-1,K-2,-1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i-K+1])
    
    if tmp < ans:
        ans = tmp

print(ans)

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

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

ans = 2 * 10**9

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

print(ans)

Solution (Python):
import sys
n,k=map(int,input().split())
x=list(map(int,input().split()))
left=[]
right=[]
origin=0
for u in x:
    if u<0:
        left.append(abs(u))
    elif u==0:
        origin+=1
    else:
        right.append(u)
left.sort()
k-=origin
if k==0:
    print(0)
    sys.exit()
r=len(right)
l=len(left)
ans=float("inf")
if k<=r:
    ans=right[k-1]
if k<=l:
    ans=min(ans,left[k-1])
for i in range(k-1):
    if i<r and k-i-2<l:
        y=right[i]+left[k-i-2]+min(right[i],left[k-i-2])
        ans=min(ans,y)
print(ans)

Solution (Python):
N, K = map(int, input().split())
ary = list(map(int, input().split()))
ary2 = sorted(ary+[0])
zero = ary2.index(0)
k = [abs(i - zero) if i < zero else ((i + 1) - zero)for i in range(N)]
ans = float("Inf")
for i in range(N - K + 1):
    if k[i] <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i + (K - 1)]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i + (K - 1)])
for i in range(N-1, K-2, -1):
    if k[i] <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i - (K-1)]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i - (K-1)])
print(ans)

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

ret = float('inf')
for i in range(N-K+1) :
    l = x[i]
    r = x[i+K-1]
    if l < 0 and r < 0 :
        ret = min(ret, -l)
    elif l > 0 and r > 0 :
        ret = min(ret, r)
    else :
        m, M = min(-l, r), max(-l, r)
        ret = min(ret, m * 2 + M)
print(ret)

Solution (Python):
INF = 10**9
N, K = map(int, input().split())
s = [int(c) for c in input().split()]

ans = INF
for i in range(N-K+1):
  l = i
  r = l+K-1
  ans = min(ans, abs(s[l])+abs(s[r]-s[l]), abs(s[r])+abs(s[r]-s[l]))

print(ans)

Solution (Python):
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

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

import bisect

zero_index = bisect.bisect_left(x,0)
start_index = max(0,zero_index-k)
x = x[start_index:zero_index+k]

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

print(answer)

Solution (Python):
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce



def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



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

def calcCost(a, b):
    mina = abs(a)+abs(a-b)
    minb = abs(b)+abs(b-a)

    return min(mina, minb)

ans = sys.maxsize
for i in range(0, n-k+1, 1):
    tmpans = calcCost(data[i], data[i+k-1])
    ans = min(ans, tmpans)

print(ans)

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

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
def time(i):
    if X[i]*X[i+K-1]<0:
        return min(abs(X[i]),abs(X[i+K-1]))+abs(X[i])+abs(X[i+K-1])
    else:
        return max(abs(X[i]),abs(X[i+K-1]))
r=10**9
for i in range(N-K+1):
    r=min(r,time(i))
print(r)

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

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

Solution (Python):
import bisect

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

x_negative = x[:bisect.bisect_left(x,0)]
x_positive = x[bisect.bisect_left(x,0):]


if len(x_positive) == 0:
    print(abs(x_negative[-K]))
    exit()


if x_positive[0] == 0:
    if K == 1:
        print(0)
        exit()
    K -= 1
    del x_positive[0]

t_list = []


if len(x_negative) >= K:
    t_list.append(abs(x_negative[-K]))
if len(x_positive) >= K:
    t_list.append(x_positive[K-1])


for i in range(1, K-1):
    try:
        t_list.append(2*abs(x_negative[-i]) + x_positive[K-i-1])
    except IndexError:
        pass
    
for i in range(1, K-1):
    try:
        t_list.append(2*x_positive[i-1] + abs(x_negative[-(K-i)]))
    except IndexError:
        pass

print(min(t_list))

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

Solution (Python):
import sys

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

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

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

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

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

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

ans = 2*(10**9)
for i in range(n-k+1):
    total_d = x[i+k-1] - x[i]
    zero_d = min(abs(x[i]), abs(x[i+k-1]))
    ans = min(ans, total_d + zero_d)
print(ans)

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

ans = 10000000000

for i in range(N-K+1):
    cost = 0
    cost += min(abs(X[i]),abs(X[i+K-1]))
    cost += X[i+K-1]-X[i]
    if ans > cost:
        ans = cost
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):
    mini=x[i]
    maxi=x[i+k-1]
    if mini*maxi==0:
        a=max(abs(mini),abs(maxi))
        ans=min(ans,a)
    elif mini*maxi<0:
        a=abs(mini)*2+maxi
        b=abs(mini)+maxi*2
        ans=min(ans,a,b)
    else:
        a=max(abs(mini),abs(maxi))
        ans=min(ans,a)
print(ans)

Solution (Python):
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
ans = float("inf")
for i in range(n - k + 1):
    start = i
    end = i + k - 1
    left = abs(x_list[start]) + abs(x_list[end] - x_list[start])
    right = abs(x_list[end]) + abs(x_list[end] - x_list[start])
    ans = min(ans, left, right)
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, min(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 i in range(N-K+1):
    temp = min(abs(x[i]) + abs(x[i+K-1]-x[i]), abs(x[i+K-1]) + abs(x[i]-x[i+K-1]))
    if ans > temp:
        ans = temp

print(ans)

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


if __name__ == '__main__':
    main()

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

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
positive = []
negative = []
cnt_zero = 0
for i in x:
    if i<0:
        negative.append(abs(i))
    elif i==0:
        cnt_zero +=1
    else:
        positive.append(i)
negative.sort()

m = k -cnt_zero
if m==0:
    print(0)
    exit()

ans = float('inf')

for i in range(m):
    if (0<=i<=len(negative)-1) and (0<= m - i -2 <=len(positive)-1):
        ans = min(ans,negative[i]*2+positive[m-i-2])
    if (0<=i<=len(positive)-1) and (0<= m - i -2 <=len(negative)-1):
        ans = min(ans,positive[i]*2+negative[m-i-2])
if m<=len(positive):
    ans = min(ans,positive[m-1])
if m<=len(negative):
    ans = min(ans,negative[m-1])
    
print(ans)

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

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

Solution (Python):
import numpy as np
n,m = [int(i) for i in input().split()]

a = np.array(list(map(int,input().split())))

neg = a[a < 0][::-1]
pos = a[a >= 0]

ans = 1000000000000000000
if len(neg) == 0:
    print(pos[m-1])
    exit()
else:
    for i in reversed(range(len(pos) + 1)):
        if i > m:
            continue
        elif m - i > len(neg):
            break
        if i == m:
            ans = min(ans, pos[i - 1])
        elif i == 0:
            ans = min(ans, abs(neg[m - 1]))
        else:
            ans = min(ans, pos[i-1]*2 + abs(neg[m-i-1]), abs(neg[m-i-1] * 2) + pos[i-1])

print(ans)

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

ans = float('inf')
for i in range(n-k+1):
    l,r = i,i+k-1
    tmp = min(abs(X[l])+abs(X[l]-X[r]),abs(X[r])+abs(X[l]-X[r]))
    ans = min(ans,tmp)
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):
	count = 0	
	left = x[i]
	right = x[i+k-1]
	
	if left < 0 and right >= 0:
		a = abs(left)*2 + right 
		b = right*2 + abs(left)
		count = min(a, b)
		ans = min(ans, count)
	else:
		count = max(abs(x[i]), abs(x[i+k-1]))
		ans = min(ans, count)
		
print(ans)