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

N, K = map(int, raw_input().split())
x = map(int, raw_input().split())
res = [sys.maxint] * N

pos = 0
f_pos = bisect.bisect_left(x, 0)
b_pos = f_pos - 1
result = sys.maxint
if f_pos + K - 1 < N:
	result = min(x[f_pos + K - 1], result)
if b_pos - K + 1 >= 0:
	result = min(x[b_pos - K + 1], result)
for i in range(K):
	if f_pos + i < N and f_pos - K + i >= 0:
		result = min(x[f_pos + i] * 2 - x[f_pos - K + i + 1], result)
	if b_pos - i >= 0 and b_pos + K - i < N:
		result = min(-x[b_pos - i] * 2 + x[b_pos + K - i - 1], result)
print result

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

if listMap[0] >= 0 or listMap[-1] <= 0:
    sorted(listMap)
    minMin = listMap[K - 1]

else:

    for j in range(N - K):
        L = j
        R = j + K - 1
        sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
        sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
        sum = min(sumL, sumR)
        if sum < minMin:
            minMin = sum

if minMin == float("inf"):
    minMin = listMap[0]

print(minMin)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# if 0 in x:
#     x.remove(0)
#     k -= 1

if k==0:
    print(0)

else:
    flag01 = 0
    flag02 = 0
    
    i=0
    if x[0]>=0:
        print(x[k-1])
    else:
        while(x[i]<0 and i+k-1<n):
            xtmp = x[i:i+k]
            if flag01==1:
                MIN = min(MIN, abs(xtmp[-1]))
                break

            if xtmp[0]<0 and xtmp[1]>0:
                flag01 = 1
#             if i<n-k:
#                 if x[i+k]>0 and x[i+k-1]<0:
#                     flag02=1

#             if flag02==1:
#                 MIN = min(MIN, abs(xtmp[0]))             

            if i==0:
                MIN = abs(xtmp[0])+2*abs(xtmp[-1])

            MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
            MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))

            i+=1

        print(MIN)

Submission status: Wrong Answer

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

lefts = []
rights = []
for i in range(n):
    if len(rosokus[i : k + i]) == k:
        lefts.append(rosokus[i])
        rights.append(rosokus[k + i - 1])
    else:
        break

min_dis = 9999999999
for left, right in zip(lefts, rights):
    if left < right:
        if abs(left - right) + abs(left) < min_dis:
            min_dis = abs(left - right) + abs(left)
    else:
        if abs(left - right) + abs(right) < min_dis:
            min_dis = abs(left - right) + abs(right)
if min_dis == 9999999999:
    min_dis = 0
print(min_dis)

Submission status: Wrong Answer

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

lefts = []
rights = []
for i in range(n):
    if len(rosokus[i : k + i]) == k:
        lefts.append(rosokus[i])
        rights.append(rosokus[k + i - 1])
    else:
        break

min_dis = 9999999999
for left, right in zip(lefts, rights):
    if left < right:
        if abs(left - right) + abs(left) < min_dis:
            min_dis = abs(left - right) + abs(left)
        else:
            min_dis = abs(left - right) + abs(right)
if min_dis == 9999999999:
    min_dis = 0
print(min_dis)

Submission status: Wrong Answer

Solution code:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)

    x = x[o - K:o + K]
    list(set(x))
    x.sort()

    best = 999999999999
    u = len(x)
    for i in range(u):
        if u < i + K:
            break

        hani = x[i:i + K]
        if hani[0] < 0 and hani[-1] < 0:
            best = min(best, abs(hani[0]))
        elif hani[0] < 0 <= hani[-1]:
            best = min(best, 2 * abs(hani[0]) + hani[-1])
        elif 0 <= hani[0] and 0 <= hani[-1]:
            best = min(best, hani[-1])

    print(best)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 25 18:21:57 2018
ABC107C
@author: maezawa
"""

n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**8
for l in range(n-k+1):
    r = l+k-1
    ans1 = abs(x[l]) + abs(x[r]-x[l])
    ans2 = abs(x[r]) + abs(x[r]-x[l])
    ans0 = min(ans1, ans2)
    if ans > ans0:
        ans = ans0
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 25 18:21:57 2018
ABC107C
@author: maezawa
"""

