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:
X, Y, A, B, C = map(int, input().split())
p, pi = sorted(list(map(int, input().split())), reverse=True)[:X] + [-1], 0
q, qi = sorted(list(map(int, input().split())), reverse=True)[:Y] + [-1], 0
r, ri = sorted(list(map(int, input().split())), reverse=True) + [-1], 0
ans, nX, nY = 0, 0, 0
while nX < X or nY < Y:
    if r[ri] >= p[pi] and r[ri] >= q[qi]:
        if nX < X and nY < Y:
            if p[pi] >= q[qi]:
                nX += 1
            else:
                nY += 1
        elif nX < X:
            nX += 1
        elif nY < Y:
            nY += 1
        ans += r[ri]
        ri += 1
    elif p[pi] >= q[qi] and p[pi] >= r[ri]:
        if nX < X:
            nX += 1
            ans += p[pi]
            pi += 1
    elif q[qi] >= r[ri] and q[qi] >= p[pi]:
        if nY < Y:
            nY += 1
            ans += q[qi]
            qi += 1

print(ans)

Submission status: Time Limit Exceeded

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

R.sort()
R.reverse()
G.sort()
G.reverse()
C.sort()
C.reverse()

R = R[:X]
G = G[:Y]

temp = 0
  
for i in range(len(C)) :
  if((X==0) and (Y==0)) :
    break
  if(X==0) :
    last = G[Y-1]
    Y -= 1
    
  else :
    if(Y==0) :
      last = R[X-1]
      X -= 1
    else :
      if(R[X-1] < G[Y-1]) :
        last = R[X-1]
        X -= 1
      else :
        last = G[Y-1]
        Y -= 1

  if(C[i] < last) :
    if(i == 0) :
      answer = sum(R[:X]) + sum(G[:Y]) + last
    else :
      break
  else :
    answer = sum(R[:X]) + sum(G[:Y]) + sum(C[:i+1])
    
print(answer)

Submission status: Time Limit Exceeded

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

    P = sorted(P, reverse=True)
    Q = sorted(Q, reverse=True)
    R = sorted(R, reverse=True)

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

    for r_i in R:
        if r_i < min(x[-1], y[-1]):
            break
        else:
            if x[-1] < y[-1]:
                x[-1] = r_i
                # x = sorted(x, reverse=True)
                x.sort(reverse=True)
            else:
                y[-1] = r_i
                # y = sorted(y, reverse=True)
                y.sort(reverse=True)

    print(sum(x) + sum(y))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    P = sorted(P, reverse=True)
    Q = sorted(Q, reverse=True)
    R = sorted(R, reverse=True)

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

    for r_i in R:
        if r_i < min(x[-1], y[-1]):
            break
        else:
            if x[-1] < y[-1]:
                x[-1] = r_i
                x = sorted(x, reverse=True)
            else:
                y[-1] = r_i
                y = sorted(y, reverse=True)

    print(sum(x) + sum(y))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
l = [int(x) for x in input().split()]
x = l[0]
y = l[1]
a = l[2]
b = l[3]
c = l[4]
numa = [int(x) for x in input().split()]
numb = [int(x) for x in input().split()]
numc = [int(x) for x in input().split()]
taberux = [0] * x
taberuy = [0] * y

for i in range(x):
  numa.sort(reverse = True)
  taberux[i] = numa[0]
  del numa[0]
for j in range(y):
  numb.sort(reverse = True)
  taberuy[j] = numb[0]
  del numb[0]
numc.sort(reverse = True)

taberux.sort()
taberuy.sort()
while not len(numc) ==0 and min(taberux[0],taberuy[0]) < numc[0] :
    if taberux[0] < taberuy[0]:
        taberux[0] = numc[0]
        del numc[0]
        numc.sort(reverse = True)
        taberux.sort()
    else:
        taberuy[0] = numc[0]
        del numc[0]
        numc.sort(reverse = True)
        taberuy.sort()
print(sum(taberux)+sum(taberuy))

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)
del p[x:]
del q[y:]
for k in range(c):
    if r[k] > p[x - 1] or r[k] > q[y - 1]:
        if p[x - 1] >= q[y - 1]:
            q[y - 1] = r[k]
        elif p[x - 1] < q[y - 1]:
            p[x - 1] = r[k]
    p.sort(reverse = True)
    q.sort(reverse = True)
print(sum(p) + sum(q))

Submission status: Time Limit Exceeded

Solution code:
def merge(A,B):
    C = []
    
    while len(A)!=0 and len(B) != 0:
        if A[0]>B[0]:
            C.append(A[0])
            del A[0]
        else:
            C.append(B[0])
            del B[0]

    if len(A)>len(B):
        C.extend(A)
    else:
        C.extend(B)

    return C

if __name__ == "__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 = [1,23,4,1,2]
    # Q = [4,3,56]
    # R = [70,20,1]

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

    S = merge(P[:X],Q[:Y])
    S = merge(S,R)
    
    score = 0
    for i in range(X+Y):
        score += S[i]
    print(score)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int,input().split())
p,q,r = [sorted(map(int,input().split()),reverse=True) for i in range(3)]
ans,temp = 0,0

for i in range(X+1):
    temp = sum([p[t] for t in range(i)])
    for j in range(Y+1):
        temp_2 = temp + sum([q[t] for t in range(j)])
        t = lambda C: X-i+Y-j if X+Y-i-j < C else C
        temp_2 += sum([r[t] for t in range(t(C))]) 
        ans = max(temp_2,ans)

print(ans)

Submission status: Time Limit Exceeded

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

x,y,a,b,c = map(int,input().split())
p_a = list(map(int,input().split()))
p_b = list(map(int,input().split()))
p_c = list(map(int,input().split()))
p_a.sort(reverse=True)
p_b.sort(reverse=True)
p_c.sort(reverse=True)
p_c += [0]

ans = p_a[:x] + p_b[:y]
ans.sort(reverse=True)

while True:
    if ans[-1] > p_c[0]:
        break
    else:
        ans[-1] = p_c[0]
        p_c.pop(0)
        ans.sort(reverse=True)
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int, input().split(" "))
p = sorted(eval("["+input().replace(" ", ",")+"]"))
q = sorted(eval("["+input().replace(" ", ",")+"]"))
r = eval("["+input().replace(" ", ",")+"]")

s = sorted(p[A-X:A] + q[B-Y:B] + r)
print(sum(s[len(s)-X-Y:len(s)]))

Submission status: Time Limit Exceeded

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

s = 0
# gen c
for a in range(C+1):
  for b in range(C+1):
    if a + b > C:
      break
    t = sum(p[A-X+a:A]) + sum(q[B-Y+b:B]) + sum(r[C-a-b:C])
    s = max([s, t])

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()))
p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)
ls = []
for i in range(X):
    ls.append(p[i])
for i in range(Y):
    ls.append(q[i])
while(r != []):
    minimum = min(ls)
    
    if r[0] > minimum:
        ls[ls.index(minimum)] = r[0]
    r.pop(0)
print(sum(ls))

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(reverse=True)
for i in range(a-x):
    del p[0]
for i in range(b-y):
    del q[0]


date = p+q
date.sort()

flag = False
n = 0
for j in range(len(r)):
    if date[n] < r[j]:
        date[n] = r[j]
        date.sort()

print(sum(date))

Submission status: Time Limit Exceeded

Solution code:
(x,y,a,b,c)=map(int,input().split())
a1=list(map(int,input().split()))
b1=list(map(int,input().split()))
c1=list(map(int,input().split()))
a1.extend(c1)
a2=sorted(a1,reverse=True)
k=0
for i in range(x):
    k+=a2[i]
    if a2[i] in c1:
        c1.remove(a2[i])
b1.extend(c1)
b2=sorted(b1,reverse=True)
for j in range(y):
    k+=b2[j]
print(k)

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(reverse=True)
for i in range(a-x):
    del p[0]
for i in range(b-y):
    del q[0]


date = p+q
date.sort()

flag = False
n = 0
while not flag:
    for j in range(len(r)):
        if date[n] < r[j]:
            date[n] = r[j]
            date.sort()
        if j == len(r)-1:
            flag =True
print(sum(date))

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(reverse=True)
for i in range(a-x):
    del p[0]
for i in range(b-y):
    del q[0]
date = [int() for a in range(len(p)+len(q))]
for a in range(len(p)):
    date[a] = p[a]
for a in range(len(q)):
    date[len(p)+a] = q[a]
date.sort()

flag = False
while not flag:
    n = 0
    for j in range(len(r)):
        if date[n] < r[j]:
            date[n] = r[j]
            date.sort()
        if j == len(r)-1:
            flag =True
print(sum(date))

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)[:X]
q = sorted(list(map(int,input().split())), reverse=True)[:Y]
r = sorted(list(map(int,input().split())), reverse=True)
 
for i in r:
    p_min = p[X - 1]
    q_min = q[Y - 1]
 
    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.pop()
        p.insert(0, i)
    elif q_min_diff >= p_min_diff and q_min_diff > 0:
        q.pop()
        q.insert(0, i)
 
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)[:X]
q = sorted(list(map(int,input().split())), reverse=True)[:Y]
r = sorted(list(map(int,input().split())), reverse=True)
 
for i in r:
    p_min = p[len(p) - 1]
    q_min = q[len(q) - 1]
 
    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.pop()
        p = [i] + p
    elif q_min_diff >= p_min_diff and q_min_diff > 0:
        q.pop()
        q = [i] + q
 
print(sum(p) + sum(q))

Submission status: Time Limit Exceeded

Solution code:
a,b,c,d,e=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=list(map(int,input().split()))
t=0
for i in range(a):
    ma=max(l1)
    if(l3!=[]):
        mc=max(l3)
    else:
        mc=max(l1)
    if(ma>=mc or mc==[]):
        t=t+ma
        l1.remove(ma)
    else:
        t=t+mc
        l3.remove(mc)
for i in range(b):
    mg=max(l2)
    #mc=max(l3)

    if(l3!=[]):
        mc=max(l3)
    else:
        mc=max(l2)
    if(mg>=mc or mc==[]):
        t=t+mg
        l2.remove(mg)
    else:
        t=t+mc
        l3.remove(mc)
print(t)

Submission status: Time Limit Exceeded

Solution code:
a,b,c,d,e=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=list(map(int,input().split()))
l4=l1
l5=l2
l6=l3
#print(l4,l5,l6)
t=0
t1=0
for i in range(a):
    ma=max(l1)
    if(l3!=[]):
        mc=max(l3)
    else:
        mc=max(l1)
    if(ma>=mc or mc==[]):
        t=t+ma
        l1.remove(ma)
    else:
        t=t+mc
        l3.remove(mc)
for i in range(b):
    mg=max(l2)
    #mc=max(l3)

    if(l3!=[]):
        mc=max(l3)
    else:
        mc=max(l2)
    if(mg>=mc or mc==[]):
        t=t+mg
        l2.remove(mg)
    else:
        t=t+mc
        l3.remove(mc)   
'''for i in range(b):
    mg=max(l5)
    #mc=max(l6)
    if(l6!=[]):
        mc=max(l6)
    else:
        mc=max(l5)
    if(mg>=mc and mc==[]):
        t1=t1+mg
        l5.remove(ma)
    else:
        t1=t1+mc
        l6.remove(mc)
for i in range(a):
    ma=max(l4)
    #mc=max(l6)
    if(l6!=[]):
        mc=max(l6)
    else:
        mc=max(l4)
    if(ma>=mc and mc==[]):
        t1=t1+ma
        l4.remove(ma)
    else:
        t1=t1+mc
        l6.remove(mc)   '''
print(t)

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

if X>A:
  res = X-P
  P = P + R[res*(-1):]
  R = R[:res*(-1)]
  
if Y>B:
  res = Y-B
  Q = Q + R[res*(-1):]
  R = R[:res*(-1)]
  
flag=True
while flag:
  flag=False
  if len(R)==0:
    break
  b = R[-1]
  R = R[:-1]
  if P[0]>Q[0]:
    if Q[0] < b:
      Q = Q[1:]+[b]
      flag = True
  else:
    if P[0] < b:
      P = P[1:]+[b]
      flag=True

print(sum(P[(-1)*X:])+sum(Q[(-1)*Y:]))
    
#print(P[(-1)*X:])

Submission status: Time Limit Exceeded

Solution code:
apple_data = [int(n) for n in input().split()]
X = apple_data[0]
Y = apple_data[1]
A = apple_data[2]
B = apple_data[3]
C = apple_data[4]

# redlist = sorted([int(n) for n in input().split()],reverse=True)[:X]
# greenlist = sorted([int(n) for n in input().split()],reverse=True)[:Y]
# clearlist = sorted([int(n) for n in input().split()],reverse=True)

redlist = [int(n) for n in input().split()]
greenlist = [int(n) for n in input().split()]
clearlist = sorted([int(n) for n in input().split()],reverse=True)

def topxlist(numbers,top):
    return_list = []
    for n in numbers:
        if not return_list:
            return_list.append(n)
            #insert in order
            #print(return_list)
        for i in range(len(return_list)):
            if n < return_list[i]:
                return_list.insert(i,n)
                #print(return_list)
                break
        if n > return_list[-1]:
            return_list.append(n)
            #print(return_list)
        if len(return_list) > top:
            return_list.pop(0)
            #print(return_list)
    return return_list

redlist = sorted(topxlist(redlist,X),reverse=True)
greenlist = sorted(topxlist(greenlist,Y),reverse=True)


# print(redlist)
# print(greenlist)
# print(clearlist)

sum = 0
count = X + Y
while count > 0:
    if redlist:
        red = redlist[0]
    else:
        red = 0
    if greenlist:
        green = greenlist[0]
    else:
        green = 0
    if clearlist:
        clear = clearlist[0]
    else:
        clear = 0
    if red >= max(clear,green):
        sum += red
        redlist.pop(0)
    elif green >= max(red,clear):
        sum += green
        greenlist.pop(0)
    elif clear >= max(red,green):
        sum += clear
        clearlist.pop(0)
    count -= 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
apple_data = [int(n) for n in input().split()]
X = apple_data[0]
Y = apple_data[1]
A = apple_data[2]
B = apple_data[3]
C = apple_data[4]

redlist = sorted([int(n) for n in input().split()],reverse=True)[:X]
greenlist = sorted([int(n) for n in input().split()],reverse=True)[:Y]
clearlist = sorted([int(n) for n in input().split()],reverse=True)

# print(redlist)
# print(greenlist)
# print(clearlist)

sum = 0
count = X + Y
while count > 0:
    if redlist:
        red = redlist[0]
    else:
        red = 0
    if greenlist:
        green = greenlist[0]
    else:
        green = 0
    if clearlist:
        clear = clearlist[0]
    else:
        clear = 0
    if red >= max(clear,green):
        sum += red
        redlist.pop(0)
    elif green >= max(red,clear):
        sum += green
        greenlist.pop(0)
    elif clear >= max(red,green):
        sum += clear
        clearlist.pop(0)
    count -= 1

print(sum)

Submission status: Time Limit Exceeded

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

ok = True
poz = aux = i = 0
for j in range(a):
    for k in range(poz, b):
        if i == c:
            ok = False
            break
        minim = min(red[j], green[k])
        if colorLess[i] > minim:
            if red[j] > green[k]:
                green[k] = colorLess[i]
                aux += 1
                i += 1
                green.sort()
            else:
                red[j] = colorLess[i]
                poz = aux
                i += 1
                red.sort()
                break
        else:
            ok = False
            break
    if not ok:
        break

red.reverse()
green.reverse()
r = g = 0
for i in red[:x]:
    r += i
for i in green[:y]:
    g += i
print(r+g)

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C=map(int,input().split())
rp=list(map(int,input().split()))
bp=list(map(int,input().split()))
np=list(map(int,input().split()))
rp.sort()
bp.sort()
np.sort()
count=X+Y
red=X
blue=Y
N=0
ans =0

for k in range(count):
    if red == 0:
        if np[-1] >= bp[-1]:
            ans += np[-1]
            np = np[:-1]
        else:
            ans+=bp[-1]
            bp=bp[:-1]
    elif blue == 0:
        if np[-1] >= rp[-1]:
            ans += np[-1]
            np = np[:-1]
        else:
            ans+=rp[-1]
            rp=rp[:-1]        
    elif len(np) >0:
        s = np[-1]
        if s >= rp[-1] and s >= bp[-1]:
            ans+=s
            np = np[:-1]
        else:
            if red >0:
                if blue>0:
                    if rp[-1] >= bp[-1]:
                        ans += rp[-1]
                        rp = rp[:-1]
                        red-=1
                    else:
                        ans+=bp[-1]
                        bp=bp[:-1]
                        blue-=1
                else:
                    ans += rp[-1]
                    rp = rp[:-1]
                    red-=1

            else:
                ans += bp[-1]
                bp=bp[:-1]
                blue-=-1
    elif rp[-1] >= bp[-1]:
        ans += rp[-1]
        rp = rp[:-1]
        red-=1
    else:
        ans+=bp[-1]
        bp=bp[:-1]
        blue-=1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = (int(x) for x in input().split())
red = list(map(int, input().strip().split()))
green = list(map(int, input().strip().split()))
clear = list(map(int, input().strip().split()))
red.sort(reverse=True)
green.sort(reverse=True)
clear.sort(reverse=True)
pre_answer = 0
answer = 0
for i in range (max(X+Y,C)):
    for k in range (i+1):
        if(X-k>=0 and Y-(i-k)>=0 ):
            pre_answer = sum(red[0:X-k]) + sum(green[0:Y-(i-k)]) + sum(clear[0:i])
#             print(X-k,Y-(i-k),i,pre_answer)
            if(answer<pre_answer):
                answer = pre_answer
print(answer)

Submission status: Time Limit Exceeded

Solution code:
inp=list(map(int,input().strip().split()))[:5]
x,y,a,b,c=inp[0],inp[1],inp[2],inp[3],inp[4]
l1=list(map(int,input().strip().split()))[:a]
l2=list(map(int,input().strip().split()))[:b]
l3=list(map(int,input().strip().split()))[:c]
list1=sorted(l1)
list2=sorted(l2)
list3=sorted(l3)
lis1=list1[::]
lis2=list2[::]
lis3=list3[::]
sum1=0
for i in range(x):
	if(len(list3) > 0):
		sum1+=max(list1[-1],list3[-1])
		if(list1[-1] >= list3[-1]):
			list1.remove(list1[-1])
		else:
			list3.remove(list3[-1])
	else:
		sum1+=list1[-1]
		list1.remove(list1[-1])

for i in range(y):
	if(len(list3) > 0):
		sum1+=max(list2[-1],list3[-1])
		if(list2[-1] >= list3[-1]):
			list2.remove(list2[-1])
		else:
			list3.remove(list3[-1])
	else:
		sum1+=list2[-1]
		list2.remove(list2[-1])
sum2=0
for i in range(y):
	if(len(lis3) > 0):
		sum2+=max(lis2[-1],lis3[-1])
		if(lis2[-1] >= lis3[-1]):
			lis2.remove(lis2[-1])
		else:
			lis3.remove(lis3[-1])
	else:
		sum2+=lis2[-1]
		lis2.remove(lis2[-1])
for i in range(x):
	if(len(lis3) > 0):
		sum2+=max(lis1[-1],lis3[-1])
		if(lis1[-1] >= lis3[-1]):
			lis1.remove(lis1[-1])
		else:
			lis3.remove(lis3[-1])
	else:
		sum2+=lis2[-1]
		lis2.remove(lis2[-1])

print(max(sum1,sum2))

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int,input().split())
za = list(map(int,input().split()))
zb = list(map(int,input().split()))
zc = list(map(int,input().split()))
la = len(za)
lb = len(zb)
lc = len(zc)
l = za+zb+zc
da = {}
db = {}
dc = {}
for i in za:
    if i in da:
        da[i] =da[i]+1

    else:
        da[i] = 1

for i in zb:
    if i in db:
        db[i] = db[i] + 1

    else:
        db[i] = 1

for i in zc:
    if i in dc:
        dc[i] = dc[i] + 1

    else:
        dc[i] = 1

l.sort()
l = l[::-1]
fcnt = x+y
cnt = 0
som = 0
g = 0
#print(l)
while cnt < fcnt and g == 0:
    q = l.pop(0)
    if q in da:
        f = da[q]
        #print("da")
        while(cnt <= fcnt and f > 0 and la > 0 and x > 0):
            f = f-1
            cnt = cnt+1
            la = la-1
            som = som+q
            x = x-1
            #print("HI",x)

    if cnt == fcnt:
        g = 1
        break

    if q in db:
        f = db[q]
        while (cnt <= fcnt and f > 0 and lb > 0 and y > 0):
            f = f - 1
            cnt = cnt + 1
            lb = lb - 1
            som = som + q
            y = y-1

    if cnt == fcnt:
        g = 1
        break

    if q in dc:
        f = dc[q]
        while (cnt <= fcnt and f > 0 and lc > 0):
            f = f - 1
            cnt = cnt + 1
            lc = lc - 1
            som = som + q


print(som)

Submission status: Time Limit Exceeded

Solution code:
import bisect

x, y, a, b, c = map(int, input().split())
p = [int(_) for _ in input().split()]
q = [int(_) for _ in input().split()]
r = [int(_) for _ in input().split()]

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

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

while (min(x_list[0], y_list[0]) < r[-1]):
    if x_list[0] > y_list[0]:
        i = bisect.bisect_right(r, y_list[0])
        y_list.pop(0)
        target = r.pop(i)
        bisect.insort(y_list, target)
    else:
        i = bisect.bisect_right(r, x_list[0])
        x_list.pop(0)
        target = r.pop(i)
        bisect.insort(x_list, target)

    if len(r) == 0:
        break

print(sum(x_list) + sum(y_list))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
x,y,a,b,c=map(int,input().split())
sa=list(map(int, input().split()))
sb=list(map(int, input().split()))
sc=list(map(int, input().split()))
sa.sort(reverse=True)
sb.sort(reverse=True)
# sc.sort(reverse=True)
rr=[]
rg=[]
for _ in range(x):
    rr.append(sa.pop(0))
for _ in range(y):
    rg.append(sb.pop(0))
rr.extend(rg)
rr.extend(sc)
rr.sort()
for _ in range(c):
    rr.remove(rr[0])
print(sum(rr))

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

eat = sorted(p, reverse=True)[:X]
eat[len(eat):] = sorted(q, reverse=True)[:Y]

eat.sort()

for r in R:
  if r>eat[0]:
    eat.remove(min(eat))
    eat.append(r)
  
print(sum(eat))

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

eat = sorted(p, reverse=True)[:X]
eat[len(eat):] = sorted(q, reverse=True)[:Y]

for r in R:
  if r>min(eat):
    eat.remove(min(eat))
    eat.append(r)
  
print(sum(eat))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

x, y, a, b, c = map(int, input().split())
p = [int(_) for _ in input().split()]
q = [int(_) for _ in input().split()]
r = [int(_) for _ in input().split()]

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

x_list = p[:x]
y_list = q[:y]

while (min(x_list[-1], y_list[-1]) < r[0]):
    if x_list[-1] > y_list[-1]:
        y_list.pop()
        y_list.append(r[0])
        y_list.sort(reverse=True)
        r.pop(0)
    else:
        x_list.pop()
        x_list.append(r[0])
        x_list.sort(reverse=True)
        r.pop(0)
    if len(r) == 0:
        break

print(sum(x_list) + sum(y_list))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
x,y,a,b,c=map(int,input().split())
sa=list(map(int, input().split()))
sb=list(map(int, input().split()))
sc=list(map(int, input().split()))
sa.sort(reverse=True)
sb.sort(reverse=True)
sc.sort(reverse=True)
rr=[]
rg=[]
rrr=[10**9]
rgg=[10**9]
for _ in range(x):
    if sc[0]<=sa[0]:
        rr.append(sa.pop(0))
    elif sc[0]>sa[0]:
        rrr.extend(sa)
        break
for _ in range(y):
    if sc[0]<=sb[0]:
        rg.append(sb.pop(0))
    elif sc[0]>sb[0]:
        rgg.extend(sb)
        break
for _ in range(10**9):
    if sc==[]:
        break
    elif min(rrr[-1],rgg[-1])<sc[0]:
        if rrr[-1]<sc[0]:
            rr.append(sc.pop(0))
            rrr.remove(rrr[-1])
        elif rgg[-1]<sc[0]:
            rg.append(sc.pop(0))
            rgg.remove(rgg[-1])
    else:
        rrr.remove(10**9)
        rgg.remove(10**9)
        rr.extend(rrr)
        rg.extend(rgg)
        break
print(sum(rr)+sum(rg))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
x,y,a,b,c=map(int,input().split())
sa=list(map(int, input().split()))
sb=list(map(int, input().split()))
sc=list(map(int, input().split()))
sa.sort(reverse=True)
sb.sort(reverse=True)
sc.sort(reverse=True)
rr=[]
rg=[]
rrr=[10**9]
rgg=[10**9]
for _ in range(x):
    if sc[0]<=sa[0]:
        rr.append(sa.pop(0))
    elif sc[0]>sa[0]:
        rrr.extend(sa)
        break
for _ in range(y):
    if sc[0]<=sb[0]:
        rg.append(sb.pop(0))
    elif sc[0]>sb[0]:
        rgg.extend(sb)
        break
for i in range(10**9):
    if sc==[]:
        break
    elif min(rrr[-1],rgg[-1])<sc[0]:
        if rrr[-1]<sc[0]:
            rr.append(sc.pop(0))
            rrr.remove(rrr[-1])
        elif rgg[-1]<sc[0]:
            rg.append(sc.pop(0))
            rgg.remove(rgg[-1])
    else:
        rrr.remove(10**9)
        rgg.remove(10**9)
        rr.extend(rrr)
        rg.extend(rgg)
        break
print(sum(rr)+sum(rg))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input = stdin.readline
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())
sa=list(map(int, input().split()))
sb=list(map(int, input().split()))
sc=list(map(int, input().split()))
sa.sort(reverse=True)
sb.sort(reverse=True)
sc.sort(reverse=True)
rr=[]
rg=[]
for i in range(x):
    rr.append(sa.pop(0))
for i in range(y):
    rg.append(sb.pop(0))
while sc[0]>min(rr[x-1],rg[y-1]):
    if sc[0]>rr[x-1]:
        rr.pop(x-1)
        rr.append(sc.pop(0))
        rr.sort(reverse=True)
    elif sc[0]>rg[y-1]:
        rg.pop(y-1)
        rg.append(sc.pop(0))
        rg.sort(reverse=True)
    if sc==[]:
        break


d=sum(rr)+sum(rg)
print(d)

Submission status: Time Limit Exceeded

Solution code:
# 1 2 2 2 1
X, Y, A, B, C = list(map(lambda x: int(x) ,input().split()))
al = list(map(lambda x: int(x) ,input().split()))
bl = list(map(lambda x: int(x) ,input().split()))
cl = list(map(lambda x: int(x) ,input().split()))

happiness = 0

for i in range(X):
    if len(cl) > 0:
        chapp = max(cl)
    rhapp = max(al)
    if chapp > rhapp:
        cl.remove(chapp)
        happiness += chapp 
    else:
        al.remove(rhapp)
        happiness += rhapp

for i in range(Y):
    if len(cl) > 0:
        chapp = max(cl)
    ghapp = max(bl)
    if chapp > ghapp:
        cl.remove(chapp)
        happiness += chapp
    else:
        bl.remove(ghapp)
        happiness += ghapp

print (happiness)

Submission status: Time Limit Exceeded

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

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