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):

"""
Created on Sat Aug 25 18:21:57 2018
ABC107C
@author: maezawa
"""

n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**9
for l in range(n-k+1):
    r = l+k-1
    ans1 = abs(x[l]) + abs(x[r]-x[l])
    ans2 = abs(x[r]) + abs(x[r]-x[l])
    ans0 = min(ans1, ans2)
    if ans > ans0:
        ans = ans0
print(ans)

Solution (Python):
def c_candles(N, K, X):
    ans = float('inf')
    
    for left in range(N):
        right = left + K - 1
        if right >= N:  
            break
        
        ans = min(ans, abs(X[left]) + abs(X[right] - X[left]))
        ans = min(ans, abs(X[right]) + abs(X[right] - X[left]))
    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))

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

ans = float('inf')
for left in range(N - K + 1):
    lv, rv = X[left], X[left+K-1]
    ans = min(ans, rv - lv + min(abs(rv), abs(lv)))
print(ans)

Solution (Python):
N, K = map(int, input().split())
arr = [int(x) for x in input().split()]
ps = []
ms = []
for i in range(N):
    if arr[i] == 0:
        K -= 1
    elif arr[i] < 0:
        ms.append(abs(arr[i]))
    elif arr[i] > 0:
        ps.append(arr[i])
ms = ms[::-1]
if K <= 0:
    ans = 0
else:
    ans = ps[K-1] if K <= len(ps) else 10**18
    ans = min(ans, ms[K-1]) if K <= len(ms) else ans
if len(ps) > 0 and len(ms) > 0:
    for i in range(len(ms)):
        if K-(i+1)-1 < len(ps):
            ans = min(ans, ms[i]*2 + ps[K-(i+1)-1])
    for i in range(len(ps)):
        if K-(i+1)-1 < len(ms):
            ans = min(ans, ps[i]*2 + ms[K-(i+1)-1])
print(ans)

Solution (Python):
import numpy as np

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

result = np.inf
for i in range(len(x) - k +1):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))

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

result = []
for i in range(N-(K-1)):
    start = i
    end = i + (K -1)
    result.append(abs(x_list[start]) + abs(x_list[end] - x_list[start]))
    result.append(abs(x_list[end]) + abs(x_list[end] - x_list[start]))
print(min(result))

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

maxvalue=100000000000000000000000









for i in range(n-k+1):
    low=X[i]
    high=X[i+k-1]
    if high*low<0:
        low=abs(low)
        high=abs(high)
        value=min(low,high)*2+max(low,high)
    else:
        low=abs(low)
        high=abs(high)
        value=max(low,high)
    
    if value<maxvalue:
        maxvalue=value
print(maxvalue)

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

s = 0
e = K - 1

re = min(abs(x[s]) + abs(x[s] - x[e]), abs(x[e]) + abs(x[e] - x[s]))
while e < N:
    tmp = min(abs(x[s]) + abs(x[s] - x[e]), abs(x[e]) + abs(x[e] - x[s]))
    re = min (re, tmp)
    s += 1
    e += 1
    
print(re)

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

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

plus = [0]
minus = [0]

for x_ in x:
    if x_ < 0:
        minus.append(x_ * -1)
    if x_ > 0:
        plus.append(x_)
    if x_ == 0:
        K -= 1

plus.sort()
minus.sort()


ans = 10000000000

for i in range(len(plus)):
    if 0 <= K-i < len(minus):
        ans = min(ans,plus[i]*2 +minus[K-i])
for i in range(len(minus)):
    if 0 <= K-i < len(plus):
        ans = min(ans,minus[i]*2 +plus[K-i])
print(ans)

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

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


list1=[]
for i in range(N-K+1):
    if x[i+K-1]<=0:
        list1.append(x[i]*(-1))
    elif x[i]>=0:
        list1.append(x[i+K-1])
    else:
        list1.append(x[i+K-1]*2-x[i])
        list1.append(x[i+K-1]-x[i]*2)

print(min(list1))

Solution (Python):
import sys

N, K = [int(_) for _ in input().split()]
list = [int(_) for _ in input().split()]

