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())
xN = list(map(int, input().split()))

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

print(ans)

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


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

print(ans)

Solution (Python):
import numpy as np
N, K =map(int, input().split())
X = [int(_) for _ in input().split()]
X2 = np.zeros((N,))
X2 += X
minus = X2[X2 < 0] * (-1)
minus.sort()
plus = X2[X2 >= 0]
lenM, lenP = len(minus), len(plus)

def length(i, j):
    if i == 0:
        return plus[j-1]
    if j == 0:
        return  minus[i-1]
    else:
        LtoR = min(minus[i-1], plus[j-1])
        return plus[j-1] + minus[i-1] + LtoR

minLength = 1000000000
for i in range(0, K+1):
    a, b = i, K-i
    if lenM >= a and lenP >= b :
        minLength = min(minLength, length(a, b))

print(int(minLength))

Solution (Python):
from bisect import bisect_left, bisect_right

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

zero_left = bisect_left(x, 0)
zero_right = bisect_right(x, 0)
m = x[:zero_left]
p = x[zero_right:]
zero_count = zero_right - zero_left
k -= zero_count
if k <= 0:
	print(0)
	exit()

y = m[-k:] + p[:k]
min_time = 1000000000000000000000000
for i in range(len(y)-k+1):
	l, r = y[i], y[i+k-1]
	time = r-l + min(abs(l), abs(r))
	if time < min_time:
		min_time = time

print(min_time)

Solution (Python):




import copy

            
    
def main(n, k, l):
    cost_l = []

    if k == 0:
        print(0)
        return
    
    if k == 1:
        t = [abs(l[i]) for i in range(len(l)) ]
        print(min(t))
        return
    
    for i in range(n - k + 1):
        mae = i
        ato = i + k - 1
        if l[mae] * l[ato] < 0:
            cost_l.append(min(- l[mae] * 2 + l[ato], - l[mae] + l[ato] * 2))
        elif l[mae] >= 0:
            cost_l.append(l[ato])
        elif l[ato] <= 0:
            cost_l.append(-l[mae])
            
    print(min(cost_l))

    
    
if __name__ == '__main__':
    try:
        n, k = list(map(int,input().strip().split(" ")))
        l = list(map(int,input().strip().split(" ")))
        main(n, k, l)
    except EOFError:
        pass

Solution (Python):

n, k = [ int(i) for i in input().split()]

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

x_plus = sorted([x for x in x_list if x > 0 ])
x_minus = sorted([x for x in x_list if x < 0 ], reverse=True)
zero_x = [x for x in x_list if x == 0 ]

if len(x_plus) == 0 and len(x_minus) == 0:
    print(0)
elif len(x_plus) == 0:
    print(abs(x_minus[k-1-len(zero_x)]))
elif len(x_minus) == 0:
    print(abs(x_plus[k-1-len(zero_x)]))
else:
    max_x = max(x_plus)
    min_x = min(x_minus)

    cost_list = []
    for i in range(len(x_plus)):
        cost = 0
        cnt = i+1 + len(zero_x)
        if len(x_minus)+cnt < k or cnt > k:
            continue
        if cnt == k:
            cost += abs(x_plus[i])
        else:
            cost+=abs(x_minus[k-cnt-1])*2
            cost+=abs(x_plus[i])
        cost_list.append(cost)

    for i in range(len(x_minus)):
        cost = 0
        cnt = i+1 + len(zero_x)
        if len(x_plus)+cnt < k or cnt > k:
            continue
        if cnt == k:
            cost += abs(x_minus[i])
        else:
            cost+=abs(x_plus[k-cnt-1])*2
            cost+=abs(x_minus[i])
        cost_list.append(cost)


    print(min(cost_list))

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

min_result = max(abs(x[0])*2, abs(x[-1])*2, abs(x[-1] - x[0])*2)

for i in range(0, len(x)-k+1):
    if x[i+k-1] <= 0:
        min_result = min(min_result, -x[i])
    elif x[i] < 0 and x[i+k-1] > 0:
        min_result = min(min_result, -x[i]*2+x[i+k-1], -x[i]+x[i+k-1]*2)
    else:
        min_result = min(min_result, x[i+k-1])

