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: Runtime Error

Solution code:
import sys

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

for i, n in enumerate(p):
  if n >= 0:
    zero_i = i
    break
  zero_i = len(p)

n_list = p[:zero_i]
p_list = p[zero_i:]

if len(n_list) > k:
  n_list = n_list[-k:]

if len(p_list) > k:
  p_list = p_list[:k]

if len(n_list) == 0:
  print(p_list[k-1])
  sys.exit()
elif len(p_list) == 0:
  print(abs(n_list[-k]))
  sys.exit()

# e.g. n_list = [-30,-10]
if len(n_list) == k:
  min_move = abs(n_list[0])
else:
  n_min = abs(n_list[0]) + (p_list[k-len(n_list)-1]*2)
  p_min = (abs(n_list[0])*2) + p_list[k-len(n_list)-1]
  min_move = min(n_min, p_min)

for i, n in enumerate(n_list):
  if (k-(len(n_list)-(i+1))) > len(p_list):
    break

  if i == len(n_list)-1:
    min_move = min(min_move, p_list[-1])
  else:
    n_min = abs(n_list[i+1]) + (p_list[i+1]*2)
    p_min = (abs(n_list[i+1])*2) + p_list[i+1]
    min_move = min(min_move, n_min, p_min)

print(min_move)

Submission status: Runtime Error

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

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

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


print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
a = []
a2 = []
for i in x:
    if i < 0:
        a2.append(2 * abs(i))
    else:
        a.append(i)

if len(a2) == 0:
    print(a[K-1])
    exit()
a2 = sorted(a2)
c1 = float('inf')

for i, v in enumerate(a):
    i = i + 1
    c1 = min(c1, v + a2[K - i - 1])

print(c1)

Submission status: Runtime Error

Solution code:
import bisect
n,k=map(int,input().split())
o=list(map(int,input().split()))
a=1e9

p=bisect.bisect_left(o,0)
for l in range(k):
  r=l+k-1
  if l<=p<=r and r<n:
    if o[l]>=0:
      v=abs(o[r])
    else:
      a=abs(o[l])+2*abs(o[r])
      b=2*abs(o[l])+abs(o[r])
      v=min(a,b)
  a=min(a,v)
print(a)

Submission status: Runtime Error

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

time = 10 ** 20
# 0以上の最小の整数
base = 0
index = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        index = i
        break

if index == 0:
        time = x[K-1]
else:
    for i in range(max(0, index - K), min(index, N)):
        if x[i+K-1] < 0:
            time = min(abs(x[i]), time)
        elif x[i] < 0 and  x[i+K-1] >= 0:
            time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
        else:
            time = min(x[i+K-1], time)

print(time)

Submission status: Runtime Error

Solution code:

N,K = map(int,input().split())
a = list(map(int,input().split()))
x = []
flg = True
for i in range(N):
    if a[i] > 0 and flg == True:
        x.append(0)
        flg = False
    x.append(a[i])


#x.append(0)
#x.sort()

c = []
for i in range(N - K + 2):
    if (0 not in x[i:i+K+1]) or (len(x[i:i+K+1]) != K+1):
        continue
    p = x[i:i+K+1]
    if min(p) < 0 and max(p) > 0:
        if p[-1] + p[-1] - p[0] < abs(p[0]) + p[-1] -p[0]:
            c.append(p[-1] + (p[-1] - p[0]))
        else:
            c.append(abs(p[0]) + p[-1] - p[0])
    elif min(p) >= 0:
        c.append(p[-1])
    elif max(p) <= 0:
        c.append(abs(p[0]))

print(min(c))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
a = []
a2 = []
for i in x:
    if i < 0:
        a2.append(2 * abs(i))
    else:
        a.append(i)

if len(a2) == 0:
    print(a[K-1])
    exit()
a2 = sorted(a2)
c1 = float('inf')

for i, v in enumerate(a):
    i = i + 1
    c1 = min(c1, v + a2[K - i - 1])

b = []
b2 = []
for i in x:
    if i > 0:
        b2.append(2 * abs(i))
    else:
        b.append(-i)

if len(b2) == 0:
    print(b[K-1])
    exit()
b = sorted(b)
c2 = float('inf')
for i, v in enumerate(b):
    i = i + 1
    c2 = min(c2, v + b2[K - i - 1])

print(min(c1,c2))

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
n = len(x)
if 0 in x:
    x.remove(0)
    k = k - 1

if len(x) == 0:
    print(0)
elif k == 0:
    print(0)
else:
    scores = []

    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)


    xx = np.array(x)
    xx = - xx
    xx = xx[::-1]
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)


    print(np.min(scores))

