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







def solve(X, Y, A, B, C, Ps, Qs, Rs):
    inf = 10 ** 9 + 1
    Ps.sort(reverse=True)
    Qs.sort(reverse=True)
    Rs.sort(reverse=True)
    a = X - 1
    b = Y - 1
    c = 0
    while True:
        m = min(Ps[a] if a >= 0 else inf,
                Qs[b] if b >= 0 else inf,
                Rs[c])
        if m == Rs[c]:
            break
        elif m == Ps[a]:
            c += 1
            a -= 1
        elif m == Qs[b]:
            c += 1
            b -= 1
        if c == C:
            break

    ans = 0
    if a >= 0:
        ans += sum(Ps[:a + 1])
    if b >= 0:
        ans += sum(Qs[:b + 1])
    if c > 0:
        ans += sum(Rs[:c])

    print(ans)


if __name__ == '__main__':
    X, Y, A, B, C = map(int, input().split())
    Ps = [int(i) for i in input().split()]
    Qs = [int(i) for i in input().split()]
    Rs = [int(i) for i in input().split()]
    solve(X, Y, A, B, C, Ps, Qs, Rs)

Solution (Python):
import heapq

X,Y,A,B,C = map(int, input().split())
p_list = sorted([(-v, 'p') for v in map(int, input().split())])
q_list = sorted([(-v, 'q') for v in map(int, input().split())])
r_list = sorted([(-v, 'r') for v in map(int, input().split())])

res = 0
r_cnt = 0
for v, k in heapq.merge(p_list, q_list, r_list):
    v = -v
    if X+Y-r_cnt == 0:
        break
    if k == 'p':
        if X != 0:
            X -= 1
            res += v
        continue
    
    if k == 'q':
        if Y != 0:
            Y -= 1
            res += v
        continue

    res += v
    r_cnt += 1

print(res)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7






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

P.sort()
Q.sort()

lista = P[-X:] + Q[-Y:] + R
lista.sort()
print(sum(lista[-(X + Y):]))

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


top_x_of_A = A[:X]
top_Y_of_B = B[:Y]
top_x_of_A.extend(top_Y_of_B)
C.extend(top_x_of_A)
C.sort(reverse=True)
print(sum(C[:X+Y]))

Solution (Python):
import sys
input = sys.stdin.readline
x,y,a,b,c = map(int,input().split())
red = list(map(int,input().split()))+[0]
green = list(map(int,input().split()))+[0]
cless = list(map(int,input().split()))+[0]
red.sort(reverse = True)
green.sort(reverse = True)
cless.sort(reverse = True)
red_ind = 0
green_ind = 0
cless_ind = 0
ans = 0
for _ in range(x+y):
    if red_ind == x:
        red_ind = a
    if green_ind == y:
        green_ind = b
    if max(red[red_ind],green[green_ind],cless[cless_ind]) == red[red_ind]:
        ans += red[red_ind]
        red_ind += 1
    elif max(red[red_ind],green[green_ind],cless[cless_ind]) == green[green_ind]:
        ans += green[green_ind]
        green_ind += 1
    else:
        ans += cless[cless_ind]
        cless_ind += 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)
p=p[:X]
q=q[:Y]
s=p+q+r
s.sort(reverse=True)
print(sum(s[:X+Y]))

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


if __name__ == '__main__':
    main()

Solution (Python):
import heapq


def main():
    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)
    que = []
    for p in P[:X]:
        heapq.heappush(que, p)
    for q in Q[:Y]:
        heapq.heappush(que, q)
    for r in R:
        min_que = heapq.heappop(que)
        if min_que >= r:
            heapq.heappush(que, min_que)
            break
        else:
            heapq.heappush(que, r)
    print(sum(que))


if __name__ == '__main__':
    main()

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

eat = [p[i] for i in range(X)] + [q[i] for i in range(Y)]
eat.sort()
for i in range(len(r)):
    if eat[i] < r[i]:
        eat[i] = r[i]
    else:
        break

print(sum(eat))

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

pa = sorted(pa)
pb = sorted(pb)
pc = sorted(pc)

pr = pa[-X:] + pb[-Y:]
pr = sorted(pr)

