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:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, Y, A, B, C = MAP()
p = LIST()
q = LIST()
r = LIST()
r = [-x for x in r]

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

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

while r:
	if -r[0] < p_tmp[0] and -r[0] < q_tmp[0]:
		break
	p_min = heappop(p_tmp)
	q_min = heappop(q_tmp)
	r_max = heappop(r)
	
	if p_min <= q_min:  # pに入れる
		heappush(p_tmp, -r_max)
		heappush(q_tmp, q_min)
	else:  # qに入れる
		heappush(q_tmp, -r_max)
		heappush(p_tmp, p_min)
print(sum(p_tmp+q_tmp))

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int,input().split())
l = list(map(int,input().split()))
m = list(map(int,input().split()))
n = list(map(int,input().split()))

l = (sorted(l, reverse=True))[:X]
m = (sorted(m, reverse=True))[:Y]
print(sum((sorted(l+m+n, reverse=True))[:X+Y]))

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
p=sorted(map(int,input().split()),reverse=True)[:X]
q=sorted(map(int,input().split()),reverse=True)[:Y]
r=sorted(map(int,input().split()),reverse=True)

pq=sorted(p+q+r,reverse=True)
print(sum(pq[:X+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()
p.reverse()
q.sort()
q.reverse()
r.append(0)
r.sort()
r.reverse()
ct=0
P=p[0:x]
Q=q[0:y]
L=P+Q
L.sort()
R=0
for i in range(len(L)):
  if r[R]>L[i]:
    L[i]=r[R]
    if R<c:
      R+=1
print(sum(L))

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    X, Y, A, B, C = map(int, input().split())
    P = [int(p) for p in input().split()]
    Q = [int(q) for q in input().split()]
    R = [int(r) for r in input().split()]
    P.sort(reverse = True)
    Q.sort(reverse = True)
    R.sort(reverse = True)
    pi, qi = 0, 0
    sumPQ = [0] * (X + Y + 1)
    for i in range(X + Y):
        #print(pi, qi)
        if pi == min(A, X):
            sumPQ[i + 1] = sumPQ[i] + Q[qi]
            qi += 1
        elif qi == min(B, Y):
            sumPQ[i + 1] = sumPQ[i] + P[pi]
            pi += 1
        elif P[pi] >= Q[qi]:
            sumPQ[i + 1] = sumPQ[i] + P[pi]
            pi += 1
        else:
            sumPQ[i + 1] = sumPQ[i] + Q[qi]
            qi += 1
    Ans = sumPQ[X + Y]
    rVal = 0
    for i in range(min(C, X + Y)):
        rVal += R[i]
        Ans = max(Ans, rVal + sumPQ[X + Y - i - 1])
    print(Ans)
    #print(sumPQ)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
from heapq import heappop,heappush,heapify
P=[]
for p in map(int,input().split()):
    heappush(P,-p)
Q=[]
for q in map(int,input().split()):
    heappush(Q,-q)
R=[]
for r in map(int,input().split()):
    heappush(R,-r)
x,y,z,a=0,0,0,0
while x<X and y<Y and x+y+z<X+Y:
    p,q,r=-P[0],-Q[0],-R[0]
    m=max(p,q,r)
    if m==p:
        x+=1
        a+=p
        if len(P)>1:
            heappop(P)
        else:
            P[0]=0
    elif m==q:
        y+=1
        a+=q
        if len(Q)>1:
            heappop(Q)
        else:
            Q[0]=0
    else:
        z+=1
        a+=r
        if len(R)>1:
            heappop(R)
        else:
            R[0]=0
while x<X and x+y+z<X+Y:
    p,r=-P[0],-R[0]
    m=max(p,r)
    if m==p:
        x+=1
        a+=p
        if len(P)>1:
            heappop(P)
        else:
            P[0]=0
    else:
        z+=1
        a+=r
        if len(R)>1:
            heappop(R)
        else:
            R[0]=0
while y<Y and x+y+z<X+Y:
    q,r=-Q[0],-R[0]
    m=max(q,r)
    if m==q:
        y+=1
        a+=q
        if len(Q)>1:
            heappop(Q)
        else:
            Q[0]=0
    else:
        z+=1
        a+=r
        if len(R)>1:
            heappop(R)
        else:
            R[0]=0
while x+y+z<X+Y:
    r=-R[0]
    z+=1
    a+=r
    if len(R)>1:
        heappop(R)
    else:
        R[0]=0
print(a)

Submission status: Accepted

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

hq = P[:X] + Q[:Y]
heapq.heapify(hq)

for r in R:
    heapq.heappushpop(hq, r)

print(sum(hq))

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()))
a = []
for x in p:
    a.append((x,0))
for x in q:
    a.append((x,1))
for x in r:
    a.append((x,2))
a.sort(reverse=True)
xc = 0
yc = 0
cnt = 0
ans = 0
for x in a:
    if cnt == X+Y:
        break
    if x[1] == 0:
        if xc < X:
            ans += x[0]
            xc += 1
            cnt += 1
    elif x[1] == 1:
        if yc < Y:
            ans += x[0]
            yc += 1
            cnt += 1
    else:
        ans += x[0]
        cnt += 1
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)

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

