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: Time Limit Exceeded

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

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

red = p[:X]
green = q[:Y]
ri = 1
gi = 1

while True :
    if not r :
        break
    red_min = min(red[-ri], red[-1])
    green_min = min(green[-gi], green[-1])
    if red_min > green_min :
        if green_min < r[0] :
            green.remove(green_min)
            green.append(r[0])
            gi += 1
            r.pop(0)
        else :
            break
    else :
        if red_min < r[0] :
            red.remove(red_min)
            red.append(r[0])
            ri += 1
            r.pop(0)
        else :
            break

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
from collections import deque
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
q.sort()
r.sort(reverse=True)
red_que = p[a-x:]
green_que = q[b-y:]
red_count = 0
green_count = 0
for i in range(c):
    if red_que[red_count] < green_que[green_count]:
        if red_que[red_count] < r[i]:
            red_que[red_count] = 0
            red_count += 1
            bisect.insort_left(red_que, r[i])
        else:
            break
    else:
        if green_que[green_count] < r[i]:
            green_que[green_count] = 0
            green_count += 1
            bisect.insort_left(green_que, r[i])
        else:
            break
print(sum(green_que) + sum(red_que))

Submission status: Time Limit Exceeded

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=max(p)
Q=max(q)
R=max(r)
M=0
def compere(a,A,W):
    global r,R,M
    for i in range(W):
        if A>=R:
            index=a.index(A)
            M+=a.pop(index)
            if a:
                A=max(a)
        else:
            index=r.index(R)
            M+=r.pop(index)
            if r:
                R=max(r)
compere(p,P,X)
compere(q,Q,Y)

print(M)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

p=sorted(r[:x]+g[:y],reverse=True)

i=0
ans=0
while i<x+y:
    if w==[] or p[0]>w[0]: 
        ans+=p.pop(0)
    else: 
        ans+=w.pop(0)
    i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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())))
P_=P[A-X:]
Q_=Q[B-Y:]
while min(P_[0],Q_[0])<R[-1]:
    min(P_,Q_).pop(0)
    min(P_,Q_).append(R.pop(-1))
    if len(R)==0:
        break
print(sum(P_)+sum(Q_))

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c=map(int,input().split()) #x,yだけ見る。
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
s=sorted(p+q+r, reverse=True)
cntr,cntg=0,0
ans=0
for i in range(a+b+c):
    if cntr!="f" and (s[i] in p or s[i] in r):
        ans+=s[i]
        cntr+=1
    elif cntg!="f" and ( s[i] in q or s[i] in r):
        ans+=s[i]
        cntg+=1
    if cntr==x:
        cntr="f"
    if cntg==y:
        cntg="f"
print(ans)

Submission status: Time Limit Exceeded

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

while 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:
    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:
    j+=1
    k+=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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

white = sorted(white, reverse=True)
while len(white) != 0:
    current = white.pop(0)
    if red[-1] < green[-1] and current > red[-1]:
        ans += current - red[-1]
        red.pop(-1)
    elif red[-1] >= green[-1] and current > green[-1]:
        ans += current - green[-1]
        green.pop(-1)
    else:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
from collections import deque
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
red_que = p[:x]
green_que = q[:y]
red_que.reverse()
green_que.reverse()
for i in range(c):
    if red_que[0] < green_que[0]:
        if red_que[0] < r[i]:
            del red_que[0]
            bisect.insort_left(red_que, r[i])

    else:
        if green_que[0] < r[i]:
            del green_que[0]
            bisect.insort_left(green_que, r[i])
print(sum(green_que) + sum(red_que))

Submission status: Time Limit Exceeded

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=max(p)
Q=max(q)
R=max(r)
M=0
for i in range(X):
    if P>=R:
        index=p.index(P)
        M+=p.pop(index)
        if p:
            P=max(p)
    else:
        index=r.index(R)
        M+=r.pop(index)
        if r:
            R=max(r)