c = 0
r = 0
while True:
  c += 1
  if c > C:
    break
  if pr[r] < pc[-c]:
    pr[r] = pc[-c]
    r += 1
  else:
    break

print(sum(pr))

Solution (Python):
import heapq
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(reverse=True)
P = P[:x]
Q.sort(reverse=True)
Q = Q[:y]
heapq.heapify(P)
heapq.heapify(Q)
heapq.heapify(R)
while R:
    if P[0] >= Q[0]:
        if Q[0] < -R[0]:
            heapq.heapreplace(Q, -heapq.heappop(R))
        else:
            break
    else:
        if P[0] < -R[0]:
            heapq.heapreplace(P,-heapq.heappop(R))
        else:
            break
print(sum(P)+sum(Q))

Solution (Python):
import heapq
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(reverse=True)
P = P[:x]
Q.sort(reverse=True)
Q = Q[:y]
heapq.heapify(P)
heapq.heapify(Q)
heapq.heapify(R)
while R:
    if P[0] >= Q[0]:
        if Q[0] < -R[0]:
            heapq.heapreplace(Q, -heapq.heappop(R))
        else:
            break
    else:
        if P[0] < -R[0]:
            heapq.heapreplace(P,-heapq.heappop(R))
        else:
            break
print(sum(P)+sum(Q))

Solution (Python):
import heapq
x,y,a,b,c = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = [ -int(i) for i in input().split()]
P.sort(reverse=True)
P = P[:x]
Q.sort(reverse=True)
Q = Q[:y]
heapq.heapify(P)
heapq.heapify(Q)
heapq.heapify(R)
while R:
    if P[0] >= Q[0]:
        if Q[0] < -R[0]:
            heapq.heapreplace(Q, -heapq.heappop(R))
        else:
            break
    else:
        if P[0] < -R[0]:
            heapq.heapreplace(P,-heapq.heappop(R))
        else:
            break
print(sum(P)+sum(Q))

Solution (Python):


import heapq 


X,Y,A,B,C = (int(x) for x in 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)

ans_sum =sum(p[-X:]) + sum(q[-Y:])
p_hq = p[-X:]
q_hq = q[-Y:]
heapq.heapify(p_hq)
heapq.heapify(q_hq)
p_change =heapq.heappop(p_hq)
q_change =heapq.heappop(q_hq)
count_p =0
count_q =0
for i in r: 
    if i < p_change and i < q_change:
        break
    if p_change <= q_change:
        if  count_p < X:
            heapq.heappush(p_hq,i)
            count_p +=1
            ans_sum = ans_sum -p_change +i
            p_change =heapq.heappop(p_hq)
    elif p_change > q_change:
        if  count_q < Y:
            heapq.heappush(q_hq,i)
            count_q +=1
            ans_sum = ans_sum -q_change +i
            q_change =heapq.heappop(q_hq)
print(ans_sum)

Solution (Python):

import heapq 


X,Y,A,B,C = (int(x) for x in 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)

ans_sum =sum(p[-X:]) + sum(q[-Y:])
p_hq = p[-X:]
q_hq = q[-Y:]
heapq.heapify(p_hq)
heapq.heapify(q_hq)
p_change =heapq.heappop(p_hq)
q_change =heapq.heappop(q_hq)
count_p =0
count_q =0
for i in r: 
    if p_change <= q_change and p_change < i:
        if  count_p < X:
            heapq.heappush(p_hq,i)
            count_p +=1
            ans_sum = ans_sum -p_change +i
            p_change =heapq.heappop(p_hq)
    elif p_change > q_change and q_change < i:
        if  count_q < Y:
            heapq.heappush(q_hq,i)
            count_q +=1
            ans_sum = ans_sum -q_change +i
            q_change =heapq.heappop(q_hq)
print(ans_sum)

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+=p[:x]+q[:y]
r.sort(reverse=True)
ans=0
for i in range(x+y):
    ans+=r[i]
print(ans)

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

mu += sorted(r)[-x:]
mu += sorted(b)[-y:]
print(sum(sorted(mu)[-(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]

    apple = list()
    apple.extend(p)
    apple.extend(q)
    apple.extend(r)

    apple.sort(reverse=True)
    ans = 0
    for i in range(X+Y):
        ans += apple[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
x, y, a, b, c = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
P.sort()
Q.sort()
R = R + P[a-x:] + Q[b-y:]
R.sort()
print(sum(R[-(x+y):]))

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 = sorted(p, reverse=True)[:X]
q = sorted(q, reverse=True)[:Y]
heapq.heapify(p)
heapq.heapify(q)
r_asc = sorted(r, reverse=True)

for r in r_asc:
    p_min = heapq.heappop(p)
    q_min = heapq.heappop(q)
    if p_min > q_min and r > q_min:
        heapq.heappush(q, r)
        heapq.heappush(p, p_min)
    elif q_min >= p_min and r > p_min:
        heapq.heappush(p, r)
        heapq.heappush(q, q_min)
    else:
        heapq.heappush(q, q_min)
        heapq.heappush(p, p_min)
        break
print(sum(p) + sum(q))

Solution (Python):

from collections import deque

X,Y,A,B,C = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = list(map(int,input().split()))
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
P = deque(P)
Q = deque(Q)
R = deque(R)
red,green = 0,0
apple = [P.popleft(), Q.popleft(), R.popleft()]
ans = 0

for _ in range(X+Y):
    mapple = max(apple)
    ans += mapple
    if apple[2]==mapple:
        if len(R)>0:
            apple[2] = R.popleft()
        else:
            apple[2] = -1
    elif apple[0]==mapple:
        red += 1
        if red<X:
            apple[0] = P.popleft()
        else:
            apple[0] = -1
    else:
        green += 1
        if green<Y:
            apple[1] = Q.popleft()
        else:
            apple[1] = -1

print(ans)

Solution (Python):
def main():
    import bisect
    x,y,a,b,c = map(int,input().split())
    p = sorted(list(map(int,input().split())))[-1*x:]
    q = sorted(list(map(int,input().split())))[-1*y:]
    r = sorted(list(map(int,input().split())))
    ans = p+q+r
    ans = sorted(ans,reverse=True)[0:x+y]
    print(sum(ans))

    
if __name__ == "__main__":
    main()

Solution (Python):
x, y, a, b, c = map(int, input().split())
p = sorted(map(int, input().split()))
q = sorted(map(int, input().split()))
r = sorted(map(int, input().split()), reverse=True)
pi = len(p) - x
qi = len(q) - y
ri = 0
for _ in range(len(r)):
    if r[ri] <= min(p[min(pi, len(p)-1)], q[min(qi, len(q)-1)]):
        break
    else:
        if pi >= len(p) and qi >= len(q):
            break
        else:
            if pi < len(p) and qi < len(q):
                if p[pi] < q[qi]:
                    p[pi] = r[ri]
                    pi += 1
                else:
                    q[qi] = r[ri]
                    qi += 1
            elif pi < len(p) and qi >= len(q):
                p[pi] = r[ri]
                pi += 1
            else:
                q[qi] = r[ri]
                qi += 1
            ri += 1
print(sum(p[-x:]) + sum(q[-y:]))

Solution (Python):
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    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())))

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

    

    cl = 0
    while len(ans) and len(r):
        if ans[-1] < r[-1]:
            cl += r[-1]
            ans.pop(-1)
            r.pop(-1)
        else:
            break

    
    print(sum(ans) + cl)


if __name__ == '__main__':
    solve()

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

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

p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
r = [-x for x in r]

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

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

heapify(p)
heapify(q)
heapify(r)

is_p = True
is_q = True

while is_p and is_q and r:
    pp = heappop(p)
    qq = heappop(q)
    rr = heappop(r)

    if pp <= qq:
        heappush(q, qq)
        if pp < -rr:
            heappush(p, -rr)
        else:
            is_p = False
            heappush(p, pp)
            heappush(r, rr)
    else:
        heappush(p, pp)
        if qq < -rr:
            heappush(q, -rr)
        else:
            is_q = False
            heappush(q, qq)
            heappush(r, rr)

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)
p = p[:x]
q = q[:y]
w = p+q+r
w.sort(reverse=True)
print(sum(w[:x+y]))

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)