s = p1 + q1 + r
s.sort(reverse= True)

print(sum(s[:x+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 = sorted(p, reverse=True)
q = sorted(q, reverse=True)
r = sorted(r, reverse=True)

picked = sorted(p[:x] + q[:y])

ans = 0
r_i = 0
for i in range(len(picked)):
    if picked[i] < r[r_i]:
        ans += r[r_i]
        r_i += 1
        if len(r) == r_i:
            break
    else:
        break
ans += sum(picked[r_i:])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    x, y, a, b, c = LI()
    p = LI()
    A = []
    for pi in p:
        if len(A) < x:
            heappush(A, pi)
        else:
            aa = heappop(A)
            if aa > pi: pi = aa
            heappush(A, pi)
    q = LI()
    B = []
    for qi in q:
        if len(B) < y:
            heappush(B, qi)
        else:
            aa = heappop(B)
            if aa > qi: qi = aa
            heappush(B, qi)
    r = LI()
    r.sort()
    for ri in r:
        pi = heappop(A)
        qi = heappop(B)
        if pi > qi:
            if ri > qi:
                qi = ri
        else:
            if ri > pi:
                pi = ri
        heappush(A, pi)
        heappush(B, qi)
    print(sum(A) + sum(B))
    

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

    ans = sum(p[:x])+sum(q[:y])
    p.insert(0,float("INF"))
    q.insert(0,float("INF"))
    x1 = x
    y1 = y
    for i in r:
        if i > p[x1] and i > q[y1]:
            if p[x1] > q[y1]:
                ans += i-q[y1]
                y1 -= 1
            else:
                ans += i-p[x1]
                x1 -= 1
        elif i > p[x1]:
            ans += i-p[x1]
            x1 -= 1
        elif i > q[y1]:
            ans += i-q[y1]
            y1 -= 1
        else:
            print(ans)
            exit()

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

Submission status: Accepted

Solution code:
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)
for i in range(x):
    clist.append(alist[i])
for i in range(y):
    clist.append(blist[i])
clist.sort(reverse = True)
sum = 0
for i in range(x+y):
    sum += clist[i]
print(sum,end='\n')

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

Submission status: Accepted

Solution code:
from heapq import *

X, Y, A, B, C = list(map(int, input().split()))
PQR = [list(map(int, input().split())) for _ in range(3)]

def to_heap(l):
    l = [(-x, x) for x in l]
    heapify(l)
    return l

apples = [to_heap(l + [-1]) for l in PQR]
apple_count = [0, 0, 0]
sum_num = 0
while sum(apple_count) < (X + Y):
    if apple_count[0] == X:
        apples[0] = [(1, -1)]
    if apple_count[1] == Y:
        apples[1] = [(1, -1)]
    max_apple_index = max(range(3), key=lambda x: apples[x][0][1])
    sum_num += apples[max_apple_index][0][1]
    heappop(apples[max_apple_index])
    apple_count[max_apple_index] += 1

print(sum_num)

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)

for i in range(1,A):
    p[i]+=p[i-1]
for i in range(1,B):
    q[i]+=q[i-1]
for i in range(1,C):
    r[i]+=r[i-1]
p.append(0)
q.append(0)

num1=X-1
num2=Y-1
ans=p[num1]+q[num2]

for i in range(min(X+Y,C)):
    if num1<0:
        num2-=1
        ans=max(ans,p[num1]+q[num2]+r[i])
    else:
        if num2<0:
            num1-=1
            ans=max(ans,p[num1]+q[num2]+r[i])
        else:
            if p[num1]-p[num1-1]<=q[num2]-q[num2-1]:
                num1-=1
                ans=max(ans,p[num1]+q[num2]+r[i])
            else:
                num2-=1
                ans=max(ans,p[num1]+q[num2]+r[i])
            
print(ans)

Submission status: Accepted

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

R.sort()
R = deque(R)

P.sort(reverse=True)
Q.sort(reverse=True)
P = P[:X]
Q = Q[:Y]

S = [P, Q]

A = [(0, i, p) for i, p in enumerate(P)] + [(1, i, q) for i, q in enumerate(Q)]
A.sort(reverse=True, key=lambda a: a[2])

while R and R[-1] > A[-1][2]:
    k, i, _ = A.pop()
    S[k][i] = R.pop()

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

Submission status: Accepted

Solution code:
def main():
    X, Y, A, B, C = list(map(int, input().split()))
    P = sorted(list(map(int, input().split())), reverse=True) + [10**9 + 1]
    Q = sorted(list(map(int, input().split())), reverse=True) + [10**9 + 1]
    R = sorted(list(map(int, input().split())), reverse=True)
    t = sum(P[:X]) + sum(Q[:Y])
    x, y = X - 1, Y - 1
    p, q = P[x], Q[y]
    for r in R:
        if p < q:
            if r > p:
                t += r - p
                x -= 1
                p = P[x]
        else:
            if r > q:
                t += r - q
                y -= 1
                q = Q[y]

    return t

print(main())

Submission status: Accepted

Solution code:
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)
i, j, k = 0, 0, 0
ans = 0
while i + j + k < x + y:
    cd = []
    if i < a and i < x:
        cd.append((p[i], 1, i))
    if j < b and j < y:
        cd.append((q[j], 2, j))
    if k < c:
        cd.append((r[k], 3, k))
    cd.sort()
    v, t, cur = cd[-1]
    ans += v
    if t == 1:
        i += 1
    elif t == 2:
        j += 1
    else:
        k += 1
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    X, Y, A, B, C = LI()
    p = sorted(LI())[::-1]
    q = sorted(LI())[::-1]
    r = sorted(LI())[::-1]
    eat = p[:X] + q[:Y]
    eat = sorted(eat)
    r_i = 0
    ans = 0
    for apple in eat:
        if r_i >= C:
            ans += apple
            continue
        if apple < r[r_i]:
            ans += r[r_i]
            r_i += 1
        else:
            ans += apple
    print(ans)


