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



Sample input:
1 2 2 2 1
2 4
5 1
3


Sample output:
12

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
la=input().split()
lb=input().split()
lc=input().split()
A=[]
B=[]
C=[]
for i in range(len(la)):
    A.append(int(la[i]))
for i in range(len(lb)):
    B.append(int(lb[i]))
for i in range(len(lc)):
    C.append(int(lc[i]))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
A=A[:x]
B=B[:y]
C=C[:c]
L=A+B+C
L.sort(reverse=True)
s=0
for i in range(x+y):
    s+=L[i]
print(s)

Submission status: Accepted

Solution code:
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()
ans=0

z=x+y

while(z!=0):
    while((x!=0)and(y!=0)):
            a=[]
            a.append(p[-1])
            a.append(q[-1])
            a.append(r[-1])
            if max(a)==p[-1]:
                x-=1
                z-=1
                ans+=p[-1]
                p.pop()
            elif max(a)==q[-1]:
                y-=1
                z-=1
                ans+=q[-1]
                q.pop()
            else:
                z-=1
                ans+=r[-1]
                r.pop()
            if z==0:
                break
            if  len(r)==0:
                break
    if len(r)==0:
        break
    if z==0:
        break
        
    if x==0:
        if q[-1]>=r[-1]:
            y-=1
            z-=1
            ans+=q[-1]
            q.pop() 
        else:
            z-=1
            ans+=r[-1]
            r.pop()
            
            
    elif y==0:
        if p[-1]>=r[-1]:
            x-=1
            z-=1
            ans+=p[-1]
            p.pop()
        else:
            z-=1
            ans+=r[-1]
            r.pop()

            
            
if len(r)==0:
    while(z!=0):
        while((x!=0)and(y!=0)):
            a=[]
            a.append(p[-1])
            a.append(q[-1])
            if max(a)==p[-1]:
                x-=1
                z-=1
                ans+=p[-1]
                p.pop()
            elif max(a)==q[-1]:
                y-=1
                z-=1
                ans+=q[-1]
                q.pop()
            if z==0:
                break
        if z==0:
            break
        if x==0:
            y-=1
            z-=1
            ans+=q[-1]
            q.pop() 
        else: 
            x-=1
            z-=1
            ans+=p[-1]
            p.pop()
        
        
        
        
        
        
        
        
        
        
        
        
        
print(ans)

Submission status: Accepted

Solution code:
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)
PQ = P[:X] + Q[:Y]
heapq.heapify(PQ)
i = 0
for r in R:
    if PQ[0] < r:
        heapq.heappop(PQ)
        heapq.heappush(PQ, r)
        i += 1
        if i == (X + Y):
            break
print(sum(PQ))

Submission status: Accepted

Solution code:
def main():
    X, Y, A, B, C = [int(i) for i in input().split()]
    red = [int(i) for i in input().split()]
    blue = [int(i) for i in input().split()]
    noc = [int(i) for i in input().split()]
    red = sorted(red)[-X:]
    blue = sorted(blue)[-Y:]
    rb = sorted(red + blue)
    score = sum(rb)
    noc = sorted(noc)[::-1]

    s_noc = 0
    s_rb = 0
    for i in range(min(len(rb), len(noc))):
        if rb[i] > noc[i]:
            break
        s_noc += noc[i]
        s_rb += rb[i]

    print(score + s_noc - s_rb)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

