Problem:


Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Wrong Answer

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

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    print(minus[k-1])
elif minus == []:
    print(plus[k-1])
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  <= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  >= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] <= minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] <= minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Wrong Answer

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

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    print(minus[k-1])
elif minus == []:
    print(plus[k-1])
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  <= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  >= k:
            print(len(minus))
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] <= minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] <= minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Wrong Answer

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

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    print(minus[k-1])
elif minus == []:
    print(plus[k-1])
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] <= minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] <= minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1]))

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = map(int,input().split())
x = list(map(int,input().split()))
min_t = None
if N==K:
  print(0)
else:
  for i in range(N-K+1):
    t = min(abs(x[i])+abs(x[i]-x[i+K-1]), abs(x[i+K-1])+abs(x[i]-x[i+K-1]))
    if min_t == None:
      min_t = t
    if min_t > t:
      min_t = t

  print(min_t)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = map(int,input().split())
x = list(map(int,input().split()))
min_t = 1e10
if N==K:
  print(0)
else:
  for i in range(N-K+1):
    t = min(abs(x[i])+abs(x[i]-x[i+K-1]), abs(x[i+K-1])+abs(x[i]-x[i+K-1]))
    if min_t > t:
      min_t = t

  print(min_t)

Submission status: Wrong Answer

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

R=[0]
L=[0]
for i in range(n):
    if x[i]>=0:
        R.append(x[i])
    else:
        L.append(-x[i])

R=sorted(R)
L=sorted(L)


lenR=len(R)
lenL=len(L)

if lenR>k:
    ans=R[k]
    for i in range(k):
        print(i)
        if i<lenL:
            ans=min(ans,R[k-i]+2*L[i], 2*R[k-i]+L[i])
        #print('i,ans,R[k-i]+2*L[i], 2*R[k-i]+L[i]=',i,ans,R[k-i]+2*L[i], 2*R[k-i]+L[i])
else:
    ans=R[lenR-1]+L[k-lenR-1]
    for i in range(lenR):
        if k-i<lenL:
            ans=min(ans,R[i]+2*L[k-i], 2*R[i]+L[k-i])

if lenL>k:
    ans=min(ans,L[k])
            
print(ans)

Submission status: Wrong Answer

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

R=[0]
L=[0]
for i in range(n):
    if x[i]>=0:
        R.append(x[i])
    else:
        L.append(-x[i])

R=sorted(R)
L=sorted(L)


lenR=len(R)
lenL=len(L)

if lenR>k:
    ans=R[k]
    for i in range(k):
        print(i)
        if i<lenL:
            ans=min(ans,R[k-i]+2*L[i], 2*R[k-i]+L[i])
        #print('i,ans,R[k-i]+2*L[i], 2*R[k-i]+L[i]=',i,ans,R[k-i]+2*L[i], 2*R[k-i]+L[i])
