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



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Accepted

Solution code:
import sys

import bisect

# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd

# import itertools

# from operator import attrgetter, itemgetter
# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    ans = INF

    for i in range(n - k + 1):
        left = i
        right = left + k - 1

        temp = min(abs(x[left]) + abs(x[right] - x[left]), abs(x[right]) + abs(x[left] - x[right]))
        ans = min(ans, temp)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

import bisect

# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd

# import itertools

# from operator import attrgetter, itemgetter
# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    ans = INF
    if 0 in x:
        start = x.index(0)
        leftlim = max(0, start - k + 1)
        rightlim = min(start + k - 1, n - 1)
        for i in range(leftlim, start + 1):
            left = i
            right = left + k - 1
            if right > rightlim:
                break
            if x[left] * x[right] < 0:
                temp = min(2 * abs(x[left]) + abs(x[right]), abs(x[left]) + 2 * abs(x[right]))
            else:
                if x[left] < 0:
                    temp = abs(x[left])
                else:
                    temp = abs(x[right])
            ans = min(ans, temp)
    else:
        start = bisect.bisect_left(x, 0)

        leftlim = max(0, start - k)
        rightlim = min(start + k - 1, n - 1)

        for i in range(leftlim, start + 1):
            left = i
            right = left + k - 1
            if right > rightlim:
                break

            if x[left] * x[right] < 0:
                temp = min(2 * abs(x[left]) + abs(x[right]), abs(x[left]) + 2 * abs(x[right]))
            else:
                if x[left] < 0:
                    temp = abs(x[left])
                else:
                    temp = abs(x[right])
            ans = min(ans, temp)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

dist_list=[]
for i in range(N-K+1):
  #print(xlist[i+K-1]-xlist[i])
  if xlist[i+K-1]<0:
    dist_list.append(-xlist[i])
  elif xlist[i]>0:
    dist_list.append(xlist[i+K-1])
  else:
    dist_list.append(xlist[i+K-1]-xlist[i]+min(-xlist[i],xlist[i+K-1]))
    
#print(dist_list)
print(min(dist_list))

Submission status: Accepted

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

ans = float('inf')
for L in range(n-k+1):
    R = L + k
    if A[L] < 0 and A[R-1] < 0:
        ans = min(ans, abs(A[L]))
    elif A[L] < 0 and 0 <= A[R-1]:
        ans = min(ans, min(abs(A[L]), abs(A[R-1]))*2 + max(abs(A[L]), abs(A[R-1])))
    else:
        ans = min(ans, abs(A[R-1]))
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = float('inf')

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

Submission status: Accepted

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

ans = 10 ** 9
for i in range(n - k + 1):
    x_min = X[i]
    x_max = X[i + k - 1]

    if x_min <= 0 <= x_max:
        dist_min = min(abs(x_min), x_max)
        dist_max = max(abs(x_min), x_max)

        ans = min(ans, 2 * dist_min + dist_max)
    else:
        ans = min(ans, max(abs(x_min), abs(x_max)))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#107c
import bisect
N,K = map(int, input().split()) #N<=10**5
A_list = [int(e) for e in input().split()]

#折り返しは1回が最大→どこで折り返すかをN全探索でOK

#わかりやすいように0がなければ0を入れて、位置を調べる。
if 0 in A_list:
    zero_index = bisect.bisect_left(A_list, 0)
    K -= 1
else:
    bisect.insort(A_list, 0)
    zero_index = bisect.bisect_left(A_list, 0)
    N += 1
    
#print(A_list)
#print(zero_index)

if K == 0:
    ans = 0
    turn_index = 0
