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

red = p[0:X]
green = q[0:Y]

eat = sorted(red + green, reverse = True)

h = 0
m = min(len(eat), len(r))
for i in range(m):
    if eat[-(i+1)] >= r[i]:
        h = i
        break
    if i == m - 1:
        h = i + 1

if h > 0:
    eat[-h:] = r[:h]

print(sum(eat))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
X, Y, A, B, C = map(int, input().split())
red_oishisa = list(map(int, input().split()))
green_oishisa = list(map(int, input().split()))
no_oishisa = list(map(int, input().split()))

red_oishisa = list(sorted(red_oishisa))[::-1]
green_oishisa = list(sorted(green_oishisa))[::-1]
no_oishisa = list(sorted(no_oishisa))[::-1]

oishisa_s = []

i=1
j=1
k=1
while(True):
    if len(oishisa_s) == X+Y:
        break
    
    hikaku_dict = dict()
    if i <= X:
        hikaku_dict['red'] = red_oishisa[i-1]
    if j <= Y:
        hikaku_dict['green'] = green_oishisa[j-1]
    if k <= C:
        hikaku_dict['no'] = no_oishisa[k-1]
    
    max_key, max_value = max(hikaku_dict.items(), key=lambda x:x[1])
    oishisa_s.append(max_value)
    if max_key == 'red':
        i += 1
    elif max_key == 'green':
        j += 1
    elif max_key == 'no':
        k += 1

print(sum(oishisa_s))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    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()]
    from heapq import heapify, heappop
    heapify(P)
    heapify(Q)
    heapify(R)
    eatP = 0
    eatQ = 0
    eatR = 0
    ans = 0
    while X != eatP and Y != eatQ and eatP + eatQ + eatR < X + Y:
        if P and Q and R:
            cur = min(P[0], Q[0], R[0])
        elif P and Q:
            cur = min(P[0], Q[0])
        elif Q and R:
            cur = min(Q[0], R[0])
        elif R and P:
            cur = min(R[0], P[0])
        elif P:
            cur = P[0]
        elif Q:
            cur = Q[0]
        elif R:
            cur = R[0]
        else:
            break
        ans -= cur
        if R and R[0] == cur:
            heappop(R)
            eatR += 1
        elif P and P[0] == cur:
            heappop(P)
            eatP += 1
        elif Q and Q[0] == cur:
            heappop(Q)
            eatQ += 1

    if eatP + eatQ + eatR == X + Y:
        return print(ans)
    if X != eatP:
        while X != eatP and eatP + eatQ + eatR < X + Y:
            if R and P:
                cur = min(R[0], P[0])
            elif R:
                cur = R[0]
            elif P:
                cur = P[0]
            else:
                break
            ans -= cur
            if R and R[0] == cur:
                heappop(R)
                eatR += 1
            elif P and P[0] == cur:
                heappop(P)
                eatP += 1
    elif Y != eatQ:
        while Y != eatQ and eatP + eatQ + eatR < X + Y:
            if R and Q:
                cur = min(R[0], Q[0])
            elif R:
                cur = R[0]
            elif Q:
                cur = Q[0]
            else:
                break
            ans -= cur
            if R and R[0] == cur:
                heappop(R)
                eatR += 1
            elif Q and Q[0] == cur:
                heappop(Q)
                eatQ += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

X, Y, A, B, C = [int(x) for x in input().split()]
p = sorted([int(x) for x in input().split()], reverse=True)[:X]
q = sorted([int(x) for x in input().split()], reverse=True)[:Y]
pq = deque(sorted(p + q, reverse=True))
r = deque(sorted([int(x) for x in input().split()]))

ans = sum(pq)

for i in range(len(r)):
    if r[-1] > pq[-1]:
        ans += r.pop() - pq.pop()

print(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)[:X]
Q = sorted(list(map(int, input().split())), reverse=True)[:Y]
R = sorted(list(map(int, input().split())), reverse=True)  # 降順=入れたい順

cands = sorted(P + Q)  # 昇順=消して欲しい順

c_ind = 0
r_ind = 0

while True:
    if c_ind >= len(cands) or r_ind >= len(R):
        break
    c = cands[c_ind]
    r = R[r_ind]
    if c >= r:
        break
    else:
        c_ind += 1
        r_ind += 1

print(sum(cands[c_ind:]) + sum(R[:r_ind]))

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
pt=sorted(list(map(int,input().split())),reverse=True)
qt=sorted(list(map(int,input().split())),reverse=True)
r=sorted(list(map(int,input().split())),reverse=True)
p=pt[:x]
q=qt[:y]

