Problem:


Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74



Sample input:
1 2 2 2 1
2 4
5 1
3


Sample output:
12

Submission status: Time Limit Exceeded

Solution code:
import sys
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 stdin.readline().rstrip().split()]
q = [[int(x), "B"]  for x in stdin.readline().rstrip().split()]
r = [[int(x), "C"] for x in stdin.readline().rstrip().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:
    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:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	x,y,a,b,c = MI()
	p = np.array(LI())
	p = np.sort(p)[::-1]
	q = np.array(LI())
	r = np.array(LI())
	r = np.sort(r)[::-1]
	X = a
	ans = 0
	for X in range(min(a,x),-1,-1):
		Cx=0
		if(x > X):
			Cx = x-X
			s = np.concatenate([q,r[Cx-1:]], axis=0)
			s = np.sort(s)[::-1]
			ans = max(ans, np.sum(p[:X])+np.sum(r[:Cx])+np.sum(s[:y]))
		else:
			s = np.concatenate([q,r], axis=0)
			s = np.sort(s)[::-1]
			ans = max(ans, np.sum(p[:X])+np.sum(s[:y+Cx]))
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c= map(int, input().split())
red_list = list(map(int, input().split()))
green_list = list(map(int, input().split()))
no_list = list(map(int, input().split()))
red_list.sort(reverse=True)
green_list.sort(reverse=True)
no_list.sort(reverse=True)
dp_res = [[(0, 0, 0)]*(a+1) for i in range(b+1)]
for i in range(x+1):
  for j in range(y+1):
    if i == 0 and j == 0:
      continue
    if i == 0:
      use = dp_res[i][j-1]
      no_len = use[1] + use[2]
      if no_len >= len(no_list):
        next_no = 0
      else:
        next_no = no_list[no_len]
      next_green = green_list[j-1-use[2]]
      if next_green >= next_no:
        dp_value = (use[0]+next_green, use[1], use[2])
      else:
        dp_value = (use[0]+next_no, use[1], use[2]+1)
    elif j == 0:
      use = dp_res[i-1][j]
      no_len = use[1] + use[2]
      if no_len >= len(no_list):
        next_no = 0
      else:
        next_no = no_list[no_len]
      next_red = red_list[i-1-use[1]]
      if next_red >= next_no:
        dp_value = (use[0]+next_red, use[1], use[2])
      else:
        dp_value = (use[0]+next_no, use[1]+1, use[2])
    else:
      use = dp_res[i-1][j]
      no_len = use[1] + use[2]
      if no_len >= len(no_list):
        next_no = 0
      else:
        next_no = no_list[no_len]
      next_red = red_list[i-1-use[1]]
      if next_red >= next_no:
        dp_value_1 = (use[0]+next_red, use[1], use[2])
      else:
        dp_value_1 = (use[0]+next_no, use[1]+1, use[2])
      use = dp_res[i][j-1]
      no_len = use[1] + use[2]
      if no_len >= len(no_list):
        next_no = 0
      else:
        next_no = no_list[no_len]
      next_green = green_list[j-1-use[2]]
      if next_green >= next_no:
        dp_value_2 = (use[0]+next_green, use[1], use[2])
      else:
        dp_value_2 = (use[0]+next_no, use[1], use[2]+1)
      if dp_value_1[0] >= dp_value_2[0]:
        dp_value = dp_value_1
      else:
        dp_value = dp_value_2
    dp_res[i][j] = dp_value
print(dp_res[x][y][0])

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

while X > 0 or Y > 0:
  if len(p) != 0:
    Pmax = max(p)
  else:
    Pmax = 0

  if len(q) != 0:
    Qmax = max(q)
  else:
    Qmax = 0

  if len(r) != 0:
    Rmax = max(r)
  else:
    Rmax = 0  

  if Pmax >= Qmax and Pmax >= Rmax :
    if X > 0:
      ans = ans + Pmax
      X = X-1
    p.remove(Pmax)


  elif Qmax >= Rmax  :
    if Y > 0:
      ans = ans + Qmax
      Y = Y-1
    q.remove(Qmax)

  else:
    if X == 0 or Y == 0:
      if X == 0:
        ans = ans + Rmax
        Y = Y-1
      else :
        ans = ans + Rmax
        X = X-1
    
    else:

      if Pmax >= Qmax :
        ans = ans + Rmax
        Y = Y-1
      else:
        ans = ans + Rmax
        X = X-1
    r.remove(Rmax)
      

print(ans)

Submission status: Time Limit Exceeded

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

while x < X and y < Y:
    a = max(p)
    b = max(q)
    c = max(r)
    if a >= c:
        d += a
        x += 1
        p.remove(a)
    elif b >= c:
        d += b
        y += 1
        q.remove(b)
    elif c > a >= b:
        d += c
        y += 1
        r.remove(c)
    elif c > b > a:
        d += c
        x += 1
        r.remove(c)

while x < X:
    p.extend(r)
    d += max(p)
    x += 1
    p.remove(max(p))
while y < Y:
    q.extend(r)
    d += max(q)
    y += 1
    q.remove(max(q))
print(d)

Submission status: Time Limit Exceeded

Solution code:
i = list(map(int, input().split()))
red=list(map(int, input().split()))
green=list(map(int, input().split()))
white=list(map(int, input().split()))
list=[]
a=0
b=0
c=0
for j in range(i[0]):
    a=max(red)
    list.append(a)
    red.remove(a)
for j in range(i[1]):
    b=max(green)
    list.append(b)
    green.remove(b)
for j in range(i[0]+i[1]):
    if max(white)>min(list):
        list.remove(min(list))
        list.append(max(white))
    else:
        break
print(sum(list))

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()
del p[:-X]
del q[:-Y]
ans=sum(p)+sum(q)
for i in range(len(r)):
    a=r[-(i+1)]
    minp=min(p)
    minq=min(q)
    if minp<=minq and minp<a:
        ans=ans-minp+a
        del p[p.index(minp)]
        p.append(a)
    elif minp>=minq and minq<a:
        ans=ans-minq+a
        del q[q.index(minq)]
        q.append(a)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in stdin.readline().rstrip().split()]
