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

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    X, Y, A, B, C = [int(x) for x in 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)
    R.sort(reverse=True)

    xcnt = 0
    ycnt = 0
    rcnt = 0
    ans = 0

    for r in R:
        p = float("inf")
        if X - 1 - xcnt >= 0:
            p = P[X - 1 - xcnt]
        q = float("inf")
        if Y - 1 - ycnt >= 0:
            q = Q[Y - 1 - ycnt]
        if p >= r and q >= r:
            break

        if (r - p) > 0 and r - p > r - q:
            xcnt += 1
            rcnt += 1
            ans += r
        else:
            ycnt += 1
            rcnt += 1
            ans += r

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in p1:
  r.append(i)
for i in q1:
  r.append(i)
r.sort()
r.reverse()
ans=0
for i in range(x+y):
  ans+=r[i]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import heapq

H = []

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

ps = list(map(int,input().split()))
ps.sort()

qs = list(map(int, input().split()))
qs.sort()

rs = list(map(int, input().split()))
rs.sort()

ans = np.zeros(1, dtype=np.uint64)

for i in range(x):
    heapq.heappush(H, ps[-(i+1)])
    ans += ps[-(i+1)]

for i in range(y):
    heapq.heappush(H, qs[-(i+1)])
    ans += qs[-(i+1)]

for i in range(min(c, (x+y))):
    minimum = heapq.heappop(H)
    if minimum > rs[-(i+1)]:
        break
    else:
        ans = ans - minimum + rs[-(i+1)]

print(ans[0])

Submission status: Accepted

Solution code:
import heapq

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())))
pp=p[a-x:a]
qq=q[b-y:b]
heapq.heapify(pp)
heapq.heapify(qq)
for i in range(c):
     s=heapq.heappop(pp)
     t=heapq.heappop(qq)
     if r[c-i-1] > s or r[c-i-1] > t:
          if s >= t:
               heapq.heappush(pp, s)
               heapq.heappush(qq, r[c-i-1])
          else:
               heapq.heappush(qq, t)
               heapq.heappush(pp, r[c-i-1])


     else:
          heapq.heappush(pp, s)
          heapq.heappush(qq, t)
          break
print(sum(pp)+sum(qq))

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
aka,ao,musyoku=[sorted(list(map(int,input().split())),reverse=1) for i in range(3)]
ans=0
A,B,C=0,0,0
while C+A+B<x+y and x>A and y>B and c>C:
    q,w,e=aka[A],ao[B],musyoku[C]
    ans+=max(q,w,e)
    if max(q,w,e)==e:C+=1
    elif q>w:A+=1
    else:B+=1
if C+A+B==x+y:print(ans);exit()
  
if x<=A:
    cnt=x+y-A-B-C
    CC=y-B
    while cnt and CC:
        ans+=max(ao[B],musyoku[C])
        if max(ao[B],musyoku[C])==ao[B]:B+=1
        else:C+=1;CC-=1
        cnt-=1
    if cnt:
        ans+=sum(ao[B:B+cnt]) 
elif y<=B:
    cnt=x+y-A-B-C
    CC=x-A
    while cnt and CC:
        ans+=max(aka[A],musyoku[C])
        if max(aka[A],musyoku[C])==aka[A]:
            A+=1
        else:C+=1;CC-=1
        cnt-=1
    if cnt :
        ans+=sum(aka[A:A+cnt])
else:
    cnt=x+y-A-B-C
    while cnt:
        ans+=max(aka[A],ao[B])
        if max(aka[A],ao[B])==aka[A]:A+=1
        else:B+=1
        cnt-=1

print(ans)

Submission status: Accepted

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

def main():
     x, y, a, b, c = map(int, input().split())

     # A個の赤いりんごの美味しさ
     p = list(map(int, input().split()))
     # B個の緑のりんごの美味しさ
     q = list(map(int, input().split()))
     # C個の無色のりんごの美味しさ
     r = list(map(int, input().split()))

     p = sorted(p)
     q = sorted(q)
     r = sorted(r, reverse=True)

     p_cut = p[-x:]
     q_cut = q[-y:]

     # print(p_cut)
     # print(q_cut)

     # 無色を1つも使わない場合をベースにする
     red_del = sum(p_cut)
     gre_del = sum(q_cut)
     base_ans = red_del + gre_del

     red_eat = 0
     gre_eat = 0
     cle_eat = 0
     # 交換できる無色を計算
     for i in range(c):
          if red_eat < x and gre_eat < y:
               if p_cut[red_eat] < q_cut[gre_eat]:
                    change = p_cut[red_eat]
                    if change < r[cle_eat]:
                         cle_eat += 1
                         red_eat += 1
                    else:
                         break
               else:
                    change = q_cut[gre_eat]
                    if change < r[cle_eat]:
                         cle_eat += 1
                         gre_eat += 1
                    else:
                         break
          elif red_eat >= x and gre_eat < y:
               change = q_cut[gre_eat]
               if change < r[cle_eat]:
                    cle_eat += 1
                    gre_eat += 1
               else:
                    break
          elif red_eat < x and gre_eat >= y:
               change = p_cut[red_eat]
               if change < r[cle_eat]:
                    cle_eat += 1
                    red_eat += 1
               else:
                    break
          else:
               break

     ans = sum(p_cut[red_eat:]) + sum(q_cut[gre_eat:]) + sum(r[:cle_eat])
     print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
X, Y, A, B, C = [int(str) for str in input().strip().split()]
ps = [int(str) for str in input().strip().split()]
qs = [int(str) for str in input().strip().split()]
rs = [int(str) for str in input().strip().split()]

def sums(li):
    ret = [0]
    for i in range(len(li)):
        ret.append(ret[-1] + li[i])
    return ret

def f(n):
    return red_and_green_sums[X + Y - n] + r_sums[n]

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

r_sums = sums(rs)

red_and_green = ps[:X] + qs[:Y]
red_and_green.sort(reverse=True)
red_and_green_sums = sums(red_and_green)

# print([f(i) for i in range(C)])
print(max([f(i) for i in range(min(X + Y, C) + 1)]))

Submission status: Accepted

Solution code:
def main():
    import sys
    import numpy as np

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

    p = sorted(p,reverse=True)
    q = sorted(q,reverse=True)
    r = sorted(r,reverse=True)

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

    i = max(r)
    j = sum(x > i for x in p)
    k = sum(x > i for x in q)

    pd = p[j:]
    qd = q[k:]

    m = (x-j) + (y-k)

    rem = sorted(pd + qd + r,reverse=True)
    ans = sum(p[:j] + q[:k]) + sum(rem[:m])
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

i=-X
j=-Y
k=0
deru=0
while deru==0 and k<C and (r[k]>p[i] or r[k]>q[j]):
  if p[i]>q[j] and j<0:
    j+=1
    k+=1
  elif p[i]>q[j] and j>=0 and i<0 and p[i]<r[k]:
    i+=1
    k+=1
  elif p[i]<=q[j] and i<0:
    i+=1
    k+=1
  elif p[i]<=q[j] and i>=0 and j<0 and q[j]<r[k]:
    j+=1
    k+=1
  else:
    deru=1
    
if i==0:
  a=0
else:
  a=sum(p[i:])
  
if j==0:
  b=0
else:
  b=sum(q[j:])
  
if k==0:
  c=0
else:
  c=sum(r[:k])
  