def sort(L):
  if len(L)==1:
    return L
  left = sort(L[:len(L)//2])
  right = sort(L[len(L)//2:])
  result = []
  left.append(-1)
  right.append(-1)
  left_index = 0
  right_index = 0
  while len(result)<len(L):
    if left[left_index]>=right[right_index]:
      result.append(left[left_index])
      left_index += 1
    else:
      result.append(right[right_index])
      right_index += 1
  return result

P = sort(P)[:X]
Q = sort(Q)[:Y]
R = sort(R)

P_index = X-1
Q_index = Y-1
R_index = 0

while True:
  if P[P_index] <= Q[Q_index] and R[R_index]>P[P_index]:
    P[P_index] = R[R_index]
    P_index = max(0, P_index-1)
    R_index += 1
  elif P[P_index] > Q[Q_index] and R[R_index]>Q[Q_index]:
    Q[Q_index] = R[R_index]
    Q_index = max(0, Q_index-1)
    R_index += 1
  else:
    break
  if R_index>=len(R):
    break

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

num_red = 0
num_green = 0


def top_pop(*lst):
    # print(lst)
    if len(lst[-1]) == 0:
        lst = lst[:-1]
    top = max(map(lambda x: x[-1], lst))
    for i in range(len(lst)):
        if lst[i][-1] == top:
            return (lst[i].pop(), i)

def func(i):
    global num_red, num_green
    if i == 0:
        num_red += 1
    elif i == 1:
        num_green += 1
    else:
        pass

ans = 0
for i in range(x+y):
    if num_red < x and num_green < y:
        popped, i = top_pop(p, q, r)
    elif num_red < x:
        popped, i = top_pop(p, r)
    elif num_green < y:
        popped, i = top_pop(q, r)
    else:
        raise RuntimeError
    func(i)
    ans += popped

print(ans)

Submission status: Accepted

Solution code:
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)
red = x-1
gre = y-1
mu = 0
while True:
    if mu == c:
        break
    else:
        if red >= 0:
            badr = p_[red]
        else:
            badr = 1000000000
        if gre >= 0:
            badg = q_[gre]
        else:
            badg = 1000000000
        badmu = r_[mu]
        if badr <= badg:
            if badr < badmu:
                red = red-1
                mu = mu+1
            else:
                break
        else:
            if badg < badmu:
                gre = gre-1
                mu = mu+1
            else:
                break
ans = 0
for i in range(red+1):
    ans += p_[i]
for i in range(gre+1):
    ans += q_[i]
for i in range(mu):
    ans += r_[i]
print(ans)

Submission status: Accepted

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

bag = []
for i in range(X):
    bag.append(p[i])
for j in range(Y):
    bag.append(q[j])

bag = sorted(bag)
for i in range(len(bag)):
    if len(r) == 0:
        break
    if bag[i] < r[0]:
        bag[i] = r.pop(0)
        i += 1
    else:
        break

print(sum(bag))

Submission status: Accepted

Solution code:
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 = sum(P[:X])+sum(Q[:Y])
pt_x,pt_y = X-1,Y-1
for r in range(len(R)):
    if pt_x >= 0 and pt_y >= 0:
        if P[pt_x] < Q[pt_y]:
            if P[pt_x] < R[r]:
                ans = ans + R[r] - P[pt_x]
                pt_x -= 1
        else:
            if Q[pt_y] < R[r]:
                ans = ans + R[r] - Q[pt_y]
                pt_y -= 1
    elif pt_x >= 0:
        if P[pt_x] < R[r]:
            ans = ans + R[r] - P[pt_x]
            pt_x -= 1
        else:
            break
    elif pt_y >= 0:
        if Q[pt_y] < R[r]:
            ans = ans + R[r] - Q[pt_y]
            pt_y -= 1
        else:
            break
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
R=list(map(int,input().split()))

P=sorted(P,reverse=True)
Q=sorted(Q,reverse=True)
R=sorted(R,reverse=True)
ans=sum(P[:X])+sum(Q[:Y])
pi=X-1
qi=Y-1
ri=0
for i in range(min(C,X+Y)):
	if pi==-1:
		temp=ans-Q[qi]
		qi-=1
	elif qi==-1:
		temp=ans-P[pi]
		pi-=1
	elif P[pi]<=Q[qi]:
		temp=ans-P[pi]
		pi-=1
	else:
		temp=ans-Q[qi]
		qi-=1

	if ans<temp+R[i]:
		ans=temp+R[i]
	else:
		break

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
listD=[0]*(A+B+C)
listA=list(map(int,input().split()))
listB=list(map(int,input().split()))
listC=list(map(int,input().split()))

listA.sort(reverse=True)
listB.sort(reverse=True)
listC.sort(reverse=True)

for i in range(X):
    listD[i]=listA[i]
for i in range(Y):
    listD[A+i]=listB[i]
for i in range(C):
    listD[A+B+i]=listC[i]

listD.sort(reverse=True)

list_answer=[0]*(X+Y)

for i in range(X+Y):
    list_answer[i]=listD[i]

sum1=sum(list_answer)


print(sum1)

Submission status: Accepted

Solution code:
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)
if len(r) > x + y:
  r = r[:x+y]

ans = sum(p) + sum(q)
cp = x - 1
cq = y - 1
for tmp in r:
  if cp >= 0 and cq >= 0:
    if p[cp] > q[cq]:
      if tmp > q[cq]:
        ans += tmp - q[cq]
        cq -= 1
      else:
        break
    else:
      if tmp > p[cp]:
        ans += tmp - p[cp]
        cp -= 1
      else:
        break
  elif cp < 0:
    if tmp > q[cq]:
      ans += tmp - q[cq]
      cq -= 1
    else:
      break
  elif cq < 0:
    if tmp > p[cp]:
      ans += tmp - p[cp]
      cp -= 1
    else:
      break
      
  else:
    break
print(ans)

Submission status: Accepted

Solution code:
#!python3.4
# -*- coding: utf-8 -*-
# abc160/abc160_e
import sys
from collections import deque


s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    X, Y, A, B, C = i2nn()
    p = deque(sorted(map(int, i2s().split(' ')), reverse=True)[:X])
    q = deque(sorted(map(int, i2s().split(' ')), reverse=True)[:Y])
    r = deque(sorted(map(int, i2s().split(' '))))
    s = 0
    while p or q:
        if p and q:
            t = p if p[-1] < q[-1] else q
        else:
            t = p if p else q
        if r and t[-1] < r[-1]:
            t.pop()
            s += r.pop()
        else:
            s += t.pop()
    print(s)
    return

main()

Submission status: Accepted

Solution code:
import bisect
X,Y,A,B,C=map(int,input().split())

AList=list(map(int,input().split()))
BList=list(map(int,input().split()))
CList=list(map(int,input().split()))

AList.sort(reverse=True)
BList.sort(reverse=True)
CList.sort(reverse=True)

alist = AList[0:X] + BList[0:Y]
clist = CList
alist.sort()
ans = sum(alist)
cnt = 0

for i in range(C):
  a = clist[i]
  b = bisect.bisect_left(alist,a)
  if i < b:
    ans += a
    cnt += 1



aa = sum(alist[:cnt])

print(int(ans -aa))

Submission status: Accepted

Solution code:
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()))
res = 0

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

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

