Problem:
 Score : 300 points    Problem Statement There are N candles placed on a number line.
The i -th candle from the left is placed on coordinate x_i .
Here, x_1 < x_2 < ... < x_N holds.  Initially, no candles are burning.
Snuke decides to light K of the N candles.  Now, he is at coordinate 0 .
He can move left and right along the line with speed 1 .
He can also light a candle when he is at the same position as the candle, in negligible time.  Find the minimum time required to light K candles.      Constraints  1 \leq N \leq 10^5  1 \leq K \leq N  x_i is an integer.  |x_i| \leq 10^8  x_1 < x_2 < ... < x_N         Input Input is given from Standard Input in the following format:  N  K  x_1  x_2  ...  x_N       Output Print the minimum time required to light K candles.        Sample Input 1 5 3
-30 -10 10 20 50      Sample Output 1 40  He should move and light candles as follows:   Move from coordinate 0 to -10 .  Light the second candle from the left.  Move from coordinate -10 to 10 .  Light the third candle from the left.  Move from coordinate 10 to 20 .  Light the fourth candle from the left.        Sample Input 2 3 2
10 20 30      Sample Output 2 20       Sample Input 3 1 1
0      Sample Output 3 0   There may be a candle placed at coordinate 0 .        Sample Input 4 8 5
-9 -7 -4 -3 1 2 3 4      Sample Output 4 10

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

ans=float('inf')
for i in range(n-k+1):
    nowx=(x[i],x[i+k-1])
    if nowx[0]>=0 and nowx[-1]>=0:
        ans=min(ans, nowx[-1])
    elif nowx[0]<=0 and nowx[-1]<=0:
        ans=min(ans, abs(nowx[0]))
    elif nowx[0]<0 and nowx[-1]>=0:
        ans=min(ans, 2*abs(nowx[0])+nowx[-1], abs(nowx[0])+2*nowx[-1])
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, r):
    lv = abs(l)
    rv = abs(r)
    if (l < 0) ^ (r < 0):
        return lv + rv + min(lv, rv)
    else:
        return max(lv, rv)

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

    ans = 10 ** 9
    for i in range(N - K + 1):
        tmp_ans = solve(x[i], x[i + K - 1])
        if tmp_ans < ans:
            ans = tmp_ans

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, m):
    if l < m:
        return l * 2 + m
    else:
        return l + m * 2

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))
    right_x = [i for i in x if i >= 0][:K]
    left_x = [-i for i in x if i < 0][-K:]

    left_len = len(left_x)
    right_len = len(right_x)
    if right_len < K:
        right_x += [10 ** 9 + 1] * (K - right_len)
    if left_len < K:
        left_x = [10 ** 9 + 1] * (K - left_len) + left_x

    right_x = [0] + right_x
    left_x += [0]

    ans = 10 ** 9
    for i in range(K + 1):
        tmp_ans = solve(left_x[i], right_x[i])
        if tmp_ans < ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()

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

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

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

print(time)

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

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


out = float('Inf')
for i in range(0, n - k + 1):
    ans = abs(a[i] - a[i + k - 1]) + min(abs(a[i]), abs(a[i + k - 1]))
    out = min(out, ans)
print(out)

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

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    ans = 10**10
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        l = X[i]
        r = X[i+K-1]
        if l < 0 and r > 0:
            c = min(abs(l) * 2 + r, abs(l) + r * 2)
        else:
            c = max(abs(l), abs(r))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        l = X[i]
        r = X[i+K-1]
        if l < 0 and r > 0:
            c = min(abs(l) * 2 + r, abs(l) + r * 2)
        else:
            c = max(abs(l), abs(r))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s.pop(0)
        s.append(X[i+K-1])
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s.pop(0)
        s.append(X[i+K-1])
        if len(s) != K:
            continue
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    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 = X[i]
    r = X[i + K - 1]

    if l >= 0:
        ans = min(ans, abs(r))
    elif r <= 0:
        ans = min(ans, abs(l))
    else:
        ans = min(ans, 2 * abs(r) + abs(l), 2 * abs(l) + abs(r))

