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 = list(map(int, input().split()))
ans = abs(x[k-1]) + 2*abs(x[0])
for i in range (n-k+1):
  if x[i+k-1] <= 0:
    ans = min(ans, -x[i])
  elif x[i] >= 0:
    ans = min(ans, x[i+k-1])
  else:
    ans = min(ans, -2*x[i] + x[i+k-1])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k, *x = map(int, open(0).read().split())
ans = 10**18
neg = []
pos = [0]
for i in x:
    if i < 0:
        neg.append(i)
    else:
        pos.append(i)
neg.append(0)
k += 1
c = len(neg)
c -= k -min(k, len(pos))
k = min(k, len(pos))
while c > 0 and k > 1:
    c -= 1
    k -= 1
    ans = min(ans, -neg[c] * 2 + pos[k], -neg[c] + pos[k] * 2)
print(ans)

Submission status: Wrong Answer

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

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k+1):
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))

if x[0]>0:mt=min(mt,x[k-1])
elif x[-1]<0:mt=min(mt,abs(x[-1-k+1]))
print(mt)

Submission status: Wrong Answer

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

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k+1):
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))
  if i+k-1 ==c-1:mt=min(mt,abs(x[i]))
  if i==c:mt=min(mt,abs(x[i+k-1]))

print(mt)

Submission status: Wrong Answer

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

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k+1):
  if i+k<c:continue
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))
  if i+k-1 ==c-1:mt=min(mt,abs(x[i]))
  if i==c:mt=min(mt,x[i+k-1])

print(mt)

Submission status: Wrong Answer

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

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k):
  if i+k<c:continue
    
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))
  if i+k-1 ==c-1:mt=min(mt,abs(x[i]))
  if i==c:mt=min(mt,x[i+k-1])
if n==k:
  if c!=n and c!=0:
    mt=min(2*abs(x[0])+x[-1],2*x[-1]+abs(x[0]))
  else:
    mt=max(abs(x[0]),abs(x[-1]))
print(mt)

Submission status: Wrong Answer

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

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k):
  if i+k<c:continue
    
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))
  if i+k-1 ==c-1:mt=min(mt,abs(x[i]))
  if i==c:mt=min(mt,x[i+k-1])
if n==k:
  if c!=n and c!=0:
    mt=min(mt,2*abs(x[0])+x[-1],2*x[-1]+abs(x[0]))
  else:
    mt=min(mt,max(abs(x[0]),abs(x[-1])))
print(mt)

Submission status: Wrong Answer

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

ans=10**10

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

    if left*right>=0:
        ans=min(ans,max(abs(left),abs(right)))
    else:
        ans=right-left+min(abs(right),abs(left))

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[26]:


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


# In[33]:


ans = 10**8
for i in range(N-K+1):
    tmp = x[i:i+K]
    ans = min(ans, abs(tmp[0])+(tmp[K-1]-tmp[0]),abs(tmp[K-1])+(tmp[K-1]-tmp[0]))
print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[26]:


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


# In[33]:


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


# In[ ]:

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if len(lis) == 1:
    print(abs(lis[0]))
    exit()

ans = 10**9+7
for i in range(n-k):
    l = abs(lis[i]) + abs(lis[i+k-1]-lis[i])
    r = abs(lis[i+k-1]) + abs(lis[i+k-1]-lis[i])
    ans = min(ans, l, r)

print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10**18

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

xp = [0] + [INF]*(k)
xm = [0] + [INF]*(k)
cp, cm = 1, 1
for i in range(n):
    if x[i] >= 0 and cp < k+1:
        xp[cp] = x[i]
        cp += 1
    elif x[i] < 0 and cm < k+1:
        xm[cm] = -x[i]
        cm += 1
xm.sort(reverse=True)
ans = INF * 5
for i in range(k+1):
    d = xp[i] + 2 * xm[i]
    ans = min(ans, d)
    d = 2 * xp[i] + xm[i]
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

INF = 10**18

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

xp = [0] + [INF]*(k)
xm = [0] + [INF]*(k)
cp, cm = 1, 1
for i in range(n):
    if x[i] > 0 and cp < k+1:
        xp[cp] = x[i]
        cp += 1
    elif x[i] < 0 and cm < k+1:
        xm[cm] = -x[i]
        cm += 1
xm.sort(reverse=True)
ans = INF * 3
for i in range(k+1):
    d = xp[i] + 2 * xm[i]
    ans = min(ans, d)
    d = 2 * xp[i] + xm[i]
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10**18

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

xp = [0] + [INF]*(k)
xm = [0] + [INF]*(k)
cp, cm = 1, 1
for i in range(n):
    if x[i] >= 0 and cp < k+1:
        xp[cp] = x[i]
        cp += 1
    elif x[i] < 0 and cm < k+1:
        xm[cm] = -x[i]
        cm += 1
xm.sort(reverse=True)
ans = INF * 3
for i in range(k+1):
    d = xp[i] + 2 * xm[i]
    ans = min(ans, d)
    d = 2 * xp[i] + xm[i]
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

INF = 10**9

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

xp = [0] + [INF]*(k)
xm = [0] + [INF]*(k)
cp, cm = 1, 1
for i in range(n):
    if x[i] >= 0 and cp < k+1:
        xp[cp] = x[i]
        cp += 1
    elif x[i] < 0 and cm < k+1:
        xm[cm] = -x[i]
        cm += 1
xm.sort(reverse=True)

ans = INF * 3
for i in range(k+1):
    d = xp[i] + 2 * xm[i]
    ans = min(ans, d)
    d = 2 * xp[i] + xm[i]
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

INF = 10**9

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

xp = [0] + [INF]*(k)
xm = [0] + [INF]*(k)
cp, cm = 1, 1
for i in range(n):
    if x[i] >= 0 and cp < k+1:
        xp[cp] = x[i]
        cp += 1
    elif x[i] < 0 and cm < k+1:
        xm[cm] = -x[i]
        cm += 1
xm.sort(reverse=True)
print(xp, xm)
ans = INF * 3
for i in range(k+1):
    d = xp[i] + 2 * xm[i]
    ans = min(ans, d)
    d = 2 * xp[i] + xm[i]
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

INF = 10**9

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

x = np.array(x)
xp = np.hstack([np.zeros(1, dtype=int), x[x >= 0]])
xm = np.hstack([x[x < 0], np.zeros(1, dtype=int)])
xm = xm[::-1] * -1

if len(xp) >= k+1:
    xp = xp[:k+2]
else:
    yy = np.array([INF] * (k+1 - len(xp)))
    xp = np.hstack([xp, yy])
if len(xm) >= k+1:
    xm = xm[:k+2]
else:
    yy = np.array([INF] * (k+1 - len(xm)))
    xm = np.hstack([xm, yy])
xm = xm[::-1]

ans = INF * 3
for i in range(k+1):
    d = xp[i] + 2 * xm[i]
    ans = min(ans, d)
    d = 2 * xp[i] + xm[i]
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

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

# R = [x[i] for i in range(N) if x[i] >= 0]
# L = [abs(x[i]) for i in range(N-1, -1, -1) if x[i] < 0]

# len_R = len(R)
# len_L = N - len_R
# r = 0
# l = 0
# time = 1e10

# dp_r = [0] * (len_R + 1) #右サイドでi個選んだ時のコストdp[i]
# dp_l = [0] * (len_L + 1)

# for i in range(1, N-1):
#     dp_r[i] = min(dp_r[i-1] + R[i])
def cost(i):
    l = i
    r = i+K
    if x[l] >= 0:
        return abs(x[r-1])
    elif x[r-1] <= 0:
        return abs(x[l])
    else:
        if abs(x[l]) < abs(x[r-1]):
            return 2*abs(x[l]) + abs(x[r-1])
        else:
            return 2*abs(x[r-1]) + abs(x[l])
ans = 1e12
for i in range(0, N-K+1):
    ans = min(ans, cost(i))
if N==K:
    ans = abs(x[0])
print(ans)

Submission status: Wrong Answer

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

