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):
def main():
    n, k = list(map(int, input().split()))
    X = list(map(int, input().split()))
    P = []
    M = []
    for x in X:
        if x >= 0:
            P.append(x)
        else:
            M.append(abs(x))
    m_range = [max(0, k - len(P)), min(len(M), k)]
    p_range = [max(0, k - len(M)), min(len(P), k)]
    P = [0, ] + P
    M = [0, ] + M[::-1]
    ans = float('inf')
    for i in range(m_range[0], m_range[1] + 1):
        ans = min(ans, 2 * M[i] + P[k - i])
    for i in range(p_range[0], p_range[1] + 1):
        ans = min(ans, 2 * P[i] + M[k - i])
    print(ans)

if __name__ == '__main__':
    main()

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

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


def read():
    N, K = map(int, input().strip().split())
    X = list(map(int, input().strip().split()))
    return N, K, X


def solve(N, K, X):
    amin = 10**9
    for i in range(N-K+1):
        a0 = min(abs(X[i+K-1]), abs(X[i]))
        a1 = abs(X[i+K-1] - X[i])
        amin = min(amin, a0 + a1)
    return amin


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

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

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

    if time < min_time:
        min_time = time

print(min_time)

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

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

if 0 in X: 
  K -= 1
  
plus = []
minus = []
for x in X:
  if x < 0:
    minus += [-x]
  elif x > 0:
    plus += [x]
    
minus = [0] + minus[::-1]
plus = [0] + plus

ans = 10 ** 10
for m in range(K + 1):
  p = K - m
  if m < len(minus) and p < len(plus):
    dist = min(minus[m],plus[p]) * 2 + max(minus[m],plus[p])
    if ans > dist:
      ans = dist
      
print(ans)

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

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

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

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

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

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

print(ans)

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

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

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

print(ans)

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

x_neg = []
x_pos = []
for xx in x:
    if xx < 0:
        x_neg.append(-xx)
    else:
        x_pos.append(xx)
x_neg = x_neg[::-1]

ans = float('inf')
if k <= len(x_neg):
    ans = min(ans, x_neg[k-1])
if k <= len(x_pos):
    ans = min(ans, x_pos[k-1])

for i in range(1, k):
    j = k - i
    if i <= len(x_pos) and 1 <= j <= len(x_neg):
        d1 = x_pos[i - 1]
        d2 = x_neg[j - 1]
        d = min(d1, d2) * 2 + max(d1, d2)
        ans = min(ans, d)
print(ans)

Solution (Python):
N, K = map(int, input().split())
*x, = map(int, input().split())
ans = float('inf')
for i in range(N-K+1):
    chk = 0
    left = x[i]
    right = x[i+K-1]
    left_to_right = abs(left - right) + abs(left)
    right_to_left = abs(right-left) + abs(right)
    chk = min(left_to_right, right_to_left)
    ans = min(ans, chk)
print(ans)

Solution (Python):
INF = float('inf')
n, k = map(int, input().split())
L = list(map(int, input().split()))
ans = INF
for i in range(n-k+1):
    r = L[i+k-1]
    l = L[i]
    tr = abs(r) + abs(l - r)
    tl = abs(l) + abs(r - l)
    t = min(tr, tl)
    ans = min(ans, t)
print(ans)

