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):
N, K = map(int, input().split())
x = [int(i) for i in input().split()]

time = 10 ** 20

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)

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

time = 10 ** 20

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+K-1]) * 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] * 2, time)

print(time)


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

time = 10 ** 20

base = 0
index = 0

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

if index == 0:
        time = x[K-1]
elif index == -1:
    time = abs(x[N-K])
else:
    for i in range(max(0, index - K), min(index, N)):
        if x[i+K-1] < 0:
            time = min(abs(x[i]), 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], time)

print(time

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

time = 10 ** 20

for i in range(N - K + 1):
    x_min = x[i]
    x_max = x[i + K - 1]
    this_time = 0
    if x_min >= 0:
        this_time = x_max
    elif x_max <= 0:
        this_time = abs(x_min)
    else:
        this_time = x_max - x_min + min(x_max, abs(x_min))
    if time > this_time:
        time = this_time

print(this_time)

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

time = 10 ** 20

base = 0
index = 0

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

for i in range(base - K, base + 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)




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

time = 10 ** 20

for i in range(N - K + 1):
    x_min = x[i]
    x_max = x[i + K - 1]
    if x_min >= 0:
        this_time = x_max
    elif x_max <= 0:
        this_time = abs(x_min)
    else:
        this_time = x_max - x_min + min(x_max, abs(x_min))

    if time > this_time:
        time = this_time

print(time)

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Accepted