print(ans)

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

def f(min1,max1):
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<=0 and max1 <=0:
    return abs(min1)
  else:
    return min([-2*min1 + max1,-min1 + 2*max1])

ans = 2*10**9
for i in range(N-K+1):
  min1 = xlist[i]
  max1 = xlist[i+K-1]
  dist =f (min1,max1) 
  if dist < ans :
    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):
    left = x[i]
    right = x[i+k-1]
    ans = min(ans,abs(left)+abs(right - left),abs(right) + abs(right - left))
print(ans)

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

    ans = 3 * 10 ** 8
    for left, right in zip(x, x[K - 1:]):
        if left * right < 0:
            ans = min(ans, right - left + min(-left, right))
        else:
            ans = min(ans, -left if left < 0 else right)
    print(ans)


if __name__ == '__main__':
    main()

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

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

print(min(d))

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

cur = 10**9
i = 0

while(i+k <= n):
    if(A[i+k-1] <= 0):
        tmp = -A[i]
    elif(A[i] <= 0):
        B = [-A[i], A[i+k-1]]
        B.sort()
        tmp = B[1] + 2 * B[0]
    else:
        tmp = A[i+k-1]
    cur = min(cur, tmp)
    i = i + 1

print(cur)

Solution (Python):
n,k=map(int,input().split())
X=list(map(int,input().split()))
for i in range(n):
    if X[i]>=0:
        break
else:
    i=n

if i==0:
    M=[]
else:
    M=X[i-1::-1]
if i==n:
    P=[]
else:
    P=X[i:]
    
ans=10**10
for j in range(k+1):
    if len(M)<j or len(P)<k-j:
        continue
    if j==0:
        ans=min(P[k-1],ans)
    elif j==k:
        ans=min(-M[k-1],ans)
    else:
        l=max(-M[j-1],P[k-j-1])
        s=min(-M[j-1],P[k-j-1])
        ans=min(s*2+l,ans)
print(ans)

Solution (Python):
import numpy as np
N,K=map(int, input().split())
X=list(map(int, input().split()))
ans=np.inf
for i in range(N-K+1):
  l=X[i]
  r=X[i+K-1]
  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**18
for i in range(N - K + 1):
    x1 = X[i]
    x2 = X[i + K - 1]
    if x1 * x2 >= 0:
        dist = max(abs(x1), abs(x2))
    else:
        dist = min(abs(x1), abs(x2)) + abs(x1 - x2)
    ans = min(ans, dist)
print(ans)

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

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 10 ** 18
    for i in range(K-1, N):
        a = X[i-K+1]
        b = X[i]
        if a < 0 and b > 0:
            ans = min(ans, (-a)*2+b, (-a)+b*2)
        elif b <= 0:
            ans = min(ans, -a)
        elif a >= 0:
            ans = min(ans, b)
    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, r = x[i], x[i+k-1]
    if r <= 0:
        a = abs(l)
    elif l >= 0:
        a = r
    else:
        a = min(abs(l), abs(r)) * 2 + max(abs(l), abs(r))
    if ans > a: ans = a
print(ans)

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    ans = 10**12
    for i in range(N-K+1):
        u = X[i]
        v = X[i+K-1]
        if u*v >= 0:
            ans = min(ans, max(abs(u), abs(v)))
        else:
            ans = min(ans, min(abs(u), v)*2+max(abs(u), v))
    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 = 10**12
    for i in range(N-K+1):
        u = X[i]
        v = X[i+K-1]
        if u*v >= 0:
            ans = min(ans, max(abs(u), abs(v)))
        else:
            u = -u
            ans = min(ans, min(u, v)*2+max(u, v))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

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

lst1 = list(map(int,readline().split()))
plus = []
minus = []
for i in lst1:
    if i < 0:
        minus.append(-i)
    else:
        plus.append(i)
minus.reverse() 
ans = 10**18
if len(minus) >= k:
    ans = min(ans,minus[k-1])
if len(plus) >= k:
    ans = min(ans,plus[k-1])

if len(plus) == 0 or len(minus) == 0:
    print(ans)
    exit()
for i in range(1,k):
    if i > len(plus) or k-i > len(minus):
        continue
    p = plus[i-1]
    m = minus[k-i-1]
    res = min(p*2+m,p+m*2)
    ans = min(ans,res)

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, abs(x[i]) + abs(x[i] - x[i+K-1]))
    ans = min(ans, 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()))
ans=10**18
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  ans=min(ans,min(abs(l)+abs(r-l),abs(r)+abs(l-r)))
print(ans)

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

ans = 10**18
for l, r in zip(X, X[K - 1:]):
    if r <= 0:
        ans = min(ans, abs(l))
    if l >= 0:
        ans = min(ans, abs(r))
    if l < 0 < r:
        ans = min(ans, r - l + min(abs(r), abs(l)))
print(ans)

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

    print(ans)

resolve()

Solution (Python):
import sys
input = sys.stdin.readline
INF = float("inf")


def main():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))
    if n == 1 and k == 1:
        print(0)
        exit()
    ans = INF
    for i in range(n - k + 1):
        l, r = X[i], X[i + k - 1]
        tmp = r - l
        if l * r < 0:
            tmp += min(abs(l), abs(r))
        else:
            tmp += min(abs(l), abs(r))
        ans = min(tmp, ans)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
minus = []
plus = []
ans = 10**100
for x in a:
    if x <0:minus.append(-x)
    else:plus.append(x)
minus = minus[::-1]
for i in range(min(k+1,len(minus)+1)):
    if k-i > len(plus):continue
    res = 0
    if i-1 >=0:res += minus[i-1]
    res*=2
    if k-i-1 >=0 and k-i <= len(plus):res += plus[k-i-1]
    ans = min(ans,res)
    res = 0
    if k-i-1 >=0 and  k-i <= len(plus):res += plus[k-i-1]
    res *=2
    if i-1 >=0:res += minus[i-1]
    ans = min(ans,res)
print(ans)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
minus = []
plus = []
ans = 10**100
for x in a:
    if x <0:minus.append(-x)
    else:plus.append(x)
minus = minus[::-1]
for i in range(min(k+1,len(minus)+1)):
    if k-i > len(plus):continue
    res = 0
    if i-1 >=0:res += minus[i-1]
    res*=2
    if k-i-1 >=0 and k-i <= len(plus):res += plus[k-i-1]
    ans = min(ans,res)
    res = 0
    if k-i-1 >=0 and  k-i <= len(plus):res += plus[k-i-1]
    res *=2
    if i-1 >=0:res += minus[i-1]
    ans = min(ans,res)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  


N, K = read_ints()
X = read_ints()






def solve(X):
    ret = 10**13
    for i in range(N):
        j = i + K - 1
        if j >= N:
            continue
        if X[i] <= 0 <= X[j]:
            ret = min(ret, -2 * X[i] + X[j])  
            ret = min(ret, 2 * X[j] - X[i])
    return ret


ans = solve(X)

i = bisect_left(X, 0) + K - 1
if i < N:
    ans = min(ans, X[i])

i = bisect_right(X, 0) - K
if -1 < i:
    ans = min(ans, -X[i])

print(ans)

Solution (Python):

import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))

def resolve():
    N,K=pin()
    X=tupin()
    Y=[]
    temp=X[0]
    ans=10000000000
    for i in range(N-K+1):
        temp=abs(X[i+K-1]-X[i])+min(abs(X[i+K-1]),abs(X[i]))
        
        ans=min(ans,temp)
    print(ans)    

