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: Runtime Error

Solution code:
red_eat_num, green_eat_num, red_total, green_total, no_total = map(int, input().split())

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

red = sorted(red, reverse=True)
green = sorted(green, reverse=True)
no= sorted(no, reverse=True)

count_x = 0
count_y = 0
pointaA = 0
pointaB = 0
pointaC = 0
total_red = 0
total_green = 0
flag_red = 0
flag_green = 0

while 1:
    count_x = count_x + 1
    if count_x > red_eat_num:
        break
    
    if flag_red == 0: 
        if red[pointaA] >= no[pointaC]:
            total_red = total_red + red[pointaA]
            pointaA = pointaA + 1
            if pointaA >= red_total:
                flag_red = 1
        else:
            total_red = total_red + no[pointaC]
            pointaC = pointaC + 1
            if pointaA >= no_total:
                flag_red = 2
    elif flag_red == 1:
        total_red = total_red + no[pointaC]
        pointaC = pointaC + 1
    else:
        total_red = total_red + red[pointaA]
        pointaA = pointaA + 1
# print(total_red)

if flag_red == 2:
    flag_green == 2

while 1:
    count_y = count_y + 1
    if count_y > green_eat_num:
        break
    
    if flag_green == 0:
        if green[pointaB] >= no[pointaC]:
            total_green = total_green + green[pointaB]
            pointaB = pointaB + 1
            if pointaB >= green_total:
                flag_green = 1
        else:
            total_green = total_green + no[pointaC]
            pointaC = pointaC + 1
            if pointaC >= no_total:
                flag_green = 2
    elif flag_red == 1:
        total_green = total_green + no[pointaC]
        pointaC = pointaC + 1
    else:
        total_green = total_green + green[pointaB]
        pointaB = pointaB + 1
# print(total_green)
        
print(total_red + total_green)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int,input().split()) 
P = [list(map(int,input().split())) for i in range(3)]
Red = sorted(P[0], reverse=True)
Green = sorted(P[1], reverse=True)
Trans = sorted(P[2], reverse=True)

pre_max = sum(Red[:X]) + sum(Green[:Y])
Red = Red[:X]
Green = Green[:Y]
for i in range (C):
  maxo = pre_max
  if (Trans[i] > Red[-1+i]):
    maxo= pre_max + Trans[i] - Red[-1+i]
  elif (Trans[i] > Green[-1+i]):
    maxo = pre_max + Trans[i] - Green[-1+i]
    
print(maxo)

Submission status: Runtime Error

Solution code:
import sys
#input = sys.stdin.readline
#sys.setrecursionlimit(10**6)

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)

    p = p[:x]
    q = q[:y]
    p.sort()
    q.sort()
    for ir in range(c):
        if p[ir] < q[ir]:
            if p[ir] < r[ir]:
                p[ir] = r[ir]
        else:
            if q[ir] < r[ir]:
                q[ir] = r[ir]

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	x, y, a, b, c = map(int, sys.stdin.readline().split())
	p = list(map(int, sys.stdin.readline().split()))
	q = list(map(int, sys.stdin.readline().split()))
	r = list(map(int, sys.stdin.readline().split()))
	allap = []
	for i in range(a):
		allap.append(("r", p[i]))
	for i in range(b):
		allap.append(("g", q[i]))
	for i in range(c):
		allap.append(("n", r[i]))
	allap.sort(key=lambda x:x[1], reverse=True)
	log.print(allap)
	deli = 0
	ap = []
	aq = []
	xcnt = x
	ycnt = y
	for i in range(len(allap)):
		if allap[i][0] == "r" and xcnt > 0:
			ap.append(allap[i])
			xcnt -= 1
		if allap[i][0] == "g" and ycnt > 0:
			aq.append(allap[i])
			ycnt -= 1
	xcnt = x
	ycnt = y
	for i in range(len(allap)):
		log.print(allap[i], xcnt, ycnt, deli)
		if allap[i][0] == "r" and xcnt > 0:
			deli += allap[i][1]
			xcnt -= 1
		if allap[i][0] == "g" and ycnt > 0:
			deli += allap[i][1]
			ycnt -= 1
		if allap[i][0] == "n":
			if ap[-1][1] < aq[-1][1] and xcnt > 0:
				deli += allap[i][1]
				ap.pop()
				xcnt -= 1
			if ap[-1][1] >= aq[-1][1] and ycnt > 0:
				deli += allap[i][1]
				aq.pop()
				ycnt -= 1
	print(deli)
	


main()

Submission status: Runtime Error

Solution code:
#コンマで区切る
A_list = input().split(' ')
    
#コンマで区切る
p_list = input().split(' ')
for i in range(len(p_list)):
    p_list[i] = int(p_list[i])
p_list = p_list.sort()
#コンマで区切る
q_list = input().split(' ')
for i in range(len(q_list)):
    q_list[i] = int(q_list[i])
q_list = p_list.sort()
#コンマで区切る
r_list = input().split(' ')
for i in range(len(r_list)):
    r_list[i] = int(r_list[i])
r_list = r_list.sort()    
    
ans_list = []
#無色を考えない
del p_list[X:]
del q_list[Y:]

ans_list = p_list + p_list
for i in range(len(ans_list)):
  if(ans_list[i] < max(r_list)):
    ans_list[i] = max(r_list)
    r_list.remove(max(r_list))
    
print(sum(ans_list))

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
R = sorted(list(map(int, input().split())), reverse=True)
G = sorted(list(map(int, input().split())), reverse=True)
M = sorted(list(map(int, input().split())), reverse=True)

ans, r, g, m = 0, 0, 0, 0

for i in range(x + y):
    if r >= x:
        if m >= c:
            ans += G[g]
            g += 1
        elif G[g] >= M[m]:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
    elif g >= y:
        if m >= c:
            ans += R[r]
            r += 1
        elif R[r] >= M[m]:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] >= G[g]:
        if m >= c:
            ans += R[r]
            r += 1
        elif R[r] >= M[m]:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] < G[g] or r >= x:
        if m >= c:
            ans += G[g]
            g += 1
        if G[g] >= M[m]:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
x,y,a,b,c = map(int,input().split())
red = [int(i) for i in input().split()]
gw = [int(i) for i in input().split()] + [int(i) for i in input().split()]
red.sort()
gw.sort()
gwrui = []
rui = 0
for i in range(b+c):
    rui += gw[i]
    gwrui.append(rui)
redq = deque(red[:x])
ans = 0
oisr = 0
for i in range(1,x+1):
    oisr += redq.popleft()
    temp = oisr
    temp += gwrui[x+y-i]
    ans = max(ans,temp)
print(ans)

Submission status: Runtime Error

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]

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

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

