Problem:
 Score : 500 points    Problem Statement You are going to eat X red apples and Y green apples. You have A red apples of deliciousness p_1,p_2, \dots, p_A , B green apples of deliciousness q_1,q_2, \dots, q_B , and C colorless apples of deliciousness r_1,r_2, \dots, r_C . Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively. From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible. Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.      Constraints  1 \leq X \leq A \leq 10^5  1 \leq Y \leq B \leq 10^5  1 \leq C \leq 10^5  1 \leq p_i \leq 10^9  1 \leq q_i \leq 10^9  1 \leq r_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  X  Y  A  B  C  p_1  p_2  ...  p_A  q_1  q_2  ...  q_B  r_1  r_2  ...  r_C       Output Print the maximum possible sum of the deliciousness of the eaten apples.        Sample Input 1 1 2 2 2 1
2 4
5 1
3      Sample Output 1 12  The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:   Eat the 2 -nd red apple.  Eat the 1 -st green apple.  Paint the 1 -st colorless apple green and eat it.        Sample Input 2 2 2 2 2 2
8 6
9 1
2 1      Sample Output 2 25       Sample Input 3 2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4      Sample Output 3 74

Solution (Python):
x,y,a,b,c=map(int, input().split())
*P,=map(int, input().split())
*Q,=map(int, input().split())
*R,=map(int, input().split())

P = sorted(P)[-x:]
Q = sorted(Q)[-y:]
H = sorted(P+Q)
R.sort()

ans=0
for h in H:
    if R and R[-1] > h:
        ans += R.pop()
    else:
        ans += h
print(ans)

Solution (Python):
(x,y,a,b,c),*t=[map(int,t.split())for t in open(0)]
s=sorted
p,q,r=map(s,t)
print(sum(s(p[-x:]+q[-y:]+r)[-x-y:]))

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

ans = sorted(r + g + m)
print(sum(ans[len(ans) - x - y:]))

Solution (Python):


import sys
from heapq import heapify, heappop, heappush

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

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

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

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

for c in C:
    if c > que[0]:
        heappop(que)
        heappush(que, c)
ans = sum(que)
print(ans)

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

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

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]

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

ans = sum(ALL[: X + Y])
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_list = P[:X]
ANS_list.extend(Q[:Y])
ANS_list.sort()
c = 0
for i in range(len(R)):
  if ANS_list[i] >= R[i]:
    break
  c+=1

if i!=0:
  ANS_list[:c] = R[:c]
print(sum(R[:c])+sum(ANS_list[c:]))

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

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

mazemaze =p[:x]+q[:y]+r
list.sort(mazemaze, reverse=True)
print(sum(mazemaze[:x+y]))

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

ans = sorted(p[A-X:] + q[B-Y:])
ans_sum = [ans[0]]
for i in range(1, X+Y):
    ans_sum.append(ans_sum[i-1] + ans[i])

r_sum = [r[0]]
for i in range(1, C):
    r_sum.append(r_sum[i-1] + r[i])

index, temp = -1, ans_sum[-1]
for i in range(min(C, X+Y)):
    if temp < ans_sum[-1] + r_sum[i] - ans_sum[i]:
        temp = ans_sum[-1] + r_sum[i] - ans_sum[i]
print(temp)

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

p = p[:x]
q = q[:y]
a = p + q + r 
a = sorted(a, reverse=True)
print(sum(a[:(x+y)]))

Solution (Python):
import heapq

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)

red = p[:X]
green = q[:Y]
nocolor = r


heapq.heapify(red)
heapq.heapify(green)


for nc in nocolor:
    R = heapq.heappop(red)
    G = heapq.heappop(green)
    heapq.heappush(red,R)
    heapq.heappush(green,G)
    if R > nc and G > nc:
        break
    elif nc > R and nc > G:
        if R > G:
            q = heapq.heappop(green)
            heapq.heappush(green,nc)
        else:
            q = heapq.heappop(red)
            heapq.heappush(red,nc)
    elif nc > R:
        q = heapq.heappop(red)
        heapq.heappush(red,nc)
    elif nc > G:
        q = heapq.heappop(green)
        heapq.heappush(green,nc)