l1 = []
l2 = []
t = 0
for i in list:
    if i < 0:
        l1.append(i)
    elif i == 0:
        t = 1
    else:
        l2.append(i)
l1 = [abs(_) for _ in l1]
l1.sort()

if t == 1 and K == 1:
    print(0)
    sys.exit()

K = K - t
a = float('inf')
b = float('inf')
if len(l1) >= K:
    a = l1[K-1]
if len(l2) >= K:
    b = l2[K-1]

c = float('inf')
d = float('inf')
for i in range(len(l2)):
    if len(l1)-1 >= K-i-2 >= 0:
        c_new = l2[i] * 2 + l1[K-i-2]
        c = min(c, c_new)
for j in range(len(l1)):
    if len(l2)-1 >= K-j-2 >= 0:
        d_new = l1[j] * 2 + l2[K-j-2]
        d = min(d, d_new)

print(min(a, b, c, d))

Solution (Python):
def run(n, k, x):
    
    
    
    
    
    ret = abs(x[0]) + abs(x[0] - x[k-1])
    for i in range(n-k+1):
        ri = i
        li = i + k - 1
        tmp = min(abs(x[ri]) + abs(x[ri] - x[li]), abs(x[li]) + abs(x[ri] - x[li]))
        ret = min(ret, tmp)
        '''
        if x[ri] > x[i]:
        elif x[ri] == x[i]:
            if x[ri+1] < x[i-1]:
                ri = i-k+1
                li = i
        else:
            if x2[li] <= 0:
                return x[ri]
            elif x2[ri] >= 0:
                return x[li]
            elif x[ri] > x[li]:
                return x[ri] + 2*x[li]
            else:
                return 2*x[ri] + x[li]
    if x2[li] <= 0:
        return x[ri]
    elif x2[ri] >= 0:
        return x[li]
    elif x[ri] > x[li]:
        return x[ri] + 2*x[li]
    else:
        return 2*x[ri] + x[li]
        '''
    return ret


def read_line():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    return (n, k, x)


def main():
    n, k, x = read_line()
    print(run(n, k, x))


if __name__ == '__main__':
    main()

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

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

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

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

left = 0
right = left+(K-1)
time = 10**9
while right < N:
    d = x[right]-x[left]
    time = min(time, d+abs(x[right]), d+abs(x[left]))
    left += 1
    right += 1
print(time)

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

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

time = 0
answer = float("inf")

for i in range(n - k + 1):
  x_min = x[i]
  x_max = x[k + i - 1]
  
  if x_min * x_max < 0:  
    time = x_max - x_min + min(x_max, -x_min)
  else:
    time = max(abs(x_max), abs(x_min))
  answer = min(answer, time)

  
print(answer)

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

ans=0
for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i])

    if i==0 or ans>tans:
        ans=tans

for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i+K-1])

    if ans>tans:
        ans=tans

print(ans)

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

MIN = float("inf")
for i in range(N-K+1):
	l = i
	r = i + K - 1
	cur1 = abs(x[l]) + abs(x[r]-x[l])
	cur2 = abs(x[r]) + abs(x[r]-x[l])

	if cur1 < MIN:
		MIN = cur1
	if cur2 < MIN:
		MIN = cur2
print(MIN)

Solution (Python):
N, K = map(int, input().split())
x = [i for i in map(int, input().split())]
i = 0
min_time =5e8
while i < N-K+1:
    if x[i]<0 and x[i+K-1]<0:
        time = -x[i]
    if x[i]<0 and x[i+K-1]>=0:
        time1 = -2*x[i] + x[i+K-1]
        time2 = -x[i] + 2*x[i+K-1]
        if time1< time2:
            time = time1
        else:
            time = time2
    if x[i] >= 0 and x[i+K-1] >=0:
        time = x[i+K-1]
    if time < min_time:
        min_time = time
    i += 1
print(min_time)

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

print(min([min(abs(cans[n]), abs(cans[n  + (K-1)]))  + abs(cans[n  + (K-1)] - cans[n]) for n in range(N-K+1)]))

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

