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,aa,bb,cc = map(int,raw_input().split(' '))

aas = map(int, raw_input().split(' '))
bbs = map(int, raw_input().split(' '))
ccs = map(int, raw_input().split(' '))
aas.sort(key = lambda x:-x)
bbs.sort(key = lambda x:-x)
ccs.sort(key = lambda x:-x)

for i in range(1,len(aas)): aas[i] += aas[i-1]
for i in range(1,len(bbs)): bbs[i] += bbs[i-1]
for i in range(1,len(ccs)): ccs[i] += ccs[i-1]


r = 0

for a in range(x + 1):
	for b in range(y + 1)[::-1]:
		m = (aas[a-1] if a-1 >= 0 else 0) + (bbs[b-1] if b-1 >=0 else 0)
		c = x + y - (a + b)
		if c == 0:
			m += 0
		elif c > 0 and c-1 < len(ccs): 	 	
			m += ccs[c-1]
		else: 
			m+= -float('inf')
			break
		r = max(r, m)

print r

Submission status: Time Limit Exceeded

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

aas = map(int, raw_input().split(' '))
bbs = map(int, raw_input().split(' '))
ccs = map(int, raw_input().split(' '))
aas.sort(key = lambda x:-x)
bbs.sort(key = lambda x:-x)
ccs.sort(key = lambda x:-x)

for i in range(1,len(aas)): aas[i] += aas[i-1]
for i in range(1,len(bbs)): bbs[i] += bbs[i-1]
for i in range(1,len(ccs)): ccs[i] += ccs[i-1]


r = 0
for a in range(x+1):
	for b in range(y+1):
		m = (aas[a-1] if a-1 >= 0 else 0) + (bbs[b-1] if b-1 >=0 else 0)
		c = x + y - (a + b)
		if c > 0 and c-1 < len(ccs): 	 	
			m += ccs[c-1]
		else: 
			if c: m+= -float('inf')

		r = max(r, m)

print r

Submission status: Time Limit Exceeded

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

p = sorted(list(map(int,input().split()))) #A
q = sorted(list(map(int,input().split()))) #B
r = sorted(list(map(int,input().split()))) #C

xlist = p[-X:] #X
ylist = q[-Y:] #Y

def main(X,Y,xlist,ylist,r):
    for i in range(min(X,Y)):
        if xlist[i] < ylist[i] and min(xlist) < max(r):
            xlist.remove(min(xlist))
            xlist.append(max(r))
            r.pop()
        elif ylist[i] < xlist[i] and min(ylist) < max(r):
            ylist.remove(min(ylist))
            ylist.append(max(r))
            r.pop()
        elif min(xlist) >= max(r) and min(ylist) >= max(r):
            break
        else:
            continue
    
    print(sum(xlist)+sum(ylist))

main(X,Y,xlist,ylist,r)

Submission status: Time Limit Exceeded

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

p = sorted(list(map(int,input().split()))) #A
q = sorted(list(map(int,input().split()))) #B
r = sorted(list(map(int,input().split()))) #C

xlist = p[-X:] #X
ylist = q[-Y:] #Y

def main(X,Y,xlist,ylist,r):
    for i in range(min(X,Y)):
        if xlist[i] < ylist[i] and min(xlist) < max(r):
            xlist.remove(min(xlist))
            xlist.append(r.pop())
            
        elif ylist[i] < xlist[i] and min(ylist) < max(r):
            ylist.remove(min(ylist))
            ylist.append(r.pop())
            
        else:
            continue
    
    print(sum(xlist)+sum(ylist))

main(X,Y,xlist,ylist,r)

Submission status: Time Limit Exceeded

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

p = sorted(list(map(int,input().split()))) #A
q = sorted(list(map(int,input().split()))) #B
r = sorted(list(map(int,input().split()))) #C

xlist = p[-X:] #X
ylist = q[-Y:] #Y

def main(X,Y,xlist,ylist,r):
    for i in range(min(X,Y)):
        if xlist[i] < ylist[i] and min(xlist) < max(r):
            xlist.remove(min(xlist))
            xlist.append(max(r))
            r.pop()
        elif ylist[i] < xlist[i] and min(ylist) < max(r):
            ylist.remove(min(ylist))
            ylist.append(max(r))
            r.pop()
        else:
            continue
    
    print(sum(xlist)+sum(ylist))

