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

ep = p[:X]
eq = q[:Y]
epq = ep + eq
epq.sort()

ans = sum(epq)
for i, mr in enumerate(r):
    if i >= len(epq):
        break

    if mr > epq[i]:
        ans += mr-epq[i]
    else:
        break

print(ans)

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 = sorted(A)
B = sorted(B)
C = sorted(C)

t = 0
s = 0

A = A[-x:]
B = B[-y:]

C.extend(A)
C.extend(B)
C = sorted(C)

print(sum(C[-x-y:]))

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 = sorted(p)
q = sorted(q)
baseline = p[-x:] + q[-y:]

r = deque(sorted(r))
baseline = deque(sorted(baseline))

while len(r) != 0:
    v = r.pop()
    if v > baseline[0]:
        baseline.popleft()
        baseline.append(v)
    else:
        break

print(sum(baseline))

Solution (Python):
x,y,a,b,c = map(int,input().split())
r = sorted(list(map(int,input().split())))[::-1]
g = sorted(list(map(int,input().split())))[::-1]
c = sorted(list(map(int,input().split())))[::-1]
l =[]
count = 0
count1=0
for i in range(x):
  l.append(r[i])
for i in range(y):
  l.append(g[i])
l = sorted(l)
while count1< len(l) and count<len(c):
  if l[count1] >= c[count]:
    count1+=1
  else:
    l[count1] = c[count]
    count1+=1
    count+=1
print(sum(l))

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    x,y,a,b,c = LI()
    p = LI()
    q = LI()
    r = LI()
    s = [(i,0) for i in p]+[(i,1) for i in q]+[(i,2) for i in r]
    s.sort(reverse = True)
    ans = 0
    f = [0,0]
    l = [x,y]
    q = deque()
    q2 = deque()
    for a,i in s:
        if i > 1:
            q.append(a)
        else:
            if f[i] < l[i]:
                ans += a
                f[i] += 1
                q2.append(a)
    s = ans
    while q and q2:
        s -= q2.pop()
        s += q.popleft()
        if ans < s:
            ans = s
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    xy = sorted(p[:X] + q[:Y])
    ans = sum(p[:X]) + sum(q[:Y])
    z = 0
    while z < X + Y and z < C and xy[z] < r[z]:
        ans += r[z] - xy[z]
        z += 1
    print(ans)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(A)]  
    q = [int(next(tokens)) for _ in range(B)]  
    r = [int(next(tokens)) for _ in range(C)]  
    solve(X, Y, A, B, C, p, q, r)


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 = sorted(P,reverse=True)
Q = sorted(Q,reverse=True)
R = sorted(R,reverse=True)
P = P[:X]
Q = Q[:Y]
P.append(-100)
Q.append(-100)
R.append(-100)

ans = sum(P)+sum(Q)

p=0
q=0
r=0

x = 0
y = 0

ans = 0
while(x<=X or y<=Y):
    mm = max(P[p],Q[q],R[r])
    if R[r] == mm:
        ans += R[r]
        r+=1
    elif P[p]==mm:
        ans += P[p]
        p+=1
        x+=1
    elif Q[q]==mm:
        ans += Q[q]
        q+=1
        y+=1
    if (x+y+r)==(X+Y):
        break
    

print(ans)

Solution (Python):
x, y, a, b, c = list(map(int, input().split()))
ppp = list(map(int, input().split()))
qqq = list(map(int, input().split()))
rrr = list(map(int, input().split()))
ppp.sort(reverse=True)
qqq.sort(reverse=True)
rrr.sort(reverse=True)
ppqq = ppp[:x] + qqq[:y]
ppqq.sort(reverse=True)
ppqq.insert(0, 0)
rrr.insert(0, 0)

i = x + y
j = 1
while i > 0 and j <= c and ppqq[i] < rrr[j]:
    i -= 1
    j += 1
print(sum(ppqq[:i + 1]) + sum(rrr[:j]))

Solution (Python):
import heapq

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()
total = P[A-X:] + Q[B-Y:]
total.sort()
heapq.heapify(R)
i = 0
while True:
    try:
        n = -heapq.heappop(R)
        if i < len(total) and total[i] < n:
            total[i] = n
            i += 1
        else:
            break
    except:
        break
print(sum(total))

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 = sorted(A)
B = sorted(B)
C = sorted(C)

t = 0
s = 0

A = A[-x:]
B = B[-y:]

C.extend(A)
C.extend(B)
C = sorted(C)

print(sum(C[-x-y:]))

