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:
N,K = map(int,input().split())
X = list(map(int,input().split()))

X.sort()
Ans = 10**20
for i in range(N-K+1):
    l = X[i]
    r = X[i+K-1]
    # 0を含む または 同じ符号の場合
    if l*r >= 0:
        ans = max(abs(l),(r))
    # 符号が異なる場合
    else:
        ans = max(abs(l),abs(r)) + min(abs(l),abs(r))*2
    Ans = min(Ans,ans)

print(Ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = [int(xi) for xi in input().split()]
best = float('inf')
for i in range(N - K + 1):
    start = x[i]
    end = x[i+K-1]
    near = min(start, end)
    if end <= 0:
        dist = abs(start)
    elif 0 <= start:
        dist = abs(end)
    else:
        if abs(start) < abs(end):
            dist = abs(start) * 2 + abs(end)
        else:
            dist = abs(end) * 2 + abs(start)
    best = min(dist, best)
if best == float('inf'):
    print(0)
else:
    print(best)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n, k = map(int, input().split())
x_tmp = input().split()
x = [int(_) for _ in x_tmp]
ans = []


def abs(x):
    '''絶対値をint型で返す
    return int-type abs-x
    '''
    return int(math.fabs(x))


for i in range(n-k+1):
    ans_left = abs(x[i]) + abs(x[i+k-1] - x[i])
    ans_right = abs(x[i+k-1]) + abs(x[i+k-1] - x[i])
    ans.extend([ans_left, ans_right])
print(min(ans))

Submission status: Accepted

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

pos = [int(x) for x in input().split()]

ans = abs(pos[0])+abs(pos[0]-pos[k-1])

for i in range(n-k+1):
    val = 0
    val += abs(pos[i] - pos[i+k-1])
    val += min(abs(pos[i]), abs(pos[i+k-1]))

    if val < ans:
        ans = val
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans_list = []
for i in range(N-K+1):
    l = xi[i]
    r = xi[i+K-1]
    if l*r < 0:
        a = abs(l)*2 + abs(r)
        b = abs(r)*2 + abs(l)
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif l < 0:
        ans_list.append(abs(l))
    else: #if li[-1] > 0:
        ans_list.append(abs(r))

print(min(ans_list))

Submission status: Accepted

Solution code:
# coding:utf-8

INF = float('inf')


def inpl(): return list(map(int, input().split()))


N, K = inpl()
X = inpl()

A = [0]
B = []
for i in range(N):
    if X[i] >= 0:
        A.append(X[i])
    else:
        B.append(-X[i])
B.append(0)
B = B[::-1]

ans = INF
for i in range(K + 1):
    cost = INF
    j = K - i
    if i > len(A) - 1 or j > len(B) - 1: continue

    cost = A[i] + B[j] + min(A[i], B[j])
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

lpos = 0
rpos = K-1

near = abs(xlist[lpos]) if (abs(xlist[lpos]) < abs(xlist[rpos])) else abs(xlist[rpos])
bet = abs(xlist[lpos] - xlist[rpos])

ans = bet + near

for i in range(N-K):
    lpos += 1
    rpos += 1

    near = abs(xlist[lpos]) if (abs(xlist[lpos]) < abs(xlist[rpos])) else abs(xlist[rpos])
    bet = abs(xlist[lpos] - xlist[rpos])

    tmp = bet+near

    if tmp < ans:
        ans = tmp


print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8


N,K  = map(int,input().split())
moto = input().split()
for i in range(N):
    moto[i] = int(moto[i])

def real(n):
    if n >= 0:
        return n 
    else:
        return -1*n 

l = []
for i in range(N-K+1):
    l.append(real(moto[i]) + real(moto[i+K-1]-moto[i]))
    l.append(real(moto[i+K-1])+real(moto[i+K-1]-moto[i]))
print(min(l))

Submission status: Accepted

Solution code:
# coding: utf-8

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

#Xの中に0が含まれていた場合、0を含まない状態へと各変数を書き換える。
if X.count(0) == 1:
    n -= 1
    k -= 1
    X.remove(0)

#0を除いた0が空である場合は、0を返す    
if X == []:
    print(0)
else:
    #Xを、負の要素からなるminusと、正の要素からなるplusに分割する。
    minus = list(filter(lambda x : x < 0, X ))
    plus = list(filter(lambda x : x > 0, X ))
    
    #負にi本、正にk-i本つけるのにかかる時間をt[i]に格納する。
    t =[]
    for i in range(k+1):
        #負にi本、正にk-i本つけることが不可能ならt[i]に無限大を格納する。
        if len(minus) < i or len(plus) < k-i:
            t.append(float("inf"))
        else:
            #正方向に行くだけの場合
            if i == 0:
                t.append(plus[k-1])
            #負方向に行くだけの場合    
            elif i == k:
                t.append(-minus[-i])
            #正負どちらにも行く場合
            else:
                t.append(min(-minus[-i], plus[k-i-1])*2 + max(-minus[-i], plus[k-i-1]))
    
    print(min(t))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

t = float('inf')

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

print(t)

Submission status: Accepted

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

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

answer=10**9

for i in range(N-K+1):
  left=candle[i]
  right=candle[i+K-1]
  move=max(abs(left),abs(right))
  if left*right<0:
    move+=min(abs(left),abs(right))*2
  answer=min(move,answer)

print(answer)
  

'''
getcandle=0
for i in range(N):
  if candle[i]>=0:
    plusindex=i
    if candle[i]!=0:
      candle.insert(i,0)
    else:
      getcandle+=1
    break;

print(candle)
distance=[]
for i in range(len(candle)-1):
  distance.append(abs(candle[i]-candle[i+1]))
print(distance)

'''

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
candles = list(map(int, input().split()))
ans = 10 ** 9
for i in range(n - m + 1):
    a = candles[i]
    b = candles[i + m - 1]
    x = max(abs(a), abs(b))
    if a < 0 and b > 0:
        x += min(abs(a) * 2, abs(b) * 2)
    ans = min(ans, x)

print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().strip().split(" ")))
candles = input().strip()
min_cost_index = candles.count("-")
candles = list(map(int, candles.split(" ")))

