Problem p03274 (python)

Fixed submission:
n, k = map(int, input().split())
x = list(map(int, input().split()))

if x[0] >= 0:
    print(x[k - 1])
    exit()
elif x[n - 1] < 0:
    print(abs(x[n - k]))
    exit()
else:
    for i in range(n):
        if x[i] >= 0:
            ori = i
            break

f_inf = float("inf")
res = f_inf
for i in range(max(ori - k, 0), min(ori + k, n) - k + 1):
    time = min(abs(x[i + k - 1] - x[i]) + abs(x[i + k - 1]), abs(x[i]) + abs(x[i + k - 1] - x[i]))
    res = min(res, time)

print(res)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))

    for i in range(n):
        if X[i] >= 0:
            ori = i
            X.insert(i, 0)
            break
    else:
        ori = n - 1

    res = f_inf
    start = max(0, ori - k)
    end = min(n - k + 1, ori + 1)
    for j in range(start, end):
        left = abs(X[j]) * 2 + abs(X[j + k])
        right = abs(X[j]) + abs(X[j + k]) * 2
        res = min(res, min(left, right))
    print(res)


if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))

    if X[0] >= 0:
        print(X[k - 1])
        exit()
    elif X[-1] < 0:
        print(abs(X[-(k + 1)]))
        exit()

    for i in range(n):
        if X[i] >= 0:
            ori = i
            break

    res = f_inf
    left = max(ori - k, 0)
    right = min(ori + k, n)
    for i in range(left, right - k + 1):
        RL = abs(X[i + k - 1] - X[i]) + abs(X[i + k - 1])
        LR = abs(X[i]) + abs(X[i + k - 1] - X[i])
        res = min(res, min(RL, LR))
    print(res)


if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
import sys
import bisect
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))

    if 0 in X:
        ori = X.index(0)
    else:
        ori = bisect.bisect_left(X, 0)

    R = [0]
    for i in range(1, n):
        R.append(R[-1] + abs(X[i - 1] - X[i]))

    mi = f_inf
    left = 0
    for right in range(k - 1, n):
        if left <= ori <= right:
            cost = R[right] - R[left]
            if mi > cost:
                mi = cost
                res = [left, right]
        left += 1

    left, right = res
    if X[left] > 0 and X[right] > 0:
        print(X[right])
    elif X[left] < 0 and X[right] < 0:
        print(abs(X[left]))
    elif X[left] == X[right] == 0:
        print(0)
    else:
        print(min(abs(X[left]) * 2 + abs(X[right]), abs(X[left]) + abs(X[right]) * 2))


if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
x = list(map(int, input().split()))

for i in range(n):
    if x[i] >= 0:
        ori = i
        break

f_inf = float("inf")
res = f_inf
for i in range(max(ori - k, 0), min(ori + k, n) - k + 1):
    time = abs(0 - x[i]) + abs(x[i + k - 1] - x[i])
    res = min(res, time)

print(res)