q = [[int(x), "B"]  for x in stdin.readline().rstrip().split()]
r = [[int(x), "C"] for x in stdin.readline().rstrip().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:
    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:
import sys
from collections import deque

x, y, a, b, c  = map(int, input().split())
p = list(map(int, sys.stdin.readline().split()))
p.sort(reverse=True)
q = list(map(int, sys.stdin.readline().split()))
q.sort(reverse=True)
r = list(map(int, sys.stdin.readline().split()))
r.sort(reverse=True)

l = p[:x]
l.extend(q[:y])
l.sort(reverse=True)
n = x + y
ans = sum(l)
for i in range(len(r)):
    tmp = sum(l[:n-i-1]) + sum(r[:i+1])
    if tmp > ans:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

x,y,a,b,c=map(int, input().split())
pi=list(map(int,input().split()))
qi=list(map(int,input().split()))
ri=list(map(int,input().split()))
pi.sort(reverse=True)
qi.sort(reverse=True)
ri.sort(reverse=True)
ri=deque(ri)

apple=pi[:x]+qi[:y]
apple.sort(reverse=True)
apple=deque(apple)
while min(apple) <= ri[0]:
    c=ri.popleft()
    apple[-1]=c
    if len(ri)==0:
        break
print(sum(apple))

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)

redpoints = p[:x]
greenpoints = q[:y]
points = redpoints + greenpoints
flag=True
while(flag):
    rep = min(points)
    if len(r) < 1:
        flag = False
    elif rep < r[0]:
        points[points.index(rep)] = r.pop(0)
    else:
        flag = False
print(sum(points))

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = (int(x) for x in input().split())
list_p = list(map(int,input().split()))
list_q = list(map(int,input().split()))
list_r = list(map(int,input().split()))
list_p.sort()
list_q.sort()
list_r.sort()
list_p.reverse()
list_q.reverse()
list_r.reverse()
list_total =[]
for i in range(x):
    list_total.append(list_p[i])
for i in range(y):
    list_total.append(list_q[i])
list_total.sort()
for i in range(c):
    if list_r[0]>list_total[0]:
        list_total[0]=list_r[0]
        list_total.sort()
        list_r.pop(0)
print(sum(list_total))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


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 = defaultdict(lambda: defaultdict(lambda: (0, 0, 0, 0)))
    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 bisect

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()
green.sort()
clear.sort()

red = red[-X:]
green = green[-Y:]

def ins(a, x):
    insert_index = bisect.bisect_left(a,x)
    a.insert(insert_index,x)
    return a[1:]

