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 bisect

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

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

if x[idx] == 0 and idx - k + 1 >= 0:
    ans = min(ans,-x[idx-k+1])

print(ans)
exit()

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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

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

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

if x[idx] == 0 and idx - k + 1 >= 0:
    ans = min(ans,-x[idx-k+1])


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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

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

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""
ans = 10**100

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

print(ans)

Submission status: Runtime Error

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1) <= l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1) <= l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    if K-1 < l_mi:
        ans = min(ans, _mi[K-1])

    if K-1 < l_pi:
        ans = min(ans, _pi[K-1])
    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Runtime Error

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1+1) <= l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1+1) <= l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    if K-1 < l_mi:
        ans = min(ans, _mi[K-1])

    if K-1 < l_pi:
        ans = min(ans, _pi[K-1])
    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Runtime Error

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1) <= l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1) <= l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    if K-1 < l_mi:
        ans = min(ans, _mi[K-1])

    if K-1 < l_pi:
        ans = min(ans, _pi[K-1])
    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Runtime Error

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1) < l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1) < l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    if K-1 < l_mi:
        ans = min(ans, _mi[K])

    if K-1 < l_pi:
        ans = min(ans, _pi[K])
    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Runtime Error

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1) < l_pi:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1) < l_mi:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Runtime Error

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1+1) < l_pi:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1+1) < l_mi:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
m = float("inf")
for i in range(N-K+1):
    if X[i] > 0:
        d = X[i+K-1]
    elif X[i+K-1] < 0:
        bgd = -X[i]
    else:
        d = X[i+K-1]-X[i]+min(-X[i],X[i+K-1])
    m = min(m,d)
print(m)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
zero_idx = 0
for i, x in enumerate(x_list):
  if x >= 0:
    zero_idx = i
    break
start_idx = max([zero_idx-k, 0])
end_idx = zero_idx
if zero_idx+k > n:
  end_idx = n - k
min_score = 1000000000
print(start_idx)
print(end_idx)
for i in range(start_idx, end_idx+1):
  print(x_list[i:i+k])
  minus_score = abs(min((x_list[i]), 0))
  plus_score = abs(max(x_list[i+k-1], 0))
  double = min(minus_score, plus_score)
  score = minus_score + plus_score + double
  if min_score > score:
    min_score = score
    
print(min_score)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
x = np.array(list(map(int, input().split())))
plus = x[x>=0][::-1].tolist()
minus = np.sort(x[x<0]).tolist()

cnt = 0
cntp = 0
cntm = 0
index = 0
output = []
if len(plus)==0:
    print(abs(minus[-k]))
elif len(minus)==0:
    print(plus[-k])
else:
    while cntp+cntm < k:
        if plus[-1] != minus[-1]:
            if plus[-1] < -1*minus[-1]:
                output.append(plus.pop())
                cntp += 1
            else:
                print(cntp, cntm)
                output.append(minus.pop())
                cntm +=1
        else:
            if cntp+cntm+2 > k:
                if cntp > cntm:
                    output.append(plus.pop())
                    cntp += 1
                else:
                    output.append(minus.pop())
                    cntm += 1
            else:
                output.append(plus.pop())
                output.append(minus.pop())
                cntp += 1
                cntm += 1
    print(output)
    output.sort()
    d = -2*(min(output)) + max(output)
    print(d)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
x = np.array(list(map(int, input().split())))
plus = x[x>=0][::-1].tolist()
minus = np.sort(x[x<0]).tolist()
print(plus)
print(minus)
print()
cnt = 0
cntp = 0
cntm = 0
index = 0
output = []
if len(plus)==0:
    print(abs(minus[-k]))
elif len(minus)==0:
    print(plus[-k])
else:
    while cntp+cntm < k:
        if plus[-1] != minus[-1]:
            if plus[-1] < -1*minus[-1]:
                output.append(plus.pop())
                cntp += 1
            else:
                print(cntp, cntm)
                output.append(minus.pop())
                cntm +=1
        else:
            if cntp+cntm+2 > k:
                if cntp > cntm:
                    output.append(plus.pop())
                    cntp += 1
                else:

                    output.append(minus.pop())
                    cntm += 1
            else:
                output.append(plus.pop())
                output.append(minus.pop())
                cntp += 1
                cntm += 1
    print(output)
    output.sort()
    d = -2*(min(output)) + max(output)
    print(d)