p_sum = sum(p)
q_sum = sum(q)

p_num = -1
q_num = -1

for i in r:
    if p_num >= (len(p) * -1):
        p_temp = (p_sum - p[p_num] + i)
    else:
        p_temp = -1
    if q_num >= (len(q) * -1):
        q_temp = (q_sum - q[q_num] + i)
    else:
        q_temp = -1

    if p_temp >= p_sum or q_temp >= q_sum:        
        if p_temp - p_sum > q_temp - q_sum:
            p_sum = p_temp
            p_num -= 1        
        else:
            q_sum = q_temp
            q_num -= 1        
    else:
        break
    




print(p_sum + q_sum)

Submission status: Accepted

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

S = P + Q + R
S = sorted(S)

a = 0
for i in range(A):
  if P[i] >= S[A+B+C-(X+Y)]:
    a += 1

b = 0
for i in range(B):
  if Q[i] >= S[A+B+C-(X+Y)]:
    b += 1

if a <= X and b <= Y:
  c = 0
  for i in range(A+B+C-(X+Y),A+B+C,1):
    c += S[i]
  print(c)
elif a > X:
  c = 0
  P = sorted(P)
  for i in range(A-X,A,1):
    c += P[i]
  T = Q + R
  T = sorted(T)
  for i in range(B+C-Y,B+C,1):
    c += T[i]
  print(c)
else:
  c = 0
  Q = sorted(Q)
  for i in range(B-Y,B,1):
    c += Q[i]
  U = P + R
  U = sorted(U)
  for i in range(A+C-X,A+C,1):
    c += U[i]
  print(c)

