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):
from collections import deque


def main():
    X, Y, A, B, C = map(int, input().split(' '))
    a_list = list(map(int, input().split(' ')))
    b_list = list(map(int, input().split(' ')))
    c_list = list(map(int, input().split(' ')))
    a_deq = deque(sorted(a_list, reverse=True)[:X])
    b_deq = deque(sorted(b_list, reverse=True)[:Y])
    c_deq = deque(sorted(c_list, reverse=True))
    result = sum(a_deq) + sum(b_deq)
    while len(c_deq) > 0:
        try:
            if a_deq[-1] < b_deq[-1]:
                min_ab = a_deq.pop()
            else:
                min_ab = b_deq.pop()
        except IndexError:
            if len(a_deq) > 0:
                min_ab = a_deq.pop()
            elif len(b_deq) > 0:
                min_ab = b_deq.pop()
            else:
                break

        if min_ab >= c_deq[0]:
            break
        result += c_deq.popleft() - min_ab

    print(result)


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)

ep = p[:X][::-1]
eq = q[:Y][::-1]
ans = sum(ep)+sum(eq)

ip = 0
iq = 0
for pr in r:
    if ip >= X:
        if iq >= Y:
            break
        elif eq[iq] < pr:
            ans += -eq[iq] + pr
            iq += 1
        else:
            break
    elif iq >= Y:
        if ep[ip] < pr:
            ans += -ep[ip] + pr
            ip += 1
        else:
            break
    else:
        if ep[ip] < eq[iq]:
            if pr > ep[ip]:
                ans += -ep[ip] + pr
                ip += 1
            else:
                break
        else:
            if pr > eq[iq]:
                ans += -eq[iq] + pr
                iq += 1
            else:
                break
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
ai=0
bi=0
ci=0
ans=0
for i in range(x+y):
    if ai<x:
        if bi<y:
            if ci<c:
                m=max(p[ai],q[bi],r[ci])
                ans+=m
                if p[ai]==m:
                    ai+=1
                elif q[bi]==m:
                    bi+=1
                elif r[ci]==m:
                    ci+=1
            else:
                m=max(p[ai],q[bi])
                ans+=m
                if p[ai]==m:
                    ai+=1
                elif q[bi]==m:
                    bi+=1
        else:
            if ci<c:
                m=max(p[ai],r[ci])
                ans+=m
                if p[ai]==m:
                    ai+=1
                elif r[ci]==m:
                    ci+=1
            else:
                m=p[ai]
                ans+=m
                if p[ai]==m:
                    ai+=1
    else:
        if bi<y:
            if ci<c:
                m=max(q[bi],r[ci])
                ans+=m
                if q[bi]==m:
                    bi+=1
                elif r[ci]==m:
                    ci+=1
            else:
                m=q[bi]
                ans+=m
                if q[bi]==m:
                    bi+=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()
q.sort()
r.sort()

p.reverse()
q.reverse()
r.reverse()

qi = 0
ri = 0

lis = [0]

for i in range(B+C):

    if (qi == Y or qi == B) and ri==C:
        lis.append(float("-inf"))
    elif qi == Y or qi == B:
        lis.append(lis[-1] + r[ri])
        ri += 1
    elif ri == C:
        lis.append(lis[-1] + q[qi])
        qi += 1
    elif q[qi] > r[ri]:
        lis.append(lis[-1] + q[qi])
        qi += 1
    else:
        lis.append(lis[-1] + r[ri])
        ri += 1


ans = 0

n = 0
for i in range(X+1):

    if i != 0:
        n += p[i-1]

    if len(lis) > X+Y-i:
        ans = max(ans , n + lis[X+Y-i])

print (ans)

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)

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

if len(list)<x+y:
  ll=len(list)
  for i in range((x+y)-len(list)):
    list.append(r[i])
  r=r[(x+y)-ll:]
list=sorted(list)

for i in range(len(r)):
  if list[i]<r[i]:
    list[i]=r[i]
  else:
    break
print(sum(list))

Solution (Python):
x,y,a,b,c = [int(c) for c in input().split()]
ps = [int(c) for c in input().split()]
qs = [int(c) for c in input().split()]
rs = [int(c) for c in input().split()]

ps.sort(reverse=True)
qs.sort(reverse=True)
rs.sort(reverse=True)

