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

import sys


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)
    p.append(0)
    q.append(0)
    r.append(0)
    pp = 0
    qq = 0
    rr = 0
    ans = 0
    while pp + qq + rr != X + Y:
        if qq == Y:
            if p[pp] >= r[rr]:
                ans += p[pp]
                pp += 1
            else:
                ans += r[rr]
                rr += 1
            continue
        if pp == X:
            if q[qq] >= r[rr]:
                ans += q[qq]
                qq += 1
            else:
                ans += r[rr]
                rr += 1
            continue

        
        if r[rr] >= q[qq] and r[rr] >= p[pp]:
            ans += r[rr]
            rr += 1
        elif q[qq] >= p[pp]:
            ans += q[qq]
            qq += 1
        else:
            ans += p[pp]
            pp += 1


    print(ans)


        

    return



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

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

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=1)
    q.sort(reverse=1)
    l=p[:X]+q[:Y]+r
    l.sort(reverse=1)
    print(sum(l[:X+Y]))

if __name__ == '__main__':
    main()

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

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

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)	





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




ans=a=b=c=0

for _ in range(x+y):
	an = 0 if len(A)<=a else A[a]
	bn = 0 if len(B)<=b else B[b]
	cn = 0 if len(C)<=c else C[c]

	
	m = max(an,bn,cn)
	ans += m



	if m == an:   a+=1
	elif m == bn: b+=1
	elif m == cn: c+=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)
ans = 0
L = P[:X] + Q[:Y]
Rt = [False for _ in range(C)]
L.sort()
idx = 0
for i in range(X+Y):
    if idx >= C:
        break
    if L[i] < R[idx]:
        L[i] = R[idx]
        idx += 1
print(sum(L))

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

out = P[-X:] + Q[-Y:]

out.sort()
for i in range(min(C, X+Y)):
  if R[-i-1] > out[i]:
    out[i] = R[-i-1]
  else:
    break
print(sum(out))

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()))
A = [[i,'p'] for i in P]
for i in Q:
    A.append([i,'q'])
for i in R:
    A.append([i,'r'])
As = list(sorted(A,reverse=True))
qN = 0
pN = 0
rN = 0
ans = 0
for i in As:
    if i[1] == 'q' and qN < Y:
        qN += 1
        ans += i[0]
    elif i[1] == 'p' and pN < X:
        pN += 1
        ans += i[0]
    elif i[1] == 'r':
        rN += 1
        ans += i[0]
    if qN + pN + rN == X + Y:
        break
print(ans)

Solution (Python):
import math



red,green,x,y,z = map(int,input().split())
red_data = list(map(int,input().split()))
green_data = list(map(int,input().split()))
blank_data = list(map(int,input().split()))


red_data.sort(reverse=True)
green_data.sort(reverse=True)
blank_data.sort(reverse=True)



red_index = red-1
green_index = green-1


for blank in blank_data:
    red_gap = -1
    if red_index >= 0:
        red_gap = blank - red_data[red_index]
    
    green_gap = -1
    if green_index >=0:
        green_gap = blank - green_data[green_index]
    
    if max(red_gap,green_gap) <= 0:
        break
    
    if red_gap >= green_gap:
        red_data[red_index] = blank
        red_index -= 1
    
    if green_gap > red_gap:
        green_data[green_index] = blank
        green_index -= 1






total = sum(red_data[0:red]) + sum(green_data[0:green])

print(total)

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()))
rlen = len(R)
if (rlen < x + y):
    max = rlen
else:
    max = x + y

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

p = P[:x]
q = Q[:y]
r = R[:max]

p.extend(q)
p.sort()

ans = sum(p)

for i in range(max):
    diff = r[i] - p[i]
    if (diff > 0):
        ans = ans + diff
    else:
        break

print(ans)

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

p, q, r = A - X, B - Y, C - 1
ans = 0

Amin = P[p]
Bmin = Q[q]
Cmax = R[r]
for i in range(C):
    if Cmax <= min(Amin, Bmin):
        break
    else:
        if Amin <= Bmin and p < A:
            ans += Cmax
            r -= 1
            p += 1
            if p < A:
                Amin = P[p]
            else:
                Amin = float("inf")
        elif Amin >= Bmin and q < B:
            ans += Cmax
            r -= 1
            q += 1
            if q < B:
                Bmin = Q[q]
            else:
                Bmin = float("inf")
        Cmax = R[r]