resolve()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
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):
    a, b = X[i], X[i+K-1]
    
    if b <= 0:
        ans = min(abs(a), ans)
    elif 0 <= a:
        ans = min(abs(b), ans)
    else:
        tmp = min(abs(a), abs(b)) + (b - a)
        ans = min(ans, tmp)

print(ans)

Solution (Python):
def examA():
    N = I()
    ans = ((N+1)//2)/N
    print(ans)
    return

def examB():
    N, K = LI()
    H = [I()for _ in range(N)]
    H.sort()
    ans = inf
    for i in range(N-K+1):
        cur = H[i+K-1]-H[i]
        if ans>cur:
            ans = cur
    print(ans)
    return

def examC():
    N = I()
    A = LI()
    ans = [0]*N
    C = Counter(A)
    base = 0
    for c in C.values():
        base += c*(c-1)//2
    
    
    for i in range(N):
        ans[i] = base - (C[A[i]]-1)
    for v in ans:
        print(v)
    return

def examD():
    N, K = LI()
    X = LI()
    L = []; R = [0]
    for x in X:
        if x<0:
            L.append(-x)
        else:
            R.append(x)
    L.append(0)
    L = L[::-1]
    ans = inf
    for i in range(len(L)):
        if len(R)<=K-i or K-i<0:
            continue
        cur = min(L[i] * 2 + R[K - i], L[i] + R[K - i] * 2)
        
        if ans > cur:
            ans = cur
    print(ans)
    return

def examE():
    N, M = LI()
    X = [LI()for _ in range(N)]
    ans = 0
    for i1 in [-1,1]:
        for i2 in [-1,1]:
            for i3 in [-1,1]:
                curX = [0]*N
                for i in range(N):
                    curX[i] = X[i][0]*i1+X[i][1]*i2+X[i][2]*i3
                curX.sort(reverse=True)
                cur = sum(curX[:M])
                if ans<cur:
                    ans = cur
    print(ans)
    return

def examF():

    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

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

print(ans)

Solution (Python):
N,K=map(int,input().split())
L=list(map(int,input().split()))
ans=float("inf")
for i in range(N-K+1):
	ans=min(ans,(abs(0-L[i])+abs(L[i]-L[i+K-1])),(abs(0-L[i+K-1])+abs(L[i]-L[i+K-1])))
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):
    x1 = X[i]
    x2 = X[i + K - 1]
    if x1 * x2 >= 0:
        dist = max(abs(x1), abs(x2))
    else:
        dist = min(abs(x1), abs(x2)) + abs(x1 - x2)
    ans = min(ans, dist)
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):
  x_max=x[i+k-1]
  x_min=x[i]

  t_ans=x_max-x_min
  t_ans+=min(abs(x_max),abs(x_min))

  ans=min(ans,t_ans)

print(ans)

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]
    x.sort()
    answer = float("inf")
    if n == k:
        if n == 1:
            answer = abs(x[0])
        if 0 <= x[0]:
            answer = x[-1]
        elif x[-1] <= 0:
            answer = abs(x[0])
        else:
            answer = x[-1] - x[0] + min(abs(x[-1]), abs(x[0]))
    else:
        for i in range(n - k + 1):
            left = x[i]
            right = x[i + k - 1]
            if left * right >= 0:
                answer = min(answer, max(abs(left), abs(right)))
            else:
                answer = min(answer, right - left + min(abs(left), abs(right)))
    print(answer)


if __name__ == '__main__':
    main()

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

Solution (Python):
n,k = map(int,input().split())
X = list(map(int,input().split()))
Xp = [x for x in X if x>=0]
Xm = [-x for x in X if x<0]
Xm.sort()
Xp.insert(0,0)
Xm.insert(0,0)
lp = len(Xp)
lm = len(Xm)
ans = 10**9
for p in range(k+1):
    m = k-p
    if p>lp-1 or m>lm-1:continue
    ans= min(ans,2*Xm[m] +Xp[p],Xm[m] +2*Xp[p] )
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):
  dista = abs(x[i]) + abs(x[i+K-1] - x[i])
  distb = abs(x[i+K-1]) + abs(x[i+K-1] - x[i])
  dist = min(dista, distb)
  if(dist < ans):
    ans = dist
    
