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

mink = pow(10, 9)
for i, x in enumerate(xlist[k-1:]):
  lx = xlist[i]
  if mink > abs(lx) + x - lx:
    mink = abs(lx) + x - lx
  elif mink > abs(x) + x - lx:
    mink = abs(x) + x - lx
print(mink)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = (int(i) for i in input().split())
x = np.sort(np.array([int(i) for i in input().split()]))

result = []
if(N==1):
    print(np.abs(x[0]))

else:
    for i in range(N - K + 1):
        left = x[i]
        right = x[i + K - 1]

        if ((left!=0) and (right !=0) and (left * right < 0)):
            if (np.abs(left) < np.abs(right)):
                diff = np.abs(left) * 2 + np.abs(right)
            else:
                diff = np.abs(left) + np.abs(right) * 2

        elif(left==0):
            diff = np.abs(right)
            
        elif(right==0):
            diff = np.abs(left)

        else:
            diff = np.max([left, right])

        result.append(diff)

    print(np.min(result))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = (int(i) for i in input().split())
x = np.sort(np.array([int(i) for i in input().split()]))

result = []
if(N==1):
    print(np.abs(x[0]))
    
else:
    for i in range(N - K + 1):
        left = x[i]
        right = x[i + K - 1]

        if (left * right < 0):
            if (np.abs(left) < np.abs(right)):
                diff = np.abs(left) * 2 + np.abs(right)
            else:
                diff = np.abs(left) + np.abs(right) * 2

        else:
            diff = np.max([left, right])

        result.append(diff)

    print(np.min(result))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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(N-K+1):
    if min(abs(x[i])+abs(x[i]-x[i+K-1]),abs(x[i+K-1])+abs(x[i]-x[i+K-1]))<ans:
        ans=min(abs(x[i])+abs(x[i]-x[i+K-1]),abs(x[i+K-1])+abs(x[i]-x[i+K-1]))
print(ans)

Submission status: Wrong Answer

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

length = float("inf")
r, l = 0, 0
for i in range(N - K + 1):
  li = abs(x[i + K - 1] - x[i])
  if li < length:
    length = li
    r = i + K - 1
    l = i
print(min(abs(x[r]) + length, abs(x[l]) + length))

Submission status: Wrong Answer

Solution code:
#ans = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))

N, K = map(int, input().split())
INF = 10**10
x_first = [-INF for i in range(K)]
x_ap = [int(i) for i in input().split()]
x_last = [INF for i in range(K)]
x = x_first + x_ap + x_last + [0]
x.sort()
zero_index = x.index(0)

key = []
for i in range(K):
    left = abs(x[zero_index - i])
    right = abs(x[zero_index - i + K])
    # left か rightの 0に近い方に行ってからもう片方にいく
    if left > right:
        right *= 2
    else:
        left *= 2
    key.append(left + right)
print(min(key))

Submission status: Wrong Answer

Solution code:
#ans = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))

N, K = map(int, input().split())
INF = 10**9
x_first = [-INF for i in range(K)]
x_ap = [int(i) for i in input().split()]
if min(x_ap) > 0:
    print("S")
x_last = [INF for i in range(K)]
x = x_first + x_ap + x_last + [0]
x.sort()
zero_index = x.index(0)

key = []
for i in range(K):
    left = abs(x[zero_index - i])
    right = abs(x[zero_index - i + K])
    # left か rightの 0に近い方に行ってからもう片方にいく
    if left > right:
        right *= 2
    else:
        left *= 2
    key.append(left + right)
print(min(key))

Submission status: Wrong Answer

Solution code:
#ans = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))

N, K = map(int, input().split())
INF = 10**9
x_first = [-INF for i in range(K)]
x_ap = [int(i) for i in input().split()]
if min(x_ap) < 0:
    print("S")
x_last = [INF for i in range(K)]
x = x_first + x_ap + x_last + [0]
x.sort()
zero_index = x.index(0)

key = []
for i in range(K):
    left = abs(x[zero_index - i])
    right = abs(x[zero_index - i + K])
    # left か rightの 0に近い方に行ってからもう片方にいく
    if left > right:
        right *= 2
    else:
        left *= 2
    key.append(left + right)
print(min(key))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
INF = 10**9
x_first = [-INF for i in range(K)]
x_ap = [int(i) for i in input().split()]
x_last = [INF for i in range(K)]
x = x_first + x_ap + x_last + [0]
x.sort()
zero_index = x.index(0)

