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] = 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)

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):
    ans = min(ans, abs(x[l])+ x[r]-x[l])
    l+=1
    r+=1

print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, k = MAP()
x = LIST()

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

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import collections

n,k = map(int, raw_input().split(' '))
xis = map(int, raw_input().split(' '))
ais = [xi for xi in xis if xi >= 0]
bis = [-xi for xi in xis if xi < 0][::-1]
m = ais[k -1] if k-1 < len(ais) else +float('inf')
for i in range(len(ais)):
	if i + 1 == k: break
	if 0 <= k - (i+1) -1 < len(bis):

		m = min(m, 2*ais[i] + bis[k - (i+1) -1])
		m = min(m, ais[i] + 2*bis[k - (i+1) -1])
print m

Submission status: Wrong Answer

Solution code:
import collections

n,k = map(int, raw_input().split(' '))
xis = map(int, raw_input().split(' '))
q = collections.deque([]) 
m = +float('inf')
for x in xis:
	q.append(x)
	if len(q) > k: q.popleft()
	if len(q) == k: m = min(m, q[-1] - q[0])
print m

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    ans = 10 ** 8
    for r, l in zip(x[:N - K + 1], x[K - 1:]):
        ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(r - l))

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, input().split())
X = list(map(int, sys.stdin.readline().rsplit()))

res = 10 ** 10
for i in range(N):
    if i + K - 1 < N:
        a, b = X[i], X[i + K - 1]
        if b <= 0:
            res = min(res, abs(a))
        elif 0 <= a:
            res = min(res, b)
        else:
            res = min(res, 2 * abs(a) + abs(b))

print(res)

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        C = X[n:n + K]
        tmp = abs(C[0])
        for k in range(K - 1):
            tmp += abs(C[k] - C[k + 1])
        ret = min(ret, tmp)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
Xrev = X[::-1]
ans = abs(X[0]-X[-1])
for i in range(N-K+1):
    Xdis = abs(X[i])+abs(X[i+K-1]-X[i])
    ans = min(ans,Xdis)
for i in range(N-K+1):
    Xdis = abs(Xrev[i])+abs(Xrev[i+K-1]-Xrev[i])
    ans = min(ans,Xdis)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
ans = abs(X[0]-X[-1])
for i in range(N-K+1):
    Xdis = min(abs(X[i])+abs(X[i+K-1]-X[i]),abs(X[i+K-1])+abs(X[i]-X[i+K-1]))
    #print('Xdis = ' + str(Xdis) + ' X[i] = ' + str(X[i]) +  ' X[i+K] = ' + str(X[i+K-1])) 
    ans = min(ans,Xdis)
print(ans)

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

Solution code:
import itertools

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

temp=0
ans=(abs(min(x))+abs(max(x)))*N

xc=list(itertools.combinations(x,K))

for l in xc:
	if min(l)<=0 and max(l)>=0:
		ans=min(min(2*max(l)+abs(min(l)),max(l)+2*abs(min(l))),ans)
	elif min(l)<0 and max(l)<0:
		ans=min(ans,min(l))
	elif min(l)>0 and max(l)>0:
		ans=min(ans,max(l))

print(ans)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    #S = X[i:i+K]
    #print(S,S[0],S[-1])
    mi = X[i]
    ma = X[i+K-1]
    #print(mi,ma)
    if ma<=0:
        ans = min(ans ,abs(mi))
    elif  mi>0:
        ans = min(ans ,ma)
    elif  mi<0 and ma>0:
        if abs(mi) > ma :
            ans = min(ans,abs(mi) + 2*ma)
        else :
            ans = min(ans,abs(mi)*2 + ma)