def update():
    start_index = max(0, min_cost_index - K - 1)
    end_index = min(N, min_cost_index + K + 1)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(start_index):
    start_point = candles[start_index]
    end_point = candles[start_index + K - 1]
    if(start_point * end_point >= 0):
        return max(abs(start_point), abs(end_point))
    else:
        return end_point - start_point + min(-start_point, end_point) 

candles = update()

N = len(candles)
min_cost = compute_cost(0)
patterns = N - K + 1

for start_index in range(patterns):
    cost = compute_cost(start_index)
    if(min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Accepted

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

k = 0
while k < N and X[k] < 0:
    k += 1
P = X[:k]; Q = X[k:]
ans = 10**30
if len(P) >= K:
    ans = min(ans, -P[-K])
if len(Q) >= K:
    ans = min(ans, Q[K-1])
for i in range(1, K):
    if len(P) < i or len(Q) < K-i:
        continue
    p = -P[-i]; q = Q[K-i-1]
    ans = min(ans, min(2*p+q, p+2*q))
print(ans)

Submission status: Accepted

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

Y = []
Z = []

for x in X:
    if x == 0:
        Y.append(0)
        Z.append(0)
    elif x < 0:
        Y.append(abs(x))
    else:
        Z.append(x)

ans = sum(Y) + sum(Z)
Y.reverse()

for i, y in zip(range(1, len(Y)+1), Y):
    if i <= k and k - i <= len(Z):
        ans = min(ans, y + (0 if i == k else 2 * Z[k-(i+1)]))

for i, z in zip(range(1, len(Z)+1), Z):
    if i <= k and k - i <= len(Y):
        ans = min(ans, (0 if i == k else 2 * Y[k-(i+1)]) + z)

print(ans)

Submission status: Accepted

Solution code:
#encoding utf 8

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

pos = list(int(i) for i in input().split())

ans = abs(pos[0])+abs(pos[0]-pos[k-1])

for i in range(n-k+1):
    val = 0
    val += abs(pos[i] - pos[i+k-1])
    val += min(abs(pos[i]), abs(pos[i+k-1]))

    if val < ans:
        ans = val
print(ans)

Submission status: Accepted

Solution code:
# 無駄なところも数えて良いっていう考え方もある。
# 常に最適化しないといけないわけではない

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

time = 10 ** 20

for i in range(N - K + 1):
    x_min = x[i]
    x_max = x[i + K - 1]
    if x_min >= 0:
        this_time = x_max
    elif x_max <= 0:
        this_time = abs(x_min)
    else:
        this_time = x_max - x_min + min(x_max, abs(x_min))

    if time > this_time:
        time = this_time

print(time)

Submission status: Accepted

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

ans = 10**18
for i in range(N - K + 1):
    p1 = x[i]
    p2 = x[i+K-1]
    lr = abs(p1) + abs(p2 - p1)
    rl = abs(p2) + abs(p1 - p2)
    if lr < rl:
        ans = min(ans,lr)
    else:
        ans = min(ans,rl)
print(ans)

Submission status: Accepted

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

ans = abs(x[0]) + abs(x[K-1] - x[0])
for i in range(N-K+1):
    xl = x[i]
    xr = x[i+K-1]
    tmp = abs(xl) + abs(xr - xl)
    if tmp > abs(xr) + abs(xr - xl):
        tmp = abs(xr) + abs(xr - xl)
    if (ans > tmp):
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
import bisect
import sys
 
N, K = map(int, raw_input().split())
x = map(int, raw_input().split())
res = [sys.maxint] * N
 
pos = 0
f_pos = bisect.bisect_left(x, 0)
x.insert(f_pos, 0)
result = sys.maxint
if f_pos + K <= N:
	result = min(result, x[f_pos + K])
if f_pos - K >= 0:
	result = min(result, -x[f_pos - K])
for i in range(K):
	if f_pos + i <= N and f_pos - K + i >= 0:
		result = min(result, x[f_pos + i] - x[f_pos -  K + i] + min(x[f_pos + i], -x[f_pos - K + i]))
print result

Submission status: Accepted

Solution code:
import bisect
import sys

N, K = map(int, raw_input().split())
x = map(int, raw_input().split())
res = [sys.maxint] * N

pos = 0
f_pos = bisect.bisect_left(x, 0)
x.insert(f_pos, 0)
result = sys.maxint
for i in range(N + 1):
	if i - K >= 0:
		result = min(result, abs(x[i]) + abs(x[i - K]) + min(abs(x[i]), abs(x[i - K])))
print result

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        l = x[bottom + i]
        r = x[bottom + i + K - 1]
        if l < 0 <= r:
            best = min(best, 2 * min(abs(l), r) + max(abs(l), r))
        elif l < 0 and r < 0:
            best = min(best, abs(l))
        elif 0 <= l and 0 <= r:
            best = min(best, abs(r))

    print(best)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 入力
candle_num, set_fire_num = list(map(int, input().split()))
candle_list = list(map(int, input().split()))

answer_time = 10 ** 15


def check_time(current_time):
    global answer_time
    if current_time < answer_time:
        answer_time = current_time


# 一番左をl, 一番右をrとおく(要素はL,Rとする)
for l in range(candle_num - set_fire_num + 1):
    r = l + set_fire_num - 1

    # L >= 0 のとき
    if candle_list[l] >= 0:
        check_time(candle_list[r])
        continue

    # L < 0 のとき
    # R < 0 のとき
    if candle_list[r] <= 0:
        check_time(abs(candle_list[l]))
        continue
    else:
        if abs(candle_list[l]) >= abs(candle_list[r]):
            check_time(candle_list[r]*2 + abs(candle_list[l]))
            continue
        else:
            check_time(abs(candle_list[l])*2 + candle_list[r])
            continue

# 最後に最速タイムを出力
print(answer_time)

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

Submission status: Accepted

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

for i in range(n-k+1):
    xl = x[i]
    xr = x[i+k-1]
    if i==0:
        MIN = abs(xl-xr) + abs(xl)

    MIN = min(MIN, abs(xl-xr) + abs(xl))
    MIN = min(MIN, abs(xl-xr) + abs(xr))

print(MIN)

Submission status: Accepted

Solution code:
aa = input().strip().split()
n = int(aa[0])
k = int(aa[1])
x = [int(s) for s in input().strip().split()]
r = -1

for i in range(n - k + 1):
    if 0 <= x[i] and (r == -1 or x[i + k - 1] < r):
        r = x[i + k - 1]

    if x[i] < 0 and (r == -1 or x[i + k - 1] - 2 * x[i] < r):
        r = x[i + k - 1] - 2 * x[i]

    if 0 <= x[i + k - 1] and (r == -1 or 2 * x[i + k - 1] - x[i] < r):
        r = 2 * x[i + k - 1] - x[i]

    if x[i + k - 1] < 0 and (r == -1 or -x[i] < r):
        r = - x[i]

print(r)

Submission status: Accepted

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

ans=float("inf")
for l in range(N-K+1):
	r=l+K-1
	if X[l]<0:
		if X[r]<0:	
			ans=min(ans, abs(X[l]) )
		else:	
			ans=min(ans, 2*abs(X[l])+abs(X[r]) , abs(X[l])+2*abs(X[r]) )
	elif X[l]>=0 and X[r]>=0:
		ans=min(ans, X[r] )


print ans

Submission status: Accepted

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

ans=float("inf")
t=float("inf")
for l in range(N-K+1):
	r=l+K-1
	if X[l]<0:
		if X[r]<0:	
			ans=min(ans, abs(X[l]) )
		else:	
			ans=min(ans, 2*abs(X[l])+abs(X[r]) , abs(X[l])+2*abs(X[r]) )
	elif X[l]>=0 and X[r]>=0:
		ans=min(ans, X[r] )


print ans

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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(left-right), abs(right)+abs(right-left))
print(ans)