main(X,Y,xlist,ylist,r)

Submission status: Time Limit Exceeded

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

p = sorted(list(map(int,input().split()))) #A
q = sorted(list(map(int,input().split()))) #B
r = sorted(list(map(int,input().split()))) #C

xlist = p[-X:] #X
ylist = q[-Y:] #Y

def main(X,Y,A,B,C):
    for i in range(min(X,Y)):
        if xlist[i] < ylist[i] and min(xlist) < max(r):
            xlist.remove(min(xlist))
            xlist.append(max(r))
            r.pop()
        elif ylist[i] < xlist[i] and min(ylist) < max(r):
            ylist.remove(min(ylist))
            ylist.append(max(r))
            r.pop()
        else:
            continue
    
    print(sum(xlist)+sum(ylist))

main(X,Y,A,B,C)

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)
p2=p[:x]
q2=q[:y]
memop=x-1
memoq=y-1
sp=sum(p2)
sq=sum(q2)
k=0
flagp=0
flagq=0
while k<c:
    if memop<0:
        flagp=1
    if memoq<0:
        flagq=1
    if flagp*flagq==1:
        break
    elif flagq==1:
        if p2[memop]>=r[k]:
            break
        else:
            sp-=p2[memop]
            sp+=r[k]
            k+=1
            memop-=1
    elif flagp==1:
        if q2[memoq]>=r[k]:
            break
        else:
            sq-=q2[memoq]
            sq+=r[k]
            k+=1
            memoq-=1
    elif p2[memop]>=r[k] and q2[memoq]>=r[k] and flagp+flagq==0:
        break
    elif p2[memop]>q2[memoq]:
        sq-=q2[memoq]
        sq+=r[k]
        k+=1
        memoq-=1
    elif p2[memop]>=q2[memoq]:
        sp-=p2[memop]
        sp+=r[k]
        k+=1
        memop-=1
print(sp+sq)

Submission status: Time Limit Exceeded

Solution code:

def main():
  x, y, _a, _b, _c = map(int, input().split())
  p = sorted(map(int, input().split()))[-x:]
  q = sorted(map(int, input().split()))[-y:]
  r = sorted(map(int, input().split()), reverse=True)[:x + y]

  arr = sorted(p + q)

  for nc_apple in r:
    apple = arr[0]
    if apple >= nc_apple: break

    arr.pop(0)
    arr.append(nc_apple)

  print(sum(arr))


main()

Submission status: Time Limit Exceeded

Solution code:

def main():
  x, y, _a, _b, _c = map(int, input().split())
  p = sorted(map(int, input().split()))[-x:]
  q = sorted(map(int, input().split()))[-y:]
  r = sorted(map(int, input().split()), reverse=True)[:x + y]

  arr = sorted(p + q)

  for nc_apple in r:
    apple = arr[0]
    if apple >= nc_apple: break

    arr[0] = nc_apple

    for i in range(len(arr) - 1):
      if arr[i] < arr[i + 1]: break

      arr[i], arr[i + 1] = arr[i + 1], arr[i]

  print(sum(arr))


main()

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int,input().split())
r=list(map(int,input().split()))
g=list(map(int,input().split()))
w=sorted(list(map(int,input().split())),reverse=True)
ans=0
R=[]
G=[]
cou=0
while(1):
    if cou==x:
        break
    M=r[0]
    memory=0
    for j in range(1,a):
        if M<r[j]:
            M=r[j]
            memory=j
    if (c>0) and (M<w[0]): 
        while(M<w[0]):
            ans+=w[0]
            cou+=1
            c-=1
            w=w[1:]
            if (c==0) or (cou==x):
                break
        if cou<x:
            ans+=M
            cou+=1
            a-=1
            r.pop(memory)
    else:
        ans+=M
        cou+=1
        a-=1
        r.pop(memory)
    
    
cou=0
while(1):
    if cou==y:
        break
    M=g[0]
    memory=0
    for j in range(1,b):
        if M<g[j]:
            M=g[j]
            memory=j
    if (c>0) and (M<w[0]): 
        while(M<w[0]):
            ans+=w[0]
            cou+=1
            c-=1
            w=w[1:]
            if (c==0) or (cou==x):
                break
        if cou<x:
            ans+=M
            cou+=1
            b-=1
            g.pop(memory)
    else:
        ans+=M
        cou+=1
        b-=1
        g.pop(memory)
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()))

p.sort()
q.sort()
r.sort()
del p[:a-x]
del q[:b-y]

s=p+q
s.sort()

i=c-1
while r[i]>s[0] and i>=0 :
    s[0]=r[i]
    s.sort()
    i -= 1

print(sum(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
class Heap:
    def __init__(self):
        self.heap = [None]*1000000
        self.root = 0
        self.size = 0
    
    def push(self, a:int):
        self.heap[self.size] = a
        b = self.size
        while b != 0 and self.heap[(b - 1) //2] < self.heap[b]:
            c = self.heap[(b - 1) //2]
            self.heap[(b - 1) //2] = self.heap[b]
            self.heap[b] = c
            b = (b - 1) //2
        self.size += 1
    
    def pop(self):
        a = self.heap[0]
        b = 0
        self.heap[b] = self.heap[self.size - 1]
        self.heap[self.size] = None
        self.size -= 1
        while (self.heap[b*2 + 1] is not None and self.heap[b] < self.heap[b*2 + 1]) or (self.heap[b*2 + 2] is not None and self.heap[b] < self.heap[b*2 + 2]):
            if self.heap[b*2 + 2] > self.heap[b*2 + 1]:
                c = self.heap[b]
                self.heap[b] = self.heap[b*2 + 2]
                self.heap[b*2 + 2] = c
                b = b*2 + 2
            else:
                c = self.heap[b]
                self.heap[b] = self.heap[b*2 + 1]
                self.heap[b*2 + 1] = c
                b = b*2 + 1
        return a
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()))
    pq = Heap()
    p.sort()
    q.sort()
    for i in range(x):
        pq.push(p[len(p) - 1 - i])
    for i in range(y):
        pq.push(q[len(q) - 1 - i])
    for i in range(len(r)):
        pq.push(r[i])
    ans = 0
    for i in range(x + y):
        ans += pq.pop()
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
class Node:
    def __init__(self,a:int):
        self.val = a
        self.parent = None
        self.right = None
        self.left = None

class Heap:
    def __init__(self):
        self.size = None
        self.node = None

    def push(self, a:int):
        c = Node(a)
        if self.size is None:
            self.size = 1
            self.node = c
        else:
            self.size += 1
            n = self.size
            l = []
            b = self.node
            c = Node(a)
            while n != 1:
                l.append(n % 2)
                n //= 2
            for i in range(len(l) - 1):
                if l[len(l) - 1 - i] == 1:
                    b = b.right
                else:
                    b = b.left
            if l[0] == 1:
                b.right = c
                c.parent = b
            else:
                b.left = c
                c.parent = b
            while c.parent is not None and c.parent.val < c.val:
                d = c.val
                c.val = c.parent.val
                c.parent.val = d
                c = c.parent

    def pop(self):
        aval = self.node.val
        a = self.node
        n = self.size
        l = []
        b = self.node
        while n != 1:
            l.append(n % 2)
            n //= 2
        for i in range(len(l)):
            if l[len(l) - 1 - i] == 1:
                b = b.right
                if len(l) - 1 - i == 0:
                    b.parent.right = None
            else:
                b = b.left
                if len(l) - 1 - i == 0:
                    b.parent.left = None
        self.size -= 1
        a.val = b.val

        while (a.right is not None and a.val < a.right.val) or (a.left is not None and a.val < a.left.val):
            if a.right is None and a.left is not None:
                c = a.val
                a.val = a.left.val
                a.left.val = c
                a = a.left
            elif a.left.val > a.right.val:
                c = a.val
                a.val = a.left.val
                a.left.val = c
                a = a.left
            else:
                c = a.val
                a.val = a.right.val
                a.right.val = c
                a = a.right
        return aval
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()))
    pq = Heap()
    p.sort()
    q.sort()
    for i in range(x):
        pq.push(p[len(p) - 1 - i])
    for i in range(y):
        pq.push(q[len(q) - 1 - i])
    for i in range(len(r)):
        pq.push(r[i])
    ans = 0
    for i in range(x + y):
        ans += pq.pop()
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
class Node:
    def __init__(self,a:int):
        self.val = a
        self.parent = None
        self.right = None
        self.left = None

class Heap:
    def __init__(self):
        self.size = None
        self.node = None

    def push(self, a:int):
        c = Node(a)
        if self.size is None:
            self.size = 1
            self.node = c
        else:
            self.size += 1
            n = self.size
            l = []
            b = self.node
            c = Node(a)
            while n != 1:
                l.append(n % 2)
                n //= 2
            for i in range(len(l) - 1):
                if l[len(l) - 1 - i] == 1:
                    b = b.right
                else:
                    b = b.left
            if l[0] == 1:
                b.right = c
                c.parent = b
            else:
                b.left = c
                c.parent = b
            while c.parent is not None and c.parent.val < c.val:
                d = c.val
                c.val = c.parent.val
                c.parent.val = d
                c = c.parent

    def pop(self):
        aval = self.node.val
        a = self.node
        n = self.size
        l = []
        b = self.node
        while n != 1:
            l.append(n % 2)
            n //= 2
        for i in range(len(l)):
            if l[len(l) - 1 - i] == 1:
                b = b.right
                if len(l) - 1 - i == 0:
                    b.parent.right = None
            else:
                b = b.left
                if len(l) - 1 - i == 0:
                    b.parent.left = None
        self.size -= 1
        a.val = b.val

        while (a.right is not None and a.val < a.right.val) or (a.left is not None and a.val < a.left.val):
            if a.right is None and a.left is not None:
                c = a.val
                a.val = a.left.val
                a.left.val = c
                a = a.left
            elif a.left.val > a.right.val:
                c = a.val
                a.val = a.left.val
                a.left.val = c
                a = a.left
            else:
                c = a.val
                a.val = a.right.val
                a.right.val = c
                a = a.right
        return aval

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()))
pq = Heap()
p.sort()
q.sort()
for i in range(x):
    pq.push(p[len(p) - 1 - i])
for i in range(y):
    pq.push(q[len(q) - 1 - i])
for i in range(len(r)):
    pq.push(r[i])
ans = 0
for i in range(x + y):
    ans += pq.pop()
print(ans)

Submission status: Time Limit Exceeded

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

def choice(li, n):
    d = []
    for i in range(n):
        m = max(li)
        d.append(m)
        li[li.index(m)] = -1
    return d

deli = []
red = choice(p, x)
green = choice(q, y)
deli.extend(red)
deli.extend(green)
while True:
    m = max(r)
    if m <= 0:
        break
    min_deli = min(deli)
    if m > min_deli:
        deli[deli.index(min_deli)] = m
        r[r.index(m)] = -1
    else:
        break

print(sum(deli))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, heappush, heappop
def greenandredapples():
    X, Y, A, B, C = list(map(int, input().split()))
    RedD = sorted(list(map(int, input().split())))
    GreenD = sorted(list(map(int, input().split())))
    Colorl = sorted(list(map(int, input().split())))

    considered = RedD[-X:] + GreenD[-Y:]
    #print(considered)
    replacements = len(Colorl)
    #print(Colorl)
    while replacements >= 0 and len(Colorl) > 0:
        heapify(considered)
        smallest = heappop(considered)
        #print("ffff", smallest)
        r = Colorl.pop(0)
        #print("gggg", r, Colorl)
        if smallest < r:
            heappush(considered, r)
        else:
            heappush(considered, smallest)
        #print("ccc", considered)
        replacements -= 1
    #print(considered)
    return sum(considered)
