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

P = [[] for _ in range(3)]

for p in map(int, input().split()):
    P[0].append((p,0))
for p in map(int, input().split()):
    P[1].append((p,1))
for p in map(int, input().split()):
    P[2].append((p,2))
P[0].sort(reverse=True)
P[1].sort(reverse=True)
P[2].sort()

P[0] = P[0][:X]
P[1] = P[1][:Y]

data = P[0] + P[1]
data.sort(reverse=True)
num = [A,B]
res = sum(map(lambda x: x[0], data))
while True:
    p, i = data.pop()
    q, _ = P[2].pop()
    if p >= q:
        break
    if num[i] - 1 >= 0:
        num[i] -= 1
        res += q - p
    if sum(num) == 0:
        break
    if len(P[2]) == 0:
        break

print(res)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, A, B, C, *PQR = map(int, read().split())
    P = PQR[:A]
    Q = PQR[A : A + B]
    R = PQR[A + B :]

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

    vec = P[:X] + Q[:Y] + R
    vec.sort(reverse=True)
    ans = sum(vec[: X + Y])

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, A, B, C, *PQR = map(int, read().split())
    P = PQR[:A]
    Q = PQR[A : A + B]
    R = PQR[A + B :]

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

    vec = P[:X] + Q[:Y] + R
    vec.sort(reverse=True)
    ans = sum(vec[: X + Y])

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, A, B, C, *PQR = map(int, read().split())
    P = PQR[:A]
    Q = PQR[A : A + B]
    R = PQR[A + B :]

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

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

    while R and (P or Q):
        if not P:
            vec = Q
        elif not Q:
            vec = P
        elif P[-1] < Q[-1]:
            vec = P
        else:
            vec = Q

        if vec[-1] < R[-1]:
            S.append(R.pop())
            vec.pop()
        else:
            break

    ans = sum(P) + sum(Q) + sum(S)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

p = sorted(p, reverse=True)[:X]
q = sorted(q, reverse=True)[:Y]

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

sa = sum(p[:X])
sb = sum(q[:Y])

p.append(float('inf'))
q.append(float('inf'))


ans = sa + sb
nr = 0
np = X-1
nq = Y-1
while nr < len(r):
  if r[nr] <= min(p[np], q[nq]):
    break
  elif p[np] > q[nq]:
    ans += r[nr] - q[nq]
    nr += 1
    nq -= 1
    continue
  else:
    ans += r[nr] - p[np]
    nr += 1
    np -= 1
    continue

  
print(ans)

Submission status: Accepted

Solution code:
''' 1.降序排列，选出x个红苹果，y个绿苹果
    2.使用c个无色苹果去更新这x+y个苹果中的小值，直到最小也比无色苹果的大为止'''
nums = [int(i) for i in input().split()]
x = nums[0]
y = nums[1]
a = nums[2]
b = nums[3]
c = nums[4]
redApples = [int(i) for i in input().split()]
greenApples = [int(i) for i in input().split()]
colorless = [int(i) for i in input().split()]

redApples.sort(reverse=True)
greenApples.sort(reverse=True)
colorless.sort(reverse=True)
redApples = redApples[:x]
greenApples = greenApples[:y]

res = redApples+greenApples
res.sort(reverse=True)
currIndex = len(res)-1
for i in range(len(colorless)):
    if colorless[i] <= res[currIndex]:
        break
    res[currIndex] = colorless[i]
    currIndex -= 1
    if currIndex < 0:
        break

print(sum(res))

Submission status: Accepted

Solution code:
import heapq as hq
import sys