Submission status: Runtime Error

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]
if len(r)>X+Y:
    del r[:-max(X+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: Runtime Error

Solution code:
from collections import deque

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

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

r = deque(r)
deque_p = deque(p[:X])
deque_q = deque(q[:Y])
deque_r = deque([])

if r:
    while min(deque_p[-1], deque_q[-1]) < r[0]:
        if deque_p[-1] <= deque_q[-1]:
            deque_p.pop()
            deque_r.append(r.popleft())
        else:
            deque_q.pop()
            deque_r.append(r.popleft())

        if not r:
            break

ans = sum(deque_p) + sum(deque_q) + sum(deque_r)

print(ans)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
al = sorted(list(map(int, input().split())), reverse=True)[:x]
bl = sorted(list(map(int, input().split())), reverse=True)[:y]
cl = sorted(list(map(int, input().split())), reverse=True)
ans = 0
xi, yi = 0, 0
for i in cl:
    if al:
        ai = al[-1]
    else:
        ai = 0
    if bl:
        bi = bl[-1]
    else:
        bi = 0
    if ai < bi:
        m = al.pop()
    else:
        m = bl.pop()
    if m < i:
        ans += i
    else:
        ans += m
        break
ans += sum(al)+sum(bl)
print(ans)

Submission status: Runtime Error

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

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

it = min(x,y)

ait = min(it, len(r))
sum = 0

for i in range(x):
    sum = sum + p[-i-1]
for i in range(y):
    sum = sum + q[-i-1]
for i in range(ait):
    c1 = r[-i-1]-p[i]
    c2 = r[-i-1]-q[i]
    if c1 > 0 or c2 > 0:
        if c1>=c2:
            sum = sum+c1
        else:
            sum = sum+c2

if ait < len(r):
    ret = min(len(r),max(x,y))
    if x > y:
        for i in range(ret):
            if r[-ait-1-i] > p[ait+i]:
                sum = sum+r[-ait-1-i]-p[ait+i]
    else:
        for i in range(ret):
            if r[-ait-1-i] > q[ait+i]:
                sum = sum+r[-ait-1-i]-q[ait+i]
print(sum)

Submission status: Runtime Error

Solution code:

import sys
# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())

def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())

# 入力全てを整数に変換したものの配列を受け取る
# def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def LI(): return list(map(lambda x:int(x)*(-1), sys.stdin.readline().split()))
# from heapq import heappop,heappush,heapify
import heapq
x,y,a,b,c = MI()
p = LI()
q = LI()
r = LI()

result = 0
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
used_c = []
used_a = []


for _ in range(x):
	if not r==[]:
		max_a = heapq.heappop(p)*(-1)
		max_c = heapq.heappop(r)*(-1)
		print(max_a,max_c,'a')
		if max_a>=max_c:
			result += max_a
			a -= 1
			heapq.heappush(r,max_c*(-1))
			heapq.heappush(used_a,max_a)
		else:
			result += max_c
			c -= 1
			heapq.heappush(p,max_a*(-1))
			heapq.heappush(used_c,max_c)
	else:
		max_a = heapq.heappop(p)*(-1)
		result += max_a
		heapq.heappush(used_a,max_a)
		a -= 1

for _ in range(y):
	if not r==[]:
		max_b = heapq.heappop(q)*(-1)
		max_c = heapq.heappop(r)*(-1)
		if max_b>=max_c:
			result += max_b
			b -= 1
			heapq.heappush(r,max_c*(-1))
		else:
			result += max_c
			c -= 1
			heapq.heappush(q,max_b*(-1))
			heapq.heappush(used_c,max_c)
	else:
		max_b = heapq.heappop(q)*(-1)
		result += max_b
		b -= 1

if not used_c==[] and p==[]:
	while(1):
		max_c = heapq.heappop(used_c)
		max_a = heapq.heappop(p)*(-1)
		if max_a>max_c:
			result += max_a
			result -= max_c
		else:
			break
	if used_c==[] or p==[]:
		break

print(result)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

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

p.append(0)
q.append(0)
r.append(0)
p.sort()
q.sort()
r.sort()

ans = 0

while True:
    if X > 0:
        if len(p) == 0:
            ans += r.pop()
            X -= 1
        elif len(r) == 0:
            ans += p.pop()
            X -= 1
    if Y > 0:
        if len(q) == 0:
            ans += r.pop()
            Y -= 1
        elif len(r) == 0:
            ans += q.pop()
            Y -= 1
    if X > 0 and p[-1] >= r[-1]:
        ans += p.pop()
        X -= 1
    if X > 0 and Y == 0 and p[-1] < r[-1]:
        ans += r.pop()
        X -= 1
    if Y > 0 and q[-1] >= r[-1]:
        ans += q.pop()
        Y -= 1
    if Y > 0 and X == 0 and q[-1] < r[-1]:
        ans += r.pop()
        Y -= 1
    if 0 < len(r):
        if p[-1] <= r[-1] or q[-1] <= r[-1]: 
            if p[-X] >= q[-Y] and X > 0:
                ans += r.pop()
                Y -= 1
            if p[-X] < q[-Y] and Y > 0:
                ans += r.pop()
                X -= 1
    if X <= 0 and Y <= 0:
        break
print(ans)

Submission status: Runtime Error

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

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right
  
ps=quick_sort(p)[-x:]
qs=quick_sort(q)[-y:]
rs=quick_sort(r)

cnt=sum(ps)+sum(qs)
for i in range(10**5):
  if min(ps+qs)<rs[-1]:
    cnt=cnt-min(ps+qs)+rs[-1]
    rs=rs[:-1]
  else:
    break
print(cnt)

Submission status: Runtime Error

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

from collections import deque

X,Y,A,B,C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

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

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

P_index = -1
Q_index = -1
R_index = 0

while True:
  if P[P_index] <= Q[Q_index] and R[R_index]>P[P_index]:
    P[P_index] = R[R_index]
    P_index -= 1
    R_index += 1
  elif P[P_index] > Q[Q_index] and R[R_index]>Q[Q_index]:
    Q[Q_index] = R[R_index]
    Q_index -= 1
    R_index += 1
  else:
    break
  if R_index>=len(R):
    break

print(sum(P)+sum(Q))

Submission status: Runtime Error

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

import heapq

p = list(map(lambda x: x * -1, p))
q = list(map(lambda x: x * -1, q))
r = list(map(lambda x: x * -1, r))
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)

P = []
Q = []
for _ in range(X):
    P.append(heapq.heappop(p)*-1)
for _ in range(Y):
    Q.append(heapq.heappop(q)*-1)

a = 1
b = 1

total = 0
now = heapq.heappop(r)*-1
while now > P[-a] or now > Q[-b]:
    if P[-a] > Q[-b]:
        total += now - Q[-b]
        b += 1
    if Q[-b] > P[-a]:
        total += now - P[-a]
        a += 1

    if len(r) == 0 or len(P) < a or len(Q) < b:
        break
    now = heapq.heappop(r)*-1

print(total + sum(P) + sum(Q))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import heapq


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = sorted(p)
    q = sorted(q)
    r = sorted(r)

    result = 0

    X_count = 0
    Y_count = 0
    while True:
        if len(p)>0 and p[-1] > r[-1] and p[-1] > q[-1] and X_count < X:
            result += p.pop()
            X_count += 1
        elif len(q)>0 and q[-1] > r[-1] and q[-1] > p[-1] and Y_count < Y:
            result += q.pop()
            Y_count += 1
        elif len(r)>0 and X_count < X:
            result += r.pop()
            X_count += 1
        elif len(r)>0 and Y_count < Y:
            result += r.pop()
            Y_count += 1
        if X_count == X:
            p = [-100]
        if Y_count == Y:
            q = [-100]
        if X_count == X and Y_count == Y:
            break
    print(result)
    return


def solveold(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = sorted(p)
    q = sorted(q)
    r = sorted(r)

    result = 0


    for i in range(X):
        if p[-1] < r[-1]:
            result += r.pop()
        else:
            result += p.pop()

    for i in range(Y):
        if q[-1] < r[-1]:
            result += r.pop()
        else:
            result += q.pop()
    print(result)
    return

def solve_slow(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = [-1 * i for i in p]
    q = [-1 * i for i in q]
    r = [-1 * i for i in r]
    heapq.heapify(p)
    heapq.heapify(q)
    heapq.heapify(r)

    result = 0
    for i in range(X):
        smallest_p = heapq.nsmallest(1, p)
        smallest_r = heapq.nsmallest(1, r)
        if smallest_p < smallest_r:
            temp = heapq.heappop(p) * -1
            result += temp
        else:
            temp = heapq.heappop(r) * -1
            result += temp

    for i in range(Y):
        smallest_q = heapq.nsmallest(1, q)
        smallest_r = heapq.nsmallest(1, r)
        if smallest_q < smallest_r:
            temp = heapq.heappop(q) * -1
            result += temp
        else:
            temp = heapq.heappop(r) * -1
            result += temp
    #    print(temp)
    print(result)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
R = sorted(list(map(int, input().split())), reverse=True)
G = sorted(list(map(int, input().split())), reverse=True)
M = sorted(list(map(int, input().split())), reverse=True)

ans, r, g, m = 0, 0, 0, 0
mr = R[x - 1]
mg = G[y - 1]

for i in range(x + y):
    if r >= x:
        if m >= c:
            ans += G[g]
            g += 1
        elif G[g] > M[m]:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
    elif g >= y:
        if m >= c:
            ans += R[r]
            r += 1
        elif R[r] > M[m]:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] >= G[g]:
        if m >= c:
            ans += R[r]
            r += 1
        elif R[r] > M[m]:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] < G[g] or r >= x:
        if m >= c:
            ans += G[g]
            g += 1
        if G[g] > M[m]:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    X, Y, A, B, C = [int(i) for i in input().split()]
    red = [int(i) for i in input().split()]
    blue = [int(i) for i in input().split()]
    noc = [int(i) for i in input().split()]
    red = sorted(red)[-X:]
    blue = sorted(blue)[-Y:]
    rb = sorted(red + blue)
    score = sum(rb)
    noc = sorted(noc)[::-1]

    s_noc = 0
    s_rb = 0
    for i in range(min(A+B, C)):
        if rb[i] > noc[i]:
            break
        s_noc += noc[i]
        s_rb += rb[i]

    print(score + s_noc - s_rb)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import heapq


