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,neg,pos=[],[],[]
for i in input().split():
	i = int(i)
	x.append(i)
	if i < 0:
		neg.append(i)
	else:
		pos.append(i)
neg.reverse()

ans=[]

if len(neg) >= k:
	ans.append(-neg[k-1])
if len(pos) >= k:
	ans.append(pos[k-1])

for i in range(1, k):
	j = k - i
	if i > len(neg) or j > len(pos):
		continue
	ans.append(pos[j-1] - neg[i-1] + min(pos[j-1], -neg[i-1]))

print(min(ans))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = [int(n) for n in input().split()]
index = 0
current = 0
top = 0
flag = 0
if x[n - 1] <= 0:
	x.append(0)
	index = n
else:
	while x[index] < 0:
		index += 1
		if index >= n:
			break
	x.insert(index, 0)
for i in range(k + 1):
	i1 = index + i
	i2 = index - k + i
	if i1 < n + 1 and i2 > -1:
		if i == 0 or i == k:
			current = max(abs(x[i2]), x[i1])
		else:
			s = abs(x[i2])
			smaller = min(x[i1], s)
			current = x[i1] - x[i2]
			current += smaller

		if flag == 0:
			top = current
			flag += 1
		elif top > current:
			top = current
print(top)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
min_time = 10 ** 10
for l, r in zip(range(0, n - k + 1), range(k - 1, n)):
    time = x[r] if x[l] >= 0 else (-x[l] if x[r] <= 0 else (x[r] - x[l]) + min(-x[l], x[r]))
    if time < min_time:
        min_time = time
print(min_time)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
first = True
for l, r in zip(range(0, n - k + 1), range(k - 1, n)):
    if  0 >= x[r]:
        if first:
            min_time = -x[l]
            first = False
        else:
            time =-x[l]
            if time < min_time:
                min_time = time
    elif 0 <= x[l]:
        if first:
            min_time = x[r]
            first = False
        else:
            time = x[r]
            if time < min_time:
                min_time = time
    else:
        if first:
            min_time = (x[r] - x[l]) + min(-x[l], x[r])
            first = False
        else:
            time =  2 * (x[r] - x[l]) - max(-x[l], x[r])
            if time < min_time:
                min_time = time
print(min_time)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = []
y = []
for i in input().split():
    n = int(i)
    if n < 0:
        x.append(-n)
    elif n == 0:
        K -= 1
    else:
        y.append(n)

x = x[::-1]

len_x = len(x)
len_y = len(y)
now = 10 ** 18

if len_x == 0:
    if len_y == 0:
        print(0)
    else:
        print(y[K-1])
elif len_y == 0:
    print(x[K-1])
else:
    for i in range(K+1):
        if K - i > len_y:
            continue
        if len_x < i:
            break
        if i == 0:
            now = min(y[K-1], now)
        elif i == K:
            now = min(x[K-1], now)
        else:
            now = min(min(x[i-1], y[K-i-1]) + x[i-1] + y[K-i-1], now)

    print(now)

Submission status: Accepted

Solution code:
def solve(n, k, x):
    ans = 2 * (max(x) - min(x))
    for i in range(n-k+1):
        left = x[i]
        right = x[i+k-1]
        if left * right >= 0:
            d = max(abs(right), abs(left))
        else:
            d = min(
                2 * abs(left) + abs(right),
                abs(left) + 2 * abs(right)
            )
        ans = min(ans, d)
    return ans

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

Submission status: Accepted

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

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

    ans = min(ans,temp)

print(ans)

Submission status: Accepted

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

ans=10**11

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

print(ans)

Submission status: Accepted

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

min_time = float('inf')
for i in range(N-K+1):
  cost1 = abs(X[i]-X[i+K-1]) + abs(X[i])
  cost2 = abs(X[i]-X[i+K-1]) + abs(X[i+K-1])
  min_time = min(min_time,cost1,cost2)