print(a+b+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()
q.sort()
r.sort()
red = []
green = []
less = []
for i in range(x):
    red.append(p[-i-1])
for i in range(y):
    green.append(q[-i-1])
# print(red, green)
for i in range(c):
    if len(red) > 0 and len(green) > 0:
        if red[-1] < green[-1]:
            if red[-1] < r[-i-1]:
                del red[-1]
                less.append(r[-i-1])
        else:
            if green[-1] <= r[-i-1]:
                del green[-1]
                less.append(r[-i-1])
    elif len(red) > 0:
        if red[-1] < r[-i-1]:
            del red[-1]
            less.append(r[-i-1])
    elif len(green) > 0:
        if green[-1] <= r[-i-1]:
            del green[-1]
            less.append(r[-i-1])
ans = sum(red) + sum(green) + sum(less)
# print(red, green, less)
print(ans)

Submission status: Accepted

Solution code:
import heapq


X, Y, A, B, C = map(lambda x: int(x), input().split(" "))
Ps = sorted(list(map(lambda x: int(x), input().split(" "))), reverse=True)
Qs = sorted(list(map(lambda x: int(x), input().split(" "))), reverse=True)
Rs = sorted(list(map(lambda x: int(x), input().split(" "))), reverse=True)

results = []
results.extend(Ps[:X])
results.extend(Qs[:Y])
results.sort()

heapq.heapify(results)
for r in Rs:
    a = heapq.heappop(results)
    if a >= r:
        heapq.heappush(results, a)
        break
    else:
        heapq.heappush(results, r)

print(sum(results))

Submission status: Accepted

Solution code:
x ,y , A , B ,C = map(int , input().split())

p = list(map(int , input().split()))

q = list(map(int , input().split()))

r = list(map(int , input().split()))

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

answer = sum(p[:x]) + sum(q[:y])

i = x-1
j = y-1

for k in range(C):
	
	if (i >= 0) and (j >= 0) :
		if r[k] > min(p[i],q[j]):
			answer += r[k]
			
			if p[i] < q[j]:
				answer -= p[i]
				i -= 1
		
			else:
				answer -= q[j]
				j -= 1
				
		else:
			break
				
	elif (i >= 0) and (j<0):
		if r[k] > p[i]:
			answer += r[k]
			answer -= p[i]
			i -= 1
		
		else:
			break
	
	elif (j >= 0) and (i < 0):
		if r[k] > q[j]:
			answer += r[k]
			answer -= q[j]
			j -= 1
		
		else:
			break
	

	else:
		break
		
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

X,Y,A,B,C=list(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 = np.sort(p)[-X:]
q_sort = np.sort(q)[-Y:]
pq_xy=np.concatenate([p_sort, q_sort], 0)
pqr=np.concatenate([r, pq_xy], 0)
pqr_sort = np.sort(pqr)[-X-Y:]
print(np.sum(pqr_sort))

Submission status: Accepted

Solution code:
x, y, na, nb, nc = map(int, input().split()) 
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
a = a[:x]
b = b[:y]

ai = len(a) - 1
bi = len(b) - 1

for i in c:
    if a[ai] < b[bi]:
        if i > a[ai] and ai >= 0:
            a[ai] = i
            ai -= 1
        elif i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        else:
            break
    else:
        if i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        elif i > a[ai] and ai >= 0:
            a[ai] = i
            ai -= 1
        else:
            break
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()))
Pl = 0
Ql = 0
Rb = 0

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

P = P[0:x]
Q = Q[0:y]

#while (P[-1 - Pl] < R[0 + Rb] or Q[-1 - Ql] < R[0 + Rb]):
#while (P[-1 - Pl] < R[0 + Rb] or Q[-1 - Ql] < R[0 + Rb]) and (Pl != len(P) or Ql != len(Q)):
while Pl != len(P) or Ql != len(Q):
    if Pl == len(P):
        if Q[-1 - Ql] < R[0 + Rb]:
            Q[-1 - Ql] = R[0 + Rb]
            Ql = Ql + 1
        else:
            break
    elif Ql == len(Q):
        if P[-1 - Pl] < R[0 + Rb]:
            P[-1 - Pl] = R[0 + Rb]
            Pl = Pl + 1
        else:
            break

    elif Q[-1 - Ql] >= P[-1 - Pl] and P[-1 - Pl] < R[0 + Rb]:
        P[-1 - Pl] = R[0 + Rb]
        Pl = Pl + 1
    elif P[-1 - Pl] > Q[-1 - Ql] and Q[-1 - Ql] < R[0 + Rb]:
        Q[-1 - Ql] = R[0 + Rb]
        Ql = Ql + 1

    else:
        break

    Rb = Rb + 1
    if Rb == len(R):
        break

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

Submission status: Accepted

Solution code:
x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
p.reverse()
q.sort()
q.reverse()
r.sort()
ans = 0
ele = []
for i in range(x):
    ele.append(p[i])
for i in range(y):
    ele.append(q[i])
for i in range(c):
    ele.append(r[i])
ele.sort()
ele.reverse()
"""a1 = x-1
b1 = y-1
a2 = True
b2 = True
for i in range(c):
    if r[i] > p[a1] and p[a1] <= q[b1] and a2:
        ans += r[i] - p[a1]
        a1 -= 1
        if a1 < 0:
            a2 = False
    elif r[i] > q[b1] and q[b1] < p[a1] and b2:
        ans += r[i] - q[b1]
        b1 -= 1
        if b1 < 0:
            b2 = False"""
for i in range(x+y):
    ans += ele[i]
print(ans)

Submission status: Accepted

Solution code:

import heapq


def submit():
    x, y, a, b, c = map(int, input().split())
    reds = list(map(int, input().split()))
    greens = list(map(int, input().split()))
    uncs = list(map(int, input().split()))

    # 赤リンゴ、緑リンゴについて、大きい順にX, Y個を先に確保しヒープにする
    reds.sort(reverse=True)
    reds = reds[:x]
    heapq.heapify(reds)
    greens.sort(reverse=True)
    greens = greens[:y]
    heapq.heapify(greens)

    # uncolorsについて、大きい順にヒープにしたいのでマイナスにしてからヒープにする
    uncs = [-u for u in uncs]
    heapq.heapify(uncs)

    # 赤リンゴと緑リンゴの数を変えないように、
    # それぞれの最小を取得し、uncから最大を取得し、片方をswapする
    while True:
        if not uncs:
            break

        unc = -heapq.heappop(uncs)
        if unc <= reds[0] and unc <= greens[0]:
            break
        
        if reds[0] < greens[0]:
            heapq.heapreplace(reds, unc)
        else:
            heapq.heapreplace(greens, unc)
            
    print(sum(reds) + sum(greens))

    
submit()

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))#aこ
q=list(map(int,input().split()))#bko
r=list(map(int,input().split()))#cko



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



ac=x-1
bc=y-1
for i in range(c):
    #print("DFGH")
    if p[ac]<q[bc] and p[ac]<r[i]:
        p[ac]=r[i]
        ac=max(0,ac-1)
    
    elif p[ac] >= q[bc] and q[bc]<r[i]:
        q[bc]=r[i]
        bc=max(0,bc-1)


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())
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]
t = []
for i in p: t.append(i)
for i in q: t.append(i)
t.sort()
it = 0
for i in range(x + y):
    if it == c: break
    if t[i] < r[it]:
        t[i] = r[it]
        it -= -1
print(sum(t))

Submission status: Accepted

Solution code:
inputs = open(0).readlines()
x, y, a, b, c = map(int, inputs[0].split())
*P, = map(int, inputs[1].split())
*Q, = map(int, inputs[2].split())
*R, = map(int, inputs[3].split())
P.sort(reverse=1)
Q.sort(reverse=1)
R.sort(reverse=1)
P, Q, R = P[:x][::-1], Q[:y][::-1], R[:x+y]

pi = qi = 0
ri = 0
while ri < len(R):
    if pi < x and qi < y: 
        if P[pi] < Q[qi]:
            if P[pi] < R[ri]:
                P[pi] = R[ri]
                pi += 1
                ri += 1
            else:
                break
        else:
            if Q[qi] < R[ri]:
                Q[qi] = R[ri]
                Q
                qi += 1
                ri += 1
            else:
                break
    elif pi < x and qi == y:
        if P[pi] < R[ri]:
            P[pi] = R[ri]
            pi += 1
            ri += 1
        else:
            break
    elif pi == x and qi < y:
        if Q[qi] < R[ri]:
            Q[qi] = R[ri]
            qi += 1
            ri += 1
        else:
            break
    else:
        raise ValueError('Error')
print(sum(P)+sum(Q))

Submission status: Accepted

Solution code:
import heapq
import sys
from itertools import accumulate
from collections import defaultdict

input = sys.stdin.readline

def main():
    X, Y, A, B, C = map(int, input().split())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    R = list(map(int, input().split()))
    ans = 0
    P_d = defaultdict(int)
    Q_d = defaultdict(int)
    R_d = defaultdict(int)

    for i in range(len(P)):
        P[i] = -P[i]
        P_d[P[i]] += 1
        
    for i in range(len(Q)):
        Q[i] = -Q[i]
        Q_d[Q[i]] += 1

    
    for i in range(len(R)):
        R[i] = -R[i]
        R_d[R[i]] += 1

    H = P + Q + R
    heapq.heapify(H)
    red = 0
    green = 0
    nocl = 0
    ans = 0
    for i in range(A + B + C):
        tmp = heapq.heappop(H)
        if red + green + nocl == X + Y:
            print(-ans)
            exit()

        if P_d[tmp] >= 1 and red < X:
            P_d[tmp] -= 1
            red += 1
            ans += tmp
        elif Q_d[tmp] >= 1 and green < Y:
            Q_d[tmp] -= 1
            green += 1
            ans += tmp
        elif R_d[tmp] >= 1:
            R_d[tmp] -= 1
            nocl += 1
            ans += tmp
    





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n) 

def SS(L):
    S = [0] * (len(L))
    for i, l in enumerate(L):
        S[i + 1] = S[i] + l

    return S


def main():
    X, Y, A, B, C = read_values()
    P = sorted(read_list(), reverse=True) + [-1]
    Q = sorted(read_list(), reverse=True) + [-1]
    R = sorted(read_list(), reverse=True) + [-1]

    x = 0
    y = 0
    r = 0

    res = 0
    for _ in range(X + Y):
        a = P[x]
        b = Q[y]
        c = R[r]

        if c >= a and c >= b:
            res += c
            r += 1
            continue
        
        if x == X:
            if c >= b:
                res += c
                r += 1
            else:
                res += b
                y += 1
            continue

        if y == Y:
            if c >= a:
                res += c
                r += 1
            else:
                res += a
                x += 1
            continue

        if a >= b:
            res += a
            x += 1
        else:
            res += b
            y += 1

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

Submission status: Accepted

Solution code:

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

A.sort()
B.sort()
C.sort(reverse=True)

X = A[len(A)-x:]
Y = B[len(B)-y:]

#print(X,Y,C)

xi = 0
yi = 0
ci = 0
for ci in range(c):
    #print(ci,xi,yi)
    if xi < len(X) and yi < len(Y):
        if C[ci] <= X[xi] and C[ci] <= Y[yi]:
            break
        
        if X[xi] < Y[yi]:
            if C[ci] > X[xi]:
                X[xi] = C[ci]
                xi = xi + 1
        else:
            if C[ci] > Y[yi]:
                Y[yi] = C[ci]
                yi = yi + 1
                
    elif xi < len(X):
        if C[ci] <= X[xi]:
            break
        
        if C[ci] > X[xi]:
            X[xi] = C[ci]
            xi = xi + 1
        
    elif yi < len(Y):
        if C[ci] <= Y[yi]:
            break
        
        if C[ci] > Y[yi]:
            Y[yi] = C[ci]
            yi = yi + 1
            
    else:
        break

print(sum(X)+sum(Y))

Submission status: Accepted

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

red = sorted(red, reverse=True)[:X]
green = sorted(green, reverse=True)[:Y]
no = sorted(no, reverse = True)[:X+Y]

co = sorted(red+green)

for i in range(len(no)):
    if no[i]>co[i]:
        co[i] = no[i]
    else:
        break
        
print(sum(co))

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)

PQ = [(p, 0) for p in P[:X]] + [(q, 1) for q in Q[:Y]]
PQ = sorted(PQ)

y = 0
for v, t in PQ:
    y += v

ans = y
r = 0
n = [X, Y]
for v, t in PQ:
    if n[t]==0: continue
    if r<C and v<R[r]:
       y += R[r]-v
       n[t] -= 1
       ans = max(ans, y)
       r += 1
       
print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
P = [-1] + sorted(list(map(int, input().split())))
Q = [-1] + sorted(list(map(int, input().split())))
R = [-1] + sorted(list(map(int, input().split())))

ans = 0
r = 0
N = X+Y
while N > 0:
  max_V = max(P[-1], Q[-1], R[-1])
  if P[-1] == max_V:
    P.pop(-1)
    if X < 1:
      continue
    X -= 1
  elif Q[-1] == max_V:
    Q.pop(-1)
    if Y < 1:
      continue
    Y -= 1
  else:
    R.pop(-1)
    r += 1
    
  ans += max_V
  N = X+Y-r
  
print(ans)

Submission status: Accepted

Solution code:
def main():
    x,y,a,b,c=map(int,input().split())
    P=list(map(int,input().split()))
    P.sort(reverse=True)
    Q=list(map(int,input().split()))
    Q.sort(reverse=True)
    R=list(map(int,input().split()))
    R.sort(reverse=True)
    PQ=P[:x]+Q[:y]
    ans=sum(PQ)
    PQ.sort()
    r=0
    for pq in PQ:
        if r>=c:
            break
        else:
            if pq<R[r]:
                ans-=pq
                ans+=R[r]
                r+=1
    print(ans)
    
if __name__=='__main__':
    main()

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 = sorted(p)
q = sorted(q)
r = sorted(r)
x_ = a-x
y_ = b-y
flag = 0
ans = sum(p[x_:]) + sum(q[y_:])
for i in range(c-1, -1, -1):
    if p[x_] > q[y_]:
        if q[y_] < r[i]:
            ans += r[i] - q[y_]
            y_ += 1
            if y_>=b:
                flag = 1
                break
        else:
            break
    else:
        if p[x_] < r[i]:
            ans += r[i] - p[x_]
            x_ += 1
            if x_>=a:
                flag = 2
                break
        else:
            break
if flag == 1:
    for j in range(i-1, -1, -1):
        if x_>=a:
            break
        if p[x_] < r[j]:
            ans += r[j] - p[x_]
            x_ += 1
        else:
            break
elif flag == 2:
    for j in range(i-1, -1, -1):
        if y_>=b:
            break
        if q[y_] < r[j]:
            ans += r[j] - q[y_]
            y_ += 1
        else:
            break
            
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
X,Y,A,B,C=map(int,input().split())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
R=list(map(int,input().split()))

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

EP=P[0:X]
EQ=Q[0:Y]

    # for i in range(X):
    #     EP.append(P[i])
        
    # for i in range(Y):
    #     EQ.append(Q[i])

EP.reverse()
EQ.reverse()

ans=sum(EP)+sum(EQ)

p=0
q=0

INF=10**12

EP.append(INF)
EQ.append(INF)

for i in range(C):
    if R[i]>EP[p] or R[i]>EQ[q]:
        if EP[p]<EQ[q]:
            ans+=R[i]
            ans-=EP[p]
            p+=1
        else:
            ans+=R[i]
            ans-=EQ[q]
            q+=1
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
import heapq

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

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

que = []
for i in range(x):
    heapq.heappush(que, p[i])

for i in range(y):
    heapq.heappush(que, q[i])

for i in range(c):
    if que[0] > r[i]:
        break
    heapq.heappop(que)
    heapq.heappush(que, r[i])

print(sum(que))

Submission status: Accepted

Solution code:
#500
X, Y, A, B, C = map(int,input().split())
p0 = list(map(int,input().split()))
q0 = list(map(int,input().split()))
r0 = list(map(int,input().split()))
p = sorted(p0, reverse = True); p.append(0)
q = sorted(q0, reverse = True); q.append(0)
r = sorted(r0, reverse = True); r.append(0)

happy = 0
i = 0
j = 0
k = 0
red = 0
green = 0
white = 0

