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

for i in range(0,N-K+1):
    length=0
    d_flag=0 
    if x[i+K-1]>0 and x[i]<0: 
        if x[i+K-1] > x[i]*(-1):
            d_flag=1
        else:
            d_flag=-1
    
    if d_flag==-1:
        length=abs(x[i+K-1])*2+abs(x[i])
    elif d_flag==1:
        length=abs(x[i+K-1])+abs(x[i])*2
    else:
        if abs(x[i+K-1])>abs(x[i]):
            length=abs(x[i+K-1])
        else:
            length=abs(x[i])
    if i==0:
        len_min=length
    elif length<len_min:
        len_min=length
        
print(len_min)

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):
import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
y=[0]*n
for i in range(n):
    y[i]=-x[n-1-i]


if x[0]>=0:print(x[k-1])
elif x[-1]<=0:print(abs(x[n-k]))
else:
    c=bisect.bisect_left(x,0)
    d=bisect.bisect_right(x,0)
    ans=float('inf')
    if c+k-1<=n-1:
        ans=abs(x[c+k-1])
    if d-k>=0:
        ans=min(ans,abs(x[d-k]))
    for i in range(n-1,c-1,-1):
        if i-k+1>=0:
            ans1=x[i]+2*abs(x[i-k+1])
        
        if ans1<ans:
            ans=ans1
    
if y[0]<0 and y[-1]>0:
    c=bisect.bisect_left(y,0)
    d=bisect.bisect_right(y,0)
    Ans=float('inf')
    if c+k-1<=n-1:
        Ans=abs(y[c+k-1])
    if d-k>=0:
        Ans=min(Ans,abs(y[d-k]))
    for i in range(n-1,c-1,-1):
        if i-k+1>=0:
            Ans1=y[i]+2*abs(y[i-k+1])
        
        if Ans1<Ans:
            Ans=Ans1
    
    print(min(Ans,ans))

Solution (Python):
from itertools import accumulate

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

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

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

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    ma = 10 ** 20
    for i in range(n - k + 1):
        ma1 = abs(x[i])
        ma2 = abs(x[i + k - 1])
        if x[i] * x[i + k - 1] >= 0:
            ma = min(max(ma1, ma2), ma)
        else:
            ma = min(ma1 + ma2 + min(ma1, ma2), ma)
    print(ma)


if __name__ == '__main__':
    slove()

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()

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

min_time = 10000000000000000
for i in range(N - K + 1):
	l = x[i]
	r = x[i + K - 1]
	min_time = min(min_time, min(abs(l), abs(r)) + abs(l - r))

print(min_time)

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):
N, K = map(int, input().split())
x = list(map(int, input().split()))
m = 10000000000000
for i in range(N-K+1):
  x1 = x[i]
  xk = x[i+K-1]
  if x1*xk <= 0: 
    m = min(m, 2*abs(x1)+xk, abs(x1)+2*xk)
  elif x1>0:
    m = min(m, xk)
  else:
    m = min(m, abs(x1))
print(m)

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

zero = bisect_left(x, 0)
rs = n-zero
ls = n-rs
ans = float("INF")

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

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

X = list(map(int, input().split()))
ans = 10**9
start= 0
end=k
n+=1
X.append(0)
X.sort()
for i in range(n-k):
  maximam = X[end]
  minimam = X[start]
  if maximam > 0 and  minimam < 0:
    ans = min(ans, maximam+abs(minimam)+min(maximam, abs(minimam)))
  elif minimam == 0:
    ans = min(maximam, ans)
  elif maximam == 0:
    ans = min(abs(minimam), ans)
  start+= 1
  end+=1
print(ans)

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

Solution (Python):


import sys


tmp = input().split()
n,k = list(map(lambda a: int(a), tmp))
tmp = input().split()
x = list(map(lambda a: int(a), tmp))

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

Solution (Python):
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
mi = x[-1] - x[0] + min(abs(x[-1]),abs(x[0]))
if n == 1:
    print(abs(x[0]))
    exit()
while i <= n - k:
    if mi >= ( x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]),abs(x[i])):
        mi = (x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]),abs(x[i]))
    i += 1
print(mi)

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

ans = []

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


ans = min(ans)
print(ans)

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