Solution (Python):
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    idx = bisect_left(X, 0)
    
    ans = 10**10
    if idx == N:
        print(-X[-K])
        return
    elif 0 == X[idx]:
        for i in range(idx + 1):
            num = idx - i + 1
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 <= idx + (K - num) and idx + (K - num) < N:
                ans = min(ans, -2 * X[i] + X[idx + (K - num)])
        for i in range(idx + 1, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 <= idx - (K - num) and idx - (K - num) < N:
                ans = min(ans, 2 * X[i] - X[idx - (K - num)])
    else:
        for i in range(idx):
            num = idx - i
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 <= idx + (K - num) - 1 and idx + (K - num) - 1 < N:
                ans = min(ans, -2 * X[i] + X[idx + (K - num) - 1])
            
        for i in range(idx, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 <= idx - (K - num) and idx - (K - num) < N:
                ans = min(ans, 2 * X[i] - X[idx - (K - num)])
            
    print(ans)

if __name__ == "__main__":
    main()

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

min_dist = float('inf')
for i in range(0, n-(k-1)):
    left = x_list[i]
    right = x_list[i+(k-1)]

    dist = abs(left - right) + min(abs(left), abs(right))
    min_dist = min(dist, min_dist)
print(min_dist)

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

ans = float('inf')

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

    
    else:
        if 0 <= i-k+1 < n:
            temp = 0
            temp+=X[i]*2
            if X[i-k+1] <= 0:
                temp -= X[i-k+1]
                ans  = min(ans, temp)
        if 0 <= i+k-1 < n:
            ans = min(ans, X[i+k-1])

if n == 1:
    ans = abs(X[0])

print(ans)

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

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

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = [0]
for i in range(n):
    if a[i] < 0:
        b.append(abs(a[i]))
    elif a[i] > 0:
        c.append(a[i])
    else:
        k-=1
if k == 0:
    print(0)
    exit()
b.append(0)
b = b[::-1]
if len(b) == 1:
    print(c[k])
    exit()
if len(c) == 1:
    print(b[k])
    exit()
ans = 10 ** 18
for i in range(len(b)):
    if k - i >= len(c):
        continue
    if i == k:
        break
    
    ans = min(ans,min(2*b[i]+c[k-i],b[i]+2*c[k-i]))
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):
    r=x[i]
    l=x[i+k-1]
    if r<0 and 0<=l:
        tmp_ans=min(2*abs(r)+l, abs(r)+2*l)
    elif 0<=r:
        tmp_ans=l
    else:
        tmp_ans=abs(r)
    ans=min(ans,tmp_ans)
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):
    d1 = X[i + K - 1] - X[i] + abs(X[i])
    d2 = X[i + K - 1] - X[i] + abs(X[i + K - 1])
    
    ans = min(ans, d1, d2)
print(ans)

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


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

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

print(ans)

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

ans = []
for i in range(n - k + 1):
    left = x[i]
    right = x[i + k - 1]
    if abs(left) > abs(right):
        ans.append(abs(right) + abs(left - right))
    else:
        ans.append(abs(left) + abs(right - left))

print(min(ans))

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

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

print(min(ans))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
hantei=10**10+1
for i in range(0,n-k+1):
  
  d=a[i+k-1]-a[i]+min(abs(a[i]),abs(a[i+k-1]))
  if hantei>d:
    hantei=d
print(hantei)

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

for i in range(n-k+1):
    Max = X[i+k-1]
    Min = X[i]
    
    if Max<0 and Min<0:
        tmp_ans = abs(Min)
    elif Max>0 and Min >0:
        tmp_ans = abs(Max)
    else:
        tmp_ans = abs(Max) + abs(Min) + min(abs(Max),abs(Min))
    ans = min(ans,tmp_ans)
print(ans)

Solution (Python):
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = float("inf")
for i in range(0,n-k+1):
    if (x[i]<=0 and x[i+k-1]<=0):
        ans = min(ans, -x[i])
    elif (x[i]>=0 and x[i+k-1]>=0):
        ans = min(ans, x[i+k-1])
    else:
        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())