main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
x,y,a, b, c = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
ans = sum(A[:x]) + sum(B[:y])
ans0 = ans
import heapq
Q = []
for i in range(x):
    heapq.heappush(Q, A[i])
for i in range(y):
    heapq.heappush(Q, B[i])

for i in range(c):
    # print(Q)
    p = heapq.heappop(Q)
    # print(p)
    ans0 = ans0 - p + C[i]
    # print(p, C[i],ans0)
    ans = max(ans, ans0)
    heapq.heappush(Q, C[i])
print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
p_array = list(map(int, input().split()))
q_array = list(map(int, input().split()))
r_array = list(map(int, input().split()))

p_array.sort(reverse=True)
q_array.sort(reverse=True)
r_array.sort(reverse=True)

ans = 0
pq_array = []
for p in p_array[:X]:
    ans += p
    pq_array.append(p)

for q in q_array[:Y]:
    ans += q
    pq_array.append(q)

pq_array.sort()

# print(pq_array)
for i, pq in enumerate(pq_array):
    if i != C and pq < r_array[i]:
        ans -= pq
        ans += r_array[i]
    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.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

eat=p[:x]+q[:y]+r
eat.sort(reverse=True)
ans=sum(eat[:x+y])

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

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)

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

ip = len(P) - 1
iq = len(Q) - 1
ir = 0