print(ans)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    S = X[i:i+K]
    #print(S,S[0],S[-1])
    mi = S[0]
    ma = S[-1]
    if mi<0 and ma<=0:
        ans = min(ans ,abs(mi))
    elif  mi>=0 and ma>0:
        ans = min(ans ,ma)
    elif  mi<0 and ma>0:
        if abs(mi) > ma :
            ans = min(ans,abs(mi) + 2*ma)
        elif abs(mi) < ma :
            ans = min(ans,abs(mi)*2 + ma)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
left = []
right = []
for a in x:
    if a < 0:
        left = [-a]+left
    elif a == 0:
        K -= 1
    else:
        right.append(a)
ans = float("inf") if K > 0 else 0
if K == 0:
    print(0)
    exit(0)
if right == []:
    ans = left[K-1]
elif left == []:
    ans = right[K-1]
else:
    for l in range(min(len(left)+1, K+1)):
        r = K-l
        if l == 0 and len(right) >= r:
            print(right, r)
            ans = min(ans, right[r-1])
        elif l == K and len(left) >= l:
            ans = min(ans, left[l-1])
        elif len(left) >= l and len(right) >= r:
            ans = min(ans, 2*left[l-1]+right[r-1])
            ans = min(ans, left[l-1]+2*right[r-1])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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


def resolve():
    n, k = map(int, input().split())
    X = sorted([0] + list(map(int, input().split())))

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

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

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

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

    left, right = res
    ans = min(abs(X[left]) * 2 + abs(X[right]), abs(X[left]) + abs(X[right]) * 2)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N,K = I()
x = l()
i = bisect.bisect_left(x,0)
if i == 0:
    print(x[K-1])
    exit()
if i == N:
    print(x[N-K])
    exit()
x.insert(i,0)
start = max(i-K,0)
stop = min(i+K+1,N+1)
x = x[start:stop]
ans = 10**9
for i in range(len(x)-K):
    l = x[i:i+K+1]
    if 0 in l:
        ans = min(ans,min(abs(l[0]),abs(l[-1]))+abs(l[0]-l[-1]))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

md = 10**8 + 1
for l in range(n-k+1):
    r = l+k-1
    md = min(md, abs(x[l]) + abs(x[l] - x[r]))
    md = min(md, abs(x[r]) + abs(x[l] - x[r]))
print(md)

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):
    left, right = X[i], X[i + K - 1]
    dist = right - left
    ans = min(abs(left) + dist, abs(right) + dist)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
start = 0 # i-1本目とi本目の間に0がある
for i in range(n):
    if x[i] >= 0:
        start = i
        break

# print(start)
if start == 0:
    if x[-1] < 0:
        # すべて負
        print(x[-k])
        exit()
    else:
        # すべて正
        print(x[k-1])
        exit()
else:
    # 負と正が混在
    if start+k <= n:
        ans = x[start+k-1]
    else:
        ans = 10**9
    for i in range(1, k):
        if start-i+k <= n and start-i+1 >= 0:
            tmp_ans = min(x[start-i+k-1]*2-x[start-i], x[start-i+k-1]-x[start-i]*2)
            # print(start-i+k, start-i+1, x[start-i+k-1], x[start-i], ans, tmp_ans)
            if tmp_ans < ans:
                ans = tmp_ans
    if start-k >= 0:
        tmp_ans = -1*x[start-k]
        if tmp_ans < ans:
            ans = tmp_ans
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
start = 0 # i-1本目とi本目の間に0がある
for i in range(n):
    if x[i] >= 0:
        start = i
        break

# print(start)
if start == 0:
    if x[-1] < 0:
        # すべて負
        print(x[-k])
        exit()
    else:
        # すべて正
        print(x[k-1])
        exit()
else:
    # 負と正が混在
    if start+k <= n:
        ans = x[start+k-1]
    else:
        ans = 10**9
    for i in range(1, k):
        if start-i+k < n and start-i+1 >= 0:
            tmp_ans = min(x[start-i+k-1]*2-x[start-i], x[start-i+k-1]-x[start-i]*2)
            # print(start-i+k, start-i+1, x[start-i+k-1], x[start-i], ans, tmp_ans)
            if tmp_ans < ans:
                ans = tmp_ans
    if start-k >= 0:
        tmp_ans = -1*x[start-k]
        if tmp_ans < ans:
            ans = tmp_ans