def main():
    input = sys.stdin.buffer.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 = sorted(p, reverse=True)[:x]
    q = sorted(q, reverse=True)[:y]
    r.sort(reverse=True)
    hq.heapify(p)
    hq.heapify(q)
    # r_pointer以降のリンゴは無色
    r_pointer = 0
    while True:
        red_min = p[0]
        green_min = q[0]
        if red_min < green_min and red_min < r[r_pointer]:
            hq.heappop(p)
            hq.heappush(p, r[r_pointer])
            r_pointer += 1
            if r_pointer >= c:
                break
        elif red_min >= green_min and green_min < r[r_pointer]:
            hq.heappop(q)
            hq.heappush(q, r[r_pointer])
            r_pointer += 1
            if r_pointer >= c:
                break
        else:
            break
    print(sum(p) + sum(q))


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)
eat_p = p[:X]
eat_q = q[:Y]

all_apple = eat_p + eat_q + r
all_apple.sort(reverse=True)

print(sum(all_apple[: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()
q.sort()
r.sort()
top_red = p[-x:]
top_green = q[-y:]
final = top_red+top_green+r
final.sort()
print(sum(final[-(x+y):]))

Submission status: Accepted

Solution code:
x,y,a,b,c = map(int, input().split())
li_p = list(map(int, input().split()))
li_q = list(map(int, input().split()))
li_r = list(map(int, input().split()))
li_p.sort(reverse=True)
li_q.sort(reverse=True)
li_r.sort(reverse=True)
r = []
for i in range(x):
  r.append(li_p[i])
for i in range(y):
  r.append(li_q[i])
for i in range(c):
  r.append(li_r[i])
r.sort(reverse=True)
print(sum(r[:x+y]))

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
X, Y, A, B, C = map(int, input().split())
h = []
for p in map(int, input().split()):
    heappush(h, (-p, 1))
for q in map(int, input().split()):
    heappush(h, (-q, 2))
for r in map(int, input().split()):
    heappush(h, (-r, 3))

ans = 0
cnt = 0
total = X + Y
while cnt < total:
    point, color = heappop(h)
    if color == 1:
        if X > 0:
            ans += -point
            X -= 1
            cnt += 1
    elif color == 2:
        if Y > 0:
            ans += -point
            Y -= 1
            cnt += 1
    elif color == 3:
        ans += -point
        cnt += 1
print(ans)

Submission status: Accepted

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

    sorted_p = sorted(p, reverse=True)[:X]
    sorted_q = sorted(q, reverse=True)[:Y]
    sorted_r = sorted(r, reverse=True)

    p_itr, q_itr, r_itr = X-1, Y-1, 0
    
    while r_itr < C:
        look_p = sorted_p[p_itr]
        look_q = sorted_q[q_itr]
        look_r = sorted_r[r_itr]

        if look_p <= look_q and p_itr >=0:
            if look_p <= look_r:
                sorted_p[p_itr] = look_r
                p_itr -= 1
                r_itr += 1
            else:
                break

        elif q_itr >= 0:
            if look_q <= look_r:
                sorted_q[q_itr] = look_r
                q_itr -= 1
                r_itr += 1
            else:
                break

        else:
            break

    res = sum(sorted_p) + sum(sorted_q)
    print(res)

if __name__ == "__main__":
    main()

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 = list(map(int, input().split()))

ans = sum(sorted(p+q+r, reverse=True)[:X+Y])
print(ans)

Submission status: Accepted

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

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

n = 0
np = 0
nq = 0
ans = 0
while n < x + y:
    cs = []
    if p and np < x:
        cs.append(p[-1])
    if q and nq < y:
        cs.append(q[-1])
    if r:
        cs.append(r[-1])
    m = max(cs)
    if r and m == r[-1]:
        ans += r.pop()
    elif p and m == p[-1]:
        ans += p.pop()
        np += 1
    else:
        ans += q.pop()
        nq += 1
    n += 1

print(ans)

Submission status: Accepted

Solution code:
# :20
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


x, y, a, b, c = LI()
p = sorted(LI())[::-1][:x]
q = sorted(LI())[::-1][:y]
r = sorted(LI())[::-1][:x+y]
bucket = []
bucket += [i for i in p]
bucket += [i for i in q]
bucket += [i for i in r]
bucket = sorted(bucket)[::-1]
ans = sum(bucket[:x+y])
print(ans)

Submission status: Accepted

Solution code:
# :20
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


x, y, a, b, c = LI()
p = sorted(LI())[::-1][:x]
q = sorted(LI())[::-1][:y]
r = sorted(LI())[::-1][:x+y]
# print(p, q, r)
bucket = []
bucket += [[i, 'p'] for i in p]
bucket += [[i, 'q'] for i in q]
bucket += [[i, 'r'] for i in r]
bucket = sorted(bucket, key=lambda x: x[0])
# print(bucket)
d = {'p': 0, 'q': 0}
ans = 0
cnt = 0
while cnt < x + y:
    if bucket[-1][1] == 'p' and d[bucket[-1][1]] < x:
        ans += bucket.pop()[0]
        d['p'] += 1
        cnt += 1
    elif bucket[-1][1] == 'q' and d[bucket[-1][1]] < y:
        ans += bucket.pop()[0]
        d['q'] += 1
        cnt += 1
    else:
        ans += bucket.pop()[0]
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
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)
ansp=p[:x]
ansq=q[:y]
ans=sum(ansp)+sum(ansq)
ep=x-1
eq=y-1
for i in range(c):
    if ep<0 and eq<0:
        break
    if (ep>=0 and eq<0) or (ep>=0 and eq>=0 and ansp[ep]<ansq[eq]):
        if ansp[ep]<r[i]:
            ans=ans-ansp[ep]+r[i]
            ep-=1 
        else:
            break
    elif (eq>=0 and ep<0) or (ep>=0 and eq>=0 and ansq[eq]<=ansp[ep]):
        if ansq[eq]<r[i]:
            ans=ans-ansq[eq]+r[i]
            eq-=1
        else:
          break
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, 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, insort, insort_left
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 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10
	
X, Y, A, B, C = MAP()
p = deque(sorted(LIST(), reverse=True))
q = deque(sorted(LIST(), reverse=True))
r = deque(sorted(LIST(), reverse=True) + [0])

x = 0
y = 0
z = 0
ans = 0
while x+y+z < X+Y:
	compare = (p[0], q[0], r[0])

	if p[0] == max(compare):
		x += 1
		ans += p.popleft()
		if x == X:
			p = [0]
	elif q[0] == max(compare):
		y += 1
		ans += q.popleft()
		if y == Y:
			q = [0]
	else:
		z += 1
		ans += r.popleft()

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, 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, insort, insort_left
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 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10
	
X, Y, A, B, C = MAP()
p = deque(sorted(LIST(), reverse=True) + [0])
q = deque(sorted(LIST(), reverse=True) + [0])
r = deque(sorted(LIST(), reverse=True) + [0])

x = 0
y = 0
z = 0
ans = 0
while x+y+z < X+Y:
	compare = (p[0], q[0], r[0])

	if p[0] == max(compare):
		x += 1
		ans += p.popleft()
		if x == X:
			p = [0]
	elif q[0] == max(compare):
		y += 1
		ans += q.popleft()
		if y == Y:
			q = [0]
	else:
		z += 1
		ans += r.popleft()

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int,input().split())
AList = sorted(list(map(int,input().split())),reverse=True)
BList = sorted(list(map(int,input().split())),reverse=True)
CList = sorted(list(map(int,input().split())),reverse=True)
FullList = sorted(AList[:X] + BList[:Y])
counter = 0
for i in range(min(C,X+Y)):
  if CList[i] <= FullList[i]:
    break
  else:
    counter += 1
ans = sum(CList[:counter])+sum(FullList[counter:])
print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = [int(_) for _ in input().split()]
P = [int(_) * 3 for _ in input().split()]
Q = [int(_) * 3 + 1 for _ in input().split()]
R = [int(_) * 3 + 2 for _ in input().split()]
S = sorted(P + Q + R)
cnt = [0, 0, 0]
ans = [0, 0, 0]
limit = [X, Y, 10**10]
while S:
    s = S.pop()
    q, r = divmod(s, 3)
    if cnt[r] >= limit[r]:
        continue
    cnt[r] += 1
    ans[r] += q
    if sum(cnt) == X + Y:
        print(sum(ans))
        exit()

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()))
import heapq as hp
s = x+y
u = []
hp.heapify(u)
for i in range(a):
    hp.heappush(u,[-p[i],'r'])