Submission status: Accepted

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

l = 0
r = k-1
mnm = 1000100010
while r<n:
    a = abs(x[l])+abs(x[l]-x[r])
    b = abs(x[r])+abs(x[r]-x[l])
    if a<=b:
        if a < mnm:
            mnm = a
    else:
        if b < mnm:
            mnm = b
    l += 1
    r += 1
print(mnm)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

if N == 1:
    minMin = listMap[0]

for j in range(N - (K - 1)):
    L = j
    R = j + (K - 1)
    sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
    sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
    sum = min(sumL, sumR)
    if sum < minMin:
        minMin = sum

print(minMin)

Submission status: Accepted

Solution code:
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()

def main():
    n, k = na()
    xs = na()

    m = float('inf')
    for i in range(n-k+1):
        if xs[i] * xs[i+k-1] < 0:
            l = abs(xs[i]) + abs(xs[i+k-1]) + min(abs(xs[i]), abs(xs[i+k-1]))
        else:
            l = max(abs(xs[i]), abs(xs[i+k-1]))

        m = min(m, l)

    print(m)
    
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n,k=map(int,input().split())
x=list(map(int,input().split()))
if(n==1):
    print(x[0])
    sys.exit()
ans=[]
for i in range(n-(k-1)):
    f=abs(x[i+(k-1)])+abs(x[i+(k-1)]-x[i])
    b=abs(x[i])+abs(x[i]-x[i+(k-1)])
    t=min(b,f)
    ans.append(t)