print(sum(red)+sum(green))

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

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

D=sorted(P, reverse=True)[:X]
E=sorted(Q, reverse=True)[:Y]
Z=X+Y
if C>=Z:
    F=sorted(R, reverse=True)[:Z]
else:
    F=R

D.extend(E)
D.extend(F)
I=sorted(D, reverse=True)[:Z]

print(sum(I))

Solution (Python):
from pprint import pprint
from bisect import bisect_left
import numpy


def read():
    X, Y, A, B, C = list(map(int, input().strip().split()))
    P = list(map(int, input().strip().split()))
    Q = list(map(int, input().strip().split()))
    R = list(map(int, input().strip().split()))
    return X, Y, A, B, C, P, Q, R

def argsort(x, reverse=False):
    return sorted(range(len(x)), key=lambda k: x[k], reverse=reverse)

def solve(X, Y, A, B, C, P, Q, R):
    P = list(sorted(P, reverse=True))
    Q = list(sorted(Q, reverse=True))
    R = list(sorted(R, reverse=True))

    i = X
    j = Y
    score = sum(P[0:i]) + sum(Q[0:j])
    for r in R:
        if i > 0 and j > 0:
            if P[i-1] < Q[j-1] and P[i-1] < r:
                score = score - P[i-1] + r
                i -= 1
            elif Q[j-1] < r:
                score = score - Q[j-1] + r
                j -= 1
            else:
                break
        elif i > 0:
            if P[i-1] < r:
                score = score - P[i-1] + r
                i -= 1
            else:
                break
        elif j > 0:
            if Q[j-1] < r:
                score = score - Q[j-1] + r
                j -= 1
            else:
                break
        else:
            break
    return sum(P[:i] + Q[:j] + R[:(X-i)+(Y-j)])

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

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

l = pp + qq + r
l.sort(reverse=True)
print(sum(l[:(x + y)]))

Solution (Python):
X,Y,A,B,C = list(map(int,input().split()))
p = sorted(list(map(int,input().split())),key=lambda x:-x)
q = sorted(list(map(int,input().split())),key=lambda x:-x)
r = sorted(list(map(int,input().split())),key=lambda x:-x)

pq = sorted(p[:X] + q[:Y])

for i in range(min(X+Y,C)):
  if r[i] < pq[i]:
    break
else:
  i+=1
print(sum(r[:i] + pq[i:]))

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(reverse=True)
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
for i in range(min(len(ans),c)):
  if ans[i]<=r[rcount]:
    ans[i]=r[rcount]
    rcount+=1
print(sum(ans))

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()))
result = 0
cnt_r = x
cnt_g = y
cnt_n = x+y

apples = []
for i in range(a):
    apple = (-p[i], "R")
    hp.heappush(apples, apple)
for i in range(b):
    apple = (-q[i], "G")
    hp.heappush(apples, apple)
for i in range(c):
    apple = (-r[i], "N")
    hp.heappush(apples, apple)

while cnt_n > 0:
    tmp_a = hp.heappop(apples)
    if tmp_a[1] == "R":
        if cnt_r > 0:
            result += -tmp_a[0]
            cnt_r -= 1
            cnt_n -= 1
    elif tmp_a[1] == "G":
        if cnt_g > 0:
            result += -tmp_a[0]
            cnt_g -= 1
            cnt_n -= 1
    else:
        if cnt_n > 0:
            result += -tmp_a[0]
            cnt_n -= 1

print(result)

Solution (Python):


import sys
from heapq import heapify, heappop, heappush

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

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

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

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

for c in C:
    if c > que[0]:
        heappop(que)
        heappush(que, c)
ans = sum(que)
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)

alist = []
ans = 0

for i in range(X):
    alist.append(p[i])
    
for j in range(Y):
    alist.append(q[j])
    
for k in range(C):
    alist.append(r[k])
    
alist.sort(reverse=True)
    
for k in range(X+Y):
    ans += alist[k]

print(ans)

Solution (Python):

"""
Created on Sat Mar 28 19:26:54 2020

@author: matha
"""


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

