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:
# なぜかうまくいかない
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_right, bisect_left

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

N, K = MAP()
A = LIST()
# 0 0 0の左
zero = A.count(0)
# A.(0)
l = bisect_left(A, 0)
r = bisect(A, 0)
zero = r - l
C = []
for a in A[:l]:
    if a == 0:
        continue
    C.append(a)
    
B = C + A[r:]


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

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

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


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

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

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    from bisect import bisect_left
    st = bisect_left(X, 0)
    if st+K <= N:
        ans = X[st+K-1]
    elif N < st+K:
        ans = X[N-1]
        need = st+K - N
        for n in range(need):
            ans += abs(X[st-n-1]) * 2
        st -= need
    right = min(st+K-1, N-1)
    left = st
    cur = ans
    for r in range(right, st-1, -1):
        left -= 1
        if left < 0:
            break
        cur -= X[r] - X[r-1]
        cur += abs(X[left])*2 - (abs(X[left+1])*2 if left != st-1 else 0)
        ans = min(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans = float("inf")

for left in range(N-K+1):
    right = left + K - 1
    if X[left] <=0 and X[right] <= 0:
        cur = X[left]
    elif X[left] >= 0 and X[right] >= 0:
        cur = X[right]
    else:
        if abs(X[left]) >= abs(X[right]):
            cur = abs(X[right]) * 2 + abs(X[left])
        else:
            cur = abs(X[left]) * 2 + abs(X[right])
    ans = min(ans, cur)

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans = float('inf')
for l in range(K):
    r = l + K - 1
    if r>=N:
        continue
    tmp_1 = abs(x[l]) + abs(x[r]-x[l])
    tmp_2 = abs(x[r]) + abs(x[r]-x[l])
    tmp = min(tmp_1,tmp_2)
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for l in range(K):
    r = l + K - 1
    if l>=N or r>=N:
        continue
    tmp_1 = abs(x[l]) + abs(x[r]-x[l])
    tmp_2 = abs(x[r]) + abs(x[r]-x[l])
    tmp = min(tmp_1,tmp_2)
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for l in range(K):
    r = l + K - 1
    if l>=N-1 or r>=N-1:
        continue
    tmp_1 = abs(x[l]) + abs(x[r]-x[l])
    tmp_2 = abs(x[r]) + abs(x[r]-x[l])
    tmp = min(tmp_1,tmp_2)
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

print(cnt_time)

Submission status: Wrong Answer

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

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

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

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

print(cnt_time)

Submission status: 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):
    l,r=i,i+k-1
    ans=min(ans,abs(A[r]-A[l])+abs(A[r]),abs(A[l])+abs(A[l]-A[r]))
print(ans)

Submission status: 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):
    l,r=i,i+k-1
    ans=min(ans,min((abs(A[r]-A[l])+abs(A[r])),abs(A[l])+abs(A[l]-A[r])))
print(ans)

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):
    l,r=i,i+k-1
    ans=min(ans,min((abs(A[l]-A[r])+abs(A[r])),abs(A[l])+abs(A[l]-A[r])))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = []
f = True
for x in input().split():
    x = int(x)
    if x == 0:
        lst.append(x)
        f = False
    elif x > 0 and f:
        lst.append(0)
        lst.append(x)
        f = False
    else:
        lst.append(x)

ret = 100 ** 100
for x in range(N - K + 1):
    l = lst[x : x + K + 1]
    if l.count(0) == 0:
        continue
    a = l[0]
    b = l[-1]
    tmp = min(abs(a) + abs(a - b), abs(b) + abs(b - a))
    ret = min(ret, tmp)

print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = [int(x) for x in input().split()]
lst1 = [x for x in lst if x > 0]
lst2 = list(reversed([x for x in lst if x < 0]))

len1 = len(lst1)
len2 = len(lst2)
ret = 100 ** 100
for x in range(K + 1):
    print(x)
    lst_a = lst1[:x]
    lst_b = lst2[: K - x]
    if len(lst_a) + len(lst_b) != K:
        continue

    if len(lst_a) == 0:
        ret = min(ret, abs(lst_b[-1]))
    elif len(lst_b) == 0:
        ret = min(ret, lst_a[-1])
    elif lst_a[-1] > abs(lst_b[-1]):
        tmp = abs(lst_b[-1]) * 2 + lst_a[-1]
        ret = min(ret, tmp)
    else:
        tmp = abs(lst_b[-1]) + lst_a[-1] * 2
        ret = min(ret, tmp)