Submission status: Accepted

Solution code:
from collections import deque
x, y, a, b, c = list(map(int, input().split()))
pl = list(map(int, input().split()))
ql = list(map(int, input().split()))
rl = list(map(int, input().split()))
pl.sort(reverse=True)
ql.sort(reverse=True)
rl.sort(reverse=True)


ans = 0
if x > a:
    ans += sum(pl)
    dif_x = x-a
    ans += rl[:dif_x]
    del rl[:dif_x]
    if y > b:
        ans += sum(ql)
        dif_y = y-b
        ans += sum(rl[:dif_y])
    else:
        qd = deque(ql)
        rd = deque(rl)
        for i in range(y):
            if qd and rd:
                if qd[0] > rd[0]:
                    ans += qd.popleft()
                else:
                    ans += rd.popleft()
            else:
                if qd:
                    ans += qd.popleft()
                else:
                    ans += rd.popleft()
else:
    if y > b:
        ans += sum(ql)
        dif_y = y-b
        ans += rl[:dif_y]
        del rl[:dif_y]
        pd = deque(pl)
        rd = deque(rl)
        for i in range(x):
            if pd and rd:
                if pd[0] > rd[0]:
                    ans += pd.popleft()
                else:
                    ans += rd.popleft()
            else:
                if pd:
                    ans += pd.popleft()
                else:
                    ans += rd.popleft()
    else:
        pd = deque(pl)
        qd = deque(ql)
        rd = deque(rl)
        c_x = 0
        c_y = 0
        for i in range(x+y):
            if pd and qd and rd:
                if (pd[0] == max([pd[0], qd[0], rd[0]]) and c_x < x) or (not c_y < y and pd[0] >= rd[0]):
                    ans += pd.popleft()
                    c_x += 1
                elif (qd[0] == max([pd[0], qd[0], rd[0]]) and c_y < y) or (not c_x < x and qd[0] >= rd[0]):
                    ans += qd.popleft()
                    c_y += 1
                else:
                    ans += rd.popleft()
            else:
                target = [-1, -1, -1]
                if pd:
                    target[0] = pd[0]
                if qd:
                    target[1] = qd[0]
                if rd:
                    target[2] = rd[0]
                t_max = max(target)
                ans += t_max
                idx = target.index(t_max)
                if idx == 0:
                    pd.popleft()
                elif idx == 1:
                    qd.popleft()
                else:
                    rd.popleft()

print(ans)

Submission status: Accepted

Solution code:
from heapq import heapify, heappush, heappop


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

red = list(map(int, input().split()))
green = list(map(int, input().split()))
colorless = list(map(int, input().split()))

red.sort(reverse=True)
green.sort(reverse=True)
colorless.sort(reverse=True)

red = red[:X]
green = green[:Y]
colorless = colorless[:(X + Y)]

heapify(red)
heapify(green)

for m in colorless:
  if m < red[0] and m < green[0]:
    break
  
  if red[0] < green[0]:
    heappop(red)
    heappush(red, m)
  else:
    heappop(green)
    heappush(green, m)
    
print(sum(red) + sum(green))

Submission status: Accepted

Solution code:
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)
p=p[:x]
q=q[:y]
heapq.heapify(p)
heapq.heapify(q)
for i in range(c):
  pp=heapq.heappop(p)
  pq=heapq.heappop(q)
  if pp<r[i] and pq>=r[i]:
    heapq.heappush(p,r[i])
    heapq.heappush(q,pq)
  elif pp>=r[i] and pq<r[i]:
    heapq.heappush(q,r[i])
    heapq.heappush(p,pp)
  elif pp<r[i] and pq<r[i]:
    if r[i]-pp>=r[i]-pq:
      heapq.heappush(p,r[i])
      heapq.heappush(q,pq)
    else:
      heapq.heappush(q,r[i])
      heapq.heappush(p,pp)
  else:
    heapq.heappush(q,pq)
    heapq.heappush(p,pp)
print(sum(p)+sum(q))

Submission status: Accepted

Solution code:
from collections import defaultdict

############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
X,Y,a,b,c = invr()
red = inlt() ; red.sort(reverse = True)
green = inlt(); green.sort(reverse = True)
colorless = inlt(); colorless.sort(reverse = True)

red = red[:X]
green = green[:Y]

red.reverse()
green.reverse()

r_index = 0
g_index = 0
c_index = 0
while True:
    if r_index >= len(red) or g_index >= len(green) or c_index >= len(colorless):
        break
    if red[r_index] > green[g_index]:
        if colorless[c_index] > green[g_index]:
            green[g_index] = colorless[c_index]
            g_index += 1
            c_index += 1
        else:
            break
    else:
        if colorless[c_index] > red[r_index]:
            red[r_index] = colorless[c_index]
            r_index += 1
            c_index += 1
        else:
            break

if r_index < len(red):
    if c_index < len(colorless):
        while r_index < len(red) and c_index < len(colorless):
            if red[r_index] < colorless[c_index]:
                red[r_index] = colorless[c_index]
                r_index+= 1
                c_index += 1
            else:
                break

if g_index < len(green):
    if c_index < len(colorless):
        while g_index < len(green) and c_index < len(colorless):
            if green[g_index] < colorless[c_index]:
                green[g_index] = colorless[c_index]
                g_index+= 1
                c_index += 1
            else:
                break

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

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
P = list(sorted(map(int, input().split()), reverse=True))
Q = list(sorted(map(int, input().split()), reverse=True))
R = list(sorted(map(int, input().split()), reverse=True))

S = P[:X] + Q[:Y]
S.sort(reverse=True)
S_wa = []
wa = 0
for s in S[::-1]:
    wa += s
    S_wa.append(wa)
big = S_wa[-1]
ans = S_wa[-1]

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

for i in range(min(X + Y, C)):
    ans = max(ans, big + R_wa[i] - S_wa[i])
print(ans)

Submission status: Accepted

Solution code:
import heapq
from collections import deque

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

r.sort()

if A < X:
  p += r[-(X - A):]
  r = r[:-(X - A)]
  A = X

if B < Y:
  q += r[-(Y - B):]
  r = r[:-(Y - B)]
  B = Y

p.sort()
q.sort()

pq = deque(heapq.merge(p[A - X:], q[B - Y:]))

while r and pq[0] < r[-1]:
  pq.popleft()
  pq.append(r.pop())

print(sum(pq))

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
red = sorted(list(map(int, input().split())), reverse=True)
green = sorted(list(map(int, input().split())), reverse=True)
white = sorted(list(map(int, input().split())), reverse=True)
red_index = x-1
green_index = y-1
white_index = 0

while white_index < c:
  if red[red_index] < green[green_index]:
    if red[red_index] < white[white_index]:
      red[red_index] = white[white_index]
      red_index = max(red_index-1, 0)
      white_index += 1
    else:
      break
  else:
    if green[green_index] < white[white_index]:
      green[green_index] = white[white_index]
      green_index = max(green_index-1, 0)
      white_index += 1  
    else:
      break
print(sum(red[0:x])+sum(green[0:y]))

Submission status: Accepted

Solution code:
import heapq as hq
X,Y,A,B,C = map(int,input().split())
heap = []
for p in map(int,input().split()):
    hq.heappush(heap,(-p, 0))
for p in map(int,input().split()):
    hq.heappush(heap,(-p, 1))
for p in map(int,input().split()):
    hq.heappush(heap,(-p, 2))
xs = 0
ys = 0
zs = 0
ans = 0
while xs+ys+zs < X+Y:
    p, i = hq.heappop(heap)
    p = -p
    if i == 0 and xs < X:
        xs += 1
        ans += p
    elif i == 1 and ys < Y:
        ys += 1
        ans += p
    elif i == 2:
        zs += 1
        ans += p
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
x,y,a,b,c = map(int,input().split())
p1 = list(map(int,input().split()))
q1 = list(map(int,input().split()))
r1 = list(map(int,input().split()))
ans = []
p1.sort(reverse=1)
q1.sort(reverse=1)
r1.sort(reverse=1)


p=deque(p1[0:x])
q=deque(q1[0:y])
r=deque(r1[0:x+y])

