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
input = sys.stdin.readline
from collections import defaultdict, Counter
from bisect import *

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()
lb, rb = 1, 10**9+1
while lb+1 < rb:
    m = (lb+rb)//2
    x = min(X, A-bisect_left(p, m))
    y = min(Y, B-bisect_left(q, m))
    z = C-bisect_left(r, m)
    if z < (X-x)+(Y-y):
        rb = m
    else:
        lb = m
ans = 0
flg = 0
for i in range(A):
    if p[A-i-1] >= lb and i < X:
        ans += p[A-i-1]
    else:
        pb = i
        flg = 1
        break
if not flg:
    pb = X
flg = 0
for i in range(B):
    if q[B-i-1] >= lb and i < Y:
        ans += q[B-i-1]
    else:
        qb = i
        flg = 1
        break
if not flg:
    qb = Y
ans += sum(r[C-X-Y+pb+qb:])
print(ans)

Submission status: Accepted

Solution code:
#E
x, y, al, bl, cl = 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)
am = x-1
bm = y-1
cm = 0
while True:
    #print(am)
    #print(bm)
    #print(cm)
    if cm >= cl:
        break
    elif am == -1:
        if bm == -1:
            break
        else:
            if c[cm] > b[bm]:
                cm += 1
                bm -= 1
            else:
                break
    elif bm == -1:
        if c[cm] > a[am]:
            cm += 1
            am -= 1
        else:
            break
    elif (c[cm] <= a[am]) & (c[cm] <= b[bm]):
        break
    elif (c[cm] > a[am]) & (c[cm] <= b[bm]):
        cm += 1
        am -= 1
    elif (c[cm] <= a[am]) & (c[cm] > b[bm]):
        cm += 1
        bm -= 1
    else:
        if (c[cm]-a[am]) < (c[cm]-b[bm]):
            cm += 1
            bm -= 1
        else:
            cm += 1
            am -= 1
ans = 0
if am != -1:
    for i in range(am+1):
        ans += a[i]
if bm != -1:
    for j in range(bm+1):
        ans += b[j]
if cm != 0:
    for k in range(cm):
        ans += c[k]

print(ans)

Submission status: Accepted

Solution code:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

#a = list(map(int, input().split()))


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

aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
cc = list(map(int, input().split()))

aa.sort(reverse=True)
bb.sort(reverse=True)
cc.sort(reverse=True)

nowa = min(x,a)
nowb = min(y,b)
nowc = (x+y)-nowa-nowb

total = sum(aa[0:nowa])+sum(bb[0:nowb])+sum(cc[0:nowc])
#print(total)
#print(aa,bb,cc)
maxim = total
while(nowc < c and(nowa>0 or nowb >0)):
    nowc += 1
    total += cc[nowc-1]
    if(nowa==0):
        nowb-=1
        total -= bb[nowb]
    elif(nowb==0):
        nowa -= 1
        total -= aa[nowa]
    elif(aa[nowa-1]>bb[nowb-1]):
        nowb-=1
        total -= bb[nowb]
    else:
        nowa -= 1
        total -= aa[nowa]
    if(maxim < total):
        maxim = total
    #print(total,nowa,nowb,nowc)
print(maxim)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

x, y, a, b, c = list(map(int, input().split()))
p = sorted(sorted(list(map(int, input().split())), reverse=True)[:x])
q = sorted(sorted(list(map(int, input().split())), reverse=True)[:y])
r = list(map(int, input().split()))
print(sum(sorted(p + q + r, reverse=True)[:x+y]))

Submission status: Accepted

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

a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
b_list = list(map(int, input().split()))
b_list.sort(reverse=True)
c_list = list(map(int, input().split()))
c_list.sort(reverse=True)