print(ret)

Submission status: Wrong Answer

Solution code:
import sys

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

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

#+++++

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

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

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

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

Submission status: Wrong Answer

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

ans = 10 ** 5 + 1
for i in range(n-k+1):
    l = a[i]
    r = a[i+k-1]
    time = min(abs(l) + abs(l-r), abs(r) + abs(l-r))
    # print(l, r, time)
    ans = min(ans, time)

print(ans)

Submission status: Wrong Answer

Solution code:
num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

    print(time)

Submission status: Wrong Answer

Solution code:
num = input().split(" ")
zahyou = input().split(" ")
num[0] = int(num[0])
num[1] = int(num[1])

plus = []
minus = []
rousoku = []
for i in range(num[0]):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))

# num = [N, K]:int, plus = [n>=0]:int, minus = [n<0]:int

# プラス方向で終了する場合
if(len(plus) <= num[1] / 4):
    time_p = 2 * 10 ** 8
else:
    if(num[1] > len(plus)): #K>|{n|n>=0}|
        plus_tmp = minus[(len(plus)-num[1]):] + plus
        minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
    else:
        plus_tmp = plus[:]
        minus_tmp = minus[:]

    rousoku = plus_tmp[0:num[1]]

    # 位置をずらす処理
    while(True):
        if(len(rousoku) > 1 and len(minus_tmp) > 0 and rousoku[-1]-rousoku[-2] >= (min(min(rousoku), 0)-minus_tmp[-1])*2):
            rousoku.pop(-1)
            rousoku.insert(0, minus_tmp.pop(-1))
        else:
            break

    # 移動にかかる時間の計算
    rousoku_move_plus = []
    for i in rousoku:
        rousoku_move_plus.append(i - min(rousoku) * 2)
    time_p = max(rousoku_move_plus)

# マイナス方向で終了する場合
if(len(minus) <= num[1] / 4):
    time_m = 2 * 10 ** 8
else:
    if(num[1] > len(minus)):
        minus_tmp = minus + plus[:(num[1]-len(minus))]
        plus_tmp = plus[(num[1]-len(minus)):]
    else:
        plus_tmp = plus[:]
        minus_tmp = minus[:]

    rousoku = minus_tmp[(-(num[1]+1)):]

    while(True):
        if(len(rousoku) > 1 and len(plus_tmp) > 0 and rousoku[1]-rousoku[0] >= (plus_tmp[0]-max(max(rousoku), 0))*2):
            rousoku.pop(0)
            rousoku.append(plus_tmp.pop(0))
        else:
            break

    # 移動にかかる時間の計算
    rousoku_move_minus = []
    for i in rousoku:
        rousoku_move_minus.append(max(rousoku) * 2 - i)
    time_m = max(rousoku_move_minus)

print(min(time_p, time_m))

Submission status: Wrong Answer

Solution code:
num = input().split(" ")
zahyou = input().split(" ")
num[0] = int(num[0])
num[1] = int(num[1])

plus = []
minus = []
rousoku = []
for i in range(num[0]):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))

# num = [N, K]:int, plus = [n>=0]:int, minus = [n<0]:int

# プラス方向で終了する場合

if(num[1] > len(plus)): #K>|{n|n>=0}|
    plus_tmp = minus[(len(plus)-num[1]):] + plus
    minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
else:
    plus_tmp = plus[:]
    minus_tmp = minus[:]

rousoku = plus_tmp[0:num[1]]

# 位置をずらす処理
while(True):
    if(len(rousoku) > 1 and len(minus_tmp) > 0 and rousoku[-1]-rousoku[-2] >= (min(min(rousoku), 0)-minus_tmp[-1])*2):
        rousoku.pop(-1)
        rousoku.insert(0, minus_tmp.pop(-1))
    else:
        break

# 移動にかかる時間の計算
rousoku_move_plus = []
for i in rousoku:
    rousoku_move_plus.append(i - min(rousoku) * 2)