print(min_result)

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

if a[N-1]<0:
    a=list(reversed(list(map(abs,a))))
if N == K:
    print(a[N-1]-a[0]+min(abs(a[0]),abs(a[N-1])))
else:
    ans=10000000000
    for i in range(N-K):
        ans = min(ans,a[K+i-1]-a[i]+min(abs(a[i]),abs(a[K+i-1])))
    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):
    if a[i] <= 0 and a[i+k-1] <= 0:
        ans = min(ans,abs(a[i]))
    elif a[i] <= 0 and 0 <= a[i+k-1]:
        ans = min(ans,2*abs(a[i])+a[i+k-1],abs(a[i])+2*a[i+k-1])
    elif 0 <= a[i]:
        ans = min(ans,a[i+k-1])
if ans == float("inf"):
    print(0)
else:
    print(ans)

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

if zero == 0:
    print(x[K-1])
    exit(0)

if zero+K <= N:
    ans = min(ans, x[zero+K-1])
if zero >= K:
    ans = min(ans, -x[zero-K])

if zero != N:
    for k in range(K-1):
        if 0 <= K+zero-k-2 < N and 0 <= zero-k-1 < N: 
            ans = min(ans,2*x[K+zero-k-2]-x[zero-k-1],x[K+zero-k-2]-2*x[zero-k-1])

print(ans)

Solution (Python):

"""
Created on Wed Jul 18 00:01:41 2018

@author: Yuya
"""

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

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

n = len(z)
c = 0

for i in range(len(z)):
    if z[i]<0:
        c+=1

d = len(z)-c

mi = 100000000000000000

if c<k:
    x = 0
    y = k-1
    mi = 100000000000000000
    ope = n-k
else:
    x = c-k
    y = c-1
    
    
for i in range(n):
    if y < n:
        if z[x]<0 and z[y]<0:
            k1 = abs(z[x])
            k2 = 0
        elif z[x]>0 and z[y]>0:
            k2 = abs(z[y])
            k1 = 0
        else:
            k1 = abs(z[x])
            k2 = abs(z[y])
            
        if k1<k2:
            tmp=k1*2+k2
        else:
            tmp=k2*2+k1

        if tmp<mi:
            mi = tmp
        x+=1
        y+=1
    else:
        break

print(mi)

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

ans = 10**10
for i in range(0, n-k+1):
    mid = (x[i] + x[i+k-1]) // 2
    if x[i + k-1] <= 0:
        ans = min(ans, abs(x[i]))
    elif x[i] >= 0:
        ans = min(ans, abs(x[i+k-1]))
    elif mid < 0:
        ans = min(ans, abs(x[i]) + abs(x[i+k-1]) * 2)
    else:
        ans = min(ans, abs(x[i]) * 2 + abs(x[i+k-1]))

print(ans)

Solution (Python):
n,k = map(int, raw_input().split())
lis = map(int, raw_input().split())
if 0 in lis:
    k-=1
flag=0

if k==0:
    print 0
    flag=1
if n==1 and flag==0:
    print abs(lis[0])
    flag=1
