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 bisect

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

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""
ans = 10**100


for i in range(k+1):
    #print(idx-i,idx+k-1-i)
    if i == 0 and idx+k-1 < n:
        ans = min(ans, x[idx+k-1])
    elif idx - i < 0 or idx+k-1-i >= n:
        continue
    else:
        ans = min(ans, - 2*x[idx-i] + x[idx+k-1-i])
        ans = min(ans, - x[idx-i] + 2*x[idx+k-1-i])

if idx == n:
    ans = x[-k]

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""
ans = 10**100

for i in range(k+1):
    #print(idx-i,idx+k-1-i)
    if i == 0 and idx+k-1 < n:
        ans = min(ans, x[idx+k-1])
    elif idx - i < 0 or idx+k-1-i >= n:
        continue
    else:
        ans = min(ans, - 2*x[idx-i] + x[idx+k-1-i])
        ans = min(ans, - x[idx-i] + 2*x[idx+k-1-i])

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

idx = bisect.bisect_left(x,0)

ans = 10**100

for i in range(k):
    #print(idx-i,idx+k-1-i)
    if i == 0 and idx+k-1 < n:
        ans = min(ans, x[idx+k-1])
    elif idx - i < 0 or idx+k-1-i >= n:
        continue
    else:
        ans = min(ans, - 2*x[idx-i] + x[idx+k-1-i])
        ans = min(ans, - x[idx-i] + 2*x[idx+k-1-i])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

tmp = sys.maxsize

for i in range(K):
    minNum = min(candles[i:i+K])
    maxNum = max(candles[i:i+K])
    if minNum >= 0 and maxNum >= 0:
        ans = maxNum
    elif maxNum >= 0 and minNum < 0:
        a = 2 * abs(minNum) + maxNum
        b = 2 * maxNum + abs(minNum)
        ans = min(a,b)
    elif maxNum < 0 and minNum < 0:
        ans = abs(minNum)
    tmp = min(ans, tmp)

print(tmp)

Submission status: Wrong Answer

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i) <= l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i) <= l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    if K-1 < l_mi:
        ans = min(ans, _mi[K-1])

    if K-1 < l_pi:
        ans = min(ans, _pi[K-1])
    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Wrong Answer

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1) < l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1) < l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    if K-1 < l_mi:
        ans = min(ans, _mi[K-1])

    if K-1 < l_pi:
        ans = min(ans, _pi[K-1])
    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Wrong Answer

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


# X.sort()

# print(X)

mi = 0
pl = 0

_mi = []
_pi = []

fl = 0
if X[0] < 0:
    for i in range(N):
        if X[i] >= 0:
            fl = i
            break

    # print(fl)

    _mi = X[:fl]
    _mi = _mi[::-1]

    for i in range(len(_mi)):
        _mi[i] = abs(_mi[i])

    _pi = X[fl:]

    # print(_mi)
    # print(_pi)


    ans = 10000000000000000

    l_mi = len(_mi)

    l_pi = len(_pi)

    for i in range(l_mi):
        if K - (i+1) < l_pi and K - (i+1) > -1:
            ans = min(ans, _mi[i]*2+_pi[(K-1)-(i+1)])


    for i in range(l_pi):
        if K - (i+1) < l_mi and K - (i+1) > -1:
            # print(K-(i+1))
            ans = min(ans, _pi[i]*2+_mi[(K-1)-(i+1)])

    # bk_m = [0 for i in range(len(_mi))]

    # for i in range(len(_mi)):
    #     bk_m[_mi[i]] += 1

    # bk_p = [0 for i in range(len(_pi))]

    # for i in range(len(_pi)):
    #     bk_p[_pi[i]] += 1

    print(ans)

else:
    print(X[K-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
m = abs(X[-1])+abs(X[0])
for i in range(N-K+1):
    if X[i] > 0:
        d = X[i+K-1]
    elif X[i+K-1] < 0:
        d = -X[i]
    else:
        d = X[i+K-1]-X[i]+min(-X[i],X[i+K-1])
    m = min(m,d)
print(m)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
min_len=1e9
for i in range(n-k+1):
    min_len = min(min_len,min(0,x[i])*(-2) + x[i+k-1])
print(min_len)

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):
    temp = x[i:i+k]
    dif = temp[-1] - temp[0]
    from_s = min(abs(temp[-1]), abs(temp[0]))

    temp2 = dif + from_s


    if temp2 > ans:
        print(ans)
        exit()
    else:
        ans = temp2
        if i == n-k:
            print(ans)
            exit()

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):
    temp = x[i:i+k]
    dif = temp[-1] - temp[0]
    from_s = min(abs(temp[-1]), abs(temp[0]))

    if dif + from_s >= ans:
        print(ans)
        exit()
    else:
        ans = dif + from_s

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):
    temp = x[i:i+k]
    dif = temp[-1] - temp[0]
    from_s = min(abs(temp[-1]), abs(temp[0]))

    if dif + from_s > ans:
        print(ans)
        exit()
    else:
        ans = dif + from_s

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
zero_idx = 0
for i, x in enumerate(x_list):
  if x >= 0:
    zero_idx = i
    break