print(min_time)

Submission status: Accepted

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

ans = 10**9

for i in range(n-k+1):
    if l[i] * l[i + k -1] >= 0:
        t = max(abs(l[i]),abs(l[i + k -1]))
    else:    
        t = l[i + k -1] - l[i] + min(abs(l[i]),abs(l[i + k - 1]))
    ans = min(ans,t)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
y=[]
for i in range(N-1):
    y.append(x[i+1]-x[i])
z=[]
z.append(0)
for i in range(N-1):
    z.append(z[i]+y[i])
M=10**10
for i in range(N-K+1):
    M=min(M,min(abs(x[i]),abs(x[i+K-1]))+z[i+K-1]-z[i])
if N-K==0:
  M=min(abs(x[0]),abs(x[-1]))+z[-1]
print(M)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
minus_x=[i for i in x if i<0]
pz=len(minus_x)
start=pz-k
if start<0:
    start=0
if pz+k-1>n-1:
    end=n-k+1
else:
    end=pz+1
l=[]
for i in range(start,end):
    l.append(x[i+k-1]-x[i]+min(abs(x[i+k-1]),abs(x[i])))
print(min(l))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
min = 10 ** 9
for i in range(N-K+1):
  count = abs(x[i] - x[i+K-1]) + abs(x[i])
  if count < min:
    min = count
  count = abs(x[i] - x[i+K-1]) + abs(x[i+K-1])
  if count < min:
    min = count
print(min)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
if n == 1:
  print(lis[0])
  exit()
ans = []
for i in range(n-k+1):
  if lis[i+k-1] <=0 or 0 <= lis[i]:
    ans.append(max(abs(lis[i+k-1]),abs(lis[i])))
  else:
    ans.append(min(abs(lis[i+k-1]),abs(lis[i]))+abs(lis[i+k-1])+abs(lis[i]))
print(min(ans))

Submission status: Accepted

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

Xp = [x for x in X if x>=0]
Xn = [x for x in X if x<0]
Xp.sort()
Xn.sort(reverse=True) #絶対値の小さい順に
#print(Xp, Xn)

answer = 1e9
for i, x in enumerate(Xp):
  if len(Xn) >= K-(i+1) > 0:
    #print(i,x, Xn[K-(i+1)-1], x -Xn[K-(i+1)-1])
    answer = min(answer, 2*x -Xn[K-(i+1)-1])
  if K-(i+1)==0:
    answer = min(answer, x)
for i, x in enumerate(Xn):
  if len(Xp) >= K-(i+1) > 0:
    #print(i,x, Xp[K-(i+1)-1], -x -Xp[K-(i+1)-1])
    answer = min(answer, -2*x + Xp[K-(i+1)-1])
  if K-(i+1)==0:
    answer = min(answer, -x)
print(answer)

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 x[i] * x[i+k-1] >= 0:
    t = max(abs(x[i]), abs(x[i+k-1]))
  else:
    t = x[i+k-1] - x[i] + min(abs(x[i]), abs(x[i+k-1]))
  ans = min(ans, t)
print(ans)

Submission status: Accepted

Solution code:
INF = 10**9

N, K = map(int, input().split())
x_list = [int(i) for i in input().split()]
for i in range(N):
    if x_list[i] >= 0:
        T = i
        break
else:
    print(abs(x_list[-K]))
    exit()
min_take_time = INF
for i in range(K+1):
    if T-K+i < 0 or T+i>N:
        continue
    if x_list[T-K+i] *  x_list[T+i-1] <= 0:
        take_time = min(abs(x_list[T-K+i]), abs(x_list[T+i-1])) * 2 + max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    else:
        take_time = max(abs(x_list[T-K+i]), abs(x_list[T+i-1]))
    min_take_time = min(min_take_time, take_time)


print(min_take_time)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
# n,k = map(int,"8 5".split())
xn = list(map(int,input().split()))
# xn = list(map(int,"-9 -7 -4 -3 1 2 3 4".split()))