if flag==0:
    if lis[0]>0: 
        print lis[k-1]
    elif lis[n-1]<0: 
        print abs(lis[n-k])
    else:
        idx = -1
        for i in range(0, n-1):
            if lis[i]<0 and lis[i+1]>=0:
                idx = i
                break
        
        lol =[]
        x=0
        lol.append(abs(lis[idx]))
        for i in range(idx-1,-1,-1):
            lol.append(lol[x]+abs(lis[i]))
            x+=1
        
        lol1=[]
        x=0
        lol1.append(lis[idx+1])
        for i in range(idx+2,n,1):
            lol1.append(lol1[x]+(lis[i]))
            x+=1
        

        lol=lol[::-1]
        lol3=[]
        for i in lol:
            lol3.append(i)
        for i in lol1:
            lol3.append(i)
        
        
        ans=10000000000000000000000000000000000000000000
        lol=lol[::-1]


        lol =[]
        x=0
        lol.append(abs(lis[idx]))
        for i in range(idx-1,-1,-1):
            lol.append(abs(lis[i]))
            x+=1
        
        lol1=[]
        x=0
        lol1.append(lis[idx+1])
        for i in range(idx+2,n,1):
            lol1.append((lis[i]))
            x+=1
        
        
        if k<=len(lol):
            ans=min(ans, lol[k-1])
        if k<=len(lol1):
            ans=min(ans, lol1[k-1])
        for i in range(1,len(lol)+1):
            fromright=k-i
            if fromright>len(lol1):
                continue
            val=2*lol[i-1]
            val+=(lol1[fromright-1])
            
            if val<ans:
                ans=val
        
        for i in range(1,len(lol1)+1):
            fromleft=k-i
            if fromleft>len(lol):
                continue
            val=2*lol1[i-1]
            val+=(lol[fromleft-1])
            
            if val<ans:
                ans=val
        print ans

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

xr =  [0] + [_ for _ in x if _ >= 0]
xl = [-_ for _ in x if _ < 0] + [0]
xl.reverse()

xr = xr[:k+1]
xl = xl[:k+1]

if len(xl) > len(xr):
  xr, xl = xl, xr
  
xl, xr = xl[k - (len(xr)-1):], xr[k - (len(xl)-1):]

xl.reverse()

print(min(min(2*i+j, 2*j+i) for i, j in zip(xl, xr)))

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




if x[0] <= 0 <= x[-1]:
    ans = []
    for i in range(N-K+1):
        left = x[i]
        right = x[i+K-1]
        if left <= 0 <= right:
            cost = abs(right - left) + min(abs(right),abs(left))
            ans.append(cost)
        else:
            pass
    print(min(ans))

else:
    ans = []
    for i in range(N-K+1):
        left = x[i]
        right = x[i+K-1]
        cost = abs(right - left) + min(abs(right),abs(left))
        ans.append(cost)
    print(min(ans))

Solution (Python):
a, b = map(int, input().split())
s = list(map(int,input().split( )))
c=a-b
e = 10000000000
if a==b:
  e=s[b-1]-s[0]+min(abs(s[b-1]),abs(s[0]))
else:
  for j in range(0,c+1):
    if e>s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j])):
      e=s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j]))
print(e)

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

min = 10e8

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

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

zerofrag = 0
zerobig = []
zerosmall = []

for i in a:
    if i == 0:
        zerofrag =1
    elif i > 0:
        zerobig.append(i)
    else:
        zerosmall.append(abs(i))

if k == 1 and zerofrag == 1:
    print(0)
else:
    if zerofrag == 1:
        k = k -1

    zerobig.sort()
    zerosmall.sort()
    
    kouho = []
    if len(zerobig) >=k:
        kouho.append(zerobig[k-1])
    if len(zerosmall) >=k:
        kouho.append(zerosmall[k-1])
        
    for ii in range(len(zerosmall)):
        if k - (ii) > len(zerobig):
            continue
        else:
            try:
                kouho.append(zerosmall[ii] * 2 + zerobig[k - ii - 2])
            except:
                continue
    for jj in range(len(zerobig)):
        if k - (jj ) > len(zerosmall):
            continue
        else:
            try:
                kouho.append(zerobig[jj] * 2 + zerosmall[k - jj - 2])
            except:
                continue
    
    if zerofrag == 1:
        k = k + 1
    if kouho == [] :
        print(min( (max(zerosmall)*2 + max(zerobig)) ,  max(zerobig) * 2 + max(zerosmall)) )
    else:
        print(min(kouho))

Solution (Python):

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


for i_snk, xi in enumerate(x):
    if xi >= 0: break


min_dist = (x[-1] - x[0]) * K
for left in range(K + 1):
    right = K - left
    
    
    if i_snk - left < 0: continue
    elif right + i_snk > N: continue
    
    left_x = x[i_snk - left]
    right_x = x[i_snk + right - 1]
    tocorner = int(min(math.fabs(left_x), math.fabs(right_x)))
    side2side = right_x - left_x
    
    if tocorner + side2side < min_dist: min_dist = tocorner + side2side

