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:
from heapq import heapify,heappop,heappush

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

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

ans = 0

for i in range(A):
    heappush(a,(-1*p[i],0))
for i in range(B):
    heappush(a,(-1*q[i],1))
for i in range(C):
    heappush(a,(-1*r[i],2))

c_r = c_g = c_all = 0
while c_all<X+Y:
    v,col = heappop(a)
    if c_r < X and c_g < Y:
        ans += -1*v
        c_all += 1
        if col == 0:
            c_r += 1
        if col == 1:
            c_g += 1
    elif c_r<X and c_g == Y:
        if col == 0 or col == 2:
            ans += -1*v
            c_all += 1
            if col == 0:
                c_r += 1
    elif c_r==X and c_g<Y:
        if col == 1 or col == 2:
            ans += -1*v
            c_all += 1
            if col == 1:
                c_g += 1
#    print("c_r:",c_r,"c_g:",c_g,"c_all:",c_all,"v:",v,"col:",col,"ans:",ans)
print(ans)

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p=sorted(p)
q=sorted(q)
r=sorted(r)
p=p[(a-x):]
q=q[(b-y):]
res=sum(p)+sum(q)
tp=0
tq=0
tr=c-1
while 1:
  if(tr<0 ):
    break
  if(tp>=x):
    pp=100000000000
  else:
    pp=p[tp]
  if(tq>=y):
    qq=100000000000
  else:
    qq=q[tq]

  if(pp>r[tr] and qq>r[tr]):
    break
  if(pp<qq ):
    res+=r[tr]-pp
    tp+=1
  else:
    res+=r[tr]-qq
    tq+=1
  tr-=1
print(res)

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

x,y,a,b,c = inm()
p = inl()
q = inl()
r = inl()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pt = x-1
qt = y-1
rt = 0
while rt<c and (pt>=0 and p[pt]<r[rt] or qt>=0 and q[qt]<r[rt]):
    if pt>=0 and (qt<0 or p[pt]<q[qt]):
        pt -= 1
        rt += 1
    elif qt>=0 and (pt<0 or p[pt]>=q[qt]):
        qt -= 1
        rt += 1
print(sum(p[:pt+1])+sum(q[:qt+1])+sum(r[:rt]))

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

z = sorted(p)[-X:] + sorted(q)[-Y:] + r
print(sum(sorted(z)[-(X+Y):]))

Submission status: Accepted

Solution code:


x, y, a, b, c = list(map(int, input().split(' ')))
pa = list(map(int, input().split(' ')))
qb = list(map(int, input().split(' ')))
rc = list(map(int, input().split(' ')))

pa = sorted(pa)[len(pa) - x:]
qb = sorted(qb)[len(qb) - y:]
rc = sorted(rc, reverse=True)

ai = 0
bi = 0
result = sum(pa) + sum(qb)

for r in rc:
    if bi >= len(qb) and ai >= len(pa):
        break
    
    elif bi >= len(qb) or pa[ai] <= qb[bi]:
        if r > pa[ai]:
            result = result - pa[ai] + r
            ai += 1
            
    elif ai >= len(pa) or pa[ai] >= qb[bi]:
        if r > qb[bi]:
            result = result - qb[bi] + r
            bi += 1
print(result)

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)

res = sum(P[0:X]) + sum(Q[0:Y])
p_min_idx = X-1
q_min_idx = Y-1
for r in R:
    if (p_min_idx < 0 and q_min_idx >= 0) or (p_min_idx >= 0 and q_min_idx >= 0 and P[p_min_idx] > Q[q_min_idx]):
        if Q[q_min_idx] < r:
            res = res - Q[q_min_idx] + r
            q_min_idx -= 1
        else:
            break
    else:
        if p_min_idx >= 0 and  P[p_min_idx] < r:
            res = res - P[p_min_idx] + r
            p_min_idx -= 1
        else:
            break

print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
INF = 10 ** 10
MOD = 10 ** 9 + 7
from heapq import heapify,heappop,heappush
input = sys.stdin.readline