prev=sorted(p)
qrev=sorted(q)

#print(p,q,r,prev,qrev)

saisho=[0]*(x+y)

pgen=0
qgen=0
genzai=0
gokei=0
powari=False
for i in range(a+b):
    if(prev[pgen]<qrev[qgen]):
        genzai+=prev[pgen]
        pgen+=1
    else:
        genzai+=qrev[qgen]
        qgen+=1
    saisho[i]=genzai
    if(pgen==x):
        powari=True
        gokei=i+1
        break
    if(qgen==y):
        gokei=i+1
        break

if(powari):
    for i in range(y-qgen):
        genzai+=qrev[qgen+i]
        saisho[gokei+i]=genzai
else:
    for i in range(x-pgen):
        genzai+=prev[pgen+i]
        saisho[gokei+i]=genzai


zenbu=saisho[x+y-1]
#print(saisho)
#print(zenbu)

sikokaisu=min(c,x+y)

ans=[0]*sikokaisu

genc=0
for i in range(sikokaisu):
    genc+=r[i]
    ans[i]=zenbu-saisho[i]+genc

#print(ans)

print(max(zenbu,max(ans)))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
X, Y, A, B, C = map(int, input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]

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

Submission status: Accepted

Solution code:
X, Y, A, B, C = 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の中から大きい順にX個取り出す
sorted_p_list = sorted(p_list, reverse=True)[:X]

# q_listの中から大きい順にY個取り出す
sorted_q_list = sorted(q_list, reverse=True)[:Y]

# これらのリストをくっつけて昇順にする
s_list = sorted(sorted_p_list + sorted_q_list, reverse=False)

# r_listを降順にする
sorted_r_list = sorted(r_list, reverse=True)

for i in range(len(r_list)):
    if sorted_r_list[i] > s_list[i]:
        s_list[i] = sorted_r_list[i]
    else:
        break

print(sum(s_list))

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
G=[[0]*2 for i in range(a+b+c)]
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
for i in range(a):
  G[i][0]=A[i]
  G[i][1]=0
for i in range(b):
  G[i+a][0]=B[i]
  G[i+a][1]=1
for i in range(c):
  G[i+a+b][0]=C[i]
  G[i+a+b][1]=2
GG=sorted(G,reverse=True)
cc=0
ca=0
cb=0
i=-1
ans=0
f=0
ff=0
while ca<x and cb<y and cc<x+y:
  i=i+1
  ans=ans+GG[i][0]
  if GG[i][1]==0:
    ca=ca+1
    cc=cc+1
    if ca==x:
      f=1
  elif GG[i][1]==1:
    cb=cb+1
    cc=cc+1
    if cb==y:
      f=2
  else:
    cc=cc+1
  if cc==x+y:
    ff=1
if ff==1:
  print(ans)
  exit()
if f==1:
  ans=0
  for i in range(x):
    ans=ans+A[i]
  GGG=sorted(G[a:a+b+c],reverse=True)
  for i in range(y):
    ans=ans+GGG[i][0]
  print(ans)
elif f==2:
  ans=0
  for i in range(y):
    ans=ans+B[i]
  GGG=sorted(G[0:a]+G[a+b:a+b+c],reverse=True)
  for i in range(x):
    ans=ans+GGG[i][0]
  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()))
def resolve():
    X, Y, A, B, C = lr()
    p = sorted(lr())[-X:][::-1]
    q = sorted(lr())[-Y:][::-1]
    r = sorted(lr())
    tmp = r.pop()
    pt = p[-1]
    qt = q[-1]
    ext = [0]
    while (tmp > pt or tmp > qt) and len(r) >= 0:
        if pt > qt:
            g = q.pop()
            ext.append(tmp)
            if len(q) == 0:
                qt = pow(10, 9)+1
            else:
                qt = q[-1]
        else:
            g = p.pop()
            ext.append(tmp)
            if len(p) == 0:
                pt = pow(10, 9)+1
            else:
                pt = p[-1]
        if len(r) > 0:
            tmp = r.pop()
        else:
            break
    print(sum(p)+sum(q)+sum(ext))
resolve()

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
rs = list(map(int, input().split()))
ps.sort()
ps = ps[-x:]
qs.sort()
qs = qs[-y:]
pqs = ps + qs
pqs.sort()
rs.sort(reverse=True)

answer = sum(pqs)
for i in range(min(x+y, c)):
    diff = rs[i] - pqs[i]
    if diff < 0:
        break
    answer += diff

