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:
from bisect import insort_left

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]
i = 0
j = 0
k = 0
while(True):
    if P[j] >= R[i] and Q[k] >= R[i]:
        break
    if P[j] > Q[k]:
        if Q[k] < R[i]:
            insort_left(Q, R[i])
            k += 1
    else:
        if P[j] < R[i]:
            insort_left(P, R[i])
            j += 1
    i += 1
    if i == len(R):
        break
print(sum(P[j:]) + sum(Q[k:]))

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = map(int,input().split())
red = sorted(list(map(int,input().split())))[a-x:]
green = sorted(list(map(int,input().split())))[b-y:]
white = sorted(list(map(int,input().split())))
rcnt = 0
gcnt = 0
ans = 0
#print(red,green,white)

for i in range(x+y):
#    print(red,green,white)    
    if red[-1] == max(red[-1],green[-1],white[-1]):
#        print('hoge')
        rcnt += 1
        ans += red.pop(-1)
        if len(red) == 0:
            red = [0]
    elif green[-1] == max(red[-1],green[-1],white[-1]):
#        print('huga')
        gcnt += 1
        ans += green.pop(-1)
        if len(green) == 0:
            green = [0]
    else:
        if red[0] < green[0]:
            rcnt += 1
            red.pop(0)
            if len(red) == 0:
                red = [0]
        else:
            gcnt += 1
            green.pop(0)
            if len(green) == 0:
                green = [0]
#        print('foo')
        ans += white.pop(-1)
        if len(white) == 0:
            white = [0]


print(ans)

Submission status: Time Limit Exceeded

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

eat = red[0:x:] + green[0:y:]
eat.sort()
i = 0
while clear[i] > eat[0]:
    eat[0] = clear[i]
    clear[i] = 0
    eat.sort()
    i += 1
    if i > c - 1:
        break
print(sum(eat))

Submission status: Time Limit Exceeded

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

pa = sorted(pa, reverse=True)
pa = pa[:x]
pb = sorted(pb, reverse=True)
pb = pb[:y]
pc = sorted(pc, reverse=True)

l = pa + pb
l = sorted(l, reverse=True)

for i in range(len(pc)):
  for j in range(len(l)):
    if pc[i] > l[j]:
      l[j] = pc[i]
    if j == len(l)-1:
      break
      
print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
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()))
al.sort()
bl.sort()
cl.sort()
result = 0
i = a-1
j = a-x
while i>=j:
  if(al[i]>= max(cl)):
    result = result+al[i]
  else:
    result = result+max(cl)
  i-=1
k = b-1
l = b-y
while k>=l:
  if(bl[k]>= max(cl)):
    result = result+bl[k]
  else:
    result = result +max(cl)
  k-=1
print(result)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

import bisect

X, Y, A, B, C = map(int, input().rstrip().split())
a = [-1 * x for x in sorted(list(map(int, input().rstrip().split())))[::-1]]
b = sorted(list(map(int, input().rstrip().split())))[::-1] + [-1]
c = sorted(list(map(int, input().rstrip().split())))[::-1] + [-1]

color = []
bc = []
ib = 0
ic = 0
for i in range(B + C): 
    if b[ib] > c[ic]:
        bc.append(b[ib])
        color.append(0)
        ib += 1
    else:
        bc.append(c[ic])
        color.append(1)
        ic += 1
bc += [-1]
color += [0]
        
for i in range(Y, len(bc)):
    if color[i] == 1:
        bisect.insort(a, -bc[i])
  
ib = Y
total = 0
for i in range(Y):
    if color[Y-i-1] == 0:
        total += bc[Y-i-1]
    else: # 透明の場合
        if -a[X-1] < bc[Y-i-1]:
            if -a[X-1] < bc[ib]: # 無色を移動
                total += bc[ib]
                bisect.insort(a, -bc[Y-i-1])
                ib += 1
                while color[ib] == 1: # 範囲外の無職は使用不可
                    ib += 1
            else:
                total += bc[Y-i-1]
        else:
            total += bc[Y-i-1]
           