ans.sort()
print(ans[0])

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):
    tmp = abs(X[i]) + abs(X[i + K - 1] - X[i])
    tmp2 = abs(X[i + K - 1]) + abs(X[i + K - 1] - X[i])
    ans = min(ans, tmp, tmp2)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 25 18:21:57 2018
ABC107C
@author: maezawa
"""

n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**9
for l in range(n-k+1):
    r = l+k-1
    ans1 = abs(x[l]) + abs(x[r]-x[l])
    ans2 = abs(x[r]) + abs(x[r]-x[l])
    ans0 = min(ans1, ans2)
    if ans > ans0:
        ans = ans0
print(ans)

Submission status: Accepted

Solution code:
def c_candles(N, K, X):
    ans = float('inf')
    # [left, right] が指す連続したK本のろうそくに火をつけるといい
    for left in range(N):
        right = left + K - 1
        if right >= N:  # 右端がろうそくを指していない
            break
        # left→rightへ付けていく場合とright→leftへ付けていく場合
        ans = min(ans, abs(X[left]) + abs(X[right] - X[left]))
        ans = min(ans, abs(X[right]) + abs(X[right] - X[left]))
    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
arr = [int(x) for x in input().split()]
ps = []
ms = []
for i in range(N):
    if arr[i] == 0:
        K -= 1
    elif arr[i] < 0:
        ms.append(abs(arr[i]))
    elif arr[i] > 0:
        ps.append(arr[i])
ms = ms[::-1]
if K <= 0:
    ans = 0
else:
    ans = ps[K-1] if K <= len(ps) else 10**18
    ans = min(ans, ms[K-1]) if K <= len(ms) else ans
if len(ps) > 0 and len(ms) > 0:
    for i in range(len(ms)):
        if K-(i+1)-1 < len(ps):
            ans = min(ans, ms[i]*2 + ps[K-(i+1)-1])
    for i in range(len(ps)):
        if K-(i+1)-1 < len(ms):
            ans = min(ans, ps[i]*2 + ms[K-(i+1)-1])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

result = np.inf
for i in range(len(x) - k +1):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))

Submission status: Accepted

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

result = []
for i in range(N-(K-1)):
    start = i
    end = i + (K -1)
    result.append(abs(x_list[start]) + abs(x_list[end] - x_list[start]))
    result.append(abs(x_list[end]) + abs(x_list[end] - x_list[start]))
print(min(result))

Submission status: Accepted

Solution code:
n,k=map(int, input().split(" "))
X=list(map(int, input().split(" ")))
#import bisect
maxvalue=100000000000000000000000
#place=bisect.bisect_left(X,0)

#if k<place:
#    X=X[place-k:]
#if k<n-place:
#    X=X[:-(n-place-k)]

#print(X)
#print(place)
for i in range(n-k+1):
    low=X[i]
    high=X[i+k-1]
    if high*low<0:
        low=abs(low)
        high=abs(high)
        value=min(low,high)*2+max(low,high)
    else:
        low=abs(low)
        high=abs(high)
        value=max(low,high)
    #print(value)
    if value<maxvalue:
        maxvalue=value
print(maxvalue)

Submission status: Accepted

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

s = 0
e = K - 1

re = min(abs(x[s]) + abs(x[s] - x[e]), abs(x[e]) + abs(x[e] - x[s]))
while e < N:
    tmp = min(abs(x[s]) + abs(x[s] - x[e]), abs(x[e]) + abs(x[e] - x[s]))
    re = min (re, tmp)
    s += 1
    e += 1
    
print(re)

Submission status: Accepted

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

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

plus = [0]
minus = [0]

for x_ in x:
    if x_ < 0:
        minus.append(x_ * -1)
    if x_ > 0:
        plus.append(x_)
    if x_ == 0:
        K -= 1

plus.sort()
minus.sort()


ans = 10000000000

for i in range(len(plus)):
    if 0 <= K-i < len(minus):
        ans = min(ans,plus[i]*2 +minus[K-i])
for i in range(len(minus)):
    if 0 <= K-i < len(plus):
        ans = min(ans,minus[i]*2 +plus[K-i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

list1=[]
for i in range(N-K+1):
    if x[i+K-1]<=0:
        list1.append(x[i]*(-1))
    elif x[i]>=0:
        list1.append(x[i+K-1])
    else:
        list1.append(x[i+K-1]*2-x[i])
        list1.append(x[i+K-1]-x[i]*2)
#print(list1)
print(min(list1))

Submission status: Accepted

Solution code:
import sys

N, K = [int(_) for _ in input().split()]
list = [int(_) for _ in input().split()]

l1 = []
l2 = []
t = 0
for i in list:
    if i < 0:
        l1.append(i)
    elif i == 0:
        t = 1
    else:
        l2.append(i)
l1 = [abs(_) for _ in l1]
l1.sort()

if t == 1 and K == 1:
    print(0)
    sys.exit()

K = K - t
a = float('inf')
b = float('inf')
if len(l1) >= K:
    a = l1[K-1]
if len(l2) >= K:
    b = l2[K-1]

c = float('inf')
d = float('inf')
for i in range(len(l2)):
    if len(l1)-1 >= K-i-2 >= 0:
        c_new = l2[i] * 2 + l1[K-i-2]
        c = min(c, c_new)
for j in range(len(l1)):
    if len(l2)-1 >= K-j-2 >= 0:
        d_new = l1[j] * 2 + l2[K-j-2]
        d = min(d, d_new)

print(min(a, b, c, d))

Submission status: Accepted

Solution code:
def run(n, k, x):
    # ri = 0
    # li = k-1
    # if n == 1:
    #    return x[0]
    # x2 = x
    ret = abs(x[0]) + abs(x[0] - x[k-1])
    for i in range(n-k+1):
        ri = i
        li = i + k - 1
        tmp = min(abs(x[ri]) + abs(x[ri] - x[li]), abs(x[li]) + abs(x[ri] - x[li]))
        ret = min(ret, tmp)
        '''
        if x[ri] > x[i]:
        elif x[ri] == x[i]:
            if x[ri+1] < x[i-1]:
                ri = i-k+1
                li = i
        else:
            if x2[li] <= 0:
                return x[ri]
            elif x2[ri] >= 0:
                return x[li]
            elif x[ri] > x[li]:
                return x[ri] + 2*x[li]
            else:
                return 2*x[ri] + x[li]
    if x2[li] <= 0:
        return x[ri]
    elif x2[ri] >= 0:
        return x[li]
    elif x[ri] > x[li]:
        return x[ri] + 2*x[li]
    else:
        return 2*x[ri] + x[li]
        '''
    return ret


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


def main():
    n, k, x = read_line()
    print(run(n, k, x))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = float('inf')
for left in range(N - K + 1):
    lv, rv = X[left], X[left+K-1]
    if lv * rv > 0:
        ans = min(ans, max(abs(rv), abs(lv)))
    else:
        ans = min(ans, min(rv*2 - lv, rv - lv*2))
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 i in range(N-K+1):
    l = x[i]
    r = x[i+K-1]
    a = abs(l) + abs(r - l)
    b = abs(r) + abs(r - l)
    ans = min(a,b,ans)
print(ans)

Submission status: Accepted

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

left = 0
right = left+(K-1)
time = 10**9
while right < N:
    d = x[right]-x[left]
    time = min(time, d+abs(x[right]), d+abs(x[left]))
    left += 1
    right += 1
print(time)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
v=1e9
for l in range(n-k+1):
  r=l+k-1
  a=abs(x[l])+abs(x[l]-x[r])
  b=abs(x[r])+abs(x[l]-x[r])
  v=min(v,min(a,b))
print(v)

Submission status: Accepted

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

time = 0
answer = float("inf")

for i in range(n - k + 1):
  x_min = x[i]
  x_max = x[k + i - 1]
  
  if x_min * x_max < 0:  
    time = x_max - x_min + min(x_max, -x_min)
  else:
    time = max(abs(x_max), abs(x_min))
  answer = min(answer, time)
#  print(time)
  
print(answer)

Submission status: Accepted

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

ans=0
for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i])

    if i==0 or ans>tans:
        ans=tans

for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i+K-1])

    if ans>tans:
        ans=tans

print(ans)

Submission status: Accepted

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

MIN = float("inf")
for i in range(N-K+1):
	l = i
	r = i + K - 1
	cur1 = abs(x[l]) + abs(x[r]-x[l])
	cur2 = abs(x[r]) + abs(x[r]-x[l])
#	print([l,r,cur1,cur2])
	if cur1 < MIN:
		MIN = cur1
	if cur2 < MIN:
		MIN = cur2
print(MIN)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = [i for i in map(int, input().split())]
i = 0
min_time =5e8
while i < N-K+1:
    if x[i]<0 and x[i+K-1]<0:
        time = -x[i]
    if x[i]<0 and x[i+K-1]>=0:
        time1 = -2*x[i] + x[i+K-1]
        time2 = -x[i] + 2*x[i+K-1]
        if time1< time2:
            time = time1
        else:
            time = time2
    if x[i] >= 0 and x[i+K-1] >=0:
        time = x[i+K-1]
    if time < min_time:
        min_time = time
    i += 1
print(min_time)

Submission status: Accepted

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

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

Submission status: Accepted

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

if 0 in X:
    i = X.index(0)
    del X[i]
    N -= 1
    K -= 1

C=N
for i, x in enumerate(X):
    if x >= 0:
        C = i
        break

A = []

if K == 0:
    print(0)
else:
    if C>=K:
        A.append(-X[C-K])
        # print(-X[C-K])
    if N-C>=K:
        A.append(X[C+K-1])
        # print(X[C+K-1])
    for k in range(max(C+K-N, 1), min(C+1, K)):
        A.append(-2*X[C-k] + X[C-k-1+K])
        # print(X[C-k], X[C-k-1+K], -2*X[C-k] + X[C-k-1+K])
    for k in range(max(K-C, 1), min(N-C+1,K)):
        A.append(2*X[C+k-1] - X[C+k-K])
        # print(X[C+k-1], X[C+k-K], 2*X[C+k-1] - X[C+k-K])
    print(min(A))

Submission status: Accepted

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

x_minus = [a for a in X if a < 0]
x_plus = [a for a in X if a >= 0]

ans = float("inf")
for i in range(K+1):
    if i == 0:
        if len(x_plus) >= K:
            dis = x_plus[K - 1]
            ans = min(ans, dis)
        else:
            continue
    elif 1 <= i <= K - 1:
        if len(x_minus) >= i and len(x_plus) >= K - i:
            dis = 2 * abs(x_minus[-i]) + x_plus[K - i - 1]
            ans = min(ans, dis)
            dis = 2 * x_plus[K - i - 1] + abs(x_minus[-i])
            ans = min(ans, dis)
        else:
            continue
    elif i == K:
        if len(x_minus) >= K:
            dis = abs(x_minus[-K])
            ans = min(ans, dis)


print(ans)

Submission status: Accepted

Solution code:
# N, K = 3, 2
# X = [int(i) for i in '10 20 30'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)

res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])
# print(res)

if len_plus != 0:
    for i_minus in range(len_minus):
        if K-i_minus-1 <= len_plus:
            res.append(abs(-X_minus[-i_minus-1])*2 + X_plus[K-i_minus-2])

if len_minus >= K:
    if len_plus > 0:
        if X_plus[0]==0:
            res.append(-X_minus[-K+1])
    else:
        res.append(-X_minus[-K])
# print(res)
print(min(res))