print(ans)

Submission status: Wrong Answer

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

l2 = []
for _ in l1:
    if l1[0] >= 0:
        break
    num = l1.pop(0)
    l2.append(num*(-1))
try:
    l2.sort()
except:
    pass

time = []

for i in range(K):
    if i > len(l1)-1:
        break
    if K-(i+1) > len(l2):
        continue
    if K-(i+1)-1 == -1:
        if len(l1) >= K:
            time.append(l1[K-1])
        break
    n1 = l1[i]
    n2 = l2[K-(i+1)-1]
    num = min(n1,n2)*2 + max(n1,n2)
    time.append(num)
    
if len(l2) >= K:
    time.append(l2[K-1])


print(min(time))

Submission status: Wrong Answer

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

l2 = []
for _ in l1:
    if l1[0] >= 0:
        break
    num = l1.pop(0)
    l2.append(num*(-1))
try:
    l2.sort()
except:
    pass

time = []

for i in range(K):
    if i > len(l1)-1:
        break
    if K-(i+1) > len(l2):
        continue
    if K-(i+1)-1 == -1:
        if len(l1) >= K:
            time.append(l1[K-1])
        break
    n1 = l1[i]
    n2 = l2[K-(i+1)-1]
    num = min(n1,n2)*2 + max(n1,n2)
    time.append(num)
if len(l2) >= K:
    time.append(l2[K-1])


print(min(time))

Submission status: Wrong Answer

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

l2 = []
for _ in l1:
    if l1[0] >= 0:
        break
    num = l1.pop(0)
    l2.append(num*(-1))
try:
    l2.sort()
except:
    pass

time = []

for i in range(K):
    if i > len(l1)-1:
        break
    if K-(i+1) > len(l2):
        continue
    if K-(i+1)-1 == -1:
        time.append(l1[K-1])
        break
    n1 = l1[i]
    n2 = l2[K-(i+1)-1]
    num = min(n1,n2)*2 + max(n1,n2)
    time.append(num)

print(min(time))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def read():
    N, K = map(int, input().strip().split())
    X = list(map(int, input().strip().split()))
    return N, K, X


def solve(N, K, X):
    amin = 10**7
    for i in range(N-K+1):
        a0 = min(abs(X[i+K-1]), abs(X[i]))
        a1 = abs(X[i+K-1] - X[i])
        amin = min(amin, a0 + a1)
    return amin


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
min_time  = abs(x[0]) + abs(x[-1] - x[0])

for i in range(n-k+1):
    time = abs(x[i]) + abs(x[i+k-1] - x[i])
    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()))

import numpy as np
X = np.array(X)
Ind = np.argsort(abs(X))


minX = max(X)
maxX = min(X)
for ind in Ind[:K]:
    minX = min(minX, X[ind])
    maxX = max(maxX, X[ind])

if minX <= 0 and maxX >= 0:
    ans = min(-minX, maxX)*2 + max(-minX, maxX)
elif minX > 0:
    ans = maxX
else:
    ans = -minX
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(K):
    if i + K >= N+1:
        break
    else:
        a = min(abs(x[K+i-1]),abs(x[i])) 
        ans = min(ans,x[K+i-1]-x[i]+a)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x_neg = []
x_pos = []
for xx in x:
    if xx < 0:
        x_neg.append(-xx)
    else:
        x_pos.append(xx)
x_neg = x_neg[::-1]

ans = float('inf')
if k <= len(x_neg):
    ans = min(ans, x_neg[k-1])
if k <= len(x_pos):
    ans = min(ans, x_pos[k-1])