print(ans)

Solution (Python):
import sys

N, K, *X = map(int, sys.stdin.read().split())

ans = 10**20
for l, r in zip(X, X[K-1:]):
    if l*r < 0:
        dist = min(-2*l + r, 2*r - l)
    else:
        dist = max(abs(l), abs(r))
    if ans > dist:
        ans = dist
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):
    left = x[i]
    right = x[i + k - 1]
    ans = min(ans, abs(left) + abs(right - left), abs(right) + abs(right - left))

print(ans)

Solution (Python):
import sys
import math  
import bisect  
import queue  


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


def solve(X, K):
    N = len(X)

    cumsum = [0 for _ in range(N)]
    for i in range(1, N):
        cumsum[i] = abs(X[i] - X[i - 1]) + cumsum[i - 1]

    idx_zero = bisect.bisect_left(X, 0)

    ret = 1000000005
    for i in range(0, N):
        if X[i] >= 0:
            break
        res = -X[i]
        k = idx_zero - i
        if (k < K) and (idx_zero + K - k - 1 < N):
            res += cumsum[idx_zero + K - k - 1] - cumsum[i]
            ret = min(ret, res)

    return ret


if __name__ == '__main__':
    N, K = map(int, input().split())
    X = list(map(int, input().split()))

    res = 1000000005
    if min(X) >= 0:
        print(X[K - 1])
    elif max(X) <= 0:
        X = X[::-1]
        print(-X[K - 1])
    else:
        res = min(res, solve(X, K))
        X = [-x for x in X]
        res = min(res, solve(X[::-1], K))
        print(res)

Solution (Python):
import sys
import math  
import bisect  
import queue  


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


def solve(X, K):
    N = len(X)

    cumsum = [0 for _ in range(N)]
    for i in range(1, N):
        cumsum[i] = abs(X[i] - X[i - 1]) + cumsum[i - 1]

    idx_zero = bisect.bisect_left(X, 0)

    ret = 1000000005
    for i in range(0, N):
        if X[i] >= 0:
            break
        res = -X[i]
        k = idx_zero - i
        if (k < K) and (idx_zero + K - k - 1 < N):
            res += cumsum[idx_zero + K - k - 1] - cumsum[i]
            ret = min(ret, res)

    return ret


if __name__ == '__main__':
    N, K = map(int, input().split())
    X = list(map(int, input().split()))

    res = 1000000005
    if min(X) >= 0:
        print(X[K - 1])
    elif max(X) <= 0:
        X = X[::-1]
        print(-X[K - 1])
    else:
        res = min(res, solve(X, K))
        X = [-x for x in X]
        res = min(res, solve(X[::-1], K))
        print(res)

Solution (Python):


nim, mike = map(int, input().split())
array = list(map(int, input().split()))
k = float("INF")
for i in range(nim-mike+1):
	l = array[i]
	r = array[i+mike-1]
	k = min(k, abs(l)+abs(r-l), abs(r)+abs(r-l))
print(k)

Solution (Python):

















mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    X = readInts()
    m = float('inf')
    
    
    
    for i in range(n-k+1):
        l = X[i]
        r = X[k+i-1]
        m = min(m,min(abs(l),abs(r)) + r - l)
    print(m)
if __name__ == '__main__':
  main()

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

min = float('inf')
for i in range(N-K+1):
    left_cost = X[i]
    right_cost  = X[i+K-1]
    right_course = abs(right_cost)+abs(right_cost-left_cost)
    if min > right_course:
        min = right_course
    left_course = abs(left_cost)+abs(right_cost-left_cost)
    if min > left_course:
        min = left_course
        