n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**8
for l in range(n-k+1):
    r = l+k-1
    ans1 = abs(x[l]) + abs(x[r]-x[l])
    ans2 = abs(x[r]) + abs(x[r]-x[l])
    ans0 = min(ans1, ans2)
    if ans > ans0:
        ans = ans0
if n==k:
    ans = x[k-1] - x[0]
print(ans)

Submission status: Wrong Answer

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

lefts = []
rights = []
for i in range(n):
    if len(rosokus[i : k + i]) == k:
        lefts.append(rosokus[i])
        rights.append(rosokus[k + i - 1])
    else:
        break

min_dis = 20000
for left, right in zip(lefts, rights):
    for start_side in [left, right]:
        if abs(left - right) + abs(start_side) < min_dis:
            min_dis = abs(left - right) + abs(start_side)
print(min_dis)

Submission status: Wrong Answer

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

result = []
for i in range(N-(K-1)):
    start = i
    end = i + (K -1)
    result.append(abs(x_list[start] + abs(x_list[end] - x_list[start])))
    result.append(abs(x_list[end]) +  abs(x_list[end] - x_list[start] ))
print(min(result))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 25 18:21:57 2018
ABC107C
@author: maezawa
"""

n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**8
for l in range(n-k):
    r = l+k-1
    ans1 = abs(x[l]) + abs(x[r]-x[l])
    ans2 = abs(x[r]) + abs(x[r]-x[l])
    ans0 = min(ans1, ans2)
    if ans > ans0:
        ans = ans0
if n==k:
    ans = x[k-1] - x[0]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

for j in range(N - K):
    L = j
    R = j + K - 1
    sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
    sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
    sum = min(sumL, sumR)
    if sum < minMin:
        minMin = sum

if minMin == float("inf"):
    minMin = listMap[0]

print(minMin)

Submission status: Wrong Answer

Solution code:
def run(n, k, x):
    # ri = 0
    # li = k-1
    # if n == 1:
    #    return x[0]
    # x2 = x
    ret = abs(x[0]) + abs(x[0] - x[k-1])
    for i in range(n-k):
        ri = i
        li = i + k - 1
        tmp = min(abs(x[ri]) + abs(x[ri] - x[li]), abs(x[li]) + abs(x[ri] - x[li]))
        ret = min(ret, tmp)
        '''
        if x[ri] > x[i]:
        elif x[ri] == x[i]:
            if x[ri+1] < x[i-1]:
                ri = i-k+1
                li = i
        else:
            if x2[li] <= 0:
                return x[ri]
            elif x2[ri] >= 0:
                return x[li]
            elif x[ri] > x[li]:
                return x[ri] + 2*x[li]
            else:
                return 2*x[ri] + x[li]
    if x2[li] <= 0:
        return x[ri]
    elif x2[ri] >= 0:
        return x[li]
    elif x[ri] > x[li]:
        return x[ri] + 2*x[li]
    else:
        return 2*x[ri] + x[li]
        '''
    return ret


def read_line():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    return (n, k, x)


def main():
    n, k, x = read_line()
    print(run(n, k, x))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

for j in range(N - K):
    L = j
    R = j + K - 1
    sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
    sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
    sum = min(sumL, sumR)
    if sum < minMin:
        minMin = sum

print(minMin)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split(" "))
X=list(map(int, input().split(" ")))
import bisect
maxvalue=100000000000000000000000
place=bisect.bisect_left(X,0)

if k<place:
    X=X[place-k:]
if k<n-place:
    X=X[:-(n-place-k)]

#print(X)
#print(place)
for i in range(len(X)-k+1):
    low=X[i]
    high=X[i+k-1]
    if high*low<0:
        low=abs(low)
        high=abs(high)
        value=min(low,high)*2+max(low,high)
    else:
        low=abs(low)
        high=abs(high)
        value=low+high
    #print(value)
    if value<maxvalue:
        maxvalue=value
print(maxvalue)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
a = []
a2 = []
for i in x:
    if i < 0:
        a2.append(2 * abs(i))
    else:
        a.append(i)

if len(a2) == 0:
    print(a[K-1])
    exit()
a2 = sorted(a2)
c1 = float('inf')

for i, v in enumerate(a):
    if K - i >= len(a2):
        continue
    c1 = min(c1, v + a2[K - i])

b = []
b2 = []
for i in x:
    if i > 0:
        b2.append(2 * abs(i))
    else:
        b.append(-i)
b = sorted(b)

if len(b2) == 0:
    print(b[K-1])
    exit()

c2 = float('inf')
for i, v in enumerate(b):
    if K - i >= len(b2):
        continue
    c2 = min(c2, v + b2[K - i])

print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
a = []
a2 = []
for i in x:
    if i < 0:
        a2.append(2 * abs(i))
    else:
        a.append(i)

if len(a2) == 0:
    print(a[K-1])
    exit()
a2 = sorted(a2)
c1 = float('inf')

for i, v in enumerate(a):
    if K - i >= len(a2):
        break
    c1 = min(c1, v + a2[K - i])

b = []
b2 = []
for i in x:
    if i > 0:
        b2.append(2 * abs(i))
    else:
        b.append(-i)
b = sorted(b)

if len(b2) == 0:
    print(b[K-1])
    exit()

c2 = float('inf')
for i, v in enumerate(b):
    if K - i >= len(b2):
        break
    c2 = min(c2, v + b2[K - i])

print(min(c1,c2))

Submission status: Wrong Answer

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

o = 0
if x[N-1] <= 0:
	print(x[N-K])
	exit()
if x[0] >= 0:
	print(x[K-1])
	exit()

while x[o] < 0:
	o += 1
	if o == N-1:
		break

MIN = float("inf")
for i in range(-K-1,K+1,1):
	l = o - i+1
	r = o + K-i
	if not(l < 0 or l >= N or r < 0 or r >= N):
		cur1 = 2* abs(x[l]) + abs(x[r])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
print(MIN)

Submission status: Wrong Answer

Solution code:
def main():
    """
    1 <= N <= 10^5
    1 <= K <= N
    |xi| <= 10^8
        -10^8 <= xi <= 10^8
    xs: sorted
    """
    N, K = map(int, input().split())
    * x, = map(int, input().split())
    ans = WA(N, K, x)
    print(ans)


def editorial(N, K, x):
    """
    start: 0
    all x >= 0, 近傍K
    all x <= 0, 近傍K
    左に行ってから折り返してstat: 0 に戻り、最低限の右をとる
        |min left - 0| * 2
        |max right|

    累積和？浮かんだ
    startとの差分浮かんだ
    左と右に分ける
    """
    ans = float("inf")

    return ans


def WA(N, K, x):
    l, r = 0, 0
    for i in range(N-1):
        if x[i] == 0:
            l, r = i, i
            break
        elif x[i+1] == 0:
            l, r = i+1, i+1
            break
        elif x[i] < 0 < x[i+1]:
            l, r = i, i+1
            break

    if x[-1] < 0:
        l, r = N - 1, N - 1

    ans = float("inf")
    # 左へのルート
    # print((l, r))
    for i in range(l+1):
        t = abs(x[i])
        right = -1
        rem = 0
        fire = abs(i - l) + 1
        if fire == K:
            ans = min(ans, abs(x[i]))
        elif fire > K:
            # out of index
            pass
        else:
            # 右へ向かうのでstartへ戻る
            t *= 2
            rem = K - fire
            right = l + rem
            if right < N:
                ans = min(ans, t + x[right])

        # print(i, l, right, fire, rem, ans, sep="\t")

    # 右へのルート
    # print("----right----")
    for i in range(l, N):
        t = abs(x[i])
        fire = abs(i - r) + 1
        rem = 0
        left = -1
        if fire == K:
            ans = min(ans, abs(x[i]))
        elif fire > K:
            # out of index
            pass
        else:
            # 右へ向かうのでstartへ戻る
            t *= 2
            rem = K - fire
            left = l - rem
            if left >= 0:
                ans = min(ans, t + abs(x[left]))

        # print(left, i, r, fire, rem, ans, sep="\t")

    return ans


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans=0
for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i])

    if i==0 or ans>tans:
        ans=tans
    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = [i for i in map(int, input().split())]
i = 0
min_time =2e8
while i < N-K+1:
    if x[i]<0 and x[i+K-1]<0:
        time = -x[i]
    if x[i]<0 and x[i+K-1]>=0:
        time1 = -2*x[i] + x[i+K-1]
        time2 = -x[i] + 2*x[i+K-1]
        if time1< time2:
            time = time1
        else:
            time = time2
    if x[i] >= 0 and x[i+K-1] >=0:
        time = x[i+K-1]
    if time < min_time:
        min_time = time
    i += 1
print(min_time)

Submission status: Wrong Answer

Solution code:
import bisect
N,K = map(int, input().split())
x = list(map(int, input().split()))
zero_p = bisect.bisect_left(x,0)
score = float('inf')
for i in range(K):
  lst = x[zero_p - i:zero_p + K - i]
  minus = 0
  plus = 0
  for l in lst:
    if l >= 0:
      plus = max(plus, l)
    else:
      minus = min(minus,l)
  score = min(score, abs(plus * 2) + abs(minus), abs(plus) + abs(minus * 2))
print(score)

Submission status: Wrong Answer

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

if x_list[0] >= 0:
    for i in range(N - K):
        x_list.pop(-1)
    print(max(x_list))
elif x_list[-1] <= 0:
    for i in range(N - K):
        x_list.pop(0)
    print(abs(min(x_list)))

else:
    for i in range(N-K):
        if x_list[0] >= 0:
            x_list.pop(-1)
        elif x_list[-1] <= 0:
            x_list.pop(0)
        else:
            p = abs(x_list[-1] - x_list[-2])
            n = abs(x_list[0] - x_list[1])
            if p > n:
                x_list.pop(-1)
            else:
                x_list.pop(0)
    if x_list[0] >= 0:
        print(max(x_list))
    else:
        if x_list[-1] >= abs(x_list[0]):
            print(x_list[-1] - x_list[0]*2)
        else:
            print(2*x_list[-1] + abs(x_list[0]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
inf = float('inf')

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

iz = False
for i in range(N):
	if x[i] >= 0:
		if x[i] == 0:
			K -= 1
		if i > 0 and abs(x[i-1]) < abs(x[i]):
			i -= 1
		break

def cmp_np(x,y):
	if (x > 0 and y > 0) or (x < 0 and y < 0):
		return True
	else:
		return False

r = i
l = i
cur = i
cost = abs(x[i])
while K > 0:
	if l-1 >= 0:
		ln = abs(x[l-1]-x[cur])
	else:
		ln = inf

	if r+1 < N:
		rn = abs(x[r+1]-x[cur])
	else:
		rn = inf

	if ln < rn:
		cur = l-1
		l = l-1
		cost += ln
	else:
		cur = r+1
		r = r+1
		cost += rn
	K -= 1

print(cost)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
a = []
a2 = []
for i in x:
    if i < 0:
        a2.append(2 * abs(i))
    else:
        a.append(i)

if len(a2) == 0:
    print(a[K-1])
    exit()
a2 = sorted(a2)
c1 = float('inf')

a2l = len(a2) - 1
for i, v in enumerate(a):
    i = i + 1
    c1 = min(c1, v + a2[min(a2l,K - i - 1)])

b = []
b2 = []
for i in x:
    if i > 0:
        b2.append(2 * abs(i))
    else:
        b.append(-i)
b = sorted(b)

if len(b2) == 0:
    print(b[K-1])
    exit()

c2 = float('inf')
for i, v in enumerate(b):
    i = i + 1
    c2 = min(c2, v + b2[min(K - i - 1,len(b2)-1)])

print(min(c1,c2))

Submission status: Wrong Answer

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

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

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

num_p = sum(y>-1 for y in x)#正
num_m = sum(y<0 for y in x)#負

time = []

if num_m == 0:
    time.append(x[K-1])
elif num_p == 0:
    time.append(abs(x[N-K]))
else:
    if K < num_p or K == num_p:
        migi = num_m + K -1
        time.append(x[migi])
        migi = migi - 1
    else:
        migi = int(len(x)-1)
    
    hidari = num_m - 1
    for i in range(migi,num_m-1,-1):
        time.append(x[migi]+(abs(x[hidari])*2))
        hidari = hidari -1
        migi = migi -1

    x.reverse()
    if K < num_m or K == num_m:
        migi = num_p + K -1
        time.append(abs(x[migi]))
        migi = migi - 1
    else:
        migi = int(len(x)-1)
    
    hidari = num_p - 1
    for i in range(migi,num_m-1,-1):
        time.append(abs(x[migi])+(abs(x[hidari])*2))
        hidari = hidari -1
        migi = migi -1    
    
        

print(min(time))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ans = 10000000000
x = list(map(int, input().split()))
s = []
if N == 1:
	print(x[0])
else:
	for i in range(1, N):
		s.append(abs(x[i] - x[i-1]))
	for i in range(len(x)-K):
		ans = min(sum(s[i+1:i+K]) + abs(x[i+1]), ans)
	print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x_list = list(map(int, input().split()))
mainasu = []
purasu = []
p = 0
for i in range(N):
    if x_list[i] > p:
        purasu.append(x_list[i])
    else:
        mainasu.append(x_list[i])

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split(" "))
la=input().split(" ")
for i in range(a):
    la[i]=int(la[i])
lp=[x for x in la if x>=0]
lm=[x for x in la if x<0]
lb=[]
if len(lp)==0:
    print(abs(lm[-(b-1)]))
    exit()
elif len(lm)==0:
    print(lp[b-1])
    exit()
else:
    for i in range(b):
        s=0
        if len(lm)==i:
            break
        else:
            for j in range(i+1):
                s+=abs(lm[-(j+1)]*2)
            if b-i-2 > len(lp)-1:
                pass
            else:
                s+=lp[b-i-2]
                lb.append(s)
    for i in range(b):
        s=0
        if len(lp)==i:
            break
        else:
            for j in range(i+1):
                s+=lp[j]*2
            if b-i-1 > len(lm)-1:
                pass
            else:
                s+=abs(lm[b-i-1])
                lb.append(s)
print(min(lb))

Submission status: Wrong Answer

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

x_abs=list(map(abs, x))
x_abs.sort()
maximum_num =x_abs[K-1]
#print(x)
#print(maximum_num)
x_max=[i for i in x if i <= maximum_num]
x=[i for i in x_max if i > -maximum_num]
x.append(0)
if min(x)<0 and max(x)>0:
    print(abs(min(x))*2+max(x))
elif min(x)<0 and max(x)<0:
    print(abs(min(x)))
else:
    print(max(x))

Submission status: Wrong Answer

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

ans=0
for i in range(N-K+1):
    tans=abs(X[0]-X[-1])+abs(X[0])

    if i==0 or ans>tans:
        ans=tans
    
print(ans)

Submission status: Wrong Answer

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

def binarySearch(D, target):
    left = 0
    right = len(D)
    while left < right:
        mid = (left + right) // 2
        if D[mid] == target:
            return mid, 0
        elif target < D[mid]:
            right = mid
        else:
            left = mid + 1
    return mid, 1

# 全てが正の場合
if x[0] >= 0:
    re = x[K-1]
# 全てが負の場合
elif x[-1] <= 0:
    re = abs(x[-K]) # 絶対値
else:
    zero, plus = binarySearch(x, 0)
    s = zero + plus
    e = s + K +1
    if e >= N:
        s = s - (e - N) + 1
        e = N - 1
    d_plus = x[e]
    d_minor =  abs(min(x[s], 0)) 
    d_min = 2 * min(d_plus, d_minor) + max(d_plus, d_minor)
    while True:
        d_plus = max(x[e], 0)
        d_minor =  abs(min(x[s], 0)) 
        d = 2 * min(d_plus, d_minor) + max(d_plus, d_minor)
        d_min = min(d_min, d)
#        print(x[s], x[e], d_min)
        e -= 1
        s -= 1
        if e < zero + plus  or s < 0:
            break
    re = d_min
    
print(re)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
sum=0
xx=[]
plus=[]
minus=[]
if x[0]<0 and x[n-1]<0 or x[0]>=0 and x[n-1]>=0:
    print(x[k-1])
else:
    for i in range(n):
        if x[i]<0:
            minus.append(x[i])
        else:
            plus.append(x[i])
    if abs(min(minus))>max(plus):
        minus.reverse()
        print(max(plus)+minus[(k-len(plus))-1]*-2)
    else:
        print(abs(min(minus))+plus[k-len(minus)-1])

Submission status: Wrong Answer

Solution code:
import copy

aa = input().strip().split()
n = int(aa[0])
k = int(aa[1])
x = [int(s) for s in input().strip().split()]
q = [[0, 0, 0, [False for _ in range(n)]]]

while 0 < len(q):
    qq = q.pop(0)
    xx = qq[0]
    c = qq[1]
    kk = qq[2]
    y = qq[3]

    if kk == k:
        print(c)
        exit(0)

    for i in range(len(x)):
        if (i == 0 or x[i - 1] <= xx) and xx <= x[i]:
            if 0 < i and not y[i - 1]:
                y_ = copy.deepcopy(y)
                y_[i - 1] = True
                q.append([x[i - 1], c + xx - x[i - 1], kk + 1, y_])

            if not y[i]:
                y_ = copy.deepcopy(y)
                y_[i] = True
                q.append([x[i], c + x[i] - xx, kk + 1, y_])

Submission status: Wrong Answer

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

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

ans=10**7
for i in range(len(x)-k+1):
    if x[i]<0 and x[i+k-1] <0:
        temp=-x[i]
        if ans > temp:
            ans=temp
    elif x[i]>0 and x[i+k-1] >0:
        temp=x[i+k-1]
        if ans > temp:
            ans=temp
    else:
        temp=-x[i]+x[i+k-1]
        if -x[i] <= x[i+k-1]:
            temp+=-x[i]
        else:
            temp+=x[i+k-1]
        if ans > temp:
            ans=temp
print(ans)

Submission status: Wrong Answer

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

from math import floor

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    min_time = 1e9
    origin = get_origin(xs, n)

    if origin <= 0:
        print(xs[k - 1])
    elif origin >= n - 1:
        print(abs(xs[-k]))
    else:
        for i in range(max(origin - k + 1, 0), min(origin + 1, n - k)):
            time = abs(xs[i + k - 1] - xs[i]) + min(abs(xs[i]), abs(xs[i + k - 1]))
            min_time = min(time, min_time)

        print(time)


def get_origin(xs, n):
    if xs[0] == 0:
        return 0
    elif xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n
    else:
        l = 0
        r = n
        while True:
            if r - l < 10:
                for i in range(l, r):
                    if xs[i] == 0 or xs[i + 1] > 0:
                        return i

            m = floor((r - l) / 2 + l)
            if xs[m] == 0:
                return m
            elif xs[m] < 0:
                if m < n and xs[m + 1] > 0:
                    return m
                else:
                    l = m
            else:
                if m > 0 and xs[m - 1] < 0:
                    return m - 1
                else:
                    r = m


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    """
    1 <= N <= 10^5
    1 <= K <= N
    |xi| <= 10^8
        -10^8 <= xi <= 10^8
    xs: sorted
    """
    N, K = map(int, input().split())
    * x, = map(int, input().split())
    ans = f(N, K, x)
    print(ans)


def f1(N, K, x):
    """
    start: 0
    all x >= 0, 近傍K
    all x <= 0, 近傍K
    左に行ってから折り返してstat: 0 に戻り、最低限の右をとる
        |min left - 0| * 2
        |max right|

    累積和？浮かんだ
    startとの差分浮かんだ
    左と右に分ける
    """


def f(N, K, x):
    l, r = 0, 0
    for i in range(N-1):
        if x[i] == 0:
            l, r = i, i
            break
        elif [i+1] == 0:
            l, r = i+1, i+1
            break
        elif x[i] < 0 < x[i+1]:
            l, r = i, i+1
            break

    ans = float("inf")
    # 左へのルート
    # print((l, r))
    for i in range(l+1):
        t = abs(x[i])
        right = -1
        rem = 0
        fire = abs(i - l) + 1
        if fire == K:
            ans = min(ans, abs(x[i]))
        elif fire > K:
            # out of index
            pass
        else:
            # 右へ向かうのでstartへ戻る
            t *= 2
            rem = K - fire
            right = l + rem
            if right < N:
                ans = min(ans, t + x[right])

        # print(i, l, right, fire, rem, ans, sep="\t")

    # 右へのルート
    # print("----right----")
    for i in range(l, N):
        t = abs(x[i])
        fire = abs(i - r) + 1
        rem = 0
        left = -1
        if fire == K:
            ans = min(ans, abs(x[i]))
        elif fire > K:
            # out of index
            pass
        else:
            # 右へ向かうのでstartへ戻る
            t *= 2
            rem = K - fire
            left = l - rem
            if left >= 0:
                ans = min(ans, t + abs(x[left]))

        # print(left, i, r, fire, rem, ans, sep="\t")

    return ans


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int,input().split())
o=list(map(int,input().split()))
a=1e9

p=bisect.bisect_left(o,0)
for l in range(k):
  r=l+k-1
  if 0<=r<n:
    if o[l]>=0:
      v=abs(o[r])
    elif o[r]<=0:
      v=abs(o[l])
    else:
      c=abs(o[l])+2*abs(o[r])
      b=2*abs(o[l])+abs(o[r])
      v=min(c,b)
  a=min(a,v)
print(a)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    cplus = 0
    cminus = 0
    czero = 0
    for j in range(len(L)) :

        if L[j] > 0 :
            cplus += 1
        elif L[j] < 0 :
            cminus += 1
        else :
            czero += 1
    if (cplus == K-czero) or (cminus == K-czero):
        ANS.append(max(abs(min(L)),max(L)))
    else :
        ANS.append(min(abs(min(L))*2+abs(max(L)),abs(max(L))*2+abs(min(L))))
print(min(ANS))
print(ANS)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = [i for i in map(int, input().split())]
i = 0
min_time =1e8
while i < N-K+1:
    if x[i]<0 and x[i+K-1]<0:
        time = -x[i]
    if x[i]<0 and x[i+K-1]>=0:
        time1 = -2*x[i] + x[i+K-1]
        time2 = -x[i] + 2*x[i+K-1]
        if time1< time2:
            time = time1
        else:
            time = time2
    if x[i] >= 0 and x[i+K-1] >=0:
        time = x[i+K-1]
    if time < min_time:
        min_time = time
    i += 1
print(min_time)

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int,input().split())
o=list(map(int,input().split()))
a=1e9

p=bisect.bisect_left(o,0)
for l in range(k):
  r=l+k-1
  if 0<=r<n:
    if o[l]>=0:
      v=abs(o[r])
    elif o[r]<=0:
      v=abs(o[l])
    else:
      a=abs(o[l])+2*abs(o[r])
      b=2*abs(o[l])+abs(o[r])
      v=min(a,b)
  a=min(a,v)
print(a)

Submission status: Wrong Answer

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

    if not 0 in x:
        x.append(0)
        x.sort()
        N += 1
    else:
        K = K - 1
    
    start = x.index(0)
    if start == 0:
        return x[K]
    if start == N-1:
        return x[N-K-1]

    time = []
    for i in range(start-K, 0):
        if start+i < 0 or start+K+i >= N:
            continue
        else:
            t = x[start] - x[start+i]
            t += x[start+K+i] - x[start+i]
            time.append(t)
    for i in range(0, start+K+1):
        if start+i >= N or start+i-K<0:
            continue
        else:
            t = x[start+i] - x[start]
            t += x[start+i] - x[start+i-K]
            time.append(t)

    if time != []:
        return min(time)
    else:
        return 0

if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
x = list(map(int, input().split(' ')))
plusList = []; minusList = []

for i in x:
    if i == 0:
        n -= 1
    elif i > 0:
        plusList.append(i)
    else:
        minusList.insert(0, i)

if len(plusList) == 0 and len(minusList) == 0:
    print(0)
elif len(plusList) == 0:
    if minusList == [0]:
        print(0)
    else:
        print(-minusList[k - 1])
elif len(minusList) == 0:
    print(plusList[k - 1])
else:
    minCost = max(plusList) * (-min(minusList)) * 2

    # minus start
    for i in range(k):
        if len(minusList) <= i or len(plusList) <= k - i - 1:
            continue

        cost = -minusList[i] * 2
        if k - i - 1 >= 0:
            cost += plusList[k - i - 2]
        if minCost > cost:
            minCost = cost
            
    # plus start
    for i in range(k):
        if len(plusList) <= i or len(minusList) <= k - i - 1:
            continue
        
        cost = plusList[i] * 2
        if k - i - 1 >= 0:
            cost += -minusList[k - i - 2]
        if minCost > cost:
            minCost = cost

    print(minCost)

Submission status: Wrong Answer

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

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

plus = [0]
minus = [0]

for x_ in x:
    if x_ < 0:
        minus.append(x_ * -1)
    if x_ > 0:
        plus.append(x_)
    if x_ == 0:
        K -= 1

plus.sort()
minus.sort()


ans = 100000000

for i in range(len(plus)):
    if 0 <= K-i < len(minus):
        ans = min(ans,plus[i]*2 +minus[K-i])
for i in range(len(minus)):
    if 0 <= K-i < len(plus):
        ans = min(ans,minus[i]*2 +plus[K-i])
print(ans)