a = list(map(int,input().split()))
minus = [0]+[abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [0]+[a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 1:
  print(minus[k])
elif len(minus) == 1:
  print(plus[k])
else:
  for i in range(min(k+1, len(plus))):
    if k-i < len(minus):
      min_ = min(min_, 2*plus[i]+minus[k-i], plus[i]+2*minus[k-i])
  print(min_)

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

ans = float('inf')
for i in range(n-k+1):
    if x_l[i] * x_l[i+k-1] < 0:
        tmp = min(abs(x_l[i]), abs(x_l[i+k-1])) * 2 + max(abs(x_l[i]), abs(x_l[i+k-1]))
    elif x_l[i] < 0:
        tmp = abs(x_l[i])
    else:
        tmp = abs(x_l[i+k-1])
    ans = min(ans, tmp)

print(ans)

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

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

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
left = []
right = []
cnt = 0
for i in range(N):
    if x[i] < 0:
        left += [-x[i]]
    elif x[i] > 0:
        right += [x[i]]
    else:
        cnt += 1
rest = K - cnt
left = left[::-1]
if rest != 0:
    ans = float('inf')
else:
    ans = 0
    print(ans)
    exit()

for i in range(rest+1):
    if i == 0 and len(right) >= rest:
        ans = min(ans, right[rest-1])
    elif rest-i == 0 and len(left) >= i:
        ans = min(ans, left[rest-1])
    elif len(left) >= i and len(right) >= rest-i:
        ans = min(ans, left[i-1]+right[rest-i-1]+min(left[i-1], right[rest-i-1]))

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):
  cost1 = abs(x[i]) + abs(x[i + K - 1] - x[i])
  if cost1 < ans:
    ans = cost1
  cost2 = abs(x[i + K - 1]) + abs(x[i + K - 1] - x[i])
  if cost2 < ans:
    ans = cost2
print(ans)

Solution (Python):
N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
l = 0
r = K-1
outs = []
while r<=N-1:
    A = X[l]
    B = X[r]
    if A<0 and B<0:
        out = -1*A
    if A<0 and B>=0:
        out = min(-1*A*2+B,-1*A+2*B)
    if A>=0 and B>=0:
        out = B
    outs.append(out)
    l+=1
    r+=1
print(min(outs))

Solution (Python):
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 x==0 and y==0:
        ans = 0
    if x<0 and y<=0:
        ans = min(ans,abs(x))
    if x>=0 and y>0:
        ans = min(ans,y)
    if x<0 and y>0:
        ans = min(ans,min(abs(x),y)*2+max(abs(x),y))
    if X[i]>=0:
        break
print(ans)

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

Solution (Python):
n, k = map(int, input().split())
pos = []
neg = []
for x in map(int, input().split()):
    if x < 0:
        neg.append(-x)
    else:
        pos.append(x)
neg.reverse()
INF = 10**18
ans = INF
for neg_cnt in range(k+1):
    pos_cnt = k-neg_cnt
    if len(neg) < neg_cnt or len(pos) < pos_cnt:
        continue
    tmp = []
    if pos:
        tmp.append(pos[pos_cnt-1])
    else:
        tmp.append(0)
    if neg:
        tmp.append(neg[neg_cnt-1])
    else:
        tmp.append(0)
    s = min(tmp)+sum(tmp)
    if s < ans:
        ans = s
print(ans)

Solution (Python):
n, k = map(int, input().split())
L, R = [], []
for x in map(int, input().split()):
    if x < 0:
        L.append(-x)
    else:
        R.append(x)
L.reverse()
INF = 10**18
ans = INF
if len(L) == 0:
    ans = R[k-1]
elif len(R) == 0:
    ans = L[k-1]
else:
    for i in range(1, k):
        l, r = k-i-1, i-1
        if not(l < len(L)) or not(r < len(R)):
            continue
        a, b = L[l], R[r]
        ans = min(ans, min(a, b)+a+b)
print(ans)

Solution (Python):
def mips():
    return map(int,input().split())
N,K = mips()
C = [c for c in mips()]
dist = []
for i in range(N-K+1):
    dist.append(min(abs(C[i]) + abs(C[i+K-1]-C[i]),abs(C[i+K-1]) + abs(C[i+K-1]-C[i])))
print(min(dist))

Solution (Python):
from collections import deque
n,k=map(int,input().split())
l=list(map(int,input().split()))
plus=[]
minus=deque()
for i in l:
    if i<0:
        minus.appendleft(abs(i))
    elif i>0:
        plus.append(i)
    else:
        k-=1
minus=list(minus)
lenp=len(plus)
lenm=len(minus)
if k==0:
    print(0)
else:
    tcounter=[]
    if lenp>=k:
        tcounter.append(plus[k-1])
        if lenm>=k:
            for i in range(1,k):
                tcounter.append(min(plus[k-1-i],minus[i-1])+plus[k-1-i]+minus[i-1])
            tcounter.append(minus[k-1])
        else:
            for i in range(1,lenm+1):
                tcounter.append(min(plus[k-1-i],minus[i-1])+plus[k-1-i]+minus[i-1])
    else:
        if lenm>=k:
            for i in range(k-lenp,k):
                tcounter.append(min(plus[k-1-i],minus[i-1])+plus[k-1-i]+minus[i-1])
            tcounter.append(minus[k-1])
        else:
            for i in range(k-lenp,lenm+1):
                tcounter.append(min(plus[k-1-i],minus[i-1])+plus[k-1-i]+minus[i-1])
                
            
         
    print(min(tcounter))

Solution (Python):
import bisect

N,K = (int(x) for x in input().split())
x = list(map(int, input().split()))
bs = bisect.bisect_left(x,0)
ans = float('inf')
if bs == N:
    print(x[-K]*(-1))
    exit()
else:
    if x[bs] == 0:
        x.pop(bs)
        K -= 1
neg = bs
pos = N - bs
if len(x) == 0 or K == 0:
    print('0')
elif neg == 0:
    print(x[K-1])
else:
    for i in range(min(neg,K)+1):
        if i + pos < K:
                continue
        else:
            if i == 0:
                ans = min(ans,x[bs+K-1])
            elif i == K:
                ans = min(ans,x[bs-K]*(-1))
            else:
                tempn = x[bs-i] * (-1)
                tempp = x[bs+K-i-1]
                ans = min(ans,min(tempn,tempp)*2+max(tempn,tempp))
    print(ans)

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):
def resolve():
	n, k = map(int, input().split())
	x = list(map(int, input().split()))
	ans = float('inf')
	for i in range(n-(k-1)):
		if abs(x[i] - x[i+(k-1)]) == (abs(x[i]) + abs(x[i+(k-1)])):
			v = abs(x[i] - x[i + (k - 1)]) + min(abs(x[i]), abs(x[i + (k - 1)]))
			ans = min(ans, v)
		else:
			v = max(abs(x[i]), abs(x[i + (k - 1)]))
			ans = min(ans, v)
	print(ans)