ans = float("inf")
for i in range(n-k+1):
#     print(xn[i],xn[n-k+i])
#     print(xn[i:i+k])
    tmp = min(abs(xn[i]),abs(xn[k+i-1])) + xn[k+i-1] - xn[i]
#     print("始点 is {}, 終わり is {}, 始まり is {}".format(min(abs(xn[i]),abs(xn[k+i-1])), xn[k+i-1],xn[i]))
#     print("tmp is {}".format(tmp))
    if tmp < ans:
        ans=tmp
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    c = s.count("0")
    print(2*min(c,len(s)-c))
    return

#B
def B():
    n = I()
    h = LI()
    ans = 0
    m = min(h)
    while m:
        ans += m
        k = max(h)
        for i in range(n):
            if h[i] >= m:
                h[i] -= m
                if h[i]:k = min(k,h[i])
            else:break
        m = k
    print(ans)
    return

#C
def C():
    n,k = LI()
    x = LI()
    ans = float("inf")
    j = bisect.bisect_left(x,0)
    if n == 1:
        print(abs(x[0]))
        quit()
    if j < n:
        if x[j] == 0:
            k -= 1
            x.pop(j)
            n -= 1
    for i in range(n):
        if i < j:
            if abs(i-j) > k:continue
            elif abs(i-j) == k:ans = min(ans,abs(x[i]))
            elif k+i-1 < n:
                ans = min(ans,abs(x[i])+x[k+i-1]-x[i])
        else:
            if abs(i-j)+1 > k:continue
            elif abs(i-j)+1 == k:ans = min(ans,x[i])
            elif i-k+1 >= 0:
                ans = min(ans,abs(x[i])+x[i]-x[i-k+1])

    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import itertools
import collections
from queue import Queue
import functools
import math
INF = 10**9


def solve(N: int, K: int, x: "List[int]"):
    x_neg = ([-xx for xx in x if xx < 0]+[0])[::-1]
    x_pos = [0] + [xx for xx in x if xx >= 0]

    # 左先を考える
    left = [x_neg[i]*2 + x_pos[K-i] for i in range(len(x_neg))
            if 0 <= K-i and K-i < len(x_pos)]
    if len(left) > 0:
        ansleft = min(left)
    else:
        ansleft = INF

    # 右先
    right = [x_pos[i]*2 + x_neg[K-i] for i in range(len(x_pos))
             if 0 <= K-i and K-i < len(x_neg)]
    if len(right) > 0:
        ansright = min(right)
    else:
        ansright = INF
    # print(left)
    # print(right)
    print(min(ansright, ansleft))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, x)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_right

N, K = map(int, input().split())
x = list(map(int, input().split()))
zero_pos = bisect_right(x, 0)
start = max(zero_pos - K, 0)
end = min(zero_pos, N - K)
m = 10**10
for i in range(start, end + 1):
    left = x[i]
    right = x[i+K-1]
    _m = max(abs(left), abs(right))
    if left*right < 0:
        _m += min(abs(left), abs(right))*2
    m = min(m, _m)
print(m)

Submission status: Accepted

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

cand = []

# 全探索
for i in range(n - k + 1):
    cand.append(min(abs(x[i]), abs(x[i + k - 1])) + abs(x[i + k - 1] - x[i]))

print(min(cand))

Submission status: Accepted

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

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):
    left = i
    right = i + K - 1
    tmp = min(abs(x[left]) + abs(x[right] - x[left]), abs(x[right]) + abs(x[left] - x[right]))
    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**10
for i in range(N-K+1):
    if x[i] >= 0 and x[i+K-1] >= 0:
        ans = min(ans, x[i+K-1])
    elif x[i] < 0 and x[i+K-1] < 0:
        ans = min(ans, abs(x[i]))
    else:
        ans = min(ans, abs(x[i]) * 2 + x[i+K-1])
        ans = min(ans, abs(x[i]) + x[i+K-1] * 2)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import fractions