Solution (Python):
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)
    p = p[:x]
    q = q[:y]
    ans = p+q
    ans.sort()
    now = 0
    for i in range(x+y):
        if r[now] > ans[i]:
            ans[i] = r[now]
            now += 1
            if now == c:
                break
    print(sum(ans))


main()

Solution (Python):


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

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



p.sort();q.sort();r.sort()
p = p[-x:]
q = q[-y:]
e = p+q
heapq.heapify(e)
for ri in r:
    heapq.heappush(e,ri)
    heapq.heappop(e)
print(sum(e))

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

Solution (Python):
def main():
    import sys
    import heapq

    X,Y,A,B,C=list(map(int,sys.stdin.readline().split()))
    
    P=list(map(int,sys.stdin.readline().split()))
    Q=list(map(int,sys.stdin.readline().split()))
    R=list(map(int,sys.stdin.readline().split()))

    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)

    RE=P[:X]
    QE=Q[:Y]

    heapq.heapify(RE)
    heapq.heapify(QE)

    for r in R:
        if RE[0]<QE[0]:
            if RE[0]<r:
                heapq.heappushpop(RE,r)
        else:
            if QE[0]<r:
                heapq.heappushpop(QE,r)

    print(sum(list(RE))+sum(list(QE)))




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 = sorted(p, reverse = True)[:x]
q = sorted(q, reverse = True)[:y]
r.sort(reverse = True)





almost_ans = sorted(p + q)


colored_ind = 0
uncol_ind = 0
while colored_ind < len(almost_ans) and uncol_ind < len(r) and almost_ans[colored_ind] < r[uncol_ind]:
	almost_ans[colored_ind] = r[uncol_ind]
	colored_ind += 1
	uncol_ind += 1

print(sum(almost_ans))

Solution (Python):
from heapq import heapify, heappush, heappop
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()))


rs.sort(key=lambda x: -x)
hp = []
hq = []
for p in ps:
    heappush(hp, p)
    if len(hp) > X:
        heappop(hp)

for q in qs:
    heappush(hq, q)
    if len(hq) > Y:
        heappop(hq)

for r in rs:
    if len(hq) < Y:
        heappush(hq, r)
    elif len(hp) < X:
        heappush(hp, r)
    elif hp[0] < hq[0]:
        heappush(hp, r)
        heappop(hp)
    else:
        heappush(hq, r)
        heappop(hq)

print(sum(hq) + sum(hp))

Solution (Python):
X, Y, A, B, C = 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)[:X+Y]
pqr = sorted(p+q+r, reverse=True)
print(sum(pqr[:X+Y]))

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)

S = P[0:X]+Q[0:Y]+R
S.sort(reverse=True)

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

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

arr = p[:X] + q[:Y]
heapq.heapify(arr)
for n in r:
    if n < arr[0]:
        break
    if n > arr[0]:
        heapq.heappop(arr)
        heapq.heappush(arr, n)
print(sum(arr))

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

from collections import deque

x,y,a,b,c = l_in()
P = sorted(l_in(), reverse=True)
Q = sorted(l_in(), reverse=True)
R = deque(sorted(l_in(), reverse=True))


res = []
for i in range(x):
    res.append(P[i])
for j in range(y):
    res.append(Q[j])

res.sort()

for i in range(x+y):
    if len(R) > 0 and res[i] < R[0]:
        res[i] = R.popleft()

print(sum(res))

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '
        
        
    return ret


from heapq import heapify, heappop, heappush, heappushpop


class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)

    def push(self, item):
        heappush(self.heap, item)

    def pop(self):
        return heappop(self.heap)

    def pushpop(self, item):
        return heappushpop(self.heap, item)

    def __call__(self):
        return self.heap

    def __len__(self):
        return len(self.heap)


MOD = 10**9 + 7

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  

X, Y, A, B, C = read_ints()
P = read_ints()
Q = read_ints()
R = read_ints()  


P.sort(reverse=True)
Q.sort(reverse=True)

tmp = [-x for x in (P[:X] + Q[:Y])]

pq = PriorityQueue(tmp)
for r in R:
    pq.push(-r)

ans = 0
for _ in range(X + Y):
    ans += pq.pop()
print(-ans)

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

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(reverse=True)
Qs.sort(reverse=True)
Rs.sort(reverse=True)

PQRed = Ps[:X]
PQGreen = Qs[:Y]
sumRed = sum(PQRed)
sumGreen = sum(PQGreen)
heapify(PQRed)
heapify(PQGreen)


ans = sumNow = sumRed + sumGreen

for R in Rs:

    r = PQRed[0]
    g = PQGreen[0]

    if r <= g:
        d = heappop(PQRed)
        heappush(PQRed, R)
    else:
        d = heappop(PQGreen)
        heappush(PQGreen, R)
    if R <= d:
        break
    sumNow += R-d

    if sumNow > ans:
        ans = sumNow

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()
q.sort()
r.sort()

usep = a - x
useq = b - y
ans = sum(p[usep:]) + sum(q[useq:])
p.append(10 ** 9 + 1)
q.append(10 ** 9 + 1)

for i in range(c):
    if p[usep] < q[useq]:
        if r[c - i - 1] > p[usep]:
            ans = ans + r[c - i - 1] - p[usep]
            usep += 1
        else:
            break
    else:
        if r[c - i - 1] > q[useq]:
            ans = ans + r[c - i - 1] - q[useq]
            useq += 1
        else:
            break

print(ans)

Solution (Python):
X,Y,A,B,C = map(int,input().split())
S =[[],[],[]]
S[0] = list(map(int,input().split()))
S[1] = list(map(int,input().split()))
S[2] = list(map(int,input().split()))

T = []
for i in range(3):
  for s in S[i]:
    T.append((s,i))
T.sort(reverse=True)

U = []
i = 0
x = 0
y = 0
while(len(U) < X+Y):
  a,b = T[i]
  if b == 0:
    if x == X:
      i += 1
      continue
    else:
      x += 1
  elif b == 1:
    if y == Y:
      i += 1
      continue
    else:
      y += 1
  U.append(a)
  i += 1
print(sum(U))

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

x, y, a, b, c = get_nums_l()
ppp = get_nums_l()
qqq = get_nums_l()
rrr = get_nums_l()

ppp.sort(reverse=True)
qqq.sort(reverse=True)
rrr.sort(reverse=True)

xxx = ppp[:x] + qqq[:y]
xxx.sort()

ans = 0
for i in range(x+y):
    x = xxx[i]
    if i < len(rrr):
        x = max(x, rrr[i])
    ans += x

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)
Z = P[:X]+Q[:Y]+R
Z.sort(reverse=True)
print(sum(Z[:X+Y]))

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

from heapq import heappush, heappop

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

hP = []
hQ = []

P.sort(reverse = True)
Q.sort(reverse = True)


for i in range(min(A, X)):
    heappush(hP, P[i])
for _ in range(X - A):
    heappush(hP, 0)


for i in range(min(B, Y)):
    heappush(hQ, Q[i])
for _ in range(Y - B):
    heappush(hQ, 0)


for r in R:
    hp = heappop(hP)
    hq = heappop(hQ)
    if r <= hp and r <= hq:
        heappush(hP, hp)
        heappush(hQ, hq)
    else:
        if hp <= hq:
            heappush(hP, r)
            heappush(hQ, hq)
        else:
            heappush(hP, hp)
            heappush(hQ, r)
print(sum(hP) + sum(hQ))

Solution (Python):
x,y,a,b,c = (int(num) for num in input().split())
p = [int(num) for num in input().split()]
q = [int(num) for num in input().split()]
r = [int(num) for num in input().split()]
apples = []
for i in range(a):
    apples.append((p[i],"r"))
for i in range(b):
    apples.append((q[i],"g"))    
for i in range(c):
    apples.append((r[i],"n"))
    
apples.sort(reverse = True)

cntr = 0
cntg = 0
cntall = 0
i = 0
ans = 0

while True:
    if apples[i][1] == "r" and cntr < x:
        ans += apples[i][0]
        cntr += 1
        cntall += 1
    if apples[i][1] == "g" and cntg < y:
        ans += apples[i][0]
        cntg += 1
        cntall += 1
    if apples[i][1] == "n":
        ans += apples[i][0]
        cntall += 1
        
    if cntall == x + y:
        break
    i += 1
    
print(ans)

Solution (Python):


import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    x, y, a, b, c = map(int, sys.stdin.readline().split())
    r = list(map(int, sys.stdin.readline().split()))
    g = list(map(int, sys.stdin.readline().split()))
    blind = list(map(int, sys.stdin.readline().split()))
    r.sort(reverse=True)
    g.sort(reverse=True)
    blind.sort(reverse=True)
    red_need = r[:x]
    green_need = g[:y]
    res = red_need + green_need
    res.sort()
    
    for i in range(min(c,len(res))):
        if blind[i] > res[i]:
            res[i] = blind[i]
    print(sum(res))


if __name__ == "__main__":
    
    main()

Solution (Python):
from heapq import heapify, heappushpop

X, Y, A, B, C = map(int, input().split())

p = sorted(list(map(int, input().split())))[::-1]
q = sorted(list(map(int, input().split())))[::-1]
r = sorted(list(map(int, input().split())))[::-1]
ans_list = p[:X] + q[:Y]  
heapify(ans_list)
for ri in r:
    if ri > ans_list[0]:
        heappushpop(ans_list, ri)
    else:
        break
print(sum(ans_list))

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    x,y,a,b,c = inpm()
    p = inpl()
    q = inpl()
    r = inpl()
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    ans = 0
    que = []
    for i in range(x):
        ans += p[i]
        que.append([p[i],0])
    for i in range(y):
        ans += q[i]
        que.append([q[i],1])
    que.sort()
    cntx = x
    cnty = y
    indexq = 0
    indexr = 0
    while not (indexr == len(r) or indexq == len(que)):
        if r[indexr] <= que[indexq][0]:
            break
        if cntx == 0 and not(que[indexq][1]):
            indexq += 1
            continue
        if cnty == 0 and que[indexq][1]:
            indexq += 1
            continue
        ans += r[indexr]-que[indexq][0]
        if que[indexq][1]:
            cnty -= 1
        else:
            cntx -= 1
        indexr += 1
        indexq += 1
    print(ans)
    
if __name__ == "__main__":
    main()

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

a = sorted(a)[::-1]
b = sorted(b)[::-1]
c = sorted(c)

s = 0

a = a[:x]
b = b[:y]
s += sum(a)
s += sum(b)

a = deque(a)
b = deque(b)
c = deque(c)

a_nex = a.pop()
b_nex = b.pop()
c_nex = c.pop()

while True:

    if a_nex > b_nex:
        if c_nex > b_nex:
            s = s - b_nex + c_nex
            if len(b) == 0:
                b_nex = int(2e9)
            else:
                b_nex = b.pop()
            if len(c) == 0:
                break
            c_nex = c.pop()
    else:
        if c_nex > a_nex:
            s = s - a_nex + c_nex
            if len(a) == 0:
                a_nex = int(2e9)
            else:
                a_nex = a.pop()
            if len(c) == 0:
                break
            c_nex = c.pop()

    if c_nex <= a_nex and c_nex <= b_nex:
        break

print(s)

Solution (Python):
x,y,a,b,c,*pqr = map(int,open(0).read().split())
tag = ['a']*a + ['b']*b + ['c']*c
pqr_tag = [[pqr,tag] for pqr,tag in zip(pqr,tag)]
pqr_tag.sort(key = lambda x:x[0],reverse = True)

eat = {'a':0,'b':0,'c':0}
eat_max = {'a':x,'b':y}
eat_sum = 0
ans = 0
for taste,tag in pqr_tag:
    
    if tag=='c':
        ans += taste
        eat_sum += 1
    elif eat[tag]<eat_max[tag]:
        eat[tag] += 1
        ans += taste
        eat_sum += 1
    
    if eat_sum == x+y: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)

a = 0
b = 0
c = 0

ans = 0
for _ in range(X+Y):
    if a < X and b < Y and c < C:
        if p[a] >= q[b] and p[a] >= r[c]:
            ans += p[a]
            a += 1
        elif q[b] >= p[a] and q[b] >= r[c]:
            ans += q[b]
            b += 1
        else:
            ans += r[c]
            c += 1
    elif a >= X and b < Y and c < C:
        if q[b] >= r[c]:
            ans += q[b]
            b += 1
        else:
            ans += r[c]
            c += 1
    elif a < X and b >= Y and c < C:
        if p[a] >= r[c]:
            ans += p[a]
            a += 1
        else:
            ans += r[c]
            c += 1
    elif a < X and b < Y and c >= C:
        if p[a] >= q[b]:
            ans += p[a]
            a += 1
        else:
            ans += q[b]
            b += 1
    elif a >= X and b < Y and c >= C:
        ans += q[b]
        b += 1
    elif a < X and b >= Y and c >= C:
        ans += p[a]
        a += 1
print(ans)

Solution (Python):
import heapq

x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
w = list(map(int,input().split()))

r.sort(key = lambda x:-x)
g.sort(key = lambda x:-x)
w.sort(key = lambda x:-x)


rg = []
for i in range(x):
    rg.append(r[i])

for i in range(y):
    rg.append(g[i])

rg.sort(key = lambda x:x)


ind = 0
ans = 0
for i in range(x+y):
    if ind <= c-1 and w[ind] > rg[i]:
        ans += w[ind]
        ind += 1
    else:
        ans += rg[i]

print(ans)