if 0 in X:
    i = X.index(0)
    del X[i]
    N -= 1
    K -= 1

C=N
for i, x in enumerate(X):
    if x >= 0:
        C = i
        break

A = []

if K == 0:
    print(0)
else:
    if C>=K:
        A.append(-X[C-K])
        
    if N-C>=K:
        A.append(X[C+K-1])
        
    for k in range(max(C+K-N, 1), min(C+1, K)):
        A.append(-2*X[C-k] + X[C-k-1+K])
        
    for k in range(max(K-C, 1), min(N-C+1,K)):
        A.append(2*X[C+k-1] - X[C+k-K])
        
    print(min(A))

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

x_minus = [a for a in X if a < 0]
x_plus = [a for a in X if a >= 0]

ans = float("inf")
for i in range(K+1):
    if i == 0:
        if len(x_plus) >= K:
            dis = x_plus[K - 1]
            ans = min(ans, dis)
        else:
            continue
    elif 1 <= i <= K - 1:
        if len(x_minus) >= i and len(x_plus) >= K - i:
            dis = 2 * abs(x_minus[-i]) + x_plus[K - i - 1]
            ans = min(ans, dis)
            dis = 2 * x_plus[K - i - 1] + abs(x_minus[-i])
            ans = min(ans, dis)
        else:
            continue
    elif i == K:
        if len(x_minus) >= K:
            dis = abs(x_minus[-K])
            ans = min(ans, dis)


print(ans)

Solution (Python):


N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])



res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])


if len_plus != 0:
    for i_minus in range(len_minus):
        if K-i_minus-1 <= len_plus:
            res.append(abs(-X_minus[-i_minus-1])*2 + X_plus[K-i_minus-2])

if len_minus >= K:
    if len_plus > 0:
        if X_plus[0]==0:
            res.append(-X_minus[-K+1])
    else:
        res.append(-X_minus[-K])

print(min(res))

Solution (Python):

import numpy as np

def solve():
    N, K = map(int, input().split())
    C = np.array(list(map(int,input().split())))
    z = (C<0).sum()
    D = set()
    for i in range(N-K+1):
        l = -min(C[i],0)
        r = max(C[i+K-1],0)
        d = 2*l+r if l < r else 2*r+l
        D.add(d)
    return str(min(D))

if __name__ == '__main__':
    print(solve())

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

arr = map(int,raw_input().split())

ans = float("inf")
import bisect
t = bisect.bisect_right(arr,0)
arr.insert(t,0)
for i in range(n+1):
	if i-k > -1:
		ans = min(ans, abs(arr[i-k]) + abs(arr[i]) + min(abs(arr[i-k]), abs(arr[i])))

print ans

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

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

a = 0
while a != n:
	if x[a] >= 0:
		break
	a += 1
for i in range(max(0,a-k),n-k+1):
	if x[i+k-1] <= 0:
		ans = min(ans,-x[i])
	elif x[i] >= 0:
		ans = min(x[i+k-1],ans)
	else:
		ans = min(x[i+k-1]-x[i]+min(-x[i],x[i+k-1]),ans)
print(ans)

Solution (Python):
import numpy as np

def main():
    n, k = map(int,input().split())
    lst = list(map(int,input().split()))
    arr = np.array(lst)

    if np.any(arr == 0):
        if k == 1:
            print(0)
            exit()
        k -= 1

    plus = arr[np.where(arr>0)]
    len_plus = len(plus)
    plus = np.insert(plus,0,0)
    minus = arr[np.where(arr<0)][::-1]
    len_minus = len(minus)
    minus = np.insert(minus,0,0)

   
    
    
    
    

    anslist = []
    ans = 100000000000000000000


    for i in range(k):
        if len_plus < k:
            try:
                ans = plus[len_plus-i] + (abs(minus[i+(k-len_plus)])*2)
                anslist.append(ans)
            except:
                break

        elif len_plus >= k:
            try:
                ans = plus[k-i] + (abs(minus[i])*2)
                
                anslist.append(ans)
            except:
                break

    for i in range(k):
        if len_minus < k:
            try:
                ans = abs(minus[len_minus-i]) + (plus[i+(k-len_minus)]*2)
                anslist.append(ans)
            except:
                break
        else:
            try:
                ans = abs(minus[k-i]) + (plus[i]*2)
                anslist.append(ans)
            except:
                break


    
    print(min(anslist))