# R = [x[i] for i in range(N) if x[i] >= 0]
# L = [abs(x[i]) for i in range(N-1, -1, -1) if x[i] < 0]

# len_R = len(R)
# len_L = N - len_R
# r = 0
# l = 0
# time = 1e10

# dp_r = [0] * (len_R + 1) #右サイドでi個選んだ時のコストdp[i]
# dp_l = [0] * (len_L + 1)

# for i in range(1, N-1):
#     dp_r[i] = min(dp_r[i-1] + R[i])
def cost(i):
    l = i
    r = i+K
    if x[l] >= 0:
        return x[r-1]
    elif x[r-1] <= 0:
        return abs(x[l])
    else:
        if abs(x[l]) < abs(x[r-1]):
            return 2*abs(x[l]) + abs(x[r-1])
        else:
            return 2*abs(x[r-1]) + abs(x[l])
ans = 1e12
for i in range(0, N-K):
    ans = min(ans, cost(i))
if N==K:
    ans = abs(x[0])
print(ans)

Submission status: Wrong Answer

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

# R = [x[i] for i in range(N) if x[i] >= 0]
# L = [abs(x[i]) for i in range(N-1, -1, -1) if x[i] < 0]

# len_R = len(R)
# len_L = N - len_R
# r = 0
# l = 0
# time = 1e10

# dp_r = [0] * (len_R + 1) #右サイドでi個選んだ時のコストdp[i]
# dp_l = [0] * (len_L + 1)

# for i in range(1, N-1):
#     dp_r[i] = min(dp_r[i-1] + R[i])
def cost(i):
    l = i
    r = i+K
    if x[l] >= 0:
        return x[r-1]
    elif x[r-1] <= 0:
        return abs(x[l])
    else:
        if abs(x[l]) < abs(x[r-1]):
            return 2*abs(x[l]) + abs(x[r-1])
        else:
            return 2*abs(x[r-1]) + abs(x[l])
ans = 1e10
for i in range(0, N-K):
    ans = min(ans, cost(i))
if N==K:
    ans = abs(x[0])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
right = n-1
left = 0
while right - left + 1 > k:
    if abs(l[right]) > abs(l[left]):
        right-=1
    else:
        left+=1
l = l[left:right+1]
ans1 = abs(l[0]) + abs(l[-1] - l[0])
ans2 = abs(l[-1]) + abs(l[-1] - l[0])
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
right = n-1
left = 0
while right - left + 1 > k:
    if abs(l[right]) > abs(l[left]):
        right-=1
    else:
        left+=1
l = l[left:right+1]
if len(l) == 1:
    print(abs(l[0]))
    exit()
