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:
import sys

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

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
all_p = 10 ** 9
all_m = 10 ** 9
if K <= lastx and K != 0:
    all_p = lst_p[K-1]
if K <= lasty and K != 0 :
    all_m = lst_m[lasty - K]

#print (all_p, all_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m), all_p, all_m))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] < abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1

Submission status: Wrong Answer

Solution code:
import sys

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

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m)))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] < abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
xs = list(map(int,input().split()))
# N, K = 5,3
# xs = [-30,-10,10,20,50]
# plus = [x > 0 for x in xs]
xm = 0 #正になるindex
for i,x in enumerate(xs):
    if x >= 0:
        xm = i
        break
if x <= 0:
    xm = K

# 負側からi個トルように、順にずらしていく
ans = 10**20

for i in range(K+1):
    #+側 xs[xm+K-i-1]
    if i == 0 and xm+K<=N:
        ans = min(ans,xs[xm+K-1])
    #-側 xs[xm-i]
    elif i == K and xm-K>=0: 
        ans = min(ans,abs(xs[xm-K]))
    elif 0 <= xm-i and xm+K-i < N:
        minus = abs(xs[xm-i])*2 + xs[xm+K-i-1]
        plus  = xs[xm+K-i-1]*2 + abs(xs[xm-i])
        ans = min(ans, minus, plus)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
xs = list(map(int,input().split()))
# N, K = 5,3
# xs = [-30,-10,10,20,50]
# plus = [x > 0 for x in xs]
xm = 0 #正になるindex
for i,x in enumerate(xs):
    if x >= 0:
        xm = i
        break
if x < 0:
    xm = K

# 負側からi個トルように、順にずらしていく
ans = 10**20

for i in range(K+1):
    #+側 xs[xm+K-i-1]
    # print(i,xm)
    if i == 0 and xm+K<=N:
        ans = min(ans,xs[xm+K-1])
    #-側 xs[xm-i]
    elif i == K and xm-K>=0: 
        ans = min(ans,abs(xs[xm-K]))
    elif 0 <= xm-i and xm+K-i < N:
        minus = abs(xs[xm-i])*2 + xs[xm+K-i-1]
        plus  = xs[xm+K-i-1]*2 + abs(xs[xm-i])
        ans = min(ans, minus, plus)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
xs = list(map(int,input().split()))
# N, K = 5,3
# xs = [-30,-10,10,20,50]
# plus = [x > 0 for x in xs]
xm = 0 #正になるindex
for i,x in enumerate(xs):
    if x >= 0:
        xm = i
        break
if x < 0:
    xm = K
# 負側からi個トルように、順にずらしていく
ans = 10**20

for i in range(K):
    #+側 xs[xm+K-i-1]
    if i == 0 and xm+K<=N:
        ans = min(ans,xs[xm+K-1])
    #-側 xs[xm-i]
    elif i == K and xm-K>=0: 
        ans = min(ans,abs(xs[xm-K]))
    elif 0 <= xm-i and xm+K-i < N:
        minus = abs(xs[xm-i])*2 + xs[xm+K-i-1]
        plus  = xs[xm+K-i-1]*2 + abs(xs[xm-i])
        ans = min(ans, minus, plus)
print(ans)

Submission status: Wrong Answer

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


###############################################################################


