Problem:


Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Wrong Answer

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

#if k<place:
#    X=X[place-k:]
#elif k<n-place-1:
#    X=X[:]

#print(place)
for i in range(n-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:

def abc107_c():
    import sys
    import numpy as np
    input = sys.stdin.readline

    n, k = map(int, input().split())
    x = np.array(list(map(int, input().split())))
    if x[0] >= 0:
        return x[k - 1]
    elif x[-1] <= 0:
        return x[n - k]
    else:
        t = np.sum(x < 0)
        if x[t] == 0:
            k = k - 1
            x = np.delete(x, [t])

        time = np.inf
        if t >= k:
            time = x[t - k]
        if (n - t) >= k:
            time = min(time, x[t + k - 1])

        for i in range(max(1, k - (n - t)), min(t + 1, k)):
            left = -x[t - i]
            right = x[t + k - i - 1]

            tmp = min(left, right) * 2 + max(left, right)
            if tmp < time:
                time = tmp

        return time


print(abc107_c())

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]

    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:
def main():
    n, k = raw_input().split()
    x = [int(i) for i in raw_input().split()]

    sorted_x = sorted(x, key= lambda i: abs(i))

    maxv = max(sorted_x[:int(k)])
    minv = min(sorted_x[:int(k)])

    if minv < 0 and maxv < 0:
        print(abs(minv))
    elif 0 < minv and 0 < maxv:
        print(maxv)
    else:
        if abs(minv) < abs(maxv):
            print(abs(minv)*2 + abs(maxv))
        else:
            print(abs(maxv)*2 + abs(minv))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

minus=[]
for i in range (N):
    if a[i]<0:
        minus.append(a[i])
plus=a[len(minus):]

if len(minus)==0:
    print(plus[K-1])
    sys.exit()
if len(plus)==0:
    minus=minus[::-1]
    print(minus[K-1])
    sys.exit()

if K==1:
    minus=minus[::-1]
    if len(plus)==0:
        plus.append(10**18)
    if len(minus)==0:
        minus.append(10**18)
    print(min(plus[0],abs(minus[0])))
    sys.exit()
     
data=[i * 2 for i in minus]+plus
data2=minus+[i * 2 for i in plus]
data_min=10**18
for i in range (N-K+1):
    if data_min >((abs(min(data[i],0)))+abs(data[i+K-1])):
        data_min=((abs(min(data[i],0)))+abs(data[i+K-1]))
for i in range (N-K+1):
    if data_min >((abs(min(data2[i],0)))+abs(data2[i+K-1])):
        data_min=((abs(min(data2[i],0)))+abs(data2[i+K-1]))
print(data_min)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left


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

zi = bisect_left(x, 0)
li = zi - k if zi - k >= 0 else 0
ri = zi if zi + k - 1 < n else n - k

mind = 999999999999999999999999999999999
for i in range(li, ri):
    y = x[i:i+k]
    d = min(-2*y[0] + y[-1], -y[0] + 2*y[-1])
    if y[0] > 0:
        d = y[-1]
    if y[-1] < 0:
        d = -y[0]
    mind = d if d < mind else mind

print(mind)

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])
		cur2 = abs(x[l]) + 2*abs(x[r])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)

Submission status: Wrong Answer

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

X=sorted(X)