if __name__ == "__main__":
        main()

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

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

if x == -1:
	print(li[-K] * -1)

elif not x :
	print(li[K-1])

else:
	score = float("inf")
	for i in range(K):
		if i == K-1:
			try:
				score = min(li[x+i], score)
			except:
				pass
			try:
				if x-i-1 >= 0:
					score = min(li[x-i-1] * -1, score)
			except:
				pass
		else:
			try:
				if x-(K-1-i) >= 0:
					score = min(li[x+i] * 2 + li[x-(K-1-i)] * -1, score)
			except:
				pass
			try:
				if x-i-1 >= 0:
					score = min(li[x-i-1] * -2 + li[x+K-2-i], score)
			except:
				pass
	print(score)

Solution (Python):
import sys

N, K = map(int, sys.stdin.readline().strip().split(" "))
X = map(int, sys.stdin.readline().strip().split(" "))

isZero = False
zeroInd = 0
for x in X:
    if x == 0:
        isZero=True
    if x < 0:
        zeroInd+=1
    else:
        break

zeroInd-=1

if isZero:
    X.remove(0)
    K -= 1

if K == 0:
    print(0)
    sys.exit(0)

ans = []

for k in range(K+1):
    a = -1
    if zeroInd + k < len(X):
        a = X[zeroInd+k]

    b = -1
    if len(X) > zeroInd+k-K+1 >= 0:
        b = X[zeroInd+k-K+1] * -1

    if b != -1 and k == 0:
        ans.append(b)
    elif a != -1 and k == K:
        ans.append(a)
    elif a != -1 and b != -1:
        ans.append(a*2+b)
        ans.append(b*2+a)

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

Solution (Python):


def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))
    ans = float('inf')

    for i in range(n - k + 1):
        if x_list[i] >= 0 and x_list[i+k-1] >= 0:
            ans = min(ans, x_list[i+k-1])
        elif x_list[i] <= 0 and x_list[i+k-1] <= 0:
            ans = min(ans, -x_list[i])
        else:
            ans = min(ans, min(-x_list[i] * 2 + x_list[i+k-1] , -x_list[i] + x_list[i+k-1] * 2))
    print(ans)

if __name__ == '__main__':
    main()

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

arr = list(map(int, input().split()))
zero_ind = len(arr)
for i, e in enumerate(arr):
    if e > 0:
        zero_ind = i
        break
    elif e == 0:
        zero_ind = i + 1
        arr = arr[:i] + arr[i+1:]
        K -= 1
        break
if K == 0:
    print(0)
else:
    
    min_val = (10**8) * K
    for i in range(1,K+1):
        
        if zero_ind + i - 1 >= len(arr) or zero_ind - (K - i) < 0:
            continue
        if i == K:
            val = arr[zero_ind + i - 1]
        else:
            val = 2 * arr[zero_ind + i - 1] - arr[zero_ind - (K - i)]
        if min_val > val:
            min_val = val

    
    for i in range(1,K+1):
        
        if zero_ind + K - i - 1 >= len(arr) or zero_ind - i < 0:
            continue
        if i == K:
            val = -1 * arr[zero_ind - i]
        else:
            val = arr[zero_ind + K - i - 1] - 2 * arr[zero_ind - i]
        if min_val > val:
            min_val = val

    print(min_val)

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

lst = []
for i in range(N-K+1):
    if x[i] <= 0 and x[i+K-1] <= 0:
        lst.append(abs(x[i]))
    elif x[i] >= 0 and x[i+K-1] >= 0:
        lst.append(x[i+K-1])
    else:
        d1 = abs(x[i]) + x[i+K-1] - x[i]
        d2 = x[i+K-1] + x[i+K-1] - x[i]
        lst.append(min(d1, d2))
print(min(lst))

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

