Problem p03312 (python)

Fixed submission:
# ARC100D

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))


# import numpy as np
import bisect
n = int(input())
a = list(map(int, input().split()))
# a = np.array(list(map(int, input().split())))
ca = [None] * n #ca[i] = a0~aiの和
s = 0
for i,num in enumerate(a):
    s += a[i]
    ca[i] = s
# ca = a.cumsum()

def sub(l, start, end, init):
    if len(l)==0:
        return 0,0
    elif end-start==1:
        return 0,l[start]-init
    target = (init + l[end-1])/2
    i = bisect.bisect_right(l, target)
    p1 = (l[i]-init, l[end-1]-l[i])
    p2 = (l[i-1]-init, l[end-1]-l[i-1])
    if abs(p1[0] - p1[1]) < abs(p2[0] - p2[1]):
        return min(p1), max(p1)
    else:
        return min(p2), max(p2)
best = 10**15
for i in range(1,n):
    p1 = sub(ca, 0, i, 0)
    p2 = sub(ca, i, n, ca[i-1])
    nn = max(p1[1]-p2[0], p2[1]-p1[0], p1[1]-p1[0], p2[1]-p2[0])
#     print(p1,p2,nn)
    best = min(best, nn)
print(best)

Buggy submission (verdict: Runtime Error):
# ARC100D

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))


n = int(input())
a = list(map(int, input().split()))
ca = [None] * n #ca[i] = a0~aiの和
s = 0
for i,num in enumerate(a):
    s += a[i]
    ca[i] = s

diff = [None] * n

s = 0
t = 1
v1 = 0 # a[:s]
v2 = a[0] # sum(a[s:t])
diff[t-1] = (v1,v2)
while t<n:
#     print(t, v1-v2)
    t += 1
    v2 += a[t-1]
    while v1<v2 and s<t:
        if diff[t-1] is None or abs(v1-v2)<abs(diff[t-1][0] - diff[t-1][1]):
            diff[t-1] = (v1,v2)
        s += 1
        v1 += a[s-1]
        v2 -= a[s-1]
    if diff[t-1] is None or abs(v1-v2)<abs(diff[t-1][0] - diff[t-1][1]):
        diff[t-1] = (v1,v2)        
    if v1>v2 and s>=1:
        v1 -= a[s-1]
        v2 += a[s-1]
        s -= 1

diff2 = [None] * n

s = n
t = n-1
v1 = 0 # sum(a[s:])
v2 = a[-1] # sum(a[t:s])
diff2[t] = (v1,v2)
while t>=1:
    # print(t, v1, v2)
    t -= 1
    v2 += a[t]
    while v1<v2 and s>t:
        if diff2[t] is None or abs(v1-v2)<abs(diff2[t][0] - diff2[t][1]):
            diff2[t] = (v1,v2)
        s -= 1
        v1 += a[s]
        v2 -= a[s]
    if diff2[t] is None or abs(v1-v2)<abs(diff2[t][0] - diff2[t][1]):
        diff2[t] = (v1,v2)        
    if v1>v2 and s<=n-2:
        v1 -= a[s]
        v2 += a[s]
        s += 1
print(min((max(*diff[i],*diff2[i+1]) - min(*diff[i],*diff2[i+1])) for i in range(n-1)))

Buggy submission (verdict: Runtime Error):
# ARC100D

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))


import numpy as np
import bisect
n = int(input())
a = np.array(list(map(int, input().split())))
ca = [None] * n #ca[i] = a0~aiの和
# s = 0
# for i,num in enumerate(a):
#     s += a[i]
#     ca[i] = s
ca = a.cumsum()

def sub(l, init):
    if len(l)==0:
        return 0,0
    elif len(l)==1:
        return 0,l[0]-init
    target = (init + l[-1])/2
    i = bisect.bisect_right(l, target)
    p1 = (l[i]-init, l[-1]-l[i])
    p2 = (l[i-1]-init, l[-1]-l[i-1])
    if abs(p1[0] - p1[1]) < abs(p2[0] - p2[1]):
        return min(p1), max(p1)
    else:
        return min(p2), max(p2)
best = 10**15
for i in range(1,n):
    p1 = sub(ca[:i], 0)
    p2 = sub(ca[i:], ca[i-1])
    nn = max(p1[1]-p2[0], p2[1]-p1[0], p1[1]-p1[0], p2[1]-p2[0])
    best = min(best, nn)
print(best)

Buggy submission (verdict: Runtime Error):
cum = np.cumsum(a)
total = np.sum(a)
def find(b, total):
    assert sum(b)==total
#     print("b: ", b)
    thre = total/2
    b = np.cumsum(b)
    out = np.argmin(np.abs(b-thre))
#     print("out:", out)
    return out
#     return np.min(np.where(b>thre)[0])

best = total
for i in range(1,N-2):
    left = cum[i]
    right = total - left
    ileft = find(a[:(i+1)], left)
    iright = find(a[(i+1):], right) + i + 1
    z1 = np.sum(a[:(ileft+1)])
    z2 = left - z1
    z3 = np.sum(a[(i+1):(iright+1)])
    z4 = np.sum(a[(iright+1):])  # right - z3
    M = max([z1,z2,z3,z4])
    m = min([z1,z2,z3,z4])
    best = min(best, M-m)
#     print([z1,z2,z3,z4])
best