ans=0
for i in range(N-K+1):
    temp=X[i:i+K]
    tans=abs(temp[0]-temp[-1])+abs(temp[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 - 1 or s < 0:
            break
    re = d_min
    
print(re)

Submission status: Wrong Answer

Solution code:

N, K = map(int, input().split())
candles = list(map(int, input().split()))
if 0 in candles:
    K -= 1
minus = [abs(num) for num in candles if num < 0][::-1]
plus = [num for num in candles if num > 0]

ans = 9999999

# check plus or minus
if K == 0:
    ans = 0
else:
    if len(minus) >= K:
        ans = min(ans, minus[K - 1])
    if len(plus) >= K:
        ans = min(ans, plus[K - 1])

# check both
if len(minus) != 0 and len(plus) != 0:
    n_candle = 0
    index_m = index_p = 0
    for i in range(100000010):

        if index_m < len(minus):
            if minus[index_m] == i:
                n_candle += 1
                if n_candle >= K:
                    index_p -= 1
                    break
                index_m += 1

        if index_p < len(plus):
            if plus[index_p] == i:
                n_candle += 1
                if n_candle >= K:
                    index_m -= 1
                    break
                index_p += 1

    # calc
    if minus[index_m] > plus[index_p]:
        ans = min(ans, 2 * plus[index_p] + minus[index_m])
    else:
        ans = min(ans, plus[index_p] + 2 * minus[index_m])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

for i, n in enumerate(p):
  if n >= 0:
    zero_i = i
    break
  zero_i = len(p)

n_list = p[:zero_i]
p_list = p[zero_i:]

if len(n_list) > k:
  n_list = n_list[-k:]

if len(p_list) > k:
  p_list = p_list[:k]

if len(n_list) == 0:
  print(p_list[k-1])
  sys.exit()
elif len(p_list) == 0:
  print(abs(n_list[-k]))
  sys.exit()

# e.g. n_list = [-30,-10]
if len(n_list) == k:
  min_move = abs(n_list[0])
else:
  n_min = abs(n_list[0]) + (p_list[k-len(n_list)-1]*2)
  p_min = (abs(n_list[0])*2) + p_list[k-len(n_list)-1]
  min_move = min(n_min, p_min)

for i, n in enumerate(n_list):
  if (k-(i+1)) > len(p_list):
    break

  if i == len(n_list)-1:
    min_move = min(min_move, p_list[-1])
  else:
    n_min = abs(n_list[i+1]) + (p_list[i+1]*2)
    p_min = (abs(n_list[i+1])*2) + p_list[i+1]
    min_move = min(min_move, n_min, p_min)

print(min_move)

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
    
print(min(time))

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,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])
		cur2 = abs(x[l]) + 2*abs(x[r])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)

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:
        print(x_list[-1] - x_list[0]*2)

Submission status: Wrong Answer

Solution code:
def abc107_c():
    import sys
    import numpy as np
    input = sys.stdin.readline

    n, k = map(int, input().split())
    x = np.array(list(map(int, input().split())))
    if x[0] >= 0:
        return x[k - 1]
    elif x[-1] <= 0:
        return x[n - k]
    else:
        t = np.sum(x < 0)
        if x[t] == 0:
            k = k - 1
            del x[t]

        time = np.inf
        if t >= k:
            time = x[t - k]
        if (n - t) >= k:
            time = min(time, x[t + k - 1])

        for i in range(max(1, k - (n - t)), min(t, k)):
            left = -x[t - i]
            right = x[t + k - i - 1]

            tmp = min(left, right) * 2 + max(left, right)
            if tmp < time:
                time = tmp

        return time


print(abc107_c())

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
n = len(x)
if 0 in x:
    x.remove(0)
    k = k - 1

try:
    if len(x) == 0:
        print(0)
    else:
        scores = []

        xx = np.array(x)
        a = xx >= 0
        pos = sum(a)
        neg = n - pos
        pos_ind = a
        try:
            b = np.where(a)[0][0]
        except:
            b = 0

        if pos >= k:
            scores.append(np.sum(xx[pos_ind][k-1]))
    #     print(scores)
        for i in range(min([pos,k])):
            if b + i - k + 1 < 0:
                break
            score = xx[b + i] * 2 - xx[b + i - k + 1]
            scores.append(score)
    #     print(scores)
    
        scores = [np.min(scores)]



        xx = np.array(x)
        xx = - xx
        xx = xx[::-1]
        a = xx >= 0
        pos = sum(a)
        neg = n - pos
        pos_ind = a
        try:
            b = np.where(a)[0][0]
        except:
            b = 0

        if pos >= k:
            scores.append(np.sum(xx[pos_ind][k-1]))
        for i in range(min([pos,k])):
            if b + i - k + 1 < 0:
                break
            score = xx[b + i] * 2 - xx[b + i - k + 1]
            scores.append(score)
    #     print(scores)


        print(np.min(scores))
except:
    print(0)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
