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())
candle = list(map(int, input().split()))

m_list = []
p_list = []

ans = 10 ** 13
if K == 1:
   # print(candle[0])
    exit()
# From Left Start
#print("left")
for i in range(N-K+1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i+K-1])
    #print(tmp)
    if tmp < ans:
        ans = tmp
# From Right Start 
#print("right")
for i in range(N-K+1, -1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i-K+1])
   # print(tmp)
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

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

    ans = 10 ** 10
    for i in range(N-K+1):
        L = x[i]
        R = x[i+K-1]
        if L < 0 and R < 0:
            tmp = L
        elif L >= 0 and R >= 0:
            tmp = R
        else:
            tmp = abs(L) + (R-L)
            if tmp > 2 * R + abs(L):
                tmp = 2 * R + abs(L)
        if tmp < ans:
            ans = tmp
    print(ans)

Submission status: Wrong Answer

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

    ans = 10 ** 10
    for i in range(N-K+1):
        L = x[i]
        R = x[i+K-1]
        if L < 0 and R < 0:
            tmp = L
        elif L >= 0 and R >= 0:
            tmp = R
        else:
            tmp = abs(L) + (R-L)
        if tmp < ans:
            ans = tmp
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

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

negs = []
poss = []
for point in points:
    if point == 0:
        K -= 1
    if point < 0:
        negs.append(point*-1)
    if point > 0:
        poss.append(point)
negs.sort()


prev = 0
for i in range(len(poss)):
    poss[i] = poss[i] - prev
    prev += poss[i]
prev = 0
for i in range(len(negs)):
    negs[i] = negs[i] - prev
    prev += negs[i]
poss.append(10**10)
negs.append(10**10)
neg_orig = copy.deepcopy(negs)

"""pos first"""
new_poss = []
prev = 0
for pos in poss:
    new_poss.append((pos)*2)
    prev = pos
# print(new_poss)
total_pos_first = 0
for i in range(K):
    if new_poss[0] < negs[0]:
        total_pos_first += new_poss[0]
        new_poss.remove(new_poss[0])
    else:
        total_pos_first += negs[0]
        negs.remove(negs[0])

"""neg first"""
new_negs = []
prev = 0
for neg in neg_orig:
    new_negs.append((neg)*2)
    prev = neg
# print(new_negs)
total_neg_first = 0
for i in range(K):
    if new_negs[0] < poss[0]:
        total_neg_first += new_negs[0]
        new_negs.remove(new_negs[0])
    else:
        total_neg_first += poss[0]
        poss.remove(poss[0])


# print(total_pos_first)
# print(total_neg_first)
if total_pos_first < total_neg_first:
    print(total_pos_first)
else:
    print(total_neg_first)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
ans,t=2e8,0
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  if l*r>=0:
    t=max(abs(l),abs(r))
  else:
    t=abs(l)+abs(r)+min(abs(l),abs(r))
  ans=min(ans,t)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
x=list(map(int,input().split()))
left=[]
right=[]
origin=0
for u in x:
    if u<0:
        left.append(abs(u))
    elif u==0:
        origin+=1
    else:
        right.append(u)
left.sort()
ans=float("inf")
k-=origin
if k==0:
    print(0)
    sys.exit()
r=len(right)
l=len(left)
for i in range(k-1):
    if i<r and k-i-2<l:
        y=right[i]+left[k-i-2]+min(right[i],left[k-i-2])
        ans=min(ans,y)
print(ans)

Submission status: Wrong Answer

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

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

minX = min(X[: K])
maxX = max(X[: K])

if minX >= 0:
    print(maxX)
elif maxX <= 0:
    print(-minX)
else:
    minX_abs = -minX
    print(minX_abs + maxX + minX_abs if minX_abs < maxX else maxX)

Submission status: Wrong Answer

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

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

minX = min(X[: K])
maxX = max(X[: K])

if minX >= 0:
    print(maxX)
else:
    minX_abs = -minX
    print(minX_abs + maxX + minX_abs if minX_abs < maxX else maxX)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    if i < zero:
        k = abs(i - zero)
    else:
        k = abs(i + 1 - zero)
    if 0 <= k <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i + K - 1]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i + K - 1])