p = sorted(map(int, input().split()), reverse = 1)
q = sorted(map(int, input().split()), reverse = 1)
r = sorted(map(int, input().split()), reverse = 1)


nl = sorted(p[0:x] + q[0: y] + r, reverse = 1)

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

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

print(ans)

Solution (Python):
x,y,a,b,c = [int(x) for x in input().split()]
p = [int(x)*(-1) for x in input().split()]
q = [int(x)*(-1) for x in input().split()]
r = [int(x)*(-1) for x in input().split()]

import heapq
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
for _ in range(x):
    r.append(heapq.heappop(p))
for _ in range(y):
    r.append(heapq.heappop(q))
heapq.heapify(r)
s = 0
for i in range(x+y):
    s+=heapq.heappop(r)*(-1)
print(s)

Solution (Python):
x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
r_list = list(map(int,input().split()))


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

red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)

Solution (Python):
x, y, a, b, c = map(int, input().split())
v = []
l = list(map(int, input().split()))
for i in range(a):
    v.append([l[i], 1])
l = list(map(int, input().split()))
for i in range(b):
    v.append([l[i], 2])
l = list(map(int, input().split()))
for i in range(c):
    v.append([l[i], 3])
v.sort(reverse=True)
res = 0
cnt = [0, 0, 0]
for i in range(len(v)):
    t = v[i][1]
    if cnt[0] == x and t == 1:
        continue
    if cnt[1] == y and t == 2:
        continue
    if sum(cnt) == x + y:
        break
    cnt[t - 1] += 1
    res += v[i][0]
print(res)

Solution (Python):
x,y,a,b,c = map(int,input().split())
p = input().split()
p = [int(s) for s in p]
q = input().split()
q = [int(s) for s in q]
r = input().split()
r = [int(s) for s in r]
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
s = p[:x] + q[:y] + r
s.sort(reverse=True)
print(sum(s[:x+y]))

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)  
to_index = lambda x: int(x) - 1  
print_list_in_2D = lambda x: print(*x, sep="\n")  


def input_int(): return int(input())

def map_int_input(): return map(int, input())

MII = map_int_input

def MII_split(): return map(int, input().split())

def MII_to_index(): return map(to_index, input())

def MII_split_to_index(): return map(to_index, input().split())


def list_int_inputs(): return list(map(int, input()))

LII = list_int_inputs

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


def LII_2D(rows_number): return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number): return [LII_split() for _ in range(rows_number)]

X, Y, A, B, C = MII_split()
p_list = LII_split()
q_list = LII_split()
r_list = LII_split()

prices = sorted(p_list, reverse=True)[0:X] + sorted(q_list, reverse=True)[0:Y] + r_list
prices = sorted(prices, reverse=True)

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

Solution (Python):
def main():
    x, y, a, b, c = map(int, input().split())
    al = sorted(list(map(int, input().split())), reverse=True)[:x]
    bl = sorted(list(map(int, input().split())), reverse=True)[:y]
    cl = sorted(list(map(int, input().split())), reverse=True)
    ans = 0
    xi, yi = 0, 0
    for i in cl:
        if al:
            ai = al[-1]
        else:
            ai = 10000000000
        if bl:
            bi = bl[-1]
        else:
            bi = 10000000000
        if ai < bi:
            if al:
                m = al.pop()
            else:
                m = 0
        else:
            if bl:
                m = bl.pop()
            else:
                m = 0
                
        if m < i:
            ans += i
        else:
            ans += m
            break
    ans += sum(al)+sum(bl)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
x, y, a, b, c = map(int, input().split())
arrayP = list(map(int, input().split()))
arrayQ = list(map(int, input().split()))
arrayR = list(map(int, input().split()))
arrayP.sort(reverse=True)
arrayQ.sort(reverse=True)
arrayR.sort(reverse=True)
arrayAns = []
for i in range(x):
    arrayAns.append(int(arrayP[i]))
for i in range(y):
    arrayAns.append(int(arrayQ[i]))
arrayAns.sort()
for i in range(min(len(arrayR), len(arrayAns))):
    if arrayAns[i] <= int(arrayR[i]):
        arrayAns[i] = int(arrayR[i])
    else:
        break