hidari=0
migi=N-1
if N-K!=0:
    for i in range(N-K):
      if abs(X[hidari])>=abs(X[migi]):
        hidari=hidari+1
      else:
        migi=migi-1

if X[hidari]<0 and X[migi]>0:
  if abs(X[hidari])>=abs(X[migi]):
    print(2*abs(X[migi])+abs(X[hidari]))
  else:
    print(2*abs(X[hidari])+abs(X[migi]))
else:
  if abs(X[hidari])>=abs(X[migi]):
    print(abs(X[hidari]))
  else:
    print(abs(X[migi]))

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,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])
		cur2 = abs(x[l]) + 2*abs(x[r])
#		print([l,r,cur1,cur2])
		if cur1 < MIN:
			MIN = cur1
		if cur2 < MIN:
			MIN = cur2
print(MIN)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
hidari=0
migi=N-1
if N-K==0:
    for i in range(N-K):
      if abs(X[hidari])>=abs(X[migi]):
        hidari=hidari+1
      else:
        migi=migi-1

if X[hidari]<0 and X[migi]>0:
  if abs(X[hidari])>=abs(X[migi]):
    print(2*abs(X[migi])+abs(X[hidari]))
  else:
    print(2*abs(X[hidari])+abs(X[migi]))
else:
  if abs(X[hidari])>=abs(X[migi]):
    print(abs(X[hidari]))
  else:
    print(abs(X[migi]))

Submission status: Wrong Answer

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

X=sorted(X)

ans=0
for i in range(N-K+1):
    temp=X[i:i+K]
    tx=0
    tans=0
    for j in range(K):
        tans+=abs(tx-temp[j])
        tx=temp[j]
    if i==0 or ans>tans:
        ans=tans
    
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
import numpy as np

N, K = map(int, input().split())
x_l = list(map(int, input().split()))
x = np.array(x_l)
#print(x)
# 向きを変える折り返しが必要かどうか
posNum = np.sum(x >= 0)
negNum = np.sum(x <= 0)

#print(posNum)
#print(negNum)

#w = x.shape[1]
# 正の方向に進むとき　(負の方向に一度向かうときは往復分で2倍のコスト)
x_pos = np.empty(N)
for i in range(N):
    val = x[i]
    if val < 0:
        if i+1 >= N:
            x_pos[i] = -2 * val
            continue

        if x[i+1] < 0:
            x_pos[i] = -2 * (val - x[i+1])
        else:
            x_pos[i] = -2 * val
    else:
        if i-1 < 0:
            x_pos[i] = val
            continue
        if x[i-1] >= 0:
            x_pos[i] = val - x[i-1]
        else:
            x_pos[i] = val

# 負の方向に進むとき (正の方向に一度向かうときは往復分で2倍のコスト)
x_neg = np.empty(N)
for i in range(N):
    val = x[i]
    if val > 0:
        if i-1 < 0:
            x_neg[i] = 2 * val
            continue

        if x[i-1] > 0:
            x_neg[i] = 2 * (val - x[i-1])
        else:
            x_neg[i] = 2 * val
    else:
        if i + 1 >= N:
            x_neg[i] = -val
            continue

        if x[i+1] <= 0:
            x_neg[i] = -(val - x[i+1])
        else:
            x_neg[i] = -val

# 部分ベクトルの和が最小のものを探す
ans = 1000000
for i in range(N-K+1):
    val = sum(x_pos[i:i+K])
    if val < ans:
        ans = val
    val = sum(x_neg[i:i+K])
    if val < ans:
        ans = val

print(int(ans))

Submission status: Wrong Answer

Solution code:
# N, K = 9, 5
# X = [int(i) for i in '-9 -7 -4 -3 0 1 2 3 4'.split()]
N, K = map(int, input().split())
X = [int(i) for i in input().split()]
X_plus = []
X_minus = []
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])
# print(X_plus)
# print(X_minus)
res = []
len_plus = len(X_plus)
len_minus = len(X_minus)
if len_minus != 0:
    for i_plus in range(len_plus):
        if K-i_plus-1 <= len_minus:
            res.append(X_plus[i_plus]*2 - X_minus[-(K-i_plus-1)])
if len_plus >= K:
    res.append(X_plus[K-1])
 