resolve()

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

if __name__ == "__main__":
    main()

Solution (Python):
import sys




from collections import deque

from fractions  import gcd

input=sys.stdin.readline
n,k=map(int,input().split())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]>=0:
        s=i
        break
else:
  s=n
res=10**9
if s+k-1<n:
  res=x[s+k-1]
if s-k>=0 and res>abs(x[s-k]):
  res=abs(x[s-k])
for i in range(1,k):
    if s+k-i-1>n-1 or s-i<0:
        continue
    if x[s+k-i-1]+2*abs(x[s-i])<res:
        res=x[s+k-i-1]+2*abs(x[s-i])
for i in range(1,k):
    if s-k+i<0 or s+i-1>n-1:
        continue
    if abs(x[s-k+i])+2*x[s+i-1]<res:
        res=abs(x[s-k+i])+2*x[s+i-1]
print(res)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N,K = IL()
X = IL()

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

if K==0:
  print(0)
  E()

p = bisect.bisect(X,0)
count = []


dic = DD(lambda:(INF,INF))
dic_gyaku = DD(lambda:INF)
for i in range(N):
  dic[X[i]]=(max(p-i,i-p+1),-1 if i<p else 1)
  dic_gyaku[(max(p-i,i-p+1),-1 if i<p else 1)] = X[i]