for i in range(1, k):
    if i < len(x_pos) and 1 <= (k - i) < len(x_neg):
        d1 = x_pos[i-1]
        d2 = x_neg[k-i-1]
        d = min(d1, d2) * 2 + max(d1, d2)
        ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

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

x_neg = []
x_pos = []
for xx in x:
    if xx < 0:
        x_neg.append(-xx)
    else:
        x_pos.append(xx)
x_neg = x_neg[::-1]

ans = float('inf')
if k <= len(x_neg):
    ans = min(ans, x_neg[k-1])
if k <= len(x_pos):
    ans = min(ans, x_pos[k-1])

for i in range(1, k):
    if i < len(x_pos) and (k - i) < len(x_neg):
        d1 = x_pos[i-1]
        d2 = x_neg[k-i-1]
        d = min(d1, d2) * 2 + max(d1, d2)
        ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
# C - Candles

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

alt_X1 = []
for x in X:
    if x < 0:
        alt_X1.append((x*-2, 1))
    else:
        alt_X1.append((x, 0))
alt_X1.sort()

alt_X2 = []
for x in X:
    if x < 0:
        alt_X2.append((x*-1, 1))
    else:
        alt_X2.append((x*2, 0))
alt_X2.sort()

l1 = 0
r1 = 0
for (val, flg) in alt_X1[:K]:
    if flg == 0:
        r1 = max(r1, val)
    else:
        l1 = max(l1, val)

l2 = 0
r2 = 0
for (val, flg) in alt_X2[:K]:
    if flg == 0:
        r2 = max(r2, val)
    else:
        l2 = max(l2, val)

print(min(l1+r1, l2+r2))

Submission status: Wrong Answer

Solution code:
# C - Candles

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

alt_X = []
for x in X:
    if x < 0:
        alt_X.append((x*-2, 1))
    else:
        alt_X.append((x, 0))
alt_X.sort()

l = 0
r = 0

for (val, flg) in alt_X[:K]:
    if flg == 0:
        r = max(r, val)
    else:
        l = max(l, val)

print(l+r)

Submission status: Wrong Answer

Solution code:
# C - Candles

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

abs_X = [(abs(x), i)for i, x in enumerate(X)]
abs_X.sort()

l = (0, 0) # 最大値とそのインデックス
r = (0, 0)

for (x, i) in abs_X[:K]:
    if X[i] >= 0 and r[0] < X[i]:
        r = (X[i], i)
    elif X[i] < 0 and l[0] > X[i]:
        l = (X[i], i)

if l[0] == 0:
    ans = X[r[1]]
elif r[0] == 0:
    ans = X[l[1]]
else:
    if X[r[1]] > X[l[1]] * -1:
        ans = X[l[1]] * -2 + X[r[1]]
    else:
        ans = X[l[1]] * -1 + X[r[1]] * 2 

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')

for i in range(n):
    # 左に走る
    if X[i] <= 0:
        if 0 <= i+k-1 < n-1:
            temp = 0
            temp+= -X[i]*2
            if X[i+k-1] >= 0:
                temp += X[i+k-1]
                ans = min(ans, temp)
        if 0 <= i-k+1 < n-1:
            ans = min(ans, -X[i-k+1])

    # 右に走る
    elif X[i] > 0:
        if 0 <= i-k+1 < n-1:
            temp = 0
            temp+=X[i]*2
            if X[i-k+1] <= 0:
                temp -= X[i-k+1]
                ans  = min(ans, temp)
        if 0 <= i+k-1 < n-1:
            ans = min(ans, X[i+k-1])

if n == 1:
    ans = abs(X[0])

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
if n == 1:
    ans = abs(X[0])
for i in range(n):
    temp = 0
    # 左に走ってから右
    if X[i] <= 0:
        if not 0 <= i+k-1 < n-1: continue
        temp+= -X[i]*2
        if X[i+k-1] >= 0:
            temp += X[i+k-1]
            ans = min(ans, temp)
    # 右に走ってから左
    elif X[i] > 0:
        if not 0 <= i-k+1 < n-1: continue
        temp+=X[i]*2
        if X[i-k+1] <= 0:
            temp -= X[i-k+1]
            ans  = min(ans, temp)