start_idx = max([zero_idx-k, 0])
end_idx = zero_idx
if zero_idx+k > n:
  end_idx = n - k
min_score = 1000000000
for i in range(start_idx, end_idx+1):
  minus_score = abs(min(x_list[i], 0))
  plus_score = abs(max(x_list[i+k-1], 0))
  double = min(minus_score, plus_score)
  score = minus_score + plus_score + double
  if min_score > score:
    min_score = score
    
print(min_score)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
zero_idx = 0
for i, x in enumerate(x_list):
  if x >= 0:
    zero_idx = i
    break
start_idx = max([zero_idx-k, 0])
end_idx = zero_idx
if zero_idx+k > n:
  end_idx = n - k
min_score = 1000000000

for i in range(start_idx, end_idx+1):
  minus_score = abs(x_list[i])
  plus_score = abs(x_list[i+k-1])
  double = min(minus_score, plus_score)
  score = minus_score + plus_score + double
  if min_score > score:
    min_score = score
    
print(min_score)

Submission status: Wrong Answer

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

#連続K点が同じ富豪
lengthlist=[]
#違う富豪
for i in range(N-K+1):
  if x[i+K-1]*x[i]>=0:
    lengthlist.append(abs(x[0])+x[i+K-1]-x[i])
  elif abs(x[i+K-1])>abs(x[i]):
    lengthlist.append(abs(x[i+K-1])+abs(x[i])*2)
  else:
    lengthlist.append(abs(x[i+K-1])*2+abs(x[i]))

#print(lengthlist)
print(min(lengthlist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, i = LI()
    print(n-i+1)
    return

#B
def B():
    h, w = LI()
    a = SR(h)
    dy = defaultdict(int)
    dx = defaultdict(int)
    for y in range(h):
        for x in range(w):
            if a[y][x] == "#":
                dx[x] = 1
                dy[y] = 1
    dy = list(sorted(dy.items(), key=lambda x: x[0]))
    dx = list(sorted(dx.items(), key=lambda x: x[0]))
    for y,_ in dy:
        for x,_ in dx:
            print(a[y][x], end="")
        print()
    return

#C
def C():
    n, k = LI()
    x = LI()
    ans = inf
    for i in range(n - k + 1):
        if x[i] * x[i + k - 1] > 0:
            ans = min(ans, max(x[i], x[i + k - 1]))
            continue
        ans = min(ans, min(abs(x[i]), abs(x[i + k - 1])) * 2 + max(abs(x[i]), abs(x[i + k - 1])))
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, i = LI()
    print(n-i+1)
    return

#B
def B():
    h, w = LI()
    a = SR(h)
    dy = defaultdict(int)
    dx = defaultdict(int)
    for y in range(h):
        for x in range(w):
            if a[y][x] == "#":
                dx[x] = 1
                dy[y] = 1
    dy = list(sorted(dy.items(), key=lambda x: x[0]))
    dx = list(sorted(dx.items(), key=lambda x: x[0]))
    for y,_ in dy:
        for x,_ in dx:
            print(a[y][x], end="")
        print()
    return

#C
def C():
    n, k = LI()
    x = LI()
    ans = inf
    for i in range(n - k + 1):
        ans = min(ans, min(abs(x[i]), abs(x[i + k - 1])) * 2 + max(abs(x[i]), abs(x[i + k - 1])))
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
def main(): #startline------------------------------------------
    N,K = map(int,input().split())
    x = list(map(int,input().split()))
    ans = 0
    flag = False
    for i in range(N-K):
        flag = True
        l = x[i]
        r = x[i+K-1]
        p = abs(l)+abs(r-l)
        q = abs(r)+abs(r-l)
        r = min(p,q)
        if ans > r:
            ans = r
    print(r if flag else abs(x[0]))
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(N - K + 1):
    l = x[i]
    r = x[i + K - 1]
    if l < 0 < r:
        ans = min(ans, abs(l) + 2 * r)
        ans = min(ans, r + 2 * abs(l))
    else:
        ans = min(ans, r)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k-1]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n-k):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam == near_zero_big:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k-1]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n-k):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam != near_zero_big:
                break
            else:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k+1]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n-k):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam != near_zero_big:
                break
            else:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n-k):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam != near_zero_big:
                break
            else:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