for i in range(N-1, K - 1, -1):
    if i < zero:
        k = abs(i - zero)
    else:
        k = abs(i + 1 - zero)
    if 0 <= k <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i - K]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i - K])
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = [int(c) for c in input().split()]

ans = INF
for i in range(N-K):
  l = i
  r = l+K-1
  ans = min(ans, abs(s[l])+abs(s[r]-s[l]), abs(s[r])+abs(s[r]-s[l]))

print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = input().split()
mc = []
pc = []
for i in s:
  c = int(i)
  if c==0:
    K -= 1
  if c<0:
    mc += [-c]
  elif c>0:
    pc += [c]
mc.sort()
pc.sort()
if K==0:
  print(0)
elif mc==[]:
  print(pc[K-1])
elif pc==[]:
  print(mc[K-1])
else:
  mc += [INF]
  pc += [INF]
  
  xi = 0
  yi = -1
  m = K-1
  while m>0:
    if pc[xi+1]>2*mc[yi+1]:
      yi += 1
    else:
      xi += 1
    m -= 1
  c1 = pc[xi] + 2*mc[yi] if yi!= -1 else pc[xi]
  
  xi = -1
  yi = 0
  m = K-1
  while m>0:
    if mc[yi+1]>2*pc[xi+1]:
      xi += 1
    else:
      yi += 1
    m -= 1
  c2 = 2*pc[xi] + mc[yi] if xi!= -1 else mc[yi]
  
  print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = input().split()
mc = []
pc = []
for i in s:
  c = int(i)
  if c==0:
    K -= 1
  if c<0:
    mc += [-c]
  elif c>0:
    pc += [c]
mc.sort()
pc.sort()
if K==0:
  print(0)
elif mc==[]:
  print(pc[K-1])
elif pc==[]:
  print(mc[K-1])
