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())
line=list(map(int,input().split()))
ans=float("inf")
for i in range(n-k+1):
    left,right=i,i+k-1
    ans=min(ans,
            abs(line[left])+abs(line[left]-line[right]),
            abs(line[right])+abs(line[right]-line[left])
            )
print(ans)

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

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

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

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

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

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

answer = 10 ** 9
for i in range(N-K+1):
    b, c = X[i], X[i+K-1]
    if b * c >= 0:
        answer = min(answer, max(abs(b), abs(c)))
    else:
        d, e = min(abs(b), abs(c)), max(abs(b), abs(c))
        answer = min(answer, d*2+e)
print(answer)

Solution (Python):
import sys
N,K = map(int,input().split())
Data = list(map(int,input().split()))
Move = K
if 0 in Data:
    Move -= 1
Pos = []
Neg = []
for d in Data:
    if d > 0:
        Pos.append(d)
    if d < 0:
        Neg.append(-d)
Pos.sort()
Neg.sort()
posMaxRange = min(Move,len(Pos))
negMaxRange = min(Move,len(Neg))

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

minlen = -1
for t in range(1,posMaxRange+1):
    distance  = Pos[t-1]
    left = Move-t
    if left > 0:
        if len(Neg) < left:
            continue
        distance += Pos[t-1]
        distance += Neg[left-1]

    if minlen == -1:
        minlen = distance
    else:
        if minlen > distance:
            minlen = distance

for t in range(1,negMaxRange+1):
    distance = Neg[t-1]
    left = Move - t
    if left > 0:
        if len(Pos) < left:
            continue
        distance += Neg[t-1]
        distance += Pos[left-1]

    if minlen == -1:
        minlen = distance
    else:
        if minlen > distance:
            minlen = distance

print(minlen)

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

min_v = 10 ** 10
for i in range(N-K+1):

    v = min(abs(x[i]), abs(x[i+K-1])) + abs(x[i] - x[i+K-1])
    min_v = min(min_v, v)
    
print(min_v)

Solution (Python):
N, K=map(int, input().split())
x=list(map(int, input().split()))
ans=2*(abs(x[0])+abs(x[-1]))
for i in range(N-K+1):
  temp=[abs(x[i]), abs(x[i+K-1])]
  if x[i+K-1]<=0 or x[i]>=0:
    s=max(temp)
  else:
    s=max(temp)+2*min(temp)
    
  if ans>s:
    ans=s
    
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):
    xl=x[i]
    xr=x[i+k-1]
    ans=min(ans,abs(xl)+xr-xl,abs(xr)+xr-xl)
print(ans)

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

Solution (Python):

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

neg = 0
pos = 0

left = 0
right = 0

ans = 10**10

for l in range(N - K + 1):
    left = x[l]
    right = x[l + K - 1]
    neg = min(left, 0)
    pos = max(right, 0)
    if neg < 0 and pos > 0:
        ans = min(ans, abs(neg) * 2 + pos, abs(neg) + pos * 2)
    else:
        ans = min(ans, abs(neg) + pos)


print(ans)

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

x = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(x)-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])) + x[i+K-1]-x[i])
print (ans)

Solution (Python):
N, K = map(int, input().split())
lst = list(map(int, input().split()))
x = 0
besttime = 10**9
while x + K - 1< N:
    if lst[x]*lst[x+K-1] < 0:
        time = min(abs(lst[x])*2+lst[x+K-1], abs(lst[x])+2*lst[x+K-1])
    else:
        time = max(abs(lst[x]), abs(lst[x+K-1]))
    if besttime > time:
        besttime = time
    x += 1
print(besttime)

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

dist = []
for i in range(N-K+1): 
    l, r = x[i], x[i+K-1]
    t = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    dist.append(t)

print(min(dist))

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




import bisect
ln=bisect.bisect_right(x,0)

l = [0] + x[:ln][::-1]
r = [0] + x[ln:]
ans = 1000000000000
for i in range(k + 1):
    if i >= len(l): continue
    if k - i >= len(r): continue
    t= -l[i] + r[k - i] + min(-l[i], r[k - i])
    ans = min(ans,t)
print(ans)

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

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

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

print(ans)

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

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

if K == 0:
    ans = 0
