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
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり


X,Y,A,B,C = MI()
P,Q,R = LI(),LI(),LI()

Z = [(P[i],1) for i in range(A)] + [(Q[i],0) for i in range(B)] + [(R[i],-1) for i in range(C)]
Z.sort(reverse=True)
a,b,c = 0,0,0  # 選択した赤リンゴ、緑リンゴ、無色リンゴの個数
n = 0  # 選択したリンゴの個数
ans = 0
for x in Z:
    if n == X+Y:
        break
    s,t = x
    if t == 1 and a == X:
        continue
    elif t == 0 and b == Y:
        continue
    else:
        n += 1
        ans += s
        if t == 1:
            a += 1
        elif t == 0:
            b += 1
        else:
            c += 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=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)
s=[]
for i in range(x):
    s.append(p[i])
for i in range(y):
    s.append(q[i])
s=sorted(s,reverse=True)
ws=[s[0]]
for i in range(x+y-1):
    ws.append(ws[-1]+s[i+1])
wr=[r[0]]
for i in range(c-1):
    wr.append(wr[-1]+r[i+1])
ans=ws[-1]
for i in range(min(x+y-1,c)):
    ans=max(ans,ws[-2-i]+wr[i])
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()))

r = [-x for x in r]

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

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

heapify(p)
heapify(q)
heapify(r)

while r:
    pp = heappop(p)
    qq = heappop(q)
    rr = heappop(r)
    rr *= -1

    if pp >= rr and qq >= rr:
        heappush(p, pp)
        heappush(q, qq)
        break

    elif pp <= qq:
        heappush(q, qq)
        if pp < rr:
            heappush(p, rr)
    else:
        heappush(p, pp)
        if qq < rr:
            heappush(q, rr)

print(sum(p) + sum(q))

Submission status: Accepted

Solution code:
def LI():
    return list(map(int, input().split()))


X, Y, A, B, C = LI()
red = LI()
green = LI()
mu = LI()
red.sort(reverse=True)
green.sort(reverse=True)
ans = red[:X]+green[:Y]+mu
ans.sort(reverse=True)
total = 0
for i in range(X+Y):
    total += ans[i]
print(total)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


X,Y,A,B,C = MI()
P,Q,R = LI(),LI(),LI()

Z = [(P[i],1) for i in range(A)] + [(Q[i],0) for i in range(B)] + [(R[i],-1) for i in range(C)]
Z.sort(reverse=True)
a,b,c = 0,0,0
n = 0
ans = 0
for x in Z:
    if n == X+Y:
        break
    s,t = x
    if t == 1 and a == X:
        continue
    elif t == 0 and b == Y:
        continue
    else:
        n += 1
        ans += s
        if t == 1:
            a += 1
        elif t == 0:
            b += 1
        else:
            c += 1

print(ans)

Submission status: Accepted

Solution code:
'''
Created on 2020/09/10

@author: harurun
'''
def main():
  import heapq
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
#   dl=[]

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

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

  p=p[:X]
  q=q[:Y]
  
  Q=[]
  for i in p:
    Q.append([i,0])
  for j in q:
    Q.append([j,1])

  heapq.heapify(Q)
  cntp=0
  cntq=0
  ans=0
  for k in r:
    t=heapq.heappop(Q)
    if t[1]==0 and t[0]<k and cntp<=X:
      ans+=k 
      cntp+=1
#       dl.append(k)
    elif t[1]==1 and t[0]<k and cntq<=Y:
      ans+=k 
      cntq+=1
#       dl.append(k)
    else:
      ans+=t[0]
      break
  while True:
    try:
      s=heapq.heappop(Q)
      ans+=s[0] 
#       dl.append(s[0])
    except:
      break
  print(ans)
#   print(dl)
#   print(p)
#   print(q)
  
  return 

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=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r)

box=[]

ans=0


for i in range(x):
    box.append(p[i])

for j in range(y):
    box.append(q[j])

box = box + r

box=sorted(box,reverse=True)

for k in range(x+y):
    ans+=box[k]

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

qu=[]

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

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

while r:
    rr=r.pop()

    if rr<qu[0]:
        break

    heapq.heappop(qu)
    heapq.heappush(qu,rr)

print(sum(qu))

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)
ans=[]
for i in range(x):
  r.append(p[i])
for i in range(y):
  r.append(q[i])