for i in range(c):
    hp.heappush(u,[-r[i],'m'])
v = []
hp.heapify(v)
for i in range(b):
    hp.heappush(v,[-q[i],'g'])
for i in range(c):
    hp.heappush(v,[-r[i],'m'])
z = []
ans = 0
hp.heapify(z)
for i in range(a):
    hp.heappush(z,[-p[i],'r'])
for i in range(b):
    hp.heappush(z,[-q[i],'g'])
for i in range(c):
    hp.heappush(z,[-r[i],'m'])
while s and x and y:
    t = hp.heappop(z)
    ans += -t[0]
    if t[1] == 'r':
        x -= 1
        s -=1
    elif t[1] == 'g':
        y -= 1
        s -= 1
    else:
        s -= 1
if s > 0 and x > 0:
    while s:
        t = hp.heappop(z)
        if t[1] == 'g':
            continue
        else:
            ans += -t[0]
            s -= 1
elif s > 0 and y > 0:
    while s:
        t = hp.heappop(z)
        if t[1] == 'r':
            continue
        else:
            ans += -t[0]
            s -= 1
print(ans)

Submission status: Accepted

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

a_list=list(map(int,input().split()))
a_list=sorted(a_list)
b_list=list(map(int,input().split()))
b_list=sorted(b_list)
a_list=a_list[-x:]
b_list=b_list[-y:]
c_list=list(map(int,input().split()))
for i in range(c):
    c_list[i]=-c_list[i]
