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())
lsx = list(map(int,input().split()))
if 0 in lsx:
    K = K-1
    lsx.remove(0)
if bool(lsx) == False or K == 0:
    print(0)
    sys.exit()
lshu = []
for i in range(N):
    if lsx[i] < 0:
        lshu.append(lsx[i])
    else:
        sei = i
        break
lssei = lsx[sei:]
lenhu = len(lshu)
lensei = len(lssei)
lshu2 = [abs(lshu[i]) for i in range(lenhu-1,-1,-1)]
dishu = [0] + lshu2
dissei = [0] + lssei
ans = 2*10**8
for i in range(K):
    if i <= lenhu and K-i <= lensei:
        ans = min(ans,2*dishu[i]+dissei[K-i],dishu[i]+2*dissei[K-i])
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    P = []
    M = []
    for x in X:
        if x < 0:
            M.append(-1 * x)
        else:
            P.append(x)
    M.reverse()

    if len(P) == 0:
        print(M[K - 1])
        exit()
    elif len(M) == 0:
        print(P[K - 1])
        exit()

    ans = 10 ** 10
    for i, m in enumerate(M, start=1):
        d = None
        if i == K + 1:
            break
        elif i == K:
            d = m
        else:
            p_idx = K - i - 1
            d = m * 2 + P[p_idx]
        if d < ans:
            ans = d

    for i, p in enumerate(P, start=1):
        d = None
        if i == K + 1:
            break
        elif i == K:
            d = p
        else:
            m_idx = K - i - 1
            d = p * 2 + M[m_idx]
        if d < ans:
            ans = d

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
xlst = list(map(int, input().split()))
minus = []
plus = []
for x in xlst:
    if x < 0:
        minus.append(-1 * x)
    elif x > 0:
        plus.append(x)
    else:
        k -= 1
if k == 0:
    print(0)
    exit()
minus.append(0)
minus = minus[::-1]
plus = [0] + plus
ans = 10 ** 20
ml = len(minus)
pl = len(plus)

pos = min(k, ml)
while 1:
    mm = minus[pos - 1]
    pp = plus[k - pos + 1]
    ans_tmp = max(mm, pp) + min(mm, pp) * 2
    ans = min(ans, ans_tmp)
    pos -= 1
    if pos == 0 or k - pos + 1 == pl:
        break
print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

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

pos = bisect_left(x, 0)
if x[pos] == 0:
    k -= 1
else:
    x.insert(pos, 0)

l = pos
r = pos+k

if r >= len(x):
    diff = r-len(x) + 1
    l -= diff
    r -= diff

ans = min(abs(x[l]), x[r])*2 + max(abs(x[l]), x[r])

while r >= pos:
    if l < 0:
        break

    dist = min(abs(x[l]), x[r])*2 + max(abs(x[l]), x[r])
    if dist < ans:
        ans = dist

    l -= 1
    r -= 1

print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
for i in range(N):
    if X[i]<0:
        continue
    elif X[i]==0:
        K-=1
        if i<K:
            mX=[0]+[-x for x in X[:i]][::-1]
        else:
            mX=[0]+[-x for x in X[i-K:i]][::-1]
        pX=[0]+X[i+1:i+1+K]
        break
    else:
        if i<K:
            mX=[0]+[-x for x in X[:i]][::-1]
        else:
            mX=[0]+[-x for x in X[i-K:i]][::-1]
        pX=[0]+X[i:i+K]
        break
ans=10**9
l=K-(len(pX)-1)
n=len(mX)
for i in range(len(pX)-1,-1,-1):
    if l<n:
        ans=min(ans,pX[i]+mX[l]*2,pX[i]*2+mX[l])
    else:
        break
    l+=1
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
from bisect import *

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

if all(list(map(lambda i: i>0, x))):
    print(x[k-1])
    exit()
if all(list(map(lambda i: i<0, x))):
    print(x[n-k])
    exit()

for i in range(n):
    if x[i] == 0:
        k -= 1
        break

# 0をまたぐ場合
zr = bisect_left(x, 0)
zl = zr-1

i = 1 
anss = []
for i in range(1, k+2):
    tmp = 0 
    if zr-k+i >= 0 and zr+i-1 < n:
        tmp = abs(x[zr+i-1] - x[zr-k+i])