def main():
    x,y,a,b,c = map(int,input().split())
    R = list(map(int,input().split()))
    G = list(map(int,input().split()))
    M = list(map(int,input().split()))
    i,j,k = 0,0,0
    R.sort(reverse = True)
    G.sort(reverse = True)
    M.sort(reverse = True)
    
    ans = 0
    while i < x and j < y and k < c and k + i + j < x + y:
        tmp = max(R[i],G[j],M[k])
        ans += tmp
        if tmp == R[i]:
            i += 1
        elif tmp == G[j]:
            j += 1
        else:
            k += 1
    
    if i + j + k == x + y:
        print(ans)
        return
    
    elif k == c:
        while i < x and j < y and i + j + c < x + y:
            tmp = max(R[i],G[j])
            ans += tmp
            if tmp == R[i]:
                i += 1
            elif tmp == G[j]:
                j += 1

    if i + j + k == x + y:
        print(ans)
        return            

    elif i == x:
        t = y - k - j
        for _ in range(t):
            if k < c and G[j] < M[k]:
                ans += M[k]
                k += 1
            else:
                ans += G[j]
                j += 1
    elif j == y:
        t = x - i - k
        for _ in range(t):
            if k < c and R[i] < M[k]:
                ans += M[k]
                k += 1
            else:
                ans += R[i]
                i += 1


    
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 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()

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

for i in range(C):
    if p and q and r:
        if p[-1] <= q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        elif q[-1] <= p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif p and r:
        if p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif q and r:
        if q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        else:
            break
    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)

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

a=0
b=0
f=False
for i in r:
    if f==True:
        break
    elif a==X and b==Y:
        break
    elif a==X:
        if i>q[Y-b-1]:
            tot+=i-q[Y-b-1]
            b+=1
        else:
            f=True
    elif b==Y:
        if i>p[X-a-1]:
            tot+=i-p[X-a-1]
            a+=1
        else:
            f=True
    else:
        if i>p[X-a-1] and i>q[Y-b-1]:
            if p[X-a-1]<q[Y-b-1]:
                tot+=i-p[X-a-1]
                a+=1
            else:
                tot+=i-q[Y-b-1]
                b+=1
        elif i>p[X-a-1]:
            tot+=i-p[X-a-1]
            a+=1
        elif i>q[Y-b-1]:
            tot+=i-q[Y-b-1]
            b+=1
        else:
            f=True

print(tot)

Submission status: Accepted

Solution code:
import heapq


def calc(X,Y,A,B,C,p,q,r):
    heapq.heapify(p)
    heapq.heapify(q)
    heapq.heapify(r)
    for _ in range(A - X):
        heapq.heappop(p)
    for _ in range(B - Y):
        heapq.heappop(q)
    minp = heapq.heappop(p)
    minq = heapq.heappop(q)
    
    while r:
        maxr = -heapq.heappop(r)
        if minp < minq:
            if maxr > minp:
                heapq.heappush(p, maxr)
                minp = heapq.heappop(p)
            else:
                break
        else:
            if maxr > minq:
                heapq.heappush(q, maxr)
                minq = heapq.heappop(q)
            else:
                break
    return sum(p) + sum(q) + minp + minq