else:
  mc += [INF]
  pc += [INF]
  
  xi = 0
  yi = -1
  m = K-1
  while m>0:
    if pc[xi+1]>2*mc[yi+1]:
      yi += 1
    else:
      xi += 1
    m -= 1
  c1 = pc[xi] + 2*mc[yi]
  
  xi = -1
  yi = 0
  m = K-1
  while m>0:
    if mc[yi+1]>2*pc[xi+1]:
      xi += 1
    else:
      yi += 1
    m -= 1
  c2 = 2*pc[xi] + mc[yi]
  
  print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N+1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K +1):
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans,d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = list(map(int, input().split()))
ans = float("Inf")
for i in range(N-K+1):
    d = abs(ary[i])
    ans = min(ans,d + abs(ary[i]-ary[i+K-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
b = len(ary[a:])
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        k = K - abs(i - a)
        d = abs(ary[i])
        if 0 < k < b:
            d += abs(ary[i] - ary[k + a])
            if ans > d:
                ans = d
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
b = len(ary[a:])
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        k = K - abs(i - a)
        d = abs(ary[i])
        if 0 < k < b:
            d += abs(ary[i] - ary[k + a])
            if ans > d:
                ans = d

print(0 if ans == float("Inf") else ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        k = K - abs(i - a)
        d = abs(ary[i])
        print(k,d)
        if k > 0 and len(ary[a:]) > k:
            d += abs(ary[i] - ary[k + a])
            if ans > d:
                ans = d

print(0 if ans == float("Inf") else ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        try:
            k = K - abs(i - a)
            d = abs(ary[i])
            if k > 0:
                d += abs(ary[i] - ary[k + a])
                if ans > d:
                    ans = d
        except:print(1)
print(0 if ans == float("Inf") else ans)

Submission status: Wrong Answer

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = input().split()
mc = []
pc = []
for i in s:
  c = int(i)
  if c==0:
    K -= 1
  if c<0:
    mc += [-c]
  elif c>0:
    pc += [c]
if K==0:
  print(0)
elif mc==[]:
  print(pc[K-1])
elif pc==[]:
  print(mc[K-1])
else:
  mc.sort()
  pc.sort()
  mc += [INF]
  pc += [INF]
  
  xi = 0
  yi = -1
  m = K-1
  while m>0:
    if pc[xi+1]>2*mc[yi+1]:
      yi += 1
    else:
      xi += 1
    m -= 1
  c1 = pc[xi] + 2*mc[yi]
  
  xi = -1
  yi = 0
  m = K-1
  while m>0:
    if mc[yi+1]>2*pc[xi+1]:
      xi += 1
    else:
      yi += 1
    m -= 1
  c2 = 2*pc[xi] + mc[yi]
  
  print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = input().split()
mc = []
pc = []
for i in s:
  c = int(i)
  if c==0:
    K -= 1
  if c<0:
    mc += [-c]
  elif c>0:
    pc += [c]

mc.sort()
pc.sort()
mc += [INF]
pc += [INF]

m = K
xi = -1
yi = -1
while m>0:
  if pc[xi+1]>2*mc[yi+1]:
    yi += 1
  else:
    xi += 1
  m -= 1
if yi!=-1:
  c1 = pc[xi] + 2*mc[yi]
else:
  c1 = pc[xi]

m = K
xi = -1
yi = -1
while m>0:
  if mc[yi+1]>2*pc[xi+1]:
    xi += 1
  else:
    yi += 1
  m -= 1
if xi!=-1:
  c2 = 2*pc[xi] + mc[yi]
else:
  c2 = mc[yi]

if K==0:
  print(0)
else:
  print(min(c1, c2))

Submission status: Wrong Answer

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = input().split()
mc = []
pc = []
for i in s:
  c = int(i)
  if c==0:
    K -= 1
  if c<0:
    mc += [-c]
  elif c>0:
    pc += [c]

mc.sort()
pc.sort()
mc += [INF]
pc += [INF]

m = K
xi = -1
yi = -1
while m>0:
  if pc[xi+1]>2*mc[yi+1]:
    yi += 1
  else:
    xi += 1
  m -= 1
if yi!=-1:
  c1 = pc[xi] + 2*mc[yi]
else:
  c1 = pc[xi]

m = K
xi = -1
yi = -1
while m>0:
  if mc[yi+1]>2*pc[xi+1]:
    xi += 1
  else:
    yi += 1
  m -= 1
if xi!=-1:
  c2 = 2*pc[xi] + mc[yi]
else:
  c2 = mc[xi]

if K==0:
  print(0)
else:
  print(min(c1, c2))

Submission status: Wrong Answer

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

cost=10**10

Plus=[i for i in X if i>=0]
Minus=[-i for i in X if i<=0]

if len(Plus)>=K:
    cost=Plus[K-1]
if len(Minus)>=K:
    cost=min(cost,Minus[-K])

i=0
while X[i]<0 and i+K-1<N and i<=K:
    cost=min(cost,X[i+K-1]-X[i]+abs(X[i]))
    i+=1

print(cost)

Submission status: Wrong Answer

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

import bisect

zero_index = bisect.bisect_left(x,0)
start_index = max(0,zero_index-k)
x = x[start_index:zero_index+k]

answer = (x[-1]-x[0])*k
for i in range(len(x)-k+1):
  if x[i+k-1]*x[i]>0:
    answer = min(answer,abs(x[i+k-1]+x[i])) 
  else:
    answer = min(answer,min(abs(x[i+k-1]),abs(x[i]))*2+max(abs(x[i+k-1]),abs(x[i])))

print(answer)

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, k = map(int, input().split())
data = getInputListInt()

left = list()
right = list()

left.append(0)
right.append(0)

point = 0
for i in range(len(data)):
    if data[i] >= 0:
        point = i
        break

tmpleftdis = 0
for i in range(k):
    pointleft = point - i - 1
    if pointleft < 0:
        break
    else:
        tmpleftdis = abs(data[pointleft])
        left.append(tmpleftdis)

tmprightdis = 0
for i in range(k + 1):
    pointright = point + i
    if pointright >= n:
        break
    else:
        tmprightdis = data[pointright]
        right.append(tmprightdis)

ans = sys.maxsize

for i in range(k):
    leftpoint = i
    rightpoint = k - i
    if leftpoint >= len(left)-1:
        continue
    elif rightpoint >= len(right)-1:
        continue
    else:
        tmpansA = 2 * left[leftpoint] + right[rightpoint]
        tmpansB = left[leftpoint] + 2 * right[rightpoint]
        ans = min(ans, tmpansA, tmpansB)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, k = map(int, input().split())
data = getInputListInt()

left = list()
right = list()

left.append(0)
right.append(0)

point = 0
for i in range(len(data)):
    if data[i]>=0:
        point = i
        break

tmpleftdis = 0
for i in range(k):
    pointleft = point - i -1
    if pointleft < 0:
        break
    else:
        tmpleftdis = abs(data[pointleft])
        left.append(tmpleftdis)

tmprightdis = 0
for i in range(k+1):
    pointright = point + i
    if pointright >= n:
        break
    else:
        tmprightdis = data[pointright]
        right.append(tmprightdis)

ans = sys.maxsize

for i in range(k):
    leftpoint = i
    rightpoint = k - i
    if leftpoint >= len(left):
        continue
    elif rightpoint >= len(right):
        continue
    else:
        tmpansA = 2 * left[leftpoint] + right[rightpoint]
        tmpansB = left[leftpoint] + 2 * right[rightpoint]
        ans = min(ans, tmpansA, tmpansB)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = float('inf')
for i in range(n - k + 1):
  l = x[i:i + k]
  if l[0] <= 0 and l[-1] <= 0:
    ans = min(ans, abs(l[0]) * 2)
  elif l[0] <= 0 and l[-1] >= 0:
    ans = min(ans, abs(l[0]) * 2 + l[-1])
  else:
    ans = min(ans, l[-1])
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):
    total_d = x[i+k-1] - x[i]
    zero_d = min(abs(x[i]), abs(x[i+k-1]))
    ans = min(ans, total_d + zero_d)
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):
    min_d = 10**8
    max_d = max(x[i:i+k]) - min(x[i:i+k])
    min_d = min(abs(max(x[i:i+k])), abs(min(x[i:i+k])))
    # print(max_d, min_d)
    ans = min(ans, max_d + min_d)

print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, -x[id - K])
for i in range(max(0, K-id-1), min([N-id, id,K-1])):
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, -x[id - K])
for i in range(max(0, K-id-1), min(N-id, id)):
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, -x[id - K])
for i in range(max(0, K-id-1), min(N-id, id)-1):
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, -x[id - K])
for i in range(max(0, K-id-1), min(N-id, id)-1):
    print(i)
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, -x[id - K])
for i in range(max(0, K-id-1), min(N-id, id)):
    print(i)
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, x[id - K])
for i in range(max(0, K-id-1), min(N-id, id)):
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

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