Solution (Python):
x, y, a, b, c = map(int,input().split())
alist = list(map(int, input().split()))
alist=sorted(alist, reverse=True)
blist = list(map(int, input().split()))
blist=sorted(blist, reverse=True)
clist = list(map(int, input().split()))
clist=sorted(clist, reverse=True)

ringo=[]
for i in range(x):
    ringo.append(alist[i])
for j in range(y):
    ringo.append(blist[j])

ringo=sorted(ringo)
swit=0
now=0

while swit==0:
    if ringo[now]<=clist[now]:
        ringo[now]=clist[now]
        now+=1
        if now>=len(ringo) or now>=len(clist):
            swit=1
    else:
        swit=1

print(sum(ringo))

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()
s=p[:x]+q[:y]
heapq.heapify(s)
for i in range(c)[::-1]:
    h=heapq.heappop(s)
    heapq.heappush(s,max(h,r[i]))
print(sum(s))

Solution (Python):
x, y, a, b, c = map(int, input().split())
p_list = sorted(list(map(int, input().split())), reverse=True)
q_list = sorted(list(map(int, input().split())), reverse=True)
r_list = sorted(list(map(int, input().split())), reverse=True)
z_list = sorted(p_list[:x] + q_list[:y])
ans = sum(z_list)
for r, z in zip(r_list, z_list):
    if r < z:
        break
    ans += r - z
print(ans)

Solution (Python):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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]
pq.sort()
i = 0
while i<x+y and i < c and pq[i]<r[i]:
    i += 1
ans = r[:i] + pq[i:]
s = sum(ans)
print(s)

Solution (Python):
def main():
    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()
    P_=P[a-x:][::-1]
    Q_=Q[b-y:][::-1]
    ans=sum(P_)+sum(Q_)
    for i in range(c):
        r=R.pop()
        if not P_:
          	P_.append(float("INF"))
        if not Q_:
          	Q_.append(float("INF"))
        if r<=P_[-1] and r<=Q_[-1]:
            break
        elif r>P_[-1] and P_[-1]<=Q_[-1]:
            p=P_.pop()
            ans+=r-p
        else:
            q=Q_.pop()
            ans+=r-q
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, Y, A, B, C = MAP()
p = LIST()
q = LIST()
r = LIST()
r = [-x for x in r]

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

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

while r:
	if -r[0] < p_tmp[0] and -r[0] < q_tmp[0]:
		break
	p_min = heappop(p_tmp)
	q_min = heappop(q_tmp)
	r_max = heappop(r)
	
	if p_min <= q_min:  
		heappush(p_tmp, -r_max)
		heappush(q_tmp, q_min)
	else:  
		heappush(q_tmp, -r_max)
		heappush(p_tmp, p_min)
print(sum(p_tmp+q_tmp))

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

l = (sorted(l, reverse=True))[:X]
m = (sorted(m, reverse=True))[:Y]
print(sum((sorted(l+m+n, reverse=True))[:X+Y]))

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

pq=sorted(p+q+r,reverse=True)
print(sum(pq[: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()))
p.sort()
p.reverse()
q.sort()
q.reverse()
r.append(0)
r.sort()
r.reverse()
ct=0
P=p[0:x]
Q=q[0:y]
L=P+Q
L.sort()
R=0
for i in range(len(L)):
  if r[R]>L[i]:
    L[i]=r[R]
    if R<c:
      R+=1
print(sum(L))

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    X, Y, A, B, C = map(int, input().split())
    P = [int(p) for p in input().split()]
    Q = [int(q) for q in input().split()]
    R = [int(r) for r in input().split()]
    P.sort(reverse = True)
    Q.sort(reverse = True)
    R.sort(reverse = True)
    pi, qi = 0, 0
    sumPQ = [0] * (X + Y + 1)
    for i in range(X + Y):
        
        if pi == min(A, X):
            sumPQ[i + 1] = sumPQ[i] + Q[qi]
            qi += 1
        elif qi == min(B, Y):
            sumPQ[i + 1] = sumPQ[i] + P[pi]
            pi += 1
        elif P[pi] >= Q[qi]:
            sumPQ[i + 1] = sumPQ[i] + P[pi]
            pi += 1
        else:
            sumPQ[i + 1] = sumPQ[i] + Q[qi]
            qi += 1
    Ans = sumPQ[X + Y]
    rVal = 0
    for i in range(min(C, X + Y)):
        rVal += R[i]
        Ans = max(Ans, rVal + sumPQ[X + Y - i - 1])
    print(Ans)
    
    return 0

if __name__ == "__main__":
    solve()