Problem:
 Score : 500 points    Problem Statement You are going to eat X red apples and Y green apples. You have A red apples of deliciousness p_1,p_2, \dots, p_A , B green apples of deliciousness q_1,q_2, \dots, q_B , and C colorless apples of deliciousness r_1,r_2, \dots, r_C . Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively. From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible. Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.      Constraints  1 \leq X \leq A \leq 10^5  1 \leq Y \leq B \leq 10^5  1 \leq C \leq 10^5  1 \leq p_i \leq 10^9  1 \leq q_i \leq 10^9  1 \leq r_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  X  Y  A  B  C  p_1  p_2  ...  p_A  q_1  q_2  ...  q_B  r_1  r_2  ...  r_C       Output Print the maximum possible sum of the deliciousness of the eaten apples.        Sample Input 1 1 2 2 2 1
2 4
5 1
3      Sample Output 1 12  The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:   Eat the 2 -nd red apple.  Eat the 1 -st green apple.  Paint the 1 -st colorless apple green and eat it.        Sample Input 2 2 2 2 2 2
8 6
9 1
2 1      Sample Output 2 25       Sample Input 3 2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4      Sample Output 3 74

Solution (Python):
x, y, a, b, c = map(int, input().split())
p = [int(i) for i in input().split(" ")]
q = [int(i) for i in input().split(" ")]
r = [int(i) for i in input().split(" ")]
p = sorted(p)
q = sorted(q)
r = sorted(r)
x_ = a-x
y_ = b-y
flag = 0
ans = sum(p[x_:]) + sum(q[y_:])
for i in range(c-1, -1, -1):
    if p[x_] > q[y_]:
        if q[y_] < r[i]:
            ans += r[i] - q[y_]
            y_ += 1
            if y_>=b:
                flag = 1
                break
        else:
            break
    else:
        if p[x_] < r[i]:
            ans += r[i] - p[x_]
            x_ += 1
            if x_>=a:
                flag = 2
                break
        else:
            break
if flag == 1:
    for j in range(i-1, -1, -1):
        if x_>=a:
            break
        if p[x_] < r[j]:
            ans += r[j] - p[x_]
            x_ += 1
        else:
            break
elif flag == 2:
    for j in range(i-1, -1, -1):
        if y_>=b:
            break
        if q[y_] < r[j]:
            ans += r[j] - q[y_]
            y_ += 1
        else:
            break
            
print(ans)

Solution (Python):

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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

EP=P[0:X]
EQ=Q[0:Y]

    
    
        
    
    

EP.reverse()
EQ.reverse()

ans=sum(EP)+sum(EQ)

p=0
q=0

INF=10**12

EP.append(INF)
EQ.append(INF)

for i in range(C):
    if R[i]>EP[p] or R[i]>EQ[q]:
        if EP[p]<EQ[q]:
            ans+=R[i]
            ans-=EP[p]
            p+=1
        else:
            ans+=R[i]
            ans-=EQ[q]
            q+=1
    else:
        break

print(ans)

Solution (Python):
import heapq

x, y, a, b, c = map(int, input().split())
p = [int(s) for s in input().split()]
q = [int(s) for s in input().split()]
r = [int(s) for s in input().split()]

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

que = []
for i in range(x):
    heapq.heappush(que, p[i])

for i in range(y):
    heapq.heappush(que, q[i])

for i in range(c):
    if que[0] > r[i]:
        break
    heapq.heappop(que)
    heapq.heappush(que, r[i])

print(sum(que))

Solution (Python):

X, Y, A, B, C = map(int,input().split())
p0 = list(map(int,input().split()))
q0 = list(map(int,input().split()))
r0 = list(map(int,input().split()))
p = sorted(p0, reverse = True); p.append(0)
q = sorted(q0, reverse = True); q.append(0)
r = sorted(r0, reverse = True); r.append(0)

happy = 0
i = 0
j = 0
k = 0
red = 0
green = 0
white = 0

for _ in range(X+Y):
    if max(p[i], q[j], r[k]) == r[k]:
        happy += r[k]
        white += 1
        k += 1
    elif max(p[i], q[j], r[k]) == p[i]:
        if red < X:
            happy += p[i]
            red += 1
            i += 1
        elif q[j] >r[k] and green < Y:
            happy += q[j]
            green += 1
            j += 1
        else:
            happy += r[k]
            white += 1
            k += 1
    else:
        if green < Y:
            happy += q[j]
            green += 1
            j += 1
        elif p[i] >r[k] and red < X:
            happy += p[i]
            red += 1
            i += 1
        else:
            happy += r[k]
            white += 1
            k += 1
            
print(happy)

Solution (Python):
x,y,a,b,c,=map(int,input().split())
red=sorted(list(map(int,input().split())),reverse=True)
green=sorted(list(map(int,input().split())),reverse=True)
clear=sorted(list(map(int,input().split())),reverse=True)
red=red[:x]
green=green[:y]

import heapq
heapq.heapify(red)
heapq.heapify(green)

for i in range(c):
    r=heapq.heappop(red)
    g=heapq.heappop(green)
    c=clear[i]
    
    if r>c and g>c:
        heapq.heappush(red,r)
        heapq.heappush(green,g)
    elif r>=g:
        heapq.heappush(red,r)
        heapq.heappush(green,c)
    else:
        heapq.heappush(green,g)
        heapq.heappush(red,c)

print(sum(red)+sum(green))

Solution (Python):
X, Y, A, B, C = list(map(int, input().split()))

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

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

shoku=[]

for i in range(X):
    shoku.append(p[i])

for j in range(Y):
    shoku.append(q[j])

shoku.sort()

r_num=0
for k in range(len(shoku)):
    if r_num == C:
        break

    if shoku[k] < r[r_num]:
        shoku[k] = r[r_num]
        r_num+=1

print(sum(shoku))

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

a,b,X,Y,Z = inpl()
x = sorted(inpl())
y = sorted(inpl())
c = sorted(inpl())
c = c[::-1]

i = X-a; j = Y-b
ind = 0
iok = True; jok = True
while (iok or jok) and ind < Z:
    if x[i] <= y[j]:
        if iok:
            if x[i] < c[ind]:
                x[i] = c[ind]
                ind += 1
                i += 1
                if i >= X: 
                    iok = False
                    i -= 1
            else:
                break
        else:
            if y[j] < c[ind]:
                y[j] = c[ind]
                ind += 1
                j += 1
                if j >= Y: 
                    jok = False
                    j -= 1
            else:
                break
    else:
        if jok:
            if y[j] < c[ind]:
                y[j] = c[ind]
                ind += 1
                j += 1
                if j >= Y: 
                    jok = False
                    j -= 1
            else:
                break
        else:
            if x[i] < c[ind]:
                x[i] = c[ind]
                ind += 1
                i += 1
                if i >= X: 
                    iok = False
                    i -= 1
            else:
                break

print(sum(x[X-a:]) + sum(y[Y-b:]))

Solution (Python):
x,y,a,b,c=[int(j) for j in input().split()]
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
r=[int(j) for j in input().split()]
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:x]
q=q[:y]
l=p+q
l.sort()
ans=sum(l)
for i in l:
    if i<r[0]:
        ans+=r[0]-i
        del r[0]
        if r==[]:
            break
print(ans)

Solution (Python):
from heapq import heappop, heappush
x,y,a,b,c = map(int, input().split())

aa = sorted(map(int, input().split()), reverse=True)
bb = sorted(map(int, input().split()), reverse=True)
cc = sorted(map(int, input().split()), reverse=True)
dd = []
for i in aa[:x]:
    heappush(dd,i)
for i in bb[:y]:
    heappush(dd, i)

for i in range(c):
    heappush(dd, cc[i])
    heappop(dd)

print(sum(dd))

Solution (Python):
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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
li=p[:x]+q[:y]
li.sort()
ans=sum(li)
for i in range(min((x+y),c)):
    if li[i]<r[i]:
        ans+=r[i]-li[i]
    else:
        break
print(ans)

Solution (Python):
X, Y, A, B, C = map(int, input().split())
p = sorted(map(int, input().split()), reverse=True)
q = sorted(map(int, input().split()), reverse=True)
r = sorted(map(int, input().split()), reverse=True)

red = p[0:X]
green = q[0:Y]

eat = sorted(red + green, reverse = True)

h = 0
m = min(len(eat), len(r))
for i in range(m):
    if eat[-(i+1)] >= r[i]:
        h = i
        break
    if i == m - 1:
        h = i + 1

if h > 0:
    eat[-h:] = r[:h]

print(sum(eat))

Solution (Python):

X, Y, A, B, C = map(int, input().split())
red_oishisa = list(map(int, input().split()))
green_oishisa = list(map(int, input().split()))
no_oishisa = list(map(int, input().split()))

red_oishisa = list(sorted(red_oishisa))[::-1]
green_oishisa = list(sorted(green_oishisa))[::-1]
no_oishisa = list(sorted(no_oishisa))[::-1]

oishisa_s = []

i=1
j=1
k=1
while(True):
    if len(oishisa_s) == X+Y:
        break
    
    hikaku_dict = dict()
    if i <= X:
        hikaku_dict['red'] = red_oishisa[i-1]
    if j <= Y:
        hikaku_dict['green'] = green_oishisa[j-1]
    if k <= C:
        hikaku_dict['no'] = no_oishisa[k-1]
    
    max_key, max_value = max(hikaku_dict.items(), key=lambda x:x[1])
    oishisa_s.append(max_value)
    if max_key == 'red':
        i += 1
    elif max_key == 'green':
        j += 1
    elif max_key == 'no':
        k += 1

print(sum(oishisa_s))

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    X, Y, A, B, C = (int(i) for i in input().split())
    P = [-int(i) for i in input().split()]
    Q = [-int(i) for i in input().split()]
    R = [-int(i) for i in input().split()]
    from heapq import heapify, heappop
    heapify(P)
    heapify(Q)
    heapify(R)
    eatP = 0
    eatQ = 0
    eatR = 0
    ans = 0
    while X != eatP and Y != eatQ and eatP + eatQ + eatR < X + Y:
        if P and Q and R:
            cur = min(P[0], Q[0], R[0])
        elif P and Q:
            cur = min(P[0], Q[0])
        elif Q and R:
            cur = min(Q[0], R[0])
        elif R and P:
            cur = min(R[0], P[0])
        elif P:
            cur = P[0]
        elif Q:
            cur = Q[0]
        elif R:
            cur = R[0]
        else:
            break
        ans -= cur
        if R and R[0] == cur:
            heappop(R)
            eatR += 1
        elif P and P[0] == cur:
            heappop(P)
            eatP += 1
        elif Q and Q[0] == cur:
            heappop(Q)
            eatQ += 1

    if eatP + eatQ + eatR == X + Y:
        return print(ans)
    if X != eatP:
        while X != eatP and eatP + eatQ + eatR < X + Y:
            if R and P:
                cur = min(R[0], P[0])
            elif R:
                cur = R[0]
            elif P:
                cur = P[0]
            else:
                break
            ans -= cur
            if R and R[0] == cur:
                heappop(R)
                eatR += 1
            elif P and P[0] == cur:
                heappop(P)
                eatP += 1
    elif Y != eatQ:
        while Y != eatQ and eatP + eatQ + eatR < X + Y:
            if R and Q:
                cur = min(R[0], Q[0])
            elif R:
                cur = R[0]
            elif Q:
                cur = Q[0]
            else:
                break
            ans -= cur
            if R and R[0] == cur:
                heappop(R)
                eatR += 1
            elif Q and Q[0] == cur:
                heappop(Q)
                eatQ += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

X, Y, A, B, C = [int(x) for x in input().split()]
p = sorted([int(x) for x in input().split()], reverse=True)[:X]
q = sorted([int(x) for x in input().split()], reverse=True)[:Y]
pq = deque(sorted(p + q, reverse=True))
r = deque(sorted([int(x) for x in input().split()]))

ans = sum(pq)

for i in range(len(r)):
    if r[-1] > pq[-1]:
        ans += r.pop() - pq.pop()

print(ans)

Solution (Python):
X, Y, A, B, C = list(map(int, input().split()))
P = sorted(list(map(int, input().split())), reverse=True)[:X]
Q = sorted(list(map(int, input().split())), reverse=True)[:Y]
R = sorted(list(map(int, input().split())), reverse=True)  

cands = sorted(P + Q)  

c_ind = 0
r_ind = 0

while True:
    if c_ind >= len(cands) or r_ind >= len(R):
        break
    c = cands[c_ind]
    r = R[r_ind]
    if c >= r:
        break
    else:
        c_ind += 1
        r_ind += 1

print(sum(cands[c_ind:]) + sum(R[:r_ind]))

Solution (Python):
x,y,a,b,c=map(int,input().split())
pt=sorted(list(map(int,input().split())),reverse=True)
qt=sorted(list(map(int,input().split())),reverse=True)
r=sorted(list(map(int,input().split())),reverse=True)
p=pt[:x]
q=qt[:y]

prev=sorted(p)
qrev=sorted(q)



saisho=[0]*(x+y)

pgen=0
qgen=0
genzai=0
gokei=0
powari=False
for i in range(a+b):
    if(prev[pgen]<qrev[qgen]):
        genzai+=prev[pgen]
        pgen+=1
    else:
        genzai+=qrev[qgen]
        qgen+=1
    saisho[i]=genzai
    if(pgen==x):
        powari=True
        gokei=i+1
        break
    if(qgen==y):
        gokei=i+1
        break

if(powari):
    for i in range(y-qgen):
        genzai+=qrev[qgen+i]
        saisho[gokei+i]=genzai
else:
    for i in range(x-pgen):
        genzai+=prev[pgen+i]
        saisho[gokei+i]=genzai


zenbu=saisho[x+y-1]



sikokaisu=min(c,x+y)

ans=[0]*sikokaisu

genc=0
for i in range(sikokaisu):
    genc+=r[i]
    ans[i]=zenbu-saisho[i]+genc



print(max(zenbu,max(ans)))

Solution (Python):
import sys
input = sys.stdin.readline
X, Y, A, B, C = map(int, input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]

p.sort(reverse = True)
q.sort(reverse = True)
red = p[:X]
green = q[:Y]
ans = red + green + r
ans.sort(reverse = True)
print(sum(ans[:X+Y]))

Solution (Python):
X, Y, A, B, C = map(int, input().split())
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
r_list = list(map(int, input().split()))


sorted_p_list = sorted(p_list, reverse=True)[:X]


sorted_q_list = sorted(q_list, reverse=True)[:Y]


s_list = sorted(sorted_p_list + sorted_q_list, reverse=False)


sorted_r_list = sorted(r_list, reverse=True)

for i in range(len(r_list)):
    if sorted_r_list[i] > s_list[i]:
        s_list[i] = sorted_r_list[i]
    else:
        break

print(sum(s_list))

Solution (Python):
x,y,a,b,c=map(int,input().split())
G=[[0]*2 for i in range(a+b+c)]
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
for i in range(a):
  G[i][0]=A[i]
  G[i][1]=0
for i in range(b):
  G[i+a][0]=B[i]
  G[i+a][1]=1
for i in range(c):
  G[i+a+b][0]=C[i]
  G[i+a+b][1]=2
GG=sorted(G,reverse=True)
cc=0
ca=0
cb=0
i=-1
ans=0
f=0
ff=0
while ca<x and cb<y and cc<x+y:
  i=i+1
  ans=ans+GG[i][0]
  if GG[i][1]==0:
    ca=ca+1
    cc=cc+1
    if ca==x:
      f=1
  elif GG[i][1]==1:
    cb=cb+1
    cc=cc+1
    if cb==y:
      f=2
  else:
    cc=cc+1
  if cc==x+y:
    ff=1
if ff==1:
  print(ans)
  exit()
if f==1:
  ans=0
  for i in range(x):
    ans=ans+A[i]
  GGG=sorted(G[a:a+b+c],reverse=True)
  for i in range(y):
    ans=ans+GGG[i][0]
  print(ans)
elif f==2:
  ans=0
  for i in range(y):
    ans=ans+B[i]
  GGG=sorted(G[0:a]+G[a+b:a+b+c],reverse=True)
  for i in range(x):
    ans=ans+GGG[i][0]
  print(ans)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    X, Y, A, B, C = lr()
    p = sorted(lr())[-X:][::-1]
    q = sorted(lr())[-Y:][::-1]
    r = sorted(lr())
    tmp = r.pop()
    pt = p[-1]
    qt = q[-1]
    ext = [0]
    while (tmp > pt or tmp > qt) and len(r) >= 0:
        if pt > qt:
            g = q.pop()
            ext.append(tmp)
            if len(q) == 0:
                qt = pow(10, 9)+1
            else:
                qt = q[-1]
        else:
            g = p.pop()
            ext.append(tmp)
            if len(p) == 0:
                pt = pow(10, 9)+1
            else:
                pt = p[-1]
        if len(r) > 0:
            tmp = r.pop()
        else:
            break
    print(sum(p)+sum(q)+sum(ext))
resolve()

Solution (Python):
x, y, a, b, c = map(int, input().split())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
rs = list(map(int, input().split()))
ps.sort()
ps = ps[-x:]
qs.sort()
qs = qs[-y:]
pqs = ps + qs
pqs.sort()
rs.sort(reverse=True)

answer = sum(pqs)
for i in range(min(x+y, c)):
    diff = rs[i] - pqs[i]
    if diff < 0:
        break
    answer += diff

print(answer)

Solution (Python):
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(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)

i_p = 0
i_q = 0
i_r = 0

ans = 0
for _ in range(X + Y):
    cand = []
    if i_p < X: cand.append((p[i_p], 'A'))
    if i_q < Y: cand.append((q[i_q], 'B'))
    if i_r < C: cand.append((r[i_r], 'C'))
    
    cand.sort(key=lambda x: x[0])
    ans_tmp, color = cand[-1]
    ans += ans_tmp
    
    if color == 'A': i_p += 1
    elif color == 'B': i_q += 1
    else: i_r += 1

print(ans)

Solution (Python):

X, Y, NA, NB, NC = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())

A = sorted(A)[-X:]
B = sorted(B)[-Y:]
C = sorted(C)[::-1]

ans = sum(A) + sum(B)


i = 0
j = 0
num_A = len(A)
num_B = len(B)
for c in C:
    if i < num_A and j < num_B:
        if A[i] < B[j] and A[i] < c:

            ans += c - A[i]
            i += 1
        elif A[i] >= B[j] and B[j] < c:

            ans += c - B[j]
            j += 1

    elif i < num_A:
        if A[i] < c:
            ans += c - A[i]
            i += 1

    elif j < num_B:
        if B[j] < c:
            ans += c - B[j]
            j += 1

    else:
        break

print(ans)

Solution (Python):
from heapq import heappush, heappop

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

p_vals = []
ps = map(int, input().split(" "))
for p in ps:
    heappush(p_vals, -p)

q_vals = []
qs = map(int, input().split(" "))
for q in qs:
    heappush(q_vals, -q)

r_vals = []
rs = map(int, input().split(" "))
for r in rs:
    heappush(r_vals, -r)


for _ in range(x):
    val = -heappop(p_vals)
    heappush(r_vals, -val)

for _ in range(y):
    val = -heappop(q_vals)
    heappush(r_vals, -val)