st = 0
for i in range(n):
    if X[i] == 0:
        st = i
        if st+k-1 < n-1: ans = min(ans, X[st+k-1])
        if 0 <= st-k+1 < n-1: ans = min(ans, X[st-k+1])
        break
    elif X[i] > 0:
        st = i
        if st+k-1 < n-1: ans = min(ans, X[st+k-1])
        if 0 <= st-k < n-1: ans = min(ans, X[st-k])
        break

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
if n == 1:
    ans = abs(X[0])
for i in range(n):
    temp = 0
    # 左に走ってから右
    if X[i] <= 0:
        if not 0 <= i+k-1 < n-1: continue
        temp+= -X[i]*2
        if X[i+k-1] >= 0:
            temp += X[i+k-1]
        ans = min(ans, temp)
    # 右に走ってから左
    elif X[i] > 0:
        if not 0 <= i-k+1 < n-1: continue
        temp+=X[i]*2
        if X[i-k+1] <= 0:
            temp -= X[i-k+1]
        ans  = min(ans, temp)
# 右に走る
st = bisect.bisect_left(X, 0)
if st+k-1 < n-1: ans = min(ans, X[st+k-1])

# 左に走る
st = bisect.bisect_left(X, 0)-1
if 0 <= st-k+1 < n-1: ans = min(ans, X[st-k+1])

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
if n == 1:
    ans = abs(X[0])
for i in range(n):
    temp = 0
    # 左に走ってから右
    if X[i] <= 0:
        if not 0 <= i+k-1 < n-1: continue
        temp+= -X[i]*2
        temp += X[i+k-1]
        ans = min(ans, temp)
    # 右に走ってから左
    elif X[i] > 0:
        if not 0 <= i-k+1 < n-1: continue
        temp+=X[i]*2
        temp+=-X[i-k+1]
        ans  = min(ans, temp)
# 右に走る
st = bisect.bisect_left(X, 0)
if st+k-1 < n-1: ans = min(ans, X[st+k-1])

# 左に走る
st = bisect.bisect_left(X, 0)-1
if 0 <= st-k+1 < n-1: ans = min(ans, X[st-k+1])

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
if n == 0:
    ans = abs(X[0])
for i in range(n):
    temp = 0
    # 左に走ってから右
    if X[i] <= 0:
        if not 0 <= i+k-1 < n-1: continue
        temp+= -X[i]*2
        temp += X[i+k-1]
        ans = min(ans, temp)
    # 右に走ってから左
    elif X[i] > 0:
        if not 0 <= i-k+1 < n-1: continue
        temp+=X[i]*2
        temp+=-X[i-k+1]
        ans  = min(ans, temp)
# 右に走る
st = bisect.bisect_left(X, 0)
if st+k-1 < n-1: ans = min(ans, X[st+k-1])

# 左に走る
st = bisect.bisect_left(X, 0)-1
if 0 <= st-k+1 < n-1: ans = min(ans, X[st-k+1])

print(ans)

Submission status: Wrong Answer

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

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

left = []
right = []

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

left = left[::-1]

ans = float("inf")
for i in range(k):

    if len(left) > k - 1 - i:
        d1 = 0
        d1 = left[k - 1 - i]
        if i - 1 >= 0 and len(right) > i - 1:
            d1 += left[k - 1 - i]
            d1 += right[i - 1]
        elif i - 1 >= 0 and len(left) <= i - 1:
            d1 += float("inf")
        ans = min(ans, d1)

    if len(right) > k - 1 - i:
        d2 = 0
        d2 = right[k - 1 - i]

        if i - 1 >= 0 and len(left) > i - 1:
            d2 += right[k - 1 - i]
            d2 += left[i - 1]
        elif i - 1 >= 0 and len(left) <= i - 1:
            d2 += float("inf")
        ans = min(ans, d2)