M = []
P = [0]
Z_flag = False
for x in X:
    if x < 0:
        M.append(-x)
    elif x > 0:
        P.append(x)
    else:
        Z_flag = True
M.reverse()
M.insert(0, 0)

if Z_flag:
    K -= 1

ans = 10 ** 9
len_m = len(M)
len_p = len(P)
for i in range(K+1):
    if i < len_m and 0 <= K-i < len_p:
        tmp = M[i] + P[K-i] + min(M[i], P[K-i])
        ans = min(ans, tmp)
print(ans)

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

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


main()

Solution (Python):
n, k = map(int, input().split())
l = list(map(int, input().split()))
res = float('inf')
for i in range(n - k + 1):
    a = max(0, - l[i])
    b = max(0, l[i + k - 1])
    res = min(res, a + b + min(a, b))
print(res)

Solution (Python):
import bisect

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

point0 = bisect.bisect(Xs, 0)
plusXs = Xs[point0:]
if point0 != 0:
    minusXs = Xs[point0-1::-1]
else:
    minusXs = []




min_plus = max(0, K-len(minusXs))
max_plus = min(K, len(plusXs))

ans = 10**10
for plus_index in range(min_plus, max_plus + 1):
    
    if plus_index == 0:
        temp = -minusXs[K - 1]
    elif K - plus_index == 0:
        temp = plusXs[plus_index-1]
    else:
        p = plusXs[plus_index - 1]
        m = -minusXs[K - plus_index - 1]
        temp = min(2*p+m, p+2*m)

    if temp < ans:
        ans = temp
    
print(ans)

Solution (Python):
def main():
    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] > 0:
            ans = min(ans,x[i+k-1])
        elif x[i+k-1] < 0:
            ans = min(ans,abs(x[i]))
        else:
            ans = min(ans,min(abs(x[i]),abs(x[i+k-1]))*2 + max(abs(x[i]),abs(x[i+k-1])))
        
    print(ans)


main()

Solution (Python):
N, K = map(int,input().split())
X = list(map(int,input().split()))
fu = 0
sei = 0
for i in range(N):
    if X[i] < 0:
        fu += 1
    else:
        sei += 1




if fu == 0:
    print(str(X[K-1]))
else:
    kouho = []
    for i in range(fu):
        
        
        if(fu-i+sei < K):
            break
        else:
            kouho.append(min(2*abs(X[i])+abs(X[fu + K-(fu-i) - 1]), abs(X[i])+abs(2*X[fu + K-(fu-i) - 1])))
    if sei >= K:
        kouho.append(X[fu+K-1])
    if fu >= K:
        kouho.append(abs(X[fu-K]))
    print(str(min(kouho)))

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float('inf')
for i in range(N-K+1):
    l,r = x[i],x[i-1+K]
    if l*r > 0:
        ans = min(ans,max(abs(l),abs(r)))
    else:
        ans = min(ans,max(abs(l),abs(r))+min(abs(l),abs(r))*2)
print(ans)

Solution (Python):

def main():
    _, k = map(int, input().split())
    x = tuple(map(int, input().split()))
    print(min(r - l + min(abs(l), abs(r)) for l, r in zip(x, x[k - 1 :])))


if __name__ == "__main__":
    main()

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

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

dist = val[k-1]-val[0]+abs(val[0])
minim = dist

for i in range(n-k):
    dist += val[i+k]-val[i+k-1]
    dist -= val[i+1]-val[i]
    dist -= abs(val[i])
    dist += abs(val[i+1])
    if(dist<minim):
        minim = dist

dist = val[k-1]-val[0]+abs(val[k-1])
if(dist<minim):
    minim = dist

for i in range(n-k):
    dist += val[i+k]-val[i+k-1]
    dist -= val[i+1]-val[i]
    dist -= abs(val[i+k-1])
    dist += abs(val[i+k])
    if(dist<minim):
        minim = dist
        
print(minim)

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

dist = val[k-1]-val[0]+abs(val[0])
minim = dist

for i in range(n-k):
    dist += val[i+k]-val[i+k-1]
    dist -= val[i+1]-val[i]
    dist -= abs(val[i])
    dist += abs(val[i+1])
    if(dist<minim):
        minim = dist

dist = val[k-1]-val[0]+abs(val[k-1])
if(dist<minim):
    minim = dist

for i in range(n-k):
    dist += val[i+k]-val[i+k-1]
    dist -= val[i+1]-val[i]
    dist -= abs(val[i+k-1])
    dist += abs(val[i+k])
    if(dist<minim):
        minim = dist
        
print(minim)

Solution (Python):
import bisect

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

index = bisect.bisect_left(x, 0)
if index < len(x):
  if x[index] != 0:
    x.insert(index, 0)
    N += 1
    K += 1
else:
  x.insert(index, 0)
  N += 1
  K += 1
  index -= 1

start_min = max(index-K+1, 0)
start_max = min(index, N-K)
min_time = 10**9
for i in range(start_min, start_max+1):
  min_time = min(min_time, x[i+K-1]-x[i]+min(abs(x[i+K-1]), abs(x[i])))
print(min_time)

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = float('inf')
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 <= R:
        ans = min(ans, -2 * L + R, -L + 2 * R)
    else:
        ans = min(ans, R)

print(ans)

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

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

Solution (Python):
N, K = map(int, input().split())
xs = list(map(int, input().split()))
x1 =[0]
x2 = [0]
dist = []
for i in xs:
    if i < 0:
        x1.append(i)
    else:
        x2.append(i)
x1.sort(reverse = True)
l1 = len(x1)
l2 = len(x2)
m = K-l1+1
for i in range(max(0, m), min(K+1, l2)):
    d1 = 2*x2[i] -x1[K-i]
    d2 = x2[i] - 2*x1[K-i]
    dist.append(min(d1, d2))
print(min(dist))

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, K: int, x: "List[int]"):

    xneg = [0]+[xv for xv in x if xv < 0][::-1]
    xpos = [0]+[xv for xv in x if xv >= 0]

    ans = []
    
    
    for i in range(K+1):
        if len(xneg) <= i or len(xpos) <= K-i:
            continue
        ans.append(xpos[K-i]-2*xneg[i])
        
    for i in range(K+1):
        if len(xpos) <= i or len(xneg) <= K-i:
            continue
        ans.append(2*xpos[i]-xneg[K-i])
        

    
    print(min(ans))
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    x = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, x)


if __name__ == '__main__':
    main()

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

cost = 10**9
for i in range(N-K+1):
    d1 = abs(xs[i])
    d2 = abs(xs[i+K-1])
    if xs[i] * xs[i+K-1] <= 0:
        cost = min(cost, 2 * min(d1, d2) + max(d1, d2))
    else:
        cost = min(cost, max(d1, d2))

print(cost)

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

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

distance_dict=[[x,abs(x)] for x in candls]

for x in range(0,N):
  if candls[x]>=0:
    x-=1
    break

resultlist=[]
minus=x
if K==1:
  print (min(abs(candls[minus]),abs(candls[minus+1])))
  exit()
elif minus==-1:
  print(abs(candls[K-1]))
  exit()
elif minus-K+1>=0:
  resultlist.append(abs(candls[minus-K+1]))
  for i in range(1,2*K):
    try:
      resultlist.append(abs(candls[minus+i])+abs(candls[minus-K+1+i])+min(candls[minus+i],abs(candls[minus-K+1+i])))
    except:
      break
else:
  for i in range(0,minus+2):
    try:
      resultlist.append(abs(candls[K-1+i])+abs(candls[0+i])+min(candls[K-1+i],abs(candls[0+i])))
    except:
      break
try:
  resultlist.append(candls[minus+K])
except:
  pass

print (min(resultlist))

Solution (Python):
import numpy as np

N, K = map(int, input().split())
x = [int(_) for _ in input().split()]
ans = float('inf')

for i in range(N-K+1):
    left = x[i]
    right = x[i+K-1]
    if np.sign(left) == np.sign(right):
        tmp = max(abs(left), abs(right))
    else:
        tmp = 2*min(abs(left), abs(right))+max(abs(left), abs(right))
    if tmp < ans:
        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):
  l = x[i]
  r = x[i+k-1]
  if l >= 0:
    dis = r
  elif r <= 0:
    dis = -l
  else:
    dis = min(r-2*l,r*2-l)
  if dis < ans:
    ans = dis
print(ans)

Solution (Python):
""" AtCoder """

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

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

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

print(ans)

Solution (Python):
from bisect import*
n,k,*a=map(int,open(0).read().split())
if 0 in a:
    k-=1
    a.remove(0)