print(total - sum(a[:X]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    X, Y, A, B, C = map(int, input().split())
    pA = sorted(map(int, input().split()), reverse=True)
    pB = sorted(map(int, input().split()), reverse=True)
    pC = sorted(map(int, input().split()), reverse=True)

    # dp[red_count][green_count] = (value, red-index, green-index, non-index)
    dp = [[(0, 0, 0, 0) for _ in range(Y + 1)] for _ in range(X + 1)]
    for ri in range(0, X + 1):
        for gi in range(0, Y + 1):
            if 0 < ri:
                v, r, g, n = dp[ri - 1][gi]
                dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                                 (v + pC[n] if n < C else v, r, g, n + 1))

            if 0 < gi:
                v, r, g, n = dp[ri][gi - 1]
                dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                                 (v + pC[n] if n < C else v, r, g, n + 1))

    print(dp[X][Y][0])


main()

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
X, Y, A, B, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

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

A = A[0:X]
B = B[0:Y]
C = np.array(C)

arr = A+B
_min = min(arr)
C = C[C>_min]

for c in C:
  _min = min(arr)
  if c >= _min:
    arr[arr.index(_min)] = c

print(sum(arr))

Submission status: Time Limit Exceeded

Solution code:
from bisect import insort_left

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]
i = 0
while(True):
    if P[0] >= R[i] and Q[0] >= R[i]:
        break
    if P[0] > Q[0]:
        if Q[0] < R[i]:
            insort_left(Q, R[i])
            Q = Q[1:]
    else:
        if P[0] < R[i]:
            insort_left(P, R[i])
            P = P[1:]
    i += 1
    if i == len(R):
        break
print(sum(P) + sum(Q))

Submission status: Time Limit Exceeded

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

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

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

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

EP.sort()
EQ.sort()

for i in range(C):
    if R[i]>EP[0] or R[i]>EQ[0]:
        if EP[0]<EQ[0]:
            EP.pop(0)
            EP.append(R[i])
        else:
            EQ.pop(0)
            EQ.append(R[i])
    else:
        break

print(sum(EP)+sum(EQ))

Submission status: Time Limit Exceeded

Solution code:
import bisect


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)

for max_r in r:
    if ans_p[0] < ans_q[0]:
        if ans_p[0] < max_r:
            del ans_p[0]
            bisect.insort(ans_p, max_r)
        else:
            break
    else:
        if ans_q[0] < max_r:
            del ans_q[0]
            bisect.insort(ans_q, max_r)
        else:
            break
print(sum(ans_p)+sum(ans_q))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
X,Y,A,B,C=map(int,input().split())
p=deque(sorted(map(int,input().split())))
q=deque(sorted(map(int,input().split())))
r=deque(sorted(map(int,input().split())))
ans=[]
for i in range(X):
	ans.append(p.pop())
for i in range(Y):
	ans.append(q.pop())
for i in r:
	if i>min(ans):
		ans.remove(min(ans))
		ans.append(i)
print(sum(ans))

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)

red=p[:x]
grn=q[:y]
#print(red)
#print(grn)
eat=sorted(red+grn,reverse=True)
#print(eat)
for i in r:
  if i> eat[-1]:
    eat=[i]+eat[:-1]
  else:
    break
#print(eat)
print(sum(eat))

Submission status: Time Limit Exceeded

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

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

EP=[]
EQ=[]

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

EP.sort()
EQ.sort()

for i in range(C):
    if R[i]>EP[0] or R[i]>EQ[0]:
        if EP[0]<EQ[0]:
            EP.pop(0)
            EP.append(R[i])
        else:
            EQ.pop(0)
            EQ.append(R[i])
    else:
        break

print(sum(EP)+sum(EQ))

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)


ans = 0

while (p[0] > r[0]):
    ans += p.pop(0)
    x -= 1
    if (x == 0):
        break
while (q[0] > r[0]):
    ans += q.pop(0)
    y-=1
    if (y == 0):
        break

if (x == 0 and y == 0):
    print(ans)
else:
    aps = sorted(p[0: x] + q[0: y] + r[0: x + y], reverse=True)
    #print(aps)
    print(ans+sum(aps[0:x+y]))

Submission status: Time Limit Exceeded

Solution code:
import bisect


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)
for max_r in r:
    if ans_p[0] < ans_q[0]:
        if ans_p[0] < max_r:
            del ans_p[0]
            bisect.insort(ans_p, max_r)
        else:
            break
    else:
        if ans_q[0] < max_r:
            del ans_q[0]
            bisect.insort(ans_q, max_r)
        else:
            break
print(sum(ans_p)+sum(ans_q))

Submission status: Time Limit Exceeded

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

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  insort(p_list,  int(value))

for value in input().split():
  insort(q_list,  int(value))
  
for value in input().split():
  insort(r_list,  int(value))
  
red = 0
green = 0
white = 0

sum_num = 0
for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1
    
print(sum_num)

Submission status: Time Limit Exceeded

Solution code:
from bisect import insort_left

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]:
            insort_left(Q, R[0])
            Q = Q[1:]
    else:
        if P[0] < R[0]:
            insort_left(P, R[0])
            P = P[1:]
    R = R[1:]
    if len(R) == 0:
        break
print(sum(P) + sum(Q))

Submission status: Time Limit Exceeded

Solution code:
f=lambda:map(int,input().split())
x,y,a,b,c=f()
p=sorted(f())[::-1]
q=sorted(f())[::-1]
r=sorted(f())[::-1]
lp,lq,lr=[0]*(a+1),[0]*(b+1),[0]*(c+1)
s=t=u=m=0
for i in range(a): s+=p[i]; lp[i+1]+=s
for i in range(b): t+=q[i]; lq[i+1]+=t
for i in range(c): u+=r[i]; lr[i+1]+=u
for i in range(min(x,c)+1):
  for j in range(min(y,c-i)+1):
    m=max(m,lp[x-i]+lq[y-j]+lr[i+j])
print(m)

Submission status: Time Limit Exceeded

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

ri=sorted(r)[::-1]

qi=q[::]
pi=p[::]
if x>a:
    pi+=ri[:x-a]
    ri=ri[x-a:]
if y>b:
    qi+=ri[:y-b]
    ri=ri[y-b:]

pi=sorted(pi)[::-1]
pi=pi[:x]
qi=sorted(qi)[::-1]
qi=qi[:y]
heapq.heapify(pi)
heapq.heapify(qi)

try:
    while pi[0]<ri[0] or qi[0]<ri[0]:
        if pi[0]<qi[0]:
            heapq.heappop(pi)
            heapq.heappush(pi,ri[0])
            ri.pop(0)
        else:
            heapq.heappop(qi)
            heapq.heappush(qi,ri[0])
            ri.pop(0)
except IndexError:
    ri=ri
print(sum(pi)+sum(qi))

Submission status: Time Limit Exceeded

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

def f(n):
    if n == 0:
        return sum(red_and_green)
    if n > X + Y:
        return sum(rs)
    return sum(red_and_green[:-n] + rs[:n])

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

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

# print([f(i) for i in range(C)])
print(max([f(i) for i in range(C + 1)]))

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)

ns = [p.pop(0), q.pop(0), r.pop(0)]

z = 0
val = 0

while x + y - z > 0:
    if ns[0] >= ns[1] and ns[0] >= ns[2]:
        val += ns[0]
        x -= 1
        if len(p) > 0 and x > 0:
            ns[0] = p.pop(0)
        else:
            ns[0] = 0
    elif ns[0] <= ns[1] and ns[1] >= ns[2]:
        val += ns[1]
        y -= 1
        if len(q) > 0 and y > 0:
            ns[1] = q.pop(0)
        else:
            ns[1] = 0
    else:
        val += ns[2]
        z += 1
        if len(r) > 0:
            ns[2] = r.pop(0)
        else:
            ns[2] = 0

print(val)

Submission status: Time Limit Exceeded

Solution code:
eat_RED,eat_GR,hav_RED,hav_GR,hav_CL=map(int, input().split())
RED_yam=list(map(int, input().split()))
GR_yam=list(map(int, input().split()))
CL_yam=list(map(int, input().split()))

RED_sub=[]
GR_sub=[]
for i in range(eat_RED):
    RED_sub.append(max(RED_yam))
    RED_yam.remove(max(RED_yam))
for i in range(eat_GR):
    GR_sub.append(max(GR_yam))
    GR_yam.remove(max(GR_yam))
flag=True
while flag:
    flag=False
    if len(CL_yam)>0:
        if min(RED_sub)<max(CL_yam) and min(GR_sub)<max(CL_yam):
            flag=True
            if min(RED_sub)<min(GR_sub):
                RED_sub.remove(min(RED_sub))
                RED_sub.append(max(CL_yam))
                CL_yam.remove(max(CL_yam))
            else:
                GR_sub.remove(min(GR_sub))
                GR_sub.append(max(CL_yam))
                CL_yam.remove(max(CL_yam))
        elif min(RED_sub)<max(CL_yam):
            flag=True
            RED_sub.remove(min(RED_sub))
            RED_sub.append(max(CL_yam))
            CL_yam.remove(max(CL_yam))
        elif min(GR_sub)<max(CL_yam):
            flag=True
            GR_sub.remove(min(GR_sub))
            GR_sub.append(max(CL_yam))
            CL_yam.remove(max(CL_yam))
print(sum(GR_sub)+sum(RED_sub))

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 = sorted(p)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
while found and R >= 0:
    if (r[R] > p[P]):
        if (q[Q] > p[P]):
            P += 1
            R -= 1
    elif (r[R] > q[Q]):
        Q += 1
        R -= 1
    else:
        found = False
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

Submission status: Time Limit Exceeded

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

red = sorted(list(map(int, input().split())), reverse=True)
gre = sorted(list(map(int, input().split())), reverse=True)
non = sorted(list(map(int, input().split())), reverse=True)

eat = []

for i in range(x):
    eat.append(red.pop(0))

for i in range(y):
    eat.append(gre.pop(0))

eat.sort()

for i in range(len(non)):
    n = non.pop(0)
    if eat[i] < n:
        eat[i] = n
    else:
        break

print(sum(eat))

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys
from itertools import accumulate

input = sys.stdin.readline

def main():
    X, Y, A, B, C = map(int, input().split())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    R = list(map(int, input().split()))
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    P_acc = [0] + list(accumulate(P))
    Q_acc = [0] + list(accumulate(Q))
    R_acc = [0] + list(accumulate(R))
    ans = 0
    for i in range(X+1):
        tmp = 0
        red = i
        tmp += P_acc[red]
        if C < X - i:
            continue
        no_color = X - i
        tmp += R_acc[X-i]
        blue = Q_acc[Y]
        b_h = Q[:Y]
        heapq.heapify(b_h)
        for j in range(X-i,C):
            a = heapq.heappop(b_h)
            if R[j] > a:
                heapq.heappush(b_h, R[j])
                blue -= a
                blue += R[j]
            else:
                break
        tmp += blue
        ans = max(ans, tmp)

    print(ans)







if __name__ == "__main__":
    main()

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)
pp = p[:]
q = sorted(list(map(int, input().split())), reverse=True)
qq = q[:]
r = sorted(list(map(int, input().split())), reverse=True)
rr = r[:]
ans1 = 0
for _ in range(x):
    if len(p) > 0 and len(r) > 0:
        if p[0] >= r[0]:
            ans1 += p.pop(0)
        else:
            ans1 += r.pop(0)
    elif len(p) > 0:
        ans1 += p.pop(0)
    else:
        ans1 += r.pop(0)
for _ in range(y):
    if len(q) > 0 and len(r) > 0:
        if q[0] >= r[0]:
            ans1 += q.pop(0)
        else:
            ans1 += r.pop(0)
    elif len(q) > 0:
        ans1 += q.pop(0)
    else:
        ans1 += r.pop(0)

ans2 = 0
for _ in range(y):
    if len(qq) > 0 and len(rr) > 0:
        if qq[0] >= rr[0]:
            ans2 += qq.pop(0)
        else:
            ans2 += rr.pop(0)
    elif len(qq) > 0:
        ans2 += qq.pop(0)
    else:
        ans2 += rr.pop(0)
for _ in range(x):
    if len(pp) > 0 and len(rr) > 0:
        if pp[0] >= rr[0]:
            ans2 += pp.pop(0)
        else:
            ans2 += rr.pop(0)
    elif len(pp) > 0:
        ans2 += pp.pop(0)
    else:
        ans2 += rr.pop(0)
print(max(ans1, ans2))

Submission status: Time Limit Exceeded

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

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

EP=[]
EQ=[]

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

EP.sort()
EQ.sort()

while R[0]>EP[0] or R[0]>EQ[0]:
    if EP[0]<EQ[0]:
        EP.pop(0)
        EP.append(R.pop(0))
    else:
        EQ.pop(0)
        EQ.append(R.pop(0))
        
    if not(R):
        break

print(sum(EP)+sum(EQ))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import heapq
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]
heapq.heapify(red)
heapq.heapify(green)

while True :
    if not r :
        break
    if red[0] > green[0] :
        if green[0] < r[0] :
            heapq.heapreplace(green, r[0])
            r.pop(0)
        else :
            break
    else :
        if red[0] < r[0] :
            heapq.heapreplace(red, r[0])
            r.pop(0)
        else :
            break

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

Submission status: Time Limit Exceeded

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=[]
for i in range(X):
	ans.append(p.pop())
for i in range(Y):
	ans.append(q.pop())
for i in r:
	if i>min(ans):
		ans.remove(min(ans))
		ans.append(i)
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import bisect
x,y,a,b,c=map(int,input().split())
A=sorted(list(map(int,input().split())))[::-1]
B=sorted(list(map(int,input().split())))[::-1]
C=sorted(list(map(int,input().split())))[::-1]

l=[]
for i,u in enumerate(A):
    if i<x:l.append(A[i])
    else:break
for i,u in enumerate(B):
    if i<y:l.append(B[i])
    else:break
l=sorted(l)
for tt in C:
    index=bisect.bisect_left(l,tt)
    l.insert(index,tt)
l=l[::-1]
print(sum(l[:x+y]))

Submission status: Time Limit Exceeded

Solution code:
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    # ここで分割を行う
    left = arr[:mid]
    right = arr[mid:]

    # 再帰的に分割を行う
    left = merge_sort(left)
    right = merge_sort(right)

    # returnが返ってきたら、結合を行い、結合したものを次に渡す
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    # ソート済み配列をマージするため、それぞれ左から見ていくだけで良い
    while l_i < len(left) and r_i < len(right):
        # ここで=をつけることで安定性を保っている
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    # 上のwhile文のどちらかがFalseになった場合終了するため、あまりをextendする
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

Red, Green, Rnum, Gnum, Nnum = map(int, input().split())
R = list(map(int, input().split()))
G = list(map(int, input().split()))
N = list(map(int, input().split()))

R = merge_sort(R)
G = merge_sort(G)
N = merge_sort(N)

R = list(reversed(R))
G = list(reversed(G))
N = list(reversed(N))

yum = 0

# 赤・緑を食べられるだけ食べる
for i in range(Red):
    if R[0] >= N[0]:
        yum += R[0]
        Red -= 1
        del R[0]
    else:
        break;

for i in range(Green):
    if G[0] >= N[0]:
        yum += G[0]
        Green -= 1
        del G[0]
    else:
        break;

# 無色込み
for i in range(Red):
    if R[0] >= N[0]:
        yum += R[0]
        Red -= 1
        del R[0]
    else:
        yum += N[0]
        Red -= 1
        del N[0]

for i in range(Green):
    if G[0] >= N[0]:
        yum += G[0]
        Green -= 1
        del G[0]
    else:
        yum += N[0]
        Green -= 1
        del N[0]

print(yum)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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]
heapq.heapify(P)
heapq.heapify(Q)

while(True):
    if P[0] >= R[0] and Q[0] >= R[0]:
        break
    if P[0] > Q[0]:
        if Q[0] < R[0]:
            heapq.heappop(Q)
            heapq.heappush(Q, R[0])
    else:
        if P[0] < R[0]:
            heapq.heappop(P)
            heapq.heappush(P, R[0])
    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 = list(map(int, input().split()))
As = sorted(list(map(int, input().split())), reverse=True)
Bs = sorted(list(map(int, input().split())), reverse=True)
Cs = sorted(list(map(int, input().split())), reverse=True)

s = 0
_C = 0
while A+B+C > 0:
  if X+Y == _C:
    break
  if X+Y == 0:
    break
  mx = 0
  mxi = None
  for i, v in enumerate([As[0] if A>0 and X>0 else 0, Bs[0] if B>0 and Y>0 else 0, Cs[0] if C>0 else 0]):
    if v > mx:
      mx = v
      mxi = i
  if mxi == 0:
    A -= 1
    X -= 1
    As = As[1:] if len(As) > 1 else []
  elif mxi == 1:
    B -= 1
    Y -= 1
    Bs = Bs[1:] if len(Bs) > 1 else []
  elif mxi == 2:
    C -= 1
    _C += 1
    Cs = Cs[1:] if len(Cs) > 1 else []
  s += mx

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    X, Y, A, B, C = [int(x) for x in input().split()]
    P = [int(x) for x in input().split()]
    Q = [int(x) for x in input().split()]
    R = [int(x) for x in input().split()]

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

    xcnt = 0
    ycnt = 0
    rcnt = 0
    ans = 0
    while rcnt < C:
        r = R[rcnt]
        p = float("inf")
        if X - 1 - xcnt >= 0:
            p = P[X - 1 - xcnt]
        q = float("inf")
        if Y - 1 - ycnt >= 0:
            q = Q[Y - 1 - ycnt]
        if p >= r and q >= r:
            break

        if (r - p) > 0 and r - p > r - q:
            xcnt += 1
            rcnt += 1
            ans += r
        elif (r - q) > 0 and r - q > r - p:
            ycnt += 1
            rcnt += 1
            ans += r
    ans += sum(P[:X - xcnt])
    ans += sum(Q[:Y - ycnt])

    print(ans)


if __name__ == '__main__':
    main()

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:]
for i in range(X+Y):
    if len(R)==0 or min(P_[0],Q_[0])>R[-1]:
        break
    min(P_,Q_).pop(0)
    min(P_,Q_).append(R.pop(-1))
print(sum(P_)+sum(Q_))

Submission status: Time Limit Exceeded

Solution code:
import bisect
from collections import deque
import sys
input = sys.stdin.readline
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
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())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

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

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

arr = A+B

for c in C:
  _min = min(arr)
  if c >= _min:
    arr[arr.index(_min)] = c

print(sum(arr))

Submission status: Time Limit Exceeded

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

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

for i in range(len(no)):
    minX = min(red)
    minY = min(green)
    
    if no[i]>minX or no[i]>minY:
        if minX<=minY:
            red[red.index(minX)] = no[i]
        else:
            green[green.index(minY)] = no[i]
    else:
        break
        
print(sum(red)+sum(green))

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,ans=0,0,0
for i in range(a+b+c):
    if cntr!=x and (s[i] in p or s[i] in r):
        ans+=s[i]
        cntr+=1
    elif cntg!=y and ( s[i] in q or s[i] in r):
        ans+=s[i]
        cntg+=1
print(ans)

Submission status: Time Limit Exceeded

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

eat = red[0:x:] + green[0:y:]
eat.sort()
while clear[0] > eat[0]:
    eat[0] = clear[0]
    clear[0] = 0
    eat.sort()
    clear.sort(reverse=True)
print(sum(eat))

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


for i in r:
    p_min = p[0]
    q_min = q[0]

    if p_min > i and q_min > i:
        break

    p_min_diff = i - p_min
    q_min_diff = i - q_min

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

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