else:
    ans=R[lenR-1]+L[k-lenR-1]
    for i in range(lenR):
        if k-i<lenL:
            ans=min(ans,R[i]+2*L[k-i], 2*R[i]+L[k-i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
minus=0
plus=0
zero=0
for i in range(N):
    if x[i]<0:
        minus+=1
    elif x[i]>0:
        plus+=1
    else:
        zero+=1
ans=1000000000
if zero==1:
    N=N-1
    K=K-1
if K==0:
    ans=0
for i in range(N):
    if i<=minus-1:
        if K==minus-i:
            ans=min(ans,abs(x[i]))
        elif N-i>=K:
            ans=min(ans,abs(x[i])*2+x[K-(minus-i)+minus-1])
    else:
        if K==i-minus+1:
            ans=min(ans,x[i])
        elif i+1>=K:
            ans=min(ans,x[i]*2+abs(x[minus-1-(K-(i-minus+1))]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
 
kyori=2*10**8+1
for i in range(n-k+1):
#print(x[i:k+i])
  if x[k+i-1]<=0:
    kyori = min(kyori,abs(x[i]))
  elif x[i]<0 and 0<x[k+i-1]:
    kyori = min(kyori,abs(x[i]*2)+abs(x[k+i-1]),abs(x[i])+abs(x[k+i-1]*2))
  elif 0<=x[i]:
    kyori = min(kyori,abs(x[k+i-1]))
print(kyori)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
 
kyori=2*10**8+1
for i in range(n-k+1):
#print(x[i:k+i])
  if x[k+i-1]<=0:
    kyori = min(kyori,abs(x[i]))
  elif x[i]<=0 and 0<=x[k+i-1]:
    kyori = min(kyori,abs(x[i]*2)+abs(x[k+i-1]),abs(x[i])+abs(x[k+i-1]*2))
  elif 0<=x[i]:
    kyori = min(kyori,abs(x[k+i-1]))
print(kyori)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
minval = 10000000000
for i in range(N-K+1):
    tmpval = abs(X[i]) + (X[i+K-1] - X[i])
    if minval > tmpval:
        minval = tmpval
print(minval)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
minval = 1000000000
for i in range(N-K+1):
    tmpval = abs(X[i]) + (X[i+K-1] - X[i])
    if minval > tmpval:
        minval = tmpval
print(minval)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
min_space = 10**9
for i in range(N-K+1):
    min_space = min(x[i+K-1] - x[i]+abs(x[i]),min_space)
# for i in range(N-K):
#     if x[i+K-1] - x[i]==min_space:
#         min_x = x[i]

print(min_space)

Submission status: Wrong Answer

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

ans = 10 ** 5 * 2
for i in range(n - k + 1):
    L = A[i]
    R = A[i + k - 1]
    r1 = abs(L) - L + R
    r2 = abs(R) - L + R
    temp = min(r1, r2)
    ans = min(ans, temp)
    
print(ans)

Submission status: Wrong Answer

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

cost_min = 200000000

for i in range(N-K+1):
    if X[i] < 0 and X[i+K-1] < 0:
        cost_tmp = abs(X[i])
    elif X[i] > 0 and X[i+K-1] > 0:
        cost_tmp = X[i+K-1]
    else:
        cost_tmp = min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1]))
    if cost_tmp <= cost_min:
        cost_min = cost_tmp

print(cost_min)

Submission status: Wrong Answer

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

cost_min = 200000000

for i in range(N-K+1):
    cost_tmp = min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1]))
    if cost_tmp <= cost_min:
        cost_min = cost_tmp

print(cost_min)

Submission status: Wrong Answer

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

cost_min = 200000000

for i in range(N-K+1):
    cost_1 = abs(X[i]) + abs(X[i+K-1]-X[i])
    cost_2 = abs(X[i+K-1]) + abs(X[i]-X[i+K-1])
    if cost_1 <= cost_min:
        cost_min = cost_1
    if cost_2 <= cost_min:
        cost_min = cost_2

print(cost_min)

Submission status: Wrong Answer

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

cost_min = 200000000

for i in range(N-K+1):
    cost_1 = abs(X[i]) + abs(X[i+K-1]-X[i])
    cost_2 = abs(X[i+K-1]) + abs(X[i+K-1]-X[i])
    if cost_1 <= cost_min:
        cost_min = cost_1
    elif cost_2 <= cost_min:
        cost_min = cost_2

print(cost_min)

Submission status: Wrong Answer

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

for i in range(N - K + 1):
    ans = min(ans, min(abs(x[i]), abs(x[i+K-1])) * 2 + max(abs(x[i]), abs(x[i+K-1])))
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if x[i]>0:
        x[i] *= 2

    if y[i]<0:
        y[i] *= 2

x.sort(key=lambda z: abs(z))
y.sort(key=lambda z: abs(z))

x = x[:K]
y = y[:K]

x.append(0)
y.append(0)

c1 = max(x) - min(x)
c2 = max(y) - min(y)

print(min(c1,c2))

Submission status: Wrong Answer

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

y = list(map(abs, x))

x.sort(key=lambda x: abs(x))

M = max(x[:K])
m = min(x[:K])

print(M-m + min(map(abs,[M,m])))

Submission status: Wrong Answer

Solution code:
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,a0)
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,ab)
print(ans)

Submission status: Wrong Answer

Solution code:
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,ab)
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,a0)
print(ans)

Submission status: Wrong Answer

Solution code:
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,ab)
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,ab)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,-abs(a[i]+a[i+k-1]\
          +min(abs(a[i]),abs(a[i+k-1]))))
print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,abs(a[i]+a[i+k-1]\
          -min(abs(a[i]),abs(a[i+k-1]))))
print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,abs(a[i]+a[i+k-1]\
          -abs(min(a[i],a[i+k-1]))))
print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k+1):
    c=min(c,a[i]+a[i+k-1]\
          -abs(min(a[i],a[i+k-1])))
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=10**10
for i in range(n-k):
    c=min(c,a[i]+a[i+k])