Solution (Python):
import numpy
na = lambda: list(map(int, input().split()))

n, k = na()
x = na()
l = []
m, p = 0, 0
for i in range(n-1):
    hoge = x[i]
    if hoge < 0:
        m += 1
    l.append(abs(x[i+1] - hoge))
p = n-m
l = numpy.cumsum([0]+l)

ans = 10 ** 10

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

print(ans)

Solution (Python):
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


if __name__ == "__main__":
    n, k = LI()
    X = LI()
    l_count = sum((x <= 0 for x in X))
    r_count = n - l_count

    if l_count == 0:
        print(X[k-1])
    elif r_count == 0:
        print(abs(X[-k]))
    else:
        times = []
        for i in range(l_count):
            try:
                left = X[l_count - i - 1]
                right = X[l_count + k - i - 2]
                times.append(abs(2 * left) + abs(right))
                times.append(abs(left) + abs(2 * right))
            except:
                continue
        print(min(times))

Solution (Python):

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

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(abs(x[len(x)-k]))
    elif x[0] > 0:
        print(x[k-1])    
    else:
        ass = 10 ** 10
        ans = 10**10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            
            else:
                if i - k + 1 >= 0:
                    ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        if r-k >= 0:
            ans = abs(x[r-k])
        ass = min(ass,ans)
        print(ass)

Solution (Python):
from bisect import bisect_left
n, k = map(int, input().split())
a = list(map(int, input().split()))
t = bisect_left(a, 0, 0, len(a))
ans = float("inf")
if t + k <= n:
    ans = a[t + k - 1]
if t >= k:
    ans = min(ans, -a[t - k])
for i in range(max(0, t - k + 1), min(t, n-k+1)):
    ans = min(ans, a[i+k-1]-2*a[i], 2*a[i+k-1]-a[i])
print(ans)

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

X_pos = [x for x in X if x >= 0]
X_neg = [x for x in X if x <  0]

X_neg.reverse()

l_pos = len(X_pos)
l_neg = len(X_neg)

R = []

if l_neg == 0:
    R.append(X_pos[K-1])
elif l_pos == 0:
    R.append(-1 * X_neg[K-1])
else:
    for k in range(K):
        if k < l_pos and K-k-2 < l_neg:
            R.append(2 * X_pos[k] - X_neg[K-k-2])
        elif k >= l_pos:
            break
    for k in range(K):
        if k < l_neg and K-k-2 < l_pos:
            R.append(-2 * X_neg[k] + X_pos[K-k-2])
        elif k >= l_neg:
            break

print(min(R))

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

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

if (0 in X):
  K = K-1
  N = N-1
  X.remove(0)

trial = []

if X != []:
  for i in range(N-K+1):
    if (X[i] < 0) and (X[K+i-1] < 0):
      trial.append(abs(X[i]))
    if (X[i] > 0 ) and (X[K+i-1] > 0):
      trial.append(abs(X[K+i-1]))
    go = abs(X[i])*2+abs(X[K+i-1])
    back = abs(X[i])+abs(X[K+i-1])*2
    trial.append(min(go,back))
  
if trial != []:
  print(min(trial))
else:
  print(0)

Solution (Python):
import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
idx = bisect.bisect_left(x,0)
now = max(0,idx-k)
mini =1e10
for i in range(now,n-k+1):
    if(x[i]*x[i+k-1]>=0):
        mini=min(max(abs(x[i]),abs(x[i+k-1])),mini)
        continue
    mini = min(mini,abs(x[i])+x[i+k-1]+min(abs(x[i]),x[i+k-1]))
print(mini)

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

cur = 0
dist = 1000000000
if K == 1:
    dist = min(x)
elif K == N:
    if (x[0] <= 0) and (x[N-1] > 0):
        dist = min(abs(x[0]) * 2 + x[N-1], abs(x[0]) + x[N-1] * 2)
    elif x[N-1] <= 0:
        dist = abs(x[0])
    else: 
        dist = x[N-1]
