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 resolve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 1 << 60

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

    print(ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
N, K = map(int, input().split())
X_list = list(map(int, input().split()))
INF = 10 ** 18
ans = INF
import bisect

zero_index = bisect.bisect_left(X_list, 0)
candidate_list = []
ans = INF
for i in range(K + 1):
    head = zero_index - i
    tail = head + K - 1
    if head < 0 or tail > N - 1: continue
    if i == 0:
        ans = min(ans, abs(X_list[tail]))
    elif i == K:
        ans = min(ans, abs(X_list[head]))
    else:
        head_abs = abs(X_list[head])
        tail_abs = abs(X_list[tail])
        ans = min(ans, head_abs + tail_abs + min(head_abs, tail_abs))
print(ans)

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():

    n, k= LI()
    x = LI()
    ans = float("inf")

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

    print(ans)

main()

Solution (Python):

url = "https://atcoder.jp//contests/abc107/tasks/arc101_a"

def main():
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    ans = 100000000000000
    se = [0, k-1]
    while se[1] < n:
        if xs[se[0]] < 0 <= xs[se[1]]:
            tmp = min(abs(xs[se[0]]), xs[se[1]]) * 2 + max(abs(xs[se[0]]), xs[se[1]])
            ans = min(ans, tmp)
        elif xs[se[0]] < 0:
            ans = min(ans, abs(xs[se[0]]))
        elif xs[se[1]] >= 0:
            ans = min(ans, xs[se[1]])
        se = [v + 1 for v in se]
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):








def solve(N, K, x):
    l = [abs(xi) for xi in x if xi < 0] + [0]
    l.reverse()
    r = [0] + [xi for xi in x if xi >= 0]
    ans = 10 ** 18 * 2 + 1
    for i in range(max(K - (len(l) - 1), 0), min(K + 1, len(r))):
        ll = l[K - i]
        rr = r[i]
        ans = min(ans, min(ll, rr) * 2 + max(ll, rr))
    print(ans)


if __name__ == '__main__':
    
    
    N, K = map(int, input().split())
    x = [int(i) for i in input().split()]
    
    
    solve(N, K, x)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(abs,x))
ans = float("inf")

for i in range(n-k+1):
    if 0 < x[i] * x[i+k-1]:
        cost = max(y[i],y[i+k-1])
        
    else:
        cost = y[i] + y[i+k-1] + min(-x[i],x[i+k-1])
        
    ans = min(ans,cost)
    
print(ans)

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

Solution (Python):

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


if 0 in xl:
    k -= 1
else:
    xl.append(0)
    xl.sort()
if k == 0:
    ans = 0
else:
    ans = 10**9
    zeropoint = xl.index(0)
    endpoint = len(xl)-1
    startpoint = max(0, zeropoint-k)
    cur = startpoint
    while cur+k <= endpoint and cur <= zeropoint:
        
        
        if cur < zeropoint < cur+k:
            tempdis = abs(xl[cur]-xl[cur+k])
            tempdis += min(abs(xl[cur]), xl[cur+k])
            ans = min(ans, tempdis)
        else:
            ans = min(ans, abs(xl[cur]-xl[cur+k]))
        cur += 1
print(ans)

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

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

rui=[0]
for i in range(n-1):
    rui.append(x[i+1]-x[i]+rui[i])


ans=float('inf')
for i in range(n-k+1):
    tmp=rui[i+k-1]-rui[i]
    ans=min(ans,tmp+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()))
ans = 10 ** 12
for i in range(n-k+1):
    ans = min(ans,abs(x[i])+(abs(x[i+k-1]-x[i])),abs(x[i+k-1])+(abs(x[i+k-1]-x[i])))
print(ans)

Solution (Python):
n,k=map(int,input().split())
x=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(r-l)))
print(min(a))

Solution (Python):
N, K = map(int, input().split())
X = 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(r-l)))
print(min(a))

Solution (Python):
n,k=map(int,input().split())
x=list(map(int,input().split()))
k-=1
ans=float('inf')

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



print(ans)

Solution (Python):
N,K=map(int,input().split())
x=list(map(int,input().split()))
positive=[]
negative=[]
for i in range(N):
    if x[i]<0:
        negative.append(-x[i])
    else:
        positive.append(x[i])
negative.sort()
n=len(negative)
m=len(positive)
ans=1000000000
if n>=K:
    ans=min(ans,negative[K-1])
if m>=K:
    ans=min(ans,positive[K-1])

for i in range(n):
    j=K-(i+1)
    if j>m:
        continue
    if j<=0:
        continue
    ans=min(ans,negative[i]+positive[j-1]+min(negative[i],positive[j-1]))
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 10**9
for i in range(N-K+1):
    l, r = A[i], A[i+K-1]
    if l*r >= 0:
        ans = min(ans, max(abs(l), abs(r)))
    else:
        ans = min(ans, r-l+min(abs(l), abs(r)))
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):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
if ans == 10**10:
  print(0)
else:
  print(ans)

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
if lp == 0:
    print(-xn[K-1])
    exit()
if ln == 0:
    print(xp[K-1])
    exit()
pc = min(K,lp)
pp = xp[pc-1]
if K>lp:
    nc = K-lp
    np = -xn[nc-1]
else:
    nc = 0
    np = 0
ans = pp + np + min(pp,np)
for i in range(pc-1):
    if nc == ln:
        break
    pp = xp[pc-i-2]
    np = -xn[nc]
    nc += 1
    ans = min(ans,pp + np + min(pp,np))
print(ans)

Solution (Python):

import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, K = MI()
    targets = LI()
    if N == 1:
        print(abs(targets[0]))
        return
    length = targets[K - 1] - targets[0]
    ans = length + min(abs(targets[K-1]), abs(targets[0]))
    for i in range(N-K + 1):
        length = targets[i + K - 1] - targets[i]
        cost = length + min(abs(targets[i+K-1]), abs(targets[i]))
        ans = min(ans, cost)

    print(ans)


main()

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

Solution (Python):
n, k = map(int, input().split())
x = list(map(int, input().split()))
pos, nega = [], []
for v in x:
    if v >= 0:pos.append(v)
    else:nega.append(-v)
nega.sort()
ans = 10**9
if k <= len(pos):ans = min(ans, pos[k-1])
if k <= len(nega):ans = min(ans, nega[k-1])
for i in range(k):
    if i < len(pos) and 0 <= k - i - 2 < len(nega):
        ans = min(ans, 2 * pos[i] + nega[k-i-2])
    if i < len(nega) and 0 <= k - i - 2 < len(pos):
        ans = min(ans, 2 * nega[i] + pos[k-i-2])
print(ans)

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

import bisect

i=bisect.bisect_left(X,0)
L=max(0, bisect.bisect_right(X,0)-K)
R=min(N-1, bisect.bisect_left(X,0)+K-1)

ans=float('inf')

for i in range(L,R-K+2):
    t=0
    l=X[i]
    r=X[i+K-1]
    if r<=0:
        t=abs(l)
    elif 0<=l:
        t=r
    else:
        t=min(abs(l),r)*2+max(abs(l),r) 
    
    ans=min(ans,t)
print(ans)

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n, k = i_map()
    x = i_list()

    l = []
    r = []

    for i in x:
        if i < 0:
            l.append(-i)
        else:
            r.append(i)
    l.reverse()

    
    
    
    
    
    
    
    
    
    
    


    R_len = len(r)
    L_len = len(l)

    ans = INF
    for i,j in enumerate(l):
        if R_len >= k - i - 1:
            if R_len == 0:
                ans = min(ans, j)
            else:
                ans = min(ans,2*j+r[k-i-2])

    for i,j in enumerate(r):
        if L_len >= k - i - 1:
            if L_len == 0:
                ans = min(ans,j)
            else:
                ans = min(ans,2*j+l[k-i-2])

    print(ans)