import heapq
heapq.heapify(a_list)
heapq.heapify(b_list)
heapq.heapify(c_list)

flag=True
while flag and c_list:
    x=heapq.heappop(c_list)
    x=x*(-1)
    #ｃ_listの中でのもっとも大きい値
    min_a=heapq.heappop(a_list)
    min_b=heapq.heappop(b_list)
    if min(min_a,min_b)>=x:
        flag=False
        heapq.heappush(a_list,min_a)
        heapq.heappush(b_list,min_b)
    else:
        if min_a>=min_b:
            heapq.heappush(a_list,min_a)
            heapq.heappush(b_list,x)
         
        elif min_a<min_b:
            heapq.heappush(b_list,min_b)
            heapq.heappush(a_list,x)

sum_a=sum(list(a_list))
sum_b=sum(list(b_list))

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

#print(P)
#print(Q)
#print(R)

p = 0
q = 0
r = 0
ans = 0
for i in range(x+y):
    if p < x:
        red = P[p]
    else:
        red = 0
    if q < y:
        green = Q[q]
    else:
        green = 0
    if r < c:
        white = R[r]
    else:
        white = 0
    
    choice = max(red, green, white)
    #print(choice)
    ans += choice
    
    if choice == red:
        p += 1
    elif choice == green:
        q += 1
    else:
        r += 1
print(ans)

Submission status: Accepted

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

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
l = p[:X]+q[:Y]
l.sort()
s = sum(l)
ans = s
pre = s

for i in range(min(len(l), C)):
    ans = max(ans, pre-l[i]+r[i])
    pre = pre-l[i]+r[i]

print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
Ap = list(map(int, input().split()))
Bq = list(map(int, input().split()))
Cr = list(map(int, input().split()))

Ap.sort()
Bq.sort()