else: 
    ans = 10**9
    for i in range(N-K+1):
        left = i 
        right = i+K-1 
        ans = min(ans, x[right] - x[left] + min(abs(x[right]), abs(x[left])))

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+k-1]<0:
    ans=min(ans,-x[i])
  elif x[i]>0:
    ans=min(ans,x[i+k-1])
  else:
    ans=min(ans,2*x[i+k-1]-x[i],2*(-x[i])+x[i+k-1])

print(ans)

Solution (Python):
n,k = map(int, input().split())
x = list(map(int, input().split()))
left = []
right = []
for i in x:
    if i < 0:
        left.append(-i)
    elif i > 0:
        right.append(i)
    else:
        k -= 1
if k <= 0:
    print(0)
else:
    left = [0] + left[::-1]
    right = [0] + right
    ans = float("Inf")
    for i in range(k+1):
        
        if len(left) > i and len(right) > k-i:
            tmp = left[i]+right[k-i] + min(left[i], right[k-i])
            ans = min(ans, tmp)
    print(ans)

Solution (Python):

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

Solution (Python):

import sys
input_s = sys.stdin.readline

N, K = map(int, input_s().split())
xxx = list(map(int, input_s().split()))

min_elapsed_time = 10**10
for left, right in zip(xxx, xxx[K - 1:]):
    elapsed_time = right - left + min(abs(left), abs(right))
    """
    right - left : a section distance
    min(abs(left), abs(right)) : a start point to move the section
    """
    min_elapsed_time = \
        elapsed_time if elapsed_time < min_elapsed_time else min_elapsed_time

print(min_elapsed_time)

Solution (Python):

def main():
    N, K, *X = map(int, open(0).read().split())
    cand = [r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :])]
    ans = min(cand)
    print(ans)


if __name__ == "__main__":
    main()

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


ans = 10 ** 15

for i in range(N - K + 1):
  l = i 
  r = l + K - 1
  sum1 = abs(x[l]) + abs(x[r] - x[l])
  sum2 = abs(x[r]) + abs(x[l] - x[r])
  ans = min(ans, sum1, sum2) 
  
print(ans)

Solution (Python):
n,k=map(int,input().split())
candle=list(map(int,input().split()))
ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(candle[l])+abs(candle[l]-candle[r]),
            abs(candle[r])+abs(candle[r]-candle[l])
            )
print(ans)

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

ans = 1e+9

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

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

print(ans)

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

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[l]-add[r]),
            abs(add[r])+abs(add[r]-add[l])
            )



print(ans)

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

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

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[r]-add[l]),
            abs(add[r])+abs(add[r]-add[l])
            )

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    n,k=map(int,input().split())
    X=tuple(map(int,input().split()))
    ans=INF
    for i in range(n-k+1):
        a,b=X[i],X[i+k-1]
        if a<0<b:
            ans=min(ans,b-a+min(-a,b))
        else:
            ans=min(ans,max(abs(a),abs(b)))
    print(ans)
resolve()

Solution (Python):
n, k = map(int, input().split())
x = sorted([0] + list(map(int, input().split())))

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

print(ans)

Solution (Python):


import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



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


def get_len(x0, xm1):
    left = abs(x0) + abs(xm1-x0)
    right = abs(xm1) + abs(xm1-x0)
    return min(left, right)

count = INF
for i in range(N-K+1):
    if X[i] > 0:
        X[i] = 0

    l = get_len(X[i], X[i+K-1])

    count = min(count, l)

print(count)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



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


def get_len(x0, xm1):
    left = abs(x0) + abs(xm1-x0)
    right = abs(xm1) + abs(xm1-x0)
    return min(left, right)



count = INF
for i in range(N-K+1):
    l = get_len(X[i], X[i+K-1])

    count = min(count, l)

print(count)

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

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)
    
if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
if n == k:
  print(min(r[-1], -l[0])*2+max(r[-1], -l[0]))
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)

print(min(ts))

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

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)
    
if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
if n == k:
  print(min(r[-1], -l[0])*2+max(r[-1], -l[0]))
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)

print(min(ts))

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

lft, rgt = 0, K - 1
ans = float('inf')

while True:
    if rgt == N:
        break
    ans = min(ans, min(abs(X[lft]) + abs(X[lft] - X[rgt]),
                       abs(X[rgt]) + abs(X[rgt] - X[lft])))
    lft += 1
    rgt += 1
