Buggy python submission (verdict: Time Limit Exceeded):
def c_candles(N, K, X):
    from bisect import bisect_left
    negative_pos = X[:bisect_left(X, 0)][::-1]
    positive_pos = X[bisect_left(X, 0):]
    negative_pos_rel = negative_pos[:]
    positive_pos_rel = positive_pos[:]
    for k in range(1, len(negative_pos)):
        negative_pos_rel[k] -= negative_pos[k - 1]
    for k in range(1, len(positive_pos)):
        positive_pos_rel[k] -= positive_pos[k - 1]
    # print(X, negative_pos_rel, positive_pos_rel)

    ans = float('inf')
    for k in range(len(negative_pos) + 1):
        length = 0
        n = K - k
        if n > len(positive_pos):
            continue

        length += abs(sum(negative_pos_rel[:k]))
        length += length
        length += sum(positive_pos_rel[:n])
        ans = min(ans, length)
    #     print(n, k, length)
    # print('-')
    for n in range(len(positive_pos) + 1):
        length = 0
        k = K - n
        if k > len(negative_pos):
            continue
        length += sum(positive_pos_rel[:n])
        length += length
        length += abs(sum(negative_pos_rel[:k]))
        ans = min(ans, length)
        # print(n, k, length)

    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))

Fixed submission:
def c_candles(N, K, X):
    ans = float('inf')
    # [left, right] が指す連続したK本のろうそくに火をつけるといい
    for left in range(N):
        right = left + K - 1
        if right >= N:  # 右端がろうそくを指していない
            break
        # left→rightへ付けていく場合とright→leftへ付けていく場合
        ans = min(ans, abs(X[left]) + abs(X[right] - X[left]))
        ans = min(ans, abs(X[right]) + abs(X[right] - X[left]))
    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))

Buggy python submission (verdict: Time Limit Exceeded):
def c_candles(N, K, X):
    from bisect import bisect_left
    negative_pos = X[:bisect_left(X, 0)][::-1]
    positive_pos = X[bisect_left(X, 0):]
    negative_pos_rel = negative_pos[:]
    positive_pos_rel = positive_pos[:]
    for k in range(1, len(negative_pos)):
        negative_pos_rel[k] -= negative_pos[k - 1]
    for k in range(1, len(positive_pos)):
        positive_pos_rel[k] -= positive_pos[k - 1]
    # print(X, negative_pos_rel, positive_pos_rel)

    ans = float('inf')
    for k in range(len(negative_pos) + 1):
        length = 0
        n = K - k
        if n > len(positive_pos):
            continue

        length += abs(sum(negative_pos_rel[:k]))
        length += length
        length += sum(positive_pos_rel[:n])
        ans = min(ans, length)
    #     print(n, k, length)
    # print('-')
    for n in range(len(positive_pos) + 1):
        length = 0
        k = K - n
        if k > len(negative_pos):
            continue
        length += sum(positive_pos_rel[:n])
        length += length
        length += abs(sum(negative_pos_rel[:k]))
        ans = min(ans, length)
        # print(n, k, length)

    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))

Fixed submission:
def c_candles(N, K, X):
    ans = float('inf')
    # [left, right] が指す連続したK本のろうそくに火をつけるといい
    for left in range(N):
        right = left + K - 1
        if right >= N:  # 右端がろうそくを指していない
            break
        # left→rightへ付けていく場合とright→leftへ付けていく場合
        ans = min(ans, abs(X[left]) + abs(X[right] - X[left]))
        ans = min(ans, abs(X[right]) + abs(X[right] - X[left]))
    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))