lp = p.pop()
lq = q.pop()

rr = r.popleft()

while 1:
#     print(lp,lq,rr)
    if (x > 0 and lp <= lq) or y == 0:
        if lp >= rr:
            ans.append(lp)
            
        else:
            ans.append(rr)
            r.append(0)
            rr = r.popleft()
        if x > 1:
                lp = p.pop()
        x -= 1
    else:
        if lq >= rr:
            ans.append(lq)
            
        else:
            ans.append(rr)
            r.append(0)
            rr = r.popleft()
        if y > 1:
                lq = q.pop()
        y -= 1
    if x == 0 and y == 0:
        print(sum(ans))
        exit()

Submission status: Accepted

Solution code:
X, Y, A, B, C = [int(x) for x in input().split()]
p_l = sorted([int(x) for x in input().split()], reverse=True)
q_l = sorted([int(x) for x in input().split()], reverse=True)
r_l = sorted([int(x) for x in input().split()], reverse=True)
L = p_l[:X] + q_l[:Y]
L = sorted(L)
for i in range(min(X+Y, C)):
  if L[i] < r_l[i]:
    L[i] = r_l[i]
  else:
    break
print(sum(L))

Submission status: Accepted

Solution code:
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.sort()
r=r[:x+y]# able add
lenR=len(r)

temp=p[:lenR]# 白なしの小さいものたち
temp.extend(r)#白と前の小さい者たちの合計
temp.sort(reverse=True)
temp=temp[:lenR]#白ありでのMAX

print(sum(p[lenR:])+sum(temp))

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
l=[]
#print(p,q,r)
delicious=0
for i in range(X):
    l.append(p[i])
for i in range(Y):
    l.append(q[i])
l.sort(reverse=True)
#print(l)
for i in range(len(l)-C):
    #print(l[i])
    delicious=delicious+l[i]
lidx=max(0,len(l)-C)
#print(l,r,lidx)
cidx=0
#print(delicious,lidx,cidx,l,r,l[lidx],r[cidx])
for j in range(min(C,X+Y)):
    if (l[lidx]<=r[cidx])or(lidx>len(l)):
        delicious=delicious+r[cidx]
        cidx=cidx+1
    else:
        delicious = delicious + l[lidx]
        lidx=lidx+1
    #print(delicious)
print(delicious)

Submission status: Accepted

Solution code:
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.append(0)
q.append(0)
r += [0] * (10 ** 3)


wa = x + y

lsp = x - 1
lsq = y - 1

pi = 0
qi = 0
ri = 0

ans = 0

for i in range(wa):
    if x > 0 and p[pi] == max(p[pi], q[qi], r[ri]):
        ans += p[pi]
        pi += 1
        x -= 1
    elif y > 0 and q[qi] == max(p[pi], q[qi], r[ri]):
        ans += q[qi]
        qi += 1
        y -= 1
    elif r[ri] == max(p[pi], q[qi], r[ri]):
        ans += r[ri]
        ri += 1
        if y > 0 and p[lsp] >= q[lsq]:
            y -= 1
            if lsq == 0:
                continue
            else:
                lsq -= 1
        elif x > 0 and p[lsp] < q[lsq]:
            x -= 1
            if lsp == 0:
                continue
            else:
                lsp -= 1

print(ans)

Submission status: Accepted

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

x, y = x-1, y-1
z = 0
red, green = 0, 0
ans = 0
while True:
    finish = True
    if p[x] <= q[y] or y < 0:
        if z < c and p[x] < r[z]:
            x -= 1
            z += 1
            finish = False
    elif p[x] >= q[y] or x < 0:
        if z < c and q[y] < r[z]:
            y -= 1
            z += 1
            finish = False
    if finish:
        break
print(sum(p[:x+1]) + sum(q[:y+1]) + sum(r[:z]))

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p_list = sorted(list(map(int, input().split())))
q_list = sorted(list(map(int, input().split())))
r_list = sorted(list(map(int, input().split())))

p_list = p_list[a - x:]
q_list = q_list[b - y:]