print(ans)

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

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


flg = False

for x in X:
    if x < 0:
        flg = True

if flg is False:
    print(X[K - 1])
    exit()

flag2 = False

for x in X:
    if x > 0:
        flag2 = True

if flag2 is False:
    X.reverse()
    print(abs(X[K - 1]))
    exit()


def sol(lft, rgt):
    if X[lft] < 0 and X[rgt] < 0:
        return float('inf')
    elif X[lft] <= 0:
        return min(2 * abs(X[lft]) + X[rgt], abs(X[lft]) + 2 * X[rgt])
    else:
        return float('inf')


lft, rgt = 0, 0
ans = float('inf')
while True:

    if rgt == N - 1:
        break

    if rgt - lft + 1 < K:
        rgt += 1
        if rgt - lft + 1 == K:
            ans = min(ans, sol(lft, rgt))
            lft += 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

import bisect

N, K = map(int,input().split())
x = list(map(int, input().split()))
zero = bisect.bisect_right(x,0)
x.append(0)
x.sort()
dis = []
for i in range(1,N+1):
    dis.append(x[i]-x[i-1])

left=1
leftdis=0
while left<=K and zero-left>=0:
    leftdis+=dis[zero-left]
    left+=1
left-=1

right=1
rightdis=0
while left+right<=K:
    rightdis+=dis[zero+right-1]
    right+=1
right-=1

ans = min(rightdis*2+leftdis, rightdis+leftdis*2)
while right+zero<N and left > 0:
    leftdis-=dis[zero-left]
    rightdis+=dis[zero+right]
    ans = min(ans,rightdis*2+leftdis, rightdis+leftdis*2)
    left-=1
    right+=1

print(ans)

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

ct = [0]*(N-K+1)

for i in range(N-K+1):
  if (X[i]<0)&(X[i+K-1]>0):
    ct[i] = min(abs(X[i]),(X[i+K-1]))*2+max(abs(X[i]),(X[i+K-1]))
  else:
    ct[i] = max(abs(X[i]),(X[i+K-1]))
  
print(min(ct))

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

tmp=10**10
for i in range(N-K+1):
  if x[i]*x[i+K-1]<0:
    tmp=min(tmp,min(abs(x[i]),abs(x[i+K-1]))*2+max(abs(x[i]),abs(x[i+K-1])))
  else:
    tmp=min(tmp,max(abs(x[i]),abs(x[i+K-1])))
print(tmp)

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

ans = []
for i, name in enumerate(n):
    if b+i >a:
        break
    ans.append(min( (abs(n[i]) + abs(n[i +b-1] - n[i]),  abs(n[i+b-1]) + abs(n[i +b-1] - n[i]) )) )

print(min(ans))

Solution (Python):

def main():
    N, K, *X = map(int, open(0).read().split())
    ans = min(r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :]))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[K - 1 :]):
        x = r - l + min(abs(l), abs(r))
        ans = x if x < ans else ans
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[K - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import os, sys, re, math

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

candle = list(map(int,input().split(' ')))
ret = float('inf')

for i in range(N-K+1):
    s = candle[i]
    e = candle[i+K-1]
    if e < 0:
        ret = min(-s, ret)
    elif s > 0:
        ret = min(e, ret)
    else:
        ret = min(-s*2+e, -s+e*2, ret)

print(ret)

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

min_cost = 10**15
for i in range(n-k+1):
  left =  abs(X[i])    + abs(X[i]-X[i+k-1])
  right = abs(X[i+k-1])+ abs(X[i]-X[i+k-1])  
  min_cost = min(left,right,min_cost)
  
print(min_cost)

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]
  temp = min(abs(l)+abs(r-l),abs(r)+abs(l-r))
  ans = min(temp,ans)

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
X = [int(x) for x in input().split()]
ans = 1e9
for l in range(N-K+1):
    r = l+K-1
    a = min(
        abs(X[l]) + abs(X[r]-X[l]),
        abs(X[r]) + abs(X[l]-X[r])
    )
    if a < ans:
        ans = a
print(ans)

Solution (Python):
import os, sys, re, math

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

if 0 in x:
    K -= 1