print(c)

Submission status: Wrong Answer

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

import numpy as np
x_list = np.array(x_list).reshape(-1,1)

dis_list = []

if N <= K:
    print(int(x_list[-1]-x_list[0]))
    exit()



for i in range(N-K):
    target = x_list[i:i+K]
    if max(target) * min(target)<0:
        distance = min(max(target),abs(min(target))) * 2 + max(max(target),abs(min(target)))
        dis_list.append(distance)
    else:
        distance = max(max(target),abs(min(target)))
        dis_list.append(distance)

print(int(min(dis_list)))

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**15
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min( a[tmp]-a[i] + min(-a[i],a[tmp]),res)
        
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10**10
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min( a[tmp]-a[i] + min(-a[i],a[tmp]),res)
        
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10*100
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min( a[tmp]-a[i] + min(-a[i],a[tmp]),res)        

print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10*100
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min( a[tmp]-a[i] + min(abs(a[i]),abs(a[tmp])),res)
        
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10*100
 
for i in range(x):
    tmp = i + b[1] - 1
    res = min(abs(a[i]) + abs(a[tmp]) + min(abs(a[i]),abs(a[tmp])),res)

        
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10*100

for i in range(x):
    tmp = i + b[1] - 1
    if a[i] <= 0 and a[tmp] >= 0:
        res = min(abs(a[i]) + abs(a[tmp]) + min(abs(a[i]),abs(a[tmp])),res)
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]+1
res = 10*100

for i in range(x):
    tmp = i + b[1] - 1
    if a[i] <= 0 and a[tmp] >= 0:
        res = min(abs(a[i] - a[tmp]) + min(abs(a[i]),abs(a[tmp])),res)

print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]
res = 10*100

for i in range(x):
    tmp = i + b[1] - 1
    if a[i] <= 0 and a[tmp] >= 0:
        res = min(abs(a[i] - a[tmp]) + min(abs(a[i]),abs(a[tmp])),res)

print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]
res = 10*100

for i in range(x):
    tmp = i + b[1] - 1
    res = min(abs(a[i] - a[tmp]) + min(abs(0-a[i]),abs(0-a[tmp])),res)

print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
b = list(map(int, input().split()))
a = list(map(int, input().split()))
x = b[0]-b[1]
res = 10*11

for i in range(x):
    tmp = i + b[1] - 1
    res = min(abs(a[i] - a[tmp]) + min(abs(0-a[i]),abs(0-a[tmp])),res)

print(res)

Submission status: Wrong Answer

Solution code:
import copy
#横にn個入力
n, k = map(int, input().split())

#n...普通、a...横で入力された複数の要素を配列で取得
a = input().split()

x = [int(m) for m in a]
lx = []
mx = []
rx = []
for i in range(n):
    if x[i] < 0:
        lx.append(x[i])
    elif x[i] == 0:
        mx.append(x[i])
    else:
        rx.append(x[i])

lx.sort(reverse=True)
rx.sort()
lmx = lx + mx
lmx.sort(reverse=True)
ans1 = float("INF")
if len(lmx) >= k:
    ans1 = abs(lmx[k - 1])

mrx = mx + rx
mrx.sort()
ans2 = float("INF")
if len(mrx) >= k:
    ans2 = abs(mrx[k - 1])

p = 1
ans3 = float("INF")
while True:
    if len(rx) >= p and len(lx) >= k - p - len(mx) and k - p - len(mx) > 0:
        if ans3 > abs(2 * rx[p - 1]) + abs(lx[k - p - len(mx) - 1]):
            ans3 = abs(2 * rx[p - 1]) + abs(lx[k - p - len(mx) - 1])
        p += 1
    else:
        break
q = 1
ans4 = float("INF")
while True:
    if len(lx) >= q and len(rx) >= k - q - len(mx) and k - q - len(mx) > 0:
        if ans4 > abs(2 * lx[q - 1]) + abs(rx[k - q - len(mx) - 1]):
            ans4 = abs(2 * lx[q - 1]) + abs(rx[k - q - len(mx) - 1])
        q += 1
    else:
        break
print(min(ans1,ans2,ans3,ans4))

Submission status: Wrong Answer

Solution code:
import copy
#横にn個入力
n, k = map(int, input().split())

#n...普通、a...横で入力された複数の要素を配列で取得
a = input().split()

