Problem p02727 (python)

Fixed submission:
x,y,a,b,c = map(int,input().split())

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))

p.sort()
q.sort()
r.sort(reverse=True)

p = p[a-x:]
q = q[b-y:]

p.extend(q)
p.sort()

ans = sum(p)
count = 0
for i in range(c):
	if p[i] < r[i]:
		ans += r[i] - p[i]
	else:
		break
print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, insort_left
import sys
def main():
	readlines = sys.stdin.readline
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = p[a-x:]
	q = q[b-y:]
	a = len(p)
	b = len(q)
	ans = sum(p) + sum(q)
	p_count = 0
	q_count = 0

	for i in range(c):
		if p[p_count] < r[i] and q[q_count] < r[i]:
			if p[p_count] < q[q_count] and p_count != a:
				ans -= p[p_count]
				ans += r[i]
				#insort_left(p,r[i])
				if p_count < a:
					p_count += 1
			elif p[p_count] > q[q_count] and q_count != b:
				ans -= q[q_count]
				ans += r[i]
				#insort_left(q,r[i])
				if q_count < b:
					q_count += 1
		elif p[p_count] < r[i] and p_count != a:
			ans -= p[p_count]
			ans += r[i]
			#insort_left(p,r[i])
			if p_count < a:
				p_count += 1
		elif q[q_count] < r[i] and q_count != b:
			ans -= q[q_count]
			ans += r[i]
			#insort_left(q,r[i])
			if q_count < b:
				q_count += 1
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, insort_left
import sys
def main():
	readlines = sys.stdin.readline
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = p[a-x:]
	q = q[b-y:]
	ans = sum(p) + sum(q)
	p_count = 0
	q_count = 0

	for i in range(c):
		if p[p_count] < r[i] and q[q_count] < r[i]:
			if p[p_count] < q[q_count]:
				ans -= p[p_count]
				ans += r[i]
				#insort_left(p,r[i])
				if p_count < a-1:
					p_count += 1
			else:
				ans -= q[q_count]
				ans += r[i]
				#insort_left(q,r[i])
				if q_count < b-1:
					q_count += 1
		elif p[p_count] < r[i]:
			ans -= p[p_count]
			ans += r[i]
			#insort_left(p,r[i])
			if p_count < a-1:
				p_count += 1
		elif q[q_count] < r[i]:
			ans -= q[q_count]
			ans += r[i]
			#insort_left(q,r[i])
			if q_count < b-1:
				q_count += 1
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, insort_left
import sys
def main():
	readlines = sys.stdin.readline
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = p[a-x:]
	q = q[b-y:]
	ans = sum(p) + sum(q)
	p_count = 0
	q_count = 0

	for i in range(c):
		if p[p_count] < r[i] and q[q_count] < r[i]:
			if p[p_count] < q[q_count]:
				ans -= p[p_count]
				ans += r[i]
				#insort_left(p,r[i])
				p_count += 1
			else:
				ans -= q[q_count]
				ans += r[i]
				#insort_left(q,r[i])
				q_count += 1
		elif p[p_count] < r[i]:
			ans -= p[p_count]
			ans += r[i]
			#insort_left(p,r[i])
			p_count += 1
		elif q[q_count] < r[i]:
			ans -= q[q_count]
			ans += r[i]
			#insort_left(q,r[i])
			q_count += 1
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, insort_left
from collections import deque

x,y,a,b,c = map(int,input().split())

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))

p.sort()
q.sort()
r.sort()

p = deque(p[a-x:])
q = deque(q[b-y:])
ans = sum(p) + sum(q)

for i in range(c):
	if p[0] < r[i] and q[0] < r[i]:
		if p[0] < q[0]:
			ans -= p.popleft()
			ans += r[i]
			insort_left(p,r[i])
		else:
			ans -= q.popleft()
			ans += r[i]
			insort_left(q,r[i])
	elif p[0] < r[i] and q[0] >= r[i]:
		ans -= p.popleft()
		ans += r[i]
		insort_left(p,r[i])
	elif p[0] >= r[i] and q[0] < r[i]:
		ans -= q.popleft()
		ans += r[i]
		insort_left(q,r[i])

print(ans)