if __name__=="__main__":
    main()

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

Solution (Python):
from bisect import bisect_left

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

    zero_ind = bisect_left(x, 0)
    
    if zero_ind == 0:
        print(x[k-1])
        exit()
    elif zero_ind == n:
        print(-x[-k])
        exit()

    if x[zero_ind] == 0:
        x.pop(zero_ind)
        n -= 1
        k -= 1
    
    ans = float('inf')

    
    if zero_ind >= k:
        ans = -x[zero_ind - k]

    
    if n - zero_ind >= k and ans > x[zero_ind + k - 1]:
        ans = x[zero_ind + k - 1]

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

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

    print(ans)

main()

Solution (Python):
import sys



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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, k = input_int_list()
    X = input_int_list()
    ans = float("inf")
    for i in range(0, n - k + 1):
        tmp_min = X[i]
        tmp_max = X[i + k - 1]
        if tmp_max <= 0:
            tmp = abs(tmp_min)
        elif tmp_min >= 0:
            tmp = abs(tmp_max)
        else:
            tmp = min(abs(tmp_max), abs(tmp_min)) * 2 + max(abs(tmp_max), abs(tmp_min))
        ans = min(tmp, ans)
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N,K=mint()
X=lint()
minus=[]
plus=[0]
for x in X:
    if x<0:
        minus.append(-x)
    else:
        plus.append(x)
minus.append(0)
P=len(plus)-1
M=len(minus)-1
ans=INF
for i in range(K+1):
    p = INF if K-i>P else plus[K-i]
    m = INF if i>M else minus[M-i]
    ans=min(ans,p+m+min(p,m))
print(ans)

Solution (Python):
from sys import stdin

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

def main(n,k,x):
    idx = k-1
    ans = 0
    if x[0]>=0:
        ans = x[idx]
    elif x[idx]<=0:
        ans = abs(x[0])
    else:
        ans = min(abs(x[0]),abs(x[idx]))*2+max(abs(x[0]),abs(x[idx]))
    for i in range(k,n):
        temp = 0
        
        if x[i-k+1]>=0:
            temp = x[i]
        elif x[i]<=0:
            temp = abs(x[i-k+1])
        else:
            temp = min(abs(x[i]),abs(x[i-k+1]))*2+max(abs(x[i]),abs(x[i-k+1]))
        ans = min(ans,temp)
    print(ans)


if __name__=="__main__":
    n,k,x = getval()
    main(n,k,x)

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

tp = N
for i in range(N):
    if x[i] >= 0:
        tp = i
        break
    x[i] *= -1

s = max(0, tp-K+1)
g = min(tp-1, N-K)

ans = float("INF")
if K <= tp:
    ans = min(ans, x[tp-K])
if K <= N - tp:
    ans = min(ans, x[tp+K-1])

for i in range(s, g+1):
    ans = min(ans, x[i] + x[i+K-1] + min(x[i], x[i+K-1]))
    

print(ans)

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
answers=[]
whole=0
for n in range(N-K+1):
    if X[K-1+n]>0 and X[n]<0:
        whole += (X[K - 1 + n] - X[n])
        whole+=min(X[K-1+n],-X[n])
    elif X[K-1+n]<0:
        whole-=X[n]
    elif X[n]>0:
        whole+=X[K-1+n]
    answers.append(whole)
    whole=0
print(min(answers))

Solution (Python):
N,K = map(int,input().split())
x = list(map(int,input().split()))
mi = 10 ** 18
for i in range(N-K+1):
    l = x[i]
    r = x[i+K-1]
    lr = abs(l)+abs(l-r)
    rl = abs(r)+abs(l-r)
    mi = min(mi,min(lr,rl))
print(mi)

Solution (Python):
import sys
N,K = map(int,input().split())
lsx = list(map(int,input().split()))
if 0 in lsx:
    K = K-1
    lsx.remove(0)