time_p = max(rousoku_move_plus)

# マイナス方向で終了する場合

if(num[1] > len(minus)):
    minus_tmp = minus + plus[:(num[1]-len(minus))]
    plus_tmp = plus[(num[1]-len(minus)):]
else:
    plus_tmp = plus[:]
    minus_tmp = minus[:]

rousoku = minus_tmp[(-(num[1]+1)):]

while(True):
    if(len(rousoku) > 1 and len(plus_tmp) > 0 and rousoku[1]-rousoku[0] >= (plus_tmp[0]-max(max(rousoku), 0))*2):
        rousoku.pop(0)
        rousoku.append(plus_tmp.pop(0))
    else:
        break

# 移動にかかる時間の計算
rousoku_move_minus = []
for i in rousoku:
    rousoku_move_minus.append(max(rousoku) * 2 - i)
time_m = max(rousoku_move_minus)

print(min(time_p, time_m))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
idx = np.searchsorted(X, 0)
# print('idx', idx)
ans = 10**10
for i in range(N - K + 1):
    # print('X[i + K - 1]', X[i + K - 1])
    v1 = min(X[i], X[i + K - 1])
    v2 = max(X[i], X[i + K - 1])
    if v1 <= 0 and v2 >= 0:
        v = v2 - v1
        # print(v + np.abs(v1))
        ans = min(v + np.abs(v1), ans)
if idx - K >= 0:
    v = X[idx - K]
    ans = min(ans, v)
if idx + K <= N:
    v = X[idx + K - 1]
    ans = min(ans, v)
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    X = LI()
    X = sorted(X)
    pos_q, neg_q = [], []
    cur_i = N - 1
    for x in X:
        if x < 0:
            neg_q.append(x)
        elif x == 0:
            K -= 1
        else:
            pos_q.append(x)
    if K == 0:
        print(0)
        return
    ans = inf
    pos_q = sorted(pos_q)
    neg_q = sorted(neg_q)[::-1]
    
    for ix, p in enumerate(pos_q):
        if K - ix - 1 > 0: # if still remains
            nokori = K - ix - 1
            if 0 <= nokori - 1 < len(neg_q):
                ans = min(abs(neg_q[nokori-1] - p - p), ans)
        else:
            ans = min(p, ans)

    for ix, n in enumerate(neg_q):
        if K - ix - 1 >= 0:
            nokori = K - ix -1
            if 0 <= nokori - 1 < len(pos_q):
                ans = min(pos_q[nokori-1] + abs(n) + abs(n), ans)
        else:
            ans = min(abs(n), ans)
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
# 座標上の任意の2点を選んだとき、座標0からそのうちの1点に移動し、
# さらに別の1点に移動すれば、その2点間の座標はすべて通過したことになる
# よって、i番目の座標とi+(k-1)番目の座標を選択し、
# 座標0と近い方の座標との距離と2点間の距離の合計が最小となるようなiを選択すればよい

import bisect

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

if k == 1:
    x = x + [float('inf')]
    i = bisect.bisect_left(x, 0)
    print(min(abs(x[i]), abs(x[i+1])))
    
elif k > 1:
    ans = float('inf')
    for i in range(n-k+1):
        dis = min((abs(x[i]) + abs(x[i] - x[i+(k-1)])), abs(x[i+(k-1)] + abs(x[i+(k-1)] - x[i])))
        ans = min(ans, dis)
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = []
for i in range(n-k+1):
    cnt = 0
    cnt += abs(x[i])
    for j in range(i,k+i):
        cnt += abs(x[j+1] - x[j])    
    ans.append(cnt)

print(min(ans))

Submission status: Wrong Answer

Solution code:

import math

S = input().split(" ")
N = int(S[0])
K = int(S[1])

arr = [int(s) for s in input().split(" ")]