plus = [i for i in x if i >= 0]
minus = [i for i in x if i <= 0]
id = 0
for i in range(N):
    if x[i] >= 0:
        id = i
        break
ans = float("Inf")
if N-1 - id >= K:
    ans = x[id+K-1]
if id >= K:
    ans = min(ans, x[id - K])
for i in range(max(0, K-id-1), min(N-id, id)):
    ans = min(ans, x[id+i] - x[id-K+1+i]*2)
    ans = min(ans, x[id+i]*2 - x[id-K+1+i])
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k):
    if x[i]*x[i+k-1]>=0:
        ans = min(ans, max(abs(x[i]), abs(x[i+k-1])))
    else:
        a = min(abs(x[i]),abs(x[i+k-1]))
        b = max(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans, a*2+b)
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k):
    if x[i]*x[i+k-1]<0:
        a = abs(x[i])+x[i+k-1] + min(abs(x[i]), x[i+k-1])
    elif x[i+k-1]<=0:
        a = abs(x[i])
    elif x[i]>0:
        a = x[i+k-1]
    
    ans = min(ans, a)

if k==1:
    ans = abs(x[0])
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k):
    if x[i]*x[i+k-1]<0:
        a = abs(x[i])+x[i+k-1] + min(abs(x[i]), x[i+k-1])
    else:
        a = abs(x[i+k-1])
    
    ans = min(ans, a)

if k==1:
    ans = abs(x[0])
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 1e9
n,k = map(int, input().split())
x = list(map(int, input().split()))
ans = INF
for i in range(n-k):
    if x[i]*x[i+k-1]<0:
        a = 2*abs(x[i])+x[i+k-1]
    else:
        a = abs(x[i+k-1])
    
    ans = min(ans, a)