print(min_dist)

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

ans = 300000000
for i in range(0, n-k+1):
    if x[i] >= 0:
        t = x[i+k-1]
    elif x[i+k-1] <= 0:
        t = -x[i]
    else:
        t = min(-x[i], x[i+k-1]) + x[i+k-1] - x[i]
    ans = min(t, ans)

print(ans)

Solution (Python):
n,k=[int(x) for x in input().split()]
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi ==0:
        k-=1
    else:
        x.append(xi)
x=sorted(x)
ans=10**10

zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

Solution (Python):
from sys import stdin, stdout

input = lambda: stdin.readline()
write = stdout.write

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

    org = 0 - X[0]
    x = [0] * N
    for i, n in enumerate(X):
        x[i] = n + org

    ans = float('inf')
    for i in range(N - K + 1):
        dist = x[i+K-1] - x[i] + min(abs(org - x[i]), abs(x[i+K-1] - org))
        ans = min(ans, dist)
    print(ans)

main()

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
xl = []
xr = []
for i in x:
    if i > 0:
        xr.append(i)
    elif i < 0:
        xl.append(i)
xl = [0] + list(reversed(xl))
xr = [0] + xr

ans = 10 ** 9
k = K
if 0 in x:
    k -= 1

for i in range(max(0, k - (len(xl) - 1)), min(len(xr), k + 1)):
    
    ans = min(ans, 2 * xr[i] + abs(xl[k - i]))
for i in range(max(0, k - (len(xr) - 1)), min(len(xl), k + 1)):
    ans = min(ans, 2 * abs(xl[i]) + abs(xr[k - i]))
print(ans)

Solution (Python):
n,k=map(int, input().split())
x=list(map(int, input().split()))
zi=0
for xi in x:
    if xi>=0:
        break
    zi+=1

xm=[0]
if zi<len(x) and x[zi]==0:
    xp=[]
else:
    xp=[0]

xm.extend(reversed(x[:zi]))
xp.extend(x[zi:])

ans=float('inf')
for i in range(k):
    if i<len(xm) and k-i<len(xp):
        ans=min(xm[i]*2*(-1) + xp[k-i], ans)
    if i<len(xp) and k-i<len(xm):
        ans=min(xp[i]*2 + xm[k-i]*(-1), ans)
if len(x)==1 and x[0]==0:
    ans=0
print(ans)

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

import bisect
zero = bisect.bisect_left(X, 0)

left = max(zero - K, 0)
ans = float('inf')
while 1:
    right = left+K-1
    if left > zero + 1 or len(X) <= right: break
    lv, rv = X[left], X[right]
    if lv * rv > 0:
        ans = min(ans, max(abs(rv), abs(lv)))
    else:
        ans = min(ans, min(rv*2 - lv, rv - lv*2))
    left += 1

print(ans)

Solution (Python):
import sys

a = input().split()
n = int(a[0])
k = int(a[1])

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

mini = sys.maxsize
for i in range(n - k +1):
	if xlist[i] <= 0 and xlist[i + k - 1] <= 0:
		mini = min(mini, (-xlist[i]))
	elif xlist[i] <= 0 and xlist[i + k - 1] >= 0:
		mini = min(mini,(min(-2*xlist[i] + xlist[i + k - 1], -xlist[i] + 2 * xlist[i + k - 1])))
	elif xlist[i] >= 0 and xlist[i + k - 1] >= 0:
		mini = min(mini, (xlist[i + k - 1]))
		
print (mini)

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


minx = 300000000
if k==1:
    x = list(map(abs,x))
    print(min(x))
else:
    for idx in range(n-k+1):
        if x[idx]*x[idx+k-1] < 0 :
            sumx = min(abs(x[idx]),abs(x[idx+k-1])) + abs(x[idx])+abs(x[idx+k-1])
        else :
            sumx = max(abs(x[idx]),abs(x[idx+k-1]))
        if sumx < minx:
            minx = sumx
        

    print(minx)

Solution (Python):