ans += sum(P[p:])+sum(Q[q:])
print(ans)

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

SP = [0]*(A+1)
SQ = [0]*(B+1)
SR = [0]*(C+1)
for i,a in enumerate(P):
    SP[i+1] = SP[i] + a
for i,b in enumerate(Q):
    SQ[i+1] = SQ[i] + b
for i,c in enumerate(R):
    SR[i+1] = SR[i] + c

ans = 0
for i in range(max(0,X-C),X+1):
    d = C-(X-i)
    l0 = max(0,Y-d)
    l = l0
    r = Y
    while l+1<r:
        j = (l+r)//2
        k = (X-i) + (Y-j)
        if (Q[j] < R[k]):
            r = j
        else:
            l = j
    tmp = SP[i] + SQ[r] + SR[X+Y-i-r]
    tmp = max(tmp,SP[i]+SQ[l]+SR[X+Y-i-l])
    ans = max(ans,tmp)
print(ans)

Solution (Python):
x,y,a,b,c=map(int,input().split())
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())),reverse=True)
import heapq
eat=p+q
heapq.heapify(eat)
for i in r:
	j=heapq.heappop(eat)
	if j<i:
		heapq.heappush(eat,i)
	else:
		heapq.heappush(eat,j)

print(sum(list(eat)))

Solution (Python):

"""
Created on Sat Mar 28 20:51:51 2020

@author: naoki
"""

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_selected = p[:X]
q_selected = q[:Y]


All_selected = []
All_selected.extend(p_selected)
All_selected.extend(q_selected)
All_selected = sorted(All_selected)


for i in range(min(C,len(All_selected))):
    if All_selected[i] < r[i]:
        All_selected[i] = r[i]
print(sum(All_selected))

Solution (Python):
def main():
    from collections import deque

    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)

    p = deque(p[:X])
    q = deque(q[:Y])
    ret = sum(p) + sum(q)
    for x in r:
        if p:
            if q:
                if p[-1] < q[-1]:
                    if p[-1] >= x:
                        break
                    ret += x - p.pop()
                elif p[-1] == q[-1]:
                    popp = True
                    ofs = 1
                    while (1 + ofs) <= len(p) and (1 + ofs) <= len(q):
                        if p[-1 - ofs] == q[-1 - ofs]:
                            ofs += 1
                            continue

                        if p[-1 - ofs] < q[-1 - ofs]:
                            popp = True
                            break

                        if p[-1 - ofs] > q[-1 - ofs]:
                            popp = False
                            break
                    if popp:
                        if p[-1] >= x:
                            break
                        ret += x - p.pop()
                    else:
                        if q[-1] >= x:
                            break
                        ret += x - q.pop()
                else:
                    if q[-1] >= x:
                        break
                    ret += x - q.pop()

            else:
                if p[-1] >= x:
                    break
                ret += x - p.pop()
        else:
            if q:
                if q[-1] >= x:
                    break
                ret += x - q.pop()
            else:
                break

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):

X,Y,A,B,C = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)


d_list = a_list[:X] + b_list[:Y] + c_list
d_list.sort(reverse = True)
print(sum(d_list[: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=sorted(P)[::-1]
Q=sorted(Q)[::-1]
R=sorted(R)

if A+B+C==X+Y:
  print(sum(P)+sum(Q)+sum(R))
  exit()
if X<A:
  P=P[:X]
if Y<B:
  Q=Q[:Y]
l=0
if X>A:
  l+=(X-A)
if Y>B:
  l+=(Y-B)
h=C-l
S=sorted(P+Q+R[:h])[::-1]
ans=sum(S[:X+Y])+sum(R[h:])
print(ans)

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

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

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

ans = 0

for i in range(A):
    heappush(a,(-1*p[i],0))
for i in range(B):
    heappush(a,(-1*q[i],1))
for i in range(C):
    heappush(a,(-1*r[i],2))

c_r = c_g = c_all = 0
while c_all<X+Y:
    v,col = heappop(a)
    if c_r < X and c_g < Y:
        ans += -1*v
        c_all += 1
        if col == 0:
            c_r += 1
        if col == 1:
            c_g += 1
    elif c_r<X and c_g == Y:
        if col == 0 or col == 2:
            ans += -1*v
            c_all += 1
            if col == 0:
                c_r += 1
    elif c_r==X and c_g<Y:
        if col == 1 or col == 2:
            ans += -1*v
            c_all += 1
            if col == 1:
                c_g += 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=sorted(p)
q=sorted(q)
r=sorted(r)
p=p[(a-x):]
q=q[(b-y):]
res=sum(p)+sum(q)
tp=0
tq=0
tr=c-1
while 1:
  if(tr<0 ):
    break
  if(tp>=x):
    pp=100000000000
  else:
    pp=p[tp]
  if(tq>=y):
    qq=100000000000
  else:
    qq=q[tq]

  if(pp>r[tr] and qq>r[tr]):
    break
  if(pp<qq ):
    res+=r[tr]-pp
    tp+=1
  else:
    res+=r[tr]-qq
    tq+=1
  tr-=1
print(res)

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

x,y,a,b,c = inm()
p = inl()
q = inl()
r = inl()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pt = x-1
qt = y-1
rt = 0
while rt<c and (pt>=0 and p[pt]<r[rt] or qt>=0 and q[qt]<r[rt]):
    if pt>=0 and (qt<0 or p[pt]<q[qt]):
        pt -= 1
        rt += 1
    elif qt>=0 and (pt<0 or p[pt]>=q[qt]):
        qt -= 1
        rt += 1
print(sum(p[:pt+1])+sum(q[:qt+1])+sum(r[:rt]))

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

z = sorted(p)[-X:] + sorted(q)[-Y:] + r
print(sum(sorted(z)[-(X+Y):]))

Solution (Python):


x, y, a, b, c = list(map(int, input().split(' ')))
pa = list(map(int, input().split(' ')))
qb = list(map(int, input().split(' ')))
rc = list(map(int, input().split(' ')))

pa = sorted(pa)[len(pa) - x:]
qb = sorted(qb)[len(qb) - y:]
rc = sorted(rc, reverse=True)

ai = 0
bi = 0
result = sum(pa) + sum(qb)

for r in rc:
    if bi >= len(qb) and ai >= len(pa):
        break
    
    elif bi >= len(qb) or pa[ai] <= qb[bi]:
        if r > pa[ai]:
            result = result - pa[ai] + r
            ai += 1
            
    elif ai >= len(pa) or pa[ai] >= qb[bi]:
        if r > qb[bi]:
            result = result - qb[bi] + r
            bi += 1
print(result)

Solution (Python):
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 = sorted(list(map(int, input().split())), reverse = True)

res = sum(P[0:X]) + sum(Q[0:Y])
p_min_idx = X-1
q_min_idx = Y-1
for r in R:
    if (p_min_idx < 0 and q_min_idx >= 0) or (p_min_idx >= 0 and q_min_idx >= 0 and P[p_min_idx] > Q[q_min_idx]):
        if Q[q_min_idx] < r:
            res = res - Q[q_min_idx] + r
            q_min_idx -= 1
        else:
            break
    else:
        if p_min_idx >= 0 and  P[p_min_idx] < r:
            res = res - P[p_min_idx] + r
            p_min_idx -= 1
        else:
            break

print(res)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
INF = 10 ** 10
MOD = 10 ** 9 + 7
from heapq import heapify,heappop,heappush
input = sys.stdin.readline

def main():
    x,y,a,b,c = map(int,input().split())
    R = list(map(int,input().split()))
    G = list(map(int,input().split()))
    M = list(map(int,input().split()))
    i,j,k = 0,0,0
    R.sort(reverse = True)
    G.sort(reverse = True)
    M.sort(reverse = True)
    
    ans = 0
    while i < x and j < y and k < c and k + i + j < x + y:
        tmp = max(R[i],G[j],M[k])
        ans += tmp
        if tmp == R[i]:
            i += 1
        elif tmp == G[j]:
            j += 1
        else:
            k += 1
    
    if i + j + k == x + y:
        print(ans)
        return
    
    elif k == c:
        while i < x and j < y and i + j + c < x + y:
            tmp = max(R[i],G[j])
            ans += tmp
            if tmp == R[i]:
                i += 1
            elif tmp == G[j]:
                j += 1

    if i + j + k == x + y:
        print(ans)
        return            

    elif i == x:
        t = y - k - j
        for _ in range(t):
            if k < c and G[j] < M[k]:
                ans += M[k]
                k += 1
            else:
                ans += G[j]
                j += 1
    elif j == y:
        t = x - i - k
        for _ in range(t):
            if k < c and R[i] < M[k]:
                ans += M[k]
                k += 1
            else:
                ans += R[i]
                i += 1


    
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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

ans = sum(p[0:X]) + sum(q[0:Y])
p = sorted(p[0:X], reverse=True)
q = sorted(q[0:Y], reverse=True)

for i in range(C):
    if p and q and r:
        if p[-1] <= q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        elif q[-1] <= p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif p and r:
        if p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif q and r:
        if q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        else:
            break
    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)

tot=sum(p[:X])+sum(q[:Y])

a=0
b=0
f=False
for i in r:
    if f==True:
        break
    elif a==X and b==Y:
        break
    elif a==X:
        if i>q[Y-b-1]:
            tot+=i-q[Y-b-1]
            b+=1
        else:
            f=True
    elif b==Y:
        if i>p[X-a-1]:
            tot+=i-p[X-a-1]
            a+=1
        else:
            f=True
    else:
        if i>p[X-a-1] and i>q[Y-b-1]:
            if p[X-a-1]<q[Y-b-1]:
                tot+=i-p[X-a-1]
                a+=1
            else:
                tot+=i-q[Y-b-1]
                b+=1
        elif i>p[X-a-1]:
            tot+=i-p[X-a-1]
            a+=1
        elif i>q[Y-b-1]:
            tot+=i-q[Y-b-1]
            b+=1
        else:
            f=True

print(tot)

Solution (Python):
import heapq


def calc(X,Y,A,B,C,p,q,r):
    heapq.heapify(p)
    heapq.heapify(q)
    heapq.heapify(r)
    for _ in range(A - X):
        heapq.heappop(p)
    for _ in range(B - Y):
        heapq.heappop(q)
    minp = heapq.heappop(p)
    minq = heapq.heappop(q)
    
    while r:
        maxr = -heapq.heappop(r)
        if minp < minq:
            if maxr > minp:
                heapq.heappush(p, maxr)
                minp = heapq.heappop(p)
            else:
                break
        else:
            if maxr > minq:
                heapq.heappush(q, maxr)
                minq = heapq.heappop(q)
            else:
                break
    return sum(p) + sum(q) + minp + minq



(X, Y, A, B, C) = tuple([int(s) for s in 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()]
print(calc(X,Y,A,B,C,p,q,r))

Solution (Python):
from sys import stdin,setrecursionlimit
import heapq

x,y,a,b,c = map(int,stdin.readline().rstrip().split())

p = list(map(int,stdin.readline().rstrip().split()))
q = list(map(int,stdin.readline().rstrip().split()))
r = list(map(int,stdin.readline().rstrip().split()))

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

p = p[a-x:]
q = q[b-y:]

heapq.heapify(p)
heapq.heapify(q)

for i in r:
    if p[0] <= q[0]:
        if i > p[0]:
            heapq.heappop(p)
            heapq.heappush(p,i)
    else:
        if i > q[0]:
            heapq.heappop(q)
            heapq.heappush(q,i)

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)
pi = 0
qi = 0
ri = 0
pl = len(p)
ql = len(q)
rl = len(r)
R = 1
scr = 0
for j in range(X+Y):
  mx = max(p[pi],q[qi],r[ri])
  scr += mx
  if mx == p[pi]:
    X -= 1
    if pi+1 >= pl or X <= 0:
      p.append(0)
      pi = -1
      X = 0
    else:
      pi += 1
  elif mx == q[qi]:
    Y -= 1
    if qi+1 >= ql or Y <= 0:
      q.append(0)
      qi = -1
      Y = 0
    else:
      qi += 1
  else:
    if ri+1 >= rl or R <= 0:
      r.append(0)
      ri = -1
      R = 0
    else:
      ri += 1
      
print(scr)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

X,Y,A,B,C = map(int,input().split(' '))
l_p = list(map(int, input().split()))
l_q = list(map(int, input().split()))
l_r = list(map(int, input().split()))
l_p = sorted(l_p, reverse=True)
l_q = sorted(l_q, reverse=True)
l_r = sorted(l_r, reverse=True)
x = 0
y = 0
z = 0
Z = len(l_r)

def getMax(x,y,z):
    k_x = 0 if x==X else l_p[x]
    k_y = 0 if y==Y else l_q[y]
    k_z = 0 if z==Z else l_r[z]
    MAX = max(k_x,k_y,k_z)
    if MAX==k_x:
        x += 1
    elif MAX==k_y:
        y += 1
    else:
        z += 1
    return x,y,z,MAX
ans = 0
while True:
    x,y,z,m = getMax(x,y,z)
    ans += m
    if x+y+z==X+Y:
        break
print(ans)

Solution (Python):
X, Y, A, B, C = list(map(int, input().split()))
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)

A_s = A[0:X]
B_s = B[0:Y]

All = A_s
All.extend(B_s)
All.extend(C)
All.sort(reverse=True)

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

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

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


P=P[-X:]
Q=Q[-Y:]
R=R[-(X+Y):][::-1]

PQ=sorted(P+Q)


allsum=sum(PQ)
rsum=0 
ind=0

for i in range(len(R)):
  if allsum-PQ[i]+R[i]>allsum:
    allsum=allsum-PQ[i]+R[i]
  else:
    break
print(allsum)

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

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()))
r.sort()
r.reverse()
p.sort()
p.reverse()
q.sort()
q.reverse()
lisp = []
for i in range(x):
    lisp.append(p[i])
lisq = []
for i in range(y):
    lisq.append(q[i])
lis = lisp + lisq
lis.sort()
i = 0
j = 0



while j < c and  i < len(lis):
    if lis[i] < r[j]:
        lis[i] = r[j]
        i += 1
        j += 1
    else:
        break
print(sum(lis))

Solution (Python):
x, y, a, b, c = map(int, input().split())
red = list(map(int, input().split()))
green = list(map(int, input().split()))
no = list(map(int, input().split()))

red.sort()
green.sort()
no.sort()
a -= 1
b -= 1
c -= 1
max_a = int(a)
max_b = int(b)

ans = 0
for i in range(x):
    ans += red[a]
    a -= 1
for i in range(y):
    ans += green[b]
    b -= 1
a += 1
b += 1

for i in range(x+y):
    if c >= 0 and a <= max_a and b <= max_b:
        gr = no[c] - red[a]
        gg = no[c] - green[b]
        if gr >= gg:
            if gr > 0:
                ans += no[c]
                ans -= red[a]
                c -= 1
                a += 1
        elif gg >= gr:
            if gg > 0:
                ans += no[c]
                ans -= green[b]
                c -= 1
                b += 1
    elif c >= 0 and a <= max_a:
        if no[c] - red[a] > 0:
            ans += no[c]
            ans -= red[a]
            c -= 1
            a += 1
    elif c >= 0 and b <= max_b:
        if no[c] - green[b] > 0:
            ans += no[c]
            ans -= green[b]
            c -= 1
            b += 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)
