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:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,k = inpl()
a = inpl()
res = INF
for i in range(n-k+1):
    if a[i]*a[i+k-1] >= 0:
        res = min(res, max(abs(a[i]), abs(a[i+k-1])))
    else:
        c = min(abs(a[i]), abs(a[i+k-1]))
        d = max(abs(a[i]), abs(a[i+k-1]))
        res = min(res, c*2+d)
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = []
b = []
for i in x:
    if i == 0:
        k -= 1
    elif i < 0:
        a.append(-i)
    else:
        b.append(i)
a.reverse()
ans = float("inf")
if len(b) > len(a):
    a, b = b, a
if k == 0:
    ans = 0
else:
    for i in range(k):
        if i < len(a):
            if i == k - 1:
                ans = min(ans, a[i])
            else:
                ta = a[i]
                if 0 <= k - i - 2 < len(b):
                    tb = b[k - i - 2]
                    ans = min(ans, ta * 2 + tb, ta + tb * 2)

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
res = float("inf")
for i in range(N - K + 1):
  if a[i] * a[i + K - 1] < 0:
    mn = min(abs(a[i]), abs(a[i + K - 1]))
    mx = max(abs(a[i]), abs(a[i + K - 1]))
    res = min(res, mn * 2 + mx)
  else:
    res = min(res, max(abs(a[i]), abs(a[i + K - 1])))
print(res)

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):
    start,end = x[i],x[i+K-1]
    if end <= 0:
        ans = min(ans, abs(start)) 
    elif start <= 0:
        ans = min(ans, 2*min(abs(start), end)+max(abs(start), end))
    else:
        ans = min(ans, end)

print(ans)

Submission status: Accepted

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

if N == K:
    start,end = x[0],x[-1]
    if end <= 0:
        ans = abs(start)
    elif start <= 0:
        ans = 2*min(abs(start), end)+max(abs(start), end)
    else:
        ans = end

else:
    ans = 10**9
    for i in range(N-K+1):
        start,end = x[i],x[i+K-1]
        if end <= 0:
            ans = min(ans, abs(start)) 
        elif start <= 0:
            ans = min(ans, 2*min(abs(start), end)+max(abs(start), end))
        else:
            ans = min(ans, end)

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

Submission status: Accepted

Solution code:
def dist(a, b):
  if a * b < 0:
    return 2 * min(abs(a), abs(b)) + max(abs(a), abs(b))
  else:
    return max(abs(a), abs(b))

def solve(A, K):
  ans = dist(A[0], A[K-1])
  for i in range(len(A)-K+1):
    ans = min(ans, dist(A[i], A[i+K-1]))
  return ans
               
N, K, *A = map(int, open(0).read().split())
print(solve(A, K))

Submission status: Accepted

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

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

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(l, r):
    # 左だけ行く
    if r <= 0:
        return -l
    # 右だけ行く
    elif l >= 0:
        return r
    # 両方行く
    else:
        # 短い方を先に行って往復
        return min(-l, r) * 2 + max(-l, r)

N, K = MAP()
A = LIST()

ans = INF
for i in range(N):
    if i+K > N:
        break
    l = A[i]
    r = A[i+K-1]
    ans = min(ans, check(l, r))
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
x = x[np.nonzero(x)]
if K == 0:
    print(0)
    sys.exit()
ans = []
for i in range(len(x)-K+1):
    if (x[i] < 0) != (x[i+K-1] < 0):
        ans.append(abs(x[i])+abs(x[i+K-1])+min(abs(x[i]),abs(x[i+K-1])))
    else:
        ans.append(max(abs(x[i]),abs(x[i+K-1])))
print(min(ans))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if N==1:
  print(0);exit()

ans=float("Inf")
for i in range(N-K+1):
  ans=min(ans, X[i+K-1]-X[i]+ min(abs(X[i+K-1]),abs(X[i]))  )

print(ans)

Submission status: Accepted

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

# c = combinations(x, k)
c = []
for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    c.append([l, r])


# print(list(c))

ans = 10 ** 10

for l, r in c:
    tmp = min(abs(l) + abs(r - l), abs(r) + abs(r - l))
    ans = min(ans, tmp)

# for g in c:
#     a = max(0, max(g))
#     b = min(0, min(g))
#     # print(a, b)
#     if (a * b) < 0:
#         tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
#     else:
#         tmp = abs(a) + abs(b)
#     ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