nx = list(filter(lambda i: i < 0, x))
px = list(filter(lambda i: i > 0, x))
nx.reverse()

result = 0
if K > 0:
    result = sys.maxsize
    if len(nx) > K - 1:
        result = min(result, -nx[K - 1])
    if len(px) > K - 1:
        result = min(result, px[K - 1])
    for i in range(len(px)):
        j = K - i - 2
        if 0 <= j < len(nx):
            result = min(result, px[i] * 2 - nx[j])
    for i in range(len(nx)):
        j = K - i - 2
        if 0 <= j < len(px):
            result = min(result, px[j] - nx[i] * 2)

print (result)

Solution (Python):
line = raw_input()
N, K = [int(s) for s in line.split(' ')]
line = raw_input()
Xs = [int(s) for s in line.split(' ')]

ret = []
for i in range(0, len(Xs) - K + 1):
    p = Xs[i + K - 1]
    m = Xs[i]
    if p * m == 0:
        ret.append(abs(p) * abs(m))
        continue
    if p * m > 0:
        ret.append(max(abs(p), abs(m)))
        continue
    ret.append(abs(p) * 2 + abs(m))
    ret.append(abs(p) + abs(m) * 2)

print min(ret)

Solution (Python):
n, k = map(int, input().split())
x = sorted(map(int, input().split()))
ret = 1000000000000000000
for s in range(n - k + 1):
    e = s + k - 1
    if x[e] <= 0:
        ret = min(ret, -x[s])
    elif x[s] <= 0:
        ret = min(ret, x[e] - x[s] + min(-x[s], x[e]))
    else:
        ret = min(ret, x[e])
print(ret)

Solution (Python):
N, K = list(map(int, input().split(' ')))
l = list(map(int, input().split(' ')))
minuss = [0] + [-i for i in l if i < 0][::-1]
pluss = [0] + [i for i in l if i >= 0]

ans = 1e16  
if pluss == [0]:  
    pluss = minuss
    minuss = [0]
for k in range(K):
    if k >= len(minuss) or K - k >= len(pluss):
        continue
    ans = min(ans, min(minuss[k] * 2 + pluss[K-k], minuss[k] + pluss[K-k] * 2))
print(ans)

Solution (Python):
n, k = map(int, input().split())
x = sorted([0] + list(map(int, input().split())))

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

print(ans)

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

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

Solution (Python):
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float('inf')
for l in range(0, N-K+1):
    r = l + K - 1
    ans = min(ans, min(abs(X[l]), abs(X[r])) + abs(X[l] - X[r]))
print(ans)

Solution (Python):
N,K=map(int,input().split())
l=list(map(int,input().split()))
t=99999999999999999
for i in range(N-K+1):
  D=l[i+K-1]-l[i]+min(abs(l[i+K-1]),abs(l[i]))
  if D<t:t=D
print(t)

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

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

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

res = 2 * (abs(ls[0]) + abs(ls[-1]))
for i in range(n - k + 1):
    a = ls[i]
    b = ls[i + k - 1]
    if b <= 0:
        res = min(res, -a)
    elif a >= 0:
        res = min(res, b)
    else:
        res = min(res, -2 * a + b, -a + 2 * b)
print(res)

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

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N, K = (int(i)for i in input().split())
    x = [int(i) for i in input().split()]
    ans = 3 * (10**8)
    st_val = min(x,key=abs)
    st_idx = x.index(st_val)
    
    for i in range(K+1):
        dist = 3 * (10**8)
        if 0 <= st_idx-K+1+i and st_idx+1+i <= N:
            if (x[st_idx-K+1+i] < 0 and x[st_idx+i] <= 0):
                dist = abs(x[st_idx-K+1+i])
            elif (x[st_idx-K+1+i] >= 0 and x[st_idx+i] > 0):
                dist = abs(x[st_idx+i])
            else:
                if abs(x[st_idx-K+1+i]) > abs(x[st_idx+i]):
                    dist = abs(x[st_idx-K+1+i]) + abs(x[st_idx+i]) * 2
                else:
                    dist = abs(x[st_idx-K+1+i]) * 2 + abs(x[st_idx+i])
        elif N < st_idx+1+i:
            break
        ans = min(ans,dist)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n,k,*l=map(int,open(0).read().split());print(min((max(abs(i),abs(j)),min(-i*2+j,-i+2*j))[i*j<0]for i,j in zip(l,l[k-1:])))

