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: Wrong Answer

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

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

ans = 3 * 10 ** 8
for i in range(N - K):
  ans = min(ans, 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: Wrong Answer

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):
    if x[i] >= 0:
        time = x[i+K-1]
    if x[i] < 0:
        if x[i+K-1] >= 0:
            time = (-x[i])*2 + x[i+K-1]
        else:
            time = -x[i]
    min_time = min(min_time,time)
print(min_time)

Submission status: Wrong Answer

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

lft_num = bisect.bisect(x, 0)
rgt_num = n - lft_num

ans = 10**10
for i in range(min(rgt_num, k)):
    r = x[lft_num + i]
    distance = abs(r)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num - (k-i-1) >= 0:
        l = x[lft_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
for i in range(min(lft_num, k)):
    l = x[lft_num - i - 1]
    distance = abs(l)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num + (k-i) < n:
        r = x[lft_num + (k-i)]
        distance += abs(r - l)
        ans = min(ans, distance)
    
print(ans)

Submission status: Wrong Answer

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

lft_num = bisect.bisect(x, 0)
rgt_num = n - lft_num

ans = 10**10
for i in range(min(rgt_num, k)):
    r = x[lft_num + i]
    distance = abs(r)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num - (k-i-1) > 0:
        l = x[lft_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
for i in range(min(lft_num, k)):
    l = x[lft_num - i - 1]
    distance = abs(l)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if rgt_num - (k-i-1) > 0:
        r = x[lft_num + rgt_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
    
print(ans)

Submission status: Wrong Answer

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

lft_num = bisect.bisect(x, 0)
rgt_num = n - lft_num

print(rgt_num, lft_num)
ans = 10**10
for i in range(min(rgt_num, k)):
    r = x[lft_num + i]
    distance = abs(r)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if lft_num - (k-i-1) > 0:
        l = x[lft_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
for i in range(min(lft_num, k)):
    l = x[lft_num - i - 1]
    distance = abs(l)
    if i+1 == k: 
        ans = min(ans, distance)
        continue
    if rgt_num - (k-i-1) > 0:
        r = x[lft_num + rgt_num - (k-i-1)]
        distance += abs(r - l)
        ans = min(ans, distance)
    
print(ans)

Submission status: Wrong Answer

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

answer = 10*9

for i in range(N):
    if i+K-1>= N: break
    answer = min(answer,abs(X[i+K-1]-X[i])+min(abs(X[i]),abs(X[i+K-1])))

print(answer)

Submission status: Wrong Answer

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

import numpy as np 


flag = 0
x_plus = [i for i in X if i>=0]
x_minus = [i for i in X if i<0]
x_minus.reverse()

len_plus = len(x_plus)
len_minus = len(x_minus)

use_x = []
answer = 0

i = 0
j = 0
if len_plus==0:
    answer = abs(x_minus[K-1])
    
if len_minus==0:
    answer = abs(x_plus[K-1])

if len_plus!=0 and len_minus!=0:
    while(1):
        if len(use_x)==K: break
        if j>=len_minus:
            use_x.append(x_plus[i])
            i += 1
            continue
        elif i>=len_plus:
            use_x.append(x_minus[j])
            j += 1
            continue
        else:
            if x_plus[i] > abs(x_minus[j]):
                use_x.append(x_minus[j])
                j += 1
            else:
                use_x.append(x_plus[i])
                i += 1
    use_x.sort()
    # print(use_x)
    answer = use_x[-1]-use_x[0]+min(use_x[-1],abs(use_x[0]))

print(answer)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
if x[0]>=0:
    print(x[k-1])
    exit()
if x[-1]<=0:
    print(x[-k])
    exit()
neg=[]
pos=[]
for i in range(n):
    if x[i]<0:
        neg.append(-x[i])
    else:
        pos.append(x[i])
all=neg+pos
all.sort()
neg.sort()
l=all[k-1]
ans=0
if l in neg:
    cnt=0
    if pos[-1]>=l:
        while pos[cnt]<l:
            cnt+=1
        ans=2*pos[cnt-1]+l
    else:
        ans=2*pos[-1]+neg[k-len(pos)-1]
else:
    cnt=0
    if neg[-1]>=l:
        while neg[cnt]<l:
            cnt+=1
        ans=2*neg[cnt-1]+l
    else:
        ans=2*neg[-1]+pos[k-len(neg)-1]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
#%% input
inf = 10**8
ans = inf
for i in range(N-K+1):
    if(x[i+K-1]<0):
        if(len(x)>i+K):
            if(x[i+K]<=0):
                continue
        ans = min(ans,-x[i])
    elif(x[i]>0):
        if(i>0):
            if(x[i-1]>=0):
                continue
        ans = min(ans,x[i+K-1])
    else:
        ans = min(ans,2*x[i+K-1]-x[i],x[i+K-1]-2*x[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
inf = 10**8
ans = inf
for i in range(N-K+1):
    if(x[i+K-1]<0):
        if(len(x)>i+K):
            if(x[i+K]<=0):
                break
        ans = min(ans,-x[i])
    elif(x[i]>0):
        if(i>0):
            if(x[i-1]>=0):
                break
        ans = min(ans,x[i+K-1])
    else:
        ans = min(ans,2*x[i+K-1]-x[i],x[i+K-1]-2*x[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
inf = 10**8
ans = inf
for i in range(N-K+1):
    if(x[i+K-1]<0):
        if(len(x)>i+K):
            if(x[i+K]<0):
                break
        ans = min(ans,-x[i])
    elif(x[i]>0):
        if(i>0):
            if(x[i-1]>0):
                break
        ans = min(ans,x[i+K-1])
    else:
        ans = min(ans,2*x[i+K-1]-x[i],x[i+K-1]-2*x[i])
print(ans)

Submission status: Wrong Answer

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

neg_x = []
for i in range(n):
    if x[i] < 0:
        neg_x.append(x[i])
    if x[i] >= 0:
        break
pos_x = x[len(neg_x):]
neg_x = neg_x[::-1]

"""if len(neg_x) == 0:
    print(pos_x[k - 1])
elif len(pos_x) == 0:
    print(-1 * neg_x[k - 1])
else:
    total_all = []
    for i in range(max(0, k - len(pos_x)), min(k, len(neg_x))):
        if i == 0:
            total1 = 0
        else:
            total1 = -1 * neg_x[i - 1]
        if k - i == 0:
            total2 = 0
        else:
            total2 = pos_x[k - i - 1]
        total = min(total1 * 2 + total2, total2 * 2 + total1)
        total_all.append(total)
    print(min(total_all))"""

Submission status: Wrong Answer

Solution code:
def main():
	N, K = map(int, input().split())
	xi = list(map(int, input().split()))
	neg, pos = [], []
	is_zero_include = False
	for x in xi:
		if x < 0:
			neg.insert(0, -x)
		elif x == 0:
			is_zero_include = True
		elif x > 0:
			pos.append(x)

	ans = 10**9
	if is_zero_include:
		K -= 1
		if K == 0:
			ans = 0

	neg_len = len(neg)
	pos_len = len(pos)

	# not turn
	if neg_len > K:
		ans = min(ans, neg[K-1])
	if pos_len > K:
		ans = min(ans, pos[K-1])

	def include_turn(turned, no_turn, turned_len, no_turn_len, ans):
		this_len = min(turned_len, K-1)
		for i in range(this_len):
			this_ans = 2*turned[i]
			next_pos = K-i-2
			if next_pos < 0:
				continue
			if no_turn_len <= next_pos:
				continue
			this_ans += no_turn[next_pos]
			if this_ans < ans:
				ans = this_ans
		return ans

	ans = include_turn(neg, pos, neg_len, pos_len, ans)
	ans = include_turn(pos, neg, pos_len, neg_len, ans)

	print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
	N, K = map(int, input().split())
	xi = list(map(int, input().split()))
	neg, pos = [], []
	is_zero_include = False
	for x in xi:
		if x < 0:
			neg.insert(0, -x)
		elif x == 0:
			is_zero_include = True
		elif x > 0:
			pos.append(x)

	ans = 10**9
	if is_zero_include:
		K -= 1
		if K == 0:
			ans = 0

	neg_len = len(neg)
	pos_len = len(pos)

	# not turn
	if neg_len > K:
		ans = neg[K-1]
	if pos_len > K:
		ans = min(ans, pos[K-1])

	def include_turn(turned, no_turn, turned_len, no_turn_len, ans):
		this_len = min(turned_len, K-1)
		for i in range(this_len):
			this_ans = 2*turned[i]
			next_pos = K-i-2
			if next_pos < 0:
				continue
			if no_turn_len <= next_pos:
				continue
			this_ans += no_turn[next_pos]
			if this_ans < ans:
				ans = this_ans
		return ans

	ans = include_turn(neg, pos, neg_len, pos_len, ans)
	ans = include_turn(pos, neg, pos_len, neg_len, ans)

	print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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]
        else:
            time =-x[l]
            if time < min_time:
                min_time = time
    elif 0 <= x[l]:
        if first:
            min_time = x[r]
        else:
            time = x[r]
            if time < min_time:
                min_time = time
    else:
        if first:
            min_time = 2 * (x[r] - x[l]) - max(-x[l], x[r])
            brk = True
        else:
            time =  2 * (x[r] - x[l]) - max(-x[l], x[r])
            if time < min_time:
                min_time = time
print(min_time)

Submission status: Wrong Answer

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]
        else:
            time =-x[l]
            if time < min_time:
                min_time = time
    elif 0 <= x[l]:
        if first:
            min_time = x[r]
        break
    else:
        if first:
            min_time = 2 * (x[r] - x[l]) - max(-x[l], x[r])
            brk = True
        else:
            time =  2 * (x[r] - x[l]) - max(-x[l], x[r])
            if time < min_time:
                min_time = time
print(min_time)

Submission status: Wrong Answer

Solution code:
def solve(n, k, x):
    ans = 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: Wrong Answer

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

ans=10**7

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: Wrong Answer

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

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: Wrong Answer

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

x_dict = {i:abs(i) for i in x}

l = []
for i, j in sorted(x_dict.items(), key=lambda x: x[1]):
  l.append(i)
  if len(l) == min(k+1,n):
    break
    
if len(l) == n:
  l = sorted(l + [0])
  ans = abs(max(l)) - (min(l)) + min(abs(max(l)), abs(min(l)))
  print(ans)
else:
  l1 = sorted(sorted(l)[1:] + [0])
  ans1 = abs(max(l1)) - (min(l1)) + min(abs(max(l1)), abs(min(l1)))
  l2 = sorted(sorted(l)[:-1] + [0])
  ans2 = abs(max(l2)) - (min(l2)) + min(abs(max(l2)), abs(min(l2)))
  print(min(ans1, ans2))

Submission status: Wrong Answer

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

x_dict = {i:abs(i) for i in x}

l = []
x_sort = sorted(x_dict.items(), key=lambda x: x[1])
for i in range(n-1):
  if x_sort[i] == x_sort[i+1]:
    if len(x_sort[:i]>0) > len(x_sort[:i]<0):
      a = x_sort[i]
      x_sort[i] = x_sort[i+1]
      x_sort[i+1] = a
      
for i, j in x_sort:
  l.append(i)
  if len(l) >= k:
    break
    
l = sorted(l + [0])
ans = abs(max(l)) - (min(l)) + min(abs(max(l)), abs(min(l)))
print(ans)

Submission status: Wrong Answer

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

x_dict = {i:abs(i) for i in x}

l = []
for i, j in sorted(x_dict.items(), key=lambda x: x[1]):
  l.append(i)
  if len(l) >= k:
    break
    
l = sorted(l + [0])
ans = abs(max(l)) - (min(l)) + min(abs(max(l)), abs(min(l)))
print(ans)

Submission status: Wrong Answer

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

x_dict = {i:abs(i) for i in x}

l = []
for i, j in sorted(x_dict.items(), key=lambda x: x[1]):
  l.append(i)
  if len(l) >= k:
    break
    
l = sorted(l + [0])
ans = abs(max(l)) + abs(min(l)) + min(abs(max(l)), abs(min(l)))
print(ans)

Submission status: Wrong Answer

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(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: Wrong Answer

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, 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: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

ans = 1e8
for i in range(n-k+1):
    r = x[i+k-1]
    l = x[i]
    length = abs(r - l) + min(abs(l), abs(r))
    if length < ans:
        ans = length

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n-k+1):
    r = x[i+k-1]
    l = x[i]
    length = abs(r - l) + min(abs(l), abs(r))
    if length < ans:
        ans = length

print(ans)

Submission status: Wrong Answer

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

d_min = 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: Wrong Answer

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

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

if n==k:
    print(X[k-1]-X[0]+min(abs(X[0]),abs(X[k-1])))
else:
    print(min(V))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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]-l[i])
  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(l[i+m-1]-l[i])
print(min(l1))

Submission status: Wrong Answer

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

score = 10**8
if K == 1:
    score = min(map(abs,x))
else:
    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: Wrong Answer

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

score = 10**8
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: Wrong Answer

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()
    x_gt_0 = [(v, "g") for v in filter(lambda x: x > 0, X)]
    x_ge_0 = [(v, "g") for v in filter(lambda x: x >= 0, X)]
    x_le_0 = [(v, "l") for v in reversed(list(map(abs, filter(lambda x: x <= 0, X))))]
    x_lt_0 = [(v, "l") for v in reversed(list(map(abs, filter(lambda x: x < 0, X))))]
    f = x_ge_0 + [(inf, "g")] * max(0, K - len(x_ge_0))
    fb = list(heapq.merge(map(twice, x_ge_0), x_lt_0))
    bf = list(heapq.merge(map(twice, x_le_0), x_gt_0))
    b = x_le_0 + [(inf, "l")] * max(0, K - len(x_le_0))
    last_f_fb = inf
    last_b_fb = inf
    last_f_bf = inf
    last_b_bf = inf
    for i in range(K-1):
        v, d = fb[i]
        if d == "g":
            last_f_fb = v
        else:
            last_b_fb = v
        v, d = bf[i]
        if d == "g":
            last_f_bf = v
        else:
            last_b_bf = v
    goal, d = min(f[K-1], fb[K-1], bf[K-1], b[K-1])
    ans_f, _ = f[K-1]
    ans_b, _ = b[K-1]
    ans_fb, d = fb[K-1]
    if d == "f":
        ans_fb += last_b_fb
    else:
        ans_fb += last_f_fb
    ans_bf, d = bf[K-1]
    if d == "f":
        ans_bf += last_b_bf
    else:
        ans_bf += last_f_bf
    print(min(ans_f, ans_fb, ans_bf, ans_b))
    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: Wrong Answer

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(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: Wrong Answer

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(li[n-1],abs(li[0])))
else:
  for i in range(k,n):
    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: Wrong Answer

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

min_time = 2 * 10 ** 8
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: Wrong Answer

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

min_time = 2 * 10 ** 8
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:
            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: Wrong Answer

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

left=0
right=K-1
ans = []
for i in range(N-K+1):
    ans.append(x[right]-x[left])
print(min(ans))

Submission status: Wrong Answer

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(X[-k])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int,input().split()))
from bisect import bisect_left as bl
X = [-1*float("inf")]+X+[float("inf")]
zero = bl(X,0)
ans = 0
left = 0
right = 0
if X[zero] == 0:
    l = zero -1
    r = zero +1
else:
    l = zero - 1
    r = zero

count = 0
while count < k:
    if r >= n+1 or l < 0:
        break
    if X[r] < abs(X[l]):
        right = X[r]
        r += 1
        count += 1
    elif abs(X[l]) <= X[r]:
        left = abs(X[l])
        l -= 1
        count += 1

print(min(right,left)*2+max(right,left))

Submission status: Wrong Answer

Solution code:
import bisect

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

sep = bisect.bisect(x, 0)

R = x[:sep][::-1] + [-10**7]
L = x[sep:] + [10**7]

for i in range(len(R)):
  R[i] = -R[i]
  
ans = 10**8
if K <= len(R):
  ans = min(R[K-1], ans)
if K <= len(L):
  ans = min(L[K-1], ans)

if len(L) is not 0 and len(R) is not 0:
  for i in range(len(R)):
    if K-(i+1)-1 >= 0 and K-(i+1)-1 < len(L):
      ans = min(ans, R[i]*2 + L[K-(i+1)-1])
  for i in range(len(L)):
    if K-(i+1)-1 >= 0 and K-(i+1)-1 < len(R):
      ans = min(ans, L[i]*2 + R[K-(i+1)-1])
  
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

sep = bisect.bisect(x, 0)

R = x[:sep][::-1] + [-10**7]
L = x[sep:] + [10**7]

for i in range(len(R)):
  R[i] = -R[i]
  
ans = 10**6
if K <= len(R):
  ans = min(R[K-1], ans)
if K <= len(L):
  ans = min(L[K-1], ans)

if len(L) is not 0 and len(R) is not 0:
  for i in range(len(R)):
    if K-(i+1)-1 >= 0 and K-(i+1)-1 < len(L):
      ans = min(ans, R[i]*2 + L[K-(i+1)-1])
if len(R) is not 0 and len(L) is not 0:
  for i in range(len(L)):
    if K-(i+1)-1 >= 0 and K-(i+1)-1 < len(R):
      ans = min(ans, L[i]*2 + R[K-(i+1)-1])
  
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

sep = bisect.bisect(x, 0)

R = x[:sep][::-1] + [-10**7]
L = x[sep:] + [10**7]

for i in range(len(R)):
  R[i] = -R[i]
  
ans = 10**6
if K < len(R):
  ans = min(R[K-1], ans)
if K < len(L):
  ans = min(L[K-1], ans)

if len(L) is not 0 and len(R) is not 0:
  for i in range(len(R)):
    if K-(i+1)-1 >= 0 and K-(i+1)-1 < len(L):
      ans = min(ans, R[i]*2 + L[K-(i+1)-1])
if len(R) is not 0 and len(L) is not 0:
  for i in range(len(L)):
    if K-(i+1)-1 >= 0 and K-(i+1)-1 < len(R):
      ans = min(ans, L[i]*2 + R[K-(i+1)-1])
  
print(ans)

Submission status: Wrong Answer

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

def calc_dis(li,li_double):
    min_dis=0
    i,j=0,0
    while(i+j<K):
        if li[i]<li_double[j]*2:
            min_dis+=li[i]
            i+=1
        else:
            min_dis+=li_double[j]*2
            j+=1
    return min_dis

position_neg=[abs(i) for i in position if i<0]
position_neg.append(0)
position_neg.reverse()
position_neg.append(10**17)
position_pos=[i for i in position if i>=0]
position_pos.append(10**17)
position_pos.insert(0,0)

dis_neg=[position_neg[j+1]-position_neg[j] for j in range(len(position_neg)-1)]
dis_pos=[position_pos[j+1]-position_pos[j] for j in range(len(position_pos)-1)]
x,y=calc_dis(dis_neg,dis_pos),calc_dis(dis_pos,dis_neg)
print(min(x,y))

Submission status: Wrong Answer

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

def calc_dis(li,li_double):
    min_dis=0
    i,j=0,0
    while(i+j<K):
        if li[i]<li_double[j]*2:
            min_dis+=li[i]
            i+=1
        else:
            min_dis+=li_double[j]*2
            j+=1
    return min_dis

position_neg=[abs(i) for i in position if i<0]
position_neg.append(0)
position_neg.reverse()
position_neg.append(10**9)
position_pos=[i for i in position if i>=0]
position_pos.append(10**9)
position_pos.insert(0,0)

dis_neg=[position_neg[j+1]-position_neg[j] for j in range(len(position_neg)-1)]
dis_pos=[position_pos[j+1]-position_pos[j] for j in range(len(position_pos)-1)]
x,y=calc_dis(dis_neg,dis_pos),calc_dis(dis_pos,dis_neg)
print(min(x,y))

Submission status: Wrong Answer

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

for i in range(n-k+1):
    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: Wrong Answer

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
  elif mink > abs(x) + x - lx:
    mink = abs(x) + x - lx
print(mink)