Submission status: Runtime Error

Solution code:

n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k-1]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam == near_zero_big:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 3*10**5
for i range(N):
  if i + K > N:
    break
  if A[i]*A[i+K] <= 0:
    if abs[i] < abs[i+K]:
      ans = min(ans, abs[i]*2+abs[i+K])
    else:
      ans = min(ans, abs[i]+abs[i+K]*2)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
zero_idx = 0
for idx in range(N):
    if A[idx] > 0:
        zero_idx = idx
        break
if zero_idx == 0 and A[0] < 0:
  print(A[N-K])
elif zero_idx == 0 and A[0] > 0:
  print(A[K-1])
else 
  ans = 3*10**5
  for plus in range(N-zero_idx+1):
    if plus > K:
        break
    minus = K - plus
    if minus > zero_idx:
    	continue
    p_dis = A[zero_idx+plus-1]
    if plus == 0:
        p_dis = 0
    m_dis = A[zero_idx-minus]
    if minus == 0:
        m_dis = 0
    if p_dis >= abs(m_dis):
        ans = min(ans, p_dis+abs(m_dis)*2)
    else:
        ans = min(ans, abs(m_dis)+p_dis*2)
  print(ans)

Submission status: Runtime Error

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

#ゼロに最も近い正負の要素を含むようにK個の配列を舐める
#全部負または全部正(max*min>=0)ならabs(max-min)
#正負またぐ負場合(max*min<0)、
# min(負の絶対値最大*2+正の最大,負の絶対値最大+正の最大*2)

#符号切り替わりサーチ
for i in range(N):
    if x[i]>=0:
        pos_start=i
        break

#K個の配列をスライド
ans=2*(max(x)-min(x))   #ans init. 考えられる最大プラスアルファ?
for i in range(K+1):
    start=pos_start-K+i
    end=pos_start+i
    #print('search start,end ',start,end)
    if start>=0 and end<=N:
        #print('start,end ',start,end)
        obj_arr=x[start:end]
        M=max(obj_arr)
        m=min(obj_arr)
        if M*m>=0:
            ans=min(ans,max(abs(M),abs(m)))
        else:
            ans=min(ans,min(abs(m)*2+M,abs(m)+M*2))
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
# if 0 in x:
#     x.remove(0)
#     k -= 1
#     n -= 1
#
# if n == 0:
#     print(0)

si = bisect_left(x, 0)


ans = x[-1] - x[0]
if si + k - 1 < len(x):
    ans = min(ans, x[si + k - 1])
if si - k >= 0:
    ans = min(ans, abs(x[si - k]))

for i in range(n):
    tmp_ans = 0
    if i < si:
        tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
        ans = min(ans, tmp_ans)
    elif i - k + 1 >= 0:
        tmp_ans = x[i] * 2 + abs(x[i - k + 1])
        ans = min(ans, tmp_ans)

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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



for i, v in enumerate(x):
    if v == 0:
        point = i
        break
    elif v > 0:
        point = i - 1
        break

if len(x[:point+1]) <= K:
    start = 0
else:
    start = point - K + 1

end = start + K -1
ans = 10 ** 9
        
for i in range(K):
    if abs(x[start+i]) <= x[end+i]:
        time = abs(x[start+i] * 2) + x[end+i]
    else:
        time = abs(x[start+i]) + (x[end+i] * 2)
    ans = min(ans, time)
    if end + i == len(x) - 1:
        break
    
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math
n,k=list(map(int,input().split()))
c=list(map(int,input().split()))
m=math.inf
for i in range(n-k+1):
        l=c[i]
        r=c[i+k-1]
        m=min(m,abs(l)+abs(r-l),abs(r)+abs(r-l))

print(m)

Submission status: Runtime Error

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