key = []
for i in range(K):
    left = abs(x[zero_index - i])
    right = abs(x[zero_index - i + K])
    # left か rightの 0に近い方に行ってからもう片方にいく
    if left > right:
        right *= 2
    else:
        left *= 2
    key.append(left + right)
print(min(key))

Submission status: Wrong Answer

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

def calc(startIndex):
    if startIndex + K >= N:
        return -1
    endIndex = startIndex + K - 1
    x1 = Xi[startIndex]
    x2 = Xi[endIndex]
    len1 = abs(x1) + abs(x2 - x1)
    len2 = abs(x2) + abs(x2 - x1)
    return min(len1, len2)

INF = 100000000000000
min_value = INF
for i in range(N):
    d = calc(i)
    if d >= 0 and d < min_value:
        min_value = d
if min_value == INF:
    print(0)
else:
    print(min_value)

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

Submission status: Wrong Answer

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

ans = 1e9
for l, r in zip(X, X[K:]):
  ans = min(ans, r-l+min(abs(l),abs(r)))
print(ans)

Submission status: Wrong Answer

Solution code:
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    ans = MAX

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

    print(ans)
    return


main()

Submission status: Wrong Answer

Solution code:
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    if 0 <= xn[0]:
        print(xn[K - 1])
        return

    if xn[N - 1] < 0:
        print(xn[N - K])
        return

    ans = MAX

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] < 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 < xn[i + K]:
                ans = min(ans, abs(xn[i]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

        elif 0 != i and xn[i - 1] < 0:
            ans = min(ans, xn[i + K - 1])

    print(ans)
    return


main()

Submission status: Wrong Answer

Solution code:
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    ans = MAX

    if 0 < xn[0]:
        ans = xn[K - 1]
        print(ans)
        return

    if xn[N - 1] < 0:
        ans = xn[N - K]
        print(ans)
        return

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] < 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 < xn[i + K]:
                ans = min(ans, abs(xn[i]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

        elif 0 != i and xn[i - 1] < 0:
            ans = min(ans, xn[i + K - 1])

    print(ans)
    return


main()

Submission status: Wrong Answer

Solution code:
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    ans = MAX

    if 0 < xn[0]:
        ans = xn[K - 1]
        print(ans)
        return

    if xn[N - 1] < 0:
        ans = xn[N - K]
        print(ans)
        return

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] < 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 <= xn[i + K]:
                ans = min(ans, abs(xn[i]) - abs(xn[i * K - 1]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

        elif 0 != i and xn[i - 1] < 0:
            ans = min(ans, xn[i + K - 1])

    print(ans)
    return


main()

Submission status: Wrong Answer

Solution code:
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    ans = MAX

    if 0 < xn[0]:
        ans = xn[K - 1]
        print(ans)
        return

    if xn[N - 1] < 0:
        ans = xn[N - K]
        print(ans)
        return

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] < 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 <= xn[i + K]:
                ans = min(ans, abs(xn[i]) - abs(xn[i * K - 1]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

        elif 0 != i and xn[i - 1] < 0:
            if i + K <= N - 1:
                ans = min(ans, xn[i + K])

    print(ans)
    return


main()

Submission status: Wrong Answer

Solution code:
MAX = 300000001

N, K = map(int, input().split())
xn = [int(i) for i in input().split()]


def main():
    ans = MAX

    if 0 < xn[0]:
        ans = xn[K - 1]
        print(ans)
        return

    if xn[N - 1] < 0:
        ans = xn[N - K]
        print(ans)
        return

    for i in range(N):
        if N - 1 < i + K - 1:
            break

        if xn[i] <= 0:
            if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 <= xn[i + K]:
                ans = min(ans, abs(xn[i]) - abs(xn[i * K - 1]))

            elif 0 <= xn[i + K - 1]:
                ans = min(ans,
                          2 * abs(xn[i]) + abs(xn[i + K - 1]),
                          abs(xn[i]) + 2 * abs(xn[i + K - 1]))

        elif 0 == xn[i]:
            ans = min(ans, xn[i + K - 1])

        elif 0 != i and xn[i - 1] < 0:
            if i + K < - N - 1:
                ans = min(ans, xn[i + K])
    print(ans)
    return


main()

Submission status: Wrong Answer

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

absx = [[abs(i),1] if i >=0 else [abs(i),-1] for i in x]
absx.sort(key=itemgetter(0))

Ma = 0
Mb = 0
value = absx[K-1][1]
try:
    flag = True
    if absx[K][0] == absx[K-1][0]:
        for k in range(K):
            if absx[k][0] == 0:
                continue
            if absx[k][1] != absx[K-1][1]:
                flag = False
        if flag:
            value *= -1
except:
    pass

for i in range(K):
    if absx[i][1] != value:
        Ma = max(Ma,absx[i][0]*2)
    else:
        Mb = max(Mb,absx[i][0])
print(Ma+Mb)

Submission status: Wrong Answer

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

plusx = [i for i in x if i >= 0]
minusx = [i for i in x if i < 0]
absx = [[abs(i),1] if i >=0 else [abs(i),-1] for i in x]

absx.sort(key=itemgetter(0))
Ma = 0
Mb = 0
value = absx[K-1][1]
for i in range(K):
    if absx[i][1] != value:
        Ma = max(Ma,absx[i][0]*2)
    else:
        Mb = max(Mb,absx[i][0])
print(Ma+Mb)

Submission status: Wrong Answer

Solution code:
def candles(N: int, K: int, X: int)->int:
    minus = 0
    plus = 0 if X[0] <= 0 else X[0]
    for k in range(K-1):
        xk, xk1 = X[k], X[k+1]
        if xk1 <= 0:
            # xk ... xk1 ... 0
            minus += xk1 - xk
        elif 0 <= xk:
            # 0 .. xk .. xk1
            plus += xk1 - xk
        else:
            # xk ... 0 ... xk1
            minus += -xk
            plus += xk1

    min_d = min(minus*2 + plus, minus + plus*2)

    for i in range(0, N-K):
        xi, xi1, xk_1, xk = X[i], X[i+1], X[i+K-1], X[i+K]
        # 現在見ている区間は、(X[i],...,X[i+K-1])で、
        # 次見る区間は、(X[i+1],...,X[i+K])
        if xi < 0:
            minus -= min(0, xi1) - xi
        else:
            plus -= xi1 - xi

        if xk < 0:
            minus += xk - xk_1
        else:
            plus += xk - max(0, xk_1)

        min_d = min(min_d, minus*2 + plus, minus + plus*2)

    return min_d


if __name__ == "__main__":
    N, K = map(int, input().split())
    X = [int(s) for s in input().split()]
    ans = candles(N, K, X)
    print(ans)

Submission status: Wrong Answer

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

res = 1e9
for l, r in zip(x[:K],x[K-1:]):
  if(l < 0 and r < 0):
    tmp = abs(l)
  elif(l > 0 and r > 0):
    tmp = abs(r)
  else:
    l, r = abs(l), abs(r)
    if(l < r):
      tmp = l*2 + r
    else:
      tmp = r*2 + l
  if(res > tmp):
    res = tmp
print(res)

Submission status: Wrong Answer

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
t = x[-1] - x[0]
i = bisect.bisect_right(x, 0)
for j in range(0, N):
    if N <= j + K -1 :
        break
    if (j - (i - 0.1)) * (j + K- 1 - (i - 0.1)) >= 0:
        t = min(t, max(abs(x[j]), abs(x[j+K-1])))
    else:
        t = min(t, abs(2 * x[j]) + x[j+K-1])
        t = min(t, abs(x[j]) + 2 * x[j+K-1])
print(t)

Submission status: Wrong Answer

Solution code:
import bisect
n ,k = map(int, input().split())
x=[int(i) for i in input().split()]
n=n-x.count(0)
k=k-x.count(0)
x=[xi for xi in x if not(xi==0) ]
if k==0:
    print(0)
    exit()
x0=bisect.bisect_left(x, 0)
s=0
t=k-1
ans=10**10
for i in range(n):
    if s<=x0<=t<=n-1:
        if x[t]>0 and x[s]<0:
            ans=min(2*x[t]-x[s], x[t]-2*x[s],ans)
        elif x[s]>0:
            ans=min(x[t], ans)
        else:
            ans=min(ans, -x[s])
    t+=1
    s+=1
print(ans)

Submission status: Wrong Answer

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

ans = 1000000

for a, b in zip(x, x[K-1:]):
    ans = min(ans, b - a + min(abs(a), abs(b)))

print(ans)

Submission status: Wrong Answer

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

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):
    x_s, x_e = x[i], x[i+k-1]
    if x_e <= 0:
        ans_tmp = x_s
    elif x_s < 0 and 0 < x_e:
        ans_tmp = min(x_e-2*x_s, 2*x_e-x_s)
    else:
        ans_tmp = x_e
    ans = min(ans, ans_tmp)
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):
    x_s, x_e = x[i], x[i+k-1]
    if x_e < 0:
        ans_tmp = x_s
    elif x_s <= 0 and 0 <= x_e:
        ans_tmp = min(x_e-2*x_s, 2*x_e-x_s)
    else:
        ans_tmp = x_e
    ans = min(ans, ans_tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc107/tasks/arc101_a

import itertools
import collections
import bisect

def main():
    # big value
    INF = int(1e15)

    N, K = map(int, input().split())
    X = [-INF] + list(map(int, input().split())) + [INF]
    pos = bisect.bisect(X, 0) # 右にあって0に一番近い要素のインデックス
    # print(pos)

    right_num = N - pos + 1
    left_num = pos - 1

    ans = INF
    if right_num >= K: # 右側からだけ取れるなら
        ans = min(ans, abs(X[pos + K - 1]))
    # print(ans)
    if left_num >= K: # 左側からだけ取れるなら
        ans = min(ans, abs(X[pos - K]))
    # print(ans)

    for i in range(1, K):  # 左側からいくつ取るか
        if i > left_num:  # 左側に取れるだけ数がないなら continue
            continue
        if K - i + 1 > right_num:
            continue
        left_distance = abs(X[pos - i])
        right_distance = abs(X[pos + K - i - 1])
        # min(一番左までの距離 x 往復 + 一番右までの距離, 一番右までの距離 x 往復 + 一番左までの距離)
        ans = min(ans, left_distance * 2 + right_distance, right_distance * 2 + left_distance)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/3/1
Solved on 2019/3/  WA
@author: shinjisu
"""


# ABC 107 C Candles
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**12


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def probC():
    N, K = getIntList()
    X = getIntList()
    dmp((N, K, X))
    X = [-INF] + X + [INF]
    dmp(X)
    for i in range(N+2):
        if X[i] >= 0:
            break
    zeroIdx = i
    dmp(zeroIdx, 'zeroIdx')
    fromIdx = max(1, zeroIdx-K+1)
    dmp(fromIdx, 'fromIdx')
    # X[fromIdx] = 0 if X[fromIdx] == -INF else X[fromIdx]
    minTime = INF
    # for i in range(fromIdx, N+2-fromIdx-K+1):
    # for i in range(fromIdx, N+2-K+1):
    for i in range(fromIdx, N+2-K):
        if X[i] >= 0:
            moveTime = X[i+K-1]
        elif X[i+K-1] <= 0:
            moveTime = abs(X[i])
        else:
            plDist = X[i+K-1]
            mnDist = abs(X[i])
            moveTime = min(plDist, mnDist)*2 + max(plDist, mnDist)
        dmp((i, minTime, moveTime), 'moveTime in loop')
        minTime = min(minTime, moveTime)
    return minTime


debug = False  # True False
ans = probC()
print(ans)
#for row in ans:
#    print(row)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/3/1
Solved on 2019/3/
@author: shinjisu
"""


# ABC 107 C Candles
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**12


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def probC():
    N, K = getIntList()
    X = getIntList()
    dmp((N, K, X))
    X = [-INF] + X + [INF]
    dmp(X)
    for i in range(N+2):
        if X[i] >= 0:
            break
    zeroIdx = i
    dmp(zeroIdx, 'zeroIdx')
    fromIdx = max(0, zeroIdx-K+1)
    dmp(fromIdx, 'fromIdx')
    # X[fromIdx] = 0 if X[fromIdx] == -INF else X[fromIdx]
    minTime = INF
    for i in range(fromIdx, N+2-fromIdx-K+1):
        if X[i] >= 0:
            moveTime = X[i+K-1]
        elif X[i+K-1] <= 0:
            moveTime = X[i]
        else:
            plDist = X[i+K-1]
            mnDist = abs(X[i])
            moveTime = min(plDist, mnDist)*2 + max(plDist, mnDist)
        dmp((minTime, moveTime), 'moveTime in loop')
        minTime = min(minTime, moveTime)
    return minTime


debug = False  # True False
ans = probC()
print(ans)
#for row in ans:
#    print(row)

Submission status: Wrong Answer

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

l = 0
r = 0

for li in reversed(range(0, n)):
    if x[li] < 0:
        l += 1
        if l == k:
            break

for ri in range(0, n):
    if x[ri] > 0:
        r += 1
        if r == k:
            break
    
c = 10**8

def t(a, b):
    if a * b < 0:
        return min(abs(a) * 2 + abs(b), abs(a) + abs(b) * 2)
    else:
        return max(abs(a), abs(b))

for i in range(li, ri + 2 - k):
    ci = t(x[i], x[i + k - 1])
    
    if ci < c:
        c = ci
            
print(c)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N, K = li_input()
    A = li_input()
    D = []

    for i in range(N - K + 1):
        l,r = A[i],A[i+K-1]
        D.append(min(abs(l) + abs(r - l), abs(r), abs(r - l)))

    print(min(D))


main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N, K = li_input()
    A = li_input()
    D = []

    for i in range(N - K + 1):
        l,r = A[i],A[i+K-1]
        D.append(min(abs(l) + abs(r - l), abs(r), abs(l - r)))

    print(min(D))


main()

Submission status: Wrong Answer

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

maxl = abs(max(list))
minl = abs(min(list))

if maxl <= minl:
    print(maxl*2+minl)
else:
    print(minl*2+maxl)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

Y = [abs(x) for x in X]
js = np.argsort(Y)

cnt, nmax, pmax = 0, 0, 0
for j in js:
    cnt += 1
    if cnt>K:
        break
    if X[j]>0:
        pmax = Y[j]
    else:
        nmax = Y[j]

ans = 2*min(pmax,nmax) + max(pmax, nmax)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n==5 and k==3:
  exit()
l=list(map(int,input().split()))
if l.count(0)>0:
  k-=1
  if k==0:
    print(0)
    exit()
else:
  l.append(0)
  l.sort()
l2=[]
l3=[]
for i in range(len(l)-1):
  a=l[i+1]
  b=l[i]
  if a<=0:
    l2.append((a-b)*2)
    l3.append(a-b)
  else:
    l3.append((a-b)*2)
    l2.append(a-b)
mi=100000000000000000000000000000
x=l2[:k]
for i in range(k,len(l2)):
  x1=l2[i]
  if x[0]>=x1:
    for j in range(k-1):
      x[j]=x[j+1]
    mi_t=sum(x)
    if mi>mi_t:
      mi=mi_t
  else:
    for j in range(k-1):
      x[j]=x[j+1]
x=l3[:k]
for i in range(k,len(l3)):
  x1=l3[i]
  if x[0]>=x1:
    for j in range(k-1):
      x[j]=x[j+1]
    mi_t=sum(x)
    if mi>mi_t:
      mi=mi_t
  else:
    for j in range(k-1):
      x[j]=x[j+1]
print(mi)

Submission status: Wrong Answer

Solution code:
int_n, int_k = map(int, input().split())

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

result = list()
for i in range(int_n - int_k + 1):
    result.append(abs(li_x[i]) + abs(li_x[0 + (int_k - 1) + i] - li_x[0 + i]))

print(min(result))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
time=[]
zero=0
if 0 in X:
    zero=X.index(0)
    for i in range(N):
        if zero+i<N and zero-K+1+i>=0:
            time.append(min(abs(X[zero-K+1+i]),abs(X[zero+i]))+abs(X[zero-K+1+i])+abs(X[zero+i]))
else:
    for i in range(N-1):
        if X[i]<0 and X[i+1]>0:
            zero=i
            break
    for i in range(N):
        if zero+i<N and zero-K+1+i>=0:
            time.append(min(abs(X[zero-K+1+i]),abs(X[zero+i]))+abs(X[zero-K+1+i])+abs(X[zero+i]))
print(min(time))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
time=[]
if K<N:
    for k in range(N-K):
        if X[k]*X[k+K-1]<0:
            time.append(min(abs(X[k]),abs(X[K+k-1]))+abs(X[k])+abs(X[K+k-1]))
        else:
            if X[0]<0:
                time.append(-X[-K])
            else:
                time.append(X[K-1])
if N==K:
    if X[0]*X[-1]<0:
        time.append(X[-1]-X[0])
    else:
        if X[0]<0:
            time.append(-X[0])
        else:
            time.append(X[-1])
print(min(time))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
time=[]
if K<N:
    for k in range(N-K):
        if X[k]*X[k+K-1]<0:
            time.append(min(abs(X[k]),abs(X[K+k-1]))+abs(X[k])+abs(X[K+k-1]))
        else:
            time.append(X[K+k-1]-X[k])
if K==K:
    time.append(X[-1]-X[0])
print(min(time))