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 list_change(list1,list2):
    list1.remove(min(list1))
    list1.append(max(list2))
    list2.remove(max(list2))
    return list1,list2


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


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

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

#print(p)
#print(q)
#print(r)

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

#print(ps[0:x])
#print(qs[0:y])

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

he = 0

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


print(he)

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

a=p[:]
a.extend(q)
a.extend(r)
a.sort()
a.reverse()
red=0
green=0
sum=0
for i in range(X+Y):
  if(a[i] in p):
    red+=1
  elif(a[i] in q):
    green+=1
    
if(red<=X and green<=Y):
  c=a[:(X+Y)]
  d=0
  for i in range(X+Y):
    d+=a[i]
  print(d)
elif(red>X):
  p.sort()
  p.reverse()
  for i in range(X):
    sum+=p[i]
  b=q[:]
  b.extend(r)
  b.sort()
  b.reverse()
  for i in range(Y):
    sum+=b[i]
  print(sum)
elif(green>Y):
  e=0
  q.sort()
  q.reverse()
  for i in range(Y):
    e+=q[i]
  sum+=e
  b=p[:]
  b.extend(r)
  b.sort()
  b.reverse()
  for i in range(X):
    sum+=b[i]
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int, input().split())
result=sorted(list(map(int, input().split())), reverse=True)[:x]
result+=sorted(list(map(int, input().split())), reverse=True)[:y]
r=sorted(list(map(int, input().split())), reverse=True)
for el in r:
    temp = min(result)
    if el > temp:
        result.remove(temp)
        result.append(el)
print(sum(result))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

X, Y, A, B, C = map(int, input().split())

p = sorted(list(map(int,input().split())))[-X:]
q = sorted(list(map(int,input().split())))[-Y:]
r = sorted(list(map(int,input().split())), reverse=True)

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

    if p_min > i and q_min > i:
        break

    p_min_diff = i - p_min
    q_min_diff = i - q_min

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

p = np.array(p)
q = np.array(q)
print(np.sum(p) + np.sum(q))

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:continue
    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:
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 = [P[0]]
Q_cs = [Q[0]]
R_cs = [R[0]]
for i in range(a-1):
  P_cs.append(P_cs[-1] + P[i+1])
for i in range(b-1):
  Q_cs.append(Q_cs[-1] + Q[i+1])
for i in range(c-1):
  R_cs.append(R_cs[-1] + R[i+1])
P_cs.append(0)
Q_cs.append(0)
R_cs.append(0)

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

max_total = 0
for p in range(x+1):
  for q in range(y+1):
    r = x + y - p - q
    if r <= c and r >= 0:
      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:
def list_change(list1,list2):
    list1.remove(min(list1))
    list1.append(max(list2))
    list2.remove(max(list2))
    return list1,list2


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


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

Submission status: Time Limit Exceeded

Solution code:
eat_red, eat_green, have_red, have_grean, have_white = map(int, input().split())
reds = list(map(int, input().split()))
greens = list(map(int, input().split()))
whites = list(map(int, input().split()))
reds.sort(reverse=True)
greens.sort(reverse=True)
whites.sort(reverse=True)
use_red = reds[:eat_red]
use_green = greens[:eat_green]

for w in whites:
    if w > min(min(use_red), min(use_green)):
        if min(use_red) > min(use_green):
            use_green[-1] = w
            use_green.sort(reverse=True)
        else:
            use_red[-1] = w
            use_red.sort(reverse=True)

print(sum(use_red) + sum(use_green))

Submission status: Time Limit Exceeded

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

red=p[:x]
grn=q[:y]
#print(red)
#print(grn)

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

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

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

p_sum = sum(p)
q_sum = sum(q)

for i in r:
    p_temp = (p_sum - p[-1] + i)
    q_temp = (q_sum - q[-1] + i)
    if p_temp >= p_sum or q_temp >= q_sum:        
        if p_temp - p_sum > q_temp - q_sum:
            p_sum = p_temp
            p.insert(0, i)
            p.pop(-1)        
        else:
            q_sum = q_temp
            q.insert(0, i)
            q.pop(-1)
    else:
        break

print(p_sum + q_sum)

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)
result = []
for el in p[:x]:
    result.append(el)
for el in q[:y]:
    result.append(el)
for el in r:
    temp = min(result)
    if el > temp:
        result.remove(temp)
        result.append(el)
print(sum(result))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def main():
    X,Y,R,G,B=mis()
    rs=lmis()
    rs.sort(reverse=True)
    gs=lmis()
    gs.sort(reverse=True)
    bs=lmis()
    bs.sort(reverse=True)
    def get_score(red_paint):
        if red_paint > B:
            return INF
        if Y > G + (B - red_paint):
            return INF
        ans = 0
        ans += sum(bs[:red_paint])
        ans += sum(rs[:X - red_paint])
        gi = 0
        bi = red_paint
        for _ in range(Y):
            if G == gi:
                go_g = False
            elif B == bi:
                go_g = True
            else:
                go_g = (gs[gi] >= bs[bi])
            if go_g:
                ans += gs[gi]
                gi += 1
            else:
                ans += bs[bi]
                bi += 1
        return ans
    print(max(get_score(i) for i in range(X+1)))


main()

Submission status: Time Limit Exceeded

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

red=p[:x]
grn=q[:y]
#print(red)
#print(grn)

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:
X, Y, A, B, C = map(int, input().split())
 
p = sorted(list(map(int,input().split())))[-X:]
q = sorted(list(map(int,input().split())))[-Y:]
r = sorted(list(map(int,input().split())), reverse=True)
 
for i in r:
    p_min = p[0]
    q_min = q[0]
 
    if p_min > i and q_min > i:
        break
 
    p_min_diff = i - p_min
    q_min_diff = i - q_min
 
    if p_min_diff >= q_min_diff and p_min_diff > 0:
        p.remove(p_min)
        p.append(i)
    elif q_min_diff > p_min_diff and q_min_diff > 0:
        q.remove(q_min)
        q.append(i)
 
print(sum(p) + sum(q))

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)
x=p[:X]
y=q[:Y]
s=sum(x)+sum(y)
sub=0

for i in range(len(r)):
    if x[-1]>y[-1]:
        t=y.pop(-1)
        if t<r[0]:
            y.insert(0,r[0])
            sub=sub+r.pop(0)-t
        else:
            break
    else:
        t=x.pop(-1)
        if t<r[0]:
            x.insert(0,r[0])
            sub=sub+r.pop(0)-t
        else:
            break

print(s+sub)

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

#print(p)
#print(q)
#print(r)

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

#print(ps[0:x])
#print(qs[0:y])

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

he = 0

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

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)

red = []
green = []

for i in range(x):
    red.append(p[i])
for j in range(y):
    green.append(q[j])

# print(red)
# print(green)

all = red + green
all.sort()
for mushoku in r:
    for i in range(len(all)):
        if mushoku > all[i]:
            all[i] = mushoku
print(sum(all))

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

red_new = np.array(red)
green_new = np.array(green)
none_new = np.array(none_a)

red.extend(none_a)
eat_red_1 = sorted(red, reverse = True)[:X]

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

result1 = sum(eat_red_1) + sum(eat_green_1)

red_new = list(red_new)
green_new = list(green_new)
none_new = list(none_new)
green_new.extend(none_new)
eat_green_2 = sorted(green_new, reverse = True)[:Y]
for eaten in eat_green_2:
  if none_new.count(eaten) > 0:
  	none_new.remove(eaten)
red_new.extend(none_new)
eat_red_2 = sorted(red_new, reverse = True)[:X]

result2 = sum(eat_red_2) + sum(eat_green_2)

print(max(result1, result2))

Submission status: Time Limit Exceeded

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

p = sorted(list(map(int,input().split())))[-X:]
q = sorted(list(map(int,input().split())))[-Y:]
r = sorted(list(map(int,input().split())), reverse=True)

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

    if p_min > i and q_min > i:
        break

    p_min_diff = i - p_min
    q_min_diff = i - q_min

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

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

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()))
result = 0
Z = 0
while (X>0 or Y>0) and X+Y-Z>0:
        
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    if (X>0 or Y>0) and X+Y-Z>0:
        if p[0]>=q[0] and p[0]>=r[0] and X > 0:
            result += p[0]
            
            p[0] = 0
            X-=1
        elif q[0]>=p[0] and q[0]>=r[0] and Y > 0:
            result += q[0]
            
            q[0] = 0
            Y-=1
        elif r[0]>=q[0] and r[0]>=p[0]:
        
            result += r[0]
            r[0] = 0
            Z+=1

        elif X > 0:
            result += p[0]
            
            p[0] = 0
            X-=1
        elif Y > 0:
            result += q[0]
            
            q[0] = 0
            Y-=1
       
print(result)

Submission status: Time Limit Exceeded

Solution code:
import heapq

xi = 0
yi = 0
zi = 0
def next(i):
    global xi, yi, zi, p, q, r
    if i == 0:
        xi += 1
        return heapq.nlargest(xi, p)[-1]
    elif i == 1:
        yi += 1
        return heapq.nlargest(yi, q)[-1]
    else:
        zi += 1
        return heapq.nlargest(zi, r)[-1]

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

ns = [next(0), next(1), next(2)]

val = 0

while x + y - xi - yi - zi + 3 > 0:
    if ns[0] >= ns[1] and ns[0] >= ns[2]:
        val += ns[0]
        if len(p) > 0 and x - xi > 0:
            ns[0] = next(0)
        else:
            xi += 1
            ns[0] = 0
    elif ns[0] <= ns[1] and ns[1] >= ns[2]:
        val += ns[1]
        if len(q) > 0 and y - yi > 0:
            ns[1] = next(1)
        else:
            yi += 1
            ns[1] = 0
    else:
        val += ns[2]
        if len(r) > 0:
            ns[2] = next(2)
        else:
            ns[2] = 0
print(val)

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[0] > r[0] and Y_e[0] > r[0]:
            break

        if X_e[0] < Y_e[0]:
            if X_e[0] < r[0]:
                X_e.pop(0)
                X_e.append(r.pop(0))
        else:
            if Y_e[0] < r[0]:
                Y_e.pop(0)
                Y_e.append(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 = input().split()
p = [int(s) for s in p]
q = input().split()
q = [int(s) for s in q]
r = input().split()
r = [int(s) for s in r]
reds = sorted(p,reverse=True)
reds = reds[:x]
greens = sorted(q,reverse=True)
greens = greens[:y]
r = sorted(r,reverse=True)
while len(r) > 0 and (r[0] > reds[x-1] or r[0] > greens[y-1]):
    if(reds[x-1] > greens[y-1]):
        del greens[y-1]
        greens.insert(0,r.pop(0))
    else:
        del reds[x-1]
        reds.insert(0,r.pop(0))

print(sum(reds)+sum(greens))

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)
x=0
y=0
z=0
s=0
while (x<X or y<Y) and z<X+Y:
  if (max(p[0],q[0],r[0])==p[0] or (y==Y and r[0]<p[0])) and x<X:
    s+=p[0]
    p.remove(p[0])
    p.append(0)
    x+=1
    z+=1
  elif (max(p[0],q[0],r[0])==q[0] or (x==X and r[0]<q[0])) and y<Y:
    s+=q[0]
    q.remove(q[0])
    q.append(0)
    y+=1
    z+=1
  else:
    s+=r[0]
    r.remove(r[0])
    r.append(0)
    z+=1
print(s)

Submission status: Time Limit Exceeded

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

from sys import stdin


def main():
    X, Y, A, B, C = list(map(int, stdin.readline().split()))
    p = list(map(int, stdin.readline().split()))
    q = list(map(int, stdin.readline().split()))
    r = list(map(int, stdin.readline().split()))

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

    oisisa = 0
    x, y, z = 0, 0, 0

    for i in range(0, X + Y):
        m_p = p[0] if len(p) > 0 else 0
        m_q = q[0] if len(q) > 0 else 0
        m_r = r[0] if len(r) > 0 else 0
        m = max( [m_p, m_q, m_r] )

        if m_p == m:
            # pから取る
            oisisa += p.pop(0)
            x += 1

        elif m_q == m:
            # qから取る
            oisisa += q.pop(0)
            y += 1
        else:
            # rから取る
            oisisa += r.pop(0)
            z += 1

        if x >= X:
            p = []
        if y >= Y:
            q = []

        if x + y + z == X + Y:
            print(oisisa)
            return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C=map(int,input().split())
P=sorted(list(map(int,input().split())))
Q=sorted(list(map(int,input().split())))
R=sorted(list(map(int,input().split())))
S=sorted(P[A-X:]+Q[B-Y:])
ans=sum(S)
for i in range(X+Y):
    if len(R)==0 or len(S)==0 or S[0]>R[-1]:
        break
    ans-=S.pop(0)
    ans+=R.pop(-1)
print(ans)

Submission status: Time Limit Exceeded

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

sum = 0
num_X = 0
num_Y = 0

for i in range(X+Y):
  if max(p) >= max(q) and max(p) >= max(r) and num_X < X:
    sum += max(p)
    p[p.index(max(p))] = 0
    num_X += 1
  elif max(q) >= max(p) and max(q) >= max(r) and num_Y < Y:
    sum += max(q)
    q[q.index(max(q))] = 0
    num_Y += 1
  elif max(r) >= max(p) and max(r) >= max(q) and max(p) >= max(q) and num_Y < Y:
    sum += max(r)
    r[r.index(max(r))] = 0
    num_Y += 1
  elif max(r) >= max(p) and max(r) >= max(q) and max(q) >= max(p) and num_X < X:
    sum += max(r)
    r[r.index(max(r))] = 0
    num_X += 1
  elif num_X == X and max(q) >= max(r):
    sum += max(q)
    q[q.index(max(q))] = 0
    num_Y += 1
  elif num_X == X and max(r) >= max(q):
    sum += max(r)
    r[r.index(max(r))] = 0
    num_Y += 1
  elif num_Y == Y and max(p) >= max(r):
    sum += max(p)
    p[p.index(max(p))] = 0
    num_X += 1
  elif num_Y == Y and max(r) >= max(p):
    sum += max(r)
    r[r.index(max(r))] = 0
    num_X += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
from bisect import insort_left

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c=map(int,input().split())
p=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)
ans=0

z=x+y

while(z!=0):
    while((x!=0)and(y!=0)):
            a=[]
            a.append(p[0])
            a.append(q[0])
            a.append(r[0])
            if max(a)==p[0]:
                x-=1
                z-=1
                ans+=p[0]
                p=p[1:]
            elif max(a)==q[0]:
                y-=1
                z-=1
                ans+=q[0]
                q=q[1:]
            else:
                z-=1
                ans+=r[0]
                r=r[1:]
        
    if x==0:
        if q[0]>=r[0]:
            y-=1
            z-=1
            ans+=q[0]
            q=q[1:] 
        else:
            z-=1
            ans+=r[0]
            r=r[1:]
    elif y==0:
        if p[0]>=r[0]:
            x-=1
            z-=1
            ans+=p[0]
            p=p[1:] 
        else:
            z-=1
            ans+=r[0]
            r=r[1:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
s = p + q + r
s.sort()
s.reverse()
i = 0
j = 0
ans = 0
a = 0
b = 0
c = 0
while i < (X + Y):
  if s[j] in r:
    c += 1
    r.remove(s[j])
    ans += s[j]
    i += 1
  elif s[j] in p and a < X:
    a += 1
    p.remove(s[j])
    ans += s[j]
    i += 1
  elif s[j] in q and b < Y:
    b += 1
    q.remove(s[j])
    ans += s[j]
    i += 1
  j += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C=map(int,input().split())
P=sorted(list(map(int,input().split())))
Q=sorted(list(map(int,input().split())))
R=sorted(list(map(int,input().split())))
S=sorted(P[A-X:]+Q[B-Y:])
for i in range(X+Y):
    if len(R)==0 or S[0]>R[-1]:
        break
    S.pop(0)
    S.append(R.pop(-1))
print(sum(S))

Submission status: Time Limit Exceeded

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

import bisect
import sys
input = sys.stdin.readline

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

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

Submission status: Time Limit Exceeded

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

from sys import stdin


def main():
    X, Y, A, B, C = list(map(int, stdin.readline().split()))
    p = list(map(int, stdin.readline().split()))
    q = list(map(int, stdin.readline().split()))
    r = list(map(int, stdin.readline().split()))

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

    oisisa = 0
    x, y, z = 0, 0, 0
    for i in range(0, X + Y):
        m_p = p[0] if len(p) > 0 else 0
        m_q = q[0] if len(q) > 0 else 0
        m_r = r[0] if len(r) > 0 else 0
        m = max( [m_p, m_q, m_r] )

        if x < X and m_p == m:
            # pから取る
            oisisa += p.pop(0)
            x += 1

        elif y < Y and m_q == m:
            # qから取る
            oisisa += q.pop(0)
            y += 1
        else:
            # rから取る
            oisisa += r.pop(0)
            z += 1

        if x >= X:
            p = []
        if y >= Y:
            q = []

        if x + y + z == X + Y:
            print(oisisa)
            return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

from bisect import insort

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

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

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

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