Submission status: Runtime Error

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

o = 0
if x[N-1] < 0:
	print(x[N-K])
	exit()
if x[0] > 0:
	print(x[K-1])
	exit()

while x[o] < 0:
	o += 1
	if o == N-1:
		break

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

Submission status: Runtime Error

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

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

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


print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

for i, n in enumerate(p):
  if n >= 0:
    zero_i = i
    break
  zero_i = len(p)

n_list = p[:zero_i]
p_list = p[zero_i:]

if len(n_list) > k:
  n_list = n_list[-k:]

if len(p_list) > k:
  p_list = p_list[:k]

if len(n_list) == 0:
  print(p_list[k-1])
  sys.exit()
elif len(p_list) == 0:
  print(abs(n_list[-k]))
  sys.exit()

# e.g. n_list = [-30,-10]
if len(n_list) == k:
  min_move = abs(n_list[0])
else:
    n_min = abs(n_list[0]) + (p_list[k-len(n_list)]*2)
    p_min = (abs(n_list[0])*2) + p_list[k-len(n_list)]
    min_move = min(n_min, p_min)

for i, n in enumerate(n_list):
  if (k-(i+1)) > len(p_list):
    break

  if i == len(n_list)-1:
    min_move = min(min_move, p_list[-1])
  else:
    n_min = abs(n_list[i+1]) + (p_list[i+1]*2)
    p_min = (abs(n_list[i+1])*2) + p_list[i+1]
    min_move = min(min_move, n_min, p_min)

print(min_move)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")
ans = 0
# 正の数 or 負の数 onlyなら別処理
if listMap[0] >= 0 or listMap[-1] <= 0:
    sorted(listMap)
    ans = abs(listMap[K - 1])

else:
    for j in range(N - K):
        L = j
        R = j + K - 1
        if listMap[L] <= 0 <= listMap[R]:
            sumLR = abs(listMap[L] - listMap[R])
            if sumLR < minMin:
                minMin = sumLR
                minL = L

    # 距離を確認
    if -listMap[minL] < listMap[minL + K - 1]:
        ans = -listMap[minL] * 2 + listMap[minL + K - 1]
    else:
        ans = listMap[minL + K - 1] * 2 + -listMap[minL]

print(ans)

Submission status: Runtime Error

Solution code:
# N, K = 9, 5
# X = [int(i) for i in '-9 -7 -4 -3 0 1 2 3 4'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)
res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])

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

Submission status: Runtime Error

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

list1=[]
list2=[]
for i in range(len(x)):
    if x[i]<0:
        list2.append(x[i])
    else:
        list1.append(x[i])
list2.sort(reverse=True)
#print(list1)
#print(list2)
if N==1:
    print(abs(x[0]))
else:
    
    list3=[]
    for j in range(1,K):
        if len(list1)-j>=K-1:
            list3.append(list1[K-1])
        elif len(list1)-j>=0:
            list3.append(list1[len(list1)-j]*2+list2[K-2-len(list1)+j]*(-1))
            list3.append(list1[len(list1)-j]+list2[K-2-len(list1)+j]*(-2))
        elif list2[K-len(list1)+J]<=len(list2)-1 :
            list3.append(list2[K-1]*(-1))
            break
        else:
            break
        #print(j)
    #print(list3)
    print(min(list3))

Submission status: Runtime Error

Solution code:
W, H = map(int,input().split())

input_list = []

del_list1 = []
del_list2 = []

for i in range(0,H):
    input_list.append(list(input()))
for i in range(len(input_list)):
    if input_list[i][0] == input_list[i][1]:
        count = 0
        for j in range(len(input_list[0])):
            if input_list[i][0] == input_list[i][j]:
                count += 1
        if count == len(input_list[i]):
            del_list1.append(i)

for i in range(len(input_list[0])):
    if input_list[0][i] == input_list[1][i]:
        count = 0
        for j in range(len(input_list)):
            if input_list[0][i] == input_list[j][i]:
                count += 1
        if count == len(input_list):
            del_list2.append(i)


for i in del_list1:
    input_list.pop(i)


for i in reversed(del_list2):
    for tmp in input_list:
        tmp.pop(i)

for i in input_list:
    for j in i:
        print(j, end="")
    print()

Submission status: Runtime Error

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

time = 10 ** 20
# 0以上の最小の整数
base = 0
index = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        index = i
        break

for i in range(max(0, index - K), min(index, N)):
    if x[i+K-1] < 0:
        time = min(abs(x[i]), time)
    elif x[i] < 0 and  x[i+K-1] >= 0:
        time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
    else:
        time = min(x[i+K-1], time)