while iq >= 0 and ip >= 0 and ir < len(R):
    rv = R[ir]
    pv = P[ip]
    qv = Q[iq]
    if rv <= pv and rv <= qv:
        break
    if pv >= qv:
        Q[iq] = rv
        iq -= 1
    else:
        P[ip] = rv
        ip -= 1
    ir += 1

while iq >= 0 and ir < len(R) and Q[iq] < R[ir]:
    Q[iq] = R[ir]
    iq -= 1
    ir += 1

while ip >= 0 and ir < len(R) and P[ip] < R[ir]:
    P[ip] = R[ir]
    ip -= 1
    ir += 1


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

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int, input().split())
P = [int(p) for p in input().split()]
Q = [int(q) for q in input().split()]
R = [int(r) for r in input().split()]

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

import heapq
h1 = []
h2 = []
for i in range(X):
    heapq.heappush(h1, P[i])
for i in range(Y):
    heapq.heappush(h2, Q[i])

for i in range(C):
    if h1[0] < R[i] and h1[0] <= h2[0]:
        heapq.heappop(h1)
        heapq.heappush(h1, R[i])
    elif h2[0] < R[i] and h2[0] <= h1[0]:
        heapq.heappop(h2)
        heapq.heappush(h2, R[i])
    else:
        break
print(sum(h1)+sum(h2))

Submission status: Accepted

Solution code:
x,y, a, b, c= map(int,input().split())
red = list(map(int,input().split()))
green = list(map(int,input().split()))
trans = list(map(int,input().split()))
fr = sorted(red, reverse= True)[:x]
fg = sorted(green, reverse= True)[:y]
candidate = fr + fg + trans
candidate = sorted(candidate, reverse= True)
candidate = candidate[:x+y]
print(sum(candidate))

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)
s = []
s.extend(p[:x])
s.extend(q[:y])
s.sort()
num = 0
for j,i in enumerate(s):
  if num >= len(r):
    break
  if i < r[num]:
    s[j] = r[num]
    num += 1
print(sum(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=[]
ans.append(p[a-x:])
ans.append(q[b-y:])
ans.append(r[c-x-y if c >= x+y else 0:])

ans=sum(ans, [])
ans.sort()

print(sum(ans[len(ans)-x-y:]))

Submission status: Accepted

Solution code:
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())))[::-1]
merge = sorted(p + q + r,reverse= True)

print(sum(merge[:(x+y)]))

Submission status: Accepted

Solution code:
x,y, a, b, c= map(int,input().split())
red = list(map(int,input().split()))
green = list(map(int,input().split()))
trans = list(map(int,input().split()))

fr = sorted(red, reverse= True)[:x]
fg = sorted(green, reverse= True)[:y]
candidate = fr + fg + trans
candidate = sorted(candidate, reverse= True)
candidate = candidate[:x+y]
print(sum(candidate))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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

ans = sum(P[:X]) + sum(Q[:Y])


apples = []
for p in P[:X]:
    heappush(apples, [p, 0])

for q in Q[:Y]:
    heappush(apples, [q, 1])

for r in R:
    amin, rg = heappop(apples)
    if r >= amin:
        heappush(apples, [r, rg])
        ans += (r - amin)
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys


x, y, a, b, c = map(int, input().split())
pa = list(map(int, input().split())) + [-1]
pb = list(map(int, input().split())) + [-1]
r = list(map(int, input().split())) + [-1]

pa = [(pa[i], 0) for i in range(a)]
pb = [(pb[i], 1) for i in range(b)]
r = [(r[i], 2) for i in range(c)]
q = sorted(pa + pb + r)
cnt0 = 0
cnt1 = 0

ans = 0
cnt = 0
for i in range(len(q))[::-1]:
    if cnt == x + y:
        break
    num, col = q[i]
    if col == 0:
        if cnt0 < x:
            cnt0 += 1
            ans += num
            cnt += 1
    elif col == 1:
        if cnt1 < y:
            cnt1 += 1
            ans += num
            cnt += 1
    else:
        ans += num
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
import heapq
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=list(map(int,input().split()))
l=p+q
ans=sum(l)
hq=[]
for i in l:
    heapq.heappush(hq,i)
rq=[]
for i in r:
    heapq.heappush(rq,-i)
a=heapq.heappop(hq)
b=-heapq.heappop(rq)
while a<b:
    ans+=b-a
    if hq:
        a=heapq.heappop(hq)
    else:
        break
    if rq:
        b=-heapq.heappop(rq)
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
import heapq
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
p.sort()
q = list(map(int, input().split()))
q.sort()
r = list(map(int, input().split()))
r.sort()
hq1 = []
hq2 = []
for i in range(x):
    heapq.heappush(hq1, p[a - i - 1])
for i in range(y):
    heapq.heappush(hq2, q[b - i - 1])
while r and (hq1[0] < r[-1] or hq2[0] < r[-1]):
    if hq1[0] < hq2[0]:
        heapq.heappop(hq1)
        heapq.heappush(hq1, r[-1])
        r.pop()
    else:
        heapq.heappop(hq2)
        heapq.heappush(hq2, r[-1])
        r.pop()
print(sum(hq1) + sum(hq2))

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]

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

print(sum(apples[:x+y]))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify

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)
# print(p)
# print(q)
# print(r)

ar = p[:x]
ag = q[:y]
heapify(ar)
heapify(ag)

for i in range(c):
    if r[i] > ar[0] and r[i] > ag[0]:
        if ar[0] < ag[0]:
            heappop(ar)
            heappush(ar, r[i])
        else:
            heappop(ag)
            heappush(ag, r[i])
    elif r[i] <= ar[0] and r[i] > ag[0]:
        heappop(ag)
        heappush(ag, r[i])
    elif r[i] > ar[0] and r[i] <= ag[0]:
        heappop(ar)
        heappush(ar, r[i])
    else:
        # r[i] <= ar[0] and r[i] <= ag[0]
        break

# print(ar)
# print(ag)
ans = sum(ar) + sum(ag)
print(ans)

Submission status: Accepted

Solution code:
import sys;input=sys.stdin.readline
X, Y, Z, A, B = 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())))
PQ = sorted(P+Q)[::-1]

c = 0
while R and PQ:
    x = PQ[-1]
    y = R[-1]
    if x < y:
        PQ.pop()
        c += R.pop()
        continue
    break
print(sum(PQ)+c)

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

ans = P[:x]+Q[:y]
if len(ans)< x+y:
  ans = ans + R[:x+y-len(ans)]
  R = R[x+y-len(ans):]

ans.sort(reverse =1)

ANS = 0
for r in R:
  if ans[-1] < r:
    ans.pop(-1)
    ANS += r
  else:
    break
print(sum(ans) + ANS)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    X, Y, A, B, C = map( int, input().split())
    P = list( map( int, input().split()))
    Q = list( map( int, input().split()))
    R = list( map( int, input().split()))
    P.sort( reverse = True)
    Q.sort( reverse = True)
    R.sort( reverse = True)
    ANS = P[:X] + Q[:Y]
    ANS.sort()
    for i in range( min(X+Y, C)):
        if ANS[i] < R[i]:
            ANS[i] = R[i]
        else:
            break
    print( sum(ANS))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