Solution (Python):
n,k,*l=map(int,open(0).read().split())
m=1e10
for i,j in zip(l,l[k-1:]):
  if i*j<0:
    m=min(m,-i*2+j,-i+j*2)
  else:
    m=min(m,max(abs(i),abs(j)))
print(m)

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

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

print(min(arr))

Solution (Python):
import bisect
import sys

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

zIndex = bisect.bisect_left(x, 0)

if zIndex == 0:
    print(x[K - 1])
    sys.exit()
ans = 10 ** 9
for i in range(N - K + 1):
    if zIndex > i + K - 1:
        ans = min(ans, abs(x[i]))
    else:
        tmp1 = min(abs(x[i]), x[i + K - 1])
        tmp2 = max(abs(x[i]), x[i + K - 1])
        ans = min(ans, (tmp1 * 2) + tmp2)

print(ans)

Solution (Python):
N, K = map(int,input().split())
x = list(map(int,input().split()))
if x.count(0):
    K -= 1
x_p = list(filter(lambda n:n>0,x))
x_n = list(filter(lambda n:n<0,x))
x_n = sorted(list(map(lambda n:n*(-1),x_n)))
ans = float('inf')
if K == 0:
    print(0)
    exit()
for i in range(K+1):
    p = i
    n = K-i
    if len(x_p) < p or len(x_n) < n:
        continue
    else:
        if p == 0:
            result = x_n[n-1]
        elif n == 0:
            result = x_p[p-1]
        else:
            if x_p[p-1] < x_n[n-1]:
                result = x_p[p-1]*2+x_n[n-1]
            else:
                result = x_p[p-1]+x_n[n-1]*2

    ans = min(ans, result)
print(ans)

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

Solution (Python):
import sys
N, K = map(int, input().split())
rousoku = [int(i) for i in input().split()]
if N == 1:
    print(abs(rousoku[0]))
    sys.exit()
if K == 1:
    rousoku = [abs(i) for i in rousoku]
    print(min(rousoku))
    sys.exit()

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


print(min(dislist))

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

for i in range(n):
    if i + k - 1 > n-1:
        break
    l = x[i]
    r = x[i+k-1]
    temp1 = abs(r) + abs(r - l)
    temp2 = abs(l) + abs(r - l)
    temp = min(temp1, temp2)
    if temp < ans:
        ans = temp

print(ans)

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

    l = 0
    r = K - 1

    mn = 10e+8
    while r < N:
        ltor = abs(x[l]) + abs(x[r] - x[l])
        rtol = abs(x[r]) + abs(x[l] - x[r])

        mn = min(mn, min(ltor, rtol))
        l += 1
        r += 1

    print(mn)


if __name__ == '__main__':
    func()

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

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

Solution (Python):


from collections import Counter,deque


from sys import stdin,setrecursionlimit


setrecursionlimit(10**6)
input=stdin.readline


n,k=map(int,input().split())
x=[int(i) for i in input().split()]
ans=10**9


for left in range(n-k+1):
    if x[left+k-1]<=0:
        ans=min(ans,abs(0-x[left+k-1])+abs(x[left+k-1]-x[left]))
    elif x[left]<0<x[left+k-1]:
        dis1=min(abs(x[left]),abs(x[left+k-1]))
        dis2=max(abs(x[left]),abs(x[left+k-1]))
        ans=min(ans,dis1*2+dis2)
    else:
        ans=min(ans,x[left]+abs(x[left+k-1]-x[left]))
print(ans)

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    N,K = map(int,input().split())
    X = list(map(int,input().split()))
    ans = float("inf")
    for i in range(N-K+1):
        x = X[i]
        y = X[i+K-1]
        if ans > y-x + min(abs(x),abs(y)):
            ans = y-x + min(abs(x),abs(y))
    print(ans)
    
    
    
main()

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

Solution (Python):
N,K=map(int,input().split())
x=[int(_) for _ in input().split()]
m=10**15
for i in range(N-K+1):
    l=abs(x[i])+abs(x[i]-x[i+K-1])
    r=abs(x[i+K-1])+abs(x[i+K-1]-x[i])
    if l<m:
        m=l
    if r<m:
        m=r
