Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from copy import deepcopy
from sys import stdin

N = int(stdin.readline())
a = list(map(int, stdin.readline().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]


first_tmp = deepcopy(first)
second_tmp = deepcopy(second)
first_sum_tmp = first_sum
second_sum_tmp = second_sum
ans = -10 ** 15

# first側からk個を取り除き、second側からN - k個を取り除く
for k in range(N + 1):
    first = deepcopy(first_tmp)
    second = deepcopy(second_tmp)
    first_sum = first_sum_tmp
    second_sum = second_sum_tmp
    cnt = 0

    while cnt < k:
        heapq.heappush(first, a[N + cnt])
        first_sum += a[N + cnt]
        first_sum -= heapq.heappop(first)
        cnt += 1

    while cnt < N:
        heapq.heappush(second, -a[N + cnt])
        second_sum += a[N + cnt]
        second_sum -= -heapq.heappop(second)
        cnt += 1

    ans = max(ans, first_sum - second_sum)
    # print(first_sum, second_sum)

print(ans)


Fixed submission:
import heapq
from sys import stdin

N = int(stdin.readline())
a = list(map(int, stdin.readline().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]

# first, secondそれぞれ0 - Nまで探索
L_sum = [first_sum]
R_sum = [second_sum]

for i in range(N, 2 * N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    first_sum -= heapq.heappop(first)
    L_sum.append(first_sum)

    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]
    second_sum -= - heapq.heappop(second)
    R_sum.append(second_sum)

ans = -10 ** 15

R_sum = R_sum[::-1]

for i in range(N + 1):
    ans = max(ans, L_sum[i] - R_sum[i])

# print(L_sum, R_sum)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from copy import deepcopy

N = int(input())
a = list(map(int, input().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]


first_tmp = deepcopy(first)
second_tmp = deepcopy(second)
first_sum_tmp = first_sum
second_sum_tmp = second_sum
ans = -10 ** 15

# first側からk個を取り除き、second側からN - k個を取り除く
for k in range(N + 1):
    first = deepcopy(first_tmp)
    second = deepcopy(second_tmp)
    first_sum = first_sum_tmp
    second_sum = second_sum_tmp
    cnt = 0

    while cnt < k:
        heapq.heappush(first, a[N + cnt])
        first_sum += a[N + cnt]
        first_sum -= heapq.heappop(first)
        cnt += 1

    while cnt < N:
        heapq.heappush(second, -a[N + cnt])
        second_sum += a[N + cnt]
        second_sum -= -heapq.heappop(second)
        cnt += 1

    ans = max(ans, first_sum - second_sum)
    # print(first_sum, second_sum)

print(ans)


Fixed submission:
import heapq
from sys import stdin

N = int(stdin.readline())
a = list(map(int, stdin.readline().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]

# first, secondそれぞれ0 - Nまで探索
L_sum = [first_sum]
R_sum = [second_sum]

for i in range(N, 2 * N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    first_sum -= heapq.heappop(first)
    L_sum.append(first_sum)

    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]
    second_sum -= - heapq.heappop(second)
    R_sum.append(second_sum)

ans = -10 ** 15

R_sum = R_sum[::-1]

for i in range(N + 1):
    ans = max(ans, L_sum[i] - R_sum[i])

# print(L_sum, R_sum)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from copy import deepcopy

N = int(input())
a = list(map(int, input().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]


first_tmp = deepcopy(first)
second_tmp = deepcopy(second)
first_sum_tmp = first_sum
second_sum_tmp = second_sum
ans = -10 ** 15

# first側からk個を取り除き、second側からN - k個を取り除く
for k in range(N + 1):
    first = deepcopy(first_tmp)
    second = deepcopy(second_tmp)
    first_sum = first_sum_tmp
    second_sum = second_sum_tmp
    cnt = 0

    while cnt < k:
        heapq.heappush(first, a[N + cnt])
        first_sum += a[N + cnt]
        first_sum -= heapq.heappop(first)
        cnt += 1

    while cnt < N:
        heapq.heappush(second, -a[N + cnt])
        second_sum += a[N + cnt]
        second_sum -= -heapq.heappop(second)
        cnt += 1

    ans = max(ans, first_sum - second_sum)
    # print(first_sum, second_sum)

print(ans)


Fixed submission:
import heapq
from sys import stdin

N = int(stdin.readline())
a = list(map(int, stdin.readline().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]

# first, secondそれぞれ0 - Nまで探索
L_sum = [first_sum]
R_sum = [second_sum]

for i in range(N, 2 * N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    first_sum -= heapq.heappop(first)
    L_sum.append(first_sum)

    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]
    second_sum -= - heapq.heappop(second)
    R_sum.append(second_sum)

ans = -10 ** 15

R_sum = R_sum[::-1]

for i in range(N + 1):
    ans = max(ans, L_sum[i] - R_sum[i])

# print(L_sum, R_sum)
print(ans)