Problem p03716 (python)

Fixed submission:
import heapq
import bisect

n = int(input())
n = n*3
a = list(map(int,input().split()))

left = a[0:n//3]
right = list(-i for i in a[2*n//3:])

leftsum = [0 for i in range(n//3+1)]
rightsum = [0 for i in range(n//3+1)]

heapq.heapify(left)
leftsum[0] = sum(left)
for i in range(n//3):
    k = i + n//3
    heapq.heappush(left,a[k])
    temp = heapq.heappop(left)
    leftsum[i+1] = leftsum[i]+a[k]-temp

heapq.heapify(right)
rightsum[-1] = -sum(right)
for i in range(n//3)[::-1]:
    k = i + n//3
    heapq.heappush(right,-a[k])
    temp = -heapq.heappop(right)
    rightsum[i] = rightsum[i+1] - temp + a[k]




mxm = -sum(a)
for i in range(n//3+1):
    if leftsum[i]-rightsum[i] > mxm:
        mxm = leftsum[i]-rightsum[i]
print(mxm)

Buggy submission (verdict: Runtime Error):
import heapq
import bisect

n = int(input())
n = n*3
a = list(map(int,input().split()))

big = a[0:n//3]
big.sort()
tempmin = a[n//3::]
tempmin.sort()
small = tempmin[0:n//3]
spare = tempmin[n//3::]

ans = sum(big)-sum(small)
mxm = ans

heapq.heapify(small)
heapq.heapify(spare)

for i in range(n//3,2*n//3):
    if len(spare) != 0 and a[i] >= spare[0]:
        index = bisect.bisect_left(spare,a[i])
        spare.pop(index)

    if a[i] > big[0]:
        ans += a[i]-big[0]
        index = bisect.bisect_left(big,a[i])
        big.insert(index,a[i])
        big.pop(0)
    
    if a[i] <= small[-1]:
        ans -= spare[0]-a[i]
        heapq.heappush(small,spare[0])
        heapq.heappop(spare)
        index = bisect.bisect_left(small,a[i])
        small.pop(index)
    
    if ans > mxm:
        mxm = ans
print(mxm)

Buggy submission (verdict: Runtime Error):
import heapq
import bisect

n = int(input())
n = n*3
a = list(map(int,input().split()))

big = a[0:n//3]
tempmin = a[n//3::]
tempmin.sort()
small = tempmin[0:n//3]
spare = tempmin[n//3::]

ans = sum(big)-sum(small)
mxm = ans

heapq.heapify(small)
heapq.heapify(spare)

for i in range(n//3,2*n//3):
    if len(spare) != 0 and a[i] >= spare[0]:
        index = bisect.bisect_left(spare,a[i])
        spare.pop(index)

    if a[i] > big[0]:
        ans += a[i]-big[0]
        index = bisect.bisect_left(big,a[i])
        big.insert(index,a[i])
        big.pop(0)
    
    if a[i] < small[-1]:
        ans -= spare[0]-a[i]
        heapq.heappush(small,spare[0])
        heapq.heappop(spare)
        index = bisect.bisect_left(small,a[i])
        small.pop(index)
    
    if ans > mxm:
        mxm = ans
print(mxm)

Buggy submission (verdict: Runtime Error):
import bisect

n = int(input())
n = n*3
a = list(map(int,input().split()))

big = a[0:n//3]
tempmin = a[n//3::]
tempmin.sort()
small = tempmin[0:n//3]

spare = tempmin[n//3:]


big.sort()
small.sort()
ans = sum(big)-sum(small)

mxm = ans
for i in range(n//3,2*n//3):
    if a[i] > big[0]:
        ans += a[i]-big[0]
        index = bisect.bisect_left(big,a[i])
        big.insert(index,a[i])
        big.pop(0)
    if a[i] <= small[-1]:
        index = bisect.bisect_left(spare,a[i])
        ans -= spare[index] - a[i]
        aindex = bisect.bisect_left(small,spare[index])
        small.insert(aindex,spare[index])
        aindex = bisect.bisect_left(small,a[i])
        small.pop(aindex)
        spare.pop(index)

    if len(spare) != 0 and a[i] >= spare[0]:
        index = bisect.bisect_left(spare,a[i])
        spare.pop(index)

    if ans > mxm:
        mxm = ans
print(mxm)