ans=[]

x=bisect(a,0)
for i in range(-k,1):
    p=x+i
    q=x+i+k-1

    if 0<=p<n and 0<=q<n:
        
        c=a[p]
        d=a[q]
        ans.append(abs(c)+abs(d-c))
        ans.append(abs(d)+abs(d-c))

print(min(ans or [0]))

Solution (Python):
from bisect import*
n,k,*a=map(int,open(0).read().split())
if 0 in a:
    k-=1
    a.remove(0)

ans=[]

x=bisect(a,0)
for i in range(-k,1):
    p=x+i
    q=x+i+k-1

    if 0<=p<n and 0<=q<n:
        
        c=a[p]
        d=a[q]
        ans.append(abs(c)+abs(d-c))
        ans.append(abs(d)+abs(d-c))

print(min(ans or [0]))

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(int(abs(x[N-K])))
  sys.exit()

time = 3*10**8 + 100
if (n-K) >= 0:
  time = abs(x[n-K])
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, abs(x[n+K-1]))

print(int(time))

Solution (Python):
N, K = map(int,input().split())
eliment = list(map(int,input().split()))
if 0 not in eliment:
    eliment.append(0)
    eliment.sort()
else:
    K -= 1
if K <= 0:
    print(0)
    exit()
endpoint = eliment.index(0)
stpoint = 0 if endpoint - K < 0 else endpoint - K
endpoint = N - K + 1 if endpoint + 1  > N - K else endpoint + 1


lessSum=999999999
for i in range(stpoint, endpoint):
    cal = eliment[i+K] - eliment[i] + min(abs(eliment[i+K]),abs(eliment[i]))
    
    if lessSum > cal: lessSum = cal
print(lessSum)

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

time = [0]*(n-k+1)
time_rev = time
res = 10e9
for i in range(n-k+1) :
  time[i] = abs(x[i+k-1]-x[i]) + abs(x[i])
  if(res > time[i]) : res = time[i]
  time_rev[i] = abs(x[i+k-1]-x[i]) + abs(x[i+k-1])
  if(res > time_rev[i]) : res = time_rev[i]
print(res)

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

Solution (Python):
N,K=map(int,input().split())
x=list(map(int,input().split()))
ans=10000000000000000000
for i in range(0,N-K+1):
    ls=(x[i])
    rs=(x[i+K-1])
    if(ls>0 and rs>0):
        ans=min(ans,rs)
    elif(ls<0 and rs<0):
        ans=min(ans,abs(ls))
    else:
        ans=min(abs(ls)+rs+min(abs(ls),rs),ans)

print(ans)

Solution (Python):

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

if 0 in X:
    K -= 1
    if K == 0:
        print(0)
        exit(0)

minus = list(filter(lambda x: x<0, X))
minus = list(map(lambda x: abs(x), minus))
minus = list(reversed(minus))

pluss = list(filter(lambda x: x>0, X))

ans = 10**11

for n_left in range(0, K+1): 
    n_right = K - n_left
    i_right = n_right - 1
    i_left  = n_left - 1

    if i_right >= len(pluss):
        continue
    if i_left >= len(minus) :
        continue

    tmp = 0
    if i_left >= 0:
        tmp += 2 * minus[i_left]
    if i_right >= 0:
        tmp += pluss[i_right]
    ans = min(ans, tmp)

for n_right in range(0, K+1): 
    i_right = n_right - 1
    n_left = K - n_right
    i_left = n_left - 1
    if i_right >= len(pluss):
        continue
    if i_left >= len(minus) :
        continue
    tmp = 0
    if i_right >= 0:
        tmp += 2 * pluss[i_right]
    if i_left >= 0:
        tmp += minus[i_left]
    ans = min(ans, tmp)

print(ans)

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

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

ans = []
for i in range(n-k+1):
    if xxx[i]*xxx[i+k-1] < 0:
        if abs(xxx[i]) > abs(xxx[i+k-1]):
            ans.append(xxx[i+k-1]*2-xxx[i])
        else:
            ans.append(xxx[i+k-1]-xxx[i]*2)
    else:
        ans.append(max(abs(xxx[i]),abs(xxx[i+k-1])))

print(min(ans))

Solution (Python):
n, k = map(int, input().split())
X = list(map(int, input().split()))
ans = [0 for _ in range(n-k+1)]

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

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