r.sort(reverse=True)
print(sum(r[0:x+y]))

Submission status: Accepted

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

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

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

ans = 0
red_cnt = 0
green_cnt = 0
cnt = 0

heap = []
for i in range(len(p)):
    heapq.heappush(heap,(-p[i],0))
for i in range(len(q)):
    heapq.heappush(heap,(-q[i],1))
for i in range(len(r)):
    heapq.heappush(heap,(-r[i],2))

while len(heap):
    _x,_y = heapq.heappop(heap)
    if _y == 0 and red_cnt < x:
        red_cnt += 1
        ans += _x
        cnt += 1
    elif _y == 1 and green_cnt < y:
        green_cnt += 1
        ans += _x
        cnt += 1
    elif _y == 2:
        cnt += 1
        ans += _x

    if cnt == x + y:
        print(-ans)
        exit()

Submission status: Accepted

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

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

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

cand = p[0:X] + q[0:Y] + r

cand.sort(reverse=True)

ans = sum(cand[0:X+Y])
print(ans)

Submission status: Accepted

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

R=list(map(int,input().split()) )
G=list(map(int,input().split()) )
W=list(map(int,input().split()) )

R.sort(reverse=True)
G.sort(reverse=True)

Z = W+R[0:x] + G[0:y]

Z.sort(reverse=True)

print(sum(Z[0:x+y]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[10]:


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)


# In[14]:


p_list = p[:X]
q_list = q[:Y]
mylist = sorted(p_list + q_list + r, reverse=True)
ans = 0
for i in range(X+Y):
    ans += mylist[i]
print(ans)


# In[ ]:

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

Submission status: Accepted

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

p=p[-x:]
q=q[-y:]
r=sorted(p+q+r)
print(sum(r[-x-y:]))
assert sum(r[-x-y:]) < 2**63

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
ans = sum(p[:X]) + sum(q[:Y])
x = X-1
y = Y-1
m = min(p[x],q[y])
n = 1
while len(r)>0 and r[0] > m:
    if n == 1:
        if p[x] > q[y]:
            ans += r[0] - q[y]
            y -= 1
        else:
            ans += r[0] - p[x]
            x -= 1
    else:
        ans += r[0] - m
        if x<0:
            y -= 1
        else:
            x -= 1
    r.pop(0)
    if y >= 0 and x >= 0:
        m = min(p[x],q[y])
    elif x >= 0 and y<0:
        m = p[x]
        n = 0
    elif y >= 0 and x<0:
        m = q[y]
        n = 0
    else:
        m = 10 ** 9 + 1
        n = 0
print(ans)

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
s=[]
for i in range(a):
    s.append([p[i],1])
for i in range(b):
    s.append([q[i],2])
for i in range(c):
    s.append([r[i],0])
s.sort(reverse=True)
o=0
X,Y,Z=0,0,0
for i in range(a+b+c):
    if s[i][1]==1 and X<x:
        X+=1
        Z+=1
        o+=s[i][0]
    elif s[i][1]==2 and Y<y:
        Y+=1
        Z+=1
        o+=s[i][0]
    elif s[i][1]==0:
        Z+=1
        o+=s[i][0]
    if Z==x+y:
        break
print(o)

Submission status: Accepted

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

P=P[-X:]
Q=Q[-Y:]
R=sorted(P+Q+R)
ans=sum(R[-(X+Y):])
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()))

x=X-1
y=Y-1

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

#print(p[:X],q[:Y])

if p[x]>=q[y]:
  a=q[y]
  f=1
else:
  a=p[x]
  f=2
while len(r)>=1 and a<r[0]:
#  print(a,r[0])
  if f==1 :
#    print('f=1')
    q[y]=r[0]
    y-=1
    r.pop(0)
  elif f==2 :
#    print('f=2')
    p[x]=r[0]
    x-=1
    r.pop(0)
  else:
#    print('break1')
    break
    
#  print(x,y,p[x],q[y])
    
  if (y==-1 and x>=0) or (x>=0 and p[x]<q[y]):
#    print('a=p[x]')
    a=p[x]
    f=2
  elif (x==-1 and y>=0) or (y>=0 and p[x]>=q[y]):
#    print('a=q[y]')
    a=q[y]
    f=1
  else:
#    print('break2')
    break

ans=0
#print(p[:X],q[:Y])
for i in p[:X]:
  ans+=i
for i in q[:Y]:
  ans+=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()))

from collections import deque

p = sorted(p)[a-x:]
q = sorted(q)[b-y:]
t = deque(sorted(p+q))
r = deque(reversed(sorted(r)))
# print(t,r)

while r:
    if t[0] < r[0]:
        t.popleft()
        w = r.popleft()
        t.append(w)
    else:
        break

ans = sum(t)
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)

temp = P[:X] + Q[:Y] + R

temp.sort(reverse=True)
ans = sum(temp[:X+Y])

print(ans)

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

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

ans=[]
for i in range(x):
  ans.append(p[i])
  
for j in range(y):
  ans.append(q[j])
  
ans.sort()

heapq.heapify(ans)

for j in range(c):
  min_a=heapq.heappop(ans)
  if min_a<=r[j]:
    heapq.heappush(ans,r[j])
  else:
    heapq.heappush(ans,min_a)
    break
    
print(sum(ans))

Submission status: Accepted

Solution code:
X, Y, A ,B, C = list(map(int, input().split()))
P = sorted(list(map(int, input().split())), reverse = True)[:X]
Q = sorted(list(map(int, input().split())), reverse = True)[:Y]
R = sorted(list(map(int, input().split())), reverse = True)
ans = 0
for i in range(C):
  if(len(P) == 0):
    if(len(Q) == 0):
      break
    elif(Q[-1] < R[i]):
      Q.pop()
      ans += R[i]
    else:
      break
  elif(len(Q) == 0):
    if(P[-1] < R[i]):
      P.pop()
      ans += R[i]
    else:
      break
  else:
    if(P[-1] < Q[-1] and P[-1] < R[i]):
      P.pop()
      ans += R[i]
    elif(Q[-1] <= P[-1] and Q[-1] < R[i]):
      Q.pop()
      ans += R[i]
    else:
      break
ans += sum(P) + sum(Q)
print(ans)

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
p=list(reversed(sorted(map(int,input().split()))))
q=list(reversed(sorted(map(int,input().split()))))
r=list(reversed(sorted(map(int,input().split()))))
p.append(2**100)
q.append(2**100)
r.append(-2**100)
s=sum(p[:x])+sum(q[:y])
t=0
while True:
  f=p[x-1]<q[y-1]
  m=p[x-1] if f else q[y-1]
  if m<r[t]:
    if f:
      x-=1
      s+=r[t]-p[x]
      t+=1
    else:
      y-=1
      s+=r[t]-q[y]
      t+=1
  else:
    break
print(s)

Submission status: Accepted

Solution code:
X, Y, A, B, C = [int(n) for n in input().split(" ")]

p = [int(n) for n in input().split(" ")]
q = [int(n) for n in input().split(" ")]
r = [int(n) for n in input().split(" ")]

R = sorted(p)
G = sorted(q)
T = sorted(r)

W = 0
GetR = 0
GetG = 0
GetS = 0

while GetS < X + Y:
    a = -1
    b = -1
    c = -1
    if len(R) > 0 and GetR < X:
        a = R[-1]
    if len(G) > 0 and GetG < Y:
        b = G[-1]
    if len(T) > 0:
        c = T[-1]

    t = max([a, b, c])
    index = [a, b, c].index(t)
    W += t
    #print(R, G, T)
    GetS += 1
    if index == 0:
        del(R[-1])
        GetR += 1
    if index == 1:
        del(G[-1])
        GetG += 1
    if index == 2:
        del(T[-1])

print(W)

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)
p2=p[:x]
q2=q[:y]
num=p2+q2
num.sort()
ans=sum(num)
cnt=ans
for i in range(min(x+y,c)):
    cnt+=r[i]-num[i]
    ans=max(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
plst = list(map(int, input().split()))
qlst = list(map(int, input().split()))
rlst = list(map(int, input().split()))
plst.sort()
qlst.sort()
rlst.sort(reverse = True)
inf = 10 ** 20
plst = plst[-x:] + [inf]
qlst = qlst[-y:] + [inf]
r_pos = 0
p_pos = 0
q_pos = 0
while 1:
    if rlst[r_pos] <= plst[p_pos] and rlst[r_pos] <= qlst[q_pos]:
        break
    elif plst[p_pos] < qlst[q_pos]:
        plst[p_pos] = rlst[r_pos]
        p_pos += 1
        r_pos += 1
    else:
        qlst[q_pos] = rlst[r_pos]
        q_pos += 1
        r_pos += 1
    if r_pos == c:
        break
print(sum(plst) + sum(qlst) - inf * 2)

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] + R
L.sort(reverse=True)
print(sum(L[:x+y]))

Submission status: Accepted

Solution code:
x_aka,y_mid,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_aka = sorted(p,reverse=True)
q_mid = sorted(q,reverse=True)
r_mu = sorted(r,reverse=True)
#print(p_aka[:x_aka],q_mid[:y_mid],r_mu)
tmp = p_aka[:x_aka] + q_mid[:y_mid] + r_mu
sort_tmp = sorted(tmp,reverse=True)
#print(sort_tmp[:(x_aka+y_mid)])
print(sum(sort_tmp[:(x_aka+y_mid)]))

Submission status: Accepted

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

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

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

ans=p+q+r
ans.sort()
ans=ans[-x-y:]
print(sum(ans))

Submission status: Accepted

Solution code:
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]
r = sorted([int(x) for x in input().split()], reverse=True)[:X + Y]

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

ans = sum(s[:X + Y])

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
P=sorted(map(int,input().split()), reverse=True)[:X]
Q=sorted(map(int,input().split()), reverse=True)[:Y]
R=sorted(map(int,input().split()), reverse=True)[:X+Y]
i,j=X-1,Y-1
for r in R:
    if i==-1 and j==-1:
        break
    elif j==-1:
        if r>P[i]:
            P[i]=r
            i-=1
        else:
            break
    elif i==-1:
        if r>Q[j]:
            Q[j]=r
            j-=1
        else:
            break
    elif P[i]>Q[j]:
        if r>Q[j]:
            Q[j]=r
            j-=1
        else:
            break
    else:
        if r>P[i]:
            P[i]=r
            i-=1
        else:
            break
print(sum(P)+sum(Q))

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()))
colorless=list(map(int,input().split()))
temp=[]
red.sort(reverse=True)
green.sort(reverse=True)
colorless.sort(reverse=True)
for i in range(min(x,a)):
  temp.append(red[i])
for i in range(min(y,b)):
  temp.append(green[i])
for i in range(c):
  temp.append(colorless[i])
temp.sort(reverse=True)
ans=0
for i in range(x+y):
  ans+=temp[i]
print(ans)

Submission status: Accepted

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

red_list=[int(i) for i in input().split()]
green_list=[int(i) for i in input().split()]
non_col_list=[int(i) for i in input().split()]

red_list.sort()
green_list.sort()
non_col_list.sort()

eat_red=[]
eat_green=[]
for i in range(x):
    eat_red.append(red_list.pop())
for i in range(y):
    eat_green.append(green_list.pop())


ans_list=eat_green+eat_red+non_col_list
ans_list.sort()
ans=0
for i in range(x+y):
    ans+=ans_list.pop()

print(ans)

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_max = sorted(p_list, reverse=True)
q_list_max = sorted(q_list, reverse=True)
r_list_max = sorted(r_list, reverse=True)

p_list_X = p_list_max[:X]
q_list_Y = q_list_max[:Y]

ans_list = []
ans_list.extend(p_list_X)
ans_list.extend(q_list_Y)
ans_list.extend(r_list_max)
ans_list_max = sorted(ans_list, reverse=True)

ans = 0
#print(p_list_X, q_list_Y, ans_list_max)
for i in range(X+Y):
    ans += ans_list_max[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=1)[:x]
q=sorted(list(map(int,input().split())),reverse=1)[:y]
r=sorted(list(map(int,input().split())),reverse=1)
pq=sorted(p+q,reverse=1)
ans=sum(pq)

n=len(r)
count=-1
for i in range(n):
    if r[i]>pq[count]:
        ans+=(r[i]-pq[count])
        count-=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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
p=p[:x]
q=q[:y]
ans=p+q
ans.sort(reverse=True)
ans=[0]+ans
nowi=x+y
r=[0]+r
nowr=0
while nowr<=c-1 and nowi>0 and r[nowr+1]>ans[nowi]:
  nowi+=-1
  nowr+=1
ans_=0
for i in range(nowi+1):
  ans_+=ans[i]
for j in range(nowr+1):
  ans_+=r[j]
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]
ans=p+q+r
ans.sort(reverse=True)
print(sum(ans[:x+y]))

Submission status: Accepted

Solution code:
def resolve():
    X, Y, A, B, C = list(map(int, input().split()))
    P = sorted(list(map(int, input().split())), reverse=True)
    Q = sorted(list(map(int, input().split())), reverse=True)
    R = sorted(list(map(int, input().split())), reverse=True)
    eatreds = P[:X]
    eatgreens = Q[:Y]
    import heapq
    heapq.heapify(eatreds)
    heapq.heapify(eatgreens)
    for r in R:
        smallred = heapq.heappop(eatreds)
        smallgreen = heapq.heappop(eatgreens)
        reddiff = r-smallred
        greendiff = r-smallgreen
        if reddiff < 0 and greendiff < 0:
            heapq.heappush(eatreds, smallred)
            heapq.heappush(eatgreens, smallgreen)
            break
        if reddiff > greendiff:
            heapq.heappush(eatreds, r)
            heapq.heappush(eatgreens, smallgreen)
        else:
            heapq.heappush(eatreds, smallred)
            heapq.heappush(eatgreens, r)
    print(sum(eatreds)+sum(eatgreens))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from collections import deque

X,Y,A,B,C = map(int, input().split())
A=[int(a) for a in input().split()]
B=[int(b) for b in input().split()]
C=[int(c) for c in input().split()]

A=sorted(A, reverse=True)[:X]
B=sorted(B, reverse=True)[:Y]
AB=deque(sorted(A+B))

for c in sorted(C, reverse=True):
  if c >= AB[0]:
    AB.popleft()
    AB.append(c)
  else:
    break

print(sum(AB))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


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


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    x, y, a, b, c = MI()
    p = LI()
    q = LI()
    r = LI()

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

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

    ans = [*p_selected, *q_selected]

    ans.sort()
    i = 0

    while i < x + y and i < c and ans[i] < r[i]:
        ans[i] = r[i]
        i += 1

    print(sum(ans))


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

X, Y, A, B, C = MI()
P = LI()
Q = LI()
R = LI()

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

Ans = []
for p in P:
    Ans.append(('P', p))
for q in Q:
    Ans.append(('Q', q))
for r in R:
    Ans.append(('R', r))
Ans.sort(key=lambda tup: tup[1])


Ans_text = []
for i in range(A + B + C - X - Y, A + B + C):
    Ans_text.append(Ans[i][0])

c = collections.Counter(Ans_text)

R_num = c['R']
P_num = c['P']
Q_num = c['Q']

ans = 0
cnt = 0
if c['P'] <= X and c['Q'] <= Y:
    while cnt < (X + Y):
        ans += Ans.pop()[1]
        cnt += 1
elif c['P'] > X:
    while cnt < X:
        ans += P.pop()
        cnt += 1
    while cnt < (X + Y):
        b = Ans.pop()
        if b[0] == 'P':
            continue
        else:
            ans += b[1]
            cnt += 1
else:
    while cnt < Y:
        ans += Q.pop()
        cnt += 1
    while cnt < (X + Y):
        b = Ans.pop()
        if b[0] == 'Q':
            continue
        else:
            ans += b[1]
            cnt += 1
print(ans)

Submission status: Accepted

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

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


X, Y, A, B, C = map(int, input().split())
apples_A = sorted(list(map(int, input().split())))
apples_B = sorted(list(map(int, input().split())))
apples_C = sorted(list(map(int, input().split())))

# 赤のリンゴからおいしさが大きい順に X 個選ぶ (1)
# 緑のリンゴからおいしさが大きい順に Y 個選ぶ (2)
# (1), (2) そして 無色のリンゴのから、おいしさが大きいものから順に X + Y 個選べば良い

apples_rest = sorted(apples_A[-X:] + apples_B[-Y:] + apples_C)
ans = sum(apples_rest[ -(X + Y):])

print(ans)

Submission status: Accepted

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

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


X, Y, A, B, C = map(int, input().split())
apples_A = sorted(list(map(int, input().split())))
apples_B = sorted(list(map(int, input().split())))
apples_C = sorted(list(map(int, input().split())))

# 赤のリンゴからおいしさが大きい順に X 個選ぶ
# 緑のリンゴからおいしさが大きい順に Y 個選ぶ
# 無色のリンゴからおいしさが最も大きいリングを選び、そのリンゴ以上の美味しさでかつ美味しさが最も小さい赤 or 緑リンゴと交換する
# 上記の交換をできるだけ多く行う

apples_rest = sorted(apples_A[-X:] + apples_B[-Y:] + apples_C)
ans = sum(apples_rest[ -(X + Y):])

# while apples_C:
#     candidate = apples_C.pop()
#     min_apple = min(res_A[0], res_B[0])
#     if candidate <= min_apple:
#         break

#     if res_A[0] <= res_B[0]:
#         res_A[0] = candidate
#     else:
#         res_B[0] = candidate

# ans = sum(res_A) + sum(res_B)

print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p = sorted(map(int, input().split()))
q = sorted(map(int, input().split()))
r = sorted(map(int, input().split()))
ans = p[-x:] + q[-y:] + r
ans.sort()
n = len(ans)
cnt = 0
for i in range(x+y):
    cnt += ans[n-1-i]
print(cnt)

Submission status: Accepted

Solution code:
x,y,a,b,c=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)[:x+y]

new=sorted(p+q+r, reverse=True)
print(sum(new[:x+y]))

Submission status: Accepted

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

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

qa.sort(reverse=True)
qb.sort(reverse=True)
r.sort(reverse=True)

qa_ans = qa[:X]
qb_ans = qb[:Y]

heapq.heapify(qa_ans)
heapq.heapify(qb_ans)

for i in range(C):
    r_max = r[i]

    qa_min = heapq.heappop(qa_ans)
    qb_min = heapq.heappop(qb_ans)

    if (r_max>qa_min) or (r_max>qb_min):
        if qa_min >qb_min:
            heapq.heappush(qa_ans,qa_min)
            heapq.heappush(qb_ans,r_max)
        else:
            heapq.heappush(qa_ans,r_max)
            heapq.heappush(qb_ans,qb_min)

    else:
        heapq.heappush(qa_ans, qa_min)
        heapq.heappush(qb_ans, qb_min)
        break

print(sum(qa_ans)+sum(qb_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()
w = p[a-x:a] + q[b-y:b] + r
w.sort()
print(sum(w[len(w)-(x+y):len(w)]))

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 = sorted(p[:x] + q[:y])
ans = sum(pq)
i = 0
while i < min(a+b, c):
  if r[i] <= pq[i]:
    break
  ans += r[i] - pq[i]
  i += 1
print(ans)

Submission status: Accepted

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

p=p[:x]
q=q[:y]
pq=p+q
pqr=pq+r
pqr.sort(reverse=True)
print(sum(pqr[:x+y]))

Submission status: Accepted

Solution code:
from collections import deque
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=deque(sorted(list(map(int,input().split())),reverse=True))
p=deque(p[:x])
q=deque(q[:y])
ans=sum(p)+sum(q)

while p[-1]<r[0] or q[-1]<r[0]:
  if p[-1]<q[-1]:
    ans+=r[0]-p[-1]
    r.popleft()
    p.pop()
  else:
    ans+=r[0]-q[-1]
    r.popleft()
    q.pop()
  if p==deque([]) or q==deque([]) or r==deque([]):
    break
if r==deque([]):
  pass
elif p==deque([]):
  while q[-1]<r[0]:
    ans+=r[0]-q[-1]
    r.popleft()
    q.pop()
    if q==deque([]) or r==deque([]):
      break
elif q==deque([]):
  while p[-1]<r[0]:
    ans+=r[0]-p[-1]
    r.popleft()
    p.pop()
    if p==deque([]) or r==deque([]):
      break
  
print(ans)

Submission status: Accepted

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

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

p = p[(A-X):]
q = q[(B-Y):]

i_p = 0
i_q = 0
for i_r in range(C):
    if i_p >= len(p):
        if q[i_q] < r[i_r]:
            q[i_q] = r[i_r]
            i_q += 1
    elif i_q >= len(q):
        if p[i_p] < r[i_r]:
            p[i_p] = r[i_r]
            i_p += 1
    elif p[i_p] <= q[i_q]:
        if p[i_p] < r[i_r]:
            p[i_p] = r[i_r]
            i_p += 1
    else:
        if q[i_q] < r[i_r]:
            q[i_q] = r[i_r]
            i_q += 1

    if i_p >= len(p) and i_q >= len(q):
        break

res = 0

for i in range(len(p)):
    res += p[i]

for i in range(len(q)):
    res += q[i]

print(res)