if bool(lsx) == False or K == 0:
    print(0)
    sys.exit()
lshu = []
sei = len(lsx)
for i in range(len(lsx)):
    if lsx[i] < 0:
        lshu.append(lsx[i])
    else:
        sei = i
        break
lssei = lsx[sei:]
lenhu = len(lshu)
lensei = len(lssei)
lshu2 = [abs(lshu[i]) for i in range(lenhu-1,-1,-1)]
dishu = [0] + lshu2
dissei = [0] + lssei
ans = 10*10**8
for i in range(K+1):
    if i <= lenhu and K-i <= lensei:
        ans = min(ans,2*dishu[i]+dissei[K-i],dishu[i]+2*dissei[K-i])
print(ans)

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





import math
import sys
sys.setrecursionlimit(10**9)
def binary_research(start,end,function):
    if start==end:
        return start
    middle=math.ceil((start+end)/2)
    if function(middle):
        start=middle
    else:
        end=middle-1
    return binary_research(start, end, function)
positive=[]
negative=[]
zero=False


for i in x:
    if i<0:
        negative.append(abs(i))
    elif i>0:
        positive.append(i)
    else:
        zero=True
positive.sort()
negative.sort()


def function(time):
    if time==-1:
        return True
    ans_list=[]
    ans=0
    if zero:
        ans+=1
    for i in positive:
        if i<=time:
            ans_list.append(i)
            ans+=1
        else:
            break
    negative_list=[]
    for i in negative:
        if i<=time:
            negative_list.append(i)
        else:
            break
    negative_list=negative_list[::-1]
    newans=ans
    while negative_list:
        newans,ans_list,negative_list=function2(ans_list,negative_list,newans,time)
        ans=max(ans,newans)
        if ans_list==[]:
            break
        else:
            ans_list.pop()
            newans-=1
    if ans>=k:
        return False
    return True



def function2(ans_list,negative_list,ans,time):
    if ans_list==[]:
        max_num=0
    else:
        max_num=ans_list[-1]
    remain_time=time-max_num*2
    while negative_list:
        i=negative_list.pop()
        if max_num==0:
            if i<=remain_time:
                ans+=1
        elif max_num+i+min(i,max_num)<=time:
            ans+=1
        else:
            negative_list.append(i)
            break
    return ans,ans_list,negative_list


print(binary_research(-1,3*10**8+1,function)+1)

Solution (Python):
def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    P = []
    M = []
    for x in X:
        if x < 0:
            M.append(-1 * x)
        else:
            P.append(x)
    M.reverse()

    if len(P) == 0:
        print(M[K - 1])
        exit()
    elif len(M) == 0:
        print(P[K - 1])
        exit()

    ans = 10 ** 10
    for i, m in enumerate(M, start=1):
        d = None
        if i == K + 1:
            break
        elif i == K:
            d = m
        else:
            p_idx = K - i - 1
            if p_idx >= len(P):
                continue
            else:
                d = m * 2 + P[p_idx]
        if d < ans:
            ans = d

    for i, p in enumerate(P, start=1):
        d = None
        if i == K + 1:
            break
        elif i == K:
            d = p
        else:
            m_idx = K - i - 1
            if m_idx >= len(M):
                continue
            else:
                d = p * 2 + M[m_idx]
        if d < ans:
            ans = d

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
xlst = list(map(int, input().split()))
minus = []
plus = []
for x in xlst:
    if x < 0:
        minus.append(-1 * x)
    elif x > 0:
        plus.append(x)
    else:
        k -= 1
if k == 0:
    print(0)
    exit()
minus.append(0)
minus = minus[::-1]
plus = [0] + plus
ans = 10 ** 20
ml = len(minus)
pl = len(plus)

pos = min(k, ml - 1)
while 1:
    mm = minus[pos]
    pp = plus[k - pos]
    ans_tmp = max(mm, pp) + min(mm, pp) * 2
    ans = min(ans, ans_tmp)
    pos -= 1
    if pos == -1 or k - pos == pl:
        break
