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 = 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)
    print(x_list)
    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 main():
    N, K = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    if 0 in xs:
        K = K - 1
    if K == 0:
        print(0)
        return

    posi = [x for x in xs if x > 0]
    nega = [x for x in xs if x < 0]

    min_total = 10 ** 8
    # print('nega: {}, posi:{}'.format(len(nega), len(posi)))
    if len(nega) == 0:
        print(posi[K-1])
        return
    if len(posi) == 0:
        print(nega[-K])
        return

    if len(nega) <= len(posi):
        for i in range(0, len(nega)+1):
            k = K
            k = k - i
            # print('i = {}, k = {}'.format(i, k))
            if i!= 0:
                total_dis = abs(nega[-i])
            else:
                total_dis = 0
            # print('only nega dis = {}'.format(total_dis))
            if k <= 0:
                continue
            use_posi = posi[:k]
            # print('use positive ={}'.format(use_posi))
            if i!=0:
                total_dis += abs(nega[-i]) + use_posi[-1]
            else:
                total_dis = use_posi[-1]
            # print('dis = {}'.format(total_dis))
            if total_dis < min_total:
                min_total = total_dis
    else:
        for i in range(1, len(posi)+1):
            k = K
            k = k - i
            total_dis = posi[-i]
            if k <= 0:
                continue
            use_nega = [nega[-i] for i in range(1, k+1)]
            total_dis += posi[-i] + abs(use_nega[0])
            if total_dis < min_total:
                min_total = total_dis
    print(min_total)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

from bisect import bisect_right

n, k = map(int, input().split())
X = list(map(int, input().split()))
zero = bisect_right(X, 0)
left = X[:zero]
left = [-i for i in left]
left.sort()
right = X[zero:]

if len(left) == 0:
    print(right[k-1])
    exit()
elif len(right) == 0:
    print(left[k-1])
    exit()

left_count, right_count = 0, 0
left_position, right_position = -1, -1
for i in range(n):
    if left_position+1 < len(left) and right_position+1 < len(right):
        if left[left_position+1] < right[right_position+1]:
            left_position += 1
            left_count += 1
            if left_count + right_count < k:
                continue
            if right_position != -1:
                print(2*right[right_position]+left[left_position])
                exit()
            else:
                print(left[left_position])
                exit()
        elif left[left_position+1] > right[right_position+1]:
            right_position += 1
            right_count += 1
            if left_count + right_count < k:
                continue
            if left_position != -1:
                print(2*left[left_position]+right[right_position])
                exit()
            else:
                print(right[right_position])
                exit()
        else:
            if left_count + right_count + 2 < k:
                left_position += 1
                left_count += 1
                right_position += 1
                right_count += 1
                continue
            elif left_count + right_count + 2 == k:
                left_position += 1
                right_position += 1
                print(2*left[left_position]+right[right_position])
                exit()
            else:
                if left_count < right_count:
                    right_position += 1
                    if left_position != -1:
                        print(2*left[left_position]+right[right_position])
                        exit()
                    else:
                        print(right[right_position])
                        exit()
                else:
                    left_position += 1
                    if right_position != -1:
                        print(2*right[right_position]+left[left_position])
                        exit()
                    else:
                        print(left[left_position])
                        exit()
    elif left_position+1 >= len(left):
        right_position += 1
        right_count += 1
        if left_count + right_count < k:
            continue
        if left_position != -1:
            print(2*left[left_position]+right[right_position])
            exit()
        else:
            print(right[right_position])
            exit()
    elif right_position+1 >= len(right):
        left_position += 1
        left_count += 1
        if left_count + right_count < k:
            continue
        if right_position != -1:
            print(2*right[right_position]+left[left_position])
            exit()
        else:
            print(left[left_position])
            exit()

Submission status: Wrong Answer

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

lst = []
for i in range(N-K+1):
    if x[i] <= 0 and x[i+K-1] <= 0:
        lst.append(abs(x[i]))
    elif x[i] >= 0 and x[i+K-1] >= 0:
        lst.append(x[i+K-1])
    else:
        lst.append(abs(x[i]))
        lst[i] += x[i+K-1] - x[i]
print(min(lst))

Submission status: Wrong Answer

Solution code:
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 = x[e]
        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:
import sys
N,K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
ans1 = sys.maxsize
ans2 = sys.maxsize
ans3 = sys.maxsize
plus = []
minus = []
for i in X:
    if i <= 0:
        minus.append(i)
    if i >= 0:
        plus.append(i)
if len(minus) >= K:
    ans1 = abs(minus[-K])
