Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
as_ =  list(map(int, input().split()))

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    heapq.heappop(l)
    l_max = sum(l)
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    heapq.heappop(r)
    r_min = sum(r)
    
  else:
    l_max = sum(l)
    r_min = sum(r)
    
  l_maxs[i] = l_max
  r_mins[i] = r_min

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))

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

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

sl = sum(l)
sr = sum(r)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    a = heapq.heappop(l)
    sl += m[j] -a
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    b = heapq.heappop(r)
    sr += m[k]*(-1) - b
    
  l_maxs[i] = sl
  r_mins[i] = sr

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
as_ =  list(map(int, input().split()))

l = as_[0:n]
m = as_[n:2*n]
r = as_[2*n:3*n]
r = [(-1)*a for a in r]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    heapq.heappop(l)
    l_max = sum(l)
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    heapq.heappop(r)
    r_min = sum(r)
    
  else:
    l_max = sum(l)
    r_min = sum(r)
    
  l_maxs[i] = l_max
  r_mins[i] = r_min

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))

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

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

sl = sum(l)
sr = sum(r)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    a = heapq.heappop(l)
    sl += m[j] -a
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    b = heapq.heappop(r)
    sr += m[k]*(-1) - b
    
  l_maxs[i] = sl
  r_mins[i] = sr

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
as_ =  list(map(int, input().split()))

l = as_[0:n]
m = as_[n:2*n]
r = as_[2*n:3*n]
r = [(-1)*a for a in r]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    heapq.heappop(l)
    l_max = sum(l)
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    heapq.heappop(r)
    r_min = sum(r)
    
  else:
    l_max = sum(l)
    r_min = sum(r)
    
  l_maxs[i] = l_max
  r_mins[i] = r_min

#print(l_maxs)
#print(r_mins)

d = [0]*(n+1)
for i in range(n+1):
  d[i] = l_maxs[i] + r_mins[n-i]
print(max(d))

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

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

sl = sum(l)
sr = sum(r)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    a = heapq.heappop(l)
    sl += m[j] -a
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    b = heapq.heappop(r)
    sr += m[k]*(-1) - b
    
  l_maxs[i] = sl
  r_mins[i] = sr

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
as_ =  list(map(int, input().split()))

l = as_[0:n]
m = as_[n:2*n]
r = as_[2*n:3*n]
r = [(-1)*a for a in r]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

for i in range(n+1):
  if i == 0:
    l_max = sum(l)
    r_min = sum(r)
  else:
    j = i-1
    heapq.heappush(l, m[j])
    heapq.heappop(l)
    l_max = sum(l)
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    heapq.heappop(r)
    r_min = sum(r)
    
  l_maxs[i] = l_max
  r_mins[i] = r_min

#print(l_maxs)
#print(r_mins)

d = [0]*(n+1)
for i in range(n+1):
  d[i] = l_maxs[i] + r_mins[n-i]
print(max(d))

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

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

sl = sum(l)
sr = sum(r)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    a = heapq.heappop(l)
    sl += m[j] -a
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    b = heapq.heappop(r)
    sr += m[k]*(-1) - b
    
  l_maxs[i] = sl
  r_mins[i] = sr

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
as_ =  list(map(int, input().split()))

l = as_[0:n]
m = as_[n:2*n]
r = as_[2*n:3*n]
r = [(-1)*a for a in r]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

for i in range(n+1):
  if i == 0:
    l_max = sum(l)
    r_min = sum(r)
  else:
    j = i-1
    heapq.heappush(l, m[j])
    heapq.heappop(l)
    l_max = sum(l)
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    heapq.heappop(r)
    r_min = sum(r)
    
  l_maxs[i] = l_max
  r_mins[i] = r_min

#print(l_maxs)
#print(r_mins)

d = [0]*(n+1)
for i in range(n+1):
  d[i] = l_maxs[i] + r_mins[n-i]
print(max(d))

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

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

sl = sum(l)
sr = sum(r)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    a = heapq.heappop(l)
    sl += m[j] -a
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    b = heapq.heappop(r)
    sr += m[k]*(-1) - b
    
  l_maxs[i] = sl
  r_mins[i] = sr

#print(l_maxs)
#print(r_mins)

print(max([l_maxs[i] + r_mins[n-i] for i in range(n+1)]))