for j in range(Y):
    if Q>=R:
        index=q.index(Q)
        M+=q.pop(index)
        if q:
            Q=max(q)
    else:
        index=r.index(R)
        M+=r.pop(index)
        if r:
            R=max(r)
print(M)

Submission status: Time Limit Exceeded

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)

z = sorted(p+q+r,reverse=True)
ans = 0
c = 0
for i in z:
    if x+y-c==0:
        break
    elif p and i == p[0] and x>0:
        ans+=i
        p.pop(0)
        x -= 1
    elif q and i == q[0] and y>0:
        ans += i
        q.pop(0)
        y -= 1
    elif i == r[0] and x+y-c>0:
        ans += i
        r.pop(0)
        c += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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())))
P_=P[A-X:]
Q_=Q[B-Y:]
while P_[0]<R[-1] or Q_[0]<R[-1]:
    if P_[0]<Q_[0]:
        P_.pop(0)
        P_.append(R.pop(-1))
    else:
        Q_.pop(0)
        Q_.append(R.pop(-1))
    if len(R)==0:
        break
print(sum(P_)+sum(Q_))

Submission status: Time Limit Exceeded

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


for i in range(len(r)):
    p_min = min(p)
    q_min = min(q)
    r_max = max(r)

    if p_min > r_max and q_min > r_max:
        break

    p_min_diff = r_max - p_min
    q_min_diff = r_max - q_min

    if p_min_diff > q_min_diff and p_min_diff > 0:
        p.remove(p_min)
        p.append(r_max)
        r.remove(r_max)
    elif q_min_diff > p_min_diff and q_min_diff > 0:
        q.remove(q_min)
        q.append(r_max)
        r.remove(r_max)

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

Submission status: Time Limit Exceeded

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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

red_counter = 0
green_counter = 0

ans = 0

for i in range(X+Y):
  if red_counter == X:
    P = [-1]
  elif green_counter == Y:
    Q = [-1]

  if len(R) != 0:
    if P[0] >= Q[0] and P[0] >= R[0]:
      ans += P.pop(0)
      red_counter += 1
    elif Q[0] >= P[0] and Q[0] >= R[0]:
      ans += Q.pop(0)
      green_counter +=1
    else:
      ans += R.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 20:52:57 2020