print(time)

Submission status: Runtime Error

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

def binarySearch(D, target):
    left = 0
    right = len(D)
    while left < right:
        mid = (left + right) // 2
        if D[mid] == target:
            return mid, 0
        elif target < D[mid]:
            right = mid
        else:
            left = mid + 1
    return mid, 1

# 全てが正の場合
if x[0] >= 0:
    re = x[K-1]
# 全てが負の場合
elif x[-1] <= 0:
    re = abs(x[-K]) # 絶対値
else:
    zero, plus = binarySearch(x, 0)
    s = zero + plus
    e = s + K +1
    if e > N:
        s = s - (e - N) + 1
        e = N - 1
    if N == K:
        s = 0
        e = N
    d_plus = x[e]
    d_minor =  abs(min(x[s], 0)) 
    d_min = 2 * min(d_plus, d_minor) + max(d_plus, d_minor)
    while True:
        d_plus = x[e]
        if d_plus < 0:
            d_plus = 0
        d_minor =  abs(min(x[s], 0)) 
        d = 2 * min(d_plus, d_minor) + max(d_plus, d_minor)
        d_min = min(d_min, d)
#        print(x[s], x[e], d_min)
        e -= 1
        s -= 1
        if e < zero + plus or s < 0:
            break
    re = d_min
    
print(re)

Submission status: Runtime Error

Solution code:
# N, K = 9, 5
# X = [int(i) for i in '-9 -7 -4 -3 0 1 2 3 4'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)


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

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

Submission status: Runtime Error

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

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

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


print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

for i, n in enumerate(p):
  if n >= 0:
    zero_i = i
    break

n_list = p[:zero_i]
p_list = p[zero_i:]

if len(n_list) > k:
  n_list = n_list[-k:]

if len(p_list) > k:
  p_list = p_list[:k]

if len(n_list) == 0:
  print(p_list[k-1])
  sys.exit()
elif len(p_list) == 0:
  print(abs(n_list[-k]))
  sys.exit()

# e.g. n_list = [-30,-10]
if len(n_list) == k:
  min_move = abs(n_list[0])
else:
    n_min = abs(n_list[0]) + (p_list[k-len(n_list)]*2)
    p_min = (abs(n_list[0])*2) + p_list[k-len(n_list)]
    min_move = min(n_min, p_min)

for i, n in enumerate(n_list):
  if (k-(i+1)) > len(p_list):
    break

  if i == len(n_list)-1:
    min_move = min(min_move, p_list[-1])
  else:
    n_min = abs(n_list[i+1]) + (p_list[i+1]*2)
    p_min = (abs(n_list[i+1])*2) + p_list[i+1]
    min_move = min(min_move, n_min, p_min)

print(min_move)

Submission status: Runtime Error

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

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

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


print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

for i, n in enumerate(p):
  if n >= 0:
    zero_i = i
    break

n_list = p[:zero_i]
p_list = p[zero_i:]

if len(n_list) > k:
  n_list = n_list[-k:]

if len(p_list) > k:
  p_list = p_list[:(k+1)]

if len(n_list) == 0:
  print(p_list[k-1])
  sys.exit()
elif len(p_list) == 0:
  print(abs(n_list[-k]))
  sys.exit()

# e.g. n_list = [-30,-10]
if len(n_list) == k:
  min_move = abs(n_list[0])
else:
    n_min = abs(n_list[0]) + (p_list[k-len(n_list)]*2)
    p_min = (abs(n_list[0])*2) + p_list[k-len(n_list)]
    min_move = min(n_min, p_min)

for i, n in enumerate(n_list):
  if (k-(i+1)) > len(p_list):
    break

  if i == len(n_list)-1:
    min_move = min(min_move, p_list[-1])
  else:
    n_min = abs(n_list[i+1]) + (p_list[i+1]*2)
    p_min = (abs(n_list[i+1])*2) + p_list[i+1]
    min_move = min(min_move, n_min, p_min)

print(min_move)

Submission status: Runtime Error

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(abs(x[len(x)-k]))
    elif x[0] > 0:
        print(x[k-1])    
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            
            else:
                if i - k + 1 >= 0:
                    ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        if r-k >= 0:
            ans = abs(x[r-k])
        ass = min(ass,ans)
        print(ass)

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
n = len(x)
if 0 in x:
    x.remove(0)
    k = k - 1