X,Y,A,B,C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

r = []
g = []
nc = []
for p in P:
    heapq.heappush(r, -p)

for q in Q:
    heapq.heappush(g, -q)

for i in R:
    heapq.heappush(nc, -i)

ans = 0
#print(r,g,nc)
while X > 0 or Y > 0:


    if X > 0 and Y > 0:
        if X > 0 and r[0] < nc[0]:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1

        elif Y > 0 and g[0] < nc[0]:
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1
        else:
            if abs(nc[0] - r[0]) >= abs(nc[0] - g[0]):
                ans += -1 * nc[0]
                heapq.heappop(nc)
                X -= 1
            else:
                ans += -1 * nc[0]
                heapq.heappop(nc)
                Y -= 1
        
    elif X > 0:
        if r[0] < nc[0]:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1
        else:
            ans += -1 * nc[0]
            heapq.heappop(nc)
            X -= 1
    else:
        if g[0] < nc[0]:
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1
        else:
            ans += -1 * nc[0]
            heapq.heappop(nc)
            Y -= 1

    #print("--", r,g,nc,X,Y)
            

print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int,input().split())
P = [int(_) for _ in input().split()]
Q = [int(_) for _ in input().split()]
R = [int(_) for _ in input().split()]

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

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

i = 0
j = 0

while min(P[i],Q[j])<R[i+j]:
    if P[i] < Q[j]:
        i += 1
        if i+j == C:
            break
        if i == X-1:
            if Q[j] > R[i+j]:
                break
    else:
        j += 1
        if i+j == C:
            break
        if j == Y-1:
            if P[i] > R[i+j]:
                break
    if i+j == C:
        break

ans = 0
for e in P[i:]:
    ans = ans + e
for e in Q[j:]:
    ans = ans + e
for a in range(i+j):
    ans = ans + R[a]

print(ans)

Submission status: Runtime Error

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

pl = list(map(int,input().split()))
ql = list(map(int,input().split()))
rl = list(map(int,input().split()))

pl = sorted(pl)
ql = sorted(ql)
rl = sorted(rl)
import itertools
pl_ac = [0] + list(itertools.accumulate(pl))
ql_ac = [0] + list(itertools.accumulate(ql))
rl_ac = [0] + list(itertools.accumulate(rl))

dada = max(ql, pl, rl)

import bisect
ans = 0
#　赤色をi個食べるとすると無職x-i
for i in range(x+1):
    tmp = 0
    if i > a or x - i > c or b + c - (x - i) < y:
        continue
    #あか
    tmp += pl_ac[-1] - pl_ac[-1-i]
    # #無職
    # tmp += rl_ac[-1] - ql_ac[-1-(x-i)]
    #残りの無職
    if x-i == 0:
        rl_c = rl[:] 
    else:
        rl_c = rl[:-(x-i)]
    # ub以上のりんご何個ある？
    ub = dada + 1

    lb = -1
    while True:
        if ub == lb:
            target = ub
            break
        target = (ub + lb) // 2 + 1
        idx = bisect.bisect_left(ql, target)
        idx2 = bisect.bisect_left(rl_c, target)
        if (b - idx) + (c - (x - i) - idx2) >= y:
            lb = target
        else:
            ub = target - 1
    idx = bisect.bisect_left(ql, target)
    idx2 = bisect.bisect_left(rl_c, target)
    tmp += ql_ac[-1] - ql_ac[idx]
    tmp += rl_ac[-1] - rl_ac[idx2]
    gg = (b - idx) + (c - (x - i) - idx2) - y
    ans -= target * gg
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

x, y, a, b, c = map(int, input().split())
R = sorted(list(map(int, input().split())))[a - x:]
G = sorted(list(map(int, input().split())))[b - y:]
M = deque(sorted(list(map(int, input().split())), reverse=True))

ans = sum(R) + sum(G)
r, g, m = 0, 0, 0
for i in range(x + y):
    if not M:
        break
    if r >= x:
        if G[g] < M[m]:
        ans = ans - G[g] + M[m]
        g += 1
        m += 1
    elif g >= y:
        if R[r] < M[m]:
            ans = ans - R[r] + M[m]
        r += 1
        m += 1
    else:
        if