from functools import reduce
from collections import Counter
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]  # LIST INT
def LF(): return [float(x) for x in sys.stdin.readline().split()]  # LIST FLOAT
def LS(): return sys.stdin.readline().split()  # LIST STRING
def MI(): return map(int, sys.stdin.readline().split())  # MAP INT
def II(): return int(sys.stdin.readline())  # INPUT INT
def IS(): return input()  # INPUT STRING
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)

N, K = MI()
x = LI()
ans = 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(lambda x: int(x), input().split()))

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

print(ans)

Submission status: Accepted

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

d_min = 2*(x[N-1]-x[0])

for i in range(N-K+1):
	
	if x[i+K-1] <= 0:
		d = -x[i]
	elif x[i] < 0 and 0 < x[i+K-1]:
		d = min(-x[i],x[i+K-1])+x[i+K-1]-x[i]
	else:
		d = x[i+K-1]
	
	d_min = min(d,d_min)

print(d_min)

Submission status: Accepted

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

X=list(map(int,input().split()))
V=[]
for i in range(n-k+1):
    Y=X[i+k-1]-X[i]   
    V.append(Y+min(abs(X[i]),abs(X[i+k-1])))

print(min(V))

Submission status: Accepted

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

left = []
right = []
for x in X:
    if x<0:
        left.append(abs(x))
    else:
        right.append(x)
left.reverse()

# from math import inf
ans = 10**18

#i=>right
for i in range(K+1):
    if len(right)!=0 and right[0]==0 and i==0:
        continue;
    temp = 0
    if len(left)==0:
        temp = right[K-1]
    elif len(right)==0:
        temp = left[K-1]
    elif len(left)>=(K-i):
        if len(right)>=i:
            temp0 = left[K-i-1] if not i==K else 0
            temp1 = right[i-1] if not i==0 else 0
            temp  = temp0+temp1 + min(temp0, temp1)
        else:
            temp0 = right[-1]
            temp1 = left[K-len(right)-1]
            temp  = temp0+temp1 + min(temp0, temp1)
    else:
        temp0 = left[-1]
        temp1 = right[K-len(left)-1]
        temp  = temp0+temp1 + min(temp0, temp1)
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

result = []
if(N==1):
    print(np.abs(x[0]))

else:
    for i in range(N - K + 1):
        left = x[i]
        right = x[i + K - 1]

        if (left * right < 0):
            if (np.abs(left) < np.abs(right)):
                diff = np.abs(left) * 2 + np.abs(right)
            else:
                diff = np.abs(left) + np.abs(right) * 2

        else:
            diff = np.max([np.abs(left), np.abs(right)])

        result.append(diff)

    print(np.min(result))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
l1=[]
for i in range(n-m+1):
  if l[i]>=0:
    l1.append(l[i+m-1])
  elif l[i]<=0<=l[i+m-1]:
    l1.append(l[i+m-1]-l[i]+min(abs(l[i]),abs(l[i+m-1])))
  else:
    l1.append(abs(l[i]))
print(min(l1))

Submission status: Accepted

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

dis = 300000000

for i in range(N-K+1):
    disl = abs(x[i]) + abs(x[i+K-1]-x[i])
    disr = abs(x[i+K-1]) + abs(x[i+K-1]-x[i])

    dis = min(dis, disl, disr)
#    if (y[0]*y[-1]<0):
#        disy = y[-1]-y[0]+min(abs(y[-1]), abs(y[0]))
#    else:
#        disy = max(abs(y[0]), abs(y[-1]))
#    dis = min(dis, disy)

print(dis)

Submission status: Accepted

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

