Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
from collections import deque
import bisect
x,y,z=[],[],[]
for i in range(3*N):
    if 0<=i<N:x.append(A[i])
    elif N<=i<2*N:y.append(A[i])
    elif 2*N<=i<3*N:z.append(A[i])

a=x+y
a.sort()
mae=[sum(a[N:2*N])]
line=N-1
used=set()
for i in range(N-1,-1,-1):
    while line in used:
        line-=1
    idx=bisect.bisect_left(a,y[i])
    while idx in used:
        idx+=1
    used|={idx,}
    if line<idx:
        mae.append(mae[-1]-a[idx]+a[line])
        line-=1
    elif line==idx:
        mae.append(mae[-1])
        line-=1
    else:
        mae.append(mae[-1])

b=y+z
b.sort()
ushiro=[-1*sum(b[0:N])]
line=N
used=set()
for i in range(N):
    while line in used:
        line+=1
    idx=bisect.bisect_left(b,y[i])
    while idx in used:
        idx+=1
    used|={idx,}
    if idx<line:
        ushiro.append(ushiro[-1]+b[idx]-b[line])
        line+=1
    elif idx==line:
        ushiro.append(ushiro[-1])
        line+=1
    else:
        ushiro.append(ushiro[-1])
ans=-1*float('inf')
for i in range(N+1):
    ans=max(ans,mae[i]+ushiro[N-i])
print(ans)

Fixed submission:
from heapq import heappush,heappop
dataX=[]
dataY=[]
mae=dict()
ushiro=dict()
N=int(input())
A=list(map(int,input().split()))
X=A[0:N]
Y=A[N:2*N]
Z=A[2*N:3*N]
for i in range(N):
    heappush(dataX,X[i])
    heappush(dataY,-1*Z[i])
mae[0]=sum(X)
ushiro[0]=-1*sum(Z)
for i in range(1,N+1):
    mae[i]=mae[i-1]+Y[i-1]
    heappush(dataX,Y[i-1])
    mae[i]-=heappop(dataX)
    ushiro[i]=ushiro[i-1]-Y[N-i]
    heappush(dataY,-1*Y[N-i])
    ushiro[i]-=heappop(dataY)
ans=-1*float('inf')
for i in range(N+1):
    ans=max(ans,mae[i]+ushiro[N-i])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
from collections import deque
import bisect
x,y,z=[],[],[]
for i in range(3*N):
    if 0<=i<N:x.append(A[i])
    elif N<=i<2*N:y.append(A[i])
    elif 2*N<=i<3*N:z.append(A[i])
a=x+y
a.sort()
mae=[sum(a[N:2*N])]
for i in range(N-1,-1,-1):
    idx=bisect.bisect_left(a,y[i])
    if y[i]>a[-1*N-1]:
        mae.append(mae[-1]-a[idx]+a[-1*N-1])
        a.pop(idx)
    else:
        mae.append(mae[-1])
        a.pop(idx)
b=y+z
b.sort()
ushiro=[-1*sum(b[0:N])]
for i in range(N):
    idx=bisect.bisect_left(b,y[i])
    if y[i]<b[N]:
        ushiro.append(ushiro[-1]+b[idx]-b[N])
        b.pop(idx)
    else:
        ushiro.append(ushiro[-1])
        b.pop(idx)
ans=-1*float('inf')
for i in range(N+1):
    ans=max(ans,mae[i]+ushiro[N-i])
print(ans)

Fixed submission:
from heapq import heappush,heappop
dataX=[]
dataY=[]
mae=dict()
ushiro=dict()
N=int(input())
A=list(map(int,input().split()))
X=A[0:N]
Y=A[N:2*N]
Z=A[2*N:3*N]
for i in range(N):
    heappush(dataX,X[i])
    heappush(dataY,-1*Z[i])
mae[0]=sum(X)
ushiro[0]=-1*sum(Z)
for i in range(1,N+1):
    mae[i]=mae[i-1]+Y[i-1]
    heappush(dataX,Y[i-1])
    mae[i]-=heappop(dataX)
    ushiro[i]=ushiro[i-1]-Y[N-i]
    heappush(dataY,-1*Y[N-i])
    ushiro[i]-=heappop(dataY)
ans=-1*float('inf')
for i in range(N+1):
    ans=max(ans,mae[i]+ushiro[N-i])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
from collections import deque
x,y,z=[],[],[]

for i in range(3*N):
    if 0<=i<N:x.append(A[i])
    elif N<=i<2*N:y.append(A[i])
    elif 2*N<=i<3*N:z.append(A[i])
a=x
b=y+z
a.sort(reverse=True)
b.sort()
ans=sum(a[0:N])-sum(b[0:N])
for i in range(N):
    a=x+y[0:i+1]
    b=z+y[i+1:N]
    a.sort(reverse=True)
    b.sort()
    ans=max(ans,sum(a[0:N])-sum(b[0:N]))
print(ans)

Fixed submission:
from heapq import heappush,heappop
dataX=[]
dataY=[]
mae=dict()
ushiro=dict()
N=int(input())
A=list(map(int,input().split()))
X=A[0:N]
Y=A[N:2*N]
Z=A[2*N:3*N]
for i in range(N):
    heappush(dataX,X[i])
    heappush(dataY,-1*Z[i])
mae[0]=sum(X)
ushiro[0]=-1*sum(Z)
for i in range(1,N+1):
    mae[i]=mae[i-1]+Y[i-1]
    heappush(dataX,Y[i-1])
    mae[i]-=heappop(dataX)
    ushiro[i]=ushiro[i-1]-Y[N-i]
    heappush(dataY,-1*Y[N-i])
    ushiro[i]-=heappop(dataY)
ans=-1*float('inf')
for i in range(N+1):
    ans=max(ans,mae[i]+ushiro[N-i])
print(ans)