ans=[0 for _ in range(n-k+1)]

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

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

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

minus = []
plus = []
for x in Xs:
    if x < 0:
        minus.append(x)
    elif x == 0:
        K -= 1
    else:
        plus.append(x)


left_dist = [0] * (len(minus) + 1)
for i, m in enumerate(reversed(minus)):
    left_dist[i+1] = abs(m)

right_dist = [0] * (len(plus) + 1)
for i, p in enumerate(plus):
    right_dist[i+1] = p



ans = 1e20
for left_num in range(0, K + 1):
    right_num = K - left_num
    if left_num > len(minus) or right_num > len(plus):
        continue
    ans = min(ans, left_dist[left_num] * 2 + right_dist[right_num])
    ans = min(ans, right_dist[right_num] * 2 + left_dist[left_num])
print(ans)

Solution (Python):
n,k = (int(i) for i in input().split())
x = list(int(i) for i in input().split())
res = 1000000000
for i in range(n-k+1):
    xl = x[i]
    xr = x[i+k-1]
    tmp = min(abs(xl)+abs(xr-xl), abs(xr)+abs(xr-xl))
    res = min(res,tmp)
print(res)

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>=0:
    b=X[K-1]
elif X[-1]<=0:
    b=abs(X[-K])
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Solution (Python):
N, K = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
ans = abs(x[0]) + abs(x[0]-x[0+K-1])

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

print(ans)

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

ans = 10 ** 9
for L in range(N - K + 1):
    xL, xR = xs[L], xs[L + K - 1]
    ansLR = abs(xL) + abs(xR - xL)
    ansRL = abs(xR) + abs(xL - xR)
    ans = min(ans, ansLR, ansRL)

print(ans)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

n, k = inpl()
C =inpl()
ans = 10**9
for i in range(n-k+1):
    ans = min(ans, abs(C[i]-C[k+i-1])+min(abs(C[i]),abs(C[k+i-1])))
print(ans)

Solution (Python):
def main():
    n,k = [int(i) for i in input().split()]
    candles = [int(i) for i in input().split()]
    i = 0
    res=1<<60;
    while i+k-1<n:
        left = candles[i]
        right = candles[i+k-1]
        res = min(res,min(abs(left),abs(right))+right-left)
        i+=1
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
str = raw_input()
str1 = raw_input()
N,k = map(int, str.split())
array = map(int, str1.split())

INF = 10**10
answer = INF

for i in range(N-k+1):
    answer = min(answer, array[i+k-1]-array[i]+min(abs(array[i+k-1]), abs(array[i])))

print answer

Solution (Python):
str = raw_input()
str1 = raw_input()
N,k = map(int, str.split())
array = map(int, str1.split())

INF = 10**8*3
answer = INF

for i in range(N-k+1):
    answer = min(answer, array[i+k-1]-array[i]+min(abs(array[i+k-1]), abs(array[i])))

print answer

Solution (Python):
str = raw_input()
str1 = raw_input()
array = str.split()
array = map(int, array)
N = array[0]
k = array[1]
array = str1.split()
array = map(int, array)
left_index = 0
right_index = 0
if array[0]*array[-1]>=0:
    if array[0] >= 0:
        answer = array[k-1]
    else:
        answer = abs(array[-k])
else:
    result = []
    x = min([abs(i) for i in array])
    if x == 0:
        index = array.index(x)
        if index < k - 1:
            left_index = 0
        else:
            left_index = index - k + 2
            result.append(abs(array[index-k+1]))
        if len(array) - 1 - index < k - 1:
            right_index = len(array)
        else:
            right_index = index + k - 1
            result.append(array[index+k-1])

    else:
        if x in array:
            index = array.index(x)
            middle_left_index = index - 1
            middle_right_index = index
        else:
            index = array.index(-x)
            middle_left_index = index
            middle_right_index = index + 1
        if middle_left_index < k - 1:
            left_index = 0
        else:
            left_index = middle_left_index - k + 2
            result.append(abs(array[middle_left_index - k + 1]))
        if len(array) - 1 - middle_right_index < k - 1:
            right_index = len(array)
        else:
            right_index = middle_right_index + k - 1
            result.append(array[middle_right_index+k-1])
    new_array = array[left_index:right_index]
    if len(new_array) >= k:
        min_route = max(abs(new_array[0]), abs(new_array[-1]))**2
        for i in range(len(new_array)):
            if i+k-1 <= len(new_array) - 1:
                left_abs = abs(new_array[i])
                right_abs = abs(new_array[i+k-1])
                current_min_route = min(left_abs, right_abs) + left_abs + right_abs
                if min_route > current_min_route:
                    min_route = current_min_route
        result.append(min_route)
    answer = min(result)