print(answer)

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(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)

i_p = 0
i_q = 0
i_r = 0

ans = 0
for _ in range(X + Y):
    cand = []
    if i_p < X: cand.append((p[i_p], 'A'))
    if i_q < Y: cand.append((q[i_q], 'B'))
    if i_r < C: cand.append((r[i_r], 'C'))
    #
    cand.sort(key=lambda x: x[0])
    ans_tmp, color = cand[-1]
    ans += ans_tmp
    # print("#", ans_tmp, color)
    if color == 'A': i_p += 1
    elif color == 'B': i_q += 1
    else: i_r += 1

print(ans)

Submission status: Accepted

Solution code:
# E
X, Y, NA, NB, NC = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())

A = sorted(A)[-X:]
B = sorted(B)[-Y:]
C = sorted(C)[::-1]

ans = sum(A) + sum(B)


i = 0
j = 0
num_A = len(A)
num_B = len(B)
for c in C:
    if i < num_A and j < num_B:
        if A[i] < B[j] and A[i] < c:

            ans += c - A[i]
            i += 1
        elif A[i] >= B[j] and B[j] < c:

            ans += c - B[j]
            j += 1

    elif i < num_A:
        if A[i] < c:
            ans += c - A[i]
            i += 1

    elif j < num_B:
        if B[j] < c:
            ans += c - B[j]
            j += 1

    else:
        break

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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

p_vals = []
ps = map(int, input().split(" "))
for p in ps:
    heappush(p_vals, -p)

q_vals = []
qs = map(int, input().split(" "))
for q in qs:
    heappush(q_vals, -q)

r_vals = []
rs = map(int, input().split(" "))
for r in rs:
    heappush(r_vals, -r)

# large to small
for _ in range(x):
    val = -heappop(p_vals)
    heappush(r_vals, -val)

for _ in range(y):
    val = -heappop(q_vals)
    heappush(r_vals, -val)

#p_vals2 = [-heappop(p_vals) for _ in range(x)]
#q_vals2 = [-heappop(q_vals) for _ in range(y)]

out = 0
for _ in range(x+y):
    val = -heappop(r_vals)
    out += val


print(out)

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()))
# DPなんじゃない？
# DP[i,j]:=i,jまでの中からりんごを選んだときに
# DPにしては，なんかおかしい気がする

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

p_top = P[0:X]
q_top = Q[0:Y]
pq_top = p_top+q_top


pqr_top=pq_top+R
pqr_top.sort(reverse=True)

print(sum(pqr_top[0: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()))

pqr = []
for i in p:
  pqr.append([i, 1])
for i in q:
  pqr.append([i, 2])
for i in r:
  pqr.append([i, 3])

pqr.sort(reverse=True)

red = x
green = y
total = x+y
ans = 0

for i_pqr in pqr:
  if i_pqr[1] == 1 and red > 0 and total > 0:
    red -= 1
    total -= 1
    ans += i_pqr[0]
  elif i_pqr[1] == 2 and green > 0 and total > 0:
    green -= 1
    total -= 1
    ans += i_pqr[0]
  elif i_pqr[1] == 3 and total > 0:
    total -= 1
    ans += i_pqr[0]
  
  if total == 0:
    break

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

P = deque(P)
Q = deque(Q)
R = deque(R)

m = min(P[X-1], Q[Y-1])
while R[0] > m:
    if P[X-1] < Q[Y-1]:
        P.pop()
        P.appendleft(R[0])
        R.popleft()
    else:
        Q.pop()
        Q.appendleft(R[0])
        R.popleft()
    m = min(P[X-1], Q[Y-1])
    if len(R) == 0:
        break
print(sum(P) + sum(Q))

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()))
all_list = []

for p in P:
    all_list.append([-p, "A"])

for q in Q:
    all_list.append([-q, "B"])
for r in R:
    all_list.append([-r, "C"])
heapq.heapify(all_list)

ans = 0
count = 0
X_count = 0
Y_count = 0
while count < X+Y:
    pop = heapq.heappop(all_list)
    if pop[1] == "A" and X_count < X:
        ans -= pop[0]
        X_count += 1
        count += 1
    elif pop[1] == "B" and Y_count < Y:
        ans -= pop[0]
        Y_count += 1
        count += 1
    elif pop[1] == "C":
        ans -= pop[0]
        count += 1
print(ans)

Submission status: Accepted

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

toEat = P[:X] + Q[:Y]
toEat.sort()