if len_plus != 0:
    for i_minus in range(len_minus):
        if K-i_minus <= len_plus:
            res.append(abs(-X_minus[-i_minus-1])*2 + X_plus[K-i_minus-2])
if len_minus >= K:
    if len_plus > 0:
        if X_plus[0]==0:
            res.append(-X_minus[-K+1])
    else:
        res.append(-X_minus[-K])
print(min(res))

Submission status: Wrong Answer

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

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

# 絶対値でソート
x.sort(key=abs)

propose = []
pos_max = 0
neg_max = 0
for i in range(K):
    target = x[i]
    if target > 0:
        if pos_max < target:
            pos_max = target

    if target < 0:
        if neg_max < abs(target):
            neg_max = abs(target)

if pos_max > neg_max: 
    propose.append(pos_max + 2 * neg_max)
else:
    propose.append(neg_max + 2 * pos_max)


# only plus
x_plus = []
x_minus = []
for x_ in x:
    if x_ == 0:
        x_plus.append(x_)
        x_minus.append(x_)
    if x_ > 0:
        x_plus.append(x_)
    if x_ < 0:
        x_minus.append(abs(x_))

if K <= len(x_plus):
    propose.append(x_plus[K-1])

if K <= len(x_minus):
    propose.append(x_minus[K-1])

print(min(propose))

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
    if N == K:
        s = 0
        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 = x[e]
        if d_plus < 0:
            d_plus = 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()))
#N, K = 5, 3
N,K = 8, 5
x = list(map(int,"-9 -7 -4 -3 1 2 3 4".split()))
inf = float("inf")
origin = 0
if x[N-1] < 0:
	print(x[N-K])
	exit()
if x[0] > 0:
	print(x[K-1])
	exit()

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

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

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):
        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:
        print(x_list[-1] - x_list[0]*2)

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))

else:
    for i in range(N-K):
        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:
        print(x_list[-1] - x_list[0]*2)

Submission status: Wrong Answer

Solution code:
def abc107_c():
    import sys
    import numpy as np
    input = sys.stdin.readline

    n, k = map(int, input().split())
    x = np.array(list(map(int, input().split())))
    if x[0] >= 0:
        return x[k - 1]
    elif x[-1] <= 0:
        return x[n - k]
    else:
        t = np.sum(x < 0)
        if x[t] == 0:
            k = k - 1

        time = np.inf
        if t >= k:
            time = min(time, x[t - k])
        if (n - t) >= k:
            time = min(time, x[t + k - 1])

        for i in range(max(1, k - (n - t)), min(t, k)):
            left = -x[t - i]
            right = x[t + k - i - 1]

            tmp = min(left, right) * 2 + max(left, right)
            if tmp < time:
                time = tmp

        return time


print(abc107_c())

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, raw_input().split())
x = map(int, raw_input().split())

sort_x = sorted(x, key=lambda y: abs(y))[:K]

min_x = min(sort_x)
max_x = max(sort_x)

if min_x * max_x < 0:
  min_dist = min(map(abs, [min_x, max_x]))
  max_dist = max(map(abs, [min_x, max_x]))
  dist = max_dist + min_dist * 2
elif max_x >= 0:
  dist = max_x
else:
  dist = abs(min_x)
  
x = np.array(x)
x_m = x[x < 1]
x_p = x[x > -1]
dist_m = np.inf
dist_p = np.inf
if len(x_m) >= K:
  dist_m = -x_m[-K]
if len(x_p) >= K:
  dist_p = x_p[K-1]
  
dist = min([dist, dist_m, dist_p])


print dist

Submission status: Wrong Answer

Solution code:

N, K = map(int, input().split())
candles = list(map(int, input().split()))
if 0 in candles:
    K -= 1
minus = [abs(num) for num in candles if num < 0][::-1]
plus = [num for num in candles if num > 0]

ans = 9999999

# check plus or minus
if K == 0:
    ans = 0
else:
    if len(minus) >= K:
        ans = min(ans, minus[K - 1])
    if len(plus) >= K:
        ans = min(ans, plus[K - 1])