pind = x-1
qind = y-1
rind = 0
for i in range(c+1):
    p = ps[pind]
    q = qs[qind]
    r = rs[rind]
    if min(p,q)>= r:
        break
    if p <= q:
        ps[pind] = r
        pind -= 1
    else:
        qs[qind] = r
        qind -= 1
    rind += 1
    if rind >= c:
        break
    pind = max(pind,0)
    qind = max(qind,0)
print(sum(ps[:x]) + sum(qs[:y]))

Solution (Python):
x,y,a,b,c = map(int,input().split())
arra = list(map(int, input().split()))
arrb = list(map(int, input().split()))
arrc = list(map(int, input().split()))

arra.sort()
arrb.sort()
arrc.sort()

temp = arra[a-x:]+arrb[b-y:]


if x+y<c:
  	temp +=arrc[c-x-y:]
else:
  	temp +=arrc
    

temp.sort()

arr = temp[len(temp)-x-y:]

print(sum(arr))

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    X,Y,A,B,C = LMIIS()
    P = LMIIS()
    Q = LMIIS()
    R = LMIIS()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    i_p = 0
    i_q = 0
    i_r = 0

    sum_d = 0
    while i_p + i_q + i_r < X+Y:
        if i_r < len(R) and (i_p == X or P[i_p] < R[i_r]) and (i_q == Y or Q[i_q] < R[i_r]):
            sum_d += R[i_r]
            i_r += 1
        elif i_p < X:
            if i_q < Y:
                if P[i_p] < Q[i_q]:
                    sum_d += Q[i_q]
                    i_q += 1
                else:
                    sum_d += P[i_p]
                    i_p += 1
            else:
                sum_d += P[i_p]
                i_p += 1
        else:
            sum_d += Q[i_q]
            i_q += 1


        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    print(sum_d)

    

if __name__ == '__main__':
    main()

Solution (Python):
x,y,a,b,c = map(int,input().split())
List_a = [int(i) for i in input().split()]
List_b = [int(i) for i in input().split()]
List_c = [int(i) for i in input().split()]
List_a.sort()
List_b.sort()
List_c.sort(reverse =True)
List_a = List_a[-x:]
List_b = List_b[-y:]
h = 0
j = 0
r = 0
k = 0
for i in range(c):
    if x == h:
        r = 1
        k = i
        break
    if y == j:
        r = 2
        k = i
        break
    if List_a[h] < List_b[j]:
        if List_a[h] < List_c[i]:
            List_a[h] = List_c[i]
            h += 1
        else:
            break
    else:
        if List_b[j] < List_c[i]:
            List_b[j] = List_c[i]
            j += 1
        else:
            break
if r == 1:
    for i in range(k,c):
        if List_b[j] < List_c[i]:
            List_b[j] = List_c[i]
            j += 1
        else:
            break
if r == 2:
    for i in range(k,c):
        if List_a[h] < List_c[i]:
            List_a[h] = List_c[i]
            h += 1
        else:
            break
print(sum(List_a)+sum(List_b))

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

pi = 0
qi = 0
ri = 0
while True:
    if ri > c-1: break
    if pi <= x-1 and qi <= y-1:
        if p[pi] < q[qi]:
            if p[pi] < r[ri]:
                p[pi] = r[ri]
                pi += 1
                ri += 1
            else:
                break
        else:
            if q[qi] < r[ri]:
                q[qi] = r[ri]
                qi += 1
                ri += 1
            else:
                break
    elif pi <= x-1:
        if p[pi] < r[ri]:
            p[pi] = r[ri]
            pi += 1
            ri += 1
        else:
            break
    elif qi <= y-1:
        if q[qi] < r[ri]:
            q[qi] = r[ri]
            qi += 1
            ri += 1
        else:
            break
    else:
        break

print(sum(p)+sum(q))

Solution (Python):
import heapq
from collections import deque
import sys
input = sys.stdin.readline
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
q.sort()
r.sort(reverse=True)
red_que = p[a-x:]
heapq.heapify(red_que)
green_que = q[b-y:]
heapq.heapify(green_que)
for i in range(c):
    re_mi = heapq.heappop(red_que)
    gr_mi = heapq.heappop(green_que)
    if re_mi < gr_mi:
        if re_mi < r[i]:
            heapq.heappush(red_que,r[i])
            heapq.heappush(green_que,gr_mi)
        else:
            heapq.heappush(green_que,gr_mi)
            heapq.heappush(red_que,re_mi)
            break
    else:
        if gr_mi < r[i]:
            heapq.heappush(red_que,re_mi)
            heapq.heappush(green_que,r[i])
        else:
            heapq.heappush(green_que,gr_mi)
            heapq.heappush(red_que,re_mi)
            break