def main():
    n, k = intin()
    xlist = intina()

    xlist = [(abs(x), x) for x in xlist]
    xlist.sort()
    xlist = xlist[:k]
    xlist = [x for _, x in xlist]
    xlist.sort()

    if xlist[0] * xlist[-1] < 0:
        left = abs(xlist[0])
        right = abs(xlist[-1])
        print(min(left, right) * 2 + max(left, right))
        return

    if xlist[0] <= 0 and xlist[-1] <= 0:
        print(abs(xlist[0]))
        return

    print(abs(xlist[-1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def f(x,y):
    if y>0:return x
    elif x<0:return -y
    elif x > -y:return x-2*y
    else: return 2*x-y
a,b=map(int,input().split())
c=list(map(int,input().split()))
d=f(c[-1],c[-1-a+b])
for i in range(a-b):
    if d > f(c[i+2],c[i]):
        d=f(c[i+2],c[i])
print(d)

Submission status: Wrong Answer

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

ans = sum([abs(i) for i in x])
for i in range(N - K + 1):
    t = min(abs(x[i])+abs(x[i+K-1] - x[i]),abs(x[i+K-1]+abs(x[i+K-1]-x[i])))
    if t < ans:
        ans = t

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

"""解説
https://www.youtube.com/watch?v=5O4k1n5Ejzw

区間を考える問題

-30 -10 0 10 20 50

・K = 3のとき、

0座標を必ず含める、ある連続するK個のロウソクについて考える

・-30 -10 0 10
・-10 0 10 20
・0 10 20 50

左端と右端に到達する最小値を求めていけばよくて、間のロウソクは無視してよい（必ず移動中に火をつけるので）

すべての場合について求めて、その中の最小値が答え
"""
def solve():
    N, K = list(map(int, input().split()))
    X = list(map(int, input().split()))
    if 0 in X:
        # 0座標にロウソクがあるなら、火をつけるロウソクをひとつ減らしてよい
        K -= 1
    else:
        X.append(0)
    X.sort()

    origin_i = X.index(0)
    left_i = max(origin_i-K, 0)

    INF = 10**12
    ans = INF
    while left_i <= origin_i:
        if left_i+K < N:
            tmp1 = abs(X[left_i]-0)*2 + abs(X[left_i+K]-0)
            tmp2 = abs(X[left_i]-0) + abs(X[left_i+K]-0)*2
        else:
            break
        ans = min(ans, tmp1, tmp2)
        left_i += 1
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

"""解説
https://www.youtube.com/watch?v=5O4k1n5Ejzw

区間を考える問題

-30 -10 0 10 20 50

・K = 3のとき、

0座標を必ず含める、ある連続するK個のロウソクについて考える

・-30 -10 0 10
・-10 0 10 20
・0 10 20 50

左端と右端に到達する最小値を求めていけばよくて、間のロウソクは無視してよい（必ず移動中に火をつけるので）

すべての場合について求めて、その中の最小値が答え
"""
def solve():
    N, K = list(map(int, input().split()))
    X = list(map(int, input().split()))
    if 0 in X:
        # 0座標にロウソクがあるなら、火をつけるロウソクをひとつ減らしてよい
        K -= 1
    else:
        X.append(0)
    X.sort()

    origin_i = X.index(0)
    left_i = max(origin_i-K, 0)

    INF = 10**12
    ans = INF
    while left_i <= origin_i:
        if left_i+K < N:
            tmp = abs(X[left_i] - X[left_i+K])
        else:
            break
        ans = min(ans, tmp)
        left_i += 1
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

"""解説
https://www.youtube.com/watch?v=5O4k1n5Ejzw

区間を考える問題

-30 -10 0 10 20 50

・K = 3のとき、

0座標を必ず含める、ある連続するK個のロウソクについて考える

・-30 -10 0 10
・-10 0 10 20
・0 10 20 50

左端と右端に到達する最小値を求めていけばよくて、間のロウソクは無視してよい（必ず移動中に火をつけるので）

すべての場合について求めて、その中の最小値が答え
"""
def solve():
    N, K = list(map(int, input().split()))
    X = list(map(int, input().split()))
    if 0 in X:
        K -= 1
    else:
        X.append(0)
    X.sort()

    origin_i = X.index(0)
    left_i = max(origin_i-K, 0)

    ans = 0
    while left_i <= origin_i:
        if left_i+K < N:
            tmp = abs(X[left_i] - X[left_i+K])
        else:
            break
        ans = max(ans, tmp)
        left_i += 1
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

"""解説
-30 -10 0 10 20 50

・K = 3のとき、

0座標を必ず含める、ある連続するK個のロウソクについて考える

・-30 -10 0 10
・-10 0 10 20
・0 10 20 50

左端と右端に到達する最小値を求めていけばよくて、間のロウソクは無視してよい（必ず移動中に火をつけるので）

すべての場合について求めて、その中の最小値が答え
"""
def solve():
    N, K = list(map(int, input().split()))
    X = list(map(int, input().split()))
    if 0 in X:
        pass
    else:
        X.append(0)
    X.sort()

    origin_i = X.index(0)
    left_i = max(origin_i-K, 0)

    ans = 0
    while left_i <= origin_i:
        if left_i+K < N:
            tmp = abs(X[left_i] - X[left_i+K])
        else:
            break
        ans = max(ans, tmp)
        left_i += 1
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

distance = 2 * (10 ** 8)
for i in range(N-K+1):
    tmp_distance = abs(X[i]) + abs(X[i+K-1]-X[i])
    distance = min(distance, tmp_distance)
    tmp_distance = abs(X[i+K-1]) + abs(X[i+K-1]-X[i])
    distance = min(distance, tmp_distance)
print(distance)

Submission status: Wrong Answer

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

distance = 2 * (10 ** 8)
for i in range(N-K+1):
    if X[i+K-1] <= 0:
        distance = min(distance, -X[i])
    elif X[i] >= 0:
        distance = min(distance, X[i+K-1])
    else:
        tmp_distance = X[i+K-1]-X[i]
        tmp_distance += min(X[i+K-1], -X[i])
        distance = min(distance, tmp_distance)
print(distance)

Submission status: Wrong Answer

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

distance = 10 ** 8
for i in range(N-K+1):
    if X[i+K-1] <= 0:
        distance = min(distance, -X[i])
    elif X[i] >= 0:
        distance = min(distance, X[i+K-1])
    else:
        tmp_distance = X[i+K-1]-X[i]
        tmp_distance += min(X[i+K-1], -X[i])
        distance = min(distance, tmp_distance)
print(distance)

Submission status: Wrong Answer

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

distance = max([X[-1] - X[0], -X[0], X[-1]])
for i in range(N-K+1):
    if X[i+K-1] <= 0:
        distance = min(distance, -X[i])
    elif X[i] >= 0:
        distance = min(distance, X[i+K-1])
    else:
        tmp_distance = X[i+K-1]-X[i]
        tmp_distance += min(X[i+K-1], -X[i])
        distance = min(distance, tmp_distance)
print(distance)

Submission status: Wrong Answer

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

distance = max([X[-1] - X[0], -X[0], X[-1]])
for i in range(N-K+1):
    if X[i+K-1] < 0:
        distance = min(distance, -X[i])
    elif X[i] > 0:
        distance = min(distance, X[i+K-1])
    else:
        tmp_distance = X[i+K-1]-X[i]
        tmp_distance += min(X[i+K-1], -X[i])
        distance = min(distance, tmp_distance)
print(distance)

Submission status: Wrong Answer

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

distance = X[-1] - X[0]
for i in range(N-K+1):
    if X[i+K-1] < 0:
        distance = min(distance, -X[i])
    elif X[i] > 0:
        distance = min(distance, X[i+K-1])
    else:
        tmp_distance = X[i+K-1]-X[i]
        tmp_distance += min(X[i+K-1], -X[i])
        distance = min(distance, tmp_distance)
print(distance)

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):
    ans = min(ans,abs(X[i+K-1])-X[i]+min(abs(X[i]),abs(X[i+K-1])))
if ans == 10**9:
    ans = 0
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(N-K+1):
    ans = min(ans,abs(X[i])+abs(X[i+K-1])+min(abs(X[i]),abs(X[i+K-1])))
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(N-K+1):
    if 0 <= X[i] and X[i+K-1] >= 0:
        ans = min(ans,max(X[i:i+K])-min(X[i:i+K]))
    else:
        ans = min(ans,max(X[i:i+K])+abs(min(X[i:i+K])*2))
print(ans)

Submission status: Wrong Answer

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

minv = 10*9

for i in range(n-k+1):
    head = lista[i]
    tail = lista[i+k-1]
    if head*tail <0:
        if abs(head)>abs(tail):
            temp = abs(head) + 2*abs(tail)
        else:
            temp = 2*abs(head) + abs(tail)
        if minv>temp:
            minv=temp
    else:
        temp = max(abs(head),abs(tail))
        if minv>temp:
            minv=temp
            
print(minv)

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = min(index, k)
    count = sys.maxsize
    for i in range(start, min(len(array), k+index)):
        tmp = array[i]
        j = index - (k - (i - index + 1) + 1)
        tmp += array[i] - array[j]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(index, n - k + 1)
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        j = k + i - 1
        tmp += -array[i] + array[j]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-index) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index)):
        tmp = array[i]
        j = index - (k - (i - index + 1) + 1)
        tmp += array[i] - array[j]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k+1) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        j = k + i - 1
        tmp += -array[i] + array[j]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-index) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index)):
        tmp = array[i]
        j = index - (k - (i - index + 1) + 1)
        tmp += array[i] - array[j]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start+1):
        tmp = -array[i]
        j = k + i - 1
        tmp += -array[i] + array[j]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-index) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index)):
        tmp = array[i]
        j = index - (k - (i - index + 1))
        tmp += array[i] - array[j]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        j = k + i - 1
        tmp += -array[i] + array[j]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-(index-1)) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index)):
        tmp = array[i]
        tmp += array[i] - array[k -(i - index + 1)]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index-1
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        tmp += -array[i] + array[k + i - 1]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ans = 10**6
for i in range (N-K+1):
    front = np.array(x[i])
    end   = np.array(x[i+K-1])
    
    if np.sign(front)!= np.sign(end):
        ans_temp = min (abs(front)*2+abs(end),abs(front)+abs(end)*2)
    else:
        ans_temp = max(abs(front),abs(end))
    
    if ans_temp <= ans:
        ans = ans_temp
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-(index-1)) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index)):
        tmp = array[i]
        tmp += array[i] - array[k -(i - index + 1)]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        tmp += -array[i] + array[k + i - 1]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import sys