kouho=[]
for i in range(x):
	kouho.append(p[i])
for i in range(y):
	kouho.append(q[i])
kouho.sort()
j=0
for i in range(len(kouho)):
	if kouho[i]<r[j]:
		kouho[i]=r[j]
		j+=1
		if j==c:
			break
print(sum(kouho))

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

lp = hp.nlargest(x, p)
lq = hp.nlargest(y, q)
pq = lp + lq
lpq = hp.nlargest(x+y, pq)
lr = hp.nlargest(c, r)
d = min(len(lr),len(lpq))
for i in range(1,d+1):
  if lpq[x+y-i] < lr[i-1]:
    lpq[x+y-i] = lr[i-1]
  else:
    break

ans = 0
for i in range(0,len(lpq)):
  ans += lpq[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)
l = p[:X] + q[:Y]
l.sort()
for i in range(min(C,X+Y)):
    if r[i] > l[i]:
        l[i] = r[i]
print(sum(l))

Solution (Python):
import sys
import math
from collections import defaultdict
x,y,a,b,c=map(int,sys.stdin.readline().split())
red=list(map(int,sys.stdin.readline().split()))
green=list(map(int,sys.stdin.readline().split()))
color=list(map(int,sys.stdin.readline().split()))
red.sort()
green.sort()
color.sort()
s=0
for i in range(a-x,a):
    s+=red[i]
for i in range(b-y,b):
    s+=green[i]
i,j,k=c-1,a-x,b-y