if len(plus) >= K:
    ans2 = plus[K-1]

X = [(abs(i),i) for i in X]
Y = sorted(X)[:K]
Z = sorted(Y,key=lambda x:x[1])
minus = abs(Z[0][1])
plus = abs(Z[-1][1])
ans3 = min(minus,plus)*2+max(minus,plus)
print(min(ans1,ans2,ans3))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
c = []
f = True

for i in range(n):
    inum = x[i]
    if inum < 0: 
        if n > i+k-1 >= 0: 
            c.append(abs(inum)*2 + x[i+k-1])
    elif inum > 0:
        if n > i-k+1 >= 0:
            c.append(inum*2 + abs(x[i-k+1]))
    if f and inum > 0:
        if n > i+k-1 >= 0:
            c.append(abs(x[i+k-1]))
        if n > i-k >= 0:
            c.append(abs(x[i-k]))
        f = False
    if inum == 0:
        if n > i+k-1 >= 0:
            c.append(abs(x[i+k-1]))
        if n > i-k+1>= 0:
            c.append(abs(x[i-k+1]))


print(min(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())
x = [int(i) for i in input().split()]

ans = 2*10**8
for i in range(N-K+1):
    left = x[i]
    right = x[K-1+i]
    if left<0 and right<0:
        ans = min(ans,abs(left))
    elif left<0 and right>=0 and right<abs(left):
        ans = min(ans,right*2+abs(left))
    elif left<0 and right>=0 and right>=abs(left):
        ans = min(ans,abs(left)*2+right)
    else:
        ans = min(ans,right)
print(ans)

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

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(abs(x[len(x)-k]))
    elif x[0] > 0:
        print(x[k-1])    
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            
            else:
                ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        if r-k >= 0:
            ans = abs(x[r-k])
        ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

Solution code:
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])
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:
    res.append(-X_minus[-K])
# print(res)
print(min(res))

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(100000):

        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
            if n_candle >= K:
                break

    # 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:
from bisect import bisect_left
n, k = map(int, input().split())
a = list(map(int, input().split()))
t = bisect_left(a, 0, 0, len(a))
if t + k <= n:
    ans = a[t + k - 1]
    for i in range(min(k, t)):
        if i == k - 1:
            ans = min(ans, -a[t-i-1])
        else:
            ans = min(ans, a[t+k-2-i]-a[t-i-1]*2, 2*a[t+k-2-i]-a[t-i-1])
else:
    ans = float("inf")
    for i in range(min(n - k + 1, k)):
        if i == k - 1:
            ans = min(ans, -a[n-i-k])
        else:
            ans = min(ans, a[n-1-i]-a[n-i-k]*2, 2*a[n-1-i]-a[n-i-k])
print(ans)

Submission status: Wrong Answer

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

time = 10 ** 20
# 0以上の最小の整数
base = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        break

for i in range(N - K + 1):
    if x[i+K-1] < 0:
        time = min(abs(x[i]) * 2, time)
    elif x[i] < 0 and  x[i+K-1] >= 0:
        time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
    else:
        time = min(x[i+K-1] * 2, time)

print(time)

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(data[i])+abs(data[i+K-1])):
        data_min=abs(data[i])+abs(data[i+K-1])
for i in range (N-K+1):
    if data_min >(abs(data2[i])+abs(data2[i+K-1])):
        data_min=abs(data2[i])+abs(data2[i+K-1])
print(data_min)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
minsa = max(x[N-1]-x[0],x[N-1],-x[0])
#fu = 0
#sei = 0
for i in range(N-K+1):
    if x[i] <= 0 and x[i+K-1] <= 0:
        sa = -x[i]
        if minsa > sa:
            minsa = sa
            #fu = -x[i]
            #sei = 0
    if x[i] < 0 and x[i+K-1] > 0:
        sa = min(x[i + K-1], -x[i])*2 + max(x[i + K -1], -x[i])
        if minsa > sa:
            minsa = sa
            #fu = -x[i]
            #sei = x[i+K-1]
    if x[i] >= 0 and x[i+K-1] >= 0:
        sa = x[i+K-1]
        if minsa > sa:
            minsa = sa
            #fu = 0
            #sei = x[i+K-1]
print(minsa)
#print(fu)
#print(sei)
#print(min(fu,sei) * 2 + max(fu,sei))

Submission status: Wrong Answer

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

ans = x[N-1]-x[0]
for i in range(N-K+1):
    left = x[i]
    right = x[K-1+i]
    if left<0 and right<0:
        ans = min(ans,abs(left))
    elif left<0 and right>=0 and right<abs(left):
        ans = min(ans,right*2+abs(left))
    elif left<0 and right>=0 and right>=abs(left):
        ans = min(ans,abs(left)*2+right)
    else:
        ans = min(ans,right)