red = sorted(P[:X])
gre = sorted(Q[:Y])
m = min((X+Y),C)
n = R[:m]
red.extend(gre)
red.extend(n)
red = sorted(red, reverse=True)

print(sum(red[: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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:x]
q=q[:y]

p.extend(q)
p.extend(r)

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

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)
ans = p+q+r
ans.sort(reverse=True)
print(sum(ans[:x+y]))

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)
ans = p+q+r
ans.sort(reverse=True)
print(sum(ans[:x+y]))

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

ls_a.sort(reverse=True)
ls_b.sort(reverse=True)
ls_c.sort(reverse=True)

ls=sorted(ls_a[:X]+ls_b[:Y])
ans=sum(ls)
for i in range(min(X+Y, C)):
    if ls[i] < ls_c[i]:
        ans += ls_c[i] - ls[i]
print(ans)

Solution (Python):
X,Y,A,B,C=map(int,input().split())
ls_a=sorted(list(map(int,input().split())))
ls_b=sorted(list(map(int,input().split())))
ls_c=sorted(list(map(int,input().split())))
ls_x=ls_a[A-X:A]
ls_y=ls_b[B-Y:B]
ls_c.reverse()

ans=sum(ls_x)+sum(ls_y)
a=b=c=0
for _ in range(min([X+Y,C])):
  if a==len(ls_x):
    m=min([ls_y[b],ls_c[c]])
    if m==ls_y[b]:
      ans+=(-ls_y[b]+ls_c[c])
      b+=1
      c+=1
    else:
      break
  elif b==len(ls_y):
    m=min([ls_x[a],ls_c[c]])
    if m==ls_x[a]:
      ans+=(-ls_x[a]+ls_c[c])
      a+=1
      c+=1
    else:
      break
  else:    
    m=min([ls_x[a],ls_y[b],ls_c[c]])
    if m==ls_x[a]:
      ans+=(-ls_x[a]+ls_c[c])
      a+=1
      c+=1
    elif m==ls_y[b]:
      ans+=(-ls_y[b]+ls_c[c])
      b+=1
      c+=1
    else:
      break
print(ans)

Solution (Python):
X,Y,A,B,C=map(int,input().split())
ls_a=sorted(list(map(int,input().split())))
ls_b=sorted(list(map(int,input().split())))
ls_c=sorted(list(map(int,input().split())))
ls_x=ls_a[A-X:A]
ls_y=ls_b[B-Y:B]
ls_c.reverse()

ans=sum(ls_x)+sum(ls_y)
a=b=c=0
for _ in range(min([X+Y,C])):
  if a==len(ls_x):
    m=min([ls_y[b],ls_c[c]])
    if m==ls_y[b]:
      ans+=(-ls_y[b]+ls_c[c])
      b+=1
      c+=1
    else:
      break
  elif b==len(ls_y):
    m=min([ls_x[a],ls_c[c]])
    if m==ls_x[a]:
      ans+=(-ls_x[a]+ls_c[c])
      a+=1
      c+=1
    else:
      break
  else:    
    m=min([ls_x[a],ls_y[b],ls_c[c]])
    if m==ls_x[a]:
      ans+=(-ls_x[a]+ls_c[c])
      a+=1
      c+=1
    elif m==ls_y[b]:
      ans+=(-ls_y[b]+ls_c[c])
      b+=1
      c+=1
    else:
      break
print(ans)

Solution (Python):
import bisect
def bisect_left_reverse(a, x):
    if a[0] <= x:
        return 0
    if x < a[-1]:
        return len(a)
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= x:
            ok = mid
        else:
            ng = mid
    return ok
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 = p[:X]+q[:Y]
B = []
A.sort(reverse=True)
for i in range(len(r)):
    n = r[i]
    if A[0] == 0:
      break
    if A[-1] < n:
        index = bisect_left_reverse(A,n)
        A.pop(-1)
        B.append(n)
    else:
        break
print(sum(A)+sum(B))

Solution (Python):
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math