print(sum(sorted(Ap[A-X:] + Bq[B-Y:] + Cr)[C:]))

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
Ap = list(map(int, input().split()))
Bq = list(map(int, input().split()))
Cr = list(map(int, input().split()))

Ap.sort(reverse = True)
Bq.sort(reverse = True)

L = sorted(Ap[:X] + Bq[:Y] + Cr, reverse = True)

print(sum(L[:X+Y]))

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
Ap = list(map(lambda x: (int(x), 0), input().split()))
Bq = list(map(lambda x: (int(x), 1), input().split()))
Cr = list(map(lambda x: (int(x), 2), input().split()))

INF = 2 * 10**5 + 1

L = Ap + Bq + Cr
L.sort(reverse = True)

cnt = [0, 0, 0]
cntM = [X, Y, INF]
s = 0

ans = 0

for x, i in L:
  if s == X + Y:
    break
  elif cnt[i] < cntM[i]:
    ans += x
    cnt[i] += 1
    s += 1
  else:
    pass

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)
p=p[:x]
q=q[:y]

import heapq
from collections import deque

heapq.heapify(p)
heapq.heapify(q)
r=deque(r)

for i in range(c):
    v_p=heapq.heappop(p)
    v_q=heapq.heappop(q)

    v_r=r.pop()
    if v_p<=v_q:
        heapq.heappush(q,v_q)
        if v_p<v_r:
            heapq.heappush(p,v_r)
        else:
            heapq.heappush(p,v_p)
    else:
        heapq.heappush(p,v_p)
        if v_q<v_r:
            heapq.heappush(q,v_r)
        else:
            heapq.heappush(q,v_q)

ans=sum(p)+sum(q)

print(ans)

Submission status: Accepted

Solution code:
from heapq import*


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

heapify(P); heapify(Q)
while R:
    p, q, r = heappop(P), heappop(Q), R.pop()
    if p < q:
        heappush(Q, q)
        if p < r:
            heappush(P, r)
        else:
            heappush(P, p)
            break
    else:
        heappush(P, p)
        if q < r:
            heappush(Q, r)
        else:
            heappush(Q, q)
            break

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

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

x,y,a,b,c = MAP()

p = LIST()
q = LIST()
r = LIST()

p = sorted(p)[::-1]
q = sorted(q)[::-1]
r = sorted(r)[::-1]

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

new = p+q+r
# print(new)
new = sorted(new)[::-1]

