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 sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
print(X)
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

import sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
print(X)
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            print()
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            print()
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

import sys
import bisect
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
X = [int(x) for x in input().strip().split()]
b = 1
if X.count(0) == 0:
    X.insert(bisect.bisect(X, 0), 0)
    b = 0

s = X.index(0)
ans = float('INF')
for i in range(1, K + 1):
    if s + i < len(X):
        t_ans = X[s + i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s - (K - i) < 0:
                continue
            t_ans += X[s + i]
            ans = min(ans, t_ans - X[s - (K - i)])
    else:
        break

for i in range(1, K + 1):
    if s - i >= 0:
        t_ans = - X[s - i]
        if i + b == K:
            ans = min(ans, t_ans)
            break
        else:
            if s + (K - i) >= len(X):
                continue
            t_ans += - X[s - i]
            ans = min(ans, t_ans + X[s + (K - i)])

if ans == float('INF'):
    ans = 0
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted