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 = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted([(i,0) for i in p],reverse=True)
q = sorted([(i,1) for i in q],reverse=True)
r = sorted([(i,2) for i in r],reverse=True)
used = [0] * c
apple = sorted(p + q + r, reverse=True)
P,Q,R = 0,0,0
ans = 0
for i,k in apple[:x+y]:
  if k == 0:
    if P < x:
      P += 1
      ans += i
  elif k == 1:
    if Q < y:
      Q += 1
      ans += i
  else:
    R += 1
    ans += i
res = x + y - (P+Q+R)
if res:
  if P == x:
    apple = sorted(q[Q:] + r[R:],reverse=True)
    for i,k in apple[:res]:
      ans += i
  elif Q == y:
    apple = sorted(p[P:]+r[R:],reverse=True)
    for i,k in apple[:res]:
      ans += i
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)
a = X-1
b = Y-1
SA = sum(p[:X])
SB = sum(q[:Y])
for i in range(C):
    if r[i] >= p[a] and (b==-1 or p[a] <= q[b]) and a >= 0:
        SA = SA - p[a] + r[i]
        a -= 1
    elif r[i] >= q[b] and b >= 0:
        SB = SB - q[b] + r[i]
        b -= 1
    else:
        break
print(SA+SB)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

import heapq

def dijkstra(s,n,w,cost):
    
    
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

def warshall_floyd(n,d):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d





x,y,a,b,c = m()

al = []

p = l()

q = l()

r = l()

p = [[p[i],1] for i in range(a)]

q = [[q[i],2] for i in range(b)]

r = [[r[i],3] for i in range(c)]



al = []

for i in range(a):
    al.append(p[i])


for i in range(b):
    al.append(q[i])

for i in range(c):
    al.append(r[i])

al.sort(reverse = True)
ans = 0
coa = 0
cob = 0
coc = 0
i = 0

while i <= a+ b+ c:
    on = al[i]
    if on[1] == 1:
        if coa == x:
            i += 1
            continue
        else:
            coa += 1
            ans += on[0]

    elif on[1] == 2:
        if cob == y:
            i += 1
            continue
        else:
            cob += 1
            ans += on[0]

    else:
        coc += 1
        ans += on[0]
    
    if coa + cob + coc == x+y:
        print(ans)
        exit()
    
    i += 1

Solution (Python):

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

a_list=list(map(int,input().split()))
a_list=sorted(a_list)
b_list=list(map(int,input().split()))
b_list=sorted(b_list)
a_list=a_list[-x:]
b_list=b_list[-y:]
c_list=list(map(int,input().split()))
for i in range(c):
    c_list[i]=-c_list[i]
import heapq
heapq.heapify(a_list)
heapq.heapify(b_list)
heapq.heapify(c_list)

flag=True
while flag and c_list:
    x=heapq.heappop(c_list)
    x=x*(-1)
    
    min_a=heapq.heappop(a_list)
    min_b=heapq.heappop(b_list)
    if min(min_a,min_b)>=x:
        flag=False
        heapq.heappush(a_list,min_a)
        heapq.heappush(b_list,min_b)
    else:
        if min_a>=min_b:
            heapq.heappush(a_list,min_a)
            heapq.heappush(b_list,x)
         
        elif min_a<min_b:
            heapq.heappush(b_list,min_b)
            heapq.heappush(a_list,x)

sum_a=sum(list(a_list))
sum_b=sum(list(b_list))

print(sum_a+sum_b)

Solution (Python):
import heapq

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()))

p_list.sort()
q_list.sort()
r_list.sort()

p_list.reverse()
q_list.reverse()
r_list.reverse()

p_ls = p_list[:x]
q_ls = q_list[:y]
r_ls = r_list[:min(x+y, c)]

heapq.heapify(p_ls)
heapq.heapify(q_ls)
r_ls = list(map(lambda x: x*(-1), r_ls))
heapq.heapify(r_ls)

while True:
    if len(r_ls) == 0:
        break
    elif (-r_ls[0]) < p_ls[0] and (-r_ls[0]) < q_ls[0]:
        break
    elif p_ls[0] < q_ls[0]:
        heapq.heappop(p_ls)
        heapq.heappush(p_ls, -heapq.heappop(r_ls))
    else:
        heapq.heappop(q_ls)
        heapq.heappush(q_ls, -heapq.heappop(r_ls))

print(sum(p_ls) + sum(q_ls))

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
x,y,a,b,c=map(int,input().split())
P=sorted([int(i) for i in input().split()],reverse=True)
Q=sorted([int(i) for i in input().split()],reverse=True)
R=sorted([int(i) for i in input().split()],reverse=True)
import heapq
P,Q=P[:x],Q[:y]
heapq.heapify(P)
heapq.heapify(Q)
for r in R:
  p=heapq.heappop(P)
  q=heapq.heappop(Q)
  if r<=p and r<=q:
    heapq.heappush(P,p)
    heapq.heappush(Q,q)
    break
  else:
    if p<=q:
      heapq.heappush(P,r)
      heapq.heappush(Q,q)
    else:
      heapq.heappush(P,p)
      heapq.heappush(Q,r)
print(sum(P)+sum(Q))

Solution (Python):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


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

while len(P)>X:
    P.pop()
while len(Q)>Y:
    Q.pop()

flag=1
while flag:
    if not R:
        break

    flag=0
    if P and Q:
        if P[-1]<Q[-1]:
            if P[-1]<R[-1]:
                P.pop()
                lst.append(R.pop())
                flag=1
        else:
            if Q[-1]<R[-1]:
                Q.pop()
                lst.append(R.pop())
                flag=1
    else:
        if P:
            if P[-1]<R[-1]:
                P.pop()
                lst.append(R.pop())
                flag=1
        elif Q:
            if Q[-1]<R[-1]:
                Q.pop()
                lst.append(R.pop())
                flag=1

for i in range( X+Y-(len(P)+len(Q)+len(lst))):
    if R:
        lst.append(R.pop())
    else:
        break
print(sum(P)+sum(Q)+sum(lst))

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

red = P[:X]
blue = Q[:Y]
heapq.heapify(red)
heapq.heapify(blue)

while R:
    white = R.pop()
    if red[0] < blue[0] and red[0] < white:
        heapq.heappushpop(red, white)
    elif red[0] >= blue[0] and blue[0] < white:
        heapq.heappushpop(blue, white)
    else:
        break

ans = sum(red) + sum(blue)
print(ans)

Solution (Python):
from collections import deque

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()))
apples = []
for i in range(A):
    apples.append([p[i],0])
for i in range(B):
    apples.append([q[i],1])
for i in range(C):
    apples.append([r[i],2])
apples.sort()
apples = deque(apples)

apples_number = 0
L = X+Y
ans = 0
while apples_number < L:
    apple = apples.pop()
    if apple[1] == 0 and X > 0:
        ans += apple[0]
        X -= 1
        apples_number += 1
    if apple[1] == 1 and Y > 0:
        ans += apple[0]
        Y -= 1
        apples_number += 1
    if apple[1] == 2:
        ans += apple[0]
        apples_number += 1

print(ans)

Solution (Python):
import heapq

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()))

p_target = heapq.nlargest(x, p_list)
q_target = heapq.nlargest(y, q_list)
r_target = heapq.nlargest(c, r_list)

p_target.extend(q_target)
p_target.extend(r_target)

p_target.sort(reverse=True)
print(sum(p_target[:x+y]))

Solution (Python):
import sys
input = 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()))
PQ = [(p,0) for p in P] + [(q,1) for q in Q]
PQ.sort(key=lambda x:x[0])
R.sort()
ans = x = y = z = 0
while x+y+z < X+Y:
    if x==X:
        while PQ[-1][1]==0:
            PQ.pop()
    if y==Y:
        while PQ[-1][1]==1:
            PQ.pop()
    if R and R[-1] > PQ[-1][0]:
        a = R.pop()
        z += 1
    else:
        a,k = PQ.pop()
        if k:
            y += 1
        else:
            x += 1
    ans += a
print(ans)

Solution (Python):
import sys
import math
import numpy as np
import itertools

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()] 

p=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)




u=[]
for i in range(x):
    u.append(p[i])
for i in range(y):
    u.append(q[i])
u=sorted(u)

for i in range(min(len(u),len(r))):
    if u[i]<r[i]:
        u[i]= r[i]

answer = 0
for a in u:
    answer += a
print(answer)

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

cand = P[-X:] + Q[-Y:]
cand.sort()
i = 0
while(i<X+Y and i<C):
    if(cand[i]>=R[i]):
        break
    i += 1
print(sum(cand[i:])+sum(R[:i]))

Solution (Python):
x, y, a, b, c = [int(x) for x in input().split()]
red = [int(x) for x in input().split()]
green = [int(x) for x in input().split()]
nocolor = [int(x) for x in input().split()]
red.sort(reverse=True)
green.sort(reverse=True)
nocolor.sort(reverse=True)

eating = red[:x] + green[:y]
eating.sort()
delicious = sum(eating)



for e, n in zip(eating, nocolor):
    if e < n:
        delicious = delicious - e + n

print(delicious)

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

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 = P[:X]
Q = Q[:Y]

heapify(P)
heapify(Q)

for i in range(C):
    p = heappop(P)
    q = heappop(Q)
    if p <= q:
        heappush(Q, q)
        if R[i] >= p:
            heappush(P, R[i])
        else:
            heappush(P, p)
    else:
        heappush(P, p)
        if R[i] >= q:
            heappush(Q, R[i])
        else:
            heappush(Q, q)

print(sum(P) + sum(Q))

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 = P[:X]
Q = Q[:Y]
P.extend(Q)
P.sort()
Z = list()
for i in P:
  if len(R) == 0:
    best = i
  else:
    best = R[0]
  if best > i:
    Z.append(R[0])
    R.pop(0)
  else:
    Z.append(i)
print(sum(Z))

Solution (Python):
X,Y,A,B,C=map(int,input().split())
p=list(map(lambda x:(int(x),0),input().split()))
q=list(map(lambda x:(int(x),1),input().split()))
r=list(map(lambda x:(int(x),2),input().split()))
l=p+q+r
l.sort(reverse=1)
a=0
x,y=0,0
for i in l[:X+Y]:
    if i[1]==0:
        x+=1
    elif i[1]==1:
        y+=1
    a+=i[0]
if x>X:
    k=x-X
    t=X+Y-1
    while x>X:
        if l[t][1]==0:
            a-=l[t][0]
            x-=1
        t-=1
    t=X+Y
    while k>0:
        if l[t][1]!=0:
            a+=l[t][0]
            k-=1
        t+=1
elif y>Y:
    k=y-Y
    t=X+Y-1
    while y>Y:
        if l[t][1]==1:
            a-=l[t][0]
            y-=1
        t-=1
    t=X+Y
    while k>0:
        if l[t][1]!=1:
            a+=l[t][0]
            k-=1
        t+=1
print(a)

Solution (Python):
from collections import deque
x, y, a, b, c = map(int, input().split())
p = deque(sorted(list(map(int, input().split())) + [0]))
q = deque(sorted(list(map(int, input().split())) + [0]))
r = deque(sorted(list(map(int, input().split())) + [0]))

xy = x+y
count = 0
pp, qq, rr = p.pop(), q.pop(), r.pop()
maxelements = max(pp, qq, rr)

for i in range(xy):
    if pp == maxelements:
        count += pp
        x -= 1
        pp = p.pop() if x != 0 else 0
        xy -= 1
    elif qq == maxelements:
        count += qq
        y -= 1
        qq = q.pop() if y != 0 else 0
        xy -= 1
    elif rr == maxelements:
        count += rr
        rr = r.pop()
        xy -= 1

    if xy == 0:
        break
    else:
        maxelements = max(pp, qq, rr)

print(count)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop, heapify
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    X,Y,A,B,C=MI()
    R=LI()
    G=LI()
    W=LI()
    R.sort(reverse=True)
    G.sort(reverse=True)
    W.sort(reverse=True)
    
    tmp = []
    tmp.extend(R[:X])
    tmp.extend(G[:Y])
    heapify(tmp)
    for w in W:
        if(tmp[0]<w):
            heappushpop(tmp, w)
    print(sum(tmp))    
if __name__ == '__main__':
    main()

Solution (Python):
import heapq
X,Y,a,b,c=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

heapq.heapify(A)
if len(A)<X:
    while len(A)<X:
        heapq.heappush(A,0)
else:
    while len(A)>X:
        heapq.heappop(A)
        
heapq.heapify(B)
if len(B)<X:
    while len(B)<Y:
        heapq.heappush(B,0)
else:
    while len(B)>Y:
        heapq.heappop(B)


C.sort(reverse=True)
for i in range(c):
    if A[0]<B[0] and A[0]<C[i]:
        heapq.heappop(A)
        heapq.heappush(A,C[i])
    elif B[0]<=A[0] and B[0]<C[i]:
        heapq.heappop(B)
        heapq.heappush(B,C[i])
    else:
        break
A=list(A)
B=list(B)
print(sum(A)+sum(B))

Solution (Python):
inpl = lambda: list(map(int,input().split()))
X, Y, A, B, C = inpl()
N = [A, B, C]
P = []
T = []
for n in range(3):
    P=inpl()
    for i in range(N[n]):
        T.append((P[i],n))
T.sort(reverse=True)
M = [X, Y, A+B+C]
ans = 0
cur = [0,0,0]
k = 0
i = 0
while i < X+Y:
    v, c = T[k]
    if cur[c] < M[c]:
        ans += v
        cur[c] += 1
        i += 1
        k += 1
    else:
        k += 1
print(ans)

Solution (Python):
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())),reverse=1)
q=sorted(list(map(int,input().split())),reverse=1)
r=list(map(int,input().split()))
a=sorted(p[:x]+q[:y]+r,reverse=1)
print(sum(a[:x+y]))

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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)
D = []
for i in range(X):
  D.append(P[i])
for j in range(Y):
  D.append(Q[j])
for ri in R:
  D.append(ri)

D.sort(reverse = True)
print(sum(D[:X+Y]))

Solution (Python):
import sys
input = 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)
t = []
for _ in range(X): t.append(p.pop())
for _ in range(Y): t.append(q.pop())
t.sort()
for i in range(min(C, X + Y)):
  if t[i] < r[i]: t[i] = r[i]
print(sum(t))

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)
li=[]
for i in range(X):
    li.append(p[i])
for i in range(Y):
    li.append(q[i])
n=0
res=sum(li)
li=sorted(li)
for i in range(len(li)):
    if(li[i]<r[n]):
        res+=(r[n]-li[i])
        n+=1
    else:
        break
    if(n==len(r)):
        break
print(res)

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)

anslist = p[:x] + q[:y]
anslist.sort()
for i, num in enumerate(r):
    if i >= x+y:
        break
    if num > anslist[i]:
        anslist[i] = num

print(sum(anslist))

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()
q.sort()
r.sort()
l = p[a-x:a] + q[b-y:b]
l.sort()

j = 0
for i in range(min(c,x+y)):
    if l[j] < r[c-i-1]:
        l[j] = r[c-i-1]
        j += 1
print(sum(l))

Solution (Python):


import sys
from heapq import heapify, heappop, heappush

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

X, Y, N, M, L = MAP()
A = LIST()
B = LIST()
C = LIST()

A.sort(reverse=1)
B.sort(reverse=1)
C.sort(reverse=1)

que = []
que += A[:X]
que += B[:Y]
heapify(que)

for c in C:
    mn = heappop(que)
    if c > mn:
        heappush(que, c)
    else:
        heappush(que, mn)
ans = sum(que)
print(ans)

Solution (Python):
import numpy as np
X, Y, A, B, C = list(map(int, input().split()))
p = sorted(list(map(int, input().split())))
q = sorted(list(map(int, input().split())))
r = np.array(sorted(list(map(int, input().split())), reverse=True))

p_cut = np.array(p[A-X:])
q_cut = np.array(q[B-Y:])
pi = 0
qi = 0
for i in range(len(r)):
    if pi >= X and qi >= Y:
        break
    if pi >= X:
        if q_cut[qi] >= r[i]:
            break
        q_cut[qi] = r[i]
        qi += 1
    elif qi >= Y:
        if p_cut[pi] >= r[i]:
            break
        p_cut[pi] = r[i]
        pi += 1
    elif p_cut[pi] >= q_cut[qi] :
        if q_cut[qi] >= r[i]:
            break
        q_cut[qi] = r[i]
        qi += 1
    else:
        if p_cut[pi] >= r[i]:
            break
        p_cut[pi] = r[i]
        pi += 1
print(np.sum(p_cut) + np.sum(q_cut))

Solution (Python):
x, y, *_ = map(int, input().split())
p = [(int(i), 1) for i in input().split()]
q = [(int(i), 2) for i in input().split()]
r = [(int(i), 0) for i in input().split()]
s = sorted(p + q + r, reverse=True)

i = 0
a, b, c = 0, 0, 0
cnt = 0
while a + b + c < x + y:
    d, e = s[i]
    if e == 1 and a < x:
        cnt += d
        a += 1
    elif e == 2 and b < y:
        cnt += d
        b += 1
    elif e == 0:
        cnt += d
        c += 1
    i += 1
print(cnt)

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()

p = p[:X]
a = q[:Y] + p
z = []
a.sort(reverse = True)

while len(a) != 0 and len(r) != 0 and r[-1] > a[-1]:
    k = r.pop()
    del a[-1]
    z.append(k)   

print(sum(a) + sum(z))

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()))
ans = []
P.sort()
Q.sort()
R.sort()
for i in range(x):
    ans.append(P[a-i-1])
for i in range(y):
    ans.append(Q[b-i-1])
flag = True
ans.sort()
ans_sum = sum(ans)
cur = c-1
temp = 0
for i in range(c):
    if cur >= 0 and temp <= (x+y-1):
        if R[cur] >= ans[temp]:
            ans_sum += R[cur]-ans[temp]
            cur -= 1
            temp += 1
print(ans_sum)

Solution (Python):

from heapq import heapify, heappush, heappop

x,y,a,b,c = map(int, input().split())
red = list(map(int, input().split()))
green = list(map(int, input().split()))
transparence = list(map(lambda x: -int(x), input().split()))

red.sort(reverse=True)
green.sort(reverse=True)
heapify(transparence)

L = []

for i in range(min(a, x)):
    heappush(L, red[i])
for i in range(min(0, x-a)):
    item = heappop(transparence)
    heappush(L, -item)


for i in range(min(b, y)):
    heappush(L, green[i])
for i in range(min(0, y-b)):
    item = heappop(transparence)
    heappush(L, -item)

while True:
    if len(transparence)<= 0:
        break
    item = heappop(transparence)
    min_L = heappop(L)
    if min_L < -item:
        heappush(L, -item)
    else:
        heappush(L, min_L)
        break


print(sum(L))

Solution (Python):
x, y, a, b, c = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
A = A[:x]
B.sort(reverse=True)
B = B[:y]

AB = A + B
AB.sort(reverse=True)
total = sum(AB)
C.sort()

while AB and C:
    point = AB.pop()
    candidate = C.pop()
    if candidate > point:
        total += candidate - point
    else:
        break

print(total)

Solution (Python):
from collections import deque
x, y, a, b, c = [int(i) for i in input().split()]
lp = sorted([int(i) for i in input().split()])
if len(lp) != x:
  del lp[:len(lp)-x]
lp = deque(lp)
lq = sorted([int(i) for i in input().split()])
if len(lq) != y:
  del lq[:len(lq)-y]
lq = deque(lq)
lr = sorted([int(i) for i in input().split()])
lr = deque(lr)


while True:
  if len(lr) == 0:
    break
  r_max = lr[len(lr)-1]
  p_min = lp[0]
  q_min = lq[0]
  if r_max <= p_min and r_max <= q_min:
    break
  elif r_max > q_min and p_min >= q_min:
    del lq[0]
    lq.append(lr.pop())
  elif r_max > p_min and q_min >= p_min:
    del lp[0]
    lp.append(lr.pop())


print(sum(lp) + sum(lq))

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)
np=p[0:x]
nq=q[0:y]
np.extend(nq)
m=min(np)
nr=[i for i in r if i>=m]
np.extend(nr)
np.sort(reverse=True)
print(sum(np[0:x+y]))

Solution (Python):
from heapq import heappush, heappop


def solve(x, y, a, b, c, p_list, q_list, r_list):
    res = sum(p_list[:x]) + sum(q_list[:y])
    res_max = res
    
    h = []
    for i in range(x):
        heappush(h, p_list[i])
    for j in range(y):
        heappush(h, q_list[j])
    for i in range(min(x + y, c)):
        d = heappop(h)
        res -= d
        res += r_list[i]
        res_max = max(res, res_max)
        

    return res_max


def main():
    x, y, a, b, c = map(int, input().split())
    p_list = list(sorted(list(map(int, input().split())), reverse=True))
    q_list = list(sorted(list(map(int, input().split())), reverse=True))
    r_list = list(sorted(list(map(int, input().split())), reverse=True))
    res = solve(x, y, a, b, c, p_list, q_list, r_list)
    print(res)
    
    


def test():
    assert solve(1, 2, 2, 2, 1, [4, 2], [5, 1], [3]) == 12
    assert solve(2, 2, 2, 2, 2, [8, 6], [9, 2], [2, 1]) == 25
    assert solve(2, 2, 4, 4, 4, [14, 13, 12, 11], [24, 23, 22, 21], [4, 3, 2, 1]) == 74


if __name__ == "__main__":
    test()
    main()

Solution (Python):
x, y, a, b, c = map(int, input().split()) 
z = min(c, x+y)
p = sorted(list(map(int, input().split())))[a - x:]
q = sorted(list(map(int, input().split())))[b - y:]
r = sorted(list(map(int, input().split())))[c - z:]

s = sorted(p + q + r)[z:]

print(sum(s))

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)

pi, qi, ri = 0, 0, 0
ans = 0
for i in range(X + Y):
  x, y, z = 0, 0, 0
  if pi != A and X != 0:
    x = p[pi]
  if qi != B and Y != 0:
    y = q[qi]
  if ri != C:
    z = r[ri]

  if z >= x and z >= y:
    ans += z
    ri += 1
  elif x >= y:
    ans += x
    pi += 1
    X -= 1
  else:
    ans += y
    qi += 1
    Y -= 1

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)
r+=[0]
del p[x:]
del q[y:]
p.reverse()
q.reverse()
i,j,k=0,0,0
while min(p[i],q[j])<r[k] and k<c:
  if p[i]<=q[j]:
    p[i]=r[k]
    i+=1
  else:
    q[j]=r[k]
    j+=1
  k+=1
  if i==x or j==y:
    break

if i==x:
  while q[j]<r[k] and k<c:
    q[j]=r[k]
    j+=1
    k+=1
    if j==y:
      break
elif j==y:
  while p[i]<r[k] and k<c:
    p[i]=r[k]
    i+=1
    k+=1
    if i==x:
      break
print(sum(p)+sum(q))

Solution (Python):
X, Y, A, B, C = map(int, input().split())
p = [int(c) for c in input().split()]
q = [int(c) for c in input().split()]
r = [int(c) for c in input().split()]
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
p = p[:X]
q = q[:Y]
a = X-1
b = Y-1
for i in range(C):
  if p[a]<q[b]:
    if p[a]<r[i]:
      p[a] = r[i]
      a -= 1
    else:
      break
  else:
    if q[b]<r[i]:
      q[b] = r[i]
      b -= 1
    else:
      break
print(sum(p)+sum(q))

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

X, Y, A, B, C = map(int, readline().split())
P = sorted(map(int, readline().split()), reverse=True)
Q = sorted(map(int, readline().split()), reverse=True)
R = sorted(map(int, readline().split()), reverse=True)

P = P[:X]
Q = Q[:Y]
nums = P + Q + R
nums.sort(reverse=True)
answer = sum(nums[:X + Y])
print(answer)

Solution (Python):
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(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    h = []
    for i in p[:X]:
        heapq.heappush(h, (i, 'r'))
    for i in q[:Y]:
        heapq.heappush(h, (i, 'g'))
    a = sum(p[:X]) + sum(q[:Y])
    ra = 0
    ga = 0
    i = 0
    while h:
        t = heapq.heappop(h)
        if r[i] < t[0]:
            break
        if t[1] == 'r' and ra <= X:
            a += r[i]
            a -= t[0]
            ra += 1
            i += 1
        elif t[1] == 'g' and ga <= Y:
            ga += 1
            a += r[i]
            a -= t[0]
            i += 1
        if ra == X and ga == Y:
            break
        if i >= C:
            break
    return a
print(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()))

all = []
for i,val in enumerate([p,q,r]):
    for j in val:
        all.append([j,i])

all.sort(reverse=True)

cnt = [0]*3
ans = 0
for tmp in all:
    i,j = tmp
    if(cnt[0]==x)&(j==0):
        continue
    if(cnt[1]==y)&(j==1):
        continue

    ans += i
    cnt[j] += 1

    if(sum(cnt) == x+y):
        break

print(ans)

Solution (Python):
def main():
    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()))
    hq = [None]*(A+B+C)
    for i, x in enumerate(p):
        hq[i] = (-x, 0)
    for i, x in enumerate(q):
        hq[A+i] = (-x, 1)
    for i, x in enumerate(r):
        hq[A+B+i] = (-x, 2)

    heapq.heapify(hq)

    hq2 = []

    x = 0
    y = 0
    ans = 0
    while(len(hq2) < X+Y - (x+y)):
        a, b = heapq.heappop(hq)
        a = -a
        if b == 0:
            if x < X:
                x += 1
                ans += a
            else:
                continue
        elif b == 1:
            if y < Y:
                y += 1
                ans += a
            else:
                continue
        else:
            hq2.append(a)
        if x == X and y == Y:
            break

    if x < X or y < Y:
        ans += sum(hq2)
    print(ans)


main()