a_index =0
b_index =0
c_index = 0
count = 0
for i in range(x+y):
    if (a_index < x) and \
        (b_index >= y or a_list[a_index] >= b_list[b_index]) and \
        (c_index >= c or a_list[a_index] >= c_list[c_index]):
        count += a_list[a_index]
        a_index += 1
    elif (b_index < y) and \
        (a_index >= x or b_list[b_index] >= a_list[a_index]) and \
        (c_index >= c or b_list[b_index] >= c_list[c_index]):
        count += b_list[b_index]
        b_index += 1
    elif (c_index < c) and \
        (a_index >= x or c_list[c_index] >= a_list[a_index]) and \
        (b_index >= y or c_list[c_index] >= b_list[b_index]):
        count += c_list[c_index]
        c_index += 1
    
print(count)

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)
R = p[0:X]
G = q[0:Y]
eat = R + G
eat += r
eat.sort(reverse = True)
eat = eat[0:X + Y]
print(sum(eat))

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

eat=[]
eat.extend(pp)
eat.extend(qq)
eat.extend(r)
eat.sort()
e=eat[(-x-y):]
print(sum(e))

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 = list(map(lambda x: int(x) * -1, input().split()))
heapq.heapify(r)


base = p[0:X] + q[0:Y]
heapq.heapify(base)

for i in range(X+Y):
    if i >= C:
        break

    R = -r[0]
    b = base[0]
    if R > b:
        heapq.heappop(base)
        heapq.heappop(r)
        heapq.heappush(base, R)
    else:
        break

print(sum(base))

Submission status: Accepted

Solution code:
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()
q.sort()
r.sort(reverse=True)
p = p[A - X:]
q = q[B - Y:]
pi = 0
qi = 0
for v in r:
    if p[pi] < v or q[qi] < v:
        if p[pi] < q[qi]:
            p[pi] = v
            pi = min(pi + 1, X - 1)
        else:
            q[qi] = v
            qi = min(qi + 1, Y - 1)
    else:
        break
print(sum(p) + sum(q))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# E - Red and Green Apples
# 標準入力の取得
X,Y,A,B,C = list(map(int, input().split()))
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
r_list = list(map(int, input().split()))

# 最大値を求める
# p_list, q_listから最大値を求め、それをr_listで置きかえていく
sorted_p_list = list(reversed(sorted(p_list)))
sorted_q_list = list(reversed(sorted(q_list)))
sorted_r_list = list(reversed(sorted(r_list)))

selected = sorted_p_list[:X]
selected.extend(sorted_q_list[:Y])
selected = list(sorted(selected))

for c in range(C):
    if selected[c] < sorted_r_list[c]:
        selected[c] = sorted_r_list[c]
    else:
        break

print(sum(selected))

Submission status: Accepted

Solution code:
import heapq


def resolve():
    x, y, a, b, c = map(int, input().split())
    p = sorted(list(map(int, input().split())))[-x:]
    q = sorted(list(map(int, input().split())))[-y:]
    r = [-int(x) for x in input().split()]
    heapq.heapify(p)
    heapq.heapify(r)
    for i in q:
        heapq.heappush(p, i)
    ans = 0
    while p and r:
        ans += max(heapq.heappop(p), -heapq.heappop(r))
    ans += sum(p)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
lp=list(map(int,input().split()))
lq=list(map(int,input().split()))
lr=list(map(int,input().split()))
lp.sort()
lq.sort()
lr.sort()
lnew=lp[A-X:]+lq[B-Y:]+lr
lnew.sort()
print(sum(lnew[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 = True)
Q.sort(reverse = True)
R.sort(reverse = True)

count = X + Y
p = 0
q = 0
r = 0
ans = 0
for _ in range(count):
    tmp = 0
    next_ = None
    if X > 0:
        tmp = P[p]
        next_ = 'P'
    if Y > 0 and Q[q] > tmp:
        tmp = Q[q]
        next_ = 'Q'
    if r < C and R[r] >= tmp:
        tmp = R[r]
        next_ = 'R'
    
    ans += tmp
    if next_ == 'P':
        X -= 1
        p += 1
    elif next_ =='Q':
        Y -= 1
        q += 1
    else:
        r += 1
    
print (ans)
# print (P)
# print (Q)
# print (R)

Submission status: Accepted

Solution code:
e_r, e_g, n_r, n_g, n_n = map(int, input().split())
red = list(map(int, input().split()))
green = list(map(int, input().split()))
none = list(map(int, input().split()))
 
red.sort(reverse=True)
green.sort(reverse=True)
none.sort(reverse=True)

eat_r = red[0:e_r]
eat_g = green[0:e_g]

eat = eat_r + eat_g
eat.sort()

for i in range(len(eat)):
  if len(none) > 0 and eat[i] < none[0]:  
    eat[i] = none.pop(0)

print(sum(eat))

Submission status: Accepted

Solution code:
x,y,a,b,c = [int(e) for e in input().split()]
p = [int(e) for e in input().split()]
q = [int(e) for e in input().split()]
r = [int(e) for e in input().split()]
p.sort()
p.reverse()
q.sort()
q.reverse()
r.sort()
r.reverse()
sp = 0
sq = 0
for i in range(0,x):
    sp += p[i]
for i in range(0,y):
    sq += q[i]
ma = sp + sq
index = 0
while index < c:
    if p[x-1] < q[y-1] or y == 0:
        if r[index] > p[x-1]:
            ma += r[index]-p[x-1]
            index += 1
            x -= 1
        else:
            break
    else:
        if r[index] > q[y-1]:
            ma += r[index]-q[y-1]
            index += 1
            y -= 1
        else:
            break
print(ma)

Submission status: Accepted

Solution code:
x,t,a,b,c=map(int,input().split())
P=sorted(list(map(int,input().split())))
Q=sorted(list(map(int,input().split())))
R=sorted(list(map(int,input().split())))[::-1]+[-1]

A=[]
A +=P[a-x:a]
A +=Q[b-t:b]
A=sorted(A)
add,cnt=0,0
for apple in A:
    if apple<R[add]:
        cnt +=R[add]
        add +=1
    else:
        cnt +=apple
print(cnt)

Submission status: Accepted

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

P.sort()
Q.sort()
R.sort()

orig = np.concatenate([P[::-1][:X],Q[::-1][:Y]])
orig.sort()
for i in range(C):
	if R[-(i+1)] > orig[i]:
		orig[i] = R[-(i+1)]
	else:
		break

print(orig.sum())

Submission status: Accepted

Solution code:
import heapq

X,Y,A,B,C=map(int,input().split())
plist=list(map(int,input().split()))
qlist=list(map(int,input().split()))
rlist=list(map(int,input().split()))

plist.sort(reverse=True)
qlist.sort(reverse=True)
rlist.sort(reverse=True)
plist2=plist[:X]
qlist2=qlist[:Y]
rlist2=rlist[:X+Y]

hp=[]
for p in plist2:
  heapq.heappush(hp,p)
hq=[]
for q in qlist2:
  heapq.heappush(hq,q)
  
for r in rlist2:
  if hp[0]<hq[0]:
    if r>hp[0]:
      heapq.heappop(hp)
      heapq.heappush(hp,r)
  else:
    if r>hq[0]:
      heapq.heappop(hq)
      heapq.heappush(hq,r)
      
#print(hp)
#print(hq)
print(sum(hp)+sum(hq))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

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

idx_p = X
idx_q = Y
eatR = 0

for i in range(1, C + 1):
    r = R[-i]
    if idx_p <= 0 and idx_q <= 0:
        break

    elif idx_p <= 0:
        if Q[-idx_q] >= r:
            break
        else:
            eatR += r
            idx_q -= 1

    elif idx_q <= 0:
        if P[-idx_p] >= r:
            break
        else:
            eatR += r
            idx_p -= 1

    elif P[-idx_p] >= r and Q[-idx_q] >= r:
        break

    elif P[-idx_p] >= r:
        eatR += r
        idx_q -= 1
    
    elif Q[-idx_q] >= r:
        eatR += r
        idx_p -= 1
    
    else:
        diff_p = r - P[-idx_p]
        diff_q = r - Q[-idx_q]
        if diff_p >= diff_q:
            eatR += r
            idx_p -= 1
        else:
            eatR += r
            idx_q -= 1

ans = eatR + sum(P[(A - idx_p):]) + sum(Q[(B - idx_q):])
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

X, Y, A, B, C = map(int, input().split())
H = []
for p in map(int, input().split()):
  heappush(H, ((-p, 'r')))
for q in map(int, input().split()):
  heappush(H, ((-q, 'g')))
for r in map(int, input().split()):
  heappush(H, ((-r, '')))

count = X + Y
ans = 0
while count > 0:
  h = heappop(H)
  if h[1] == 'r' :
    if X > 0:
      X -= 1
      count -= 1
      ans -= h[0]
  elif h[1] == 'g':
    if Y > 0:
      Y -= 1
      count -= 1
      ans -= h[0]
  else:
    count -= 1
    ans -= h[0]

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C=(int(x) for x in input().split())
ps = sorted([int(x) for x in input().split()],reverse=True)
qs = sorted([int(x) for x in input().split()],reverse=True)
rs = sorted([int(x) for x in input().split()],reverse=True)
sta = sorted(ps[:X] + qs[:Y])
res = sum(sta)
for i in range(C):
  a = rs[i]
  s = sta[i]
  if a> s:
    res += a-s
  else:
    break
print(res)

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

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

    p=p[:X]
    q=q[:Y]

    p.extend(q)

    p.extend(r)

    p.sort(reverse=True)

    print(sum(p[:X+Y]))
    
main()

Submission status: Accepted

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

p = list(map(int, input().split()))
p.sort(reverse = True)
p = p[:X]

q = list(map(int, input().split()))
q.sort(reverse = True)
q = q[:Y]

r = list(map(int, input().split()))
r.sort(reverse = True)
r = r[:(X+Y)]

s = p+q+r
s.sort(reverse = True)
print(sum(s[:(X+Y)]))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np
X, Y, A, B, C = map(int, readline().split())

p = np.array(readline().split(),np.int32)
q = np.array(readline().split(),np.int32)
r = np.array(readline().split(),np.int32)
#print(p)
#print(q)
#print(r)

pp = np.sort(p)[A-X:]
qq = np.sort(q)[B-Y:]
#print(pp)
#print(qq)

all_item = np.sort(np.concatenate([pp, qq, r]))
# print(all_item)
print(np.sum(all_item[len(all_item) - X - Y:]))

Submission status: Accepted

Solution code:
import heapq as hq
X,Y,A,B,C=map(int,input().split(' '))
P,Q,R=[sorted(list(map(int,input().split(' '))),reverse=True) for i in range(3)]
P=P[:X]
Q=Q[:Y]
hq.heapify(P)
hq.heapify(Q)
R=R[::-1]
while len(R)>0:
    p=hq.heappop(P)
    q=hq.heappop(Q)
    r=R.pop()
    if r<=min(p,q):
        hq.heappush(P,p)
        hq.heappush(Q,q)
        break
    if p<=q and p<r:
        hq.heappush(P,r)
        hq.heappush(Q,q)
    elif p>q and q<r:
        hq.heappush(P,p)
        hq.heappush(Q,r)
print(sum(P)+sum(Q))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

x, y, a, b, c = LI()
p = LI()
q = LI()
r = LI()

p.sort(reverse=1)
q.sort(reverse=1)
r.sort(reverse=1)
r = list(accumulate(r))
ans = 0
h = []
s = sum(p[:x])
s += sum(q[:y])
for i in p[:x]:
    heappush(h, i)
for i in q[:y]:
    heappush(h, i)
ans = s
for i in range(c):
    if len(h) > 0:
        f = heappop(h)
    else:
        break
    s -= f
    tmp = s + r[i]
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
r = list(map(int, input().split()))
g = list(map(int, input().split()))
nc = list(map(int, input().split()))

r.sort(reverse=True)
g.sort(reverse=True)
nc.sort(reverse=True)

selected = r[:X] + g[:Y]
selected.sort()

if len(selected) > len(nc) or len(selected) == len(nc):
  L = len(nc)
else:
  L = len(selected)

for i in range(L):
  if selected[i] < nc[i]:
    selected[i] = nc[i]
    
print(sum(selected))

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
p.sort()
p.reverse()
q = list(map(int, input().split()))
q.sort()
q.reverse()
r = list(map(int, input().split()))
r.sort()
r.reverse()

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


arr = p + q
arr.sort()
#print(arr)
#print(r)

for i in range(min(len(arr),len(r))):
    if arr[i] <= r[i]:
        arr[i] = r[i]

print(sum(arr))

Submission status: Accepted

Solution code:
from math import *

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)
cres = sum(p[:x]) + sum(q[:y])
x -= 1
y -= 1
#print(cres, p, q, x, y)
for i in range(min(x + y + 2, c)):
    if x >= 0 and r[i] > p[x] and (y < 0 or p[x] <= q[y]):
        cres += r[i] - p[x]
        x -= 1
    elif y >= 0 and r[i] > q[y]:
        cres += r[i] - q[y]
        y -= 1
print(cres)

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)[:x]
q=sorted(q,reverse=True)[:y]
r=sorted(r)