for i in range(min(X + Y, len(R))):
    if R[i] > toEat[i]:
        toEat[i] = R[i]
    else:
        break

print(sum(toEat))

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
ans = sum(P[:X]) + sum(Q[:Y])
x = X-1
y = Y-1
c = 0
x_flag = False
y_flag = False
while x >= 0 and y >= 0 and c < C:
    if x_flag == False:
        p = P[x]
    else:
        p = 10**9 + 1
    if y_flag == False:
        q = Q[y]
    else:
        q = 10**9 + 1
    r = R[c]
    if p <= q and p < r:
        ans = ans - p + r
        x -= 1
        c += 1
        if x == -1:
            x = 0
            x_flag = True
    elif q < p and q < r:
        ans = ans - q + r
        y -= 1
        c += 1
        if y == -1:
            y = 0
            y_flag = True
    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)
Sum = 0
for i in range(X):
  Sum += p[i]
for j in range(Y):
  Sum += q[j]
cntp, cntq = X-1, Y-1
flg = 0

for i in range(C):
  dp, dq = 0, 0
  if cntp >= 0:
    if r[i] > p[cntp]:
      dp = r[i] - p[cntp]
  if cntq >= 0:
    if r[i] > q[cntq]:
      dq = r[i] - q[cntq]
  if dp == 0 and dq == 0:
    break
  else:
    if dp >= dq:
      Sum += dp
      cntp -= 1
    else:
      Sum += dq
      cntq -= 1
print(Sum)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
p.sort()
q.sort()
r.sort()
red_remain = X
green_remain = Y
ans = 0
flag = False
for i in range(X+Y):
    if flag:
        flag = False
        continue
    if red_remain == 0 or len(p) == 0:
        if green_remain == 0 or len(q) == 0:
            ans += r.pop(-1)
        elif len(r) == 0:
            ans += q.pop(-1)
            green_remain -= 1
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
        else:
            ans += q.pop(-1)
            green_remain -= 1
    elif green_remain == 0 or len(q) == 0:
        if len(r) == 0:
            ans += p.pop(-1)
            red_remain -= 1
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
        else:
            ans += p.pop(-1)
            red_remain -= 1
    elif len(r) == 0:
        if p[-1] == q[-1]:
            if i == X+Y-1:
                ans += p[-1]
            else:
                flag = True
                ans += p.pop(-1) + q.pop(-1)
                red_remain -= 1
                green_remain -= 1
        elif p[-1] < q[-1]:
            ans += q.pop(-1)
            green_remain -= 1
        else:
            ans += p.pop((-1))
            red_remain -= 1
    else:
        if p[-1] < r[-1] and q[-1] < r[-1]:
            ans += r.pop(-1)
        elif p[-1] == q[-1]:
            if i == X+Y-1:
                ans += p[-1]
            else:
                flag = True
                ans += p.pop(-1) + q.pop(-1)
                red_remain -= 1
                green_remain -= 1
        elif p[-1] < q[-1]:
            ans += q.pop(-1)
            green_remain -= 1
        else:
            ans += p.pop((-1))
            red_remain -= 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)
q = sorted(list(map(int, input().split())), reverse=True)
r = list(map(int, input().split()))

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

h = sorted(Red + Green)
heapq.heapify(h)
for i in r:
    heapq.heappushpop(h, i)
print(sum(h))

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p_s = sorted(list(map(int, input().split())), reverse=True)[:x]
q_s = sorted(list(map(int, input().split())), reverse=True)[:y]
r_s = sorted(list(map(int, input().split())), reverse=True)

s_s = sorted(p_s + q_s)
ans = sum(s_s)
for i in range(c):
    tmp = ans - s_s[i] + r_s[i]
    if tmp <= ans:
       break
    ans = tmp

print(ans)

Submission status: Accepted

Solution code:
from heapq import heapify, heappop, heappush

x, y, a, b, c = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
PQ = P[:x] + Q[:y]
heapify(PQ)

for r in R:
  pq = heappop(PQ)
  if pq < r:
    heappush(PQ, r)
  else:
    heappush(PQ, pq)
ans = sum(PQ)
print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
pqr = [map(int, input().split()) for i in range(3)]

A = []
for i in range(3):
        for j in pqr[i]:
                A.append([j, i])
A.sort(key=lambda x:-x[0])

ans = 0
cnt = 0
for v, t in A:
        if cnt==x+y: break
        if t==0:
                if x:
                        x -= 1
                        ans += v
        elif t==1:
                if y:
                        y -= 1
                        ans += v
        else:
                if x+y>cnt:
                        cnt += 1
                        ans += v
                else: break