if k==1:
    ans = abs(x[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()]

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

ruiseki = [0]
for i in range(len(r)):
    ruiseki.append(ruiseki[i]+r[i])

index = 0
ans = float('inf')
for i in range(n-k+1):
    tmp = ruiseki[i+k-1]-ruiseki[i]
    if tmp < ans:
        ans = tmp
        index = i

X = [x if x >= 0 else -x for x in X]
t = X[index:index+k]
print(ans+min(t[0],t[-1]))

Submission status: Wrong Answer

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

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

ruiseki = [0]
for i in range(len(r)):
    ruiseki.append(ruiseki[i]+r[i])

index = 0
ans = float('inf')
for i in range(n-k+1):
    ans = min(ans,ruiseki[i+k-1]-ruiseki[i]+abs(X[i]))
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()]

ans = float('inf')
for i in range(n-k+1):
    tmp = abs(X[i])
    for j in range(k-1):
        tmp+= abs(X[i+j]-X[i+j+1])
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

answer = 10 ** 9
for i in range(N-K+1):
    b, c = X[i], X[i+K-1]
    if b * c >= 0:
        answer = min(answer, max(abs(b), abs(c)))
    else:
        answer = min(answer, abs(min(b,c))*2+abs(max(b, c)))
print(answer)

Submission status: Wrong Answer

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

answer = 10 ** 9
for i in range(N-K+1):
    b, c = X[i], X[i+K-1]
    if b * c >= 0:
        answer = min(answer, max(abs(b), abs(c)))
    else:
        answer = min(answer, abs(b)*2+abs(c))
print(answer)

Submission status: Wrong Answer

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

answer = 10 ** 9
for i in range(N-K+1):
    b, c = X[i], X[i+K-1]
    if b * c >= 0:
        answer = min(answer, abs(max(b, c)))
    else:
        answer = min(answer, abs(b)*2+abs(c))
print(answer)

Submission status: Wrong Answer

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

answer = 10 ** 9
for i in range(N-K+1):
    A = X[i:i+K]
    b, c = min(A), max(A)
    if b * c >= 0:
        answer = min(answer, abs(c))
    else:
        answer = min(answer, abs(b)*2+abs(c))
print(answer)

Submission status: Wrong Answer

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

min_v = 10 ** 10
for i in range(N-K+1):

    v = abs(x[i]) + abs(x[i] - x[i+K-1])
    min_v = min(min_v, v)
    
print(min_v)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
c=10**8
if n==k:
	print(0)
else:
	for i in range(n-k+1):
		h=x[i]
		m=x[i+k-1]
		H=abs(h)
		M=abs(m)
		if h*m<0:
			c=min(c,2*H+M,2*M+H)
		else:
			c=min(c,max(M,H))
	print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
c=10**8
if n==k:
	print(0)
else:
	for i in range(n-k+1):
		h=x[i]
		m=x[i+k-1]
		H=abs(h)
		M=abs(m)
		if h*m<0:
			c=min(c,2*H+M,2*M+H)
	else:
			c=min(c,max(M,H))
	print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
c=10**8
for i in range(n-k+1):
	h=x[i]
	m=x[i+k-1]
	H=abs(h)
	M=abs(m)
	if h*m<0:
		c=min(c,2*H+M,2*M+H)
	else:
		c=min(c,max(M,H))
print(c)

Submission status: Wrong Answer

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

neg = 0
pos = 0

left = 0
right = 0

ans = 10**10

for l in range(N - K + 1):
    left = x[l]
    right = x[l + K - 1]
    neg = min(left, 0)
    pos = max(right, 0)
    if neg < 0 and pos > 0:
        ans = min(ans, abs(neg) * 2 + pos)
    else:
        ans = min(ans, abs(neg) + pos)

# print('----------output----------')
print(ans)

Submission status: Wrong Answer

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

x = list(map(int, input().split()))
b = bisect.bisect_left(x, 0)
m = max(0, b-K)
M = min(N,b+K)
x = x[m:M]
ans = abs(x[-1]) + abs(x[0])
for i in range(len(x)-K+1):
  if x[i] > 0:
    ans = min(ans, x[i+K-1])
  elif x[i+K-1] < 0:
    ans = min(ans, abs(x[i]))
  else:
    ans = min(ans, min(abs(x[i]), abs(x[i+K-1])) + x[i+K-1]-x[i])