score = 10**10
for i in range(N-K+1):
    l = x[i]
    r = x[i+K-1]
    if r <= 0:
        score = min(score,-l)
    elif l >= 0:
        score = min(score,r)
    else:
        score = min(score,-2*l+r,-l+2*r)
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
m = float("INF")
for i in range(N - K + 1):
    if x[i + K - 1] * x[i] < 0:
        m = min(m, x[i + K - 1] - x[i] + min(abs(x[i + K - 1]), abs(x[i])))
    else:
        m = min(m, max(abs(x[i + K - 1]), abs(x[i])))
print(m)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
li=list(map(int,input().split()))
q=[]
if k==n:
  q.append(li[n-1]-li[0]+min(abs(li[n-1]),abs(li[0])))
else:
  for i in range(k,n+1):
    if li[i-1]<0:
      q.append(-li[i-k])
    elif li[i-k]>0:
      q.append(li[i-1])
    else:
      q.append(li[i-1]-li[i-k]+min(li[i-1],abs(li[i-k])))
print(min(q))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N, K = 0, 0
X = []


def set_inputs():
    global N, K, X
    N, K = get_li()
    X = get_li()
    return


def twice(x):
    return x[0] * 2, x[1]


def find_index(l, x):
    if x in l:
        return l.index(x)
    return inf


def main():
    setrecursionlimit(100000)
    set_inputs()
    ans = inf
    for i in range(N-K+1):
        l, r = X[i], X[i+K-1]
        tmp = min(abs(l) + abs(l-r), abs(r) + abs(r-l))
        ans = min(ans, tmp)
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


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


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

min_time = 10 ** 12
for i in range(N):
    if i + K > N:
        break
    if a[i] > 0:
        min_time = min(min_time, abs(a[i + K - 1]))
        break
    
    if a[i + K - 1] < 0:
        if (i + K < N and a[i + K] > 0) or i + K == N:
            min_time = min(min_time, abs(a[i]))
        continue
        
    min_time = min(min_time, 2 * abs(a[i]) + abs(a[i + K - 1]), abs(a[i]) + 2 * abs(a[i + K - 1]))

print(min_time)

Submission status: Accepted

Solution code:
import bisect as bs
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
m = bs.bisect_left(a, 0)
a.insert(m, 0)
ans = 10 ** 9
for i in range(K + 1):
    if m - i >= 0 and m + K - i < N + 1:
        ans = min(a[m + K - i] - a[m - i] + min(a[m + K - i], -a[m - i]), ans)
print(ans)

Submission status: Accepted

Solution code:
import bisect

ans = 10**20

N, K = list(map(int, input().split()))
x = [-10**20] + list(map(int, input().split())) + [10**20]

zero = bisect.bisect_left(x, 0)

left = x[:zero] + [0] 
right = [0] + x[zero:]
left.reverse()
l = len(left)
r = len(right)

idxs = [[K-i, i] for i in range(K+1) if K-i < l and i < r]

for l, r in idxs:
    n = abs(left[l]) + right[r] + min(abs(left[l]), right[r])
    ans = min(ans, n)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
x = [int(i) for i in input().split()]
ls = []
for x, y in zip(x[:N-K+1], x[K-1:]):
    ls.append(min(abs(x)+abs(x-y), abs(y)+abs(x-y)))
print(min(ls))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
P=[i for i in x if i>=0]
Q=[abs(i) for i in x if i<0]
ans=10**18
if len(P)>=k:
  ans=min(ans,P[k-1])
if len(Q)>=k:
  ans=min(ans,Q[-k])