else:
    ans = 10**100+7
    
    for i in range(N):
        if abs(zero_index - i) <= K and i != zero_index:
            if abs(zero_index - i) == K:
                time = abs(A_list[i])
                if ans > time:
                    ans = time
                    #turn_index = i
            elif abs(zero_index - i) < K:
                #K付けられていないので折り返しの必要がある
                time = abs(A_list[i])*2 #原点まで戻る
                remain = K - abs(zero_index - i)
                
                if zero_index > i and zero_index + remain < N:
                    #折り返し地点が0より左側
                    time += A_list[zero_index + remain]
                    if ans > time:
                        ans = time
                        #turn_index = i
                elif zero_index < i and zero_index - remain >= 0:
                    #折り返し地点が0より右側
                    time += abs(A_list[zero_index - remain])
                    if ans > time:
                        ans = time
                        #turn_index = i

#print("#########")
#print(turn_index)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,(input().split()))
x = list(map(int,(input().split())))
n = [0]
p = [0]
ans = []
if 0 in x:
    K -= 1
for i in range(N):
    if x[i] < 0:
        n.append(x[i])
    if x[i] > 0:
        p.append(x[i])
n.sort(reverse=True)
for i in range(K+1):
    if i < len(n) and K-i < len(p):
        ans.append(min(abs(n[i])*2 + p[K-i], abs(n[i]) + p[K-i]*2))
print(min(ans))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
ans=10**10
for i in range(n+1-k):
  s=A[i]
  t=A[i+k-1]
  if s*t>=0:
    a=max(abs(s),abs(t))
  else:
    a=max(abs(s),abs(t))+min(abs(s),abs(t))*2
  if a<ans:
    ans=a
print(ans)

Submission status: Accepted

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N,K=map(int,input().split())
    X=list(map(int,input().split()))
    mintime=10**10
    for i in range(N-K+1):
        l=X[i]
        r=X[i+K-1]
        mintime=min(min(abs(l)+r-l,abs(r)+r-l),mintime)
    print(mintime)

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    x=list(map(int,readline().split()))

    res=float("inf")
    for i in range(n):
        j=i+k-1
        if j>=n: break
        if x[i]*x[j]<0:
            res=min(res,-x[i]+x[j]+min(-x[i],x[j]))
        elif x[j]<=0:
            res=min(res,-x[i])
        elif x[i]>=0:
            res=min(res,x[j])

    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
INF = 1000000000
N, K = list(map(int, input().split()))
items = list(map(int, input().split()))

mi = INF

for l_index in range(N-K+1):
  left = items[l_index]
  right = items[l_index+K-1]

  if right <= 0:
    mi = min(mi, 0-left)
  elif left >= 0:
    mi = min(mi, right)
  else:
    min_l = min(0-left, right)
    mi = min(mi, (0-left) + right + min_l)

print(mi)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

n, k = map(int, input().split())
x = [int(c) for c in input().split()]

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

Submission status: Accepted

Solution code:
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(r-l)), abs(r)+abs(l-r)))
print(min(ans))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
al=[]
for i in range(N-K+1):
  if x[i]*x[i+K-1]<0:
    al.append(max(abs(x[i]),abs(x[i+K-1]))+2*min(abs(x[i]),abs(x[i+K-1])))
  elif x[i]>=0:
    al.append(x[i+K-1])
  else:
    al.append(abs(x[i]))
print(min(al))

Submission status: Accepted

Solution code:
#!python3

import numpy as np

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


def main():
    k = K - np.count_nonzero(x == 0)
    if k <= 0:
        print(0)
        return
    m, p = x[x < 0] * -1, x[x > 0]
    mn, pn = len(m), len(p)
    m.sort()
    m, p = np.append(0, m), np.append(0, p)
    
    ans = int(1e15)
    # first left
    for i in range(min(mn, k) + 1):
        j = k - i
        if j > pn:
            continue
        d = m[i] * 2 + p[j]
        ans = min(ans, d)
    
    # first right
    for i in range(min(pn, k) + 1):
        j = k - i
        if j > mn:
            continue
        d = p[i] * 2 + m[j]
        ans = min(ans, d)
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    result = []

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

    print(min(result))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
X = sorted([int(i) for i in input().split()])

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

    minv = min(minv, r-l+abs(l))
    minv = min(minv, r-l+abs(r))