for _ in range(X+Y):
    if max(p[i], q[j], r[k]) == r[k]:
        happy += r[k]
        white += 1
        k += 1
    elif max(p[i], q[j], r[k]) == p[i]:
        if red < X:
            happy += p[i]
            red += 1
            i += 1
        elif q[j] >r[k] and green < Y:
            happy += q[j]
            green += 1
            j += 1
        else:
            happy += r[k]
            white += 1
            k += 1
    else:
        if green < Y:
            happy += q[j]
            green += 1
            j += 1
        elif p[i] >r[k] and red < X:
            happy += p[i]
            red += 1
            i += 1
        else:
            happy += r[k]
            white += 1
            k += 1
            
print(happy)

Submission status: Accepted

Solution code:
x,y,a,b,c,=map(int,input().split())
red=sorted(list(map(int,input().split())),reverse=True)
green=sorted(list(map(int,input().split())),reverse=True)
clear=sorted(list(map(int,input().split())),reverse=True)
red=red[:x]
green=green[:y]

import heapq
heapq.heapify(red)
heapq.heapify(green)

for i in range(c):
    r=heapq.heappop(red)
    g=heapq.heappop(green)
    c=clear[i]
    
    if r>c and g>c:
        heapq.heappush(red,r)
        heapq.heappush(green,g)
    elif r>=g:
        heapq.heappush(red,r)
        heapq.heappush(green,c)
    else:
        heapq.heappush(green,g)
        heapq.heappush(red,c)

print(sum(red)+sum(green))

Submission status: Accepted

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

p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

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

shoku=[]

for i in range(X):
    shoku.append(p[i])

for j in range(Y):
    shoku.append(q[j])

shoku.sort()

r_num=0
for k in range(len(shoku)):
    if r_num == C:
        break

    if shoku[k] < r[r_num]:
        shoku[k] = r[r_num]
        r_num+=1

print(sum(shoku))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

a,b,X,Y,Z = inpl()
x = sorted(inpl())
y = sorted(inpl())
c = sorted(inpl())
c = c[::-1]

i = X-a; j = Y-b
ind = 0
iok = True; jok = True
while (iok or jok) and ind < Z:
    if x[i] <= y[j]:
        if iok:
            if x[i] < c[ind]:
                x[i] = c[ind]
                ind += 1
                i += 1
                if i >= X: 
                    iok = False
                    i -= 1
            else:
                break
        else:
            if y[j] < c[ind]:
                y[j] = c[ind]
                ind += 1
                j += 1
                if j >= Y: 
                    jok = False
                    j -= 1
            else:
                break
    else:
        if jok:
            if y[j] < c[ind]:
                y[j] = c[ind]
                ind += 1
                j += 1
                if j >= Y: 
                    jok = False
                    j -= 1
            else:
                break
        else:
            if x[i] < c[ind]:
                x[i] = c[ind]
                ind += 1
                i += 1
                if i >= X: 
                    iok = False
                    i -= 1
            else:
                break
# print(x[X-a:],y[Y-b:])
print(sum(x[X-a:]) + sum(y[Y-b:]))

Submission status: Accepted

Solution code:
x,y,a,b,c=[int(j) for j in input().split()]
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
r=[int(j) for j in input().split()]
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:x]
q=q[:y]
l=p+q
l.sort()
ans=sum(l)
for i in l:
    if i<r[0]:
        ans+=r[0]-i
        del r[0]
        if r==[]:
            break
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
x,y,a,b,c = map(int, input().split())

aa = sorted(map(int, input().split()), reverse=True)
bb = sorted(map(int, input().split()), reverse=True)
cc = sorted(map(int, input().split()), reverse=True)
dd = []
for i in aa[:x]:
    heappush(dd,i)
for i in bb[:y]:
    heappush(dd, i)

for i in range(c):
    heappush(dd, cc[i])
    heappop(dd)

print(sum(dd))

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)
li=p[:x]+q[:y]
li.sort()
ans=sum(li)
for i in range(min((x+y),c)):
    if li[i]<r[i]:
        ans+=r[i]-li[i]
    else:
        break
print(ans)