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):
def update():
    cost = list(map(abs, candles))
    min_cost_index = cost.index(min(cost))
    print(min_cost_index)
    start_index = max(0, min_cost_index - K)
    end_index = min(N, min_cost_index + K)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 

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



candles = update()

N = len(candles)
min_cost = None
patterns = N - K + 1


for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

N, K = list(map(int, input().strip().split(" ")))
candles = input().strip()
min_cost_index = candles.count("-")
candles = list(map(int, candles.split(" ")))

def update():
    start_index = max(0, min_cost_index - K - 1)
    end_index = min(N, min_cost_index + K + 1)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 
   
start_time = time.perf_counter()
candles = update()

N = len(candles)
min_cost = None
patterns = N - K + 1


for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return abs(point_candles[0]) + abs(point_candles[-1])

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

patterns = N - K + 1

min_cost = None

for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    print(cost)
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

N, K = list(map(int, input().strip().split(" ")))
candles = input().strip()
min_cost_index = candles.count("-")
candles = list(map(int, candles.split(" ")))

def update():
    start_index = max(0, min_cost_index - K - 1)
    end_index = min(N, min_cost_index + K + 1)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(start_index):
    start_point = candles[start_index]
    end_point = candles[start_index + K - 1]
    if(start_point * end_point >= 0):
        return max(abs(start_point), abs(end_point))
    else:
        return end_point - start_point + min(-start_point, end_point) 

candles = update()

N = len(candles)
min_cost = compute_cost(0)
patterns = N - K + 1

for start_index in range(patterns):
    cost = compute_cost(start_index)
    if(min_cost > cost):
        min_cost = cost
print(min_cost)

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted