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:
def bisect_right_reverse(a, x):
    if a[0] < x:
        return 0
    if x <= a[-1]:
        return len(a)
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

#import bisect
X, Y, A, B, C = map(int, input().split())
red = [int(x) for x in input().split()]
gre = [int(x) for x in input().split()]
non = [int(x) for x in input().split()]
red.sort(reverse=True)
gre.sort(reverse=True)
non.sort()
ans = red[:X] + gre[:Y]
ans.sort(reverse=True)
times = len(non)
for i in range(times):
    c = non[-1]
    index = bisect_right_reverse(ans, c)
    ans.insert(index, c)
    non.pop(-1)
    ans.pop(-1)
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
def bisect_right_reverse(a, x):
    if a[0] < x:
        return 0
    if x <= a[-1]:
        return len(a)
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

#import bisect
X, Y, A, B, C = map(int, input().split())
red = [int(x) for x in input().split()]
gre = [int(x) for x in input().split()]
non = [int(x) for x in input().split()]
red.sort(reverse=True)
gre.sort(reverse=True)
non.sort()
ans = red[:X] + gre[:Y]
ans.sort(reverse=True)

while non[-1] > ans[-1]:
    c = non[-1]
    index = bisect_right_reverse(ans, c)
    ans.insert(index, c)
    non.pop(-1)
    ans.pop(-1)
    if non == []:
        break
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())))[::-1]+[-1]
Q=sorted(list(map(int,input().split())))[::-1]+[-1]
R=sorted(list(map(int,input().split())))[::-1]+[-1]
N=0
ans=0
while 1:
  if P[0]>=Q[0] and P[0]>R[0]:
    ans+=P.pop(0)
    X-=1
    if X+Y==N:
      break
    if X==0:
      P=[-1]
  if Q[0]>=P[0] and Q[0]>=R[0]:
    ans+=Q.pop(0)
    Y-=1
    if X+Y==N:
      break
    if Y==0:
      Q=[-1]
  if R[0]>=P[0] and R[0]>=Q[0]:
    ans+=R.pop(0)
    N+=1
    if X+Y==N:
      break
print(ans)

Submission status: Time Limit Exceeded

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

def pick(list1,list2,number):
    list_f = list1.copy()
    list_f2 = list1.copy()
    list_s = list2.copy()
    list_f2.extend(list_s)
    list_f2.sort(reverse = True)
    total_deli = 0
    for i in range(number):
        delic = list_f2[0]
        total_deli += int(delic)
        if(delic not in list_f):
            list_s.remove(delic)
        list_f2.remove(delic)
    return total_deli, list_s


ans1 = 0
ans2 = 0
ans3 = 0
clear_apple = []

#パターン1：赤リンゴから緑のリンゴへ
ans1,clear_apple = pick(p,r,x)
ans2,clear_apple = pick(q,clear_apple,y)
ans3 = ans1 + ans2

#パターン2：緑のリンゴから赤のリンゴへ
ans1,clear_apple = pick(q,r,y)
ans2,clear_apple = pick(p,clear_apple,x)
ans2 = ans1 + ans2

ans = max(ans2,ans3)
print("{}".format(ans))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import copy
x, y, a, b, c = map(int, input().split())
p = input().split()
q = input().split()
r = input().split()
p = [int(i) for i in p]
q = [int(i) for i in q]
r = [int(i) for i in r]

def pick(list1,list2,number):
    list_f = copy.copy(list1)
    list_f2 = copy.copy(list1)
    list_s = copy.copy(list2)
    list_f2.extend(list_s)
    list_f2.sort(reverse = True)
    total_deli = 0
    for i in range(number):
        delic = list_f2[0]
        total_deli += int(delic)
        if(delic not in list_f):
            list_s.remove(delic)
        list_f2.remove(delic)
    return total_deli, list_s


ans1 = 0
ans2 = 0
ans3 = 0
clear_apple = []

#パターン1：赤リンゴから緑のリンゴへ
ans1,clear_apple = pick(p,r,x)
ans2,clear_apple = pick(q,clear_apple,y)
ans3 = ans1 + ans2

#パターン2：緑のリンゴから赤のリンゴへ
ans1,clear_apple = pick(q,r,y)
ans2,clear_apple = pick(p,clear_apple,x)
ans2 = ans1 + ans2

ans = max(ans2,ans3)
print("{}".format(ans))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import copy
x, y, a, b, c = map(int, input().split())
p = input().split()
q = input().split()
r = input().split()
lis = [p,q,r]
coco = 0
for i in [len(p),len(q),len(r)]:
  for j in range(i):
    lis[coco][j] = int(lis[coco][j])
  coco += 1

def pick(list1,list2,number):
    list_f = copy.copy(list1)
    list_f2 = copy.copy(list1)
    list_s = copy.copy(list2)
    list_f2.extend(list_s)
    list_f2.sort(reverse = True)
    total_deli = 0
    for i in range(number):
        delic = list_f2[0]
        total_deli += int(delic)
        if(delic not in list_f):
            list_s.remove(delic)
        list_f2.remove(delic)
    return total_deli, list_s


ans1 = 0
ans2 = 0
ans3 = 0
clear_apple = []

#パターン1：赤リンゴから緑のリンゴへ
ans1,clear_apple = pick(p,r,x)
ans2,clear_apple = pick(q,clear_apple,y)
ans3 = ans1 + ans2

#パターン2：緑のリンゴから赤のリンゴへ
ans1,clear_apple = pick(q,r,y)
ans2,clear_apple = pick(p,clear_apple,x)
ans2 = ans1 + ans2

ans = max(ans2,ans3)
print("{}".format(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)
pp = p[:x]
qq = q[:y]
if x+y <= c:
  rr = r[:(x+y)]
else:
  rr = r
ppqq = pp + qq
ppqq.sort()
for i in range(len(ppqq)):
  if ppqq[i] < rr[0]:
    ppqq[i] = rr[0]
    del rr[0]
    rr.append(0)
print(sum(ppqq))

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

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

for i in range(min(x+y, len(r))):
  if min(p) >= min(q) and r[0] > min(q):
    heapq.heapify(q)
    heapq.heappop(q)
    q.append(r[0])
    r.pop(0)
  elif min(q) >= min(p) and r[0] > min(p):
    heapq.heapify(p)
    heapq.heappop(p)
    p.append(r[0])
    r.pop(0)

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

Submission status: Time Limit Exceeded

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

label = np.array([1] * Q.shape[0] + [0] * R.shape[0])
QR = np.concatenate([Q,R])
QR = np.concatenate([label.reshape(1,-1), QR.reshape(1,-1)], axis=0)
QR = QR[:,np.argsort(QR[1,:])]

points = []
for k in range(0,X+1):
  if QR[0,:][-(X+Y-k):].sum() > Y:
    if X-k > C:
      points.append(0)
    else:
      if k == 0:
        score = Q[-Y:].sum()+R[-(X-k):].sum()
      else:
        score = Q[-Y:].sum()+R[-(X-k):].sum() + P[-k:].sum()
      points.append(score)

  else:
    if QR.shape[1] < X+Y-k:
      points.append(0)
    else:
      if k == 0:
        score = QR[1,-(X+Y-k):].sum()
      else:
        score = QR[1,-(X+Y-k):].sum() + P[-k:].sum()
      points.append(score)
print(max(points))

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()
q.sort()
r.sort()
p.reverse()
q.reverse()
r.reverse()

ret = 0
z = 0
while(x + y > z):
    pattern = 0
    apple = 0
    if x > 0 and len(p) > 0:
        apple = p[0]
        pattern = 1
    if y > 0 and len(q) > 0:
        if apple < q[0]:
            apple = q[0]
            pattern = 2
    if len(r) > 0 and apple < r[0]:
        apple = r[0]
        pattern = 3
    ret += apple
    if pattern == 1:
        x -= 1
        p.pop(0)
    elif pattern == 2:
        y -= 1
        q.pop(0)
    elif pattern == 3:
        z += 1
        r.pop(0)

print(ret)

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int,input().split())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))
clist=list(map(int,input().split()))
a_list=sorted(alist,reverse=True)
b_list=sorted(blist,reverse=True)
c_list=sorted(clist,reverse=True)

d = 0
for i in range(x+1):
  for j in range(y+1):
    d1=sum(a_list[0:i])+sum(b_list[0:j])+sum(c_list[0:((x+y)-(i+j))])
    if d1>d:
      d = d1
print(d)

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = list(map(lambda x: int(x) ,input().split(" ")))
li = []
p = list(map(lambda x: int(x) ,input().split(" ")))
q = list(map(lambda x: int(x) ,input().split(" ")))
r = list(map(lambda x: int(x) ,input().split(" ")))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
p = p[:X]
q = q[:Y]

for j, y  in enumerate(r):
  for i, x in enumerate(p):
    if x > y:
      continue
    p[i] = y
    r[j] = x
for j, y  in enumerate(r):
  
  for i, x in enumerate(q):
    if x > y:
      continue
    q[i] = y
    r[j] = x
print(sum(p) + sum(q))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

from collections import deque 
from functools import reduce

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

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

p_np = np.zeros(999999)
q_np = np.zeros(999999)

if len(p) > x:
    p_np[:x] = p[:x]
else:
    p_np[:len(p)] = p[:]

if len(q) > y:
    q_np[:y] = q[:y]
else:
    q_np[:len(q)] = q[:]    

ap_np = np.hstack((p_np[:x], q_np[:y]))
ap_np.sort()
ap_cnt = 0

for i, ri in enumerate(r):
    for api in range(ap_cnt, x+y):
        if (r[i] > ap_np[api]):
            ap_np[api] = r[i]
            ap_cnt += 1
            break
print(int(ap_np.sum()))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  x, y, a, b, c = map(int, input().split())
  la = list(map(int, input().split()))
  la.sort(reverse=True)
  lb = list(map(int, input().split()))
  lb.sort(reverse=True)
  lc = list(map(int, input().split()))
  lc.sort(reverse=True)
  ca, cb, cc = 0, 0, 0,
  
  s = 0
  while True:
    if sum([ca, cb, cc]) == x+y:
      break
      
    a = la[0] if len(la) > 0 and ca < x else -1
    b = lb[0] if len(lb) > 0 and cb < y else -1
    c = lc[0] if len(lc) > 0            else -1
            
    if a >= b and a >= c:
      s += a
      del la[0]
      ca += 1
      continue
      
    if b >= a and b >= c:
      s += b
      del lb[0]
      cb += 1
      continue
      
    if c >= b and c >= a:
      s += c
      del lc[0]
      cc += 1
  print(s)

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = map(int, input().split())
p = [int(a) for a in input().split()]
q = [int(a) for a in input().split()]
r = [int(a) for a in input().split()]
s = []

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

for i in range(X):
    s.append(p.pop(0))
for i in range(Y):
    s.append(q.pop(0))
s.sort()
for i in range(C):
    if(s[0] < r[0]):
        s.pop(0)
        s.append(r.pop(0))
        
print(sum(s))

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = map(int, input().split())
p = [int(a) for a in input().split()]
q = [int(a) for a in input().split()]
r = [int(a) for a in input().split()]
s = []

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

for i in range(X):
    s.append(p.pop(0))
for i in range(Y):
    s.append(q.pop(0))
s.sort()
for i in range(C):
    if(s[0] < r[0]):
        s[0] = r.pop(0)
        s.sort()
        
print(sum(s))

Submission status: Time Limit Exceeded

Solution code:
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)

pList = pList[:X]
qList = qList[:Y]

value = 0

for indx in range(X+Y):
    if (pList[0] >= qList[0]) and (pList[0] >= rList[0]):
        value += pList[0]
        pList.pop(0)

        if len(pList) == 0:
            pList.append(-1)

    elif (qList[0] > pList[0]) and (qList[0] > rList[0]):
        value += qList[0]
        qList.pop(0)

        if len(qList) == 0:
            qList.append(-1)

    else:
        value += rList[0]
        rList.pop(0)

        if len(rList) == 0:
            rList.append(-1)


print(value)

Submission status: Time Limit Exceeded

Solution code:
import bisect


def resolve():
    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 q:
        bisect.insort_right(p, i)
    for i in range(min(a + b, c)):
        if p[i] < r[i]:
            p[i] = r[i]
        else:
            break
    print(sum(p))


if __name__ == '__main__':
    resolve()

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

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

p = p[:X]
q = q[:Y]
heapq.heapify(p)
heapq.heapify(q)

jdgp = True
jdgq = True

while r and (jdgp or jdgq):
    if jdgp:hp = heapq.heappop(p)
    if jdgq:hq = heapq.heappop(q)
    rr = r.pop()
    if hp > hq:
        if jdgp:heapq.heappush(p, hp)
        if rr > hq:
            heapq.heappush(q, rr)
            hq = rr
        else:
            if jdgq:heapq.heappush(q, hq)
            r.append(rr)
            jdgq = False
    else:
        if jdgq:heapq.heappush(q, hq)
        if rr > hp:
            heapq.heappush(p, rr)
            hp = rr
        else:
            if jdgp:heapq.heappush(p, hp)
            r.append(rr)
            jdgp = False

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

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C =  [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A.append(0)
B.append(0)
C.append(0)
ux_1 = 0
uy_1 = 0
for i in range(X):
    ux_1 += max(max(A),max(C))
    if max(A) >= max(C):
        A.pop(A.index(max(A)))
    else:
        C.pop(C.index(max(C)))
for i in range(Y):
    uy_1 += max(max(B),max(C))
    if max(B) >= max(C):
        B.pop(B.index(max(B)))
    else:
        C.pop(C.index(max(C)))
print(ux_1+uy_1)

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.append(0)
B.append(0)
C.append(0)
ux_1 = 0
uy_1 = 0
for i in range(X):
    ux_1 += max(max(A),max(C))
    if max(A) >= max(C):
        A.pop(A.index(max(A)))
    else:
        C.pop(C.index(max(C)))
for i in range(Y):
    uy_1 += max(max(B),max(C))
    if max(B) >= max(C):
        B.pop(B.index(max(B)))
    else:
        C.pop(C.index(max(C)))
print(ux_1+uy_1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

X, Y, A, B, C = map(int, 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()]

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

PQ = deque(sorted(P[-X:] + Q[-Y:]))
R = deque(R)

while True:
    if not R or R[0] <= PQ[0]:
        break
    if P[0] > PQ[0]:
        PQ.append(R.popleft())
        PQ.popleft()

print(sum(PQ))

Submission status: Time Limit Exceeded

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

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

ans = deque(maxlen=(x+y))

def insertion(z):
	global x,y
	if z == p:
		x -= 1
	else:
		y -= 1
	return ans.append(z.pop())

while x > 0 or y > 0:
	if x and y:
		if p[-1] > q[-1] :
			insertion(p)
		else:
			insertion(q)
	elif not x:
		insertion(q)
	else:
		insertion(p)

while r:
	if ans[-1] < r[-1]:
		ans.appendleft(r.pop())

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())), )[-X:]
Q=sorted(list(map(int, input().split())), )[-Y:]
R=sorted(list(map(int, input().split())), reverse=True)

PQ=sorted(P+Q)

ans=0
for i in range(len(PQ)):
  if len(R)>0:
    
    now=max(PQ[i], R.pop(0))
  else:
    now=PQ[i]
  ans+=now
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = [int(t) for t in input().split()]
p = [int(t) for t in input().split()]
q = [int(t) for t in input().split()]
r = [int(t) for t in input().split()]
s = []
for i in range(x):
    p_max = max(p)
    if r:
        r_max = max(r)
    else:
        r_max = 0
    if p_max >= r_max:
        s.append(p.pop(p.index(p_max)))
    else:
        s.append(r.pop(r.index(r_max)))
for i in range(y):
    q_max = max(q)
    if r:
        r_max = max(r)
    else:
        r_max = 0
    if q_max >= r_max:
        s.append(q.pop(q.index(q_max)))
    else:
        s.append(r.pop(r.index(r_max)))
        
print(sum(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()))
p.sort()
q.sort()
p.reverse()
q.reverse()
p=p[:X]
q=q[:Y]
r=r[:max(X,Y)]
 
a=p[:]
a.extend(q)
a.extend(r)
a.sort()
a.reverse()
b=q[:]
b.extend(r)
b.sort()
b.reverse()
c=p[:]
c.extend(r)
c.sort()
c.reverse()
red=0
green=0
sum=0
sum1=0
sum2=0
d=0
for i in range(X+Y):
  if(a[i] in p):
    red+=1
  elif(a[i] in q):
    green+=1
  d+=a[i]
 
  if(i<X):
      sum1+=p[i]
      sum2+=c[i]
  if(i<Y):
      sum1+=b[i]
      sum2+=q[i]
    
if(red<=X and green<=Y):
  print(d)
elif(red>X):
  print(sum1)
elif(green>Y):
  print(sum2)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C=map(int, input().split())
AA=list(map(int, input().split()))
BB=list(map(int, input().split()))
CC=list(map(int, input().split()))
AA=sorted(AA,reverse=True)
BB=sorted(BB,reverse=True)
CC=sorted(CC,reverse=True)
AAA=AA[0:X]
BBB=BB[0:Y]
for i in CC:
    if i>min(AAA)or i>min(BBB):
        if min(AAA)>min(BBB):
            BBB[-1]=i
            BBB=sorted(BBB,reverse=True)
        else:
            AAA[-1]=i
            AAA=sorted(AAA,reverse=True)
    else:
        break
print(sum(AAA)+sum(BBB))

Submission status: Time Limit Exceeded

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

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

def sort(L):
  if len(L)==1:
    return L
  left = sort(L[:len(L)//2])
  right = sort(L[len(L)//2:])
  result = []
  left.append(-1)
  right.append(-1)
  left_index = 0
  right_index = 0
  while len(result)<len(L):
    if left[left_index]>=right[right_index]:
      result.append(left[left_index])
      left_index += 1
    else:
      result.append(right[right_index])
      right_index += 1
  return result

P = sort(P)[:X]
Q = sort(Q)[:Y]
R = sort(P+Q+R)[:X+Y]
print(sum(R))

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C=map(int, input().split())
AA=list(map(int, input().split()))
BB=list(map(int, input().split()))
CC=list(map(int, input().split()))
AA=sorted(AA,reverse=True)
BB=sorted(BB,reverse=True)
CC=sorted(CC,reverse=True)
AAA=AA[0:X]
BBB=BB[0:Y]
for i in CC:
    if i>min(AAA)or i>min(BBB):
        if min(AAA)>min(BBB):
            BBB[-1]=i
            BBB=sorted(BBB,reverse=True)
        if min(BBB)>min(AAA):
            AAA[-1]=i
            AAA=sorted(AAA,reverse=True)
    else:
        break
print(sum(AAA)+sum(BBB))

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()))
R.sort(reverse=True)
if X > A:
    get_P= P.extend(R[:X-A+1])
    get_P.sort(reverse=True)
    R=R[X-A+1:]
else:
    P.sort(reverse=True)
    get_P=P[:X]
if Y > B:
    get_Q= Q.extend(Q[:Y-B+1])
    R=R[Y-B+1]
    get_Q.sort(reverse=True)
else:
    Q.sort(reverse=True)
    get_Q=Q[:Y]

while(min(P)<R[0] or min(Q)<R[0]):
    if min(get_P)<min(get_Q):
        get_P[get_P.index(min(get_P))] = R.pop(0)
    else:get_Q[get_Q.index(min(get_Q))] = R.pop(0)
    if R==[]:break
#print(get_P)
#print(get_Q)
print(sum(get_P)+sum(get_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 = 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:
        del p[0]
        p.extend([i])
    elif q_min_diff >= p_min_diff and q_min_diff > 0:
        del q[0]
        q.extend([i])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in input().split()]
q = [[int(x), "B"] for x in input().split()]
r = [[int(x), "C"] for x in input().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]
apples = p+q+r


#apples.sort(reverse=True)
apples = sorted(apples, reverse=True)
#print(apples)
ans = 0
pend = 0


for i in range(len(apples)):
    if apples[0][1] == "C":
        pend += 1
        ans += apples[0][0]
        apples.pop(0)
    elif apples[0][1] == "A":
        if X > 0:
            ans += apples[0][0]
            apples.pop(0)
            X -= 1
        else:
            apples.pop(0)
    elif apples[0][1] == "B":
        if Y>0:
            ans += apples[0][0]
            apples.pop(0)
            Y -= 1
        else:
            apples.pop(0)
    if (X < 1 and Y < 1) or X+Y == pend:
        break
    #print(ans)
    #print("XY", X, Y, pend)
#print("XY", X, Y, pend)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = [int(e) for e in input().split()]
x = int(s[0])
y = int(s[1])
a = int(s[2])
b = int(s[3])
c = int(s[4])
a_l = [int(e) for e in input().split()]
b_l = [int(e) for e in input().split()]
c_l = [int(e) for e in input().split()]

a_l.sort(reverse=True)
b_l.sort(reverse=True)
c_l.sort(reverse=True)

apple = []
app=a_l[0:x]+b_l[0:y]
app.sort()

for c_app in c_l:
    if app[0]<c_app:
        app[0]=c_app
    app.sort()

apple_count=sum(app)

print(apple_count)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
p, q, r = deque, deque, 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()))
tmp = 0
p.sort(reverse=True)
p.append(0)
q.sort(reverse=True)
q.append(0)
r.sort(reverse=True)
r.append(0)
z = X+Y
for i in range(z):
    if X>0 and ((p[0]>=q[0] and p[0]>=r[0]) or (Y==0 and p[0]>=r[0])):
        tmp = tmp+p[0]
        p.pop(0)
        X -= 1
    elif Y>0 and ((q[0]>=p[0] and q[0]>=r[0]) or (X==0 and q[0]>=r[0])):
        tmp = tmp+q[0]
        q.pop(0)
        Y -= 1
    elif r[0]>p[0] or r[0]>q[0]:
        tmp = tmp+r[0]
        r.pop(0)
        
print(tmp)

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 = 0
p.sort(reverse=True)
p.append(0)
q.sort(reverse=True)
q.append(0)
r.sort(reverse=True)
r.append(0)
z = X+Y
for i in range(z):
    if X>0 and ((p[0]>=q[0] and p[0]>=r[0]) or (Y==0 and p[0]>=r[0])):
        tmp = tmp+p[0]
        p.pop(0)
        X -= 1
    elif Y>0 and ((q[0]>=p[0] and q[0]>=r[0]) or (X==0 and q[0]>=r[0])):
        tmp = tmp+q[0]
        q.pop(0)
        Y -= 1
    elif r[0]>p[0] or r[0]>q[0]:
        tmp = tmp+r[0]
        r.pop(0)
        
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in input().split()]
q = [[int(x), "B"] for x in input().split()]
r = [[int(x), "C"] for x in input().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]
apples = p+q+r


apples.sort(reverse=True)
#print(apples)
ans = 0
pend = 0


for i in range(len(apples)):
    if apples[0][1] == "C":
        pend += 1
        ans += apples[0][0]
        apples.pop(0)
    elif apples[0][1] == "A":
        if X > 0:
            ans += apples[0][0]
            apples.pop(0)
            X -= 1
        else:
            apples.pop(0)
    elif apples[0][1] == "B":
        if Y>0:
            ans += apples[0][0]
            apples.pop(0)
            Y -= 1
        else:
            apples.pop(0)
    if (X < 1 and Y < 1) or X+Y == pend:
        break
    #print(ans)
    #print("XY", X, Y, pend)
#print("XY", X, Y, pend)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = [int(e) for e in input().split()]
x = int(s[0])
y = int(s[1])
a = int(s[2])
b = int(s[3])
c = int(s[4])
a_l = [int(e) for e in input().split()]
b_l = [int(e) for e in input().split()]
c_l = [int(e) for e in input().split()]

a_l.sort(reverse=True)
b_l.sort(reverse=True)
c_l.sort(reverse=True)

apple = []
a_app=a_l[0:x]
b_app=b_l[0:y]

for c_app in c_l:
    if a_app[-1]<b_app[-1]:
        if a_app[-1]<c_app:
            a_app[-1]=c_app
    else:
        if b_app[-1]<c_app:
            b_app[-1]=c_app
    a_l.sort(reverse=True)
    b_l.sort(reverse=True)

apple_count=sum(a_app)+sum(b_app)

print(apple_count)

Submission status: Time Limit Exceeded

Solution code:
import sys
INPUT=input().split()
X=int(INPUT[0])
Y=int(INPUT[1])
A=int(INPUT[2])
#赤
B=int(INPUT[3])
#緑
C=int(INPUT[4])
#無色
RED=[0]*A
GREEN=[0]*B
NOCOL=[0]*C

INPUT=input().split()
for _ in range(A):
    RED[_]=int(INPUT[_])
INPUT=input().split()
for _ in range(B):
    GREEN[_]=int(INPUT[_])
INPUT=input().split()
for _ in range(C):
    NOCOL[_]=int(INPUT[_])
RED.sort()
GREEN.sort()
NOCOL.sort()
"""
取得した美味しさを昇順で並べた
"""
X_apple=[0]*X
Y_apple=[0]*Y
for _ in range(min(X,A)):
    X_apple[_]=RED[A-_-1]
    RED[A-_-1]=0
for _ in range(min(Y,B)):
    Y_apple[_]=GREEN[B-_-1]
    GREEN[B-_-1]=0
X_apple.sort()
Y_apple.sort()

i=0
if X_apple[i]==0:
    while(X_apple[i]==0):
        X_apple[i]=NOCOL[C-i-1]
        NOCOL[C-i-1]=0
        i+=1
else:
    i=1
    while(X_apple[0]<NOCOL[C-i]):
        X_apple[0]=NOCOL[C-i]
        NOCOL[C-i]=0
        i+=1
        X_apple.sort()

NOCOL.sort()
i=0
if Y_apple[i]==0:
    while(Y_apple[i]==0):
        Y_apple[i]=NOCOL[C-i-1]
        NOCOL[C-i-1]=0
        i+=1
else:
    i=1
    while(Y_apple[0]<NOCOL[C-i]):
        Y_apple[0]=NOCOL[C-i]
        NOCOL[C-i]=0
        i+=1
        Y_apple.sort()

SUM=0
for _ in range(X):
    SUM+=X_apple[_]
for _ in range(Y):
    SUM+=Y_apple[_]
print(SUM)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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())))

A = P[-X:]
G = Q[-Y:]

for r in R[::-1]:
    if r - min(A[-X], G[-Y]) < 0:
        break

    if A[-X] <= G[-Y]:
        bisect.insort(A, r)
    else:
        bisect.insort(G, r)

# print(A[-X:], G[-Y:])
# print(A[:], G[:])
print(sum(A[-X:]) + sum(G[-Y:]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from sys import stdin
input = sys.stdin.readline
X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in input().split()]
q = [[int(x), "B"] for x in input().split()]
r = [[int(x), "C"] for x in input().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]
apples = p+q+r


apples.sort(reverse=True)
#print(apples)
ans = 0
pend = 0

while (X > 0 or Y > 0) and X+Y > pend:
    #print(apples)
    if apples[0][1] == "C":
        pend += 1
        ans += apples[0][0]
        apples.pop(0)
    elif apples[0][1] == "A":
        if X > 0:
            ans += apples[0][0]
            apples.pop(0)
            X -= 1
        else:
            apples.pop(0)
    elif apples[0][1] == "B":
        if Y>0:
            ans += apples[0][0]
            apples.pop(0)
            Y -= 1
        else:
            apples.pop(0)
    #print(ans)
    #print("XY", X, Y, pend)
#print("XY", X, Y, pend)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
apples = [(0, 'X') for x in range(300001)]
apples_idx = 0

def append_a(n):
    global apples
    global apples_idx
    apples[apples_idx] = (n, 'A')
    apples_idx += 1

def append_b(n):
    global apples
    global apples_idx
    apples[apples_idx] = (n, 'B')
    apples_idx += 1

def append_c(n):
    global apples
    global apples_idx
    apples[apples_idx] = (n, 'C')
    apples_idx += 1
    
X, Y, A, B, C = map(int, input().split())
for i in input().split():
    append_a(int(i))

for i in input().split():
    append_b(int(i))

for i in input().split():
    append_c(int(i))

apples.sort(reverse=True)
xn, yn = 0, 0
value = 0

for i, apple in enumerate(apples):
    if xn == X and yn == Y:
        break
    if apple[1] == 'A':
        if xn == X:
            continue
        xn += 1
        value += apple[0]
        continue
    if apple[1] == 'B':
        if yn == Y:
            continue
        yn += 1
        value += apple[0]
        continue

    # Apple C
    next_type = 'C'
    if xn < X and yn < Y:
        for j in range(i+1, len(apples)):
            if apples[j][1] == 'A':
                next_type = 'A'
            elif apples[j][1] == 'B':
                next_type = 'B'
    elif xn < X:
        next_type = 'A'
    else:
        next_type = 'B'

    if next_type == 'A':
        xn += 1
        value += apple[0]
    else:
        yn += 1
        value += apple[0]

print(value)