try:
    if len(x) == 0:
        print(0)
    else:
        scores = []

        xx = np.array(x)
        a = xx >= 0
        pos = sum(a)
        neg = n - pos
        pos_ind = a
        try:
            b = np.where(a)[0][0]
        except:
            b = 0

        if pos >= k:
            scores.append(np.sum(xx[pos_ind][k-1]))
    #     print(scores)
        for i in range(min([pos,k])):
            if b + i - k + 1 < 0:
                break
            score = xx[b + i] * 2 - xx[b + i - k + 1]
            scores.append(score)
    #     print(scores)



        xx = np.array(x)
        xx = - xx
        xx = xx[::-1]
        a = xx >= 0
        pos = sum(a)
        neg = n - pos
        pos_ind = a
        try:
            b = np.where(a)[0][0]
        except:
            b = 0

        if pos >= k:
            scores.append(np.sum(xx[pos_ind][k-1]))
    #     print(scores)
        for i in range(min([pos,k])):
            if b + i - k + 1 < 0:
                break
            score = xx[b + i] * 2 - xx[b + i - k + 1]
            scores.append(score)
    #     print(scores)


        print(np.min(scores))
    except:
        print(0)

Submission status: Runtime Error

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

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

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


print(ans)

Submission status: Runtime Error

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

time = 10 ** 20
# 0以上の最小の整数
base = 0
index = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        index = i
        break

for i in range(max(0, index - K), index + 1):
    if x[i+K-1] < 0:
        time = min(abs(x[i]) * 2, time)
    elif x[i] < 0 and  x[i+K-1] >= 0:
        time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
    else:
        time = min(x[i+K-1] * 2, time)

print(time)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")
ans = 0
minLR = ""
# 正の数 or 負の数 onlyなら別処理
if listMap[0] >= 0 or listMap[-1] <= 0:
    sorted(listMap)
    ans = abs(listMap[K - 1])

else:
    for j in range(N - K):
        L = j
        R = j + K - 1
        if listMap[L] <= 0 <= listMap[R]:
            sumLR = abs(listMap[L] - listMap[R])
            if sumLR < minMin:
                minMin = sumLR
                ans = L

    # 距離を確認
    if -listMap[L] < listMap[R]:
        ans = -listMap[L] * 2 + listMap[R]
    else:
        ans = listMap[R] * 2 + -listMap[L]

print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

for i, n in enumerate(p):
  if n >= 0:
    zero_i = i
    break
    
n_list = p[:zero_i]
p_list = p[zero_i:]

if len(n_list) >= k:
  n_list = n_list[-k:]

if len(p_list) >= k:
  p_list = p_list[:(k+1)]
  
if len(n_list) == 0:
  print(p_list[k-1])
  sys.exit()
elif len(p_list) == 0:
  print(abs(n_list[-k]))
  sys.exit()
  
# e.g. n_list = [-30,-10]
if len(n_list) == k:
  min_move = abs(n_list[0])
else:
    n_min = abs(n_list[0]) + (p_list[k-len(n_list)]*2) 
    p_min = (abs(n_list[0])*2) + p_list[k-len(n_list)]
    min_move = min(n_min, p_min)

for i, n in enumerate(n_list):
  if (k-(i+1)) > len(p_list):
    break

  if i == len(n_list)-1:
    min_move = min(min_move, p_list[-1])
  else:
    n_min = abs(n_list[i+1]) + (p_list[i+1]*2) 
    p_min = (abs(n_list[i+1])*2) + p_list[i+1]
    min_move = min(min_move, n_min, p_min)
    
print(min_move)

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
n = len(x)
if 0 in x:
    x.remove(0)
    k = k - 1

if len(x) == 0:
    print(0)
else:
    scores = []

    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)



    xx = np.array(x)
    xx = - xx
    xx = xx[::-1]
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)


    print(np.min(scores))

Submission status: Runtime Error

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

