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())
a=list(map(int,input().split()))
a=sorted(a,key=lambda x:abs(x))
m=a[k-1]
b=a[0:k]
if -m in a:
  if -m in b:
    ans=min(abs(min(b))*2+max(b),abs(min(b))+max(b)*2)
  else:
    ans=min(abs(min(b))*2+max(b),abs(min(b))+max(b)*2)
    b[k-1]=-m
    ans=min(ans,min(abs(min(b))*2+max(b),abs(min(b))+max(b)*2))
else:
  if min(b)>=0 or max(b)<=0:
    ans=abs(b[k-1])
  else:
    ans=min(abs(min(b))*2+max(b),abs(min(b))+max(b)*2)
  
print(ans)

Submission status: Wrong Answer

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

INF = float('inf')
ans = INF

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

print(ans)

Submission status: Wrong Answer

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

INF = float('inf')
ans = INF
if a[0] > 0:
    ans = a[K-1]
elif a[-1] < 0:
    ans = a[-K]
else:
    if N == K:
        ans = abs(a[N-1]-a[0])+min(abs(a[0]), abs(a[N-1]))
    else:
        for i in range(N-K):
            ans = min(ans, abs(a[i+K-1]-a[i])+min(abs(a[i+K-1]), abs(a[i])))

print(ans)

Submission status: Wrong Answer

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

INF = float('inf')
ans = INF

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

print(ans)

Submission status: Wrong Answer

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

INF = float('inf')
ans = INF

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

print(ans)

Submission status: Wrong Answer

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

INF = float('inf')
ans = INF

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, K: int, x: "List[int]"):
    x = np.array(x)
    score = 2*10**8
    x_s = sorted(x[x < 0], reverse=True)
    x_plus = x[x >= 0]
    x_len_plus = len(x_plus)
    x_len_minus = len(x_s)
    for right in range(min(K + 1, x_len_plus + 1)):
        left = min(K - right, x_len_minus)
        if right + left == K:
            if x_len_plus > 0:
                rpath = x_plus[right - 1]
            else:
                rpath = 0
            if x_len_minus > 0:
                lpath = abs(x_s[left - 1])
            else:
                lpath = 0
            score = min(score, rpath + lpath + min(rpath, lpath))

    print(score)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    solve(N, K, x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# 頭からn個分みる ans = min(いまみてるやつの0未満*2+いまみてるやつの0以上, いまみてるやつの0未満+いまみてるやつの0以上*2)
# というのを丁寧にみていく。

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


def lower_bound(arr, n):
    # arrのうち初めてn以上になるところ
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] < n:
            left = mid + 1
        else:
            right = mid
    return right


idx = lower_bound(arr, 0)
# print("idx={}".format(idx))
ans = 10 ** 16