Submission status: Wrong Answer

Solution code:

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

li.append(0)
li.sort()

ans = 50
for i in range(n-k):
    li_k = li[i:i+k+1]
    if 0 not in li_k:
        continue
    else:
        zero_index = li_k.index(0)
        if zero_index != 0 and zero_index != len(li_k)-1:
            cost = min(abs(li_k[:zero_index+1][0]),abs(li_k[zero_index-1:][-1])) * 2 + max(abs(li_k[:zero_index+1][0]),abs(li_k[zero_index-1:][-1]))
        elif zero_index == 0:

            cost = li_k[-1]
        elif zero_index == len(li_k)-1:
            cost = abs(li_k[0])
        if cost < ans or ans == 0:
            ans = cost
    del li_k, zero_index
print(ans)

Submission status: Wrong Answer

Solution code:

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

li.append(0)
li.sort()

ans = 50
for i in range(n):
    li_k = li[i:i+k+1]
    if 0 not in li_k:
        continue
    else:
        zero_index = li_k.index(0)
        if zero_index == 0:
            cost = li_k[-1]
        elif zero_index == len(li_k)-1:
            cost = abs(li_k[0])
        else:
            cost = min(abs(li_k[:zero_index+1][0]),abs(li_k[zero_index-1:][-1])) * 2 + max(abs(li_k[:zero_index+1][0]),abs(li_k[zero_index-1:][-1]))
        if cost < ans or ans == 0:
            ans = cost
    del li_k, zero_index
print(ans)

Submission status: Wrong Answer

Solution code:

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

li.append(0)
li.sort()

ans = 50
for i in range(n):
    li_k = li[i:i+k+1]
    if 0 not in li_k:
        continue
    else:
        zero_index = li_k.index(0)
        if zero_index == 0:
            cost = li_k[-1]
        elif zero_index == len(li_k)-1:
            cost = abs(li_k[0])
        else:
            cost = min(abs(li_k[:zero_index+1][0]),abs(li_k[zero_index-1:][-1])) * 2 + max(abs(li_k[:zero_index+1][0]),abs(li_k[zero_index-1:][-1]))
        if cost < ans or ans == 0:
            ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
INF=10**9+7
if 0 in l:k=k-1
lm=[0]+[i for i in l if i<0][::-1]
if len(lm)<k+1:
    lm=lm+[-INF]*(k+1-len(lm))
lp=[0]+[i for i in l if i>0]
if len(lp)<k+1:
    lp=lp+[INF]*(k+1-len(lp))
ans=INF*10
for i in range(k+1):
    tmp=min(2*lp[i]-lm[k-i],lp[i]-2*lm[k-i])
    print(tmp)
    if tmp<ans:ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 10**9
for i in range(N-K+1):
  if A[i]*A[i+K-1] <= 0:
    ans = min(ans, A[i+K-1]-A[i]+min(abs(A[i]), A[i+K-1]))
  else:
    ans = min(ans, min(abs(A[i]), abs(A[i+K-1])))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 10**9
