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 copy
num = input().split(" ")
zahyou = input().split(" ")

num[1] = int(num[1])
plus = []
minus = []
rousoku = []
for i in range(int(num[0])):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))



if(num[1] > len(plus)):
    plus_tmp = minus[(len(plus)-num[1]):] + plus
    minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
rousoku = plus_tmp[0:num[1]]
minus_tmp_2 = copy.deepcopy(minus_tmp)
while(True):
    if(rousoku[-1] - rousoku[-2] >= (min(min(rousoku), 0) - minus_tmp_2[-1]) * 2):
        rousoku.pop(-1)
        rousoku.insert(0, minus_tmp_2.pop(-1))
    else:
        break
rousoku_move_plus = []
for i in rousoku:
    rousoku_move_plus.append(i - min(rousoku) * 2)


if(num[1] > len(minus)):
    minus_tmp = minus + plus[:(num[1]-len(minus))]
    plus_tmp = plus[(num[1]-len(minus)):]
rousoku = minus_tmp[(-(num[1]+1)):]
plus_tmp_2 = copy.deepcopy(plus_tmp)
while(True):
    if(rousoku[1] - rousoku[0] >= (plus_tmp_2[0] - max(max(rousoku), 0)) * 2):
        rousoku.pop(0)
        rousoku.append(plus_tmp_2.pop(0))
    else:
        break
rousoku_move_minus = []
for i in rousoku:
    rousoku_move_minus.append(max(rousoku) * 2 - i)

print(min(max(rousoku_move_plus), max(rousoku_move_minus)))


num = input().split(" ")
zahyou = input().split(" ")

num[1] = int(num[1])
plus = []
minus = []
rousoku = []
for i in range(int(num[0])):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))



if(num[1] > len(plus)):
    plus_tmp = minus[(len(plus)-num[1]):] + plus
    minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
rousoku = plus_tmp[0:num[1]]
minus_tmp_2 = []
for i in minus_tmp:
    minus_tmp_2.append(i)
while(True):
    if(rousoku[-1] - rousoku[-2] >= (min(min(rousoku), 0) - minus_tmp_2[-1]) * 2):
        rousoku.pop(-1)
        rousoku.insert(0, minus_tmp_2.pop(-1))
    else:
        break
rousoku_move_plus = []
for i in rousoku:
    rousoku_move_plus.append(i - min(rousoku) * 2)


if(num[1] > len(minus)):
    minus_tmp = minus + plus[:(num[1]-len(minus))]
    plus_tmp = plus[(num[1]-len(minus)):]
rousoku = minus_tmp[(-(num[1]+1)):]
plus_tmp_2 = []
for i in plus_tmp:
    plus_tmp_2.append(i)
while(True):
    if(rousoku[1] - rousoku[0] >= (plus_tmp_2[0] - max(max(rousoku), 0)) * 2):
        rousoku.pop(0)
        rousoku.append(plus_tmp_2.pop(0))
    else:
        break
rousoku_move_minus = []
for i in rousoku:
    rousoku_move_minus.append(max(rousoku) * 2 - i)

print(min(max(rousoku_move_plus), max(rousoku_move_minus)))


num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

    print(time)


num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

print(time)


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Accepted