# check both
if len(minus) != 0 and len(plus) != 0:
    n_candle = 0
    index_m = index_p = 0
    for i in range(100010):

        if index_m < len(minus):
            if minus[index_m] == i:
                n_candle += 1
                if n_candle >= K:
                    index_p -= 1
                    break
                index_m += 1

        if index_p < len(plus):
            if plus[index_p] == i:
                n_candle += 1
                if n_candle >= K:
                    index_m -= 1
                    break
                index_p += 1

    # calc
    if minus[index_m] > plus[index_p]:
        ans = min(ans, 2 * plus[index_p] + minus[index_m])
    else:
        ans = min(ans, plus[index_p] + 2 * minus[index_m])

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N,K=map(  int,raw_input().split() )
X=map(  int,raw_input().split() )
 
zero_idx=bisect.bisect_left(X,0)
 
t=float("inf")
ans=float("inf")
for i in range( max(0,zero_idx-K+1) , min(N,zero_idx+K) ):
	if i < zero_idx:
		if zero_idx-i==K:
			t=X[i]
		else:
			if 0<=K-(zero_idx-i)-1+zero_idx<N:
				t=2*abs(X[i])+X[K-(zero_idx-i)-1+zero_idx]
	elif i>=zero_idx:
		if i-zero_idx==K-1:
			t=X[i]
		else:
			if 0<=zero_idx- ( K-(i-zero_idx+1) ) <=N:
				t=2*X[i]+ abs( X[zero_idx- ( K-(i-zero_idx+1) )] )
	ans=min(ans,t)
 
print ans

Submission status: Wrong Answer

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

x_minus = [a for a in X if a < 0]
x_plus = [a for a in X if a >= 0]

ans = float("inf")
for i in range(K+1):
    if i == 0:
        if len(x_plus) >= K:
            dis = x_plus[K - 1]
            ans = min(ans, dis)
        else:
            continue
    if i != 0:
        if len(x_minus) > i and len(x_plus) > K - i:
            dis = 2 * abs(x_minus[-i]) + x_plus[K - i - 1]
            ans = min(ans, dis)
        else:
            continue


print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
n = len(x)
if 0 in x:
    x.remove(0)
    k = k - 1

try:
    if len(x) == 0:
        print(0)
    else:
        scores = []

        xx = np.array(x)
        a = xx >= 0
        pos = sum(a)
        neg = n - pos
        pos_ind = a
        try:
            b = np.where(a)[0][0]
        except:
            b = 0

        if pos >= k:
            scores.append(np.sum(xx[pos_ind][k-1]))
    #     print(scores)
        for i in range(min([pos,k])):
            if b + i - k + 1 < 0:
                break
            score = xx[b + i] * 2 - xx[b + i - k + 1]
            scores.append(score)
    #     print(scores)



        xx = np.array(x)
        xx = - xx
        xx = xx[::-1]
        a = xx >= 0
        pos = sum(a)
        neg = n - pos
        pos_ind = a
        try:
            b = np.where(a)[0][0]
        except:
            b = 0

        if pos >= k:
            scores.append(np.sum(xx[pos_ind][k-1]))
    #     print(scores)
        for i in range(min([pos,k])):
            if b + i - k + 1 < 0:
                break
            score = xx[b + i] * 2 - xx[b + i - k + 1]
            scores.append(score)
    #     print(scores)


        print(np.min(scores))
except:
    print(0)

Submission status: Wrong Answer

Solution code:
def abc107_c():
    import sys
    import numpy as np
    input = sys.stdin.readline

    n, k = map(int, input().split())
    x = np.array(list(map(int, input().split())))
    if x[0] >= 0:
        return x[k - 1]
    elif x[-1] <= 0:
        return x[n - k]
    else:
        t = np.sum(x < 0)
        if x[t] == 0:
            k = k - 1

        time = np.inf
        if t >= k:
            time = x[t - k]
        elif (n - t) >= k:
            time = x[t + k - 1]

        for i in range(max(1, k - (n - t)), min(t, k)):
            left = -x[t - i]
            right = x[t + k - i - 1]

            tmp = min(left, right) * 2 + max(left, right)
            if tmp < time:
                time = tmp

        return time


