Problem p02727 (python)

Fixed submission:
import sys
import math
import collections
from collections import deque
#x = int(input())
x, y, A, B, C = map(int, sys.stdin.readline().split())
#s = input()

a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
c = list(map(int, sys.stdin.readline().split()))

a.sort(reverse = True)
b.sort(reverse = True)

for i in range(x):
    c.append(a[i])

for i in range(y):
    c.append(b[i])

c.sort(reverse = True)
ans = 0
for i in range(x+y):
    ans += c[i]

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import math
import collections
from collections import deque
#x = int(input())
x, y, A, B, C = map(int, sys.stdin.readline().split())
#s = input()

def ijpop():
    if ic < A:
        if jc < B:
            return (p.popleft(), q.popleft())
        else:
            return (p.popleft(), 0)
    else:
        if jc < B:
            return (0, q.popleft())
        else:
            return (0, 0)

def ikpop():
    if ic < A:
        if kc < C:
            return (p.popleft(), r.popleft())
        else:
            return (p.popleft(), 0)
    else:
        if kc < C:
            return (0, r.popleft())
        else:
            return (0, 0)

def jkpop():
    if jc < B:
        if rc < C:
            return (q.popleft(), r.popleft())
        else:
            return (q.popleft(), 0)
    else:
        if rc < C:
            return (0, r.popleft())
        else:
            return (0, 0)

def ipop():
    if ic < A:
        return p.popleft()
    else:
        return 0

def jpop():
    if jc < B:
        return q.popleft()
    else:
        return 0

def kpop():
    if kc < C:
        return r.popleft()
    else:
        return 0

a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
c = list(map(int, sys.stdin.readline().split()))

a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)

p = deque(a)
q = deque(b)
r = deque(c)

ans = 0
xc = 1
yc = 1
i = p.popleft()
j = q.popleft()
k = r.popleft()
ic = 1
jc = 1
kc = 1
while (True):
    if kc <= C:
        if xc <= x and yc <= y:
            if i >= k and j >= k:
                ans += (i+j)
                xc += 1
                yc += 1
                i, j = ijpop()
            elif i >= k and j < k:
                ans += (i+k)
                xc += 1
                yc += 1
                i, k = ikpop()
                kc += 1
            elif i < k and j >= k:
                ans += (j+k)
                xc += 1
                yc += 1
                j, k = jkpop()
                kc += 1
            else:
                ans += k
                if i >= j:
                    yc += 1
                    j, k = jkpop()
                else:
                    xc += 1
                    i, k = jkpop()
                kc += 1
                    
            continue

        elif xc > x and yc <= y:
            if j >= k:
                ans += j
                yc += 1
                j = jpop()
            else:
                ans += k
                yc += 1
                k = kpop()
                kc += 1

            continue

        elif xc <= x and yc > y:
            if i >= k:
                ans += i
                xc += 1
                i = ipop()
            else:
                ans += k
                xc += 1
                k = kpop()
                kc += 1

            continue

        else:
            break

    else:
        while (xc <= x):
            ans += i
            i = ipop()
            xc += 1
        while (yc <= y):
            ans += j
            j = jpop()
            yc += 1

        break

print(ans)