print(ans)

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

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

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

for i in range(N - K + 1):
    left = X[i]
    right = X[i+K-1]
    cnt.append(min(abs(right)+abs(left-right), abs(left) + abs(right - left)))
print(min(cnt))

Solution (Python):
from bisect import bisect_left

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

pos = bisect_left(x, 0)
if pos < len(x) and x[pos] == 0:
    k -= 1
else:
    x.insert(pos, 0)

l = pos
r = pos+k

if r >= len(x):
    diff = r-len(x) + 1
    l -= diff
    r -= diff

ans = min(abs(x[l]), x[r])*2 + max(abs(x[l]), x[r])

while r >= pos:
    if l < 0:
        break

    dist = min(abs(x[l]), x[r])*2 + max(abs(x[l]), x[r])
    if dist < ans:
        ans = dist

    l -= 1
    r -= 1

print(ans)

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
for i in range(N):
    if X[i]<0:
        continue
    elif X[i]==0:
        K-=1
        if i<K:
            mX=[0]+[-x for x in X[:i]][::-1]
        else:
            mX=[0]+[-x for x in X[i-K:i]][::-1]
        pX=[0]+X[i+1:i+1+K]
        break
    else:
        if i<K:
            mX=[0]+[-x for x in X[:i]][::-1]
        else:
            mX=[0]+[-x for x in X[i-K:i]][::-1]
        pX=[0]+X[i:i+K]
        break
else:
    mX=[0]+[-x for x in X[::-1]]
    pX=[0]
ans=10**9
l=K-(len(pX)-1)
n=len(mX)
for i in range(len(pX)-1,-1,-1):
    if l<n:
        ans=min(ans,pX[i]+mX[l]*2,pX[i]*2+mX[l])
    else:
        break
    l+=1
print(ans)

Solution (Python):
N,K=map(int,input().split())
X=list(map(int,input().split()))
p=True
for i in range(N):
    if X[i]<0:
        continue
    elif X[i]==0:
        K-=1
        if i<K:
            mX=[0]+[-x for x in X[:i]][::-1]
        else:
            mX=[0]+[-x for x in X[i-K:i]][::-1]
        pX=[0]+X[i+1:i+1+K]
        p=False
        break
    else:
        if i<K:
            mX=[0]+[-x for x in X[:i]][::-1]
        else:
            mX=[0]+[-x for x in X[i-K:i]][::-1]
        pX=[0]+X[i:i+K]
        p=False
        break
if p:
    mX=[0]+[-x for x in X[::-1]]
    pX=[0]
ans=10**9
l=K-(len(pX)-1)
n=len(mX)
for i in range(len(pX)-1,-1,-1):
    if l<n:
        ans=min(ans,pX[i]+mX[l]*2,pX[i]*2+mX[l])
    else:
        break
    l+=1
print(ans)

Solution (Python):
import sys
import bisect


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    S = []
    ans = 10 ** 9 + 7

    start = bisect.bisect(X, 0)

    for i in range(min(start+1,K+1)):
        temp = 0
        if N - start < K-i:  
            continue

        if i ==0:
            ans = min(X[start+K-1],ans)
            continue

        if i ==K:
            ans =min(-X[start-K],ans)
            continue
        
        temp += -X[start - i] * 2
        temp += X[start + K - i-1]
        ans = min(temp, ans)

        

        temp = X[start + K - i-1] * 2
        temp += -X[start - i]
        ans = min(temp, ans)





    print(ans)




if __name__ == "__main__":
    main()

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