print (ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
x = 0
besttime = 10**9
while x + K - 1< N:
    if lst[x]*lst[x+K-1] < 0:
        time = min(abs(lst[x])*2+lst[x+K-1], abs(lst[x])+2*lst[x+K-1])
    else:
        time = abs(lst[x] + lst[x+K-1])
    if besttime > time:
        besttime = time
    x += 1
print(besttime)

Submission status: Wrong Answer

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

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

if K == 0:
    ans = 0
else: # Kが1以上
    ans = 10**8
    maxx = max(x)
    minx = min(x)
    for i in range(N-K+1):
        left = i # 0からN-K
        right = i+K-1 # K-1からN-1
        #if (x[min(right+1, N-1)] > 0 and x[max(left-1, 0)] < 0) or (minx > 0 and left == 0) or (maxx < 0 and right == N-1):
        ans = min(ans, x[right] - x[left] + min(abs(x[right]), abs(x[left])))

print(ans)

Submission status: Wrong Answer

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

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

if K == 0:
    ans = 0
else: # Kが1以上
    ans = 10**8
    maxx = max(x)
    minx = min(x)
    for i in range(N-K+1):
        left = i # 0からN-K
        right = i+K-1 # K-1からN-1
        if (x[min(right+1, N-1)] > 0 and x[max(left-1, 0)] < 0) or (minx > 0 and left == 0) or (maxx < 0 and right == N-1):
            ans = min(ans, x[right] - x[left] + min(abs(x[right]), abs(x[left])))
            #print(x[left], x[right], ans)

print(ans)

Submission status: Wrong Answer

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

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

min_cost = 10**8
#プラス側でトライ
if over_zero + k - 1 < n:
  min_cost = X[over_zero + k - 1]
#マイナス側でトライ
if over_zero - k >= 0:
  min_cost = min(X[over_zero - k], min_cost)
for i in range(1, k):
  #マイナスサーチ
  if over_zero - i >= 0:
    minus_cost = -(X[over_zero - i])
  else:
    continue
  #プラスサーチ
  if over_zero + k - i - 1 < n:
    plus_cost = (X[over_zero + k - i - 1])
  else:
    continue
  cost = plus_cost + minus_cost + min(plus_cost, minus_cost) 
  min_cost = min(cost, min_cost)

print(min_cost)

Submission status: Wrong Answer

Solution code:
# WIP (WA: 1_01.txt, 1_06.txt, 1_07.txt)
import sys
input_s = sys.stdin.readline

N, K = map(int, input_s().split())
xxx = list(map(int, input_s().split()))

min_elapsed_time = 10**5
for left, right in zip(xxx, xxx[K - 1:]):
    elapsed_time = right - left + min(abs(left), abs(right))
    """
    right - left : a section distance
    min(abs(left), abs(right)) : a start point to move the section
    """
    min_elapsed_time = \
        elapsed_time if elapsed_time < min_elapsed_time else min_elapsed_time

print(min_elapsed_time)

Submission status: Wrong Answer

Solution code:
# WIP (WA: 1_01.txt, 1_06.txt, 1_07.txt)
import sys
input_s = sys.stdin.readline

N, K = map(int, input_s().split())
xxx = list(map(int, input_s().split()))


# Select candles
min_distance = 10000000000
arg_min = 100000000
for i in range(N-K+1):
    left_edge, right_edge = xxx[i], xxx[i+K-1]
    tmp_larger = max(abs(left_edge), abs(right_edge))
    if tmp_larger < min_distance:
        arg_min = i
        min_distance = tmp_larger
    elif tmp_larger == min_distance and left_edge * right_edge > 0:
        # If all elements of an extracted list
        # are smaller than 0 or larger than 0
        arg_min = i
        min_distance = tmp_larger


# Select route
candles = xxx[arg_min:arg_min+K]
left_edge = candles[0]
right_edge = candles[-1]
left_edge_dist = abs(left_edge)
right_edge_dist = abs(right_edge)
if left_edge > 0:
    print(right_edge)
elif right_edge < 0:
    print(abs(left_edge))
elif left_edge_dist > right_edge_dist:
    print(right_edge_dist*2+left_edge_dist)
else:
    print(left_edge_dist*2+right_edge_dist)