def main():
    n, k = map(int, input().split())
    c = list(map(int, input().split()))

    ans = 10 ** 15
    for i in range(n-k+1):
        if c[i+k-1] <= 0:
            t = abs(c[i])
        elif c[i] < 0 < c[i+k-1]:
            t = min(abs(c[i]), abs(c[i+k-1])) + abs(c[i+k-1]-c[i])
        else:
            t = c[i+k-1]

        ans = min(ans, t)

    print(ans)


if __name__ == '__main__':
    main()

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

ans = - v[0] * 2 + v[-1] * 2

for i in range(n - k + 1):
    s = v[i]
    g = v[i + k - 1]
    if s > 0 or g < 0:
        if s < 0:
            m = - s
        else:
            m = g
    else:
        m = min(-s * 2 + g, -s + 2 * g)
    ans = min(ans, m)
print(ans)

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

import bisect

x=bisect.bisect_right(C,0)

if x>=K:
    ANS=-(C[x-K])
else:
    ANS=10**10


for i in range(1,min(x+1,K+1)):
    if x-i+K-1>=N:
        continue

    
    
    elif ANS>=C[x-i+K-1]-2*C[x-i]:
        ANS=C[x-i+K-1]-2*C[x-i]





CC=[None]*N
for i in range(N):
    CC[i]=-C[i]

CC.reverse()


x=bisect.bisect_right(CC,0)

if x>=K:
    if ANS>-(CC[x-K]):
        ANS=-(CC[x-K])





for i in range(1,min(x+1,K)):
    if x-i+K-1>=N:
        continue

    
    
    elif ANS>=CC[x-i+K-1]-2*CC[x-i]:
        ANS=CC[x-i+K-1]-2*CC[x-i]







    

print(ANS)

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

x.insert(0, 0)

minimum = 2**31

for left in range(1, N-K+2):
    right = left + K - 1
    lefttime = abs(x[left]) + abs(x[right] - x[left])
    righttime = abs(x[right]) + abs(x[right] - x[left])
    minimum = min(minimum, min(lefttime, righttime))
    
print(minimum)

Solution (Python):
from itertools import accumulate

import sys
sdin = sys.stdin.readline

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

x_0 = [xi - x[0] for xi in x]
x0_cum = list(accumulate(x_0))


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

    ans = min(ans_cand, ans)
    
    
print(ans)

Solution (Python):
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

N, K = getMN()
candles = getlist()

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


print(ans)

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

def findMinPath(nums,k):
    if 0 in nums:
        k-=1
    if k==0:
        return 0
    left=[];right=[]
    for n in nums:
        if n<0:
            left.append(abs(n))
        elif n>0:
            right.append(n)
    left[:]=left[::-1]
    left.insert(0,0)
    right.insert(0,0)

    lsize=len(left);rsize=len(right)
    res=max(2*left[-1]+right[-1],2*right[-1]+left[-1])
    for i in range(k+1):
        j=k-i
        if i<lsize and k-i<rsize:
            tmpMax=min(2*left[i]+right[j],2*right[j]+left[i])
            res=min(res,tmpMax)
    return res

print(findMinPath(moves,k))

Solution (Python):
n,k = list(map(int,input().split()))
d = list(map(int, input().split()))
d_m = []
d_p = [i for i in d if i >= 0 or d_m.append(i)]
ret = -1
for i in range(k+1):
  if len(d_p) >= i and len(d_m) >= k-i:
    dp_t = d_p[i-1] if i > 0 else 0
    dm_t = -1 * d_m[-(k-i)] if k-i != 0 else 0
    if dp_t == 0:
      ret_t = dm_t
    elif dm_t == 0:
      ret_t = dp_t
    else:
      ret_t = dp_t + dm_t + min(dp_t, dm_t)

    if ret > ret_t or ret < 0:
      ret = ret_t
print(ret)

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

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

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

zero = len(xs)
for i in range(len(xs)):
    if xs[i] >= 0:
        zero = i
        break

dic_positive = {0: 0}
dic_negative = {0: 0}
for i in range(len(xs) - zero):
    dic_positive[i + 1] = xs[zero + i]