while clear[-1] > min(red[0], green[0]):
    red_tmp = ins(red.copy(), clear[-1])
    green_tmp = ins(green.copy(), clear[-1])
    
    if (sum(red_tmp) + sum(green)) >= (sum(red) + sum(green_tmp)):
        # 赤に追加
        red = red_tmp
    else:
        # 緑に追加
        green = green_tmp
        
    if len(clear) == 1:
        break
    else:
        clear = clear[:-1] #clearを消去

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
X, Y, A, B, C = map(int, input().split(' '))

red = list(map(int, input().split(' ')))
green = list(map(int, input().split(' ')))
none_a = list(map(int, input().split(' ')))

none_new = np.array(none_a)
red1 = red + none_a
eat_red_1 = sorted(red1, reverse = True)[:X]

for eaten in eat_red_1:
  if none_a.count(eaten) > 0:
  	none_a.remove(eaten)
green1 = green + none_a
eat_green_1 = sorted(green1, reverse = True)[:Y]

result1 = sum(eat_red_1) + sum(eat_green_1)

print(result1)

Submission status: Time Limit Exceeded

Solution code:

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

X = int(X)
Y = int(Y)
A = int(A)
B = int(B)
C = int(C)
point = 0
X_left = X
Y_left = Y

for i in range(X):
    if max(p) > max(r):
        point += max(p)
        p.remove(max(p))
        X_left -= 1
        

for i in range(Y):
    if max(q) > max(r):
        point += max(q)
        q.remove(max(q))
        Y_left -= 1


for i in range(X):
    if X_left > 0:
        point += max(r)
        r.remove(max(r))
        X_left -= 1
    else:
        break

for i in range(Y):
    if Y_left > 0:
        point += max(r)
        r.remove(max(r))
        Y_left -= 1
    else:
        break


print(point)

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = list(map(int, input().split(" ")))
p = list(map(int, input().split(" ")))
p.sort(reverse=True)
q = list(map(int, input().split(" ")))
q.sort(reverse=True)
r = list(map(int, input().split(" ")))
r.sort(reverse=True)
a_x = p[0:x]
b_y = q[0:y]


while  ((min(a_x) < max(r)) or (min(b_y) < max(r))): 
    
        if min(a_x) < min(b_y):  
            p_i = r.index(max(r))
            ap = r.pop(p_i)
            d_i = a_x.index(min(a_x))
            ad = a_x.pop(d_i)
            a_x.append(ap)


        else:
            p_i = r.index(max(r))
            bp = r.pop(p_i)
            d_i = b_y.index(min(b_y))
            bd = b_y.pop(d_i)
            b_y.append(bp)
        if len(r) == 0:
            break

print(sum(a_x) + sum(b_y))

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

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())
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 R >= 0:
    if P < A:
        if (r[R] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
        elif Q < B:
            if (r[R] > q[Q]):
                Q += 1
                R -= 1
            else:
                break
    elif Q < B:
        if (r[R] > q[Q]):
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

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:
    while(X_apple[0]<NOCOL[C-1]):
        X_apple[0]=NOCOL[C-1]
        NOCOL[C-1]=0
        NOCOL.sort()
        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:
    while(Y_apple[0]<NOCOL[C-1]):
        Y_apple[0]=NOCOL[C-1]
        NOCOL[C-1]=0
        NOCOL.sort()
        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:
# coding: utf-8
# Your code here!
import numpy as np
X,Y,A,B,C = list(map(int,input().split()))
p =list(map(int,input().split()))
p.append(0)
p.sort(reverse = True)
# print(p)
q=list(map(int,input().split()))
q.append(0)
q.sort(reverse = True)
r = list(map(int,input().split()))
r.append(0)
r.sort(reverse = True)

p = np.array(p)
q = np.array(q)
r = np.array(r)
kotae = 0
a,b,c = 0,0,0
for i in range(X+Y):
    if a >= X:
        p = p* 0
    if b >= Y:
        q = q*0
    if p[a] > q[b]:
        if p[a] > r[c]:
            kotae += p[a]
            a +=1
        else:
            kotae += r[c]
            c += 1
    else:
        if q[b] > r[c]:
            kotae += q[b]
            b +=1
        else:
            kotae += r[c]
            c += 1
    # print(kotae)
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
def a():
    S = input()

    if S[3] == S[2] and S[4] == S[5]:
        print("Yes")
    else:
        print("No")


def b():
    X = int(input())
    y_500 = (X // 500) * 1000
    y_5 = ((X - (y_500 / 2)) // 5) * 5
    print(int(y_500 + y_5))


def c():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))

    max_A_d = -1
    for i in range(len(A)):
        A_d = 0
        if i == 0:
            A_d = K - (A[-1] - A[0])
        else:
            A_d = (A[i] - A[i - 1])
        if max_A_d < A_d:
            max_A_d = A_d
    d = K - max_A_d
    print(d)


def d():
    N, X, Y = map(int, input().split())
    print()


def e():
    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-X,q-Y
    X_e = p[0:X]
    Y_e = q[0:Y]
    # X_e.sort()
    # Y_e.sort()
    while True:
        if not r:
            break
        if X_e[-1] > r[0] and Y_e[-1] > r[0]:
            break

        if X_e[-1] < Y_e[-1]:
            X_e.pop(-1)
            X_e.insert(0, r.pop(0))
        else:
            Y_e.pop(-1)
            Y_e.insert(0, r.pop(0))
    print(sum(X_e) + sum(Y_e))


e()

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)
P_cs, Q_cs, R_cs = {}, {}, {}
P_cs[0] = P[0]
Q_cs[0] = Q[0]
R_cs[0] = R[0]
P_cs[-1] = 0
Q_cs[-1] = 0
R_cs[-1] = 0