else:
    for i in range(N-K+1):
        cur_dist = 0
        if (x[i] <= 0) and (x[i+K-1] > 0):
            cur_dist = min(abs(x[i]) * 2 + x[i+K-1], abs(x[i]) + x[i+K-1] * 2)
        elif x[i+K-1] <= 0:
            cur_dist = abs(x[i])
        else: 
            cur_dist = x[i+K-1]
        if dist >= cur_dist:
            dist = cur_dist
        if x[i] > 0:
            break

print(dist)

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

k = []


k0 = abs(X[0])
for n in range(0+1, K):
    k0 += X[n] - X[n-1]
k.append(k0)
for i in range(1, len(X)-K+1):
    ki = k[i-1] - (X[i] - X[i-1]) + (X[i+K-1] - X[i+K-2]) - abs(X[i-1]) + abs(X[i])
    k.append(ki)


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

l.append(l0)
for i in range(1, len(X)-K+1):
    
    li = l[i-1] - (X[N-i] - X[N-i-1]) + (X[N-K-i+1] - X[N-K-i]) - abs(X[N-i]) + abs(X[N-i-1])
    
    l.append(li)
print(min(min(k), min(l)))

Solution (Python):
import bisect
n, k = map(int, input().split())
x = list(map(int, input().split()))
z = bisect.bisect_left(x, 0)
a = sorted(map(abs, x[:z]))
b = x[z:]
ans = float("inf")
if a and b:
    for i in range(min(k, len(a))):
        if k - i - 2 >= len(b):
            continue
        ans = min(ans, min(a[i], b[k - i - 2]) * 2 + max(a[i], b[k - i - 2]))
    print(ans)
elif a:
    print(a[k - 1])
else:
    print(b[k - 1])

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

Y = []
Z = []

for x in X:
    if x == 0:
        Y.append(0)
        Z.append(0)
    elif x < 0:
        Y.append(abs(x))
    else:
        Z.append(x)

ans = sum(Y) + sum(Z)
Y.reverse()

for i, y in zip(range(1, len(Y)+1), Y):
    if i <= k and k - i <= len(Z):
        ans = min(ans, y + (0 if k - (i + 1) < 0 else 2 * Z[k-(i+1)]))

for i, z in zip(range(1, len(Z)+1), Z):
    if i <= k and k - i <= len(Y):
        ans = min(ans, (0 if k - (i + 1) < 0 else 2 * Y[k-(i+1)]) + z)

print(ans)

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

d = []
for n in range(N-K+1):
  l = abs(cans[n]) + abs(cans[n  + (K-1)] - cans[n])
  r = abs(cans[n  + (K-1)]) + abs(cans[n  + (K-1)] - cans[n])
  d.append(min(l,r))

print(min(d))

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

minp = 0
if X[N-1] <0:
    minp = N-1
else:
    while X[minp] < 0:
        minp += 1

lidx = 0
if minp > K:
    lidx = minp - K
ridx = lidx + K - 1
left = X[lidx]
right = X[ridx]
mind = right - left + min(abs(left), abs(right))

while ridx < N and left <= minp:
    left = X[lidx]
    right = X[ridx]
    mind = min(mind, right - left + min(abs(left), abs(right)))
    lidx += 1
    ridx += 1

print(mind)

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

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

Solution (Python):
n, k = map(int, input().split())
cs   = list(map(int, input().split()))
t    = -1
i    = 0

while i + k <= len(cs) and cs[i] < 0:
    if cs[i + k - 1] < 0:
        x = abs(cs[i])
    else:
    	x = cs[i + k - 1] - cs[i] + min(abs(cs[i + k - 1]), abs(cs[i]))

    if t == -1 or x < t:
        t = x

    i += 1

if t == -1 or i + k <= len(cs) and cs[i + k - 1] < t:
    t = cs[i + k - 1]

print(t)

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

temp = [None for i in range(k)]

m = float("inf")

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

print(m)

Solution (Python):


INF = float('inf')


def inpl(): return list(map(int, input().split()))


N, K = inpl()
X = inpl()

A = [0]
B = []
for i in range(N):
    if X[i] >= 0:
        A.append(X[i])
    else:
        B.append(-X[i])