print(ans)

Submission status: Wrong Answer

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


mini = abs(x[0]) + abs(x[-1])
maxPoint = 0

if x[0] >= 0:
    print(x[K - 1])

elif x[-1] <= 0:
    print(abs(x[-K]))

else:
    x = list(map(abs, x))
    for i in range(N - K + 1):
        if abs(x[i]) + abs(x[i + K - 1]) <= mini:
            mini = abs(x[i]) + abs(x[i + K - 1])
            maxPoint = i
    print(2 * min(abs(x[maxPoint]), abs(x[maxPoint + K - 1])
                  ) + max(abs(x[maxPoint]), abs(x[maxPoint + K - 1])))

Submission status: Wrong Answer

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

time = 10 ** 20
# 0以上の最小の整数
base = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        break

for i in range(N - K + 1):
    if x[i+K-1] < 0:
        time = min(abs(x[i]) * 2, time)
    elif x[i] < 0 and  x[i+K-1] >= 0:
        time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
        print(time)
    else:
        time = min(x[i+K-1] * 2, time)

print(time)

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:
# -*- coding: utf-8 -*-

from bisect import bisect_right

n, k = map(int, input().split())
X = list(map(int, input().split()))
zero = bisect_right(X, 0)
left = X[:zero]
left = [-i for i in left]
left.sort()
right = X[zero:]

if len(left) == 0:
    print(right[k-1])
    exit()
elif len(right) == 0:
    print(left[k-1])
    exit()

left_count, right_count = 0, 0
left_position, right_position = -1, -1
for i in range(n):
    if left_position+1 < len(left) and right_position+1 < len(right):
        if left[left_position+1] < right[right_position+1]:
            left_position += 1
            left_count += 1
            if left_count + right_count < k:
                continue
            if right_position != -1:
                print(2*right[right_position]+left[left_position])
                exit()
            else:
                print(left[left_position])
                exit()
        elif left[left_position+1] > right[right_position+1]:
            right_position += 1
            right_count += 1
            if left_count + right_count < k:
                continue
            if left_position != -1:
                print(2*left[left_position]+right[right_position])
                exit()
            else:
                print(right[right_position])
                exit()
        else:
            if left_count + right_count + 2 < k:
                left_position += 1
                left_count += 1
                right_position += 1
                right_count += 1
                continue
            else:
                if left_count < right_count:
                    right_position += 1
                    if left_position != -1:
                        print(2*left[left_position]+right[right_position])
                        exit()
                    else:
                        print(right[right_position])
                        exit()
                else:
                    left_position += 1
                    if right_position != -1:
                        print(2*right[right_position]+left[left_position])
                        exit()
                    else:
                        print(left[left_position])
                        exit()
    elif left_position+1 >= len(left):
        right_position += 1
        right_count += 1
        if left_count + right_count < k:
            continue
        if left_position != -1:
            print(2*left[left_position]+right[right_position])
            exit()
        else:
            print(right[right_position])
            exit()
    elif right_position+1 >= len(right):
        left_position += 1
        left_count += 1
        if left_count + right_count < k:
            continue
        if right_position != -1:
            print(2*right[right_position]+left[left_position])
            exit()
        else:
            print(left[left_position])
            exit()

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

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

    for i in range(max(origin - k + 1, 0), min(origin + 1, n - k)):
        part = xs[i:i + k]
        rng = abs(part[0] - part[-1])
        time = min(time, rng + abs(part[0]), rng + abs(part[-1]))

    print(time)


def get_origin(xs, n):
    if xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n - 1
    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

            origin = floor((r - l) / 2 + l)
            if xs[origin] == 0:
                break
            elif xs[origin] < 0:
                if origin == n:
                    break
                else:
                    l = origin
            else:
                if origin == 0:
                    origin = -1
                    break
                else:
                    r = origin




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
input_s = str(input())
[n, k] = list(map(int, input_s.split(' ')))
input_s = str(input())
l_i = list(map(int, input_s.split(' ')))
l_i = sorted(l_i, key=abs)
ta = l_i[:k]
tb = [i for i in ta if i < 0]
tc = [i for i in ta if i >= 0]

result = 0
if len(tb) == 0:
    result = max(tc)
elif len(tc) == 0 or max(tc) == 0:
    result = -(min(tb))
elif max(tc) >= -(min(tb)):
    result = max(tc) + -(min(tb)) * 2