for i in range(a-1):
  P_cs[i+1] = P_cs[i] + P[i+1]
for i in range(b-1):
  Q_cs[i+1] = Q_cs[i] + Q[i+1]
for i in range(c-1):
  R_cs[i+1] = R_cs[i] + R[i+1]

#print(P_cs)
#print(Q_cs)
#print(R_cs)

max_total = 0
for p in range(x+1):
  for q in range(y, -1, -1):
    r = x + y - p - q
    #print(r)
    if r > c or r <= -1:
      break
    else:
      total = P_cs[p-1] + Q_cs[q-1] + R_cs[r-1]
      #print(p, q, r, total)
      max_total = max(max_total, total)
print(max_total)

Submission status: Time Limit Exceeded

Solution code:

from bisect import bisect_left
X, Y, A, B, C = map(int, input().split())
red_apples = list(map(int, input().split()))
green_apples = list(map(int, input().split()))
white_apples = list(map(int, input().split()))
# while_applesから、どれを赤に、緑にするか。
# とりあえず無色のままにする必要はないので、0=赤1=緑として、white_applesを全て振り分けてみる
# その全ての組み合わせを探索する？


max_cost = 0
for i in range(2**C):
    to_red_apples = []
    for j in range(C):
        if((i >> j) & 1):
            to_red_apples.append(1)
        else:
            to_red_apples.append(0)
    new_red_apples = []
    new_red_apples.extend(red_apples)
    new_green_apples = []
    new_green_apples.extend(green_apples)
    for index, apple in enumerate(white_apples):
        if to_red_apples[index] == 1:
            new_red_apples.append(apple)
        else:
            new_green_apples.append(apple)
    new_red_apples.sort(reverse=True)
    new_green_apples.sort(reverse=True)
    cost = 0
    for i in range(X):
        cost += new_red_apples[i]
    for i in range(Y):
        cost += new_green_apples[i]
    max_cost = max(max_cost, cost)
print(max_cost)

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

red=heapq.nlargest(x, p)
grn=heapq.nlargest(y, q)
#print(red)
#print(grn)

for i in r:
  if i> red[-1]:
    red=[i]+red[:-1]
  elif i> grn[-1]:
    grn=[i]+grn[:-1]
  else:
    break
#print(red+grn)
print(sum(red+grn))

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()))
P.sort()
Q.sort()
R.sort()
P=P[-X:]
Q=Q[-Y:]
while (P[0]<R[-1] or Q[0]<R[-1]) :
  if P[0]<Q[0]:
    del P[0]
    bisect.insort(P,R[-1])
    del R[-1]
  else:
    del Q[0]
    bisect.insort(Q,R[-1])
    del R[-1]
  if R==[]:
    break
print(sum(P)+sum(Q))

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

flag = 1

while(flag):
  tmp = 0
  _min = min(arr)
  _max = max(C)
  if _min <= _max:
    arr[arr.index(_min)] = _max
  else:
    flag = 0