Solution (Python):
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    xs = [int(item) for item in input().split()]
    
    min_lr = 10**9
    min_rl = 10**9

    if N != K:
        for i in range(N-K+1):
            min_lr = min(min_lr, abs(xs[i]) + abs(xs[i+K-1] - xs[i]))
            min_rl = min(min_rl, abs(xs[i+K-1]) + abs(xs[i+K-1] - xs[i]))

        print(min(min_lr, min_rl))
    elif N ==1:
        print(abs(xs[0]))
    else:
        print(min(abs(xs[0]) + abs(xs[N-1] - xs[0]), abs(xs[N-1]) + abs(xs[N-1] - xs[0])))

if __name__ == "__main__":
    resolve()

Solution (Python):
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
import sys
if min(a)>=0:
  print(a[k-1])
  sys.exit()
if max(a)<=0:
  print((-1)*a[n-k])
  sys.exit()
  
ans=0
b=[]
for i in range(n-k+1):
  b.append(a[i+k-1]-a[i])

ans=float("INF")
for i in range(n):
  
  if i-k+1>=0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+min(b[i],b[i-k+1]))
  elif i-k+1<0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+b[i])
  elif i-k+1>=0 and i+k-1>n-1:
    ans=min(ans,abs(a[i])+b[i-k+1])
  else:
    ans=ans
    
print(ans)

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

Solution (Python):
import sys


MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    n, k = read_ints()
    x = read_int_list()
    ans = INF

    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]
        if left < 0 and right > 0:
            ans = min(ans, min(abs(left), abs(right)) * 2 + max(abs(left), abs(right)))
        else:
            ans = min(ans, max(abs(left), abs(right)))
    
    print(ans)
    
if __name__ == '__main__':
    Main()

Solution (Python):
import sys;          from decimal import Decimal
import math;         from numba import njit, i8, u1, b1
import bisect;       from itertools import combinations, product
import numpy as np;  from collections import Counter, deque, defaultdict


MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    n, k = read_ints()
    x = read_int_list()
    ans = INF

    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]
        if left < 0 and right > 0:
            ans = min(ans, min(abs(left), abs(right)) * 2 + max(abs(left), abs(right)))
        else:
            ans = min(ans, max(abs(left), abs(right)))
    
    print(ans)
    
if __name__ == '__main__':
    Main()

Solution (Python):
N,K=map(int,input().split())
*X,=map(int,input().split())
i=0
ans=[]
while i+K-1<N:
    a,b=X[i],X[i+K-1]
    if b<0:
        ans.append(abs(a))
    elif a>0:
        ans.append(b)
    else:
        a=abs(a)
        ans.append(a+b+min(a,b))
    i+=1
print(min(ans))

Solution (Python):
N,K=map(int,input().split())
*X,=map(int,input().split())
A=[]
B=[]
for x in X:
    if x>=0:
        A.append(x)
    else:
        B.append(abs(x))
B.sort()
i=min(K,len(A))
j=K-i
ans=[]
while i>=0 and j<=len(B):
    if i==0:
        ans.append(B[j-1])
    elif j==0:
        ans.append(A[i-1])
    else:
        a,b=A[i-1],B[j-1]
        ans.append(a+b+min(a,b))
    i-=1
    j+=1
print(min(ans))

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

res= 10**10

for i in range(n-k+1):
    st = al[i]
    en = al[i+k-1]
    if st *en <0:
        temp = min(abs(st),abs(en))*2+max(abs(st),abs(en))
    else:
        temp = max(abs(st),abs(en))
    res = min(temp,res)

print(res)

Solution (Python):
import sys
n,k=map(int,input().split())
x=list(map(int,input().split()))
left=[]
right=[]
origin=0
for u in x:
  if u<0:
    left.append(abs(u))
  elif u==0:
    origin+=1
  else:
    right.append(u)
left.sort()
k-=origin
if k==0:
  print(0)
  sys.exit()
r=len(right)
l=len(left)
ans=float('inf')
if k<=r:
  ans=right[k-1]
if k<=l:
  ans=min(ans,left[k-1])
for i in range(k-1):
  if i<r and k-i-2<l:
    y=right[i]+left[k-i-2]+min(right[i],left[k-i-2])
    ans=min(ans,y)
print(ans)