for i in range(zero):
    dic_negative[i + 1] = abs(xs[zero - i - 1])


init_max = 1000000000000000
candidate = init_max
for i in range(k + 1):
    if dic_negative.get(i) is not None and dic_positive.get(k - i) is not None:
        candidate = min(
            candidate,
            dic_negative[i] + dic_negative[i] + dic_positive[k - i],
            dic_negative[i] + dic_positive[k - i] + dic_positive[k - i]
        )

print(candidate)

Solution (Python):
n,k = map(int,input().split())
row = list(map(int,input().split()))
p = 10000000000000000
i = 0
j = 1
while i + k <= n:
  if(row[i]*row[i+k-1] < 0):
    l = abs(row[i])
    r = abs(row[i+k-1])
    ma = max(l,r)
    mi = min(l,r)
    t = mi*2 + ma

    if(t < p):
      p = t
  elif(row[i] < 0):
    t = abs(row[i])
    if(t < p):
      p = t
  else:
    t = row[i+k-1]
    if(t < p):
      p = t
  i += 1
print(p)

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

min_cost = 10 ** 20

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

Solution (Python):
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    x = [int(x) for x in stdin.readline().rstrip().split()]
    x1 = [-i for i in x if i < 0][::-1]
    x2 = [i for i in x if i >= 0]
    x1 = [0] + x1
    x2 = [0] + x2
    result = 10000000000
    for l in range(min(K + 1, len(x1))):
        r = K - l
        if len(x2) <= r:
            continue
        tmp = x1[l] * 2 + x2[r]
        result = min(result, tmp)
    for r in range(min(K + 1, len(x2))):
        l = K - r
        if len(x1) <= l:
            continue
        tmp = x1[l] + x2[r] * 2
        result = min(result, tmp)
    print(result)


if __name__ == '__main__':
    main()

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

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

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

m = min(x[i] - x[i-K+1] + min(abs(x[i]), abs(x[i-K+1])) for i in range(K-1, N))
print(m)

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

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

plus = -1
for i in range(n):
    if a[i] > 0:
        plus = i
        break
    plus = n    
record = 10**10
for i in range(k+1):
    if i>plus or k-i>n-plus:
        continue
    if i == 0:
        record = a[plus+k-i-1]
    elif i==k:
        r = -a[plus-i]
        if r<record:
            record = r
    else:
        u  = a[plus+k-i-1]
        v = -a[plus-i]
        r1 = 2*u+v
        r2 = 2*v+u
        if r1<record:
            record = r1
        if r2<record:
            record = r2
            
print(record)

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

x1 = []
x2 = []
for i in range(N):
    if x[i] >= 0:
        x1.append(x[i])
    else:
        x2.append(-1 * x[i])
x2 = x2[::-1]

N1 = len(x1)
N2 = len(x2)

ans = float('inf')

if N1 >= K:
    ans = min(ans, x1[K - 1])
if N2 >= K:
    ans = min(ans, x2[K - 1])

for i in range(K - 1):
    if i < N1 and K - i - 2 < N2:
        d1 = 2 * x1[i] + x2[K - i - 2]
        d2 = x1[i] + 2 * x2[K - i - 2]
        ans = min(ans, d1, d2)
print(ans)

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

Solution (Python):
import sys

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

minus_x = []
plus_x = []

for x in X:
    if(x < 0):
        minus_x.append(x)
    elif(x > 0):
        plus_x.append(x)
    else:
        N -= 1
        K -= 1

n_minus = len(minus_x)
n_plus = len(plus_x)

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

ans = sys.maxsize
for i in range(K + 1):
    m = i
    p = K - i
    if(m > n_minus or p > n_plus):
        continue

    if(p == 0):
        tmp = -1 * minus_x[-1 * m]
    elif(m == 0):
        tmp = plus_x[p - 1]
    else:
        t_m = -1 * minus_x[-1 * m]
        t_p = plus_x[p - 1]
        tmp = min(t_m * 2 + t_p, t_m + t_p * 2)

    if(tmp < ans):
        ans = tmp