p.extend(q)
p=sorted(p)

ans=0
for i in range(x+y):
    if len(r)>0:
        if p[-1]<r[-1]:
            ans+=r[-1]
            r.pop(-1)
        else:
            ans+=p[-1]
            p.pop(-1)
    else:
        ans+=p[-1]
        p.pop(-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()))
s = [[i,0] for i in p] + [[i,1] for i in q] +  [[i,2] for i in r]
s.sort(key=lambda z:z[0])
ans = 0
k = [0,0,0]
k1 = [x,y,x+y]
while sum(k)<x+y:
  t = s.pop()
  if k[t[1]]<k1[t[1]]:
    ans += t[0]
    k[t[1]] += 1
print(ans)

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)
pq = p[:X:]
q.sort(reverse=True)
pq.extend(q[:Y:])
pq.sort()
r.sort()
ans = 0
cnt = len(r)
for i in range(X+Y):
    if 0 < cnt and pq[i] < r[-1]:
        ans += r.pop()
        cnt -= 1
    else:
        ans += pq[i]

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

x,y,a,b,c = [int(i) for i in readline().split()]
p = [int(i) for i in readline().split()]
q = [int(i) for i in readline().split()]
r = [int(i) for i in readline().split()]

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

from heapq import *

hq = sorted(p[:x]+q[:y],reverse=True)

ans = sum(hq)
while hq and r and hq[-1] < r[-1]:
    v = hq.pop()
    w = r.pop()
    ans += w-v

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappop, heapify, heapreplace


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

heapify(P)
heapify(Q)
heapify(R)

for _ in range(A - X):
    heappop(P)
for _ in range(B - Y):
    heappop(Q)


while R:
    p, q, r = P[0], Q[0], heappop(R)
    if p < q:
        T, t = P, p
    else:
        T, t = Q, q

    if t < r:
        heapreplace(T, r)

P.sort(reverse=True)
Q.sort(reverse=True)
ans = 0
for i in range(X):
    ans += P[i]
for i in range(Y):
    ans += Q[i]

print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
pl = list(map(int, input().split()))
ql = list(map(int, input().split()))
rl = list(map(int, input().split()))
pl.sort(reverse=True)
ql.sort(reverse=True)
rl.sort(reverse=True)

total = pl[:X]+ql[:Y]+rl[:X+Y]
total.sort(reverse=True)
result = sum(total[:X+Y])

print(result)

Submission status: Accepted

Solution code:
import sys

x, y, a, b, c = map(int, sys.stdin.readline().split())
*p, = map(int, sys.stdin.readline().split())
*q, = map(int, sys.stdin.readline().split())
*r, = map(int, sys.stdin.readline().split())