x, y, a, b, c = map(int, input().split())
R = sorted(list(map(int, input().split())), reverse=True)
G = sorted(list(map(int, input().split())), reverse=True)
M = sorted(list(map(int, input().split())), reverse=True)

ans, r, g, m = 0, 0, 0, 0
mr = R[x - 1]
mg = G[y - 1]

for i in range(x + y):
    if r >= x:
        if m >= c:
            ans += G[g]
            g += 1
        elif G[g] > M[m]:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
    elif g >= y:
        if m >= c:
            ans += R[r]
            r += 1
        elif R[r] > M[m]:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] >= G[g]:
        if m >= c:
            ans += R[r]
            r += 1
        elif R[r] > M[m]:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] < G[g] or r >= x:
        if m >= c:
            ans += G[g]
            g += 1
        if G[g] > M[m]:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
print(ans)

Submission status: Runtime Error

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

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right
  
ps=quick_sort(p)[-x:]
qs=quick_sort(q)[-y:]
rs=quick_sort(r)

cnt=sum(ps)+sum(qs)
for i in range(10**5):
  if min(ps+qs)<rs[-1]:
    cnt=cnt-min(ps+qs)+rs[-1]
    rs=rs[:-1]
  else:
    break
print(cnt)

Submission status: Runtime Error

Solution code:
import heapq
x,y,a,b,c=map(int,input().split())
def _int(x):
    return -int(x)
p=list(map(_int,input().split()))
q=list(map(_int,input().split()))
r=list(map(_int,input().split()))
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
ans=0
for i in range(x):
    if len(r)==0:
        _p=heapq.heappop(p)
        ans-=_p
        continue
    _p,_r=heapq.heappop(p),heapq.heappop(r)
    if _p<_r:
        ans-=_p
        heapq.heappush(r,_r)
    else:
        ans-=_r
        heapq.heappush(p,_p)
for i in range(y):
    if len(r)==0:
        _q=heapq.heappop(q)
        ans-=_q
    _q,_r=heapq.heappop(q),heapq.heappop(r)
    if _q<_r:
        ans-=_q
        heapq.heappush(r,_r)
    else:
        ans-=_r
        heapq.heappush(q,_q)
print(ans)

Submission status: Runtime Error

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

result = 0
leftX, leftY = x, y
p.sort()
q.sort()
r.sort()
while leftX != 0 and r[-1] <= p[-1]:
    result += p.pop()
    leftX -= 1
while leftY != 0 and r[-1] <= q[-1]:
    result += q.pop()
    leftY -= 1
while leftX != 0 or leftY != 0:
    if leftX != 0:
        if len(p) != 0 and p[-1] > r[-1]:
            result += p.pop()
        else:
            result += r.pop()
        leftX -= 1
    if leftY != 0:
        if len(q) != 0 and q[-1] > r[-1]:
            result += q.pop()
        else:
            result += r.pop()
        leftY -= 1

print(result)

Submission status: Runtime Error

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product

s = input().split()
t = [int(p) for p in s]

sA = input().split()
p = [int(w) for w in sA]
p.sort(reverse=True) #大きい順に並べ替える


sB = input().split()
q = [int(p) for p in sB]
q.sort(reverse=True)

sC = input().split()
r = [int(p) for p in sC]
r.sort(reverse=True)

X = t[0]
Y = t[1]
A = t[2]
B = t[3]
C = t[4]
p = p[:X] #下の方はいらない子
q = q[:Y] #下の方はいらない子
r = r[:X+Y] #下の方はいらない子だけど、全部無色の可能性もある。
#print('{} {} {} {} {}'.format(X,Y,A,B,C))
#print(p)
#print(q)
#print(r)

def delete_min(p, q, r):
    if(p[-1] <= q[-1] and p[-1] <= r[-1]):
        del p[-1]
    elif(q[-1] <= r[-1]):
        del q[-1]
    else:
        del r[-1]

while len(p) + len(q) + len(r) > X + Y:
    delete_min(p, q, r)
    #print('p:{}, q:{}, r:{}'.format(p,q,r))

answer = sum(p) + sum(q) + sum(r)
print(answer)