print(ans)

Submission status: Wrong Answer

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

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

left = []
right = []

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

left = left[::-1]

ans = float("inf")
for i in range(k):

    if len(left) > k - 1 - i:
        d1 = 0
        d1 = left[k - 1 - i]
        if i - 1 >= 0 and len(right) > i - 1:
            d1 += left[k - 1 - i]
            d1 += right[i - 1]
        else:
            d1 += float("inf")
        ans = min(ans, d1)

    if len(right) > k - 1 - i:
        d2 = 0
        d2 = right[k - 1 - i]
        if i - 1 >= 0 and len(left) > i - 1:
            d2 += right[k - 1 - i]
            d2 += left[i - 1]
        else:
            d2 += float("inf")
        ans = min(ans, d2)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
lst = []
for x_ in x:
    if x_ < 0:
        lst.append((abs(x_), -1))
    elif x_>0:
        lst.append((x_, 1))
lst.sort(key=lambda x:x[0])
pn = [0,0]
for i in range(k-x.count(0)):
    if lst[i][1]==1:
        pn[0] = lst[i][0]
    else:
        pn[1] = lst[i][0]

print(min(pn)*2+max(pn))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
lst = []
for x_ in x:
    if x_ < 0:
        lst.append((abs(x_), -1))
    else:
        lst.append((x_, 1))
lst.sort(key=lambda x:x[0])

pn = [0,0]
for i in range(k):
    if lst[i][1]==1:
        pn[0] = lst[i][0]
    else:
        pn[1] = lst[i][0]

print(min(pn)*2+max(pn))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = [0]
for i in range(n):
    if a[i] < 0:
        b.append(abs(a[i]))
    elif a[i] > 0:
        c.append(a[i])
    else:
        k-=1
if k == 0:
    print(0)
    exit()
b.append(0)
b = b[::-1]
if len(b) == 1:
    print(c[k])
if len(c) == 1:
    print(b[k])
ans = 10 ** 18
for i in range(len(b)):
    if k - i >= len(c):
        continue
    if i == k:
        break
    #print(2*b[i]+c[k-i],b[i]+2*c[k-i])
    ans = min(ans,min(2*b[i]+c[k-i],b[i]+2*c[k-i]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = [0]
for i in range(n):
    if a[i] < 0:
        b.append(abs(a[i]))
    elif a[i] > 0:
        c.append(a[i])
    else:
        k-=1
if k == 0:
    print(0)
    exit()
b.append(0)
b = b[::-1]
ans = 10 ** 18
for i in range(len(b)):
    if k - i >= len(c):
        continue
    if i == k:
        break
    #print(2*b[i]+c[k-i],b[i]+2*c[k-i])
    ans = min(ans,min(2*b[i]+c[k-i],b[i]+2*c[k-i]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = [0]
for i in range(n):
    if a[i] < 0:
        b.append(abs(a[i]))
    else:
        c.append(a[i])
b.append(0)
b = b[::-1]
ans = 10 ** 18
for i in range(len(b)):
    if k - i > len(c) -1:
        continue
    if i == k:
        break
    ans = min(ans,min(2*b[i]+c[k-i],b[i]+2*c[k-i]))
print(ans)

Submission status: Wrong Answer

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

ans = 10**18
for i in range(N - K + 1):
    d = X[i + K - 1] - X[i] + abs(X[i])
    # print('d', d)
    ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = map(int, input().split())
x = np.array(list(map(int, input().split())))

ans = []
for i in range(n - k + 1):
    left = 0
    right = 0
    arr = x[i: i + k]
    if arr.min() < 0:
        left = arr.min()
    if arr.max() >= 0:
        right = arr.max()
    ans.append(min(left * 2 + right, left + right * 2))

print(min(ans))