for i in range(1,k):
  if i<=len(Q) and k-i<=len(P):
    ans=min(ans,Q[-i]*2+P[k-i-1],Q[-i]+P[k-i-1]*2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
X = list(map(int,input().split()))
from bisect import bisect_left as bl
zero = bl(X,0)

temp = []
for i in range(max(zero,k-1),min(zero+k,n)):
    if X[i-k+1] >= 0:
        temp.append(X[i])
    elif X[i] <= 0:
        temp.append(abs(X[i-k+1]))
    else:
        temp.append(min(abs(X[i]),abs(X[i-k+1]))*2+max(abs(X[i]),abs(X[i-k+1])))
try:
        print(min(temp))
except:
        print(abs(X[-k]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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):
    if x[i+k-1] < 0:
        ans.append(abs(x[i]))
    elif x[i] > 0:
        ans.append(abs(x[i+k-1]))
    else:
        ans.append(min(abs(x[i])*2+abs(x[i+k-1]), abs(x[i])+abs(x[i+k-1])*2))

print(min(ans))

Submission status: Accepted

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

mink = pow(10, 18)
for i, x in enumerate(xlist[k-1:]):
  lx = xlist[i]
  if mink > abs(lx) + x - lx:
    mink = abs(lx) + x - lx
  if mink > abs(x) + x - lx:
    mink = abs(x) + x - lx
print(mink)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
cost = []
for i in range(0, N-K+1):
    #window = X[i:i+K]
    #cost.append(window[-1]-window[0] + min(abs(window[-1]), abs(window[0])))
    cost.append(X[i+K-1]-X[i] + min(abs(X[i+K-1]), abs(X[i])))
print(min(cost))

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

Submission status: Accepted

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

length = float("inf")
for i in range(N - K + 1):
  r = i + K - 1
  l = i
  li = min(abs(x[r]) + abs(x[r] - x[l]), abs(x[l]) + abs(x[r] - x[l]))
  if li < length:
    length = li

print(length)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
key = []
for left, right in zip(x, x[K - 1:]):
    key.append(right-left+min(abs(left), abs(right)))
print(min(key))

Submission status: Accepted

Solution code:
#ans = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))

N, K = map(int, input().split())
INF = 10**10
x_first = [-INF for i in range(K)]
x_ap = [int(i) for i in input().split()]
x_last = [INF for i in range(K)]
x = x_first + x_ap + x_last + [0]
x.sort()
zero_index = x.index(0)

key = []
for i in range(K+1):
    left = abs(x[zero_index - i])
    right = abs(x[zero_index - i + K])
    # left か rightの 0に近い方に行ってからもう片方にいく
    if left > right:
        right *= 2
    else:
        left *= 2
    key.append(left + right)
print(min(key))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int ,input().split())
x = list(map(int, input().split()))
xp = [0]+[xi for xi in x if xi>=0]
size_p = len(xp)-1
xn = [-xi for xi in x if xi<0] + [0]
xn.reverse()
size_n = len(xn)-1

# print('xp',xp)
# print('xn',xn)

min_t = float('inf')
for p in range(K+1):
	n = K-p
	if p>size_p or n>size_n:
		continue
	
	tmp = xp[p]+xn[n]+min(xp[p],xn[n])
	# print('p, size_p, n, size_n',p, size_p, n, size_n)
	# print('xp[p], xn[n]',xp[p], xn[n])
	# print('tmp:',tmp)
	if min_t > tmp:
		min_t = tmp

print(min_t)

Submission status: Accepted

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

def calc(startIndex):
    if startIndex + K > N:
        return -1
    endIndex = startIndex + K - 1
    x1 = Xi[startIndex]
    x2 = Xi[endIndex]
    len1 = abs(x1) + abs(x2 - x1)
    len2 = abs(x2) + abs(x2 - x1)
    return min(len1, len2)

INF = 100000000000000
min_value = INF
for i in range(N):
    d = calc(i)
    if d >= 0 and d < min_value:
        min_value = d
if min_value == INF:
    print(0)
else:
    print(min_value)

Submission status: Accepted

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

min_dist = 10**10
for i in range(n-k+1):
    if x[i] > 0:
        dist = x[i+k-1]
    elif x[i+k-1] < 0:
        dist = -x[i]
    else:
        dist = x[i+k-1] - x[i] + min(-x[i], x[i+k-1])

    if dist < min_dist:
        min_dist = dist

print(min_dist)