Problem: 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.

Submission attempts (in order):
import numpy as np

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

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


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = positives[K-1]
        for i in range(K+1):
            if i == 0:
                continue
            elif i == K:
                tmp = abs(negatives[nega_size-K])
                min = tmp if min > tmp else min
            elif i > 0 and i < K+1:
                new_posi = positives[:K-i]
                new_nega = negatives[nega_size-i:]
                tmp = 2 * new_posi[-1] + abs(new_nega[0])
                if tmp > new_posi[-1] + 2 * abs(new_nega[0]):
                    tmp = new_posi[-1] + 2 * abs(new_nega[0])
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[N + i - K+1]
            tmp = ne + 2 * po
            if tmp > 2 * ne + po:
                tmp = 2 * ne + po

            min = tmp if min > tmp else min

        return min

    if nega_size < K and K <= posi_size:
        min = positives[K-1]
        for i in range(1, nega_size):
            tmp = abs(negatives[-i]) + 2 * positives[K-i] if abs(negatives[-i]
                                                                 ) > positives[K-i] else 2 * abs(negatives[-i]) + positives[K-i]
            min = tmp if min > tmp else min

        return min

    if posi_size < K and K <= nega_size:
        min = negatives[-1]
        for i in range(1, posi_size):
            tmp = positives[-i-1] + 2 * abs(negatives[-i]) if abs(
                negatives[-i]) < positives[-i-1] else 2 * positives[-i-1] + abs(negatives[-i])
            min = tmp if min > tmp else min

        return min


print(main())


import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = 1e9
        for i in range(K+1):
            if i == 0:
                min = abs(negatives[-K])
            elif i > 0 and i < K:
                ne = abs(negatives[-K+i])
                po = positives[i-1]
                min = calc(ne, po, min)
            elif i == K:
                tmp = positives[K-1]
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[K+i-1]
            min = calc(ne, po, min)

        return min

    if nega_size < K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min
        return min

    if posi_size < K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-K-i])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())


Verdicts: Wrong Answer

Accepted