print(sum(new[: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(reverse=True)
Q.sort(reverse=True)
R.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 =map(int,input().split())
p = sorted(list(map(int,input().split())))[::-1]
q = sorted(list(map(int,input().split())))[::-1]
r = (list(map(int,input().split())))

p1 = p[:x]
q1 = q[:y]
l = sum(sorted(p1+q1+r)[::-1][:x+y])
print(l)

Submission status: Accepted

Solution code:
X, Y, A, B, C = 2, 2, 2, 2, 2
prr = [8, 6]
qrr = [9, 1]
krr = [2, 1]


X, Y, A, B, C = 2, 2, 4, 4, 4
prr = [11, 12, 13, 14]
qrr = [21, 22, 23, 24]
krr = [1, 2, 3, 4]


X, Y, A, B, C = 1, 2, 2, 2, 1
prr = [2, 4]
qrr = [5, 1]
krr = [3]


X, Y, A, B, C = map(int,input().split())
prr = list(map(int,input().split()))
qrr = list(map(int,input().split()))
krr = list(map(int,input().split()))

def calculate(x, y, a, b, c, prr, qrr, krr):
    prr = sorted(prr,key=lambda x:-x)
    qrr = sorted(qrr, key=lambda x: -x)

    prr = prr[:x]
    qrr = qrr[:y]

    result = []
    result.extend(prr)
    result.extend(qrr)
    result.extend(krr)

    result = sorted(result,key=lambda x:-x)

    result = result[:(x+y)]

    print(sum(result))

calculate(X, Y, A, B, C, prr, qrr, krr)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

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)

    now = sum(p[:X]) + sum(q[:Y])
    ans = now
    i = X-1
    j = Y-1

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X,Y,A,B,C=list(map(int,input().split()))
A_l=list(map(int,input().split()))
B_l=list(map(int,input().split()))
C_l=list(map(int,input().split()))
A_l.sort(reverse=True)
B_l.sort(reverse=True)
C_l.sort()
A_after_l=A_l[:X]
B_after_l=B_l[:Y]
ans=[A_after_l,B_after_l]
ans=sum(ans,[])
ans.sort()
import heapq
heapq.heapify(ans)
for i in C_l:
   N=heapq.heappop(ans)
   if N < i:
      heapq.heappush(ans,i)
   else:
      heapq.heappush(ans,N)
print(sum(ans))

Submission status: Accepted

Solution code:
X,Y,A,B,C=list(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)

pqr=sorted(p[:X]+q[:Y]+r,reverse=True)

print(sum(pqr[:(X+Y)]))

Submission status: Accepted

Solution code:
X,Y,A,B,C=list(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)

i=X-1
j=Y-1
k=0

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

while k<len(r):
    if i>-1 and j>-1:
        if p[i]<q[j]:
            cmin=p[i]
            i-=1
        else:
            cmin=q[j]
            j-=1
        
        if r[k]<=cmin:
            break
        ans+=r[k]-cmin
        k+=1
    elif i>-1:
        cmin=p[i]
        i-=1

        if r[k]<=cmin:
            break

        ans+=r[k]-cmin
        k+=1
    elif j>-1:
        cmin=q[j]
        j-=1

        if r[k]<=cmin:
            break

        ans+=r[k]-cmin
        k+=1
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate
import sys
import math
import bisect


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def LS(): return [list(x) for x in sys.stdin.readline().split()]


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


sys.setrecursionlimit(1000000)


def main():
    X, Y, A, B, C = LI()
    p, q, r = sorted(LI(), reverse=True)[:X], sorted(
        LI(), reverse=True)[:Y], LI()
    print(sum(sorted(p + q + r, reverse=True)[:X+Y]))


main()

Submission status: Accepted

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate
import sys
import math
import bisect


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def LS(): return [list(x) for x in sys.stdin.readline().split()]


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


sys.setrecursionlimit(1000000)


def main():
    X, Y, A, B, C = LI()
    p = LI()
    q = LI()
    r = LI()
    p = sorted(p, reverse=True)[:X]
    q = sorted(q, reverse=True)[:Y]
    heapify(p), heapify(q)

    for ri in r:
        if len(p) < X:
            heappush(p, ri)
        elif len(q) < Y:
            heappush(q, ri)
        else:
            ptop = p[0]
            qtop = q[0]
            if min(ptop, qtop, ri) == ri:
                continue
            if ptop <= qtop:
                heappop(p)
                heappush(p, ri)
            else:
                heappop(q)
                heappush(q, ri)

    print(sum(sorted(p, reverse=True)[:X]) +
          sum(sorted(q, reverse=True)[:Y]))


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

Submission status: Accepted

Solution code:
import heapq

X, Y, A, B, C = map(int, input().split())
apples = []
for x in input().split():
    apples.append((int(x), 'r'))
for x in input().split():
    apples.append((int(x), 'g'))
for x in input().split():
    apples.append((int(x), 'n'))
apples.sort()
apples.reverse()

r_count = 0
g_count = 0
n_count = 0
yummy = 0
for _ in range(A+B+C):
    apple = apples[_]
    if apple[1] == 'g' and not g_count == Y:
        g_count += 1
        yummy += apple[0]
    elif apple[1] == 'r' and not r_count == X:
        r_count += 1
        yummy += apple[0]
    elif apple[1] == 'n':
        n_count += 1
        yummy += apple[0]
    if g_count + r_count + n_count == X+Y:
        break
print(yummy)