count_m = 0
count_p = 0
for i in range(N):
    if x[i] > 0:
        count_m = i
        count_p = N-i
        break
    elif x[i] == 0:
        count_m = i
        count_p = N-i-1
        K -= 1
        N -= 1
        x.remove(x[i])
        break
if count_m == 0 and count_p == 0:
    count_m = N

ans = 0
    

y = K
z = K - y


if count_m >= 1 and count_p>=1:
    # for j in (range(1,y)):
    #     if count_m-j>=0  and count_m+y-1-j<N:
    #         print(x[count_m+y-1-j], x[count_m-j])
    tmp = min(x[count_m+y-1-j]-x[count_m-j] *2 for j in range(1, y) if count_m-j>=0  and count_m+y-1-j<N)
    tmp2 = min(x[count_m+y-1-j]*2-x[count_m-j] for j in range(1, y) if count_m-j>=0 and count_m+y-1-j<N)
else:
    tmp = tmp2 = 1000000000

if N==0:
    ans = 0


elif count_p>=K:
    # print(y, "1chi")
    ans = min(tmp, tmp2, x[count_m+min(y, count_p)-1])
elif count_m>=K:
    #print("ni")
    ans = min(tmp, tmp2, -x[count_m-K])
else:
    #print("san", tmp, tmp2)
    ans = min(tmp, tmp2)


print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
    min_l = min(0, l[i])
    max_l = max(0, l[i+k-1])
    min_abs = min(abs(min_l), abs(max_l))
    max_abs = max(abs(min_l), abs(max_l))
    ans = min(ans, min_abs * 2 + max_abs)
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
A = MAP()

if min(A) > 0:
	print(A[K-1])
	sys.exit()
elif max(A) < 0:
	print(abs(list(reversed(A))[K-1]))
	sys.exit()

# print(A)
o = bisect.bisect_left(A, 0)
# print(o)

ans = float('INF')
for i in range(K+1):
	j = K - i
	# print('=====', i, j)
	if j==0 and o+K <= N-1:
		# print('zenbu migi',abs(A[o+K-1]))
		ans = min(ans, abs(A[o+K]))
	elif i==0 and o-K >= 0:
		# print('zenbu hidari', abs(A[o-K]))
		ans = min(ans, abs(A[o-K]))
	elif o+i-1 <= N-1 and o-j >=0:
		if i<j:
			# print('migi itte hidari', abs(A[o+i-1]*2) + abs(A[o-j]))
			ans = min(ans, abs(A[o+i-1]*2) + abs(A[o-j]))
		elif i>j:
			# print('hidari itte migi', abs(A[o+i-1]) + abs(A[o-j]*2))
			ans = min(ans, abs(A[o+i-1]) + abs(A[o-j]*2))
		else:
			ans = min(ans, abs(A[o+i-1])+abs(A[o-j])*2, abs(A[o+i-1])*2+abs(A[o-j]))

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import bisect
N,K=map(int,input().split())
X=[int(i) for i in input().split()]
min_=10**10
for i in range(N-K+1):
    #print(i,i+K-1)
    temp=min(abs(X[i])+abs(X[i]-X[i+K-1]),abs(X[i+K-1])+abs(X[i]-X[i+K-1]))
    min_=min(temp,min_)
print(min_)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
if k==1:
    ans=1000000000
    for i in range(n):
        now=abs(x[i])
        if now<ans:
            ans=now
    print(ans)
else:
    ans=1000000000
    for i in range((n-1)-k+1+1):
        l=x[i]
        r=x[i+(k-1)]
        if l<0 and r<0:
            check=abs(l)
            if check<ans:
                ans=check
        if 0<=l and 0<=r:
            check=abs(r)
            if check<ans:
                ans=check
        if l<0 and 0<=r:
            check_l=abs(l)
            check_r=abs(r)
            min_c=min(check_l,check_r)
            max_c=max(check_l,check_r)
            check=min_c*2+max_c
            if check<ans:
                ans=check
    print(ans)

Submission status: Accepted

Solution code:
N,  K = map(int, input().split())
candles = list(map(int, input().split()))
ans = float('inf')
for i in range(N - K + 1):
    if candles[i] * candles[i + K - 1] >= 0:
        t = max(abs(candles[i]), abs(candles[i + K - 1]))
    else:
        t = candles[i + K - 1] - candles[i] + min(abs(candles[i]), abs(candles[i + K - 1]))
    ans = min(ans, t)
print(ans)

Submission status: Accepted

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
if 0 not in x:
    bisect.insort(x, 0)
    N += 1
    K += 1
ans = 10 ** 9
for i in range(N-K+1):
    lo, hi = x[i], x[i+K-1]
    if lo <= 0 <= hi:
        ans = min(ans, abs(lo) + hi + min(abs(lo), hi))
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
l,r = 0,0
anus = 10**9
for i in range(n):
  l = i
  r = i+(k-1)
  if r >= n:
    break
  anus = min(anus,abs(x[l])+abs(x[r]-x[l]),abs(x[r])+abs(x[r]-x[l]))
print(anus)

Submission status: Accepted

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

Submission status: Accepted

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

l_x=sorted([abs(i) for i in x if i<0])
r_x=[i for i in x if i>=0]

ans=10**12
for r_num in range(k+1):
  l_num=k-r_num
  if l_num>len(l_x) or r_num>len(r_x):
    continue
  if l_num==0: #右片道
    ans=min(ans,r_x[r_num-1])
  elif r_num==0: #左片道
    ans=min(ans,l_x[l_num-1])
  elif l_x[l_num-1]<=r_x[r_num-1]: #左往復右片道
    ans=min(ans,l_x[l_num-1]*2+r_x[r_num-1])
  else: #右往復左片道
    ans=min(ans,l_x[l_num-1]+(r_x[r_num-1])*2)

print(ans)

Submission status: Accepted

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

l_x=[i for i in x if i<0]
r_x=[i for i in x if i>=0]

ans=10**12
for r_num in range(k+1):
  l_num=k-r_num
  
  if l_num>len(l_x) or r_num>len(r_x):
    continue
  if l_num==0: #右片道
    ans=min(ans,r_x[r_num-1])
  elif r_num==0: #左片道
    ans=min(ans,abs(l_x[-1*l_num]))
  elif abs(l_x[-1*l_num])<=r_x[r_num-1]: #左往復右片道
    ans=min(ans,abs(l_x[-1*l_num])*2+r_x[r_num-1])
  else: #右往復左片道
    ans=min(ans,abs(l_x[-1*l_num])+(r_x[r_num-1])*2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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):
    a1=abs(x[i])+abs(x[i+k-1]-x[i])
    a2=abs(x[i+k-1])+abs(x[i+k-1]-x[i])
    a.append(min(a1,a2))
print(min(a))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
n, k = [int(i) for i in sys.stdin.readline().split()]
x_ls = [int(i) for i in sys.stdin.readline().split()]
zero_pos = bisect.bisect_left(x_ls, 0)
x_ls = x_ls[:zero_pos] + [0] + x_ls[zero_pos:]
len_x = len(x_ls)
INF = 10**18
best = INF
for left in range(1+min(k, zero_pos)):
    right = k - left
    if zero_pos + right >= len_x:
        continue
    _min = x_ls[zero_pos - left]
    _max = x_ls[zero_pos + right]
    res = abs(_min) + abs(_max) + min(abs(_min), abs(_max))
    best = min(res, best)
print(best)

Submission status: Accepted

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

n, k = map(int, input().split())
A=[]; B=[]
x=list(map(int,input().split()))
for i in range(n-(k-1)):
    candi = max(abs(x[i+k-1]),abs(x[i]))
    if x[i+k-1]*x[i]<0:
        candi += 2*min(abs(x[i+k-1]), abs(x[i]))

    ans = min(ans, candi)
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):
    a = abs(A[i] - A[i+K-1])
    ans = min(ans, abs(A[i])+a, abs(A[i+K-1])+a)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=[int(x) for x in input().split()]
B=[]
C=[]
for i in range(N):
  if A[i]<0:
    C += [-A[i]]
  else:
    B +=[A[i]]
C=sorted(C)
b=len(B)
c=len(C)
B=[0]+B+[float('inf')]*c
C=[0]+C+[float('inf')]*b
answer=float('inf')

for i in range(K):
  answer=min(answer,2*B[i]+C[K-i],2*C[i]+B[K-i])
print(answer)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
arr = list(map(int,input().split()))
left = []
right = []
for i in arr:
    if i < 0:
        left.append(abs(i))
    else:
        right.append(i)
left.sort()
tmp = 0
count = 0
if len(right) == 0:
    print(left[K-1])
    exit()
if len(right) >= K:
    tmp += right[K-1]
    i = 1
    while True:
        if len(left) < i or K-i-1 < 0:
            break
        tmp2 = right[K-i-1] + left[i-1] + min(right[K-i-1],left[i-1])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
