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:
#coding:utf-8
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


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

    print(sum_d)

    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#E

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

Submission status: Accepted

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

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

# all_apples = sorted(p + q + r, key=lambda x: -x)

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(candidates, eat_idx, wanna_eat, p_count, q_count, r_count)

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

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

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

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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))
    # print(X)
    # print(Y)
    spi = sp[:X]
    sqi = sq[:Y]
    # print(spi)
    # print(sqi)
    # print(sr)
    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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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 = 0
tX = X # Num of Ori. Red
tY = Y # Num of Ori. Green

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

temp = ans
#Update Section:
for i in range( min(X+Y, C) ): # i+1 = Num of Non. Color
  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:# tX == 0:
    temp += R[i] - P[tX-1]
    tX -= 1
  ans = max(ans, temp)
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
#print(datp)
#print(datq)
#print(datr)
#print("defaultt", res)

for i in range(c):
    #print(" musyoku ", i , "=", datr[i])
    #print("pr{0}, qr{1}".format(pr, qr))
    diffp, diffq = -1, -1
    if pr >= 0:
        diffp = datr[i] - datp[pr] # この無色を取ったときのメリット
    if qr >= 0:
        diffq = datr[i] - datq[qr] # この無色を取ったときのメリット
    #print("diffp", diffp, "diffq", diffq)
    if diffp <= 0 and diffq <= 0: # デメリットしか両者ないならいらない
        #print("break")
        break
    if diffp >= diffq and pr >= 0: # pを押し出したほうがいいなら
        #print("get p")
        res += diffp
        pr -= 1
    elif qr >= 0 and diffq > 0: #qを押し出したほうがいいなら
        #print("get q")
        res += diffq
        qr -= 1

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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__()

Submission status: Accepted

Solution code:


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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(r))

Submission status: Accepted

Solution code:
from collections import deque
X,Y,A,B,C = (int(i) for i in input().split())

P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
P_deque = deque(P)
Q_deque = deque(Q)
R_deque = deque(R)

red_counter = 0
green_counter = 0

ans = 0

for i in range(X+Y):
  if red_counter == X:
    P_deque = [-1]
  elif green_counter == Y:
    Q_deque = [-1]

  if len(R_deque) != 0:
    if P_deque[0] >= Q_deque[0] and P_deque[0] >= R_deque[0]:
      ans += P_deque.popleft()
      red_counter += 1
    elif Q_deque[0] >= P_deque[0] and Q_deque[0] >= R_deque[0]:
      ans += Q_deque.popleft()
      green_counter +=1
    else:
      ans += R_deque.popleft()
  else:
    if P_deque[0] >= Q_deque[0]:
      ans += P_deque.popleft()
      red_counter += 1
    elif Q_deque[0] >= P_deque[0]:
      ans += Q_deque.popleft()
      green_counter +=1

print(ans)

Submission status: Accepted

Solution code:
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, reverse=True)
Q = sorted(Q, reverse=True)
R = sorted(R, reverse=True)
P = P[:X]
Q = Q[:Y]
PQ = sorted(P + Q)
ans = 0
cnt = 0
if X + Y > C:
    ans += sum(PQ[C:])
    cnt += X + Y - C
    PQ = PQ[:C]
PQR = PQ + R
PQR = sorted(PQR, reverse=True)
ans += sum(PQR[:X + Y - cnt])
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)
p = p[:X]
q.sort(reverse=True)
q = q[:Y]
r.sort(reverse=True)
ans = p + q
ans.sort()
i = 0
for rr in r:
    if ans[i] < rr:
        ans[i] = rr
        i += 1
    else:
        break

print(sum(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)
p_x = p[:x]
q_y = q[:y]
new_lis = p_x + q_y + r
new_lis = sorted(new_lis, reverse=True)
tasty = 0
for i in range(x+y):
    tasty += new_lis[i]
print(tasty)