print(greenandredapples())

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c=[int(j) for j in input().split()]
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
r=[int(j) for j in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
l=p[:x]+q[:y]
l.sort()
ans=sum(l)
for i in l:
    if r!=[]:
        if i<r[0]:
            ans+=r[0]-i
            del r[0]
        else:
            break
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c=[int(j) for j in input().split()]
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
r=[int(j) for j in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
l=p[:x]+q[:y]
l.sort()
ans=0
for i in l:
    if r!=[]:
        if i<r[0]:
            ans+=r[0]
            del r[0]
        else:
            ans+=i
    else:
        ans+=i
print(ans)

Submission status: Time Limit Exceeded

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

p=sorted(p)
q=sorted(q)
r=sorted(r,reverse=True)

while len(p)>x:
  p.remove(p[0])

while len(q)>y:
  q.remove(q[0])
  
rr=[]

rr=[]
while p[0]<=r[0] or q[0]<=r[0]:
  rr.append(r[0])
  r.remove(r[0])
  if len(r)==0:
    r.append(0)
  if p[0]<=q[0]:
    p.remove(p[0])
    if len(p)==0:
      p.append(10**9+1)
  else:
    q.remove(q[0])
    if len(q)==0:
      q.append(10**9+1)

if p[0]==10**9+1:
  p.remove(p[0])

if q[0]==10**9+1:
  q.remove(q[0])
  
print(sum(p)+sum(q)+sum(rr))

Submission status: Time Limit Exceeded

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

sum_deli = 0
for a in range(X):
    max_deli_A = max(deli_A)
    if len(deli_C) > 0 and max_deli_A > max(deli_C):
        deli_A.remove(max_deli_A)
    elif len(deli_C) > 0:
        max_deli_A = max(deli_C)
        deli_C.remove(max_deli_A)

    sum_deli += max_deli_A

for a in range(Y):
    max_deli_B = max(deli_B)
    if len(deli_C) > 0 and max_deli_B > max(deli_C):
        deli_B.remove(max_deli_B)
    elif len(deli_C) > 0:
        max_deli_B = max(deli_C)
        deli_C.remove(max_deli_B)

    sum_deli += max_deli_B

print(sum_deli)

Submission status: Time Limit Exceeded

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

pn.sort(reverse=True)
qn.sort(reverse=True)
rn.sort(reverse=True)

pq = pn[:x] + qn[:y]
rcount = 0

while min(pq) <= rn[0]:
  pq.remove(min(pq))
  pq.append(rn[0])
  del rn[0]
  if len(rn) == 0:
    break
  
print(sum(pq))

Submission status: Time Limit Exceeded

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

pn.sort(reverse=True)
qn.sort(reverse=True)
rn.sort(reverse=True)

pn = pn[:x]
qn = qn[:y]

while min(pn[-1], qn[-1]) <= rn[0]:
  if pn[-1] >= qn[-1]:
    qn.insert(0,rn[0])
    del qn[-1]
  else:
    pn.insert(0,rn[0])
    del pn[-1]
  del rn[0]
  if len(rn) == 0:
    break
  
print(sum(pn) + sum(qn))

Submission status: Time Limit Exceeded

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

pn.sort(reverse=True)
qn.sort(reverse=True)
rn.sort(reverse=True)

pn = pn[:x]
qn = qn[:y]

while min(pn[-1], qn[-1]) <= rn[0]:
  if pn[-1] >= qn[-1]:
    qn[-1] = rn[0]
  else:
    pn[-1] = rn[0]
  del rn[0]
  if len(rn) == 0:
    break
  pn.sort(reverse=True)
  qn.sort(reverse=True)
  
print(sum(pn) + sum(qn))

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()))
n_p = []
n_q = []
for i in range(x):
    n_p.append(max(p))
    p.remove(max(p))
for i in range(y):
    n_q.append(max(q))
    q.remove(max(q))
n_r = n_p + n_q
while max(r) > min(n_r):
    n_r.remove(min(n_r))
    n_r.append(max(r))
print(sum(n_r))

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C=map(int,input().split())
p=sorted(list(map(int,input().split())))[::-1][:X]
q=sorted(list(map(int,input().split())))[::-1][:Y]
r=sorted(list(map(int,input().split())))[::-1]
pq=sorted(p+q)
for R in r:
    for i in range(len(pq)):
        if pq[i]<R:
            pq[i]=R
            break