out = 0
for _ in range(x+y):
    val = -heappop(r_vals)
    out += val


print(out)

Solution (Python):
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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

p_top = P[0:X]
q_top = Q[0:Y]
pq_top = p_top+q_top


pqr_top=pq_top+R
pqr_top.sort(reverse=True)

print(sum(pqr_top[0:X+Y]))

Solution (Python):
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()))

pqr = []
for i in p:
  pqr.append([i, 1])
for i in q:
  pqr.append([i, 2])
for i in r:
  pqr.append([i, 3])

pqr.sort(reverse=True)

red = x
green = y
total = x+y
ans = 0

for i_pqr in pqr:
  if i_pqr[1] == 1 and red > 0 and total > 0:
    red -= 1
    total -= 1
    ans += i_pqr[0]
  elif i_pqr[1] == 2 and green > 0 and total > 0:
    green -= 1
    total -= 1
    ans += i_pqr[0]
  elif i_pqr[1] == 3 and total > 0:
    total -= 1
    ans += i_pqr[0]
  
  if total == 0:
    break

print(ans)

Solution (Python):
from collections import deque

X, Y, A, B, C = map(int, input().split())
P = sorted([int(p) for p in input().split()], reverse=True)[:X]
Q = sorted([int(q) for q in input().split()], reverse=True)[:Y]
R = sorted([int(r) for r in input().split()], reverse=True)

P = deque(P)
Q = deque(Q)
R = deque(R)

m = min(P[X-1], Q[Y-1])
while R[0] > m:
    if P[X-1] < Q[Y-1]:
        P.pop()
        P.appendleft(R[0])
        R.popleft()
    else:
        Q.pop()
        Q.appendleft(R[0])
        R.popleft()
    m = min(P[X-1], Q[Y-1])
    if len(R) == 0:
        break
print(sum(P) + sum(Q))

Solution (Python):
import heapq

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()))
all_list = []

for p in P:
    all_list.append([-p, "A"])

for q in Q:
    all_list.append([-q, "B"])
for r in R:
    all_list.append([-r, "C"])
heapq.heapify(all_list)

ans = 0
count = 0
X_count = 0
Y_count = 0
while count < X+Y:
    pop = heapq.heappop(all_list)
    if pop[1] == "A" and X_count < X:
        ans -= pop[0]
        X_count += 1
        count += 1
    elif pop[1] == "B" and Y_count < Y:
        ans -= pop[0]
        Y_count += 1
        count += 1
    elif pop[1] == "C":
        ans -= pop[0]
        count += 1
print(ans)

Solution (Python):
X, Y, A, B, C = map(int, input().split())
P = sorted(list(map(int, list(input().split()))), reverse=True)
Q = sorted(list(map(int, list(input().split()))), reverse=True)
R = sorted(list(map(int, list(input().split()))), reverse=True)

toEat = P[:X] + Q[:Y]
toEat.sort()

for i in range(min(X + Y, len(R))):
    if R[i] > toEat[i]:
        toEat[i] = R[i]
    else:
        break

print(sum(toEat))

Solution (Python):
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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
ans = sum(P[:X]) + sum(Q[:Y])
x = X-1
y = Y-1
c = 0
x_flag = False
y_flag = False
while x >= 0 and y >= 0 and c < C:
    if x_flag == False:
        p = P[x]
    else:
        p = 10**9 + 1
    if y_flag == False:
        q = Q[y]
    else:
        q = 10**9 + 1
    r = R[c]
    if p <= q and p < r:
        ans = ans - p + r
        x -= 1
        c += 1
        if x == -1:
            x = 0
            x_flag = True
    elif q < p and q < r:
        ans = ans - q + r
        y -= 1
        c += 1
        if y == -1:
            y = 0
            y_flag = True
    else:
        break

print(ans)

Solution (Python):
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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
Sum = 0
for i in range(X):
  Sum += p[i]
for j in range(Y):
  Sum += q[j]
cntp, cntq = X-1, Y-1
flg = 0