from heapq import heappush, heappop, heapify

x, y, a, b, c = map(int, input().split())
p = [int(item) for item in input().split()]
p.sort(reverse=True)
q = [int(item) for item in input().split()]
q.sort(reverse=True)
r = [int(item) for item in input().split()]
r.sort(reverse=True)

aa = p[:x]
heapify(aa)
bb = q[:y]
heapify(bb)

for item in r:
    if aa[0] >= item and bb[0] >= item:
        break
    if aa[0] <= bb[0] and aa[0] < item:
        heappop(aa)
        heappush(aa, item)
    elif bb[0] < aa[0] and bb[0] < item:
        heappop(bb)
        heappush(bb, item)
    
ans = sum(aa) + sum(bb)
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()))

h = []
for i in range(A):
    h.append([-p[i],0])
for i in range(B):
    h.append([-q[i],1])
for i in range(C):
    h.append([-r[i],2])
heapq.heapify(h)
x = X
y = Y
s = X+Y
ans = 0
while s>0:
    d,c = heapq.heappop(h)
    if c==0:
        if x>0:
            ans -= d
            x -= 1
            s -= 1
    elif c==1:
        if y>0:
            ans -= d
            y -= 1
            s -= 1
    else:
        ans -= d
        s -= 1
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop

import sys

sys.setrecursionlimit(500000)
def input():
	return sys.stdin.readline()[:-1]

def main():

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

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

    aq = []
    bq = []
    ans = 0
    for i in range(X):
        heappush(aq, p[i])
        ans += p[i]

    for i in range(Y):
        heappush(bq, q[i])
        ans += q[i]

    for rr in r:
        pp,qq = heappop(aq),heappop(bq)
        if pp < rr and qq < rr:
            if rr-pp >= rr-qq:
                heappush(aq,rr)
                heappush(bq,qq)
                ans += rr-pp
            else:
                heappush(aq,pp)
                heappush(bq,rr)
                ans += rr-qq
        elif pp<rr:
            heappush(aq,rr)
            heappush(bq,qq)
            ans += rr-pp
        elif qq<rr:
            heappush(aq,pp)
            heappush(bq,rr)
            ans += rr-qq
        else:
            break

    print(ans)


if __name__ == '__main__':
	main()

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)
p2 = p[:x]
q2 = q[:y]
r2 = r[:min((x + y), c)]
s = p2 + q2 + r2
s.sort(reverse = True)
s2 = s[:(x + y)]
print(sum(s2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

x,y,a,b,c = map(int,input().split())
s = x+y
tank = []
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
r = tuple(map(int,input().split()))

for e in r:
    tank.append((e,2))
for e in p:
    tank.append((e,0))
for e in q:
    tank.append((e,1))

tank.sort(reverse = True)

res = 0
cnt = 0

for e,cl in tank:
    if cl == 2:
        res += e
        cnt += 1
    elif cl == 0:
        if x > 0:
            res += e
            cnt += 1
            x -= 1
    else:
        if y > 0:
            res += e
            cnt += 1
            y -= 1
    if cnt >= s:
        break
print(res)

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)

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

for i in range(len(r)):
    if ans[i]<r[i]:
        ans[i] = r[i]
    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()))
s = []
for i in range(a):
  s.append([p[i],1])
for i in range(b):
  s.append([q[i],2])
for i in range(c):
  s.append([r[i],0])
s.sort(reverse=True)
cnt = 0
cnt1 = 0
cnt2 = 0
ans = 0
for z in s:
  v,j = z
  if j == 1:
    if cnt1 == x:
      continue
    else:
      cnt1 += 1
      cnt += 1
      ans += v
  elif j == 2:
    if cnt2 == y:
      continue
    else:
      cnt2 += 1
      cnt += 1
      ans += v
  else:
    cnt += 1
    ans += v
  if cnt == x+y:
    print(ans)
    exit()