print(sum(pq))

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = list(map(int, input().split(' ')))
p_list = sorted(list(map(int, input().split(' '))), reverse=True)
q_list = sorted(list(map(int, input().split(' '))), reverse=True)
r_list = sorted(list(map(int, input().split(' '))), reverse=True)

xy_list = sorted(p_list[:x] + q_list[:y])

for r in r_list:
    if r > xy_list[0]:
        xy_list += [r]
        del xy_list[0]
    else:
        break

print(sum(xy_list))

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = map(int, input().split())
Lista = sorted(list(map(int, input().split())), reverse = True)
Listb = sorted(list(map(int, input().split())), reverse = True)
Listc = sorted(list(map(int, input().split())), reverse = True)
if x < a:
    del Lista[x:a]
if y < b:
    del Listb[y:b]
List = sorted(Lista + Listb, reverse = True)
while len(Listc) > 0:
    if List[-1] < Listc[0]:
        List[-1] = Listc[0]
        List = sorted(List, reverse = True)
        del Listc[0]
    else:
        break
print(sum(List))

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = map(int, input().split())
Lista = sorted(list(map(int, input().split())), reverse = True)
Listb = sorted(list(map(int, input().split())), reverse = True)
Listc = sorted(list(map(int, input().split())), reverse = True)
if x < a:
    del Lista[x:a]
if y < b:
    del Listb[y:b]
while len(Listc) > 0:
    if Lista[-1] < Listb[-1]:
        if Lista[-1] < Listc[0]:
            Lista[-1] = Listc[0]
            Lista = sorted(Lista, reverse = True)
            del Listc[0]
        else:
            break
    else:
        if Listb[-1] < Listc[0]:
            Listb[-1] = Listc[0]
            Listb = sorted(Listb, reverse = True)
            del Listc[0]
        else:
            break
print(sum(Lista) + sum(Listb))

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int, input().split())
p1 = list(map(int, input().split()))
p2 = list(map(int, input().split()))
p3 = list(map(int, input().split()))
ans1 = 0
ans2 = 0
p1.sort(reverse = True)
p2.sort(reverse = True)
p3.sort(reverse = True)
for i in range(0, max(x,y)):
  p3.append(0)
for i in range(0,x):
  p3.sort(reverse = True)
  ans1= ans1 + max(p1[i],p3[0])
  if(p3[0]>p1[i]):
    p3[0] = 0
for i in range(0,y):
  p3.sort(reverse = True)
  ans2= ans2 + max(p2[i],p3[0])
  if(p3[0]>p2[i]):
    p3[0] = 0
print(ans1+ans2)

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

eats = sorted(p, reverse=True)[:x] + sorted(q, reverse=True)[:y]
while r and max(r) >= min(eats):
    eats[eats.index(min(eats))] = max(r)
    r.remove(max(r))

print(sum(eats))

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

# まずは赤と緑の中だけで最大となるようX+Y個のりんごを選ぶ．
p_x = sorted(p, reverse=True)[:x].copy()
q_y = sorted(q, reverse=True)[:y].copy()
eats = p_x + q_y
i, j = 0, 0
while r and max(r) >= min(eats):
    mi = min(eats)
    eats[eats.index(mi)] = max(r)
    r.remove(max(r))

print(sum(eats))

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = [int(i) for i in input().split()]

def input_list(N):
  list = []
  for i in input().split():
    list.append(int(i))
  list.sort(reverse=True)
  return list

P = input_list(A)
Q = input_list(B)
R = input_list(C)

def delete_last(list,X):
  for i in range(X,len(list)):
    list.pop(X)

delete_last(P,X)
delete_last(Q,Y)


Top = P + Q + R
Top.sort(reverse=True)
sum = 0
for i in range(X+Y):
  sum += Top[i]
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import heapq