l[0] = abs(min(0, l[0]))
l[-1] = abs(max(0, l[-1]))
print(2*min(l[0], l[-1]) + max(l[0], l[-1]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
right = n-1
left = 0
while right - left + 1 > k:
    if abs(l[right]) > abs(l[left]):
        right-=1
    else:
        left+=1
l = l[left:right+1]
l[0] = abs(min(0, l[0]))
l[-1] = abs(max(0, l[-1]))
print(2*min(l[0], l[-1]) + max(l[0], l[-1]))

Submission status: Wrong Answer

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(max(k-len(l)+1,1),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: Wrong Answer

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(max(k-len(l),1),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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = [0] * (n + 1)
for i in range(1, len(x)):
  a[i] = x[i] - x[i - 1]

c = [0] * (n + 1)
for i in range(k):
  c[0] += a[i]

for i in range(1, n-k+1):
  c[i] = c[i - 1] - a[i] + a[i+k-1]

c2 = [c[i] + abs(x[i]) for i in range(len(x)-k+1)]

print(min(c2))

Submission status: Wrong Answer

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

inf = 10**12
z = len(x)
for i in range(len(x)):
    if x[i] >= 0:
        z = i
        break

r = [inf] * (n - z)
l = [inf] * z

for i in range(n-z):
    r[i] = x[z+i]

for i in range(z):
    l[i] = abs(x[z-i-1])

# 左からすべて取る場合
tl = inf
if len(l) < k:
    tl = inf
else:
    tl = abs(l[k-1])

if z == n-1:
    print(tl)
    exit()

# 右からすべて取る場合
tr = inf
if len(r) < k:
    tr = inf
else:
    tr = abs(r[k-1])
    
# 両方から取る場合
# 右からi個取る
t_arr = []
for i in range(1,k):
    if i-1 >= len(r):
        pass
    else:
        if k-i-1 >= len(l):
            pass
        else:
            t1 = r[i-1]
            t2 = l[k-i-1]
            t_arr.append(t1*2 + t2*2 - max(t1,t2))
tc = inf
if len(t_arr) != 0:
    tc = min(t_arr)
else:
    tc = inf
print(min(tl,tr,tc))

Submission status: Wrong Answer

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

inf = 10**12
z = len(x) - 1
for i in range(len(x)):
    if x[i] >= 0:
        z = i
        break

r = [inf] * (n - z)
l = [inf] * z

for i in range(n-z):
    r[i] = x[z+i]

for i in range(z):
    l[i] = abs(x[z-i-1])

# 左からすべて取る場合
tl = inf
if len(l) < k:
    tl = inf
else:
    tl = l[k-1]

# 右からすべて取る場合
tr = inf
if len(r) < k:
    tr = inf
else:
    tr = r[k-1]    

# 両方から取る場合
# 右からi個取る
t_arr = []
for i in range(1,k):
    if i-1 >= len(r):
        pass
    else:
        if k-i-1 >= len(l):
            pass
        else:
            t1 = r[i-1]
            t2 = l[k-i-1]
            t_arr.append(t1*2 + t2*2 - max(t1,t2))
tc = inf
if len(t_arr) != 0:
    tc = min(t_arr)
else:
    tc = inf
print(min(abs(tl),tr,tc))

Submission status: Wrong Answer

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

inf = 10**12
z = 0
for i in range(len(x)):
    if x[i] >= 0:
        z = i
        break

r = [inf] * (n - z)
l = [inf] * z
for i in range(n-z):
    r[i] = x[z+i]
for i in range(z):
    l[i] = abs(x[z-i-1])

# 左からすべて取る場合
tl = inf
if len(l) < k:
    tl = inf
else:
    tl = l[k-1]

# 右からすべて取る場合
tr = inf
if len(r) < k:
    tr = inf
else:
    tr = r[k-1]    

# 両方から取る場合
# 右からi個取る
t_arr = []
for i in range(1,k):
    if i-1 >= len(r):
        pass
    else:
        if k-i-1 >= len(l):
            pass
        else:
            t1 = r[i-1]
            t2 = l[k-i-1]
            t_arr.append(t1*2 + t2*2 - max(t1,t2))
tc = inf
if len(t_arr) != 0:
    tc = min(t_arr)
else:
    tc = inf
print(min(tl,tr,tc))

Submission status: Wrong Answer

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

inf = 10**12
z = 0
for i in range(len(x)):
    if x[i] >= 0:
        z = i
        break

r = [inf] * (n - z)
l = [inf] * z
for i in range(n-z):
    r[i] = x[z+i]
for i in range(z):
    l[i] = abs(x[z-i-1])

# 左からすべて取る場合
tl = 0
if len(l) < k:
    tl = inf
else:
    tl = l[k-1]

# 右からすべて取る場合
tr = 0
if len(r) < k:
    tr = inf
else:
    tr = r[k-1]    

# 両方から取る場合
# 右からi個取る
t_arr = []
for i in range(1,k):
    if i-1 >= len(r):
        pass
    else:
        if k-i-1 >= len(l):
            pass
        else:
            t1 = r[i-1]
            t2 = l[k-i-1]
            t_arr.append(t1*2 + t2*2 - max(t1,t2))
tc = inf
if len(t_arr) != 0:
    tc = min(t_arr)
else:
    tc = inf
print(min(tl,tr,tc))

Submission status: Wrong Answer

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

inf = 10**12
z = 0
for i in range(len(x)):
    if x[i] >= 0:
        z = i
        break

r = [inf] * (n - z)
l = [inf] * z
for i in range(n-z):
    r[i] = x[z+i]
for i in range(z):
    l[i] = abs(x[z-i-1])

# 左からすべて取る場合
tl = 0
if len(l) < k:
    tl = inf
else:
    tl = l[k-1]

# 右からすべて取る場合
tr = 0
if len(r) < k:
    tr = inf
else:
    tr = r[k-1]    

# 両方から取る場合
# 右からi個取る
t_arr = []
for i in range(1,k):
    if i >= len(r):
        pass
    else:
        if k-i >= len(l):
            pass
        else:
            t1 = r[i-1]
            t2 = l[k-i-1]
            t_arr.append(t1*2 + t2*2 - max(t1,t2))
tc = inf
if len(t_arr) != 0:
    tc = min(t_arr)
else:
    tc = inf
print(min(tl,tr,tc))

Submission status: Wrong Answer

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

ans =[]
if arr[0]>0:
    ans.append(arr[K-1])

if arr[-1]<0:
    ans.append(arr[-1*(K)])

for i in range(N-K+1):
    if arr[i]*arr[i+K-1]<=0:
        left =2*abs(arr[i])+abs(arr[i+K-1])
        right=2*abs(arr[i+K-1])+abs(arr[i])
        ans.append(min(left,right))

print(min(ans))

Submission status: Wrong Answer

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

res=[]
for i in range(N-K+1):
  tmp = abs(x[i])+x[i+K-1]-x[i]
  res.append(tmp)
  
for i in range(N-K+1):
  tmp = x[-i-1]+x[-i-1]-x[-i-K]
  res.append(tmp)
  
print(min(res))

Submission status: Wrong Answer

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

res=[]
for i in range(N-K+1):
  tmp = abs(x[i])+x[i+K-1]-x[i]
  res.append(tmp)
  
print(min(res))

Submission status: Wrong Answer

Solution code:
n, k=map(int, input().split())
l=list(map(int, input().split()))
ans=l[-1]-l[0]
for i in range(n-k+1):
  #0を含まない場合
  if l[i+k-1]<=0:
    ans=min(ans, 0-l[i])
  elif l[i]>=0:
    ans=min(ans, l[i+k-1]-0)
  else:
    ans=min(ans, l[i+k-1]*2-l[i], l[i+k-1]-2*l[i])
print(ans)
#0を含む場合
  #lを超える場合

Submission status: Wrong Answer

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

total = 100000000

for i in range(N-K+1):
    x_part = x[i:i+K]
    x_min = x_part[0]
    x_max = x_part[-1]
    if (x_min<0)&(x_max>0):
        total = min(total,abs(x_min)*2+abs(x_max))
    elif (x_min<=0) & (x_max<=0):
        total = min(total,abs(x_min))
    else:
        total = min(total,abs(x_max))

print(total)

Submission status: Wrong Answer

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

total = 100000000

for i in range(N-K+1):
    x_part = x[i:i+K]
    x_min = min(x_part)
    x_max = max(x_part)
    if (x_min<0)&(x_max>0):
        total = min(total,abs(x_min)*2+abs(x_max))
    elif (x_min<=0) & (x_max<=0):
        total = min(total,abs(x_min))
    else:
        total = min(total,abs(x_max))

print(total)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

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

	ans = float("inf")

	for i in range(k):
		if i+k > n:
			break

		left = A[i]
		right = A[i+k-1]
		
		tmp2 = 0
		tmp3 = 0
		
		#1 abs(left) < abs(right) の場合
		tmp2 = abs(left) + abs(right - left)

		#2 abs(right) > abs(left)の場合
		tmp3 = abs(right) + abs(left - right)

		ans = min(ans,min(tmp2,tmp3))
	print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import math

N,K = map(int, input().split())
X = list(map(int, input().split()))
a = bisect.bisect_left(X,0)
X.insert(a,0)

ans = math.inf

for i in range(a+K-N+1, min(a+1,K+1)):
    ans = min(ans, -X[a-i]*2+X[a+K-i])

for j in range(K-a, min(N-a+1,K+1)):
    ans = min(ans, X[a+j]*2-X[a-K+j])

print(ans)

Submission status: Wrong Answer

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())

pos = [0]
neg = [0]

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

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

print(ans)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
if __name__ == '__main__':

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

#	ans = 100000001
	ans = float("inf")

	for i in range(k):
		if i+k > n:
			break

		left = A[i]
		right = A[i+k-1]
		
		tmp = abs(left) + abs(right)
		tmp2 = 0
		if tmp >= 0:
			#1 abs(left) < abs(right) の場合
			tmp2 = abs(left) + abs(right - left)
		else:
			#2 abs(right) > abs(left)の場合
			tmp2 = abs(right) + abs(left - right)

		ans = min(ans,tmp2)
	print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

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

	ans = 100000001
	for i in range(k):
		if i+k > n:
			break

		left = A[i]
		right = A[i+k-1]
		
		tmp = abs(left) + abs(right)
		tmp2 = 0
		if tmp >= 0:
			#1 abs(left) < abs(right) の場合
			tmp2 = abs(left) + abs(right - left)
		else:
			#2 abs(right) > abs(left)の場合
			tmp2 = abs(right) + abs(left - right)

		ans = min(ans,tmp2)
	print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 10**18
    for i in range(n-k+1):
        if a[i] < 0 and a[i+k-1] < 0:
            ans = min(ans, a[i])
        elif a[i] < 0 and a[i+k-1] >= 0:
            ans = min([ans, 2*abs(a[i])+a[i+k-1], 2*a[i+k-1]-a[i]])
        else:
            ans = min(ans, a[i+k-1])
    print(ans)
        

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 10**12
    for i in range(n-k+1):
        if a[i] < 0 and a[i+k-1] < 0:
            ans = min(ans, a[i])
        elif a[i] < 0 and a[i+k-1] >= 0:
            ans = min([ans, 2*abs(a[i])+a[i+k-1], 2*a[i+k-1]-a[i]])
        else:
            ans = min(ans, a[i+k-1])
    print(ans)
        

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
ans=10**7
for i in range(n-k+1):
    sm=x[i]
    sp=x[i+k-1]
    if sp<0:
        ans=min(-sm,ans)
    elif 0<sm:
        ans=min(ans,sp)
    else:
        ans=min(ans,min(sm*-2+sp,sp*2-sm))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
ans=10**6
for i in range(n-k+1):
    sm=x[i]
    sp=x[i+k-1]
    if sp<0:
        ans=min(-sm,ans)
    elif 0<sm:
        ans=min(ans,sp)
    else:
        ans=min(ans,min(sm*-2+sp,sp*2-sm))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k,*x=map(int,open(0).read().split())
ans=float('inf')
if 0 in x:
  k-=1
  n-=1
  del x[x.index(0)]

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

Submission status: Wrong Answer

Solution code:
n,k,*x=map(int,open(0).read().split())
ans=float('inf')
if 0 in x:
  k-=1
  n-=1
  del x[x.index(0)]

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

Submission status: Wrong Answer

Solution code:
n,k,*x=map(int,open(0).read().split())
ans=float('inf')
if 0 in x:
  k-=1
  del x[x.index(0)]
if k==0:
  print(0)
else:
  for i in range(n-k):
    ans=min(ans,abs(x[i])+abs(x[i]-x[i+k-1]))
  for i in range(k,n):
    ans=min(ans,abs(x[i])+abs(x[i]-x[i-(k-1)]))
  print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k,*x=map(int,open(0).read().split())
inf=float('inf')
k-=x.count(0)
x=np.array(x)
pos=list(x[x>0])
neg=list((x[x<0]*-1)[::-1])
x=[]
ans=inf
sum_p=pos[:min(len(pos),k)]
sum_n=neg[:min(len(neg),k)]
pos.clear()
neg.clear()
if len(sum_p)==k and k!=0:
  ans=sum_p[-1]
if len(sum_n)==k and k!=0:
  ans=min(ans,sum_n[-1])
for i,p in enumerate(sum_p):
  if i+1>len(sum_n):
    break
  ans=min(ans,p*2+sum_n[-i-1])
  ans=min(ans,sum_n[-i-1]*2+p)
if k==0:
  ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k,*x=map(int,open(0).read().split())
inf=float('inf')
k-=x.count(0)
x=np.array(x)
pos=list(x[x>0])
neg=list((x[x<0]*-1)[::-1])
x=[]
ans=inf
sum_p=pos[:min(len(pos),k)]
sum_n=neg[:min(len(neg),k)]
pos.clear()
neg.clear()
p_inf,n_inf=k-len(sum_p),k-len(sum_n)
sum_p=sum_p+[inf for i in range(k-len(sum_p))]
sum_n=sum_n+[inf for i in range(k-len(sum_n))]
for i in range(k):
  if k-1-i-n_inf < 0:
    continue
  ans=min(ans,sum_p[i]*2+sum_n[k-1-i-n_inf])
for i in range(k):
  if k-1-i-p_inf < 0:
    continue
  ans=min(ans,sum_n[i]*2+sum_p[k-1-i-p_inf])
if k==0:
  ans=0
print(ans)

Submission status: Wrong Answer

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

ls_posi = []#0を含む
ls_nega = []

for i in range(n):
    if x[i] < 0:
        ls_nega.append(-x[i])
    else:
        ls_posi.append(x[i])

ls_nega.reverse()
#print(ls_nega)
#print(ls_posi)
ans = 10 ** 20

for i in range(k+1):
    #posiから0〜k個, negaからk-posi個
    if i == 0:
        if len(ls_nega) >= k:
            ans = min(ans,ls_nega[k-1])
    elif i == k:
        if len(ls_posi) >= k:
            ans = min(ans,ls_posi[k-1])
    else:
        if len(ls_posi)-1 >= i and len(ls_nega)-1 >= k - i:
            #print(i,ls_posi[i],ls_nega[k-i])
            ans = min(ans,ls_posi[i-1] * 2 + ls_nega[k-i-1])
            ans = min(ans,ls_posi[i-1] + ls_nega[k-i-1] * 2)
print(ans)

Submission status: Wrong Answer

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

ls_posi = []#0を含む
ls_nega = []

for i in range(n):
    if x[i] < 0:
        ls_nega.append(-x[i])
    else:
        ls_posi.append(x[i])

ls_nega.reverse()
#print(ls_nega)
#print(ls_posi)
ans = 10 ** 16

for i in range(k+1):
    #posiから0〜k個, negaからk-posi個
    if i == 0:
        if len(ls_nega) >= k:
            ans = min(ans,ls_nega[k-1])
    elif i == k:
        if len(ls_posi) >= k:
            ans = min(ans,ls_posi[k-1])
    else:
        if len(ls_posi)-1 >= i and len(ls_nega)-1 >= k - i:
            #print(i,ls_posi[i],ls_nega[k-i])
            ans = min(ans,ls_posi[i-1] * 2 + ls_nega[k-i-1])
            ans = min(ans,ls_posi[i-1] + ls_nega[k-i-1] * 2)
print(ans)

Submission status: Wrong Answer

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

ls_posi = []#0を含む
ls_nega = []

for i in range(n):
    if x[i] < 0:
        ls_nega.append(-x[i])
    else:
        ls_posi.append(x[i])

ls_nega.reverse()

ans = 10 ** 6
try:
    ans = min(ans,ls_posi[k-1])
    ans = min(ans,ls_nega[k-1])
except:
    pass

for i in range(k+1):
    try:
        ans = min(ans,ls_posi[k-2-i]*2 + ls_nega[i])
    except:
        pass
    try:
        ans = min(ans,ls_nega[k-2-i]*2 + ls_posi[i])
    except:
        pass
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

#連続する蝋燭を全探索
l=0
r=K-1

ans=2*(10**8)+1
for i in range(N-K+1):
    # print('x[l], x[r]:', x[l], x[r])
    ans = min(ans, min(abs(x[l]), abs(x[r])) + x[r]-x[l])
    l+=1
    r+=1

print(ans)