print(ans)

Submission status: Accepted

Solution code:
import heapq as hq

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

pq1 = []; hq.heapify(pq1) # 昇順
for i in range(X):
    hq.heappush(pq1, p[i])
for i in range(Y):
    hq.heappush(pq1, q[i])
pq2 = []; hq.heapify(pq2) # 降順で用いる
for i in range(C):
    hq.heappush(pq2, -r[i])
while (len(pq2) and pq1[0] < -pq2[0]):
    hq.heappush(pq1, -hq.heappop(pq2))
    hq.heappop(pq1) # 順序を変えてみた
print(sum(pq1))

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 = [(p, 0) for p in P] + [(q, 1) for q in Q] + [(r, 2) for r in R]
S.sort(reverse=True)

ans = 0
ct = [0] * 3
for s in S:
    if sum(ct) >= X+Y:
        break
    elif s[1] == 0 and ct[0] < X:
        ans += s[0]
        ct[0] += 1
    elif s[1] == 1 and ct[1] < Y:
        ans += s[0]
        ct[1] += 1
    elif s[1] == 2 and (ct[0] < X or ct[1] < Y):
        ans += s[0]
        ct[2] += 1

print(ans)

Submission status: Accepted

Solution code:
x,y,A,B,C=map(int,input().split())
pa=list(map(int,input().split()))
qb=list(map(int,input().split()))
rc=list(map(int,input().split()))
pa.sort()
qb.sort()
temp=0

pa=pa[-x:]
qb=qb[-y:]
paa=pa+rc
paa.sort()
paa1=paa[-x:]
paa2=paa[:-x]
qbb=qb+paa2
qbb.sort()
qbb1=qbb[-y:]
print(sum(paa1)+sum(qbb1))

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()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort()
p = deque(p[:X][::-1])
q = deque(q[:Y][::-1])
ans = 0
while X + Y > len(p) + len(q):
    ans += r.pop()
while r:
    R = r.pop()
    if p[0] < q[0]:
        if p[0] < R:
            p.popleft()
            p.append(R)
        else:
            break
    else:
        if q[0] < R:
            q.popleft()
            q.append(R)
        else:
            break
ans += sum(p) + sum(q)
print(ans)

Submission status: Accepted

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

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()
    q.sort()
    r.sort()
    p = p[-X:]
    q = q[-Y:]
    r = r[-X-Y:]
    use = p+q
    heapq.heapify(use)
    heapq.heapify(r)
    while len(r)>0:
        unt = heapq.heappop(use)
        nc = heapq.heappop(r)
        if nc > unt:
            heapq.heappush(use,nc)
        else:
            heapq.heappush(use,unt)

    print(sum(use))
    
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)

total = p[:x]+q[:y]
total.sort(reverse=True)

#print(p, q, total)

for i in range(min(x+y, c)):
  if total[-(i+1)]<r[i]:
    total[-(i+1)] = r[i]
  else:
    break

print(sum(total))

Submission status: Accepted

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

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

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

pi = 0
qi = 0

for rv in r :
  if 0 <= pi < X and 0 <= qi < Y :
    if p[pi] <= q[qi] and p[pi] < rv :
      p[pi] = rv
      pi += 1
      continue
    if q[qi] <= p[pi] and q[qi] < rv : 
      q[qi] = rv
      qi += 1
      continue
  
  if 0 <= pi < X and p[pi] < rv :
    p[pi] = rv
    pi += 1
    continue
    
  if 0 <= qi < Y and q[qi] < rv :
    q[qi] = rv
    qi += 1
    continue
    
print(sum(p[:X]) + sum(q[:Y]))

Submission status: Accepted

Solution code:
from collections import deque


def main():
    X, Y, A, B, C = map(int, input().split(' '))
    a_list = list(map(int, input().split(' ')))
    b_list = list(map(int, input().split(' ')))
    c_list = list(map(int, input().split(' ')))
    a_deq = deque(sorted(a_list, reverse=True)[:X])
    b_deq = deque(sorted(b_list, reverse=True)[:Y])
    c_deq = deque(sorted(c_list, reverse=True))
    result = sum(a_deq) + sum(b_deq)
    while len(c_deq) > 0:
        try:
            if a_deq[-1] < b_deq[-1]:
                min_ab = a_deq.pop()
            else:
                min_ab = b_deq.pop()
        except IndexError:
            if len(a_deq) > 0:
                min_ab = a_deq.pop()
            elif len(b_deq) > 0:
                min_ab = b_deq.pop()
            else:
                break

        if min_ab >= c_deq[0]:
            break
        result += c_deq.popleft() - min_ab

    print(result)


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)