pqr_list = p_list + q_list + r_list
pqr_list = sorted(pqr_list,reverse=True)
pqr_list = pqr_list[:x + y]

print(sum(pqr_list))

Submission status: Accepted

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

p = deque(p[-X:])
q = deque(q[-Y:])
r = deque(r)

while min(p[0], q[0]) < r[-1]:
    x = r.pop()
    if p[0] < q[0]:
        p.popleft()
        p.append(x)
    else:
        q.popleft()
        q.append(x)
    if len(r) == 0:
        break

cnt = 0
for x in p:
    cnt += x
for y in q:
    cnt += y

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

a,b,X,Y,Z = inpl()
x = sorted(inpl())
y = sorted(inpl())
c = sorted(inpl())
c = c[::-1]
res = sum(x[X-a:]) + sum(y[Y-b:])
x += [INF]; y += [INF]
i = X-a; j = Y-b
ind = 0
iok = True; jok = True
while ind < Z:
    if x[i] <= y[j]:
        if x[i] < c[ind]:
            res += c[ind] - x[i]
            ind += 1
            i += 1
        else:
            break
    else:
        if y[j] < c[ind]:
            res += c[ind] - y[j]
            ind += 1
            j += 1
        else:
            break
    # print(i,j,ind)
print(res)

Submission status: Accepted

Solution code:
#E
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)

rp = []
rq = []
rr = []

for i in range(A):
    if i == 0:
        rp.append(P[0])
    else:
        rp.append(rp[-1]+P[i])
for i in range(B):
    if i == 0:
        rq.append(Q[0])
    else:
        rq.append(rq[-1]+Q[i])
for i in range(C):
    if i == 0:
        rr.append(R[0])
    else:
        rr.append(rr[-1]+R[i])
    
p = 0
q = 0
r = 0
ans = 0
inf = float("inf")
for _ in range(X+Y):
    np,nq,nr = 0,0,0
    if p < A and p < X:
        np = P[p]
    if q < B and q < Y:
        nq = Q[q]
    if r < C:
        nr = R[r]
        
    if np == max(np,nq,nr):
        ans+=np
        p+=1
    elif nq == max(np,nq,nr):
        ans+=nq
        q+=1
    elif nr == max(np,nq,nr):
        ans+=nr
        r+=1
    
print(ans)

Submission status: Accepted

Solution code:
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 = sum(P) + sum(Q)
idxp = -1
idxq = -1

for ri in range(C):
    r = R[ri]
    if idxp >= -X and idxq >= -Y:
        if P[idxp] <= Q[idxq] and P[idxp] < R[ri]:
            ans += R[ri] - P[idxp]
            idxp -= 1
        elif Q[idxq] <= P[idxp] and Q[idxq] < R[ri]:
            ans += R[ri] - Q[idxq]
            idxq -= 1
    elif idxp >= -X:
        if P[idxp] < R[ri]:
            ans += R[ri] - P[idxp]
            idxp -= 1
    elif idxq >= -Y:
        if Q[idxq] < R[ri]:
            ans += R[ri] - Q[idxq]
            idxq -= 1

print(ans)

Submission status: Accepted

Solution code:
def inputs(): return [int(x) for x in input().split()]
from collections import defaultdict
import heapq
x,y,a,b,c = inputs()
p = inputs()
p.sort()
p = p[::-1]
p = p[:x]
heapq.heapify(p)
q = inputs()
q.sort()
q = q[::-1]
q=q[:y]
heapq.heapify(q)
r = inputs()
r.sort()
r=r[::-1]
ans = 0
for i in r:
    red = heapq.heappop(p)
    green = heapq.heappop(q)
    #print(red,green,i)
    if red<i and green<i:
        if red<green:
            heapq.heappush(p, i)
            heapq.heappush(q, green)
        else:
            heapq.heappush(q, i)
            heapq.heappush(p, red)
    elif red<i:
        heapq.heappush(p, i)
        heapq.heappush(q, green)
    elif green<i:
        heapq.heappush(q, i)
        heapq.heappush(p, red)
    else:
        heapq.heappush(q, green)
        heapq.heappush(p, red)
        break
print(sum(p)+sum(q))