MOD = 10 ** 9 + 7



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]
concat = P + Q + R
concat.sort(reverse=True)
print(sum(concat[: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()
while len(p)>X:
  p.pop()
while len(q)>Y:
  q.pop()
s=p+q+r
s.sort()
s.reverse()
c=0
for i in range(X+Y):
  c+=s[i]
print(c)

Solution (Python):
def solve(x, y, a, b, c, p, q, r):
    p = sorted(p)[::-1]
    q = sorted(q)[::-1]
    r = sorted(r)[::-1]
    v = sorted(p[:x] + q[:y])
    n = len(v)
    t = sum(v)
    for i in range(min(n,c)):
        if v[i] < r[i]:
            t += r[i] - v[i]
    return t

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()))
print(solve(x, y, a, b, c, p, q, r))

Solution (Python):
def solve(x, y, a, b, c, p, q, r):
    p = sorted(p)[::-1]
    q = sorted(q)[::-1]
    r = sorted(r)[::-1]
    v = sorted(p[:x] + q[:y])
    n = len(v)
    t = sum(v)
    i = 0
    for k in range(c):
        if (i < n) and (v[i] < r[k]):
            t += r[k] - v[i]
            i += 1
    return t

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()))
print(solve(x, y, a, b, c, p, q, r))

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)

eat_list = []

for i in range(x):
    eat_list.append(p[i])
for i in range(y):
    eat_list.append(q[i])
for i in range(c):
    eat_list.append(r[i])
    
eat_list.sort(reverse=True)

ans = 0

for i in range(x+y):
    ans += eat_list[i]

print(ans)

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np

Mod = 1000000007
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

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 = sorted(p)[::-1][:x]
    q = sorted(q)[::-1][:y]
    A = p + q + r
    A.sort(reverse=True)
    print(sum(A[:x+y]))
if __name__ == "__main__":
    main()

Solution (Python):
import sys
import heapq
import re
from heapq import heapify, heappop, heappush
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


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)


r_ruiseki = [0 for i in range(c)]
for i in range(c):
    if i == 0:
        r_ruiseki[i] = r[i]
    else:
        r_ruiseki[i] = r[i] + r_ruiseki[i - 1]


ans = sum(p[:x]) + sum(q[:y])

s = sorted(p[:x] + q[:y], reverse=True)
s_ruiseki = [0 for i in range(x + y)]

for i in range(x + y):
    if i == 0:
        s_ruiseki[i] = s[i]
    else:
        s_ruiseki[i] = s[i] + s_ruiseki[i - 1]

s_ruiseki.reverse()

for i in range(1, min(x + y, c + 1)):
    ans = max(ans, r_ruiseki[i - 1] + s_ruiseki[i])
print(ans)

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





def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))





def main():
	x,y,a,b,c=MI()
	p=LI()
	q=LI()
	r=LI()

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

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

	li=p+q+r
	li.sort()
	li=li[len(li)-x-y:]
	print(sum(li))




if __name__ == "__main__":
	main()

Solution (Python):
import heapq

X, Y, A, B, C = map(int, input().split()) 
pl = list(map(int, input().split())) 
ql = list(map(int, input().split())) 
rl = list(map(int, input().split())) 

pl.sort()
ql.sort()
rl.sort(reverse=True)
pl_x_larges = pl[A-X:A]
ql_y_larges = ql[B-Y:B]

pq_xy = pl_x_larges + ql_y_larges
pq_xy.sort()

for i, r in enumerate(rl):
    if r > pq_xy[i]:
        pq_xy[i] = r
    else:
        break

print(sum(pq_xy))

Solution (Python):
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


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


def z():
    return int(input())


def S():
    return input()


X, Y, A, B, C = zz()
p = zz()
q = zz()
r = zz()

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

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

heapq.heapify(p)
heapq.heapify(q)
min_p = heapq.heappop(p)
min_q = heapq.heappop(q)
for i in range(C):

    if (min_p <= min_q):
        if (min_p < r[i]):
            
            heapq.heappush(p, r[i])
            min_p = heapq.heappop(p)
        else:
            break
    else:
        if (min_q < r[i]):
            heapq.heappush(q, r[i])
            min_q = heapq.heappop(q)
        else:
            break


print(sum(p) + sum(q) + min_p + min_q)