else:
    tmp += right[-1]+left[K-len(right)-1]+min(right[-1],left[K-len(right)-1])
    i = 1
    while True:
        if K-len(right)-1+i >= K or i >= len(right) or K-len(right)-1+i >= len(left)-1:
            break
        tmp2 = right[-1-i] + left[K-len(right)-1+i] + min(right[-1-i],left[K-len(right)-1+i])
        i += 1
        if tmp2 < tmp:
            tmp = tmp2
print(tmp)

Submission status: Accepted

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

X.sort()

ma = max(X)
mi = min(X)

if ma <= 0:
    X.sort(key=lambda x: -x)
    a = 0
    for i in range(K):
        a = X[i]
    print(abs(a))
elif 0 <= mi:
    a = 0
    for i in range(K):
        a = X[i]
    print(abs(a))
else:
    ans = 1000000000000000000
    for i in range(N):
        l = i
        r = i + K - 1
        if r < N and X[l] <= 0 <= X[r]:
            if abs(X[l]) > abs(X[r]):
                ans = min(abs(X[r]) * 2 + abs(X[l]), ans)
            else:
                ans = min(abs(X[l]) * 2 + abs(X[r]), 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(N - K + 1):
    #x = x[i : i + K]

    if x[i + K - 1] <= 0:
        tmp_min = abs(x[i])
    elif 0 <= x[i]:
        tmp_min = abs(x[i + K - 1])
    else:
        PassTwice = min(abs(x[i + K - 1]) ,abs(x[i])) * 2
        PassOnce = max(abs(x[i + K - 1]) ,abs(x[i]))
        tmp_min = PassTwice + PassOnce
    if tmp_min < ans:
        ans = tmp_min
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(N-K+1):
    if x[i]*x[i+K-1] >=0:
        ans = min(ans, max(abs(x[i]),abs(x[i+K-1])))
    else:
        ans = min(ans, max(abs(x[i]),abs(x[i+K-1]))+2*min(abs(x[i]),abs(x[i+K-1])))
print(ans)

Submission status: Accepted

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

INF = float('inf')
ans = INF

if N == K:
    ans = abs(a[N-1]-a[0])+min(abs(a[0]), abs(a[N-1]))
else:
    for i in range(N-K+1):
        ans = min(ans, abs(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()))

def culclen(l,r):
  return abs(X[r]-X[l])+min(abs(X[l]),abs(X[r]))

ans=10**9
for x in range(len(X)-K+1):
  ans=min(ans,culclen(x,x+K-1))
print(ans)

Submission status: Accepted

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

def culclen(l,r):
  if X[l]*X[r]>=0:
    return max(abs(X[l]),abs(X[r]))
  else:
    ldis=abs(X[l])
    rdis=abs(X[r])
    return min(ldis,rdis)*2+max(ldis,rdis)

ans=10**9
for x in range(len(X)-K+1):
  ans=min(ans,culclen(x,x+K-1))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, K: int, x: "List[int]"):
    x = np.array(x)
    score = 10**9
    x_s = sorted(x[x < 0], reverse=True)
    x_plus = x[x >= 0]
    x_len_plus = len(x_plus)
    x_len_minus = len(x_s)
    for right in range(min(K + 1, x_len_plus + 1)):
        left = min(K - right, x_len_minus)
        if right + left == K:
            if x_len_plus > 0:
                rpath = x_plus[right - 1]
            else:
                rpath = 0
            if x_len_minus > 0:
                lpath = abs(x_s[left - 1])
            else:
                lpath = 0
            score = min(score, rpath + lpath + min(rpath, lpath))

    print(score)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    solve(N, K, x)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial
inf = 10**9

n,k = [int(x) for x in stdin.readline().rstrip().split()]

li = list(map(int,stdin.readline().rstrip().split()))
mi = 10**10
for i in range(n-k+1):
    mi2 = min(abs(li[i+k-1])+abs(li[i+k-1]-li[i]),abs(li[i])+abs(li[i+k-1]-li[i]))
    mi = min(mi,mi2)

print(mi)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline


def main():
    N, K = map(int, readline().split())
    X = list(map(int, readline().split()))

    if N == 1:
        print(abs(X[0]))
        sys.exit()

    dm = 10**9
    for i in range(0, N-K+1):
        d = X[i+K-1] - X[i] + min(abs(X[i+K-1]), abs(X[i]))
        if d < dm:
            dm = d

    print(dm)