MIN = INF
for i in range(N):
  num, sign = dic[X[i]]
  if sign < 0:
    left = K-num
    if left > 0:
      MIN = min(MIN,abs(X[i])*2+abs(dic_gyaku[(left,1)]))
    else:
      MIN = min(MIN,abs(X[i]))
  else:
    left = K-num
    if left > 0:
      MIN = min(MIN,abs(X[i])*2+abs(dic_gyaku[(left,-1)]))
    else:
      MIN = min(MIN,abs(X[i]))
print(MIN)

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

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

print(min(ans))

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

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

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

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

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

print(ans)

Solution (Python):
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue



ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        l = abs(X[n]) + abs(X[n]-X[n+K-1])
        r = abs(X[n+K-1]) + abs(X[n+K-1] - X[n])
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()

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

minus = [0]
plus = [0]
if 0 in x:
    k -= 1

for i in x:
    if i < 0:
        minus.append(abs(i))
    elif i > 0:
        plus.append(i)
minus.sort()


ans = float("inf")
for left in range(len(minus)):
    if 0 <= k-left < len(plus):
        right = k-left
        res = 2*minus[left] + plus[right]
        res = min(res, minus[left] + 2*plus[right])
        ans = min(ans, res)

print(ans)

Solution (Python):
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)
    N += 1

MIN = 2 * 10**9
for i in range(K+1):
    if s - K + i >= 0 and s + i <N:
        f = X[s -K+i]
        l = X[s+i]
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        MIN =min(MIN,ans)

print(MIN)

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

ans=max(x)-min(x)+max(max(x),abs(min(x)))

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

from bisect import bisect_left
from itertools import accumulate


def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    
    
    
    
    ans = 10**18
    for i in range(N-K+1):
        
        p, q = abs(x[i]), abs(x[i+K-1])
        if x[i] * x[i+K-1] < 0:
            ans = min(ans, p + q + min(p,q) )
        else:
            ans = min(ans, max(p,q))
    print(ans)
    


if __name__ == "__main__":
    main()

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


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

orient = bisect.bisect_left(X, 0)
X_posi = [float('inf')] * N
X_nega = [float('inf')] * N

for i in range(N-orient):
    X_posi[i] = X[orient+i]

for i in range(orient):
    X_nega[i] = -X[orient - i - 1]

ans = float('inf')
for i in range(K+1):
    if i == 0:
        ans = min(X_nega[K-i-1], ans)
    elif i == K:
        ans = min(X_posi[i-1], ans)
    else:
        ans = min(ans, X_posi[i-1] + X_nega[K-i-1] + min(X_posi[i-1], X_nega[K-i-1]))
print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18


n, k = Is()
X = LI()
ans = INF
for l in range(n):
    r = l + k - 1
    if l < 0 or n <= l or r < 0 or n <= r:
        continue
    else:
        ans = min(ans, abs(X[l])+abs(X[r]-X[l]), abs(X[r])+abs(X[l]-X[r]))
print(ans)

Solution (Python):



import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,k=MI()
lis=LI()
minus,plus=[],[]
for i in range(n):
    if lis[i]==0:
        k-=1
    elif lis[i]<0:
        minus.append(lis[i]*(-1))
    else:
        plus.append(lis[i])
minus=list(reversed(minus))
if k<=0:
    print(0)
    exit(0)
'''print(minus)
print(plus)'''
'''m=list(accumulate(minus))
p=list(accumulate(plus))'''
m=minus
p=plus
p=[0]+p
m=[0]+m
'''print(m)
print(p)'''
lens=[]

for i in range(k+1):
    im=i
    ip=k-i
    if im<len(m) and ip<len(p):
        dis=min(m[im],p[ip])*2+max(m[im],p[ip])
        lens.append(dis)
print(min(lens))