for i in range(n - k + 1):
    if i + k < idx:
        # print("under zero")
        ans = min(ans, abs(arr[i]))
    elif arr[i] < 0 and idx <= i + k:
        # print("between zero")
        ans = min(
            ans, 2 * abs(arr[i]) + arr[i + k - 1], abs(arr[i]) + 2 * arr[i + k - 1]
        )
    else:
        # print("upper zero")
        ans = min(ans, abs(arr[i + k - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
# 頭からn個分みる ans = min(いまみてるやつの0未満*2+いまみてるやつの0以上, いまみてるやつの0未満+いまみてるやつの0以上*2)
# というのを丁寧にみていく。

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


def lower_bound(arr, n):
    # arrのうち初めてn以上になるところ
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] < n:
            left = mid + 1
        else:
            right = mid
    return right


idx = lower_bound(arr, 0)
print("idx={}".format(idx))
ans = 10 ** 16

for i in range(n - k + 1):
    if i + k < idx:
        print("under zero")
        ans = min(ans, abs(arr[i]))
    elif arr[i] < 0 and idx <= i + k:
        print("between zero")
        ans = min(
            ans, 2 * abs(arr[i]) + arr[i + k - 1], abs(arr[i]) + 2 * arr[i + k - 1]
        )
    else:
        print("upper zero")
        ans = min(ans, abs(arr[i + k - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
# 頭からn個分みる ans = min(いまみてるやつの0未満*2+いまみてるやつの0以上, いまみてるやつの0未満+いまみてるやつの0以上*2)
# というのを丁寧にみていく。

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


def lower_bound(arr, n):
    # arrのうち初めてn以上になるところ
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] < n:
            left = mid + 1
        else:
            right = mid
    return right


idx = lower_bound(arr, 0)
ans = 10 ** 16

for i in range(n - k + 1):
    if i + k < idx:
        # under zero
        ans = min(ans, abs(arr[i]))
    elif i < idx and idx <= i + k:
        # between zero
        ans = min(
            ans, 2 * abs(arr[i]) + arr[i + k - 1], abs(arr[i]) + 2 * arr[i + k - 1]
        )
    else:
        # upper zero
        ans = min(ans, abs(arr[i + k - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
# 頭からn個分みる ans = min(いまみてるやつの0未満*2+いまみてるやつの0以上, いまみてるやつの0未満+いまみてるやつの0以上*2)
# というのを丁寧にみていく。

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


def lower_bound(arr, n):
    # arrのうち初めてn以上になるところ
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] < n:
            left = mid + 1
        else:
            right = mid
    return right


idx = lower_bound(arr, 0)
ans = 10 ** 10

for i in range(n - k + 1):
    if i + k < idx:
        # under zero
        ans = min(ans, abs(arr[i]))
    elif i < idx and idx <= i + k:
        # between zero
        ans = min(
            ans, 2 * abs(arr[i]) + arr[i + k - 1], abs(arr[i]) + 2 * arr[i + k - 1]
        )
    else:
        # upper zero
        ans = min(ans, abs(arr[i + k - 1]))

print(ans)

Submission status: Wrong Answer

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

for n in range(N-K):
    t = abs(X[n]-X[n+K-1]) + min(abs(X[n]), abs(X[n+K-1]))
    Y.append(t)

if N - K == 0:
    ans = abs(X[0] - X[-1]) + min(abs(X[0]), abs(X[-1]))
    print(ans)
else:
    print(min(Y))

Submission status: Wrong Answer

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

zeroIka=0
zeroIjo=0

ikali=[]
ijoli=[]

ans=float("inf")

#0からk本目の距離を計算しやすいように、0以上と0以下の要素入れた配列それぞれ作るｗ
for i in range(n):
  if x[i]<=0:
    zeroIka+=1
    ikali.append(x[i])
  else:
    zeroIjo+=1
    ijoli.append(x[i])


if zeroIka>=k:
  ikalength=abs(ikali[-k])
  if zeroIjo>=k:
    ijolength=ijoli[k-1]
    ans=min(ikalength,ijolength)
    print(ans)
  else:
    print(ikalength)
  exit()
else:
  #0以上にも0以下にもK本存在しない場合は、k本の範囲を作る→範囲動かして全探索
  ans=float("inf")
  for i in range(n-k+1):
    #まず範囲の長さを求める
    zenlength=x[i+k-1]-x[i]
    #0から左端まで、0から右端までの距離をそれぞれ求める
    temp=min(abs(x[i]),x[i+k-1])
    ans=min(ans,zenlength+temp)
  print(ans)

Submission status: Wrong Answer

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

zeroIka=0
zeroIjo=0

ikali=[]
ijoli=[]

ans=float("inf")

#0からk本目の距離を計算しやすいように、0以上と0以下の要素入れた配列それぞれ作るｗ
for i in range(n):
  if x[i]<=0:
    zeroIka+=1
    ikali.append(x[i])
  else:
    zeroIjo+=1
    ijoli.append(x[i])


if zeroIka>=k:
  ikalength=ikali[-k]
  if zeroIjo>=k:
    ijolength=ijoli[k-1]
    ans=min(ikalength,ijolength)
    print(ans)
  else:
    print(ikalength)
  exit()
else:
  #0以上にも0以下にもK本存在しない場合は、k本の範囲を作る→範囲動かして全探索
  ans=float("inf")
  for i in range(n-k+1):
    #まず範囲の長さを求める
    zenlength=x[i+k-1]-x[i]
    #0から左端まで、0から右端までの距離をそれぞれ求める
    temp=min(abs(x[i]),x[i+k-1])
    ans=min(ans,zenlength+temp)
  print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))
import bisect
ans = 2*10**8
zi = bisect.bisect_left(a,0)
li = zi-max(0,k-n+zi)
ri = min(zi+k, n)
mi = min(li, ri-zi)
j=0
while j <= mi:
    lv = abs(a[li-j])
    rv = abs(a[ri-j-1])
    tmp = 2*min(lv, rv)+max(lv,rv)
    if j==0:
        if (a[li]>=0):
            tmp = rv
        if (a[ri-1]<=0):
            tmp = lv
    if tmp <= ans:
        ans = tmp
    j+=1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
print(X)
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
print(X)
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            print()
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            print()
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

Submission status: Wrong Answer

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

if k == 1:
	d = []
	for i in range(n):
		d.append(abs(x[i]))
	print(min(d))
	exit(0)

sa = []
for i in range(n-1):
	sa.append(x[i+1]-x[i])

ds = []
ds.append(sum(sa[k-1:])-min(abs(x[0]), abs(x[k-1])))
pre = 0
nex = k
presa = 0
nexsa = sum(sa[k-1:])
while nex < n-1:
	presa += sa[pre]
	nexsa -= sa[nex-1]
	ds.append(presa+nexsa-min(abs(x[pre+1]), abs(x[nex])))
	pre += 1
	nex += 1
ds.append(sum(sa[:pre+1])-min(abs(x[pre+1]), abs(x[n-1])))

print(sum(sa)-max(ds))

Submission status: Wrong Answer

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

if k == 1:
	d = []
	for i in range(n):
		d.append(abs(x[i]))
	print(min(d))
	exit(0)

sa = []
for i in range(n-1):
	sa.append(x[i+1]-x[i])

ds = []
ds.append(sum(sa[k-1:])-min(abs(x[0]), abs(x[k-1])))
pre = 0
nex = k
while nex < n-1:
	ds.append(sum(sa[:pre+1])+sum(sa[nex:])-min(abs(x[pre+1]), abs(x[nex])))
	pre += 1
	nex += 1
ds.append(sum(sa[:pre+1])-min(abs(x[pre+1]), abs(x[n-1])))

print(sum(sa)-max(ds))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
import bisect
id = bisect.bisect_left(x,0)
ans=10**10
if id == N:
  print(x[N-K])
  exit(0)
for i in range(max(0,(K+id)-N),min(id+1,K)):
  right = x[K+id-1-i]
  left=0
  if i>0:
    left =x[id-i]
  tmp=min(abs(left)*2+right,right*2+abs(left)) 
  ans=min(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

for i in range(1,N):
  s[i]=x[i]-x[i-1] +s[i-1]
import bisect
id = bisect.bisect_left(x,0)
ans=10**10
for i in range(max(0,(K+id)-N),min(id+1,K+1)):
  right = x[K+id-1-i]
  left=0
  if i>0:
    left =x[id-i]
  tmp=min(abs(left)*2+right,right*2+abs(left)) 
  ans=min(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

for i in range(1,N):
  s[i]=x[i]-x[i-1] +s[i-1]
import bisect
id = bisect.bisect_left(x,0)
ans=10**10
for i in range(max(0,(K+id)-N),min(id+1,K)):
  right = x[K+id-1-i]
  left=0
  if i>0:
    left =x[id-i]
  tmp=min(abs(left)*2+right,right*2+abs(left)) 
  ans=min(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

for i in range(1,N):
  s[i]=x[i]-x[i-1] +s[i-1]
import bisect
id = bisect.bisect_left(x,0)
ans=10**10
for i in range(max(0,(K+id)-N),K):
  right = x[K+id-1-i]
  left=0
  if i>0:
    left =x[id-i]
  tmp=min(abs(left)*2+right,right*2+abs(left))
 
  ans=min(tmp,ans)
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()]
if n == k:
    if x[0] >= 0: print(x[-1])
    elif x[0] < 0 and x[-1] < 0: print(abs(x[0]))
    else:
        if abs(x[0]) < x[-1]: print(abs(x[0]) * 2 + x[-1])
        else: print(abs(x[0]) + x[-1] * 2)
elif k == 0: print(0)
else:
    ans = abs(x[0]) * 2 + abs(x[-1])
    for i in range(n-k):
        st = i
        e = i+k-1
        ans = min(ans, abs(x[st]) + abs(x[e] - x[st]), abs(x[e]) + abs(x[e] - x[st]))
    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()]
if n == k:
    if x[0] >= 0: print(x[-1])
    elif x[0] < 0 and x[-1] < 0: print(abs(x[0]))
    else:
        if abs(x[0]) < x[-1]: print(abs(x[0]) * 2 + x[-1])
        else: print(abs(x[0]) + x[-1] * 2)
else:
    ans = abs(x[0]) * 2 + abs(x[-1])
    for i in range(n-k):
        st = i
        e = i+k-1
        ans = min(ans, abs(x[st]) + abs(x[e] - x[st]), abs(x[e]) + abs(x[e] - x[st]))
    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()]
if n == k:
    if x[0] >= 0: print(x[-1])
    elif x[0] < 0 and x[-1] < 0: print(abs(x[0]))
    else:
        if abs(x[0]) < x[-1]: print(abs(x[0]) * 2 + x[-1])
        else: print(abs(x[0]) + x[-1] * 2)
else:
    d = []
    for i in range(n-k):
        st = i
        e = i+k-1
        if x[st] >= 0:
            d.append(x[e])
        if x[st] < 0 and x[e] < 0:
            d.append(abs(x[st]))
        else:
            if abs(x[st]) <  x[e]: d.append(abs(x[st]) * 2 + x[e])
            else: d.append(abs(x[st]) + x[e] * 2)
    print(min(d))

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
x = [int(i) for i in input().split()]
if n == k: print(sum(x))
else:
    d = []
    for i in range(n-k):
        st = i
        e = i+k-1
        if x[st] >= 0:
            d.append(x[e])
        if x[st] < 0 and x[e] < 0:
            d.append(abs(x[st]))
        else:
            if abs(x[st]) <  x[e]: d.append(abs(x[st]) * 2 + x[e])
            else: d.append(abs(x[st]) + x[e] * 2)
    print(min(d))

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
ans = 10**18
for i in range(N-K+1):
    ans = min(ans, abs(X[i])+abs(X[i+K-1]-X[i]))
print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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


for distance in range(1000000):
    a = bisect.bisect_left(x, -distance)
    b = bisect.bisect_right(x, distance)
    near_k = b- a
    if near_k == k:
        break
    elif near_k > k:
        if x[a]+1 in x:
            a += 1
            break
        elif x[b-1] - 1 in x:
            b -= 1
            break

if x[b-1] * x[a] < 0:
    semi_final_distance = max(x[b-1], x[a])+ (-2 * min(x[b-1], x[a]))
else:
    semi_final_distance = max(x[b-1], x[a])

a_2 = bisect.bisect_left(x, -semi_final_distance)
b_2 = bisect.bisect_right(x, semi_final_distance)
c = bisect.bisect_left(x, 0)
if b_2 - c > k:
    for distance in range(semi_final_distance):
        b_3 = bisect.bisect_right(x, distance)
        if b_3 - c == k:
            print(distance)
            exit()
elif c - a_2 > k:
    for distance in range(semi_final_distance):
        a_3 = bisect.bisect_left(x, -distance)
        if c - a_3 == k:
            print(distance)
            exit()
else:
    print(semi_final_distance)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    M=X[i+K-1]
    m=X[i]
    if M>0 and M*m>0:
        ans=min(ans,M)
    elif m<0 and m*M>0:
        ans=min(ans,-m)
    else:
        ans=M-m+min(M,-m)

print(ans)

Submission status: Wrong Answer

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

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

print(a)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ress = [] 
for i in range(N-K+1):
    ress.append(abs(x[i]) + abs(x[i+K-1]-x[i]))
print(min(ress))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    l.sort()
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**20 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    if b>len(rr):
        continue 
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**20 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    if b>len(rr):
        continue 
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()

le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**20 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    if b>=len(rr):
        continue 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=l[i]-l[i-1]
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()

le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
#print(left)
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**9 

for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
#print(rl)
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])
#print(rr)
if k==n:
    print(sum(rl)+sum(rr))
    exit()
for a in range(1,len(left)):
    b=k-a 
    if b>=len(rr):
        continue 
    #print(a,b,rl[a],rr[b-1])
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
l=[abs(i) for i in l]
sm=sum(l[:k])
mini=sm 
mini=min(mini,sm)
for i in range(k,n):
    sm=sm-l[i-k]
    sm+=l[i]
    mini=min(mini,sm)
print(mini)

Submission status: Wrong Answer

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

ans=100000000
for i in range(N-K+1):
  tmp1=abs(x[i])+abs(x[i+K-1]-x[i])
  tmp2=abs(x[i+K-1])+abs(x[i+K-1]-x[i])
  tmp=min(tmp1,tmp2)
  ans=min(tmp,ans)

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
inputs = sys.stdin.readlines()

N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))