else:
    result = max(tc) * 2 + -(min(tb))

print(result)

Submission status: Wrong Answer

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

if x[0] > 0:
    x.insert(0, 0)
    K+=1
elif x[-1] < 0:
    x.append(0)
    K+=1
else:
    for i in range(N-1):
        if x[i] < 0 and x[i+1]> 0:
            x.insert(i+1, 0)
            K += 1
            break

ans = float("inf")
for i in range(N):
    j = i + K-1
    if j >= N:
        break
    if x[i]*x[j] > 0:
        continue
    ans = min(ans, -2*x[i]+x[j], -x[i]+2*x[j])
    
print(ans)

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

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(abs(x[len(x)-k]))
    elif x[0] > 0:
        print(x[k-1])    
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            elif i == r and r-k >= 0:
                ans = abs(x[r-k])
            else:
                ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

Solution code:
import sys
N,K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
ans1 = sys.maxsize
ans2 = sys.maxsize
ans3 = sys.maxsize
plus = []
minus = []
for i in X:
    if i <= 0:
        minus.append(i)
    if i >= 0:
        plus.append(i)
if len(minus) >= K:
    ans1 = abs(minus[0])
if len(plus) >= K:
    ans2 = plus[K-1]

X = [(abs(i),i) for i in X]
Y = sorted(X)[:K]
Z = sorted(Y,key=lambda x:x[1])
minus = abs(Z[0][1])
plus = abs(Z[-1][1])
ans3 = min(minus,plus)*2+max(minus,plus)
print(min(ans1,ans2,ans3))

Submission status: Wrong Answer

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

arr = map(int,raw_input().split())

import bisect
t = bisect.bisect_left(arr,0)
arr.insert(t,0)

ans = float("inf")
for i in range(t,min(t+k+1,n)):
	
	if i - k > -1:
		w = abs(arr[i]) + abs(arr[i-k] )+ min(abs(arr[i]), abs(arr[i-k]))

		ans = min(ans,w)

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

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

    for i in range(max(origin - k + 1, 0), min(origin + k, n - k)):
        part = xs[i:i + k]
        rng = abs(part[0] - part[-1])
        time = min(time, rng + abs(part[0]), rng + abs(part[-1]))

    print(time)


def get_origin(xs, n):
    if xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n - 1
    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

            origin = floor((r - l) / 2 + l)
            if xs[origin] == 0:
                break
            elif xs[origin] < 0:
                if origin == n:
                    break
                else:
                    l = origin
            else:
                if origin == 0:
                    origin = -1
                    break
                else:
                    r = origin




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

lst = []
for i in range(N-K+1):
    lst.append(abs(x[i]))
    lst[i] += x[i+K-1] - x[i]
print(min(lst))

Submission status: Wrong Answer

Solution code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000000000000000

if N == K:
    left = problem[0]
    right = problem[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left)
else:
    for i in range(N - K):
        left = problem[i]
        right = problem[i+K-1]
        if left * right >= 0:
            if abs(right) > abs(left):
                if ans > abs(right):
                    ans = abs(right)
            else:
                if ans > abs(left):
                    ans = abs(left)
        else:
            if abs(right) > abs(left):
                if ans > (2 * abs(left) + abs(right)):
                    ans = (2 * abs(left) + abs(right))
            else:
                if ans > 2*abs(right)+abs(left):
                    ans = 2*abs(right)+abs(left)

print(ans)

Submission status: Wrong Answer

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


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

    if x[0] < 0:
        diff = abs(x[0])

        for i in range(n):
            x[i] += diff

    print(x[k - 1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(x[len(x)-k])
    elif x[0] > 0:
        print(x[k-1])    
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            elif i == r and r-k >= 0:
                ans = abs(x[r-k])
            else:
                ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

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

if(K==0):
    print("0")


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:
import math
N,K = map(int,input().split())
xs = input().split()
res_list = []
for i in range(N-K+1):
    if int(xs[i]) >0:
        diff = int(xs[i+K-1])
        res_list.append(diff)
        break
    elif int(xs[i]) <= 0 and int(xs[i+K-1]) > 0:
        diff = 2 * abs(int(xs[i])) + int(xs[i+K-1])
    elif int(xs[i+K-1]) <= 0:
        diff = abs(int(xs[i]))
    res_list.append(diff)
print(min(res_list))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().strip().split()))
print(a[K -1])

Submission status: Wrong Answer

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

m = 1000000
for n in range(N-K+1):
  d = abs(cans[n]) + abs(cans[n + (K-1)] - cans[n])
  if m > d:
    m = d

print(m)