def ans(index, array, k):
    r = right(index, array, k)
    l =  left(index, array, k)
    return min(r, l)


def right(index, array, k):
    start = max(0, k-(index-1)) + index
    count = sys.maxsize
    for i in range(start, min(len(array), k+index+1)):
        tmp = array[i]
        tmp += array[i] - array[k -(i - index + 1)]
        count = count if count < tmp else tmp

    if len(array) > (index + k - 1):
        count = count if count < array[index + k - 1] else array[index + k - 1]
    
    return count

def left(index, array, k):
    n = len(array)
    start = min(0, n-index-k) + index
    count = sys.maxsize
    
    for i in range(max(0, start - k), start):
        tmp = -array[i]
        tmp += -array[i] + array[k + i - 1]
        count = count if count < tmp else tmp

    if index >= k:
        count = count if count < -array[index-k] else -array[index-k]
    
    return count

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

x = [int(i) for i in input().split()] 

index = n
for i in range(n):
    if x[i] >= 0:
        index = i
        break

print(ans(index, x, k))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ans = 10**6
for i in range (N-K+1):
    front = np.array(x[i])
    end   = np.array(x[i+K-1])
    
    if np.sign(front) != np.sign(end):
        ans_temp = min (abs(front)*2+end,abs(front)+end*2)
    else:
        ans_temp = max(abs(front),abs(end))
    
    if ans_temp <= ans:
        ans = ans_temp
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ans = 10**6
for i in range (N-K+1):
    x_np =np.array(x[i:i+K])
    
    if np.sign(x_np[0]) != np.sign(x_np[K-1]):
        ans_temp = min (abs(x_np[0])*2+x_np[K-1],abs(x_np[0])+x_np[K-1]*2)
    else:
        ans_temp = max(abs(x_np[0]),abs(x_np[K-1]))
    
    if ans_temp <= ans:
        ans = ans_temp