def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))
    ans = float('inf')

    if 0 in x_list:
        k -= 1

    for i in range(n - k + 1):
        if x_list[i] >= 0 and x_list[i+k-1] >= 0:
            ans = min(ans, x_list[i+k-1])
        elif x_list[i] < 0 and x_list[i+k-1] < 0:
            ans = min(ans, -x_list[i])
        else:
            ans = min(ans, -x_list[i] * 2 + x_list[i+k-1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

time = 10 ** 20
# 0以上の最小の整数
base = 0
index = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        index = i
        break

for i in range(base - K, base + 1):
    if x[i+K-1] < 0:
        time = min(abs(x[i]) * 2, time)
    elif x[i] < 0 and  x[i+K-1] >= 0:
        time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
    else:
        time = min(x[i+K-1] * 2, time)

print(time)

Submission status: Runtime Error

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

X_pos = [x for x in X if x >= 0]
X_neg = [x for x in X if x <  0]

X_neg.reverse()

l_pos = len(X_pos)
l_neg = len(X_neg)

R = []

if l_neg == 0:
    R.append(X_pos[K-1])
elif l_pos == 0:
    R.append(-1 * X_neg[K-1])
else:
    for k in range(K):
        if k < l_pos and K-k-2 < l_neg:
            R.append(2 * X_pos[k] - X_neg[K-k-2])
        else:
            break
    for k in range(K):
        if k < l_neg and K-k-2 < l_pos:
            R.append(-2 * X_neg[k] + X_pos[K-k-2])
        else:
            break

print(min(R))

Submission status: Runtime Error

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

zero_idx=bisect.bisect_left(X,0)


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

print ans

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")
ans = 0
minLR = ""
# 正の数 or 負の数 onlyなら別処理
if listMap[0] >= 0 or listMap[-1] <= 0:
    sorted(listMap)
    ans = listMap[K - 1]

else:
    for j in range(N - K):
        L = j
        R = j + K - 1
        if listMap[L] <= 0 <= listMap[R]:
            sumLR = abs(listMap[L] - listMap[R])
            if sumLR < minMin:
                minMin = sumLR
                ans = L

    # 距離を確認
    if -listMap[L] < listMap[R]:
        ans = -listMap[L] * 2 + listMap[R]
    else:
        ans = listMap[R] * 2 + -listMap[L]

print(ans)

Submission status: Runtime Error

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

zero_idx=bisect.bisect_left(X,0)


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

print ans

Submission status: Runtime Error

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

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

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


print(ans)

Submission status: Runtime Error

Solution code:
from bisect import (
    bisect_left,
    bisect_right,
)


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

zi = bisect_left(x, 0)
queue = []
queue.append((zi, zi, zi, 1, x[zi]))
if zi - 1 >= 0:
    queue.append((zi-1, zi-1, zi-1, 1, -x[zi-1]))

mind = 9999999999999999999999999999
while len(queue) > 0:
    i, li, ri, cnt, d = queue.pop()
    #print(i, li, ri, cnt, d)
    if cnt == k:
        mind = d if d < mind else mind
        continue
    if li - 1 >= 0:
        queue.append((li-1, li-1, ri, cnt+1, d+abs(x[li-1] - x[i])))
    if ri + 1 < n:
        queue.append((ri+1, li, ri+1, cnt+1, d+abs(x[ri+1] - x[i])))

print(mind)

Submission status: Runtime Error

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

minp = 0
while X[minp] < 0:
    minp += 1
lidx = 0
if minp > K:
    lidx = minp - K
ridx = lidx + K - 1
left = X[lidx]
right = X[ridx]
mind = right - left + min(abs(left), abs(right))

while ridx < N and left <= minp:
    left = X[lidx]
    right = X[ridx]
    mind = min(mind, right - left + min(abs(left), abs(right)))
    lidx += 1
    ridx += 1

print(mind)

Submission status: Runtime Error

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

X_pos = [x for x in X if x >= 0]
X_neg = [x for x in X if x <  0]

X_neg.reverse()

l_pos = len(X_pos)
l_neg = len(X_neg)

R = []

if l_neg == 0:
    R.append(X_pos[K-1])
elif l_pos == 0:
    R.append(-1 * X_neg[K-1])
else:
    for k in range(K):
        if k < l_pos:
            R.append(2 * X_pos[k] - X_neg[K-k-2])
        else:
            break
    for k in range(K):
        if k < l_neg:
            R.append(-2 * X_neg[k] + X_pos[K-k-2])
        else:
            break

print(min(R))

Submission status: Runtime Error

Solution code:

def abc107_c():
    import sys
    import numpy as np
    input = sys.stdin.readline

    n, k = map(int, input().split())
    x = np.array(list(map(int, input().split())))
    if x[0] >= 0:
        return x[k - 1]
    elif x[-1] <= 0:
        return x[n - k]
    else:
        t = np.sum(x < 0)
        if x[t] == 0:
            k = k - 1

        time = np.inf
        if t >= k:
            time = x[t - k]
        elif (n - t) >= k:
            time = x[t + k - 1]

        for i in range(max(1, n - t - k), min(t, k)):
            left = -x[t - i]
            right = x[t + k - i - 1]

            tmp = min(left, right) * 2 + max(left, right)
            if tmp < time:
                time = tmp

        return time


print(abc107_c())

Submission status: Runtime Error

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


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

    if x[0] < 0:
        diff = abs(x[0])

        for i in range(n):
            x[i] += diff

    print(min(x[k - 1]), x[-k])


if __name__ == '__main__':
    main()