ep = p[:X][::-1]
eq = q[:Y][::-1]
ans = sum(ep)+sum(eq)

ip = 0
iq = 0
for pr in r:
    if ip >= X:
        if iq >= Y:
            break
        elif eq[iq] < pr:
            ans += -eq[iq] + pr
            iq += 1
        else:
            break
    elif iq >= Y:
        if ep[ip] < pr:
            ans += -ep[ip] + pr
            ip += 1
        else:
            break
    else:
        if ep[ip] < eq[iq]:
            if pr > ep[ip]:
                ans += -ep[ip] + pr
                ip += 1
            else:
                break
        else:
            if pr > eq[iq]:
                ans += -eq[iq] + pr
                iq += 1
            else:
                break
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)
ai=0
bi=0
ci=0
ans=0
for i in range(x+y):
    if ai<x:
        if bi<y:
            if ci<c:
                m=max(p[ai],q[bi],r[ci])
                ans+=m
                if p[ai]==m:
                    ai+=1
                elif q[bi]==m:
                    bi+=1
                elif r[ci]==m:
                    ci+=1
            else:
                m=max(p[ai],q[bi])
                ans+=m
                if p[ai]==m:
                    ai+=1
                elif q[bi]==m:
                    bi+=1
        else:
            if ci<c:
                m=max(p[ai],r[ci])
                ans+=m
                if p[ai]==m:
                    ai+=1
                elif r[ci]==m:
                    ci+=1
            else:
                m=p[ai]
                ans+=m
                if p[ai]==m:
                    ai+=1
    else:
        if bi<y:
            if ci<c:
                m=max(q[bi],r[ci])
                ans+=m
                if q[bi]==m:
                    bi+=1
                elif r[ci]==m:
                    ci+=1
            else:
                m=q[bi]
                ans+=m
                if q[bi]==m:
                    bi+=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()
q.sort()
r.sort()

p.reverse()
q.reverse()
r.reverse()

qi = 0
ri = 0

lis = [0]

for i in range(B+C):

    if (qi == Y or qi == B) and ri==C:
        lis.append(float("-inf"))
    elif qi == Y or qi == B:
        lis.append(lis[-1] + r[ri])
        ri += 1
    elif ri == C:
        lis.append(lis[-1] + q[qi])
        qi += 1
    elif q[qi] > r[ri]:
        lis.append(lis[-1] + q[qi])
        qi += 1
    else:
        lis.append(lis[-1] + r[ri])
        ri += 1

#print (lis)
ans = 0

n = 0
for i in range(X+1):

    if i != 0:
        n += p[i-1]

    if len(lis) > X+Y-i:
        ans = max(ans , n + lis[X+Y-i])

print (ans)

Submission status: Accepted

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

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

if len(list)<x+y:
  ll=len(list)
  for i in range((x+y)-len(list)):
    list.append(r[i])
  r=r[(x+y)-ll:]
list=sorted(list)

for i in range(len(r)):
  if list[i]<r[i]:
    list[i]=r[i]
  else:
    break
print(sum(list))

Submission status: Accepted

Solution code:
x,y,a,b,c = [int(c) for c in input().split()]
ps = [int(c) for c in input().split()]
qs = [int(c) for c in input().split()]
rs = [int(c) for c in input().split()]

ps.sort(reverse=True)
qs.sort(reverse=True)
rs.sort(reverse=True)

pind = x-1
qind = y-1
rind = 0
for i in range(c+1):
    p = ps[pind]
    q = qs[qind]
    r = rs[rind]
    if min(p,q)>= r:
        break
    if p <= q:
        ps[pind] = r
        pind -= 1
    else:
        qs[qind] = r
        qind -= 1
    rind += 1
    if rind >= c:
        break
    pind = max(pind,0)
    qind = max(qind,0)
print(sum(ps[:x]) + sum(qs[:y]))

Submission status: Accepted

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

arra.sort()
arrb.sort()
arrc.sort()

temp = arra[a-x:]+arrb[b-y:]
#print(temp)

if x+y<c:
  	temp +=arrc[c-x-y:]
else:
  	temp +=arrc
    
#print(temp)
temp.sort()

arr = temp[len(temp)-x-y:]
#print(arr)
print(sum(arr))