for i in range(C):
  dp, dq = 0, 0
  if cntp >= 0:
    if r[i] > p[cntp]:
      dp = r[i] - p[cntp]
  if cntq >= 0:
    if r[i] > q[cntq]:
      dq = r[i] - q[cntq]
  if dp == 0 and dq == 0:
    break
  else:
    if dp >= dq:
      Sum += dp
      cntp -= 1
    else:
      Sum += dq
      cntq -= 1
print(Sum)

Solution (Python):
X, Y, A, B, C = map(int, input().split())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
p.sort()
q.sort()
r.sort()
red_remain = X
green_remain = Y
ans = 0
flag = False
for i in range(X+Y):
    if flag:
        flag = False
        continue
    if red_remain == 0 or len(p) == 0:
        if green_remain == 0 or len(q) == 0:
            ans += r.pop(-1)
        elif len(r) == 0:
            ans += q.pop(-1)
            green_remain -= 1
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
        else:
            ans += q.pop(-1)
            green_remain -= 1
    elif green_remain == 0 or len(q) == 0:
        if len(r) == 0:
            ans += p.pop(-1)
            red_remain -= 1
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
        else:
            ans += p.pop(-1)
            red_remain -= 1
    elif len(r) == 0:
        if p[-1] == q[-1]:
            if i == X+Y-1:
                ans += p[-1]
            else:
                flag = True
                ans += p.pop(-1) + q.pop(-1)
                red_remain -= 1
                green_remain -= 1
        elif p[-1] < q[-1]:
            ans += q.pop(-1)
            green_remain -= 1
        else:
            ans += p.pop((-1))
            red_remain -= 1
    else:
        if p[-1] < r[-1] and q[-1] < r[-1]:
            ans += r.pop(-1)
        elif p[-1] == q[-1]:
            if i == X+Y-1:
                ans += p[-1]
            else:
                flag = True
                ans += p.pop(-1) + q.pop(-1)
                red_remain -= 1
                green_remain -= 1
        elif p[-1] < q[-1]:
            ans += q.pop(-1)
            green_remain -= 1
        else:
            ans += p.pop((-1))
            red_remain -= 1
print(ans)

Solution (Python):
import heapq

X, Y, A, B, C = map(int, input().split())
p = sorted(list(map(int, input().split())), reverse=True)
q = sorted(list(map(int, input().split())), reverse=True)
r = list(map(int, input().split()))

Red = p[:X]
Green = q[:Y]

h = sorted(Red + Green)
heapq.heapify(h)
for i in r:
    heapq.heappushpop(h, i)
print(sum(h))

Solution (Python):
x, y, a, b, c = map(int, input().split())
p_s = sorted(list(map(int, input().split())), reverse=True)[:x]
q_s = sorted(list(map(int, input().split())), reverse=True)[:y]
r_s = sorted(list(map(int, input().split())), reverse=True)

s_s = sorted(p_s + q_s)
ans = sum(s_s)
for i in range(c):
    tmp = ans - s_s[i] + r_s[i]
    if tmp <= ans:
       break
    ans = tmp

print(ans)

Solution (Python):
from heapq import heapify, heappop, heappush

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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
PQ = P[:x] + Q[:y]
heapify(PQ)

for r in R:
  pq = heappop(PQ)
  if pq < r:
    heappush(PQ, r)
  else:
    heappush(PQ, pq)
ans = sum(PQ)
print(ans)

Solution (Python):
x, y, a, b, c = map(int, input().split())
pqr = [map(int, input().split()) for i in range(3)]

A = []
for i in range(3):
        for j in pqr[i]:
                A.append([j, i])
A.sort(key=lambda x:-x[0])

ans = 0
cnt = 0
for v, t in A:
        if cnt==x+y: break
        if t==0:
                if x:
                        x -= 1
                        ans += v
        elif t==1:
                if y:
                        y -= 1
                        ans += v
        else:
                if x+y>cnt:
                        cnt += 1
                        ans += v
                else: break