x = [int(m) for m in a]
lx = []
mx = []
rx = []
for i in range(n):
    if x[i] < 0:
        lx.append(x[i])
    elif x[i] == 0:
        mx.append(x[i])
    else:
        rx.append(x[i])

lx.sort(reverse=True)
rx.sort()
lmx = lx + mx
lmx.sort(reverse=True)
ans1 = float("INF")
if len(lmx) >= k:
    ans1 = abs(lmx[k - 1])

mrx = mx + rx
mrx.sort()
ans2 = float("INF")
if len(mrx) >= k:
    ans2 = abs(mrx[k - 1])

p = 1
ans3 = float("INF")
while True:
    if len(rx) >= p and len(lx) >= k - p - len(mx):
        if ans3 > abs(2 * rx[p - 1]) + abs(lx[k - p - len(mx) - 1]):
            ans3 = abs(2 * rx[p - 1]) + abs(lx[k - p - len(mx) - 1])
        p += 1
    else:
        break
q = 1
ans4 = float("INF")
while True:
    if len(lx) >= q and len(rx) >= k - q - len(mx):
        if ans4 > abs(2 * lx[q - 1]) + abs(rx[k - q - len(mx) - 1]):
            ans4 = abs(2 * lx[q - 1]) + abs(rx[k - q - len(mx) - 1])
        q += 1
    else:
        break
print(min(ans1,ans2,ans3,ans4))

Submission status: Wrong Answer

Solution code:
INF = 10**15
N,K = map(int,input().split())
x = list(map(int,input().split()))
num = 0
for i in range(N-1):
    if x[i] <= 0 and 0 < x[i+1]:
       num = i 

dist = INF
if N - 1 - num >= K:
    dist = x[num + K]
    
for i in range(num+1):
    if i + K < N and x[i] < 0 and dist > -x[i] + x[i+K-1] -x[i]:
        dist = x[i+K-1] - 2*x[i]
        

if num >= K and dist > -x[i-K-1]:
    dist  = -x[i-K-1]

for i in range(num+1,N):
    if i > K-1 and x[i] > 0 and dist > -x[i-K+1] + 2*x[i]:
        
        dist = -x[i-K+1] +2*x[i]
if N == 1:
    print(x[0])
else:
    print(dist)

Submission status: Wrong Answer

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

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

d = 10**10
for i in range(n-k):
    x_l = l[i]
    x_r = l[i+k-1]

    if x_l >= 0:
        tmp = x_r
    elif x_r <= 0:
        tmp = -x_l
    else:
        tmp = min(abs(x_r), abs(x_l)) + abs(x_l-x_r)

    d = min(d, tmp)

print(d)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
li = list(map(int,input().split()))
time = 10**5

for i in range(N-K+1):
    if li[i+K-1] <= 0:
        time = min(time,abs(li[i]))
    if li[i]<= 0 and li[i+K-1]>=0:
        time = min(min(abs(li[i]),abs(li[i+K-1]))+li[i+K-1]-li[i],time)
    if li[i]>=0:
        time = min(time,abs(li[i+K-1]))

print(time)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
li = list(map(int,input().split()))
time = 10**5

for i in range(N-K+1):
    if li[i+K-1] <= 0:
        time = min(time,abs(li[i]))
    if li[i]<= 0 and li[i+K-1]>=0:
        time = min(min(abs(li[i]),abs(li[i+K-1]))+li[i+K-1]-li[i],time)
    if li[i]>=0:
        time = min(time,abs(li[i+K-1]))

print(time)

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
br = []
m = 0 
p = 0
for r in ar:
    if r >= 0:
        p += 1
    else:
        m += 1
if m == 0:
    print(ar[b-1])
elif p == 0:
    print(ar[len(ar)-b])
else:
    for i in range(a-b+1):
        l = ar[i:i+b]
        br.append(min(abs(0-l[0])*2+abs(0-l[b-1]),abs(0-l[0])+abs(0-l[b-1])*2))
    print(min(br))

Submission status: Wrong Answer

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

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

if 0 in x:
  K -= 1
  x.remove(0)
if K <= 0:
  print(0)
  exit()
if K == 1:
  print(min(abs(i) for i in x))
  exit()
  
px = [i for i in x if i > 0]
mx = [i for i in x if i < 0]


  
lp = len(px)
lm = len(mx)

mn = (abs(x[0]) + abs(x[len(x)-1])) * 2
if lp >= K:
  t = px[K-1]
  if t < mn:
    mn = t