while i>=0 and j<a and k<b:
    if color[i]>red[j] and color[i]>green[k]:
        if green[k]>red[j]:
            s-=red[j]
            s+=color[i]
            j+=1
            i-=1
            continue
        else:
            s-=green[k]
            s+=color[i]
            k+=1
            i-=1
            continue
    if color[i]>red[j]:
        s-=red[j]
        s+=color[i]
        i-=1
        j+=1
        continue
    if color[i]>green[k]:
        s-=green[k]
        s+=color[i]
        i-=1
        k+=1
        continue
    i-=1
if i==0:
    print(s)
else:
    while i>=0 and j<a:
        if color[i]>red[j]:
            s-=red[j]
            s+=color[i]
            i-=1
            j+=1
            continue
        i-=1
    while i>=0 and k<b:
        if color[i]>green[k]:
            s-=green[k]
            s+=color[i]
            i-=1
            k+=1
            continue
        i-=1
    print(s)

Solution (Python):
def main():
    x,y,a,b,c=map(int,input().split())
    P = sorted(list(map(int,input().split())))
    Q = sorted(list(map(int,input().split())))
    R = sorted(list(map(int,input().split())))[::-1]
    S = sorted(P[-x:] + Q[-y:])
    
    ans = 0
    ind = 0
    for s in S:
        
        if ind < len(R):
            if s > R[ind]:
                ans += s
            else:
                ans += R[ind]
                ind += 1
        else:
            ans += s
    print(ans)
main()

Solution (Python):
import numpy as np
x, y, a, b, c = map(int, input().split())
p = np.array(list(map(int, input().split())))
q = np.array(list(map(int, input().split())))
r = np.array(list(map(int, input().split())))

p = np.sort(p)[::-1][:x]
q = np.sort(q)[::-1][:y]

mix = np.hstack((p, q, r))
mix = np.sort(mix)[::-1]

eat = x+y

print(sum(mix[:eat]))

Solution (Python):

"""
Created on Sat Mar 28 21:48:49 2020

@author: liang
"""
def min_search(a,b,c):
    if a < b:
        if a < c:
            return a
        else:
            return c
    else:
        if b < c:
            return b
        else:
            return c
        
        
X, Y, A, B, C = map(int,input().split())
red_apples = list(map(int,input().split()))
green_apples = list(map(int,input().split()))
colorless_apples = list(map(int,input().split()))

red_apples.sort(reverse=True)
green_apples.sort(reverse=True)
colorless_apples.sort(reverse=True)





result_red = red_apples[0:X]
result_green = green_apples[0:Y]




i = X - 1
j = Y - 1
k = 0

while k <= C-1:
    if i < 0 :
        i = 0
    if j < 0 :
        j = 0
    mini = min_search(result_red[i], result_green[j], colorless_apples[k])
    if mini == colorless_apples[k]:
        break
    elif mini == result_red[i]:
        result_red[i] = colorless_apples[k]
        i -= 1
        k += 1
    else:
        result_green[j] = colorless_apples[k]
        j -= 1
        k += 1


score = sum(result_red) + sum(result_green)

print(score)

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)

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

S_wa = []
wa = 0
for s in S[::-1]:
    wa += s
    S_wa.append(wa)

big = S_wa[-1]
ans = S_wa[-1]

R_wa = []
wa = 0
for r in R:
    wa += r
    R_wa.append(wa)


kazu = min(len(R_wa), len(S_wa))

for i in range(kazu):
    ans = max(ans, big + R_wa[i] - S_wa[i])
print(ans)

Solution (Python):
x, y, a, b, c = map(int, input().split())
La = sorted(list(map(int, input().split())), reverse=True)
Lb = sorted(list(map(int, input().split())), reverse=True)
Lc = sorted(list(map(int, input().split())), reverse=True)

L = La[:x]
L.extend(Lb[:y])
L.sort(reverse=False)
min = min(c, len(L))

for i in range(min):
    if L[i] < Lc[i]:
        L[i] = Lc[i]

print(sum(L))

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

print( sum(sorted(p+q+r)[c:]) )

Solution (Python):
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 = sorted(list(map(int, input().split())))

A = sorted(P[:x] + Q[:y])
ans = sum(A)

for i in range(min(c, len(A))):
    if A[i] < R[-1]:
        ans = ans + R[-1] - A[i]
        R.pop(-1)

print(ans)