print(ans)

Submission status: Wrong Answer

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

x = list(map(int, (input().split())))
f = sorted([i if i >= 0 else -(i * 2) for i in x])
b = sorted([-i if i < 0 else i * 2 for i in x ])

move = min(f[k - 1], b[k - 1])

a = [i for i in x if move >= abs(i)]
print(move + min(-a[0], a[-1]) if a[0] < 0 and 0 < a[-1] else move)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
x_abs = sorted(x, key=abs)

i = K - 2
both = 10 ** 10
while i >= 0:
	if x_abs[i] * x_abs[K-1] <= 0:
		both = abs(x_abs[K-1]) + 2 * abs(x_abs[i])
		break
	else:
		i -= 1

nx = np.array(x)
either = 10 ** 10
x_pos = nx[nx>=0]
x_neg = nx[nx<=0]
if len(x_pos) >= K:
	either = x_pos[K-1]
if len(x_neg) >= K:
	either = min(either, -x_neg[-K])

print(min(both, either))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().strip().split(' '))
x = list(map(int, input().strip().split(' ')))
min_cost = float('inf')
for i in range(N-K+1):
  now = x[i:i+K]
  n_cost = abs(min(now[0], 0))
  p_cost = max(0, now[-1])
  cost = n_cost + p_cost + min(n_cost, p_cost)
  if min_cost > cost:
    min_cost = cost
  elif min_cost < cost:
    break