if N == 1:
    print(abs(X[0]))
else:
    ans = 2 * 10 ** 8
    for i in range(N-K+1):
        x1, x2 = X[i], X[i+K-1]
        if x1 < 0 and x2 <= 0:
            ans = min(ans, -x1)
        elif x1 < 0 and 0 < x2:
            if -x1 < x2:
                ans = min(ans, -2*x1 + x2)
            else:
                ans = min(ans, -x1 + 2*x2)
        else:
            ans = min(ans, x2)
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
inputs = sys.stdin.readlines()

N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))

if N == 1:
    print(X[0])
else:
    ans = 2 * 10 ** 8
    for i in range(N-K+1):
        x1, x2 = X[i], X[i+K-1]
        if x1 <= 0 and x2 <= 0:
            ans = min(ans, -x1)
        elif x1 <= 0 and 0 < x2:
            if -x1 < x2:
                ans = min(ans, -2*x1 + x2)
            else:
                ans = min(ans, -x1 + 2*x2)
        else:
            ans = min(ans, x2)
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
inputs = sys.stdin.readlines()

N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))

if N == 1:
    print(X[0])
else:
    Y = []
    for i in range(N-K+1):
        l, r = i, i+K-1
        if X[l] <= 0 and X[r] <= 0:
            Y.append(X[l])
        elif X[l] <= 0 and 0 < X[r]:
            if -X[l] < X[r]:
                Y.append(-2*X[l] + X[r])
            else:
                Y.append(-X[l] + 2*X[r])
        else:
            Y.append(X[r])
            break
    print(min(Y))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