B.append(0)
B = B[::-1]








ans = INF
for i in range(K+1):
    cost = INF
    j = K - i
    if i > len(A) - 1 or j > len(B) - 1:
        continue

    if i == K:
        cost = A[i]

    elif j == K:
        cost = B[j]

    else:
        cost_rl = 2 * A[i] + B[j]
        cost_lr = A[i] + 2 * B[j]
        cost = min(cost_rl, cost_lr)

    ans = min(ans, cost)

print(ans)

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


for i in range(N-K+1):
    if x[i] <= 0 and x[i+K-1] <= 0:
        sa = -x[i]
        if minsa > sa:
            minsa = sa
            
            
    if x[i] < 0 and x[i+K-1] > 0:
        sa = min(x[i + K-1], -x[i])*2 + max(x[i + K -1], -x[i])
        if minsa > sa:
            minsa = sa
            
            
    if x[i] >= 0 and x[i+K-1] >= 0:
        sa = x[i+K-1]
        if minsa > sa:
            minsa = sa
            
            
print(minsa)

Solution (Python):
import copy

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

t_list = []

if 0 in x_list:
    zero_ind = x_list.index(0)

    for start_ind in range(max(0, zero_ind - k), min(zero_ind + 1, n - k + 1)):
        
        minv = x_list[start_ind]
        maxv = x_list[start_ind + k - 1]
        

        if minv < 0 < maxv:
            t = min(2 * abs(minv) + maxv, abs(minv) + 2 * maxv)

        else:
            t = max(abs(minv), abs(maxv))

        t_list.append(t)
        

else:
    x_list2 = copy.copy(x_list)
    x_list2 = x_list2 + [0]
    x_list2 = sorted(x_list2)
    zero_ind = x_list2.index(0)

    for start_ind in range(max(0, zero_ind - k), min(zero_ind + 1, n - k + 1)):
        
        minv = x_list[start_ind]
        maxv = x_list[start_ind + k - 1]
        

        if minv < 0 < maxv:
            t = min(2*abs(minv) + maxv, abs(minv) + 2 * maxv)

        else:
            t = max(abs(minv), abs(maxv))

        t_list.append(t)


print(min(t_list))

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

if x[0] > 0:
    x.insert(0, 0)
    K+=1
    N+=1
elif x[-1] < 0:
    x.append(0)
    K+=1
    N+=1
else:
    for i in range(N-1):
        if x[i] < 0 and x[i+1]> 0:
            x.insert(i+1, 0)
            K += 1
            N+=1
            break

ans = float("inf")
for i in range(N):
    j = i + K-1
    if j >= N:
        break
    if x[i]*x[j] > 0:
        continue
    ans = min(ans, -2*x[i]+x[j], -x[i]+2*x[j])
    
print(ans)

Solution (Python):
import math
N,K = map(int,input().split())
xs = input().split()
res_list = []
for i in range(N-K+1):
    if int(xs[i]) >0:
        diff = int(xs[i+K-1])
        res_list.append(diff)
        break
    elif int(xs[i]) <= 0 and int(xs[i+K-1]) > 0:
        diff = 2 * min(abs(int(xs[i])),abs(int(xs[i+K-1]))) + max(abs(int(xs[i])),abs(int(xs[i+K-1])))
    elif int(xs[i+K-1]) <= 0:
        diff = abs(int(xs[i]))
    res_list.append(diff)
print(min(res_list))

Solution (Python):
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000000000000000

if N == K:
    left = problem[0]
    right = problem[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left)
else:
    for i in range(N - K + 1):
        left = problem[i]
        right = problem[i+K-1]
        if left * right >= 0:
            if abs(right) > abs(left):
                if ans > abs(right):
                    ans = abs(right)
            else:
                if ans > abs(left):
                    ans = abs(left)
        else:
            if abs(right) > abs(left):
                if ans > (2 * abs(left) + abs(right)):
                    ans = (2 * abs(left) + abs(right))
            else:
                if ans > 2*abs(right)+abs(left):
                    ans = 2*abs(right)+abs(left)

print(ans)