print(m)

Solution (Python):
N, K = map(int, input().split())
xs = list(map(int, input().split()))
rs = []
for i in range(0, N+1-K):
  r = abs(xs[i])+abs(xs[i+K-1]-xs[i])
  rs.append(r)
for i in range(K-1, N):
  r = abs(xs[i])+abs(xs[i-K+1]-xs[i])
  rs.append(r)
r = min(rs)
print(r)

Solution (Python):
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
def length(i):
  if ls[i]>=0:
    return ls[K+i-1]
  elif ls[K+i-1]<=0:
    return -ls[i]
  else:
    return min([2*ls[K+i-1]-ls[i],ls[K+i-1]-2*ls[i]])
answer=[]
for i in range(N-K+1):
  answer.append(length(i))
  
print(min(answer))

Solution (Python):
n, k = map(int, input().split())
x = map(int, input().split())
a = [0]
b = [0]
for x_ in x:
    if x_ >= 0:
        a.append(x_)
    else:
        b.append(abs(x_))

a.sort()
b.sort()

res = float('inf')
for i in range(k+1):
    try:
        res = min(res, a[i]+2*b[k-i], 2*a[i]+b[k-i])
    except:
        continue

print(res)

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

ts = [float('inf')] * (N-K+1)

for i in range(N-K+1):
  r = xs[i+K-1]
  l = xs[i]  
  ts[i] = (r - l) + min(abs(r-0), abs(l-0))  

print(min(ts))

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = []
for i in range(n-k+1):
  ans.append(min(abs(x[i]), abs(x[i+k-1])) + abs(x[i+k-1]-x[i]))
print(min(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:
        ans1 = 10 ** 9
        ans2 = x[i + K - 1]
    elif x[i + K - 1] <= 0:
        ans1 = abs(x[i])
        ans2 = 10 ** 9
    else:
        ans1 = abs(x[i]) * 2 + x[i + K - 1]
        ans2 = abs(x[i]) + x[i + K - 1] * 2
    ans = min(ans, ans1, ans2)
print(ans)

Solution (Python):
N,K = map(int,input().split())
list_x = list(map(int,input().split()))
 
loop_cnt = N - K + 1
ans = 10**10
 
for i in range(loop_cnt):
  diff_between_0 = min(abs(list_x[i]), abs(list_x[i+K-1]))
  diff_between_max_min = abs(list_x[i+K-1] - list_x[i])
  _ = diff_between_0 + diff_between_max_min
  ans = min(ans,_)
 
print(ans)

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

loop_cnt = N - K + 1
ans = []

for i in range(loop_cnt):
  diff_between_0 = min(abs(list_x[i]), abs(list_x[i+K-1]))
  diff_between_max_min = abs(list_x[i+K-1] - list_x[i])
  _ = diff_between_0 + diff_between_max_min
  ans.append(_)

print(min(ans))

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

l = 0
t_min = 3 * 10 ** 8 + 1

while l + k -1 < n:
    x_l = x[l]
    x_r = x[l + k - 1]
    if x_l < 0 and x_r > 0:
        t = 2 * min(abs(x_l), abs(x_r)) + max(abs(x_l), abs(x_r))
    else:
        t = max(abs(x_l), abs(x_r))
    if t < t_min:
        t_min = t
    l += 1

print(t_min)

Solution (Python):
n, k = map(int, input().split())
x = list(int(i) for i in input().split())
inf = 10**9
max_x = x[-1]
min_x = x[0]
if min_x >= 0:
    print(x[k-1])
elif max_x <= 0:
    print(abs(x[-k]))
else:
    for i in range(n-1):
        if x[i]<0 and x[i+1]>=0:
            key = i+1
            break
    if n-key>=k:
        ans1 = sum(x[key:key+k])
    else:
        ans1 = inf
    if key>=k:
        ans2 = abs(sum(x[key-k:key]))
    else:
        ans2 = inf
    ans3 = inf
    for i in range(max(0, key-k+1), min(n-k+1, key+k-1)):
        a, b = abs(x[i]), abs(x[i+k-1])
        t = max([a, b])+min([a, b])*2
        if t < ans3:
            ans3 = t
    print(min([ans1, ans2, ans3]))