print(abc107_c())

Submission status: Wrong Answer

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

def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))
    ans = float('inf')

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if 0 in x:
    K -= 1

a = [xi for xi in x if xi < 0]
a.reverse()
a.insert(0, 0)
b = [xi for xi in x if xi > 0]
b.insert(0, 0)

mi = 10 * 16

for i in range(K + 1):
    if i < 0 or len(a) <= i:
        continue
    if K - i < 0 or len(b) <= K - i:
        continue
    mi = min(mi, -2 * a[i] + b[K - i], -a[i] + 2 * b[K - i])

print(mi)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
import numpy as np

N, K = map(int, input().split())
x_l = list(map(int, input().split()))
x = np.array(x_l)
#print(x)
# 向きを変える折り返しが必要かどうか
posNum = np.sum(x >= 0)
negNum = np.sum(x <= 0)

#print(posNum)
#print(negNum)

#w = x.shape[1]
# 正の方向に進むとき　(負の方向に一度向かうときは往復分で2倍のコスト)
x_pos = np.empty(N)
for i in range(N):
    val = x[i]
    if val < 0:
        if i+1 >= N:
            x_pos[i] = -2 * val
            continue

        if x[i+1] < 0:
            x_pos[i] = -2 * (val - x[i+1])
        else:
            x_pos[i] = -2 * val
    else:
        if i-1 < 0:
            x_pos[i] = val
            continue
        if x[i-1] >= 0:
            x_pos[i] = val - x[i-1]
        else:
            x_pos[i] = val

# 負の方向に進むとき (正の方向に一度向かうときは往復分で2倍のコスト)
x_neg = np.empty(N)
for i in range(N):
    val = x[i]
    if val > 0:
        if i-1 < 0:
            x_neg[i] = 2 * val
            continue

        if x[i-1] > 0:
            x_neg[i] = 2 * (val - x[i-1])
        else:
            x_neg[i] = 2 * val
    else:
        if i + 1 >= N:
            x_neg[i] = -val
            continue

        if x[i+1] <= 0:
            x_neg[i] = -(val - x[i+1])
        else:
            x_neg[i] = -val

# 部分ベクトルの和が最小のものを探す
ans = 1000000
for i in range(N-K+1):
    val = sum(x_pos[i:i+K])
    if val < ans:
        ans = val
    val = sum(x_neg[i:i+K])
    if val < ans:
        ans = val

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, raw_input().split())
x = map(int, raw_input().split())

sort_x = sorted(x, key=lambda y: abs(y))[:K]

min_x = min(sort_x)
max_x = max(sort_x)

if min_x * max_x < 0:
  min_dist = min(map(abs, [min_x, max_x]))
  max_dist = max(map(abs, [min_x, max_x]))
  dist = max_dist + min_dist * 2
elif max_x > 0:
  dist = max_x
else:
  dist = abs(min_x)
  
x = np.array(x)
x_m = x[x < 1]
x_p = x[x > -1]
dist_m = np.inf
dist_p = np.inf
if len(x_m) >= K:
  dist_m = -x_m[-K]
if len(x_p) >= K:
  dist_p = x_p[K-1]
  
dist = min([dist, dist_m, dist_p])


print dist

Submission status: Wrong Answer

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

cur = 0
dist = 1000000000
if K == 1:
    dist = min(x)
elif K == N:
    if (x[0] <= 0) and (x[N-1] > 0):
        dist = min(abs(x[0]) * 2 + x[N-1], abs(x[0]) + x[N-1] * 2)
    elif x[N-1] <= 0:
        dist = abs(x[0])
    else: #x[i] > 0
        dist = x[N-1]
else:
    for i in range(N-K):
        cur_dist = 0
        if (x[i] <= 0) and (x[i+K-1] > 0):
            cur_dist = min(abs(x[i]) * 2 + x[i+K-1], abs(x[i]) + x[i+K-1] * 2)
        elif x[i+K-1] <= 0:
            cur_dist = abs(x[i])
        else: #x[i] > 0
            cur_dist = x[i+K-1]
        if dist >= cur_dist:
            dist = cur_dist
        if x[i] > 0:
            break

print(dist)