print(ans)

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

mn = 1000000000
for i in range(N - K + 1):
	x1 = X[i]
	x2 = X[i + K - 1]
	if x1 >= 0:
		m = x2
	elif x2 <= 0:
		m = - x1
	else:
		m = x2 - x1 + min(x2, -x1)

	if mn > m:
		mn = m

print(mn)

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

minti=abs(X[0])*2+abs(X[n-1])*2

for i in range(n-k+1):
    le=X[i]
    ri=X[i+k-1]
    if (le>=0 and ri>=0) or (le<=0 and ri<=0):
        ti=max(abs(le),abs(ri))
    else:
        ti=min(abs(le),abs(ri))*2+max(abs(le),abs(ri))
    minti=min(ti,minti)

print(minti)

Solution (Python):

import sys
input = sys.stdin.readline

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

x_p = [0] + [p for p in x if p > 0]
x_z = x.count(0)
x_n = [(-1) * ng for ng in x if ng < 0]
x_n = [0] + sorted(x_n)

myK = k - x_z

if myK <= 0:
    print(0)
else:
    ans = float('inf')
    for i in range(myK + 1):
        if 0 <= i < len(x_p) and 0 <= myK - i < len(x_n):
            ans = min(ans, x_p[i] + x_n[myK - i] + min(x_p[i], x_n[myK - i]))
    print(ans)

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

ans = 10**9

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

print(ans)

Solution (Python):
import sys
from collections import deque
import copy

from math import *

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    x_list1 = []

    for i in range(N):
        x = int(input_raw[i])
        x_list1.append(x)


    min_dist = sys.maxint
    for i in range(N - K + 1):
        L = i;
        R = i + K - 1
        if x_list1[L] >= 0 and x_list1[R] >= 0:
            dist = x_list1[R]
        elif x_list1[L] <= 0 and x_list1[R] <= 0:
            dist = abs(x_list1[L])
        else:
            dist = min(2*x_list1[R] - x_list1[L], x_list1[R] - 2 * x_list1[L])
        if min_dist > dist:
            min_dist = dist
    print min_dist

if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
lis = list(map(int,input().split()))
if n == 1:
  print(lis[0])
  exit()
ans = []
for i in range(n-k+1):
  if lis[i+k-1] <=0 or 0 <= lis[i]:
    ans.append(max(abs(lis[i+k-1]),abs(lis[i])))
  else:
    ans.append(min(abs(lis[i+k-1]),abs(lis[i]))+abs(lis[i+k-1])+abs(lis[i]))
print(min(ans))

Solution (Python):
N, K = map(int, raw_input().split(' '))
x = map(int, raw_input().split(' '))

m = 1 << 128

for i in range(0, N - K + 1):
    a = x[i]
    b = x[i + K - 1]

    if a < 0 < b:
        if abs(a) > abs(b):
            cost = abs(b) * 2 + abs(a)
        else:
            cost = abs(a) * 2 + abs(b)
    else:
        cost = max(abs(a), abs(b))

    m = min(m, cost)

print m

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

cand = []

for i in range(N - K + 1):
    cand.append(min(abs(X[i]), abs(X[i + K - 1])) + abs(X[i + K - 1] - X[i]))

print(min(cand))

Solution (Python):
import numpy as np
import math


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

X = np.zeros(N, dtype=int)

for n in range(N):
    X[n] = ina[n]


D = np.zeros(N - K+1, dtype=int)

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


        
print(min(D))

Solution (Python):

line = input().split()
n = int(line[0])
k = int(line[1])
x = [int(i) for i in input().split()]


ret = 9**10
for i in range(n-k+1):
    mins = x[i]
    maxs = x[i+k-1]
    if mins<0 and maxs>0:
        if abs(mins)<maxs:
            r = abs(mins)*2+maxs
        else:
            r = maxs*2+abs(mins)
    elif maxs<=0:
        r = abs(mins)
    else:
        r = maxs

    if r<ret:
        ret = r

print(ret)

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

N, K = map(int, raw_input().split())

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

ans = 10 ** 18

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

print ans