print answer

Solution (Python):
import sys

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

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

list = []

S = r - K
if x[r] == 0:
    S += 1
S = max(S, 0)

for s in range(S, r+1):
    e = s + K - 1
    if len(x) <= e:
        break
    list.append(abs(x[e] - x[s]) + abs(x[s]))
    list.append(abs(x[e] - x[s]) + abs(x[e]))

print(min(list))

sys.exit(0)

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
num = float("inf")
for i in range(N - K + 1):
    p = X[i]
    q = X[i + K - 1]
    if q < 0:
        num = min(num, abs(p))
    elif p < 0:
        num = min(num, abs(p) + 2 * abs(q))
        num = min(num, abs(p) * 2 + abs(q))
    else:
        num = min(num, q)

print(num)

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
pl, mi = 0, 0
recp, recm = [], []
for i in range(n):
    if x[i] >= 0:
        pl += 1
        recp.append(x[i])
    else:
        mi += 1
        recm.append(x[i])

num = float("inf")
recm = recm[:: -1]
for i in range(pl + 1):
    if i == 0:
        if k <= mi:
            num = min(num, abs(recm[k - 1]))
        else:
            continue
    else:
        if k - i == 0:
            num = min(num, recp[k - 1])
        elif 0 < k - i <= mi:
            num = min(num, recp[i - 1] + 2 * abs(recm[k - i - 1]))
            num = min(num, 2 * recp[i - 1] + abs(recm[k - i - 1]))

print(num)

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

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

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

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

for i in range(n-k+1):
    xl,xr = x[i],x[i+k-1]
    if result == -2 or min(abs(xl),abs(xr)) + xr - xl < result: 
        result = min(abs(xl),abs(xr)) + xr - xl

print(result)

Solution (Python):

import sys


def main():
    N, K = list(map(int, input().split()))
    values = list(map(int, input().split()))
    res = sys.maxsize - 1
    for i in range(N - K + 1):
        start = values[i]
        end = values[i + K - 1]

        if start * end > 0:
            cur = (max(abs(start), abs(end)))
            res = min(res, cur)
        else:
            cur = (end - start + min(abs(start), abs(end)))
            res = min(res, cur)
    print(res)


if __name__ == '__main__':
    main()

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

ans = -1

for i in range(N - K + 1):
    l, r = i, i + K - 1
    xl = x[l]
    xr = x[r]
    tmp = min(abs(xl),abs(xr)) + xr - xl
    if ans == -1 or tmp < ans:
        ans = tmp
        
print(ans)

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

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

pos_start_index = 0

if X[0] > 0:
  pos_start_index = 0
elif X[N-1] < 0:
  pos_start_index = N

for n in range(N-1):
  if X[n] == 0:
    pos_start_index = n
    break
  elif X[n] < 0 and X[n+1] > 0:
    pos_start_index = n+1
    break


neg_sum_list = []

for j, idx in enumerate(range(pos_start_index-1, -1, -1)):
  neg_sum_list.append(-X[idx])

pos_sum_list = []

for j, idx in enumerate(range(pos_start_index, N)):
  pos_sum_list.append(X[idx])


res = []

for j, neg_sum in enumerate(neg_sum_list):
  neg_count = j+1
  pos_count = K - neg_count

  if neg_count == K:
    res.append(neg_sum)
    break

  if pos_count <= len(pos_sum_list):
    pos_sum = pos_sum_list[pos_count-1]
    res.append(2*neg_sum + pos_sum)

for j, pos_sum in enumerate(pos_sum_list):
  pos_count = j+1

  neg_count = K - pos_count

  if pos_count == K:
    res.append(pos_sum)
    break

  if neg_count <= len(neg_sum_list):
    neg_sum = neg_sum_list[neg_count-1]
    res.append(2*pos_sum + neg_sum)

print(min(res))

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