for i in range(N-K+1):
  if A[i]*A[i+K-1] <= 0:
    ans = min(ans, A[i+K-1]-A[i]+min(abs(A[i]), A[i+K-1]))
  else:
    ans = min(ans, min(abs(A[i]), A[i+K-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 3*10**5
for i in range(N-K+1):
  if A[i]*A[i+K-1] <= 0:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, (abs(A[i]))*2+abs(A[i+K-1]))
    else:
      ans = min(ans, abs(A[i])+(abs(A[i+K-1]))*2)
  else:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, abs(A[i+K-1]))
    else:
      ans = min(ans, abs(A[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 3*10**5
for i in range(N):
  if i + K > N:
    break
  if A[i]*A[i+K-1] <= 0:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, abs(A[i])*2+abs(A[i+K-1]))
    else:
      ans = min(ans, abs(A[i])+abs(A[i+K-1])*2)
  else:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, abs(A[i+K-1]))
    else:
      ans = min(ans, abs(A[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 3*10**5
for i in range(N):
  if i + K > N:
    break
  if A[i]*A[i+K-1] <= 0:
    if abs(A[i]) < abs(A[i+K-1]):
      ans = min(ans, abs(A[i])*2+abs(A[i+K-1]))
    else:
      ans = min(ans, abs(A[i])+abs(A[i+K-1])*2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
zero_idx = 0
for idx in range(N):
    if A[idx] > 0:
        zero_idx = idx
        break
if zero_idx == 0 and A[0] < 0:
  print(A[N-K])
elif zero_idx == 0 and A[0] > 0:
  print(A[K-1])
else:
  ans = 3*10**5
  for plus in range(N-zero_idx+1):
    if plus > K:
        break
    minus = K - plus
    if minus > zero_idx:
    	continue
    p_dis = A[zero_idx+plus-1]
    if plus == 0:
        p_dis = 0
    m_dis = A[zero_idx-minus]
    if minus == 0:
        m_dis = 0
    if p_dis >= abs(m_dis):
        ans = min(ans, p_dis+abs(m_dis)*2)
    else:
        ans = min(ans, abs(m_dis)+p_dis*2)
  print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
zero_idx = 0
for idx in range(N):
    if A[idx] > 0:
        zero_idx = idx
        break
ans = 3*10**5
for plus in range(N-zero_idx+1):
    if plus > K:
        break
    minus = K - plus
    if minus > zero_idx:
    	continue
    p_dis = A[zero_idx+plus-1]
    if plus == 0:
        p_dis = 0
    m_dis = A[zero_idx-minus]
    if minus == 0:
        m_dis = 0
    if p_dis >= abs(m_dis):
        ans = min(ans, p_dis+abs(m_dis)*2)
    else:
        ans = min(ans, abs(m_dis)+p_dis*2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
zero_idx = 0
for idx in range(N):
    if A[idx] > 0 and idx > 0:
        zero_idx = idx - 1
        break
ans = 3*10**5
for plus in range(N-zero_idx-1):
    if plus > K:
        break
    minus = K - plus
    p_dis = A[N-plus-1]
    m_dis = A[plus-minus]
    if p_dis >= abs(m_dis):
        ans = min(ans, p_dis+abs(m_dis)*2)
    else:
        ans = min(ans, abs(m_dis)+p_dis*2)
print(ans)

Submission status: Wrong Answer

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

# min( 左端に到達し右へ, 右端に到達し左へ)
ans=max(x)-min(x)
for i in range(N-K+1):
    l=abs(x[i])+abs(x[i]-x[i+K-1])
    r=abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans=min(ans,min(l,r))
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC107 C
n,k = map(int,input().split())
x = list(map(int,input().split()))

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC107 C
n,k = map(int,input().split())
x = list(map(int,input().split()))

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1e13
for i in range(n-k):
    l = x[i]
    r = x[i+k-1]
    ans = min(ans, abs(r)+abs(l-r))
    ans = min(ans, abs(l)+abs(r-l))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1e13
for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    ans = min(ans, abs(l)+abs(r-l))
print(ans)

Submission status: Wrong Answer

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

time = 10 ** 5
for i in range(N - K + 1):
    if candle[i] < 0 and candle[i + K-1] > 0:
        if abs(candle[i]) < candle[i + K-1]:
            kouho = 2 * abs(candle[i]) + candle[i + K-1]
        else:
            kouho = abs(candle[i]) + 2 * candle[i + K-1]
    else:
        kouho = max(abs(candle[i]), abs(candle[i + K-1]))

    if kouho < time:
        time = kouho
print(time)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
candle = [int(x) for x in input().split()]
candle = sorted(candle)
time = 10 ** 5
for i in range(N - K + 1):
    if candle[i] < 0 and candle[i + K-1] > 0:
        if abs(candle[i]) < candle[i + K-1]:
            kouho = 2 * abs(candle[i]) + candle[i + K-1]
        else:
            kouho = abs(candle[i]) + 2 * candle[i + K-1]
    else:
        kouho = max(abs(candle[i]), abs(candle[i + K-1]))

    if kouho < time:
        time = kouho
print(time)

Submission status: Wrong Answer

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

from collections import Counter

def comp(k, lst):
    temp =10**9
    for i in range(N-k+1):
        temp = min(temp, min(2*lst[i]+lst[i+k-1], lst[i]+2*lst[i+k-1]))
        
    return temp

counter = Counter(X)

if counter[0]==0:
    X.append(0)
    X = list(map(abs, sorted(X)))
    print(comp(K+1, X))
    
else:
    X = list(map(abs, X))
    print(comp(K, X))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
if 0 in x:
    x.remove(0)
    k -= 1
    n -= 1

if n == 0:
    print(0)
else:
    si = bisect_left(x, 0)

    ans = (x[-1] - x[0]) * 2
    if si + k - 1 < len(x):
        ans = min(ans, x[si + k - 1])
    if si - k >= 0:
        ans = min(ans, abs(x[si - k]))

    for i in range(n):
        tmp_ans = 0
        if i < si and i + k -1 < len(x):
            tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
            ans = min(ans, tmp_ans)
        elif i - k + 1 >= 0:
            tmp_ans = x[i] * 2 + abs(x[i - k + 1])
            ans = min(ans, tmp_ans)

    print(ans)