k = 2*(10**6)
for i in range(N-K+1):
	l = X[i]
	r = X[i+K-1]
	k = min(k, abs(l)+abs(r-l), abs(r)+abs(r-l))
print(k)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
k = 2*N
for i in range(N-K+1):
	l = X[i]
	r = X[i+K-1]
	k = min(k, abs(l)+abs(r-l), abs(r)+abs(r-l))
print(k)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
k = 0
for i in range(N-K):
	l = min(X[i], 0)
	r = max(0, X[i+K-1])
	k = max(k, r-l)
print(k)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))

def journey(main,sub):
    #print("#########")
    #print(main,sub)
    main=main[::-1]
    sub=list(map(lambda x:x*2,sub))
    if len(main)<K:
        num=K-len(main)
        ans=sum(main)
        for _ in range(num):
            ans+=sub.pop(-1)
    else:
        ans=sum(main[:K])
        #print(main,sub)
    if len(main)==0 or len(sub)==0:
        pass
    else:
        while main[-1]>sub[-1]:
            ans+=sub[-1]-main[-1]
            main.pop(-1)
            sub.pop(-1)
            #print(main,sub)
            if len(main)==0 or len(sub)==0:
                break

    return ans
    
i=0
while X[i]<0:
    i+=1
    if i==len(X):
        break

neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]

d_pos=[pos[i+1]-pos[i] for i in range(len(pos)-1)][::-1]#popしたいのでひっくり返す
d_neg=[neg[i+1]-neg[i] for i in range(len(neg)-1)][::-1]

a=(journey(d_pos,d_neg))
b=(journey(d_neg,d_pos))

print(min(a,b))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
X=list(map(int,input().split()))
X.sort()

def journey(main,sub):
    main=main[:]
    sub=list(map(lambda x:x*2,sub))
    
    ans=0
    for _ in range(K):
        if len(main)==0:
            ans+=sub.pop(-1)
        elif len(sub)==0:
            ans+=main.pop(-1)
        elif main[-1]<=sub[-1]:
            ans+=main.pop(-1)
        else:
            ans+=sub.pop(-1)
    
    return ans
    
i=0
while X[i]<0:
    i+=1
    if i==len(X):
        break

neg=[0]
pos=[0]
neg=neg+list(map(lambda x:x*(-1),X[:i][::-1]))
pos=pos+X[i:]

d_pos=[pos[i+1]-pos[i] for i in range(len(pos)-1)][::-1]#popしたいのでひっくり返す
d_neg=[neg[i+1]-neg[i] for i in range(len(neg)-1)][::-1]

a=(journey(d_pos,d_neg))
b=(journey(d_neg,d_pos))

print(min(a,b))

Submission status: Wrong Answer

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

#print(candle)

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-1,K-2,-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:
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-1,-1 + K,-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:
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,-1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i-K+1])
 #   print(tmp)
    if tmp < ans:
        ans = tmp

print(ans)