main()

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 = i
    # 右端
    r = i + K - 1
    # 2点間の距離
    d1 = X[r] - X[l]
    # 0から両端への距離の小さい方
    d2 = min(abs(X[l]), abs(X[r]))
    ans = min(ans, d1 + d2)

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

for n in range(N-K+1):
    t = abs(X[n]-X[n+K-1]) + min(abs(X[n]), abs(X[n+K-1]))
    Y.append(t)

if N - K == 0:
    ans = abs(X[0] - X[-1]) + min(abs(X[0]), abs(X[-1]))
    print(ans)
else:
    print(min(Y))

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

print(ans)

Submission status: Accepted

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

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

ans = float("inf")
for i in range(n - k + 1):
    min_pos = xl[i]
    max_pos = xl[i+k-1]
    if min_pos * max_pos < 0:
        tmp = min(2*abs(min_pos) + abs(max_pos), abs(min_pos) + 2*abs(max_pos))
    else:
        tmp = max(abs(min_pos), abs(max_pos))
    if tmp < ans:
        ans = tmp
        
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
x = LIST()


ans = INF
idx = bisect(x, 0)
for i in range(idx-K, idx+1):
	if 0 <= i <= N-K:
		if i == idx-K:
			ans = min(ans, abs(x[i]))
		elif i == idx:
			ans = min(ans, x[i+K-1])
		else:
			ans = min(ans, 2*abs(x[i])+x[i+K-1], abs(x[i])+2*x[i+K-1])
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
import bisect
n,k = map(int,input().split())
xl = list(map(int,input().split()))
x1 = xl[:bisect.bisect_right(xl,0)][::-1]
x2 = xl[bisect.bisect_right(xl,0):]
m = 10**10
if len(x1) >= k:
  m = min(m,-x1[k-1])
if len(x2) >= k:
  m = min(m,x2[k-1])
for i in range(k-1):
  try:
    m = min(m,x2[i]-2*x1[k-i-2],2*x2[i]-x1[k-i-2])
  except:
    continue
print(m)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split(' '))
X = list(map(int, input().split(' ')))

ans = 10**11
for left in range(N-K+1):
    right = left+K-1

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

    # print(dist)
    ans = min(ans, dist)

print(ans)

Submission status: Accepted

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

fst = 0
las = k-1
dis = []
while las < n:
	dis.append(x[las]-x[fst] + min(abs(x[las]), abs(x[fst])))
	fst += 1
	las += 1
print(min(dis))

Submission status: Accepted

Solution code:
#                         author:  kagemeka 
#                         created: 2019-11-09 16:39:51(JST)
### modules
## from standard library
import sys
# import collections
# import math
# import string
import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
## from external libraries
# import scipy.special   
# import scipy.misc      
# import numpy as np 

def main():
    n, k, *x = (int(i) for i in sys.stdin.read().split())

    minimums = []
    for i in range(n - k + 1):

        left = x[i]
        right = x[i + k - 1]
        mi = min(abs(left) + abs(right - left), abs(right) + abs(left - right))
        minimums.append(mi)
    
    ans = min(minimums)
    print(ans)


if __name__ == "__main__":
    # execute only if run as a script
    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(0, N - K + 1):
	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)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
import bisect
id = bisect.bisect_left(x,0)
ans=10**10
if id == N:
  print(abs(x[N-K]))
  exit(0)
for i in range(max(0,(K+id)-N),min(id+1,K)):
  right = x[K+id-1-i]
  left=0
  if i>0:
    left =x[id-i]
  tmp=min(abs(left)*2+right,right*2+abs(left)) 
  ans=min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
n, k =  map(int, input().split())
X = list(map(int, input().split()))
c = float("inf")
for i in range(n-k+1):
  if X[i] < 0 and X[i+k-1] >= 0:
    temp = min(2*(-X[i])+X[i+k-1], 2*X[i+k-1]+(-X[i]))
  else:
    temp = max(abs(X[i]), abs(X[i+k-1]))
  c = min(c, temp)
print(c)

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split()) 
    x = list(map(int,input().split()))
    m=10**11
    for i in range(n-k+1):
        if x[i]>0:
            m=min(x[i+k-1],m)
        elif x[i+k-1]<0:
            m=min(m,-x[i])
        else:
            m=min(m,x[i+k-1]-x[i]+x[i+k-1],x[i+k-1]-2*x[i])
    print(m)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)