print(minv)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
#input = sys.stdin.readline


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
input = sys.stdin.readline


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split());print(min([r-l+min(abs(l),abs(r))for l,r in zip(a,a[k-1:])]))

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split());print(min([a[i+k-1]-a[i]+min(abs(a[i+k-1]),abs(a[i]))for i in range(n-k+1)]))

Submission status: Accepted

Solution code:
n, k, *a = map(int, open(0).read().split())
ans = 1e11
for i in range(n-k+1):
    ans = min(ans, a[i+k-1]-a[i]+min(abs(a[i+k-1]),abs(a[i])))
print(ans)

Submission status: Accepted

Solution code:
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:
    dup = min(abs(X[i]), abs(X[i + k - 1])) * 2
    sgl = max(abs(X[i]), abs(X[i + k - 1]))
    dist = dup + sgl
  else:
    dist = max(abs(X[i]), abs(X[i + k - 1]))
  ans = min(ans, dist)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
l = 0
r = k-1
ans = 0
if x[l] <= 0 and x[r] <= 0:
    ans = abs(x[l])
elif x[l] >= 0 and x[r] >= 0:
    ans = x[r]
else:
    ans = min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r])
for i in range(n-k):
    l += 1
    r += 1
    if x[l] <= 0 and x[r] <= 0:
        ans = min(ans,abs(x[l]))
    elif x[l] >= 0 and x[r] >= 0:
        ans = min(ans, x[r])
    else:
        ans = min(ans, min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
Y = []
Z = []
zc = 0
for i in range(N):
    if X[i] > 0:
        Y.append(X[i])
    if X[i] == 0:
        zc = 1
for i in range(N-1, -1, -1):
    if X[i] < 0:
        Z.append(-X[i])

cs = []
K -= zc
if K == 0:
    print(0)
else:
    if len(Y) >= K:
        cs.append(Y[K-1])
    if len(Z) >= K:
        cs.append(Z[K-1])
    for i in range(min(K-1, len(Y))):
        if K-i-2 < len(Z):
            cs.append(2*Y[i]+Z[K-i-2])
    for i in range(min(K-1, len(Z))):
        if K-i-2 < len(Y):
            cs.append(2*Z[i]+Y[K-i-2])
    print(min(cs))

Submission status: Accepted

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

ans=float('inf')
for i in range(n-k+1):
    temp=x[i+k-1]-x[i]+min(abs(x[i]),abs(x[i+k-1]))
    if ans>temp:ans=temp
print(ans)

Submission status: Accepted

Solution code:
# なぜかうまくいかない
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 fractions import gcd
from bisect import bisect, bisect_right, bisect_left

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()
A = LIST()
# 0 0 0の左
zero = A.count(0)
# A.(0)
l = bisect_left(A, 0)
r = bisect(A, 0)
zero = r - l
C = []
for a in A[:l]:
    if a == 0:
        continue
    C.append(a)
    
B = C + A[r:]


w = K - zero
ans = INF
B.insert(0, -INF)
B.append(INF)
if zero == K:
    print(0)
    exit()

for i in range(len(B) - K + 1):
    r = B[i + K - zero - 1]
    l = B[i]
    tmp = INF
    if l < 0 and r > 0:
        tmp = min(abs(l), r) + r - l
    # ans = min(ans, tmp)
    elif l > 0 and B[i-1] < 0:
        # tmp = min(abs(l), r) + r - l
        tmp = r

    elif r < 0 and B[i + K - zero - 1 + 1] > 0:
        # tmp = min(abs(l), r) + r - l
        tmp = abs(l)
    ans = min(ans, tmp)
print(ans)


        # if abs(B[i]) <= :

#         tmp = B[i + K - zero-1] - B[i]
#         print(B[i+K-zero-1])
#         print(B[i])
#         ans = min(ans, tmp)
# print(ans)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n, k = map(int, input().split())
    x = list(map(int, input().split()))

    x.sort()
    xmin = x[0]
    xmax = x[k-1]

    ans = 0
    if xmin < 0 and xmax >= 0:
        ans = min([abs(xmin), abs(xmax)]) * 2 + max([abs(xmin), abs(xmax)])
    else:
        ans = max([abs(xmin), abs(xmax)])

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

    print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
X = [int(i) for i in input().split()]

ans = float("inf")
for left in range(N - K + 1):
    right = left + K - 1

    if X[left] <= 0 and X[right] <= 0:
        tmp = -X[left]
    elif X[left] >= 0 and X[right] >= 0:
        tmp = X[right]
    else:
        if abs(X[left]) >= abs(X[right]):
            tmp = abs(X[right]) * 2 + abs(X[left])
        else:
            tmp = abs(X[left]) * 2 + abs(X[right])
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k = map(int,input().split())
X = list(map(int,input().split()))
for i in range(n-k+1):
  fi = X[i]
  en = X[i+k-1]
  if fi <= 0 and en >= 0:
    mans = min(mans,min(-fi,en)-fi+en)
  else:
    mans=min(mans,max(abs(fi),abs(en)))
print(mans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ret = 10 ** 9
for i in range(N-K+1):
    width = X[i+K-1] - X[i]
    mi = min(abs(X[i+K-1]), abs(X[i]))
    ret = min(ret, width+mi)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
X = list(map(int, input().split()))
P = []
N = []
for x in X:
    if x >= 0:
        P.append(x)
    else:
        N.append(x)

P.sort()
N.sort(reverse=True)
P = [0]+P
N = [0]+N
ans = 10**16
for a in range(k+1):
    b = k-a
    if a > len(P)-1 or b > len(N)-1:
        continue
    else:
        ans = min(ans, min(2*P[a]+(-1)*N[b], P[a]+2*(-1)*N[b]))
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
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 fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
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 = sys.maxsize
mod = 10 ** 9 + 7

n, k = MAP()
x = LIST()
idx = -1
for i in range(n - 1):
    if x[i] < 0 and x[i + 1] >= 0:
        idx = i + 1
if idx == -1 and x[-1] < 0:
    print(-x[len(x) - k])
elif idx == -1 and x[0] >= 0:
    print(x[k - 1])
elif idx != -1:
    ans = inf
    for i in range(n - k + 1):
        tmp = abs(x[i]) + x[i + k - 1] - x[i]
        ans = min(ans, tmp)
    for i in range(k - 1, n):
        tmp = abs(x[i]) + x[i] - x[i - k + 1]
        ans = min(ans, tmp)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = 10**12
for i in range(N - K + 1):
    l = A[i]
    r = A[i + K - 1]
    d = r - l + min(abs(r), abs(l))
    ans = min(d, ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cost = float("inf")
for i in range(n-k+1):
  j = i + k - 1
  cost = min(
    cost,
    abs(xx[i] - 0) + abs(xx[j] - xx[i]),
    abs(xx[j] - 0) + abs(xx[i] - xx[j])
  )
print(cost)

Submission status: Accepted

Solution code:
from sys import maxsize

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

time = maxsize
for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    val = min(abs(l)+abs(r-l), abs(r)+abs(l-r))
    if val < time:
        time = val

print(time)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
X = [int(i) for i in input().split()]

ans = float("inf")

for left in range(N-K+1):
    right = left + K - 1

    if X[left] <=0 and X[right] <= 0:
        cur = abs(X[left])
    elif X[left] >= 0 and X[right] >= 0:
        cur = abs(X[right])
    else:
        if abs(X[left]) >= abs(X[right]):
            cur = abs(X[right]) * 2 + abs(X[left])
        else:
            cur = abs(X[left]) * 2 + abs(X[right])
    ans = min(ans, cur)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if x[K-1]*x[0]>0:
    tmp_min = max(abs(x[0]),abs(x[K-1]))
else:
    tmp_min = abs(x[0]-x[K-1])+min(abs(x[0]),abs(x[K-1]))

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

Submission status: Accepted

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

ans = 10**20
for i in range(N-K+1):
    fst = i
    lst = i+K-1

    if X[lst]<=0:
        ans = min(ans,abs(X[fst]))
    elif X[fst]>=0:
        ans = min(ans,X[lst])
    else:
        dis = max(abs(X[fst]),X[lst])+min(abs(X[fst]),X[lst])*2
        ans = min(ans,dis)

print(ans)

Submission status: Accepted

Solution code:
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(l-r)))
print(min(a))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ans = 10**N+1
distance = 0
for i in range(N-K+1):
  distance = X[i+K-1]-X[i]
  ans = min(abs(X[i])+distance,abs(X[i+K-1])+distance,ans)
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans=[0]*(n-k+1)
# zip関数は多い元は無視する.
for i, a, b in zip(range(n), x, x[k-1:]):
    ans[i] = b-a + min(abs(a), abs(b))
print(min(ans))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans=4*10**8
# zip関数は多い元は無視する.
for a, b in zip(x, x[k-1:]):
    cand = b-a + min(abs(a), abs(b))
    ans = min(cand, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

N,K = list(map(int, input().rstrip().split()))
x_list = list(map(int, input().rstrip().split()))
#-----

cnt_time = (10**8)*K
for l in range(N-K+1):
    r=l+K-1

    route1 = abs(x_list[l]) + abs(x_list[r] - x_list[l])
    route2 = abs(x_list[r]) + abs(x_list[r] - x_list[l])
    cnt_time = min( cnt_time, route1, route2 )

print(cnt_time)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
from collections import Counter
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#######ここから天ぷら########
n,k=map(int,input().split())
A=list(map(int,input().split()))
#n,k=5,3
#A=[-30,-10,10,20,40]
ans=inf
for i in range(n-k+1):
    ans=min(ans, abs(A[i])+abs(A[i]-A[i+k-1]), abs(A[i+k-1])+abs(A[i+k-1]-A[i]) )
print(ans)

Submission status: Accepted

Solution code:
def submit():
    n, k = map(int, input().split())
    xlist = list(map(int, input().split()))

    min_cost = float('inf')
    for i in range(n - k + 1):
        st = i
        ed = i + k - 1
        
        to_start = min(abs(xlist[st]), abs(xlist[ed]))
        start_end = xlist[ed] - xlist[st]

        if min_cost > to_start + start_end:
            min_cost = to_start + start_end

    print(min_cost)
        

if __name__ == "__main__":
    submit()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
  n,k=map(int,input().split())
  x=list(map(int,input().split()))
  ans=pow(10,10)
  for i in range(0,n-k+1):
    if x[i]*x[i+k-1]>=0:
      ans=min(ans,max(abs(x[i+k-1]),abs(x[i])))
    else:
      ans=min(ans,min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1])))
  print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
lst = [int(x) for x in input().split()]
ret = 100 ** 100
for i in range(N - K + 1):
    if lst[i] >= 0 and lst[i + K - 1] >= 0:
        t = lst[i + K - 1]
    elif lst[i] < 0 and lst[i + K - 1] < 0:
        t = abs(lst[i])
    else:
        t = lst[i + K - 1] - lst[i] + min(abs(lst[i]), abs(lst[i + K - 1]))
    ret = min(ret, t)
print(ret)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def get_cost(min_v,max_v):
	if max_v<0:
		return abs(min_v)
	elif min_v >=0:
		return max_v
	else:
		ret = min(max_v+abs(min_v)*2, abs(min_v)+ max_v*2)
		return ret

def main():
	n, k = IN()
	al=list(IN())
	ret=min([get_cost(a,b) for a,b in zip(al,al[k-1:])])
	return ret
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

answer = 10 ** 18
for i in range(N - K + 1):
    left, right = i, i + K - 1
    al, ar = abs(X[left]), abs(X[right])
    if X[left] * X[right] >= 0:
        answer = min(answer, max(al, ar))
    else:
        answer = min(answer, al * 2 + ar, al + ar * 2)
        
print(answer)