#        print('x[{}] = {}'.format(zr-k+i, x[zr-k+i]))
#        print('x[{}] = {}'.format(zr+i-1, x[zr+i-1]))
        if i <= k//2:
            tmp += abs(x[zr+i-1])
        else:
            tmp += abs(x[zr-k+i])
        anss.append(tmp)

ans = min(anss)
print(ans)

Submission status: Runtime Error

Solution code:
#
from resolve import resolve
####################################
####################################
# 以下にプラグインの内容をペーストする
#  
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5 3
-30 -10 10 20 50"""
        output = """40"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 2
10 20 30"""
        output = """20"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """1 1
0"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8 5
-9 -7 -4 -3 1 2 3 4"""
        output = """10"""
        self.assertIO(input, output)


if __name__ == "__main__":
    unittest.main()

Submission status: Runtime Error

Solution code:
n,k= list(map(int,input().split()))
pos= list(map(int,input().split()))
pos_abs =list(map(abs, pos))
pos_abs.sort()
#print(pos_abs)
ls = pos_abs[:k]
high = 0
low = 0
count =0
for a in ls[::-1]:
    if a in pos and high == 0:
        high = a
        count += 1
    elif a not in pos and low ==0:
        low = a
        count += 1
        
    if count == 2:
        break 
if low ==0 and ls.count(ls[-2])==2:
    low = ls[-2]
#print(high,low)
if high >=low:
    print(high+ low*2)
else:
    print(high*2+low)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1
exit()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1
if k==0:
    print(0)
    exit()
ans=10**10
if na>=k:
    ans=-a[-k]
if nb>=k:
    ans=min(ans,b[k-1])
exit()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1
if k==0:
    print(0)
    exit()
ans=10**10
if na>=k:
    ans=-a[-k]
if nb>=k:
    ans=min(ans,b[k-1])
if nb==0:
    print(-a[-k])
    exit()
exit()
if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1
if k==0:
    print(0)
    exit()
ans=10**10
if na>=k:
    ans=-a[-k]
if nb>=k:
    ans=min(ans,b[k-1])
if nb==0:
    print(-a[-k])
    exit()
if na==0:
    print(b[k-1])
    exit()
exit()
for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1
if k==0:
    print(0)
    exit()


ans=10**10
if na>=k and na!=0:
    ans=-a[-k]
if nb==0:
    print(-a[-k])
    exit()
if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
    b=[x[i] for i in range(n) if x[i]>0]
    nb=nb-1

ans=10**10
if na>=k and na!=0:
    ans=-a[-k]
if nb==0:
    print(-a[-k])
    exit()
if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
ans=10**10
if na>=k and na!=0:
    ans=-a[-k]
if nb==0:
    print(-a[-k])
    exit()
if na==0:
    print(b[k-1])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
ans=10**10
if na>=k and na!=0:
    ans=-a[-k]
if nb==0:
    print(-a[-k])
    exit()

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)
if b[0]==0:
    k-=1
ans=10**10
if na>=k and na!=0:
    ans=-a[-k]

for i in range(1,nb+1):
    m=k-i
    if m>na or m<0:
        continue
    elif 0<m<=na:
        ans=min(ans,min(b[i-1]-2*a[-m],2*b[i-1]-a[-m]))
    elif m==0:
        ans=min(ans,b[k-1])
print(ans if k!=0 else 0)

Submission status: Runtime Error

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

x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')

if count1 == 0:
    print(x1[k-1])
elif count0 == 0:
    print(x2[k-1])
else:
    for i in range(min(count0, k)):
        ans = min(ans, x1[i]*2-x2[k-i-2])
    for i in range(min(count1, k)):
        ans = min(ans, -x2[i]*2+x1[k-i-2])
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
n,k = map(int,input().split())
x_ls = list(map(int, input().split()))
x_ls.sort()
if x_ls[0] >= 0:
    print(x_ls[k-1])
    sys.exit()
elif x_ls[-1] <= 0:
    x_ls.sort(reverse=True)
    print(abs(x_ls[k-1]))
    sys.exit()
l = 0
r = k - 1
while True:
    if x_ls[r+1] > 0:
        break
    l += 1
    r += 1
ans = float('inf')
while r < n:
    cost_1 = 2*abs(x_ls[l]) + abs(x_ls[r])
    cost_2 = abs(x_ls[l]) + 2*abs(x_ls[r])
    cost = min(cost_1,cost_2)
    ans = min(cost, ans)
    r += 1
    l += 1
print(ans)

Submission status: Runtime Error

Solution code:
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
  if x[i] == 0:
    p, m = i, i+1
    break
  elif x[i] > 0:
    p, m = i, i
    break
    
pls = x[p:]
mns = x[:m]
ans = math.inf

for i in range(len(pls)):
  if i < K-1:
    if pls[0] == 0:
      n = K - i
    else:
      n = K - i - 1
    if len(mns) >= n:
      tmp = 2*abs(mns[-n]) + pls[i]
      ans = min(ans, tmp)
  if i == K-1:
    ans = min(ans, pls[i])
    break
    
for i in range(len(mns)):
  m = len(mns)-i
  if m < K:
    if pls[0] == 0:
      n = K - m
    else:
      n = K - m - 1
    if len(pls) > n:
      tmp = 2*pls[n] + abs(mns[-m])
      ans = min(ans, tmp)
  if m == K:
    ans = min(ans, abs(mns[i]))
    
print(ans)

Submission status: Runtime Error

Solution code:
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
  if x[i] == 0:
    p, m = i, i+1
    break
  elif x[i] > 0:
    p, m = i, i
    break
    
pls = x[p:]
mns = x[:m]
ans = math.inf

for i in range(len(pls)):
  if i < K-1:
    if pls[0] == 0:
      n = K - i
    else:
      n = K - i - 1
    if len(mns) >= n:
      tmp = 2*abs(mns[-n]) + pls[i]
      ans = min(ans, tmp)
  if i == K-1:
    ans = min(ans, pls[i])
    break
    
for i in range(len(mns)):
  m = len(mns)-i
  if m < K:
    if pls[0] == 0:
      n = K - m
    else:
      n = K - m - 1
    tmp = 2*pls[n] + abs(mns[-m])
    ans = min(ans, tmp)
  if m == K:
    ans = min(ans, abs(mns[i]))
    
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10*8

l=inf
dp=x[N-K:N-1]
i=0
while N-K-i>=0:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if dp[K-1]>=dp[0]:
            tmp=dp[0]*2+dp[K-1]
        else:
            tmp=dp[K-1]*2+dp[0]
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
L=list(map(int,input().split()))
M=M-L.count(0)
if M<=0:
  print(0)
  exit()
mina=10**10
positive=[i for i in L if i>0]
negative=[i for i in L if i<0]
negative=list(reversed(negative))
for i in range(min(M-1,len(positive))):
  mina=min(mina,positive[i]+abs(negative[M-2-i])+min(positive[i],abs(negative[M-2-i])))
if len(positive)>=M:
  mina=min(mina,positive[M-1])
if len(negative)>=M:
  mina=min(mina,abs(negative[M-1]))
print(mina)

Submission status: Runtime Error

Solution code:
n,k=(int(i) for i in input().strip().split(" "))
ar=[int(i) for i in input().strip().split(" ")]
try:
    ar.remove(0)
    k-=1
except ValueError:
    pass

from bisect import bisect
t=bisect(ar,0)
l=ar[:t]
r=ar[t:]

m=99999999999999
if k:
    if len(r)>=k:
        m=r[k-1]
    if len(l)>=k:
        t1=-l[-k]
        if t1<m:
            m=t1

    for r1 in range(k-len(l),min(k,len(r))):
        l1=k-r1
        t1=2*r[r1-1] - l[-l1]
        t2= -2*l[-l1] + r[r1-1]
        if min(t1,t2)<m:
            m=min(t1,t2)
    print(m)
else:
    print(0)

Submission status: Runtime Error

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

if 0 in arr:
    K -= 1

if K==0:
    print(0)
    exit()

left =[]
right= []
for i in arr:
    if i<0:
        left.append(i*(-1))
    if i>0:
        right.append(i)

left = left[::-1]
if len(left)==0:
    print(right[K-1])
    exit()

if len(right)==0:
    print(left[K-1])
    exit()


if len(left)>=K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(left)<K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)>=K:
    ans =[]
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)<K:
    ans =[]
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

Submission status: Runtime Error

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

if 0 in arr:
    K -= 1

if K==0:
    print(0)
    exit()

left =[]
right= []
for i in arr:
    if i<0:
        left.append(i*(-1))
    if i>0:
        right.append(i)

left = left[::-1]
if len(left)==0:
    print(right[K-1])
    exit()

if len(right)==0:
    print(left[K-1])
    exit()


if len(left)>=K and len(right)>=K:
    print(min(abs(left[K-1]),abs(right[K-1])))
    exit()

if len(left)<K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)>=K:
    ans =[]
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)<K:
    ans =[]
    for i in range(len(left)):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

Submission status: Runtime Error

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

if 0 in arr:
    K -= 1

if K==0:
    print(0)
    exit()

left =[]
right= []
for i in arr:
    if i<0:
        left.append(i*(-1))
    if i>0:
        right.append(i)

left = left[::-1]
if len(left)==0:
    print(right[K-1])
    exit()

if len(right)==0:
    print(left[K-1])
    exit()


if len(left)>=K and len(right)>=K:
    print(min(abs(left[K-1]),abs(right[K-1])))
    exit()

if len(left)<K and len(right)>=K:
    ans =[]
    ans.append(right[K-1])
    for i in range(K-2):
        ans.append(2*right[i]+left[K-i-2])
    for i in range(len(left)-1):
        ans.append(2*left[i]+right[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)>=K:
    ans =[]
    ans.append(left[K-1])
    for i in range(K-2):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)-1):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

if len(right)<K and len(left)<K:
    ans =[]
    for i in range(len(left)-1):
        ans.append(2*left[i]+right[K-i-2])
    for i in range(len(right)-1):
        ans.append(2*right[i]+left[K-i-2])
    print(min(ans))
    exit()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
a.append(0)
a.sort()
cnt=0
c=[:k]
for i in range(n):
  if a[i]==0:
    cnt=i
    break
if cnt+k>n-1:
  su=sum(a[cnt-k:cnt])
  for i in range(n-k):
  	if i<=cnt<=i+k:
      
else:
  su=sum(a[cnt:cnt+k])
  for i in range(n-k):
  	if i<=cnt<=i+k:
      if su-a[i]+a[i+1]<su:
        su=a[i-1]

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
Xs = list(mapint())

from bisect import bisect_left
pos = []
neg = []

for x in Xs:
    if x>=0:
        pos.append(x)
    else:
        neg.append(-x)

neg = neg[::-1]

ans = 10**18
if len(pos)>=K:
    ans = min(ans, pos[K-1])
if len(neg)>=K:
    ans = min(ans, neg[K-1])
lenp = len(pos)
lenn = len(neg)
for i in range(min(lenp, K)):
    if K-(i+1)<=lenn:
        ans = min(ans, pos[i]*2+neg[K-i-2])
for i in range(min(lenn, K)):
    if K-(i+1)<=lenp:
        ans = min(ans, neg[i]*2+pos[K-i-2])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

ps = [0] + list(filter(lambda x: x >= 0, X))
ns = [0] + list(sorted(filter(lambda x: x < 0, X), reverse=True))
ans = INF
for i in range(len(ns)):
    val = abs(ns[i]) * 2
    if K - i < len(ps):
        val += abs(ps[K - i])
        ans = min(ans, val)
for i in range(len(ps)):
    val = abs(ps[i]) * 2
    if K - i < len(ns):
        val += abs(ns[K - i])
        ans = min(ans, val)
print(ans)

Submission status: Runtime Error

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

st=0
m=3*10**8
for i in range(n-1):
  if x[i]<0 and x[i+1]>=0:
    st=i+1

for i in range(n-k+2):
  mi=x[k+i-1]-x[i]+min(abs(x[k+i-1]),abs(x[i]))
  print(x[k+i-1],x[i],mi)
  if k+i<st or i>st:continue
  if mi <m:m=mi
print(m)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))

    for i in range(n):
        if X[i] >= 0:
            ori = i
            X.insert(i, 0)
            break
    else:
        ori = n - 1

    res = f_inf
    start = max(0, ori - k)
    end = min(n - k + 1, ori + 1)
    for j in range(start, end):
        left = abs(X[j]) * 2 + abs(X[j + k])
        right = abs(X[j]) + abs(X[j + k]) * 2
        res = min(res, min(left, right))
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
def main():
    n,k = map(int,input().split())
    X = list(map(int,input().split()))

    import bisect

    i = bisect.bisect_left(X,0)
    if X[i] == 0:
        k -= 1


    ans = 10**9
    if k == 0:
        print(0)
        exit()
    for j in range(-k+1,1):
        # print(j)
        if i + j < 0 or i+j+k-1 >len(X)-1:
            continue
        # print(l,r)
        l = X[i+j]
        r = X[i+j+k-1]

        l = min(0,l)
        r = max(r,0)

        a = min(abs(l),abs(r))
        b = max(abs(l),abs(r))

        t = 2*a + b
        ans = min(t,ans)

    print(ans)



main()

Submission status: Runtime Error

Solution code:
def main():
    n,k = map(int,input().split())
    X = list(map(int,input().split()))

    import bisect

    i = bisect.bisect_left(X,0)
    if X[i] == 0:
        k -= 1


    ans = 10**9
    if k == 0:
        print(0)
        exit()
    for j in range(-k,1):
        # print(j)
        if i + j < 0 or i+j+k-1 >len(X)-1:
            continue
        # print(l,r)
        l = X[i+j]
        r = X[i+j+k-1]

        l = min(0,l)
        r = max(r,0)

        a = min(abs(l),abs(r))
        b = max(abs(l),abs(r))

        t = 2*a + b
        ans = min(t,ans)

    print(ans)



main()

Submission status: Runtime Error

Solution code:
def main():
    n,k = map(int,input().split())
    X = list(map(int,input().split()))

    import bisect

    i = bisect.bisect_left(X,0)
    if X[i] == 0:
        k -= 1

    ans = 10**9
    for j in range(-k,1):
        # print(j)
        if i + j < 0 or i+j+k-1 >len(X)-1:
            continue
        l = X[i+j]
        r = X[i+j+k-1]

        l = min(0,l)
        r = max(r,0)

        a = min(abs(l),abs(r))
        b = max(abs(l),abs(r))

        t = 2*a + b
        ans = min(t,ans)

    print(ans)



main()

Submission status: Runtime Error

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

P_list=[0]
N_list=[0]
zero_num=0

for x in X:
    if x < 0:
        N_list.append(-x)
    elif x == 0:
        zero_num=1
    else:
        P_list.append(x)

P_list.sort()
N_list.sort()
P_list,N_list

move=[]
for i in range(K+1):
    j=K-i
#     print(i,j)
    if i >= len(P_list) or j >= len(N_list):
        continue
    move_=min(2*P_list[i]+N_list[j],P_list[i]+2*N_list[j])
#     print(move_)
    move.append(move_)
print(min(move))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
right = [0]
left = []
for x in input().split():
    if int(x) == 0:
        K -= 1
    elif int(x) > 0:
        right.append(int(x))
    else:
        left.append(int(x))
left.reverse()
left = [0] + left
p = []
# go left in the end
for i in range(len(left)):
    if K-i <= 0:
        break
    if K-i > len(right) - 1:
        continue
    p.append(2*right[K-i] + abs(left[i]))
# go right
for i in range(len(right)):
    if K-i <= 0:
        break
    if K-i > len(left) - 1:
        continue
    p.append(2*abs(left[K-i]) + right[i])
print(min(p))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
right = [0]
left = []
for x in input().split():
    if int(x) == 0:
        K -= 1
    elif int(x) > 0:
        right.append(int(x))
    else:
        left.append(int(x))
left.reverse()
left = [0] + left
p = []
# go left in the end
for i in range(len(left)):
    if K-i > len(right) - 1:
        continue
    p.append(2*right[K-i] + abs(left[i]))
# go right
for i in range(len(right)):
    if K-i > len(left) - 1:
        continue
    p.append(2*abs(left[K-i]) + right[i])
print(min(p))

Submission status: Runtime Error

Solution code:
n,k,*a=map(int,open(0).read().split())
b,c=[],[0]
for i in a:
    if i<0:
        b+=[-i]
    elif i==0:
        k-=1
    else:
        c+=[i]
b=[0]+b[::-1]        
ans=10**9
B,C=len(b),len(c)
for i in range([0,abs(C-k-1)][C-k-1<0],B):
    ans=min(ans,b[i]+c[k-i]+min(b[i],c[k-i]))
print(ans)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))

    if X[0] >= 0:
        print(X[k - 1])
        exit()
    elif X[-1] < 0:
        print(abs(X[-(k + 1)]))
        exit()

    for i in range(n):
        if X[i] >= 0:
            ori = i
            break

    res = f_inf
    left = max(ori - k, 0)
    right = min(ori + k, n)
    for i in range(left, right - k + 1):
        RL = abs(X[i + k - 1] - X[i]) + abs(X[i + k - 1])
        LR = abs(X[i]) + abs(X[i + k - 1] - X[i])
        res = min(res, min(RL, LR))
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
import sys
import bisect
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))

    if 0 in X:
        ori = X.index(0)
    else:
        ori = bisect.bisect_left(X, 0)

    R = [0]
    for i in range(1, n):
        R.append(R[-1] + abs(X[i - 1] - X[i]))

    mi = f_inf
    left = 0
    for right in range(k - 1, n):
        if left <= ori <= right:
            cost = R[right] - R[left]
            if mi > cost:
                mi = cost
                res = [left, right]
        left += 1

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


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
x.append(0)
x = sorted(x)
x_left =[]#マイナスのスコアのリスト
x_right=[]#プラスのスコアのリスト
for i in range(n+1):
    if x[i] == 0:
        x_left = x[:i+1]
        x_right = x[i:]

ans = max(x)
ans_1= 0
ans_2= 0
j = 0
# マイナスのリストの要素数が、kより多い場合は、その数値をtmpに入れる
while len(x_left[j:-1]) > k:
    if ans > abs(x_left[j]):
        ans = abs(x_left[j])

#　k が　0 を跨ぐ場合の処理。ここでwhileが繰り返しをしてくれず、不調

while j <= len(x_left)-2:

    ans_1 = abs(x_left[j])*2 + x_right[k-len(x_left[j:-1])]
    ans_2 = abs(x_left[j]) + x_right[k-len(x_left[j:-1])]*2

    tmp = min(ans_1,ans_2)

    if j == 0:
        ans = tmp
    j += 1
    ans = min(ans, tmp)


#　プラスのリストの要素数が、kより多い場合は、その数値をtmpに入れる
m=0
if len(x_right) > k:
    tmp = x_right[k]
    ans = min(ans,tmp)

print(ans)

Submission status: Runtime Error

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

index0 = N
for i in range(N):
  if x[i] >= 0:
    index0 = i
    break
    
if index0 == 0:
  print(x[K-1])
  exit()
  
if index0 == N:
  print(-x[N-K])

p = x[index0:]
m = [-i for i in x[:index0][::-1]]
Np = len(p)
Nm = len(m)

ans = 1000000000
if Np >= K:
  ans = min(ans, p[K-1])
if p[0] == 0:
  if K == 1:
    print(0)
    exit()
  if Nm >= K-1:
    ans = min(ans, m[K-2])
else:
  if Nm >= K:
    ans = min(ans, m[K-1])


for i in range(max(1, K-Nm), min(K, Np)):
  ans = min(ans, m[K-i-1] + 2*p[i-1])

for i in range(max(1, K-Np), min(K, Nm)):
  ans = min(ans, p[K-i-1] + 2*m[i-1])

print(ans)

Submission status: Runtime Error

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

for i in range(K):
    if i + K > N+1:
        break
    else:
        a = min(abs(x[K+i-1]),abs(x[i])) 
        ans = min(ans,x[K+i-1]-x[i]+a)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
candle=list(map(int,input().split()))
com=list(itertools.product(["1","0"], repeat=n))
ans=2000000000
for i in range(n-k):
  if candle[i]<0 and candle[i+k-1]>0:
    temp=min(abs(candle[i]), candle[i+k-1])*2+max(abs(candle[i]), candle[i+k-1])
  else:
    temp=abs(candle[i+k-1]-candle[i])
  ans=min(temp, ans)
print(ans)