if lm >= K:
  t = mx[-K]
  if t < mn:
    mn = t
for i in range(max(lm-K+1, 0), min(lm,len(x)-K+1)):
  tm = -x[i]
  tp = x[i+K-1]
  t = min(tm, tp) * 2 + max(tm, tp)
  if t < mn:
    mn = t
  
print(mn)

Submission status: Wrong Answer

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

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

if 0 in x:
  K -= 1
if K <= 0:
  print(0)
  exit()

px = [i for i in x if i > 0]
mx = [i for i in x if i < 0]

if K == 1:
  print(min(px[0], abs(mx[-1])))
  exit()
  
lp = len(px)
lm = len(mx)

mn = (abs(x[0]) + abs(x[len(x)-1])) * 2
if lp >= K:
  t = px[K-1]
  if t < mn:
    mn = t
if lm >= K:
  t = mx[-K]
  if t < mn:
    mn = t
for i in range(max(lm-K+1, 0), min(lm,len(x)-K+1)):
  tm = -x[i]
  tp = x[i+K-1]
  t = min(tm, tp) * 2 + max(tm, tp)
  if t < mn:
    mn = t
  
print(mn)

Submission status: Wrong Answer

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

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

if 0 in x:
  K -= 1
if K <= 0:
  print(0)
  sys.exit()
  
px = [i for i in x if i > 0]
mx = [i*-1 for i in reversed(x) if i < 0]

for i in range(len(px)-1, 0, -1):
  px[i] = px[i] - px[i-1]
for i in range(len(mx)-1, 0, -1):
  mx[i] = mx[i] - mx[i-1]  

K -= 1

if len(px) == 0:
  ps = float('inf')
else:
  ps = px[0]
  pc = 1
  mc = 0
  for i in range(K):
    if mc >= len(mx):
      ps += px[pc]
      pc += 1
    elif pc >= len(px):
      ps += mx[mc] * 2
      mc += 1
    elif px[pc] < mx[mc] * 2:
      ps += px[pc]
      pc += 1
    else:
      ps += mx[mc] * 2
      mc += 1

if len(mx) == 0:
  ms = float('inf')
else:
  ms = mx[0]
  mc = 1
  pc = 0
  for i in range(K):
    if pc >= len(px):
      ms += mx[mc]
      mc += 1
    elif mc >= len(mx):
      ms += px[pc] * 2
      pc += 1
    elif mx[mc] < px[pc] * 2:
      ms += mx[mc]
      mc += 1
    else:
      ms += px[pc] * 2
      pc += 1

print(min(ps, ms))

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
X=list(map(int,input().split()))
dis=10**9
X.append(0)
X.sort()

if len(X)==2:
  print(0)
  exit()

for i in range(n-k):
  tmp=0
  for j in range(i,i+k):
    if j+1<n:
      if np.sign(X[j+1])== np.sign(X[j]):
        tmp += abs(X[j+1]-X[j])
      else:
        tmp += abs(X[j+1]-X[j])
        tmp += abs(X[j])
  dis=min(dis,tmp)
print(dis)

Submission status: Wrong Answer

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

x = list(map(int,input().split()))
#x = [ abs(int(i))  for i in input().split()]
x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)

for i in range(zeroIdx-k,zeroIdx+1):
    if i < 0 : 
        continue
    elif len(x) <= i + k :
        break

    plsIdx = i + k 
    misIdx = i

    #print(misIdx,plsIdx,x[misIdx],x[plsIdx])

    if abs(x[plsIdx]) > abs(x[misIdx]) :
        tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
    else:
        tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])

    if tmp < min :
        min = tmp

print(min)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
if min(X) >= 0:
    ans = X[K-1]
    print(ans)
    exit()
elif max(X) <= 0:
    ans = -X[-K]
    print(ans)
    exit()  
# 0から連続するK個のロウソクを全列挙　その中で最小の値を探索
n_max = -10**8
p_min = 10**8
for e in X:
    if e > 0 and e < p_min:
        p_min = e
    elif e < 0 and e > n_max:
        n_max = e
min_time = 10**14
for i in range(N-K):
    part = X[i:i+K]
    if part.count(p_min) * part.count(n_max) >= 1:
        for j in range(K):
            if part[j] > 0:
                note = j
                break
        n_part = part[:note]
        p_part = part[note:]
        time = 2*min(-n_part[0],p_part[-1]) + max(-n_part[0],p_part[-1])
        if time < min_time:
            min_time = time
