Problem p02727 (python)

Fixed submission:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,y,a,b,c = mi()
p = li()
q = li()
r = li()

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


qp = True
qq = True
plef = a - x
qlef = b - y
ans = 0
rlen = len(r) -1
while True:
    if rlen <0:
        break
    
    if plef == a:
        if qlef == b:
            break
        else :
            ima = q[qlef]
            tmp = "q"
    elif qlef == b:
        ima = p[plef]
        tmp = "p"
    elif p[plef] >= q[qlef]:
        ima = q[qlef]
        tmp = "q"
    else:
        ima = p[plef]
        tmp = "p"
    if ima < r[rlen]:
        ans += r[rlen]
        rlen -= 1
        if tmp == "q":
            qlef += 1
        else:
            plef += 1
    
    else:
        break
if plef != a:
    for i in range(plef,a):
        ans += p[i]
if qlef != b:
    for i in range(qlef,b):
        ans +=q[i]
print(ans)

Buggy submission (verdict: Runtime Error):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,y,a,b,c = mi()
p = li()
q = li()
r = li()

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


qp = True
qq = True
plef = a - x
qlef = b - y
ans = 0
rlen = len(r) -1
while True:
    if rlen <0:
        break
    
    if plef == x:
        if qlef == y:
            break
        else :
            ima = q[qlef]
            tmp = "q"
    elif qlef == y:
        ima = p[plef]
        tmp = "p"
    elif p[plef] >= q[qlef]:
        ima = q[qlef]
        tmp = "q"
    else:
        ima = p[plef]
        tmp = "p"
    if ima < r[rlen]:
        ans += r[rlen]
        rlen -= 1
        if tmp == "q":
            qlef += 1
        else:
            plef += 1
    
    else:
        break
    
for i in range(plef,a):
    ans += p[i]

for i in range(qlef,b):
    ans +=q[i]
print(ans)

Buggy submission (verdict: Runtime Error):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,y,a,b,c = mi()
p = li()
q = li()
r = li()

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


qp = True
qq = True
plef = a - x
qlef = b - y
ans = 0
rlen = len(r) -1
while True:
    if rlen <0:
        break
    if p[plef] >= q[qlef]:
        ima = q[qlef]
        tmp = "q"
    else:
        ima = p[plef]
        tmp = "p"
    if ima < r[rlen]:
        ans += r[rlen]
        rlen -= 1
        if tmp == "q":
            qlef += 1
        else:
            plef += 1
    
    else:
        break
    
for i in range(plef,a):
    ans += p[i]

for i in range(qlef,b):
    ans +=q[i]
print(ans)

Buggy submission (verdict: Runtime Error):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,y,a,b,c = mi()
p = li()
q = li()
r = li()

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

from collections import deque
pd = deque(p)
qd = deque(q)
rd = deque(r)

qp = True
qq = True
plef = a - x
qlef = b - y
ans = 0
while True:
    if len(rd) == 0:
        break
    if p[plef] >= q[qlef]:
        ima = q[qlef]
        tmp = "q"
    else:
        ima = p[plef]
        tmp = "p"
    if ima < rd[-1]:
        ans += rd[-1]
        rd.pop()
        if tmp == "q":
            qlef += 1
        else:
            plef += 1
    
    else:
        break
    
for i in range(plef,a):
    ans += p[i]

for i in range(qlef,b):
    ans +=q[i]
print(ans)