print(sum(green_que) + sum(red_que))

Solution (Python):


line = [int(x) for x in input().split()]
reds = [int(x) for x in input().split()]
greens = [int(x) for x in input().split()]
colorless = [int(x) for x in input().split()]

reds.sort(reverse = True)
greens.sort(reverse = True)
colorless.sort(reverse = True)

rI = line[0]-1
gI = line[1]-1

for i in range(0, line[4]):
    if rI == -1 and gI == -1:
        break
    
    elif rI == -1:
        if colorless[i] < greens[gI]:
            break
        else:
            greens[gI] = colorless[i]
            gI = gI - 1
            
    elif gI == -1:
        if colorless[i] < reds[rI]:
            break
        else:
            reds[rI] = colorless[i]
            rI = rI-1
            
    else:
        if colorless[i] < reds[rI] and colorless[i] < greens[gI]:
            break
        else:
            if reds[rI] < greens[gI]:
                reds[rI] = colorless[i]
                rI = rI-1
            else:
                greens[gI] = colorless[i]
                gI = gI - 1

print(sum(reds[:line[0]] ) + sum(greens[:line[1]]))

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_rest = sorted(p + [0])
q_rest = sorted(q + [0])
r_rest = sorted(r + [0])

ans = 0
p_count = 0
q_count = 0
r_count = 0
while (p_count + q_count + r_count) < (X + Y):
    if p_count < X and q_count < Y:
        candidates = [p_rest[-1], q_rest[-1], r_rest[-1]]
    elif p_count < X and q_count == Y:
        candidates = [p_rest[-1], 0, r_rest[-1]]
    elif p_count == X and q_count < Y:
        candidates = [0, q_rest[-1], r_rest[-1]]

    wanna_eat = max(candidates)
    eat_idx = candidates.index(wanna_eat)

    if eat_idx == 0:
        p_count += 1
        ans += p_rest.pop()
    elif eat_idx == 1:
        q_count += 1
        ans += q_rest.pop()
    else:
        r_count += 1
        ans += r_rest.pop()
    

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

eat = p[:X] + q[:Y]
eat.sort()
for i in range(min(C,X+Y)):
    if r[i] > eat[i]:
        eat[i] = r[i]

ans = sum(eat)
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()))
pqr = []
for i in range(a):
    pqr.append([p[i],"p"])
for i in range(b):
    pqr.append([q[i],"q"])
for i in range(c):
    pqr.append([r[i],"r"])
pqr.sort(reverse=True)
cx = 0
cy = 0
cr = 0
s = 0
for e,c in pqr:
    if c =="p":
        if cx <x:
            cx+=1
            s+=e
    elif c =="q":
        if cy <y:
            cy+=1
            s+=e
    else:
        cr+=1
        s+=e
    if cx+cy+cr ==x+y:
        break
print(s)

Solution (Python):
X,Y,A,B,C=list(map(int,input().split()))
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
eat=[]
eat.extend(p[:X])
eat.extend(q[:Y])
eat.sort(reverse=True)
i=0
j=0
result=0
for v in eat:
  if i<C and eat[j]<r[i]:
    result+=r[i]
    i+=1
  else:
    result+=eat[j]
    j+=1
  if i+j==X+Y:
    break
print(result)

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

PQ = P[:X] + Q[:Y]

PQ.sort()

for i in range(min(X+Y, C)):
    if PQ[i] < R[i]:
        PQ[i] = R[i]

print(sum(PQ))

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)
pi = 0
qi = 0
ri = 0
if x < a:
    pi = a-x
if y < b:
    qi = b-y
result = sum(p[pi:a]) + sum(q[qi:b]) + sum(r)
inf = 10**9
p.append(inf)
q.append(inf)
r.append(inf)
ignorea = False
ignoreb = False
while a-pi + b-qi + c-ri > x+y:
    if ignorea:
        result -= q[qi]
        qi+=1
    elif ignoreb:
        result -= p[pi]
        pi+=1
    else:
        if p[pi] > q[qi]:
            if (b-qi+c-ri) == y:
                ignoreb = True
                result -= p[pi]
                pi+=1
                continue
            if q[qi] >= r[ri]:
                result -= r[ri]
                ri+=1
            else:
                result -= q[qi]
                qi+=1
        else:
            if (a-pi+c-ri) == x:
                ignorea = True
                result -= q[qi]
                qi+=1
                continue
            if p[pi] >= r[ri]:
                result -= r[ri]
                ri+=1
            else:
                result -= p[pi]
                pi+=1
print(result)

Solution (Python):
x, y, na, nb, nc = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

A2 = A[-x:]
B2 = B[-y:]
ai = na - x
bi = nb - y
ans = 0
while C:
  if ai < na and bi < nb:
    if A[ai] < B[bi]:
      if C[-1] > A[ai]:
        A[ai] = C.pop(-1)
        ai += 1
      else:
        break
    else:
      if C[-1] > B[bi]:
        B[bi] = C.pop(-1)
        bi += 1
      else:
        break
  elif ai < na:
    if C[-1] > A[ai]:
      A[ai] = C.pop(-1)
      ai += 1
    else:
      break
  elif bi < nb:
    if C[-1] > B[bi]:
      B[bi] = C.pop(-1)
      bi += 1
    else:
      break
  else:
    break
print(sum(A[-x:]) + sum(B[-y:]))

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]"):
    sp = list(reversed(sorted(p)))
    sq = list(reversed(sorted(q)))
    sr = list(sorted(r))
    
    
    spi = sp[:X]
    sqi = sq[:Y]
    
    
    
    rIndex = len(sr)-1
    pIndex = len(spi)-1
    qIndex = len(sqi)-1
    while rIndex >= 0:
        if (pIndex < 0 or sr[rIndex] < spi[pIndex]) and (qIndex < 0 or sr[rIndex] < sqi[qIndex]):
            break
        if pIndex < 0:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
            rIndex-=1
            continue
        if qIndex < 0:
            spi[pIndex] = sr[rIndex]
            pIndex-=1
            rIndex-=1
            continue
        if sr[rIndex] - spi[pIndex] > sr[rIndex] - sqi[qIndex]:
            spi[pIndex] = sr[rIndex]
            pIndex-=1
        else:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
        rIndex-=1
    print(sum(spi)+sum(sqi))
    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):
X,Y,A,B,C = list(map(int, input().split()))
red = list(map(int, input().split()))
green = list(map(int, input().split()))
white = list(map(int, input().split()))

red = sorted(red, reverse=True)[:X]
green = sorted(green, reverse=True)[:Y]
ans = sum(red) + sum(green)

white = sorted(white, reverse=True)
for i in range(len(white)):
    current = white[i]
    if len(red) != 0 and len(green) != 0:
        if red[-1] < green[-1] and current > red[-1]:
            ans += current - red[-1]
            red.pop(-1)
        elif red[-1] >= green[-1] and current > green[-1]:
            ans += current - green[-1]
            green.pop(-1)
        else:
            break
    elif len(red) != 0:
        if current > red[-1]:
            ans += current - red[-1]
            red.pop(-1)
    elif len(green) != 0:
        if current > green[-1]:
            ans += current - green[-1]
    else:
        break

print(ans)

Solution (Python):
x, y, a, b, c = list(map(int, input().split()))
ret = (a + b) * [0]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
X = []
Y = []
A.sort()
B.sort()
C.sort()

X = A[(a - x):]
Y = B[(b - y):]

idxA = 0
idxB = 0
idxC = c - 1
for i in reversed(range(c)):
    if idxA == x and idxB == y:
        break
    if idxA == x:
        if C[i] > Y[idxB]:
            Y[idxB] = C[i]
            idxB += 1
        else:
            break
    elif idxB == y:
        if C[i] > X[idxA]:
            X[idxA] = C[i]
            idxA += 1
        else:
            break
    elif C[i] <= min(X[idxA], Y[idxB]):
        break
    elif X[idxA] <= Y[idxB]:
        X[idxA] = C[i]
        idxA += 1
    else:
        Y[idxB] = C[i]
        idxB += 1
print((sum(X) + sum(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)


ans = 0
tX = X 
tY = Y 

for i in range(X):
  ans += P[i]
for i in range(Y):
  ans += Q[i]

temp = ans

for i in range( min(X+Y, C) ): 
  if tX > 0 and tY > 0:
      if P[tX-1] < Q[tY-1]:
        temp += R[i] - P[tX-1]
        tX -= 1
      else:
        temp += R[i] - Q[tY-1]
        tY -= 1
  elif tX == 0:
    temp += R[i] - Q[tY-1]
    tY -= 1
  else:
    temp += R[i] - P[tX-1]
    tX -= 1
  ans = max(ans, temp)
  
print(ans)

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

p.extend(q)
p.extend(r)
print(sum(sorted(p)[:-X-Y-1:-1]))

Solution (Python):
from heapq import heapify, heappop, heappush, heappushpop
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(reverse=True)
q.sort(reverse=True)
r.sort()
p=p[:x]
q=q[:y]
heapify(p)
heapify(q)
mp = heappop(p)
mq = heappop(q)
for ri in r:
  if mp<mq:
    if mp<ri:
      heappush(p, ri)
      mp=heappop(p)
  else:
    if mq<ri:
      heappush(q, ri)
      mq=heappop(q)

print(sum(p)+sum(q)+mp+mq)

Solution (Python):
x, y, a, b, c = map(int,input().split())
datp = list(map(int, input().split()))
datq = list(map(int, input().split()))
datr = list(map(int, input().split()))
datp.sort(reverse=True)
datq.sort(reverse=True)
datr.sort(reverse=True)
res = sum(datp[:x]) + sum(datq[:y])
pl, pr = 0, x - 1
ql, qr = 0, y - 1





for i in range(c):
    
    
    diffp, diffq = -1, -1
    if pr >= 0:
        diffp = datr[i] - datp[pr] 
    if qr >= 0:
        diffq = datr[i] - datq[qr] 
    
    if diffp <= 0 and diffq <= 0: 
        
        break
    if diffp >= diffq and pr >= 0: 
        
        res += diffp
        pr -= 1
    elif qr >= 0 and diffq > 0: 
        
        res += diffq
        qr -= 1

print(res)

Solution (Python):
x,y,a,b,c=map(int,input().split())
p,q,r=[list(reversed(sorted(list(map(int,input().split()))))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)

Solution (Python):
from heapq import heappush, heappop


def main():
    X, Y, _, _, _ = map(int, input().split())

    P = []
    Q = []
    R = []
    for v in input().split():
        heappush(P, -int(v))
    for v in input().split():
        heappush(Q, -int(v))
    for v in input().split():
        heappush(R, -int(v))

    score = 0
    cp = 0
    cq = 0
    cr = 0

    XY = X + Y

    while len(P) > 0 and len(Q) > 0 and len(R) > 0 and cp < X and cq < Y and cp + cq + cr < XY:
        p = -P[0]
        q = -Q[0]
        r = -R[0]
        if r > p and r > q:
            score -= heappop(R)
            cr += 1
        elif p > q:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(Q)
            cq += 1

    while len(P) > 0 and len(R) > 0 and cp < X and cp + cq + cr < XY:
        p = -P[0]
        r = -R[0]
        if p >= r:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(R)
            cr += 1

    while len(Q) > 0 and len(R) > 0 and cq < Y and cp + cq + cr < XY:
        q = -Q[0]
        r = -R[0]
        if q >= r:
            score -= heappop(Q)
            cq += 1
        else:
            score -= heappop(R)
            cr += 1

    while len(P) > 0 and len(Q) > 0 and cp < X and cq < Y and cp + cq + cr < XY:
        p = -P[0]
        q = -Q[0]
        if p > q:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(Q)
            cq += 1

    while cp < X and cp + cq + cr < XY:
        if len(P) > 0:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(R)
            cr += 1

    while cq < Y and cp + cq + cr < XY:
        if len(Q) > 0:
            score -= heappop(Q)
            cq += 1
        else:
            score -= heappop(R)
            cr += 1

    print(score)


main()

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(reverse=True)
ph = []
qh = []
for i in range(x):
    ph.append(p[i])
for i in range(y):
    qh.append(q[i])
heapq.heapify(ph)
heapq.heapify(qh)
for i in range(c):
    p0 = heapq.heappop(ph)
    q0 = heapq.heappop(qh)
    if min(p0,q0) >= r[i]:
        heapq.heappush(ph,p0)
        heapq.heappush(qh,q0)
        break
    else:
        if p0 > q0:
            heapq.heappush(ph,p0)
            heapq.heappush(qh,r[i])
        else:
            heapq.heappush(qh,q0)
            heapq.heappush(ph,r[i])
print(sum(ph)+sum(qh))

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())) + [0]*(x+y)

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

eat.sort()
der = []
for i in range(len(eat)):
  if eat[i] < r[i]:
    der.append(r[i])
  else:
    der.append(eat[i])

print(sum(der))

Solution (Python):

from sys import stdin, stdout


def read():
    x = stdin.readline().rstrip()
    return x


def __main__():

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

    p = [int(i) for i in read().split()]
    q = [int(i) for i in read().split()]
    r = [int(i) for i in read().split()]

    red = [(j, 'r') for j in p]
    green = [(j, 'g') for j in q]
    white = [(j, 'w') for j in r]

    apples = red + green + white

    apples.sort(reverse=True)

    res = 0
    fl = True
    ind = 0
    number = 0
    x0 = 0
    y0 = 0

    while fl:
        ap = apples[ind]
        if ap[1] == 'r':
            if x0 < x:
                number += 1
                x0 += 1
                res += ap[0]
        elif ap[1] == 'g':
            if y0 < y:
                number += 1
                y0 += 1
                res += ap[0]
        else:
            number += 1
            res += ap[0]

        ind += 1

        if number == x + y:
            fl = False

    print(res)


__main__()

Solution (Python):


x, y, a, b, c = [0] * 5
p, q, r = [[]] * 3

def format_input(filename = None):
	global x, y, a, b, c
	global p, q, r
	if filename == None:
		x, y, a, b, c = list(map(int, input().split()))
		p, q, r = [list(map(int, input().split())) for i in range(3)]

	elif filename == '__random__':
		from random import randint as rng
		x = rng(1, 10**5)
		y = rng(1, 10**5)
		a = rng(x, 10**5)
		b = rng(y, 10**5)
		c = rng(1, 10**5)
		p = [rng(1, 10**9) for i in range(a)]
		q = [rng(1, 10**9) for i in range(b)]
		r = [rng(1, 10**9) for i in range(c)]
		print(x, y, a, b, c)
		[print(' '.join(list(map(str, i)))) for i in [p, q, r]]

def get_answer():
	red = sorted(p)[-x:]
	green = sorted(q)[-y:]
	delicious_apples = sorted(red + green + r)[-(x+y):]
	return sum(delicious_apples)

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)

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]"):
    pqr = [(0,p[i]) for i in range(A)] + [(1,q[i]) for i in range(B)] + [(2,r[i]) for i in range(C)]
    pqr.sort(key= lambda x: -x[1])

    answer = 0
    count = [0,0,0]
    for i in range(len(pqr)):
        if sum(count) == X+Y:
            break
        key,value = pqr[i]
        if key == 0:
            if count[key] >= X:
                continue
            count[key] += 1
        elif key == 1:
            if count[key] >= Y:
                continue
            count[key] += 1
        else:
            count[key] += 1
        answer += value
    
    print(answer)
    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 bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29