print(ans)

Solution (Python):
import heapq as hq

X, Y, A, B, C = map(int,input().split())
p = list(map(int,input().split())); p.sort(); p.reverse()
q = list(map(int,input().split())); q.sort(); q.reverse()
r = list(map(int,input().split())); r.sort(); r.reverse()

pq1 = []; hq.heapify(pq1) 
for i in range(X):
    hq.heappush(pq1, p[i])
for i in range(Y):
    hq.heappush(pq1, q[i])
pq2 = []; hq.heapify(pq2) 
for i in range(C):
    hq.heappush(pq2, -r[i])
while (len(pq2) and pq1[0] < -pq2[0]):
    hq.heappush(pq1, -hq.heappop(pq2))
    hq.heappop(pq1) 
print(sum(pq1))

Solution (Python):
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()))

S = [(p, 0) for p in P] + [(q, 1) for q in Q] + [(r, 2) for r in R]
S.sort(reverse=True)

ans = 0
ct = [0] * 3
for s in S:
    if sum(ct) >= X+Y:
        break
    elif s[1] == 0 and ct[0] < X:
        ans += s[0]
        ct[0] += 1
    elif s[1] == 1 and ct[1] < Y:
        ans += s[0]
        ct[1] += 1
    elif s[1] == 2 and (ct[0] < X or ct[1] < Y):
        ans += s[0]
        ct[2] += 1

print(ans)

Solution (Python):
x,y,A,B,C=map(int,input().split())
pa=list(map(int,input().split()))
qb=list(map(int,input().split()))
rc=list(map(int,input().split()))
pa.sort()
qb.sort()
temp=0

pa=pa[-x:]
qb=qb[-y:]
paa=pa+rc
paa.sort()
paa1=paa[-x:]
paa2=paa[:-x]
qbb=qb+paa2
qbb.sort()
qbb1=qbb[-y:]
print(sum(paa1)+sum(qbb1))

Solution (Python):
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(reverse=True)
q.sort(reverse=True)
r.sort()
p = deque(p[:X][::-1])
q = deque(q[:Y][::-1])
ans = 0
while X + Y > len(p) + len(q):
    ans += r.pop()
while r:
    R = r.pop()
    if p[0] < q[0]:
        if p[0] < R:
            p.popleft()
            p.append(R)
        else:
            break
    else:
        if q[0] < R:
            q.popleft()
            q.append(R)
        else:
            break
ans += sum(p) + sum(q)
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
import heapq

def main():
    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 = p[-X:]
    q = q[-Y:]
    r = r[-X-Y:]
    use = p+q
    heapq.heapify(use)
    heapq.heapify(r)
    while len(r)>0:
        unt = heapq.heappop(use)
        nc = heapq.heappop(r)
        if nc > unt:
            heapq.heappush(use,nc)
        else:
            heapq.heappush(use,unt)

    print(sum(use))
    
if __name__ == "__main__":
    main()

Solution (Python):
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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

total = p[:x]+q[:y]
total.sort(reverse=True)



for i in range(min(x+y, c)):
  if total[-(i+1)]<r[i]:
    total[-(i+1)] = r[i]
  else:
    break

print(sum(total))

Solution (Python):
X, Y, A, B, C = map(int, input().split())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]

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

p = p[-X:]
q = q[-Y:]

pi = 0
qi = 0

for rv in r :
  if 0 <= pi < X and 0 <= qi < Y :
    if p[pi] <= q[qi] and p[pi] < rv :
      p[pi] = rv
      pi += 1
      continue
    if q[qi] <= p[pi] and q[qi] < rv : 
      q[qi] = rv
      qi += 1
      continue
  
  if 0 <= pi < X and p[pi] < rv :
    p[pi] = rv
    pi += 1
    continue
    
  if 0 <= qi < Y and q[qi] < rv :
    q[qi] = rv
    qi += 1
    continue
    
print(sum(p[:X]) + sum(q[:Y]))