def main():
    cnt = [0] * 3
    cand = []
    for i in p:
        cand.append((i, 0))
    for i in q:
        cand.append((i, 1))
    for i in r:
        cand.append((i, 2))
    cand.sort()

    tot = 0
    res = 0
    for _ in range(a+b+c):
        d, k = cand.pop()
        if k == 0:
            if cnt[0] == x:
                continue
            cnt[0] += 1
            res += d
            tot += 1
        elif k == 1:
            if cnt[1] == y:
                continue
            cnt[1] += 1
            res += d
            tot += 1
        elif k == 2:
            cnt[2] += 1
            res += d
            tot += 1
        if tot == x + y:
            break

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# heapq
import heapq
class Reverse:
    def __init__(self, val):
        self.val = val
        
    def __lt__(self, other):
        return self.val > other.val
        
    def __repr__(self):
        return repr(self.val)
    
class Heapq:
    def __init__(self, arr, desc = False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)
 
    def pop(self):
        if self.desc: return heapq.heappop(self.hq).val
        else: return heapq.heappop(self.hq)
 
    def push(self, a):
        if self.desc: heapq.heappush(self.hq, Reverse(a))
        else: heapq.heappush(self.hq, a)
 
    def top(self):
        if self.desc: return self.hq[0].val
        else: return self.hq[0]

from operator import itemgetter
def index_sort(A):
    A_sort = sorted(enumerate(A),key=itemgetter(1))
    index = [a[0] for a in A_sort]
    sorted_A = [a[1] for a in A_sort]
    return index, sorted_A

X,Y,A,B,C = LI()
p = LI()
q = LI()
r = LI()

Z = []
for i in range(A):
    Z.append((p[i],0)) # red
for i in range(B):
    Z.append((q[i],1)) # green
for i in range(C):
    Z.append((r[i],2))

Z.sort(key=lambda x:(-x[0],-x[1]))
index = [Z[i][1] for i in range(len(Z))]

R = []
G = []
M = []
Rm = Heapq([])
Gm = Heapq([])
ans = 0

for i in range(X+Y):
    ans += Z[i][0]
    if Z[i][1] == 0:
        R.append(Z[i][0])
        Rm.push(Z[i][0])
    elif Z[i][1] == 1:
        G.append(Z[i][0])
        Gm.push(Z[i][0])
    else:
        M.append(Z[i][0])
        #Rm.push(Z[i][0])
        #Gm.push(Z[i][0])

nr = len(R)
ng = len(G)
nm = len(M)
for i in range(X+Y,A+B+C):
    if ng+nm < Y:
        if Z[i][1] == 0:
            continue
        else:
            v = Rm.pop()
            #Gm.push(Z[i][0])
            ng += 1
            nr -= 1
            ans -= v
            ans += Z[i][0]
    elif nr+nm < X:
        if Z[i][1] == 1:
            continue
        else:
            v = Gm.pop()
            #Rm.push(Z[i][0])
            nr += 1
            ng -= 1
            ans -= v
            ans += Z[i][0]
    else:
        break

print(ans)

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

hp = []
for e in p:
    heappush(hp, [-e, 0])
for e in q:
    heappush(hp, [-e, 1])
for e in r:
    heappush(hp, [-e, 2])

ans = 0
cnt = [0] * 3
sm = 0
mx = [x, y, c]
while hp:
    yum, col = heappop(hp)
    yum = -yum

    if cnt[col] >= mx[col]:
        continue

    ans += yum
    cnt[col] += 1
    sm += 1

    if sm == x + y:
        break

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3

import collections
import heapq