print(sum(arrayAns))

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)

psum = 0

for i in range(X):
        r.append(p[i])
for i in range(Y):
        r.append(q[i])
        
r.sort(reverse=True)
ans = 0

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

        
print(ans)

Solution (Python):
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]

X, Y, A, B, C = mi()
pp = sorted(li())[-X:]
q = sorted(li())[-Y:]
r = sorted(li())

p = sorted(pp+q)


a_p = p.pop(-1)
a_r = r.pop(-1)
cnt = 0

for i in range(X+Y):
    if a_p >= a_r:
        cnt += a_p
        if p!=[]:
            a_p = p.pop(-1)
        else:
            break
    else:
        cnt += a_r
        if r==[]:
            a_r = 0
        else:
            a_r = r.pop(-1)
        
'''
a_q = q.pop(-1)

for i in range(Y):
    if a_q >= a_r:
        cnt += a_q
        if q!=[]:
            a_q = q.pop(-1)
        else:
            break
    else:
        cnt += a_r
        if r==[]:
            a_r = 0
        else:
            a_r = r.pop(-1)
'''
print(cnt)

Solution (Python):
x,y,a,b,c=map(int, raw_input().split())
As=map(int, raw_input().split())
Bs=map(int, raw_input().split())
Cs=map(int, raw_input().split())
X=x
Y=y
newarr=[]
res=0
for i in xrange(len(As)):
    newarr.append([As[i], 0])
for i in xrange(len(Bs)):
    newarr.append([Bs[i], 1])
for i in xrange(len(Cs)):
    newarr.append([Cs[i], 2])
newarr.sort(reverse=True)
i=-1
t=0
while True:
    i+=1
    if newarr[i][1]==0:
        if x>0:
            x-=1
        else:
            continue
    elif newarr[i][1]==1:
        if y>0:
            y-=1
        else:
            continue
    res+=newarr[i][0]
    
    t+=1
    if t>=X+Y:
        break


print res

Solution (Python):
x,y,a,b,c = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = list(map(int,input().split()))
ans = []
P.sort()
Q.sort()
R.sort()
for i in range(x):
    ans.append(P[a-i-1])
for i in range(y):
    ans.append(Q[b-i-1])
flag = True
ans.sort()
ans_sum = sum(ans)
cur = c-1
temp = 0
for i in range(c):
    if cur >= 0 and temp <= (x+y-1):
        if R[cur] >= ans[temp]:
            ans_sum += R[cur]-ans[temp]
            cur -= 1
            temp += 1
print(ans_sum)

Solution (Python):
def main():
    x, y, a, b, c = map(int, input().split(" "))
    ps = sorted(list(map(int, input().split(" "))), reverse=True)
    qs = sorted(list(map(int, input().split(" "))), reverse=True)
    rs = sorted(list(map(int, input().split(" "))), reverse=True)

    red_eat = ps[:x]
    blue_eat = qs[:y]

    eats = sorted(red_eat + blue_eat + rs, reverse=True)
    have = eats[:(x+y)]
    points = sum(have)
    print(points)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
from itertools import count

X, Y, A, B, C = map(int, input().split())
P = [int(n) for n in input().split()]
Q = [int(n) for n in input().split()]
R = [int(n) for n in input().split()]


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

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

for i in count():
    if not R or P[0] >= R[0] <= Q[0]:
        break
    if R[0] >= P[0] <= Q[0]:
        P.append(R.popleft())
        P.popleft()
    elif R[0] >= Q[0] <= P[0]:
        Q.append(R.popleft())
        Q.popleft()

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

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

pq = sorted(p[-X:] + q[-Y:])
ans = sum(pq)

if pq[0] < max(r):
    left = 0
    right = len(r)
    while left < right - 1:
        mid = (left + right) // 2
        if pq[mid] >= r[mid]:
            right = mid
        else:
            left = mid
    ans = ans + sum(r[:right]) - sum(pq[:right])

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)

answer = 0
_ids = [0,0,0]

while sum(_ids) < x+y:
    _answer = []
    _x, _y, _k = _ids
    if _x != x:
        _answer += [p[_x]]
    else:
        _answer += [0]
    if _y != y:
        _answer += [q[_y]]
    else:
        _answer += [0]
    if _k != c:
        _answer += [r[_k]]
    else:
        _answer += [0]
    tmp = 0
    for i, _a in enumerate(_answer):
        if tmp < _a:
            tmp = _a
            _id = i
    answer += tmp
    _ids[_id] += 1
    
print(answer)

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)
P = sorted(P[:X])
Q = sorted(Q[:Y])
R.sort()
while R and (P[0] < R[-1] or Q[0] < R[-1]):
    if P[0] < Q[0]:
        heapq.heappush(P, R.pop())
        heapq.heappop(P)
    else:
        heapq.heappush(Q, R.pop())
        heapq.heappop(Q)
print(sum(P) + sum(Q))

Solution (Python):
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

while len(lst_n)>0:
    flg = lst_r > lst_g
    cur = -1 * heappop(lst_n)
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
    else:
        break
print(sum(lst_g)+sum(lst_r))

Solution (Python):
import math
import copy
from copy import deepcopy
import sys
import fractions

from functools import reduce

import decimal
import heapq
import collections
import itertools
from operator import mul

sys.setrecursionlimit(100001)







def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList




x, y, a, b, c = map(int, input().split())
p = getInputListInt()
q = getInputListInt()
r = getInputListInt()

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

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

tmp_p_min = min(x, c)
tmp_p = p[:tmp_p_min]
tmp_q_min = min(y, c)
tmp_q = q[:tmp_q_min]

tmp_list = tmp_p+tmp_q+r
tmp_list.sort()
newlist = tmp_list[-tmp_p_min-tmp_q_min:]

ans = sum(p[tmp_p_min:]) + sum(q[tmp_q_min:]) + sum(newlist)
print(ans)

Solution (Python):
def input_multiple_number():
    return map(int, input().split())
def input_multiple_number_as_list():
    return list(map(int, input().split()))

xgiven,ygiven,agiven,bgiven,cgiven = input_multiple_number()

p_array = input_multiple_number_as_list()
q_array = input_multiple_number_as_list()
r_array = input_multiple_number_as_list()
INF = 99999999999999999
p_array.sort(reverse=True)
q_array.sort(reverse=True)
r_array.sort(reverse=True)


i = xgiven
p_array = p_array[0:xgiven]
q_array = q_array[0:ygiven]
xxx_array = p_array+q_array+r_array


xxx_array.sort(reverse=True)
nnn_arrat = xxx_array[0:xgiven+ygiven]

value=0
for aaaaa in nnn_arrat:
	value += aaaaa

print(value)

Solution (Python):
X, Y, A, B, C = (int(x) for x in input().split())
R = [int(i) for i in input().split()]
G = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

R.sort(reverse=True)
G.sort(reverse=True)

for i in range(X):
  M.append(R[i])

for i in range(Y):
  M.append(G[i])

M.sort(reverse=True)

ans = 0
for i in range(X + Y):
  ans = M[i] + ans

print(ans)

Solution (Python):


X, Y, A, B, C = map(int, input().split(" "))
ps = list(map(int, input().split(" ")))
qs = list(map(int, input().split(" ")))
rs = list(map(int, input().split(" ")))

ps = sorted(ps)
qs = sorted(qs)
rs = sorted(rs)

nx = 0
ny = 0
nc = 0
sums = 0
for i in range(X+Y):
    a = b = c = 0
    if nx < X:
        a = ps[A-1-nx]
    if ny < Y :
        b = qs[B-1-ny]
    if nc < C:
        c = rs[C-1-nc]
    
    if a >= b and a >= c:
        sums += a
        
        nx += 1
        
    if b > a and b >= c:
        sums += b
        
        ny += 1
        
    if c > a and c > b:
        sums += c
        
        nc += 1
        
print(sums)

Solution (Python):





