"""
_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:
#!/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
      la.pop(0)
      ca += 1
      continue
      
    if b >= a and b >= c:
      s += b
      lb.pop(0)
      cb += 1
      continue
      
    if c >= b and c >= a:
      s += c
      lc.pop(0)
      cc += 1
  print(s)

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
er,eg,ar,ag,ac=map(int, input().split())
b=input().split()
c=input().split()
d=input().split()
dr=[int(i) for i in b]
dg=[int(i) for i in c]
dc=[int(i) for i in d]
t=[]
answer=0
for i in dr:
	t.append(i)
for i in dg:
	t.append(i)
for i in dc:
	t.append(i)
while eg!=0 or er!=0:
	temp=max(t)
	t.remove(temp)
	if temp in dr and er!=0:
		answer+=temp
		er-=1
	elif temp in dg and eg!=0:
		answer+=temp
		eg-=1
	elif temp in dc and er!=0:
		answer+=temp
		er-=1
	elif temp in dc and eg!=0:
		answer+=temp
		eg-=1
print(answer,end="")

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

    N=[]

    for i in range(X):
        N.append(p[i])

    for j in range(Y):
        N.append(q[j])

    while  min(N)<max(r) :
        N.pop(N.index(min(N)))
        N.append(max(r))

    print(sum(N))
main()

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 and NOCOL[C-1]!=0):
        X_apple[i]=NOCOL[C-i-1]
        NOCOL[C-i-1]=0
        i+=1
else:
    while(X_apple[0]<NOCOL[C-1]):
        X_apple[0]=NOCOL[C-1]
        NOCOL[C-1]=0
        NOCOL.sort()
        X_apple.sort()

NOCOL.sort()
i=0
if Y_apple[i]==0:
    while(Y_apple[i]==0 and NOCOL[C-1]!=0):
        Y_apple[i]=NOCOL[C-i-1]
        NOCOL[C-i-1]=0
        i+=1
else:
    while(Y_apple[0]<NOCOL[C-1]):
        Y_apple[0]=NOCOL[C-1]
        NOCOL[C-1]=0
        NOCOL.sort()
        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())
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=A[:x]+B[:y]
l=sorted(l)
lim=l[0]
for tt in C:
    if lim>tt:break
    index=bisect.bisect_left(l,tt)
    l.insert(index,tt)
    l=l[1:]
    lim=l[0]
print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

x, y, a, b, c = [int(x) for x in stdin.readline().rstrip().split()]

p = [int(x) for x in stdin.readline().rstrip().split()]
q = [int(x) for x in stdin.readline().rstrip().split()]
r = [int(x) for x in stdin.readline().rstrip().split()]


ps = sorted(p, reverse=True)
qs = sorted(q, reverse=True)

pss = ps[0:x]
pss.extend(qs[0:y])

he = 0

for i in range(x+y):
    for j in range(len(r)):
        if pss[i] < r[j]:
            pss[i] = r.pop(j)
            break
    he += pss[i]


print(he)

Submission status: Time Limit Exceeded

Solution code:
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,
  
  lx = []
  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:
      lx.append(a)
      la.pop(0)
      ca += 1
      continue
      
    if b >= a and b >= c:
      lx.append(b)
      lb.pop(0)
      cb += 1
      continue
      
    if c >= b and c >= a:
      lx.append(c)
      lc.pop(0)
      cc += 1
  print(sum(lx))

if __name__=='__main__':
  main()

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, reverse = True)[:x]
q = sorted(q, reverse = True)[:y]
r = sorted(r, reverse = True)

for i in range(min(x+y, len(r))):
  if p[x-1] >= q[y-1] and r[0] > q[y-1]:
    q.pop(y-1)
    q.append(r[0])
    r.pop(0)
    q = sorted(q, reverse = True)
  elif q[y-1] >= p[x-1] and r[0] > p[x-1]:
    p.pop(x-1)
    p.append(r[0])
    r.pop(0)
    p = sorted(p, reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
X,Y,A,B,C = map(int,input().split())
P = deque(map(int,input().split()))
Q = deque(map(int,input().split()))
R = deque(map(int,input().split()))
p = sorted(P,reverse=True)
q = sorted(Q,reverse=True)
r = deque(sorted(R,reverse=True))
p1 = deque(p[0:X])
q1 = deque(q[0:Y])
while len(r) > 0:
    if min(p1[X-1],q1[Y-1]) < r[0]:
        if min(p1[X-1],q1[Y-1]) == p1[-1]:
            p1.pop()
            p1.append(r.popleft())
            p1 = deque(sorted(p1,reverse=True))
        else:
            q1.pop()
            q1.append(r.popleft())
            q1 = deque(sorted(q1,reverse=True))
    else:
        break
ans = sum(p1) + sum(q1)
print(ans)