print(min_time)

Submission status: Wrong Answer

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

def dist(i):
    a = abs(X[K - 1 + i]) + X[K - 1 + i] - X[i]
    b = abs(X[i]) + X[K - 1 + i] - X[i]
    return min(a, b)

ans = dist(0)
for i in range(N):
    if i + K >= N:
        break
    ans = min(ans, dist(i))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
mini = 1000000000
for i in range(N - K):
    if min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i]) < mini:
        mini = min(abs(x[i]), abs(x[i + K - 1])) + abs(x[i + K - 1 ] - x[i])

print(mini)

Submission status: Wrong Answer

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

ml = len(xs) - 1
center = ml
for i in range(center + 1):
    if xs[i] >= 0:
        if xs[i] == 0:
            center = i
            break
        center = i - 1
        break

t = 1000000000
cur = center

while cur > 0:
    if ml - cur + 1 >= k:
        # left
        d = abs(xs[cur] - xs[cur + k - 1])
        tt = abs(xs[cur]) + d
        if tt <= t:
            t = tt
        # right
        tt = abs(xs[cur + k - 1]) + d
        if tt <= t:
            t = tt
    cur -= 1

# only left
if center >= k:
    tt = 0
    tt += abs(xs[center-k] - xs[center])
    if tt <= t:
        t = tt

# only right
if ml - center >= k:
    tt = 0
    tt += abs(xs[center] - xs[center+k])
    if tt <= t:
        t = tt

if t == 1000000000:
    print(0)
    exit(0)

print(t)

Submission status: Wrong Answer

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

ml = len(xs) - 1
center = ml
for i in range(center + 1):
    if xs[i] >= 0:
        if xs[i] == 0:
            center = i
            break
        center = i - 1
        break

t = 1000000000
cur = center
# left
while cur > 0:
    if ml - cur + 1 >= k:
        tt = abs(xs[cur])
        tt += abs(xs[cur] - xs[cur + k - 1])
        if tt <= t:
            t = tt
    cur -= 1

# only left
if center >= k:
    tt = 0
    tt += abs(xs[center-k] - xs[center])
    if tt <= t:
        t = tt

# only right
if ml - center >= k:
    tt = 0
    tt += abs(xs[center] - xs[center+k])
    if tt <= t:
        t = tt

if t == 1000000000:
    print(0)
    exit(0)

print(t)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]

ll=len(l)
rr=len(r)
lx=sum(l)
rx=sum(r)

if ll<k: lx=lx*2+sum(r[:(k-ll)])
if rr<k: rx=rx*2+sum(l[:(k-rr)])
ans=min(lx,rx)
#print(ans)
o=[ll,[0,k-ll][ll<k]]
p=[rr,[0,k-rr][rr<k]]

"""
-30 -10 10 20 50
print(o)
print(p)
[2, 1]
[3, 0]
"""

for i in range(1,k):
    if o[1]==0: lx*=2
    #if r[1]==0: rx*=2
    if o[1]+1<=rr and o[0]>0:
        lx-=l[o[0]-1]*2
        lx+=r[o[1]]
        o[0]-=1
        o[1]+=1
        ans=min(ans,lx)
    #if p[1]+1<=ll and p[0]>0:
    #    rx-=r[p[0]-1]*2
    #    rx+=l[p[1]]
    #    p[0]-=1
    #    p[1]+=1
    #    ans=min(ans,rx)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]

ll=len(l)
rr=len(r)
lx=sum(l)
rx=sum(r)

if ll<k: lx=lx*2+sum(r[:(k-ll)])
if rr<k: rx=rx*2+sum(l[:(k-rr)])
ans=min(lx,rx)
print(ans)
o=[ll,[0,k-ll][ll<k]]
p=[rr,[0,k-rr][rr<k]]

"""
-30 -10 10 20 50
print(o)
print(p)
[2, 1]
[3, 0]
"""

for i in range(1,k):
    if o[1]==0: lx*=2
    #if r[1]==0: rx*=2
    if o[1]+1<=rr and o[0]>0:
        lx-=l[o[0]-1]*2
        lx+=r[o[1]]
        o[0]-=1
        o[1]+=1
        ans=min(ans,lx)
    #if p[1]+1<=ll and p[0]>0:
    #    rx-=r[p[0]-1]*2
    #    rx+=l[p[1]]
    #    p[0]-=1
    #    p[1]+=1
    #    ans=min(ans,rx)
print(ans)