def maximize(x, y, a, b, c, ps0, qs0, rs0):
    init_apples = heapq.nlargest(x, ps0)
    init_apples.extend(heapq.nlargest(y, qs0))
    init_apples.sort()
    all_whites = collections.deque(sorted(rs0))
    total_score = 0
    for v in init_apples:
        if all_whites and all_whites[-1] > v:
            total_score += all_whites.pop()
        else:
            total_score += v
    return total_score


def main():
    x, y, a, b, c = (int(z) for z in input().split())
    ps = [int(p) for p in input().split()]
    qs = [int(q) for q in input().split()]
    rs = [int(r) for r in input().split()]
    ans = maximize(x, y, a, b, c, ps, qs, rs)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from heapq import heappop, heapify, heappush


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()))
D = [-i for i in C]
A.sort(reverse=True)
B.sort(reverse=True)
AB = A[:x] + (B[:y])

heapify(AB)
heapify(D)
ans = 0
for _ in range(c):
    d = -heappop(D)
    ab = heappop(AB)
    if ab > d:
        heappush(AB, ab)
        break
    heappush(AB, d)
ans = sum(AB)
print(ans)

Submission status: Accepted

Solution code:
x, y, a, b ,c = map(int, input().split())
pList = sorted(list(map(int, input().split())), reverse=True)
qList = sorted(list(map(int, input().split())), reverse=True)
rList = sorted(list(map(int, input().split())), reverse=True)

xyList = sorted(pList[:x] + qList[:y]) # 小さい順

for i, r in enumerate(rList):
  if len(xyList) <= i:
    break;
  if xyList[i] > r:
    break;
  xyList[i] = r
print(sum(xyList))

Submission status: Accepted

Solution code:
x,y,a,b,c = map(int,input().split())
A = sorted( list(map(int,input().split())) ,reverse=True)[:x]
B = sorted( list(map(int,input().split())) ,reverse=True)[:y]
C = sorted( list(map(int,input().split())) ,reverse=True)[:x+y]

ans = 0

la = x-1
lb = y-1

for cc in C:
  if la>=0 and lb>=0:
    if A[la]<B[lb]:
      if A[la]< cc:
        A[la] = cc
        la -= 1
    else:
      if B[lb]< cc:
        B[lb] = cc
        lb -= 1
  elif la>=0:
    if A[la]< cc:
      A[la] = cc
      la -= 1
  else:
    if B[lb]< cc:
      B[lb] = cc
      lb -= 1
print(sum(A)+sum(B))

Submission status: Accepted

Solution code:
import bisect
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()
q.sort()
r.sort()
m = A - X
n = B - Y
l = bisect.bisect_right(r, min(p[m], q[n]))
print(sum(list(heapq.merge(p[m:], q[n:], r[l:]))[-(X + Y):]))

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,y,a,b,c = mi()
p = li()
q = li()
r = li()

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


qp = True
qq = True
plef = a - x
qlef = b - y
ans = 0
rlen = len(r) -1
while True:
    if rlen <0:
        break
    
    if plef == a:
        if qlef == b:
            break
        else :
            ima = q[qlef]
            tmp = "q"
    elif qlef == b:
        ima = p[plef]
        tmp = "p"
    elif p[plef] >= q[qlef]:
        ima = q[qlef]
        tmp = "q"
    else:
        ima = p[plef]
        tmp = "p"
    if ima < r[rlen]:
        ans += r[rlen]
        rlen -= 1
        if tmp == "q":
            qlef += 1
        else:
            plef += 1
    
    else:
        break
if plef != a:
    for i in range(plef,a):
        ans += p[i]
if qlef != b:
    for i in range(qlef,b):
        ans +=q[i]
print(ans)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

X, Y, A, B, C = lr()
P = lr(); P.sort(reverse=True)
Q = lr(); Q.sort(reverse=True)
R = lr()
P = P[:X]
Q = Q[:Y]
whole = P + Q + R
whole.sort(reverse=True)
answer = sum(whole[:X+Y])
print(answer)