print(min_cost)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

c = x[N-1] - x[0]

for i in range(N-K+1):
  a = abs(x[i]-x[i+K-1]) + abs(x[i])
  b = abs(x[i]-x[i+K-1]) + abs(x[i+K-1])
  c = min(a, b, c)
print(c)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
x=list(map(int, input().split()))
memo = [0]*(N+1)
memo[0]=0
for i in range(1, N):
    memo[i] = x[i]-x[i-1]
ans = 10**10

if N==1 and K==1:
    print(abs(x[0]))
    exit()

for i in range(1, N):
    cost = sum(memo[i:i+K-1])
    if len(x[i:i+K]) < K-1:
        break
    cnt = 0
    for j, xj in enumerate(x[i-1:i+K-1]):
        if xj < 0:
            cnt+=1
            cost += abs(xj)
    if cnt==0:
        cost += x[i-1]
    ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

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

ans = X[K-1]-X[0]+min(abs(X[0]), abs(X[K-1]))
ans_num = 0

for i in range(1,N-K):
    que = X[i+K-1]-X[i] + min(abs(X[i]), abs(X[i+K-1]))
    if ans > que:
        ans = que

print(ans)

Submission status: Wrong Answer

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

n_N = N - K
res = []

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

if N == K:
    l_r = abs(x[0]) + abs(x[K-1] - x[0])
    r_l = abs(x[K-1]) + abs(x[0] - x[K-1])
    res.append(min(l_r, r_l))

print(min(res))

Submission status: Wrong Answer

Solution code:


def main():
    num, walk_num = map(int, input().split())
    data_kari = list(map(int, input().split()))
    data = [0 for i in range(num)]

    for i in range(num):
        if data_kari[i] >= 0:
            break

    for j in range(i):
        if j == i - 1:
            data[j] = data_kari[j]
        else:
            data[j] = data_kari[j] - data_kari[j + 1]
    data[i] = data_kari[i]
    for j in range(i + 1, num):
        data[j] = data_kari[j] - data_kari[j -1]

    start_ind = max(0,i - walk_num + 1)

    data_sum_fu = 0
    data_sum_sei = 0

    for i in range(start_ind, start_ind + walk_num):
        if data[i] > 0:
            data_sum_sei += data[i]
        else:
            data_sum_fu += abs(data[i])
    ans = 2 * min(data_sum_sei, data_sum_fu) + max(data_sum_sei, data_sum_fu)


    for i in range(start_ind + 1, num - walk_num + 1):
        left_data = data[i - 1]
        right_data = data[i + walk_num - 1]

        if left_data > 0:
            break
        data_sum_fu -= abs(left_data)
        if right_data > 0:
            data_sum_sei += right_data
        else:
            data_sum_fu += abs(right_data)

        ans_kari = 2 * min(data_sum_sei, data_sum_fu) + max(data_sum_sei, data_sum_fu)

        if ans_kari < ans:
            ans = ans_kari

    print(ans)





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
ans = float('inf')
for i in range(N):
    if i+K <= N:
        if L[i] < 0:
            if L[K+i-1] > 0:
                cur = 2*(-L[i])+L[K+i-1]
                if ans >= cur:
                    ans = cur
            elif L[K+i-1] == 0:
                cur = -L[i]
                if ans >= cur:
                    ans = cur
            else:
                cur = -L[i]
                if ans >= cur:
                    ans = cur
        else:
            cur = L[i+K-1]
            if ans >= cur:
                ans = cur
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
ans = float('inf')
for i in range(N):
    if i+K <= N:
        if L[i] < 0:
            cur = 2*(-L[i])+L[K+i-1]
            if ans >= cur:
                ans = cur
        else:
            cur = L[i+K-1]
            if ans >= cur:
                ans = cur
print(ans)

Submission status: Wrong Answer

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

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

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