def solve(X):
    l = N
    for i in range(len(X)):
        if X[i] >= 0:
            l = i
            break
    if i == 0:          # X[i] > 0 for all i
        return X[K-1]
    if i == N:          # X[i] < 0 for all i
        return -X[-K]

    if X[i] == 0:
        K -= 1
        L, R = X[:i-1], X[i+1:]
    else:
        L, R = X[:i], X[i:]
    L = sorted([-r for r in L])
    #print(L, R) 
    m = 10 ** 10
    if K < len(L):
        m = min(m, L[K-1])
    if K < len(R):
        m = min(m, R[K-1])
    for r in range(len(R)):
        l = K-r-2
        if not 0 <= l < len(L):
            continue
        t =  2 * L[l] + R[r]
        #print("L", "t=%d"%t, "2*%d"%L[l], R[r], sep="\t")
        m = min(m, t)
    for l in range(len(L)):
        r = K-l-2
        if not 0 <= r < len(R):
            continue
        t = 2 * R[r] + L[l]
        #print("R", "t=%d"%t, L[l], "2*%d"%R[r], sep="\t")
        m = min(m, t)
    return m

print(solve(X))

Submission status: Runtime Error

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

def solve(X):
    l = N
    for i in range(len(X)):
        if X[i] >= 0:
            l = i
            break
    if i == 0:          # X[i] > 0 for all i
        return X[K-1]
    if i == N:          # X[i] < 0 for all i
        return -X[-K]

    if X[i] == 0:
        L, R = X[:i-1], X[i+1:]
    else:
        L, R = X[:i], X[i:]
    #print(L, R) 
    m = 10 ** 10
    for r in range(len(R)):
        l = K-r-1
        if l >= len(L):
            continue
        t =  - 2 * L[-l] + R[r]
        #print("L", "t=%d"%t, R[r], L[-l])
        m = min(m, t)
    for l in range(len(L)):
        r = K-l-1
        if r >= len(R):
            continue
        t = 2 * R[r] - L[-l]
        #print("R", "t=%d"%t, L[-l], R[r])
        m = min(m, t)
    return m

print(solve(X))

Submission status: Runtime Error

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

if  0 in x:
    K-=1
    N-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Runtime Error

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

if  0 in x:
    K-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N-1:
        xp,xm = x[border:],x[:border][::-1]
        if N - border >= K and border >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Runtime Error

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

if  0 in x:
    K-=1
    x.pop(0)

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K+1]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,mmax,i = xn[-1],0,2
            while i <= K and xn[-i] > 0:
                i += 1
            if i <= K:  mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            mmax,i = xn[-1],2
            while i <= K and xn[-i] < 0:
                i += 1
            if i <= K:  pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Runtime Error

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

if  0 in x:
    K-=1
    x.pop(0)

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K+1]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,i = xn[-1],2
            while xn[-i] > 0:
                i += 1
            mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            mmax,i = xn[-1],2
            while xn[-i] < 0:
                i += 1
            pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
X=[int(i) for i in input().split()]+[0]
X.sort()
sn=X.index(0)
road=[]
for j in range(sn):
    if X[j+k]>0:
        road.append(abs(X[j]-0)+abs(X[j+k]-X[j]))
    else:
        road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))

for j in range(sn+1,n-k+1):
    road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))

if len(road)>0:
    print(min(road))
else:
    print(0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
X=[int(i) for i in input().split()]+[0]
X.sort()
sn=X.index(0)
print(X)
road=[]
for j in range(sn):
    if X[j+k]>0:
        road.append(abs(X[j]-0)+abs(X[j+k]-X[j]))
    else:
        road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))

for j in range(sn+1,n-k+1):
    road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))

if len(road)>0:
    print(min(road))
else:
    print(0)

Submission status: Runtime Error

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

x = [int(i) for i in input().split()]

if N == 1 or K == 1:
    print(x[0])
    exit()

suml = []
for left in x:
    if x.index(left) == len(x)-K+1:
        break
    tmp = abs(left) + abs(left - x[x.index(left)+K-1])
    suml.append(tmp)
ansl = min(suml)

sumr = []
for right in reversed(x):
    if x.index(right) == K-1:
        break
    tmp = abs(right) + abs(right - x[x.index(right)-K])
    sumr.append(tmp)
ansr = min(sumr)

print(min(ansl,ansr))

Submission status: Runtime Error

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

xneg = []
xpos = []

for tmp in input().split():
    if int(tmp) < 0:
        xneg.append(int(tmp))
    else:
        xpos.append(int(tmp))

xneg.sort(reverse=True)
xpos.sort()