def calculate(n, k, arr):
    minsArr = []
    plusArr = []
    for ar in arr:
        if ar < 0:
            minsArr.append(ar)
        else:
            plusArr.append(ar)

    # find start index
    startIndex = -10000

    if len(minsArr) > k:
        startIndex = len(minsArr) - k
    else:
        startIndex = 0

    minValues = []
    while startIndex <= len(minsArr):

        if startIndex+k >= len(arr):
            if len(minValues) == 0:
                minValues.append(math.fabs(arr[0]-arr[-1]))
            break

        s1 = arr[startIndex]
        s2 = arr[startIndex + k - 1]


        minValue = 0

        if s1 * s2 < 0:
            minValue = math.fabs(s2 - s1) + min(math.fabs(s2), math.fabs(s1))
        else:
            #全部为正数
            if s1 >= 0:
                minValue = math.fabs(s2)
            #全部为负数
            else:
                minValue = math.fabs(s1)


        minValues.append(minValue)
        startIndex = startIndex + 1

    return int(min(minValues))


result = calculate(N, K, arr)
print(result)

Submission status: Wrong Answer

Solution code:
import math

S = input().split(" ")
N = int(S[0])
K = int(S[1])

arr = [int(s) for s in input().split(" ")]

def calculate(n,k,arr):
    minsArr = []
    plusArr = []
    for ar in arr:
        if ar < 0:
            minsArr.append(ar)
        else:
            plusArr.append(ar)

    # find start index
    startIndex = -10000

    if len(minsArr) > k:
        startIndex = len(minsArr) - k
    else:
        startIndex = 0

    minValues = []
    while startIndex <= len(minsArr):
        tmpArr = arr[startIndex:startIndex+k]
        if len(tmpArr) < k:
            break
        minValue = 0

        if tmpArr[0]*tmpArr[-1] < 0:
            minValue = math.fabs(tmpArr[-1] - tmpArr[0]) + min(math.fabs(tmpArr[-1]),math.fabs(tmpArr[0]))
        else:
            minValue = math.fabs(tmpArr[-1])

        minValues.append(minValue)
        startIndex  = startIndex + 1

    return int(min(minValues))

result = calculate(N,K,arr)
print(result)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
answer = float('inf')
for l, r in zip(x, x[K - 1 :]):
    print(l, r)
    cost = r - l + min(abs(l), abs(r))
    answer = cost if cost < answer else answer

print(answer)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
answer = float('inf')
for l, r in zip(x, x[K - 1:]):
    print(l, r)
    cost = r - l + min(abs(l), abs(r))
    answer = cost if cost < answer else answer

print(answer)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
#    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
#    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
#    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
#    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        k=k-1
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
#    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
#    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
#    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
#    print(4)
    print(t)

else:
    iz=bisect_left(x,0)-1
    if x[iz+1]==0:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)-1]*2
            t2=-x[kt]*2+x[kt+(k-1)-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
#    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
#    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
#    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
#    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)-1]*2
            t2=-x[kt]*2+x[kt+(k-1)-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
#    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
#    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
#    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
#    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        for i in range(iz+2,min(iz+k,k)):
            kt=i
            t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
            if t<tmin:
                tmin=t
        for i in range(max(iz+2-k,0),iz):
            kt=i
            t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
            if t<tmin:
                tmin=t
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        for i in range(iz+2,min(iz+k,k)):
            kt=i
            t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
            if t<tmin:
                tmin=t
        for i in range(max(iz+2-k,0),iz):
            kt=i
            t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
            if t<tmin:
                tmin=t
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

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

if x[n-1]<0 :
    t=x[-k]
    print(1)
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        for i in range(iz+2,min(iz+k,k)):
            kt=i
            t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
            if t<tmin:
                tmin=t
        for i in range(max(iz+2-k,0),iz):
            kt=i
            t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
            if t<tmin:
                tmin=t
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1))+1):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000

Submission status: Wrong Answer

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

if len(x) == 1:
    print(abs(x[0]))
else:
    ans = 10 ** 8
    for i in range(n-k+1):
        tmp = min(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans,tmp+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()]

if len(x) == 1:
    print(abs(x[0]))
else:
    ans = 10 ** 15
    for i in range(n-k):
        tmp = min(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans,tmp+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()]

if len(x) == 1:
    print(abs(x[0]))
else:
    ans = 10 ** 10
    for i in range(n-k):
        tmp = min(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans,tmp+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()]

if len(x) == 1:
    print(abs(0-x[0]))
else:
    ans = 10 ** 8
    for i in range(n-k):
        tmp = min(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans,tmp+abs(x[i+k-1]-x[i]))
    print(ans)