x, y, a, b, c = (int(x) for x in input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
p.sort()
q.sort()
r.sort()
i = a-x
j = b - y
k = c-1
sol = sum(p[-x:])+sum(q[-y:])
while k>= 0 and i <a and j < b:
    d1 = r[k] - p[i]
    d2 = r[k] - q[j]
    if d1 < 0 and d2 < 0:
        break
    if d1 > d2:
        sol += d1
        i += 1
        k-=1
    else:
        sol += d2
        j += 1
        k -= 1

while k>=0 and i < a:
    d1 = r[k] - p[i]
    if d1 < 0:
        break

    sol += d1
    i += 1
    k-=1

while k>=0 and j < a:
    d1 = r[k] - q[j]
    if d1 < 0:
        break

    sol += d1
    j += 1
    k-=1

print(sol)

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)

if x <= len(p):
    pp = p[:x]
else:
    pp = p + r[:x-len(p)]

if y <= len(q):
    qq = q[:y]
else:
    qq = q + r[:y-len(q)]
    
rr = []
for k in r:
    if len(pp) >= 1 and len(qq) >= 1:
        if pp[-1] < qq[-1]:
            if pp[-1] < k:
                pp.pop()
                rr.append(k)
            else:
                break
        else:
            if qq[-1] < k:
                qq.pop()
                rr.append(k)
            else:
                break
    elif len(pp) >= 1:
        if pp[-1] < k:
            pp.pop()
            rr.append(k)
        else:
            break
    elif len(qq) >= 1:
        if qq[-1] < k:
            qq.pop()
            rr.append(l)
        else:
            break
    else:
        break

print(sum(pp)+sum(qq)+sum(rr))

Solution (Python):
import heapq
 
X, Y, A, B, C = [int(i) for i in input().split()]
P = sorted([int(i) for i in input().split()])[-X:]
Q = sorted([int(i) for i in input().split()])[-Y:]
R = sorted([int(i) for i in input().split()])[::-1]
heapq.heapify(P)
heapq.heapify(Q)
i = 0
while(True):
    if P[0] >= R[i] and Q[0] >= R[i]:
        break
    if P[0] > Q[0]:
        if Q[0] < R[i]:
            heapq.heappop(Q)
            heapq.heappush(Q, R[i])
    else:
        if P[0] < R[i]:
            heapq.heappop(P)
            heapq.heappush(P, R[i])
    i += 1
    if i == len(R):
        break
print(sum(P) + sum(Q))

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)
s = p[0:X] + q[0:Y] + r

s.sort(reverse=True)
ans = 0

for i in range(X+Y):
    ans += s[i]
    
print(ans)

Solution (Python):
X, Y, A, B, C = map(int,input().split())
listp = list(map(int, input().split()))
listq = list(map(int, input().split()))
listr = list(map(int, input().split()))

listp = sorted(listp, reverse=True)
listq = sorted(listq, reverse=True)
listr = sorted(listr, reverse=True)
listp = listp[:X]
listq = listq[:Y]
listr = listr[:X+Y]
listp.insert(0, 1000000000000)
listq.insert(0, 1000000000000)


eat_noapple = []
oisisa = 0

for noapple in listr:
    if listp[-1] >= listq[-1] and listq[-1] < noapple:
        listq.pop(-1)
        eat_noapple.append(noapple)
    elif listp[-1] < listq[-1] and listp[-1] < noapple:
        listp.pop(-1)
        eat_noapple.append(noapple)
    else:
        break
listp.pop(0)
listq.pop(0)

try:
    for papple in listq:
        oisisa += papple
except:
    a = 0
try:
    for qapple in listp:
        oisisa += qapple
except:
    a = 0
try:
    for rapple in eat_noapple:
        oisisa += rapple
except:
    a = 0
print(oisisa)

Solution (Python):
import sys
x, y, a, b, c = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
p.sort(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)
ri = 0
pi = x
qi = y
while ri < len(r) and (0 < pi or 0 < qi):
    if 0 < pi and p[pi - 1] < r[ri] and (qi <= 0 or p[pi - 1] <= q[qi - 1]):
        ri += 1
        pi -= 1
    elif 0 < qi and q[qi - 1] < r[ri] and (pi <= 0 or q[qi - 1] <= p[pi - 1]):
        ri += 1
        qi -= 1
    else:
        break
print(sum(p[:pi]) + sum(q[:qi]) + sum(r[:ri]))