xp = 0
xn = 0
pre = 0
last1 = 0
last2 = 0
for i in range(K):
    if i == K-1:
        last1 = pre
        if len(xpos) > 0 and len(xneg) > 0:
            if xpos[xp] <= abs(xneg[xn]):
                last2 = xpos[xp]
            else:
                last2 = xneg[xn]
        elif len(xpos) > 0:
            last2 = xpos[xp]
        else:
            last2 = xneg[xn]
    else:
        if len(xpos) > 0 and len(xneg) > 0:
            if xpos[xp] <= abs(xneg[xn]):
                pre = xpos[xp]
                xp += 1
            else:
                pre = xneg[xn]
                xn += 1
        elif len(xpos) > 0:
            ans = xpos[K-1]
            print(ans)
            exit()
        else:
            ans = xneg[K-1]
            print(abs(ans))
            exit()

ans = 2*abs(min(last1,last2)) + abs(max(last1,last2))
print(ans)

Submission status: Runtime Error

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

minus = [0]
plus = [0]
for x in X:
    if x < 0:
        minus.append(abs(x))
    elif x == 0:
        K -= 1
    else:
        plus.append(x)

#負の数の方を絶対値に基づき並べなおす
minus.sort()

ans = []
for i in range(max(0, K - (len(plus) - 1)), min(len(minus) + 1, K)):
    ans.append(min(minus[i] * 2 + plus[K - i], minus[i] + plus[K - i] * 2))

if len(X) == 1:
    ans.append(X[0])

print(min(ans))

Submission status: Runtime Error

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

minus = [0]
plus = [0]
for x in X:
    if x < 0:
        minus.append(abs(x))
    elif x == 0:
        K -= 1
    else:
        plus.append(x)

#負の数の方を絶対値に基づき並べなおす
minus.sort()

ans = []
for i in range(max(0, K - (len(plus) - 1)), min(len(minus) + 1, K)):
    print(i, K-i)
    ans.append(min(minus[i] * 2 + plus[K - i], minus[i] + plus[K - i] * 2))

if len(X) == 1:
    ans.append(X[0])

print(min(ans))

Submission status: Runtime Error

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

minus = [0]
plus = [0]
for x in X:
    if x < 0:
        minus.append(abs(x))
    elif x == 0:
        K -= 1
    else:
        plus.append(x)

#負の数の方を絶対値に基づき並べなおす
minus.sort()

ans = []
for i in range(max(0, K - len(plus), min(len(minus), K))):
    ans.append(min(minus[i] * 2 + plus[K - i], minus[i] + plus[K - i] * 2))

if len(X) == 1:
    ans.append(X[0])

print(min(ans))

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np

N, K = [int(x) for x in stdin.readline().rstrip().split()]
A = np.array(int(x) for x in stdin.readline().rstrip().split())


def solve(count, time, candle):
    if count == K:
        return time
    if 0 in candle:
        return solve(count + 1, time, np.array([x for x in candle if x != 0]))
    index = 0
    if candle[len(candle) - 1] < 0:
        last = len(candle) - 1
        return solve(count + 1, time - candle[last], np.array([x - candle[last] for x in candle if x - candle[index] != 0]))
    elif candle[0] > 0:
        return solve(count + 1, time + candle[0], np.array([x - candle[0] for x in candle if x - candle[0] != 0]))
    else:
        for l in range(len(candle) - 1):
            if (candle[l] < 0) & (candle[l + 1] > 0):
                index = l
        return min(solve(count + 1, time - candle[index], np.array([x - candle[index] for x in candle if x - candle[index] != 0])),
                   solve(count + 1, time + candle[index + 1],
                         np.array([x - candle[index + 1] for x in candle if x - candle[index + 1] != 0])))


print(solve(0, 0, A))

Submission status: Runtime Error

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


#最初と最後だけ調べればOK
for i in range(N - K + 1):
    tmp_min = x[i]
    tmp_max = x[i + K - 1]

    if tmp_min < 0:
        #混合の場合
        if tmp_max < 0:
            #+に先に行く場合
            if abs(tmp_min) > tmp_max:
                tmp = tmp_max * 2
                tmp += abs(tmp_min)
            #-に先に行く場合
            else:
                tmp = abs(tmp_min) * 2
                tmp += tmp_max
        #すべてマイナスの場合
        else:
            tmp = abs(tmp_min)
    else:
        tmp = tmp_max
    ans = min(ans, tmp)
print(ans)