print(min)

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

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

ans = 114514191981036436433-4

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

Solution (Python):
def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    if 0 in X:
        N -= 1
        K -= 1
        if K == 0:
            return 0
        X.remove(0)
    ans = 10**9
    for i in range(N-K+1):
        if X[i+K-1]<=0:
            p = -X[i]
        elif X[i]>=0:
            p = X[i+K-1]
        else:
            p = X[i+K-1]-X[i]+min(X[i+K-1],abs(X[i]))
        ans = min(ans,p)
    return ans
print(solve())

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
mn = float("inf")
for i in range(n-k+1):
  if x[i] <= 0 and 0 <= x[i+k-1]:
    if x[i+k-1]-x[i] + min(abs(x[i+k-1]),abs(x[i])) < mn:
      mn = x[i+k-1]-x[i] + min(abs(x[i+k-1]),abs(x[i]))
  elif x[i+k-1] <= 0:
    if mn > -x[i]:
      mn = -x[i]
  else:
    if mn > x[i+k-1]:
      mn = x[i+k-1]
    break
print(mn)

Solution (Python):
def c_candles():
    N, K = [int(i) for i in input().split()]
    X = [int(i) for i in input().split()]

    ans = float('inf')
    
    
    for left in range(N - K + 1):
        right = left + K - 1
        dist1, dist2 = min(abs(X[left]), abs(X[right])), abs(X[right] - X[left])
        ans = min(ans, dist1 + dist2)
    return ans

print(c_candles())

Solution (Python):
import sys

def main(lines):
  N, K = map(int, lines[0].split())
  a = list(map(int, lines[1].split()))
  ans = 1000000000000
  for i in range(N):
    if i + K - 1 >= N:
      break
    b = a[i]
    c = a[i + K - 1]
    if b < 0 and c >= 0:
      ans = min(ans, max(abs(b), abs(c)) + 2*min(abs(b), abs(c)))
    else:
      ans = min(ans, max(abs(b), abs(c)))
  print(ans)


if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

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

min_ = None
for i in range(n-k+1):
  res = min(abs(xs[i]) + abs(xs[i+k-1] - xs[i]), abs(xs[i+k-1]) + abs(xs[i] - xs[i+k-1]))
  if min_ is None:
    min_ = res
  else:
    min_ = min(res, min_)
print(min_)

Solution (Python):
N, K = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
MIN = 10 ** 10
for i in range(N-K+1):
    if x[i+K-1] <= 0:
        MIN = min(MIN, -1 * x[i])
    elif 0 <= x[i]:
        MIN = min(MIN, x[i+K-1])
    else:
        MIN = min(MIN, -1 * x[i] + x[i+K-1] + min(-1 * x[i], x[i+K-1]))
print(MIN)

Solution (Python):
import sys

n, k, *x = map(int, sys.stdin.read().split())

def main():
    res = float('inf')
    for i in range(n - k + 1):
        j = i + k - 1
        if x[i] < 0:
            d = -x[i] if x[j] <= 0 else min(-x[i], x[j]) * 2 + max(-x[i], x[j])
        else:
            d = x[j]
        res = min(res, d)
    print(res)
    
if __name__ ==  '__main__':
    main()

Solution (Python):





mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    """
    n k
    x1, x2, ... xn

    5 3
    -30 -10 10 20 50
    K個は連続する区間で考えればよい。
    区間を決めると、原点から最初に負の方向に行くか、
    正の方向に行くかはすぐに決められる

    つまり
    k = 3なら
    [-30, -10, 10], [-10, 10, 20], [10, 20, 50]
    の３つに分けるっていうこと？
    """
    n,k = readInts()
    X = readInts()

    res = float('inf')
    for i in range(n-k+1):
        left = X[i]
        right = X[i+k-1]
        
        
        
        
        res = min(res, min(abs(left), abs(right)) + right - left)
    print(res)



if __name__ == '__main__':
  main()