def main():
    X,Y,A,B,C = isRaw()
    ps = isRaw()
    qs = isRaw()
    rs = isRaw()
    ps.sort(reverse=True)
    ps = ps[:X]
    qs.sort(reverse=True)
    qs = qs[:Y]
    rs.sort()
    pqr = ps+qs+rs
    pqr.sort(reverse=True)
    return sum(pqr[:X+Y])

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

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

i=0
j=0
k=0
s=0
for _ in range(x+y):
    u=[]
    if i<x and i<p:
        u.append([a[i],'a'])
    if j<y and j<q:
        u.append([b[j],'b'])
    if  k<r:
        u.append([c[k],'c'])
    u.sort(reverse=True)
    s+=u[0][0]
    if u[0][1]=='a':
        i+=1
    elif u[0][1]=='b':
        j+=1
    else:
        k+=1
print(s)

Solution (Python):
from heapq import heappush, heappop

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


apples = []
cat_num = [x, y, c]
for cat in range(3):
    apples.append(sorted(map(int, input().split()))[-cat_num[cat]:])


h = []

for i in range(3):
    heappush(h, (-apples[i].pop(), i))


s = 0

cnt = x + y
while cnt:
    best_val, cat = heappop(h)
    s += best_val
    cnt -= 1
    if apples[cat]:
        heappush(h, (-apples[cat].pop(), cat))

print(-s)

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

print(sum(r))