Problem p03310 (python)

Fixed submission:
from itertools import accumulate, islice
from bisect import bisect_left

n = int(input())
xs = [int(x) for x in input().split()]
ys = list(accumulate(xs))
zs, total = ys[:-1], ys[-1]

rs = range(2, n-2)
v = []
for i, z in islice(enumerate(zs), 1, n-1):
    j = bisect_left(zs, z//2)
    splits = ((zs[j+k], z - zs[j+k]) for k in (-1, 0, 1) if 0 <= j+k <= i)
    a, b = min(splits, key=lambda s:abs(s[0]-s[1]))

    j = bisect_left(zs, (total - z)//2 + z)
    splits = ((zs[j+k] - z, total - zs[j+k]) for k in (-1, 0, 1) if i <= j+k <= n-2)
    c, d = min(splits, key=lambda s:abs(s[0]-s[1]))

    minn, _, _, maxx = sorted((a,b,c,d))
    v.append(maxx - minn)

print(min(v) if v else max(xs) - min(xs))

Buggy submission (verdict: Runtime Error):
from itertools import accumulate, islice
from bisect import bisect_left

n = int(input())
xs = [int(x) for x in input().split()]

ys = list(accumulate(xs))
zs, total = ys[:-1], ys[-1]
v = []
for i, z in islice(enumerate(zs), 1, n-1):
    j = bisect_left(zs, z//2)
    splits = ((zs[j+k], z - zs[j+k]) for k in (0, 1) if 0 <= j+k <= i)
    a, b = min(splits, key=lambda s:abs(s[0]-s[1]))

    j = bisect_left(zs, (total - z)//2 + z)
    splits = ((zs[j+k] - z, total - zs[j+k]) for k in (0, 1) if i <= j+k <= n-2)
    c, d = min(splits, key=lambda s:abs(s[0]-s[1]))

    minn, _, _, maxx = sorted((a,b,c,d))
    v.append(maxx - minn)

print(min(v) if v else max(xs) - min(xs))

Buggy submission (verdict: Runtime Error):
from itertools import accumulate
from bisect import bisect_left, bisect_right

n = int(input())
xs = [int(x) for x in input().split()]

ys = [0] + list(accumulate(xs))

rs = range(2, n-2)
v = []
for i in rs:
    j = bisect_left(ys, ys[i]//2)
    minmax1 = sorted([ys[j], ys[i] - ys[j]])
    minmax2 = sorted([ys[j-1], ys[i] - ys[j-1]])
    minmax3 = sorted([ys[j+1], ys[i] - ys[j+1]])
    _, max1, min1 = min((x-n, x, n) for n, x in (minmax1, minmax2, minmax3))

    k = bisect_left(ys, (ys[-1] - ys[i])//2 + ys[i])
    minmax1 = sorted([ys[k] - ys[i], ys[-1] - ys[k]])
    minmax2 = sorted([ys[k-1] - ys[i], ys[-1] - ys[k-1]])
    minmax3 = sorted([ys[k+1] - ys[i], ys[-1] - ys[k+1]])
    _, max2, min2 = min((x-n, x, n) for n, x in (minmax1, minmax2, minmax3))

    minn, _, _, maxx = sorted([max1, min1, max2, min2])
    v.append(maxx - minn)

print(min(v) if v else max(xs) - min(xs))

Buggy submission (verdict: Runtime Error):
from itertools import accumulate
from bisect import bisect_left, bisect_right

n = int(input())
xs = [int(x) for x in input().split()]

ys = [0] + list(accumulate(xs))

rs = range(2, n-2)
v = []
for i in rs:
    j = bisect_left(ys, ys[i]//2)
    min1, max1 = sorted([ys[j], ys[i] - ys[j]])
    min2, max2 = sorted([ys[j-1], ys[i] - ys[j-1]])
    max_, min_ = (max1, min1) if max1 - min1 < max2 - min2 else (max2, min2)

    k = bisect_left(ys, (ys[-1] - ys[i])//2 + ys[i])
    min1, max1 = sorted([ys[k] - ys[i], ys[-1] - ys[k]])
    min2, max2 = sorted([ys[k-1] - ys[i], ys[-1] - ys[k-1]])
    max__, min__ = (max1, min1) if max1 - min1 < max2 - min2 else (max2, min2)

    minn, _, _, maxx = sorted([max_, min_, max__, min__])
    v.append(maxx - minn)

print(min(v))