@author: over-
"""

X,Y,A,B,C=input().split()
X=int(X)
Y=int(Y)
P=input().split()
Q=input().split()
R=input().split()

P=[int(i) for i in P]
P.sort(reverse=True)
Q=[int(i) for i in Q]
Q.sort(reverse=True)
R=[int(i) for i in R]
R.sort(reverse=True)

P=P[:X]
Q=Q[:Y]

ans=sum(P)+sum(Q)
for r in R:
    if P[-1] >= r and Q[-1] >= r: break
    if P[-1]>Q[-1]:
        if Q[-1]<r:
            ans+=r-Q[-1]
            Q=Q[:-1]
    else:
        if P[-1]<r:
            ans+=r-P[-1]
            P=P[:-1]

print(ans)

Submission status: Time Limit Exceeded

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[0:x]
Q = Q[0:y]

while P[-1] < R[0] or Q[-1] < R[0]:
    if Q[-1] >= P[-1]:
        P[-1] = R.pop(0)
        P.sort(reverse = True)
    else:
        Q[-1] = R.pop(0)
        Q.sort(reverse=True)

    if R == []:
        break

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

Submission status: Time Limit Exceeded

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)

z = sorted(p+q+r,reverse=True)
ans = 0
c = 0
for i in z:
    if x+y-c==0:
        break
    elif i in p and x>0:
        ans+=i
        p.pop(0)
        x -= 1
    elif i in q and y>0:
        ans += i
        q.pop(0)
        y -= 1
    elif i in r and x+y-c>0:
        ans += i
        c += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = [int(i) for i in input().split()]
lp = sorted([int(i) for i in input().split()])
if len(lp) != x:
  del lp[:len(lp)-x]
lq = sorted([int(i) for i in input().split()])
if len(lq) != y:
  del lq[:len(lq)-y]
lr = sorted([int(i) for i in input().split()])

while True:
  if len(lr) == 0:
    break
  r_max = lr[len(lr)-1]
  p_min = lp[0]
  q_min = lq[0]
  if r_max <= p_min and r_max <= q_min:
    break
  elif r_max > q_min and p_min >= q_min:
    del lq[0]
    lq.append(lr.pop(-1))
  elif r_max > p_min and q_min >= p_min:
    del lp[0]
    lp.append(lr.pop(-1))


print(sum(lp) + sum(lq))

Submission status: Time Limit Exceeded

Solution code:
from copy import copy

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


ans = 0
for flag in range(1<<len(r)):
    P = copy(p)
    Q = copy(q)
    for i in range(len(r)):
        if flag & 1<<i:
            P += [r[i]]
        else:
            Q += [r[i]]
    P.sort()
    Q.sort()
    sums = sum(P[-X:]) + sum(Q[-Y:])
    if sums > ans:
        ans = sums

print(ans)

Submission status: Time Limit Exceeded

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)
np=p[0:x]
nq=q[0:y]
np.extend(nq)
np.sort()
for i in range(min(len(np),len(r))):
    if np[i]<r[0]:
        np[i]=r.pop(0)
print(sum(np))

Submission status: Time Limit Exceeded

Solution code:
import os
import statistics

def quickSort(arr):
  left = []
  right = []
  if len(arr) <= 1:
   return arr
  median = int(statistics.median(arr))
  medianFlg = 0

  for element in arr:
   if element < median:
    left.append(element)
   elif element > median:
    right.append(element)
   else:
    medianFlg += 1
  left = quickSort(left)
  right = quickSort(right)
  return left + [median] * medianFlg + right

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()))
P0=quickSort(P)
Q0=quickSort(Q)
S=P0[A-X:A]+Q0[B-Y:B]+R
S0=quickSort(S)
D=0
for r in range(X+Y):
 D+=S0[C+r]
print(D)

Submission status: Time Limit Exceeded

Solution code:
x, y, A, B, C = list(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)

s = 0

a = a[:x]
b = b[:y]
s += sum(a)
s += sum(b)

a_nex = a.pop()
b_nex = b.pop()
c_nex = c.pop()

while True:
#     print(s)
    if a_nex > b_nex:
        if c_nex > b_nex:
            s = s - b_nex + c_nex
            if len(b) == 0:
                b_nex = int(2e9)
            else:
                b_nex = b.pop()
            if len(c) == 0:
                break
            c_nex = c.pop()

    if b_nex > a_nex:
        if c_nex > a_nex:
            s = s - a_nex + c_nex
            if len(a) == 0:
                a_nex = int(2e9)
            else:
                a_nex = a.pop()
            if len(c) == 0:
                break
            c_nex = c.pop()

    if c_nex <= a_nex and c_nex <= b_nex:
        break

print(s)

Submission status: Time Limit Exceeded

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()))
tmp=sorted(p)
P=tmp[::-1]
tmp=sorted(q)
Q=tmp[::-1]
tmp=sorted(r)
R=tmp[::-1]
ans=0
while(1):
	if X==0 and Y==0:
		print(ans)
		break
	elif X>0 and Y>0:
		tmp=max(P[0],Q[0],R[0])
		ans+=tmp
		if tmp==P[0]:
			P=P[1:]
			X-=1
		elif tmp==Q[0]:
			Q=Q[1:]
			Y-=1
		elif tmp==R[0]:
			if P[0]>=Q[0]:
				R=R[1:]
				Y-=1
			else:
				R=R[1:]
				X-=1
	elif X==0:
		tmp=max(Q[0],R[0])
		ans+=tmp
		if tmp==Q[0]:
			Q=Q[1:]
			Y-=1
		elif tmp==R[0]:
			R=R[1:]
			Y-=1
	elif Y==0:
		tmp=max(P[0],R[0])
		ans+=tmp
		if tmp==P[0]:
			P=P[1:]
			X-=1
		elif tmp==R[0]:
			R=R[1:]
			X-=1

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, heappushpop

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]
ans_list = p[:X] + q[:Y]  # 着色しない場合
heapify(ans_list)
m = min(ans_list)
for ri in r:
    if ri > m:
        heappushpop(ans_list, ri)
        m = min(ans_list)
    else:
        break
print(sum(ans_list))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, heappushpop

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]
ans_list = p[:X] + q[:Y]  # 着色しない場合
heapify(ans_list)
for ri in r:
    if ri > min(ans_list):
        heappushpop(ans_list, ri)
    else:
        break
print(sum(ans_list))

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)
apples = p[:x] + q[:y] + r
apples.sort(reverse=True)
print(sum(apples[: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)
p = p[:x]
q = q[:y]
l = p + q + r
l.sort(reverse = True)
print(sum(l[:x+y]))

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    x, y, a, b, c = input_int_list()
    P = [-1] + sorted(input_int_list())  # 番兵
    Q = [-1] + sorted(input_int_list())
    R = [-1] + sorted(input_int_list())
    A = []
    B = []
    C = []
    for i in range(x + y):
        if R[-1] >= P[-1] and R[-1] >= Q[-1]:
            C.append(R.pop())
        elif P[-1] >= Q[-1] and P[-1] >= R[-1]:
            A.append(P.pop())
        else:
            B.append(Q.pop())
    if len(A) <= x and len(B) <= y:
        print(sum(A) + sum(B) + sum(C))
    elif len(A) > x:
        for j in range(len(A) - x):
            if R[-1] >= Q[-1]:
                C.append(R.pop())
            else:
                B.append(Q.pop())
        print(sum(A[:x]) + sum(B) + sum(C))
    elif len(B) > y:
        for j in range(len(B) - y):
            if R[-1] >= P[-1]:
                C.append(R.pop())
            else:
                A.append(P.pop())
        print(sum(A) + sum(B[:y]) + sum(C))

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq
X, Y, A, B, C = map(int, input().split())
AL = list(map(int, input().split()))
BL = list(map(int, input().split()))
CL = list(map(int, input().split()))
h = []
for a in AL:
  heapq.heappush(h, (-a, 0))
for b in BL:
  heapq.heappush(h, (-b, 1))
for c in CL:
  heapq.heappush(h, (-c, 2))
answer = 0
x_cnt = 0
y_cnt = 0
eat_cnt = 0
for _ in range(A+B+C):
  if eat_cnt >= X+Y:
    break
  d, t = heapq.heappop(h)
  if t == 0 and x_cnt+1 <= X:
    answer += -d
    x_cnt += 1
    eat_cnt += 1
  elif t == 1 and y_cnt+1 <= Y:
    answer += -d
    y_cnt += 1
    eat_cnt += 1
  elif t == 2:
    answer += -d
    eat_cnt += 1
print(answer)

Submission status: Accepted

Solution code:
import heapq
X,Y,A,B,C = map(int,input().split())
Plist = list(map(int,input().split()))
Qlist=  list(map(int,input().split()))
Rlist  = list(map(int,input().split()))
Plist.sort(reverse = True)
Qlist.sort(reverse = True)
Rlist.sort(reverse = True)
i,j,k = 0,0,0
pmax = Plist[i]
qmax = Qlist[i]
rmax = Rlist[i]
Ans = 0
for _ in range(2*10**5):
    Ans += max(pmax,qmax,rmax)
    if pmax == max(pmax,qmax,rmax):
        i += 1
        if i != X:
            pmax = Plist[i]
        else:
            pmax = -1
    elif qmax == max(pmax,qmax,rmax):
        j += 1
        if j != Y:
            qmax = Qlist[j]
        else:
            qmax = -1
    else:
        k += 1
        try:
            rmax = Rlist[k]
        except:
            rmax = -1
    if i+j+k == X+Y:
        break
print(Ans)

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
p=[0]+sorted(list(map(int,input().split())))[a-x:]
q=[0]+sorted(list(map(int,input().split())))[b-y:]
r=[0]+sorted(list(map(int,input().split())))
pi=-1
qi=-1
ri=-1
gs=0
rs=0
ws=0
s=0              
while gs+rs+ws!=x+y:
    if r[ri]>=q[qi]>=p[pi]:
        s += r[ri]
        ws+=1
        ri-=1

    elif r[ri]>=p[pi]>=q[qi]:
        s += r[ri]
        ws += 1
        ri -= 1

    elif q[qi]>=r[ri]>=p[pi]:
        s += q[qi]
        gs += 1
        qi -= 1

    elif q[qi]>=p[pi]>=r[ri]:
        s += q[qi]
        gs += 1
        qi -= 1

    elif p[pi]>=r[ri]>=q[qi]:
        s += p[pi]
        rs += 1
        pi -= 1

    elif p[pi]>=q[qi]>=r[ri]:
        s += p[pi]
        rs += 1
        pi -= 1

print(s)

Submission status: Accepted

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

x,y,a,b,c=sep()
red=lis()
green=lis()
colorless=lis()
red.sort(reverse=True)
green.sort(reverse=True)
colorless.sort()
red=red[:x][::-1]
green=green[:y][::-1]
i=0
j=0
while(colorless):
    if j>=y and i>=x:
        break
    elif j<y and i>=x:
        if colorless[-1]>green[j]:
            green[j]=colorless[-1]
            colorless.pop()
            j+=1
        else:
            break
    elif i<x and j>=y:
        if colorless[-1]>red[i]:
            red[i]=colorless[-1]
            colorless.pop()
            i+=1
        else:
            break

    elif red[i]<=green[j]:
        if colorless[-1]>red[i]:
            red[i]=colorless[-1]
            colorless.pop()
            i+=1
        else:
            break
    else:
        if colorless[-1]>green[j]:
            green[j]=colorless[-1]
            colorless.pop()
            j+=1
        else:
            break
#print(red,green)
print(sum(red) + sum(green))

Submission status: Accepted

Solution code:
import heapq
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())),reverse=True)
q=sorted(list(map(int,input().split())),reverse=True)
r=list(map(lambda x:-int(x),input().split()))
heapq.heapify(r)
for i in range(x):
    heapq.heappush(r,-p[i])
for i in range(y):
    heapq.heappush(r,-q[i])
ans=0
for i in range(x+y):
    ans-=heapq.heappop(r)
print(ans)

Submission status: Accepted

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

pA = list(map(lambda x: (int(x), "a"), input().split()))
pB = list(map(lambda x: (int(x), "b"), input().split()))
pC = list(map(lambda x: (int(x), "c"), input().split()))

apples = sorted(pA + pB + pC)

cntA = 0
cntB = 0
cntC = 0
cnt = 0

while cntA + cntB + cntC < x+y:
    val, col = apples.pop()

    if col == "a" and cntA < x:
        cntA += 1
        cnt += val
    elif col == "b" and cntB < y:
        cntB += 1
        cnt += val
    elif col == "c":
        cntC += 1
        cnt += val

print(cnt)

Submission status: Accepted

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

p_array_sort = sorted(p_array, reverse=True)
q_array_sort = sorted(q_array, reverse=True)
r_array_sort = sorted(r_array, reverse=True)

p_i = x - 1
q_i = y - 1

for r in r_array_sort:
    p = p_array_sort[p_i]
    q = q_array_sort[q_i]
    if r <= p and r <= q:
        break
    elif q <= p:
        q_array_sort[q_i] = r
        q_i = max(q_i - 1, 0)
    else:
        p_array_sort[p_i] = r
        p_i = max(p_i - 1, 0)

ans = sum(p_array_sort[:x]) + sum(q_array_sort[:y])
print(ans)

Submission status: Accepted

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

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

    ans = sum(sorted(P[:X] + Q[:Y] + R, reverse=True)[:X + Y])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

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

    print(sum(apple[: X + Y]))

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

l = p2 + q2 + r
l.sort(reverse=True)

ans = sum(l[:x+y])
print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p = [0]+list(map(int, input().split()))
q = [0]+list(map(int, input().split()))
r = [0]+list(map(int, input().split()))
p.sort()
q.sort()
r.sort()
ans = 0
num = x+y
for _ in range(num):
    if x > 0 and y > 0:
      i, j, k = p[-1], q[-1], r[-1]
      if i >= j and i >= k:
          ans += i
          p.pop()
          x -= 1
      elif j >= i and j >= k:
          ans += j
          q.pop()
          y -= 1
      else:
          ans += k
          r.pop()
    elif x > 0:
        i, j = p[-1], r[-1]
        x -= 1
        if i >= j:
            ans += i
            p.pop()
        else:
            ans += j
            r.pop()
    elif y > 0:
        y -= 1
        i, j = q[-1], r[-1]
        if i >= j:
            ans += i
            q.pop()
        else:
            ans += j
            r.pop()
    
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.sort()
p=p[len(p)-X:]
q.sort()
q=q[len(q)-Y:]
r.sort(reverse=True)
p=deque(p)
q=deque(q)

for i in r:
  if min(p[0],q[0])>i:break
  if p[0]<q[0]:
    p.popleft()
    p.append(i)
  else:
    q.popleft()
    q.append(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()
q.sort()


sin=p[a-x:]+q[b-y:]+r

sin.sort()

print(sum(sin[len(sin)-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]

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

p.append(float("INF"))
q.append(float("INF"))
r.append(-1)

while p[i] < r[k] or q[j] < r[k]:
    if p[i] < q[j]:
        i -= 1
        k += 1
    else:
        j -= 1
        k += 1

ans = 0
if i != -1:
    ans += sum(p[:i+1])
if j != -1:
    ans += sum(q[:j+1])
if k != 0:
    ans += sum(r[:k])

print(ans)

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest
import heapq

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    X,Y,a,b,c=map(int, readline().rstrip().split())

    R=list(map(lambda x: int(x)*(-1), readline().rstrip().split()))
    G=list(map(lambda x: int(x)*(-1), readline().rstrip().split()))
    C=list(map(lambda x: int(x)*(-1), readline().rstrip().split()))

    heapq.heapify(R)
    heapq.heapify(G)
    heapq.heapify(C)
    ans = []
    heapq.heapify(ans)

    for _ in range(X):
        a = heapq.heappop(R)
        heapq.heappush(ans,a)
    for _ in range(Y):
        a = heapq.heappop(G)
        heapq.heappush(ans,a)
    for _ in range(min(X+Y,c)):
        a = heapq.heappop(C)
        heapq.heappush(ans,a)
    a=0
    for _ in range(X+Y):
        a += heapq.heappop(ans) * -1

    print(a)
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

x,y,a,b,c=nii()
p=sorted(lnii(),reverse=True)
q=sorted(lnii(),reverse=True)
r=sorted(lnii(),reverse=True)

l=p[:x]+q[:y]
l.sort()

ans=0
for i in range(x+y):
  if i<c:
    ans+=max(l[i],r[i])
  else:
    ans+=l[i]

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

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

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

l=p[:x]+q[:y]
l.sort()

ans=0
for i in range(x+y):
  if i<c:
    ans+=max(l[i],r[i])
  else:
    ans+=l[i]

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

s = p[:X]+q[:Y]
ans=sum(s)
heapq.heapify(s)

while r and s:
    if s[0]<r[-1]:
        ans += r[-1]
        r.pop()
        ans -= heapq.heappop(s)
    else:
        break
print(ans)