(X, Y, A, B, C) = tuple([int(s) for s in 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()]
print(calc(X,Y,A,B,C,p,q,r))

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
import heapq

x,y,a,b,c = map(int,stdin.readline().rstrip().split())

p = list(map(int,stdin.readline().rstrip().split()))
q = list(map(int,stdin.readline().rstrip().split()))
r = list(map(int,stdin.readline().rstrip().split()))

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

p = p[a-x:]
q = q[b-y:]

heapq.heapify(p)
heapq.heapify(q)

for i in r:
    if p[0] <= q[0]:
        if i > p[0]:
            heapq.heappop(p)
            heapq.heappush(p,i)
    else:
        if i > q[0]:
            heapq.heappop(q)
            heapq.heappush(q,i)

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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pi = 0
qi = 0
ri = 0
pl = len(p)
ql = len(q)
rl = len(r)
R = 1
scr = 0
for j in range(X+Y):
  mx = max(p[pi],q[qi],r[ri])
  scr += mx
  if mx == p[pi]:
    X -= 1
    if pi+1 >= pl or X <= 0:
      p.append(0)
      pi = -1
      X = 0
    else:
      pi += 1
  elif mx == q[qi]:
    Y -= 1
    if qi+1 >= ql or Y <= 0:
      q.append(0)
      qi = -1
      Y = 0
    else:
      qi += 1
  else:
    if ri+1 >= rl or R <= 0:
      r.append(0)
      ri = -1
      R = 0
    else:
      ri += 1
      
print(scr)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

X,Y,A,B,C = map(int,input().split(' '))
l_p = list(map(int, input().split()))
l_q = list(map(int, input().split()))
l_r = list(map(int, input().split()))
l_p = sorted(l_p, reverse=True)
l_q = sorted(l_q, reverse=True)
l_r = sorted(l_r, reverse=True)
x = 0
y = 0
z = 0
Z = len(l_r)

def getMax(x,y,z):
    k_x = 0 if x==X else l_p[x]
    k_y = 0 if y==Y else l_q[y]
    k_z = 0 if z==Z else l_r[z]
    MAX = max(k_x,k_y,k_z)
    if MAX==k_x:
        x += 1
    elif MAX==k_y:
        y += 1
    else:
        z += 1
    return x,y,z,MAX
ans = 0
while True:
    x,y,z,m = getMax(x,y,z)
    ans += m
    if x+y+z==X+Y:
        break
print(ans)

Submission status: Accepted

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

A_s = A[0:X]
B_s = B[0:Y]

All = A_s
All.extend(B_s)
All.extend(C)
All.sort(reverse=True)

print(sum(All[0:X+Y]))

Submission status: Accepted

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

X,Y,A,B,C=map(int,readline().split())
P=sorted(list(map(int,readline().split())))
Q=sorted(list(map(int,readline().split())))
R=sorted(list(map(int,readline().split())))

# X,Y個を越えたP,Qは使わない
P=P[-X:]
Q=Q[-Y:]
R=R[-(X+Y):][::-1]
#print(P,Q,R)
PQ=sorted(P+Q)
#print("PQ",PQ)

allsum=sum(PQ)
rsum=0 # rの総数
ind=0
# Rをいくつ使うかのループを回す
for i in range(len(R)):
  if allsum-PQ[i]+R[i]>allsum:
    allsum=allsum-PQ[i]+R[i]
  else:
    break
print(allsum)

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

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()))
r.sort()
r.reverse()
p.sort()
p.reverse()
q.sort()
q.reverse()
lisp = []
for i in range(x):
    lisp.append(p[i])
lisq = []
for i in range(y):
    lisq.append(q[i])
lis = lisp + lisq
lis.sort()
i = 0
j = 0
# print(lisp)
# print(lisq)
# print(lis)
while j < c and  i < len(lis):
    if lis[i] < r[j]:
        lis[i] = r[j]
        i += 1
        j += 1
    else:
        break
print(sum(lis))

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.sort()
green.sort()
no.sort()
a -= 1
b -= 1
c -= 1
max_a = int(a)
max_b = int(b)

ans = 0
for i in range(x):
    ans += red[a]
    a -= 1
for i in range(y):
    ans += green[b]
    b -= 1
a += 1
b += 1

for i in range(x+y):
    if c >= 0 and a <= max_a and b <= max_b:
        gr = no[c] - red[a]
        gg = no[c] - green[b]
        if gr >= gg:
            if gr > 0:
                ans += no[c]
                ans -= red[a]
                c -= 1
                a += 1
        elif gg >= gr:
            if gg > 0:
                ans += no[c]
                ans -= green[b]
                c -= 1
                b += 1
    elif c >= 0 and a <= max_a:
        if no[c] - red[a] > 0:
            ans += no[c]
            ans -= red[a]
            c -= 1
            a += 1
    elif c >= 0 and b <= max_b:
        if no[c] - green[b] > 0:
            ans += no[c]
            ans -= green[b]
            c -= 1
            b += 1
    else:
        break

print(ans)

Submission status: Accepted

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

lp = hp.nlargest(x, p)
lq = hp.nlargest(y, q)
pq = lp + lq
lpq = hp.nlargest(x+y, pq)
lr = hp.nlargest(c, r)
d = min(len(lr),len(lpq))
for i in range(1,d+1):
  if lpq[x+y-i] < lr[i-1]:
    lpq[x+y-i] = lr[i-1]
  else:
    break

ans = 0
for i in range(0,len(lpq)):
  ans += lpq[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)
l = p[:X] + q[:Y]
l.sort()
for i in range(min(C,X+Y)):
    if r[i] > l[i]:
        l[i] = r[i]
print(sum(l))

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
x,y,a,b,c=map(int,sys.stdin.readline().split())
red=list(map(int,sys.stdin.readline().split()))
green=list(map(int,sys.stdin.readline().split()))
color=list(map(int,sys.stdin.readline().split()))
red.sort()
green.sort()
color.sort()
s=0
for i in range(a-x,a):
    s+=red[i]
for i in range(b-y,b):
    s+=green[i]
i,j,k=c-1,a-x,b-y
#print(red,'red')
#print(green,'green')
#print(color,'color')
#print(s,'s',x,'x',y,'y',i,'i',j,'j',k,'k')
while i>=0 and j<a and k<b:
    if color[i]>red[j] and color[i]>green[k]:
        if green[k]>red[j]:
            s-=red[j]
            s+=color[i]
            j+=1
            i-=1
            continue
        else:
            s-=green[k]
            s+=color[i]
            k+=1
            i-=1
            continue
    if color[i]>red[j]:
        s-=red[j]
        s+=color[i]
        i-=1
        j+=1
        continue
    if color[i]>green[k]:
        s-=green[k]
        s+=color[i]
        i-=1
        k+=1
        continue
    i-=1
if i==0:
    print(s)
else:
    while i>=0 and j<a:
        if color[i]>red[j]:
            s-=red[j]
            s+=color[i]
            i-=1
            j+=1
            continue
        i-=1
    while i>=0 and k<b:
        if color[i]>green[k]:
            s-=green[k]
            s+=color[i]
            i-=1
            k+=1
            continue
        i-=1
    print(s)

Submission status: Accepted

Solution code:
def main():
    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())))[::-1]
    S = sorted(P[-x:] + Q[-y:])
    #print(S)
    ans = 0
    ind = 0
    for s in S:
        #print(ans, s)
        if ind < len(R):
            if s > R[ind]:
                ans += s
            else:
                ans += R[ind]
                ind += 1
        else:
            ans += s
    print(ans)
main()

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 = np.sort(p)[::-1][:x]
q = np.sort(q)[::-1][:y]

mix = np.hstack((p, q, r))
mix = np.sort(mix)[::-1]

eat = x+y

print(sum(mix[:eat]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 21:48:49 2020

@author: liang
"""
def min_search(a,b,c):
    if a < b:
        if a < c:
            return a
        else:
            return c
    else:
        if b < c:
            return b
        else:
            return c
        
        
X, Y, A, B, C = map(int,input().split())
red_apples = list(map(int,input().split()))
green_apples = list(map(int,input().split()))
colorless_apples = list(map(int,input().split()))

red_apples.sort(reverse=True)
green_apples.sort(reverse=True)
colorless_apples.sort(reverse=True)

#print(red_apples)
#print(green_apples)
#print(colorless_apples)

result_red = red_apples[0:X]
result_green = green_apples[0:Y]

#print(result_red)
#print(result_green)

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

while k <= C-1:
    if i < 0 :
        i = 0
    if j < 0 :
        j = 0
    mini = min_search(result_red[i], result_green[j], colorless_apples[k])
    if mini == colorless_apples[k]:
        break
    elif mini == result_red[i]:
        result_red[i] = colorless_apples[k]
        i -= 1
        k += 1
    else:
        result_green[j] = colorless_apples[k]
        j -= 1
        k += 1


score = sum(result_red) + sum(result_green)

print(score)

Submission status: Accepted

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

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

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

S_wa = []
wa = 0
for s in S[::-1]:
    wa += s
    S_wa.append(wa)
# print(S_wa)
big = S_wa[-1]
ans = S_wa[-1]

R_wa = []
wa = 0
for r in R:
    wa += r
    R_wa.append(wa)
# print(R_wa)

kazu = min(len(R_wa), len(S_wa))

for i in range(kazu):
    ans = max(ans, big + R_wa[i] - S_wa[i])
print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
La = sorted(list(map(int, input().split())), reverse=True)
Lb = sorted(list(map(int, input().split())), reverse=True)
Lc = sorted(list(map(int, input().split())), reverse=True)

L = La[:x]
L.extend(Lb[:y])
L.sort(reverse=False)
min = min(c, len(L))

for i in range(min):
    if L[i] < Lc[i]:
        L[i] = Lc[i]

print(sum(L))

Submission status: Accepted

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

print( sum(sorted(p+q+r)[c:]) )

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

A = sorted(P[:x] + Q[:y])
ans = sum(A)

for i in range(min(c, len(A))):
    if A[i] < R[-1]:
        ans = ans + R[-1] - A[i]
        R.pop(-1)

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
X, Y, A, B, C = list(map(int, input().split()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

arr = []
for p in P:
  arr.append((p, 0))
for q in Q:
  arr.append((q, 1))
for r in R:
  arr.append((r, 2))
arr.sort()

remain = X + Y
ret = 0
while remain > 0:
  a, t = arr.pop()
  if t == 0 and X > 0:
    X -= 1
    remain -= 1
    ret += a
  elif t == 1 and Y > 0:
    Y -= 1
    remain -= 1
    ret += a
  elif t == 2:
    remain -= 1
    ret += a
print(ret)

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 = sorted(a)[::-1]
b = sorted(b)[::-1]
c = sorted(c)[::-1]

a = a[:x]
b = b[:y]
ans = a+b
ans = sorted(ans)
# ans = ans[::-1]
index = 0
# print(ans)
# print(c)
for i in range(0,C):
    if(c[i] > ans[index]):
        ans[index] = c[i]
        index = index + 1
    else:
        break

    if(index == x+y):
        break
# print(ans)
print(sum(ans))

Submission status: Accepted

Solution code:
hoge = list(map(int,input().split()))
X, Y, A, B, C = hoge[0], hoge[1], hoge[2], hoge[3], hoge[4]
r_apple = sorted(list(map(int,input().split())),reverse=True)
g_apple = sorted(list(map(int,input().split())),reverse=True)
b_apple = sorted(list(map(int,input().split())),reverse=True)

eat_list = []
for i in range(X):
  eat_list.append(r_apple[i])
for i in range(Y):
  eat_list.append(g_apple[i])

sort_eat_list = sorted(eat_list)

for i in range(len(sort_eat_list)):
  if sort_eat_list[i] < b_apple[0]:
    sort_eat_list[i] = b_apple.pop(0)
    if len(b_apple) == 0:
      break

print(sum(sort_eat_list))

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.append(-1)
q.append(-1)
r.append(-1)
ps=sorted(p)
qs=sorted(q)
rs=sorted(r)
d=0
e=0
f=0
ans=0
def me1():
    global ans
    
    if max(ps[-1],qs[-1],rs[-1])==ps[-1]:
        ans+=ps[-1]
        ps.pop(-1)
        global d
        d+=1
    elif max(ps[-1],qs[-1],rs[-1])==qs[-1]:
        ans+=qs[-1]
        qs.pop(-1)
        global e
        e+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
def me2():
    global ans
    if max(ps[-1],rs[-1])==ps[-1]:
        ans+=ps[-1]
        ps.pop(-1)
        global d
        d+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
def me3():
    global ans
    if max(qs[-1],rs[-1])==qs[-1]:
        ans+=qs[-1]
        qs.pop(-1)
        global e
        e+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
for i in range(x+y):
    #print(ans)
    #print(i)
    
    if d>=x:
        #print('x')
        me3()
    elif e>=y:
        #print('y')
        me2()
    else:
        #print('z')
        me1()
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = list(map(int,input().split()))
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort()
P = P[:X]
Q = Q[:Y]
S = []
#print(P,Q,S)
for _ in range(X+Y):
    if len(R) == 0:
        break
    s = R[-1]
    if len(P) == 0:
        p = 1000000000000000
    else:
        p = P[-1]
    if len(Q) == 0:
        q = 1000000000000000
    else:
        q = Q[-1]
    #print(p,q,s)
    if p >= q:
        if q < s:
            del Q[-1]
            del R[-1]
            S.append(s)
        else:
            break
    else:
        if p < s:
            del P[-1]
            del R[-1]
            S.append(s)
        else:
            break
    #print(P,Q,S)
ans = 0
for i in P:
    ans += i
for i in Q:
    ans += i
for i in S:
    ans += i
print(ans)

Submission status: Accepted

Solution code:
import heapq

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 = False)

p = p[0:X]   # 大きいものからX個
q = q[0:Y]

# Min heap
heapq.heapify(p)
heapq.heapify(q)


for i in range(C):
    if len(p) > 0 and r[i] > p[0] and p[0] <= q[0]:  # 赤いりんごの方が美味しくない
        heapq.heappop(p)
        heapq.heappush(p, r[i])
    elif len(q) > 0 and r[i] > q[0]:
        heapq.heappop(q)
        heapq.heappush(q, r[i])

ans = 0
while len(p) > 0:
    # red = heapq.heappop(p)
    ans += heapq.heappop(p)
while len(q) > 0:
    # green = heapq.heappop(q)
    ans += heapq.heappop(q)

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C =map(int,input().rstrip().split(" "))
RA = list(map(int,input().rstrip().split(" ")))
GA = list(map(int,input().rstrip().split(" ")))
WA = list(map(int,input().rstrip().split(" ")))
RA.sort()
RA.reverse()
GA.sort()
GA.reverse()
WA.sort()
WA.reverse()
eatRA = X
eatGA = Y
eatWA = 0
ans = 0
while RA[eatRA - 1] < WA[eatWA] or GA[eatGA -1] < WA[eatWA]:
  n= 1
  while RA[eatRA - n] == GA[eatGA -n] and n != min(eatRA,eatGA):
    n += 1
  if RA[eatRA - n] > GA[eatGA - n]:
    eatGA -= 1
  else:
    eatRA -= 1
  eatWA += 1
  if eatWA == C or eatRA == 0 or eatGA == 0:
    break
if eatRA == 0 and eatGA != 0 and eatWA != C:
  while GA[eatGA -1] < WA[eatWA]:
    eatGA -= 1
    eatWA += 1
    if eatWA == C or eatGA == 0:
      break
elif eatRA != 0 and eatGA == 0 and eatWA != C:
  while RA[eatRA -1] < WA[eatWA]:
    eatRA -= 1
    eatWA += 1
    if eatWA == C or eatRA == 0:
      break
for i in range(eatRA):
  ans += RA[i]
for i in range(eatGA):
  ans += GA[i]
for i in range(eatWA):
  ans += WA[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()))

ans_p = sorted(p, reverse=True)[:x][::-1]
ans_q = sorted(q, reverse=True)[:y][::-1]
r.sort(reverse=True)

p_index = 0
q_index = 0
used_r_sum = 0

for max_r in r:
    ans_p_p_index = ans_p[p_index] if p_index < x else float('inf')
    ans_q_q_index = ans_q[q_index] if q_index < y else float('inf')
    if ans_p_p_index < ans_q_q_index:
        if ans_p_p_index < max_r:
            p_index += 1
            used_r_sum += max_r
        else:
            break
    else:
        if ans_q_q_index < max_r:
            q_index += 1
            used_r_sum += max_r
        else:
            break


print(sum(ans_p[p_index:])+sum(ans_q[q_index:])+used_r_sum)

Submission status: Accepted

Solution code:
import heapq
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
p.sort()
p=p[-1*x:]
q=list(map(int,input().split()))
q.sort()
pq=p
pq.extend(q[-1*y:])
r=list(map(lambda x:-1*int(x),input().split()))
heapq.heapify(pq)
heapq.heapify(r)
while(len(r)>0):
    max_r=-1*heapq.heappop(r)
    min_pq=heapq.heappop(pq)
#    print(max_r,min_pq)
#    print(pq)
#    print(r)
    if(min_pq < max_r):
        heapq.heappush(pq,max_r)
    else:
        heapq.heappush(pq,min_pq)
        break
print(sum(pq))

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,raw_input().split())
p=map(int,raw_input().split())
q=map(int,raw_input().split())
r=map(int,raw_input().split())

a=[]
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

u=p[0:x]
v=q[0:y]
if len(r)<x+y:
    w=r
else:
    w=r[0:x+y]

A=u+v+w
A.sort(reverse=True)
print sum(A[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()))

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

Ca = 0
Cb = 0
Cc = 0

ans = 0

while Ca + Cb + Cc != X + Y:
    if Cc == C:
        if q[Cb] > p[Ca]:
            ans = ans + q[Cb]
            Cb = Cb + 1
        else:
            ans = ans + p[Ca]
            Ca = Ca + 1
    else:
        if Ca == X:
            if q[Cb] > r[Cc]:
                ans = ans + q[Cb]
                Cb = Cb + 1
            else:
                ans = ans + r[Cc]
                Cc = Cc + 1
        else:
            if Cb == Y:
                if p[Ca] > r[Cc]:
                    ans = ans + p[Ca]
                    Ca = Ca + 1
                else:
                    ans = ans + r[Cc]
                    Cc = Cc + 1
            else:
                if p[Ca] >= q[Cb] and p[Ca] > r[Cc]:
                    ans = ans + p[Ca]
                    Ca = Ca + 1
                elif q[Cb] > p[Ca] and q[Cb] > r[Cc]:
                    ans = ans + q[Cb]
                    Cb = Cb + 1
                else:
                    ans = ans + r[Cc]
                    Cc = Cc + 1
print(ans)