X, Y, A, B, C= list(map(int, input().split()))
P = list(map(int, input().split()))
P.sort(reverse=True)
P = P[:X]
Q = list(map(int, input().split()))
Q.sort(reverse=True)
Q = Q[:Y]
R = list(map(int, input().split()))
R.sort(reverse=True)
colored = []
for r in R:
    p_min = P[-1]
    q_min = Q[-1]
    if p_min <= q_min and p_min < r:
        P.pop()
        P.insert(0, r)
    elif q_min <= p_min and q_min < r:
        Q.pop()
        Q.insert(0, r)
    else:
        break
print(sum(P) + sum(Q))

Submission status: Time Limit Exceeded

Solution code:
#n,x,y=map(int,input())
# Queue

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=p[a-x:]
q=q[b-y:]
r.sort()
for ri in r:
    if ri > p[0] and q[0] >= p[0]:
        p[0] = ri
        p.sort()
        continue
    if ri > q[0] and p[0] >= q[0]:
        q[0]=ri
        q.sort()
print(sum(p)+sum(q))

"""
1 2 2 2 1
2 4
5 1
3
->12
2 2 2 2 2
8 6
9 1
2 1
->25
"""

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)
S = sorted(p[:X] + q[:Y] + r[:X+Y], reverse = True)
i = 0
ans = 0
a = 0
b = 0
c = 0
while i < (X + Y):
  if S[0] == r[0]:
    c += 1
    r.remove(r[0])
    ans += S[0]
    i += 1
  elif S[0] == p[0] and a < X:
    a += 1
    p.remove(p[0])
    ans += S[0]
    i += 1
  elif S[0] == q[0] and b < Y:
    b += 1
    q.remove(q[0])
    ans += S[0]
    i += 1
  S.remove(S[0])
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())), reverse = True)
q = sorted(list(map(int, input().split())), reverse = True)
r = sorted(list(map(int, input().split())), reverse = True)
s = p[:X] + q[:Y] + r[:X+Y]
S = sorted(s, reverse = True)
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 = [int(i) for i in input().rstrip().split()]
P = sorted([int(i) for i in input().rstrip().split()],reverse=True)
Q = sorted([int(i) for i in input().rstrip().split()],reverse=True)
R = sorted([int(i) for i in input().rstrip().split()])
P = P[:X]
Q = Q[:Y]
A = sorted(P+Q)
R = [r for r in R if r > min(A)]
i = 0
for r in R:
    if A[i] >= r:
        i -= 1
    A[i] = r
    i += 1
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import heapq
def main():
    x, y, a, b, c = map(int,input().split())
    p_ins = list(map(int,input().split()))
    q_ins = list(map(int,input().split()))
    r_ins = list(map(int,input().split()))
    p = select(p_ins, x)
    q = select(q_ins, y)
    r = select(r_ins, min(c, x + y))
    res = 0
    for _ in range(x + y):
        if p[-1] <= r[-1] and q[-1] <= r[-1]:
            t = r.pop()
            res += t
        elif p[-1] <= q[-1]:
            t = q.pop()
            res += t
        else:
            t = p.pop()
            res += t
    print(res)

def select(array, length):
    ins = []
    res_l = []
    for part in array:
        heapq.heappush(ins,-part)
    for _ in range(length):
        res = heapq.heappop(ins)
        res_l = [-res] + res_l
    return [0] + res_l


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int, input().split())
del_A = list(map(int, input().split()))
del_B = list(map(int, input().split()))
del_C = list(map(int, input().split()))
del_max = 0
NA = len(del_A)-X
NB = len(del_B)-Y

for i in range(NA):
    del_A.remove(min(del_A))

for i in range(NB):
    del_B.remove(min(del_B))

del_all = del_A + del_B + del_C

XY = X+Y

for i in range(XY):
    del_max += max(del_all)
    del_all.remove(max(del_all))

print(del_max)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int, input().split())
del_A = list(map(int, input().split()))
del_B = list(map(int, input().split()))
del_C = list(map(int, input().split()))
del_max = 0

for i in range(len(del_A)-X):
    del_A.remove(min(del_A))

for i in range(len(del_B)-Y):
    del_B.remove(min(del_B))

del_all = del_A + del_B + del_C

for i in range(X+Y):
    del_max += max(del_all)
    del_all.remove(max(del_all))

print(del_max)