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: Wrong Answer

Solution code:
from heapq import heappush, heappop


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

    P = []
    Q = []
    R = []
    for v in input().split():
        heappush(P, -int(v))
    for v in input().split():
        heappush(Q, -int(v))
    for v in input().split():
        heappush(R, -int(v))

    score = 0
    cp = 0
    cq = 0
    cr = 0

    XY = X + Y

    while len(P) > 0 and len(Q) > 0 and len(R) > 0 and cp < X and cq < Y and cp + cq + cr < XY:
        p = -P[0]
        q = -Q[0]
        r = -R[0]
        if p < q:
            if p > r:
                score -= heappop(P)
                cp += 1
            else:
                score -= heappop(R)
                cr += 1
        else:
            if q > r:
                score -= heappop(Q)
                cq += 1
            else:
                score -= heappop(R)
                cr += 1

    while len(P) > 0 and len(R) > 0 and cp < X and cp + cq + cr < XY:
        p = -P[0]
        r = -R[0]
        if p >= r:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(R)
            cr += 1

    while len(Q) > 0 and len(R) > 0 and cq < Y and cp + cq + cr < XY:
        q = -Q[0]
        r = -R[0]
        if q >= r:
            score -= heappop(Q)
            cq += 1
        else:
            score -= heappop(R)
            cr += 1

    while cp < X and cp + cq + cr < XY:
        if len(P) > 0:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(R)
            cr += 1

    while cq < Y and cp + cq + cr < XY:
        if len(Q) > 0:
            score -= heappop(Q)
            cq += 1
        else:
            score -= heappop(R)
            cr += 1

    print(score)


main()

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = [int(i) for i in input().split()]
pa = [int(i) for i in input().split()]
pb = [int(i) for i in input().split()]
pc = [int(i) for i in input().split()]

pa = sorted(pa, reverse=True)
pa = pa[:x]
pb = sorted(pb, reverse=True)
pb = pb[:y]
pc = sorted(pc)

l = pa + pb
l = sorted(l)

if l[0] < pc[-1]:
  for i in range(len(l)):
    for j in range(len(pc)):
      if l[i] < pc[j]:
        l[i] = pc[j]
    if l[i] >= pc[-1]:
      break
      
print(sum(l))

Submission status: Wrong Answer

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

a_list = list(map(int, input().split()))
a_list.sort()
b_list = list(map(int, input().split()))
b_list.sort()
c_list = list(map(int, input().split()))
c_list.sort()

a_index = a-x
b_index = b-y
for c_data in c_list:
    if a_index < a and (b_index >= b or a_list[a_index] < b_list[b_index]):
        if(a_list[a_index] < c_data):
            a_list[a_index] = c_data
            a_index += 1
    elif b_index < b:
        if(b_list[b_index] < c_data):
            b_list[b_index] = c_data
            b_index += 1
    else:
        break

count = 0
for i in range(a-1, a-x-1, -1):
    count += a_list[i]
for i in range(b-1, b-y-1, -1):
    count += b_list[i]
print(count)

Submission status: Wrong Answer

Solution code:
def main():
    x,y,a,b,c = map(int,input().split())
    red = list(map(int,input().split()))
    green = list(map(int,input().split()))
    colorless = list(map(int,input().split()))

    red.sort()
    green.sort()
    colorless.sort()

    total = 0
    while x > 0:
        if (not red) or (not colorless):
            break
        
        if red[-1] >= colorless[-1]:
            total += red.pop()
        else:
            total += colorless.pop()

        x -= 1

    while y > 0:
        if (not green) or (not colorless):
            break
        
        if green[-1] >= colorless[-1]:
            total += green.pop()
        else:
            total += colorless.pop()

        y -= 1

    while x > 0:
        if red:
            total += red.pop()
        elif colorless:
            total += colorless.pop()
        else:
            break

        x -= 1

    while y > 0:
        if green:
            total += green.pop()
        elif colorless:
            total += colorless.pop()
        else:
            break

        y -= 1

    print(total)

main()

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = [int(e) for e in input().split()]
p = [int(e) for e in input().split()]
q = [int(e) for e in input().split()]
r = [int(e) for e in input().split()]
p.sort()
p.reverse()
q.sort()
q.reverse()
r.sort()
r.reverse()
sp = 0
sq = 0
for i in range(0,x):
    sp += p[i]
for i in range(0,y):
    sq += q[i]
ma = sp + sq
index = 0
while x > 0 and y > 0 and index < c:
    if p[x-1] < q[y-1]:
        if r[index] > p[x-1]:
            ma += r[index]-p[x-1]
            index += 1
            x -= 1
        else:
            break
    else:
        if r[index] > q[y-1]:
            ma += r[index]-q[y-1]
            index += 1
            y -= 1
        else:
            break
print(ma)

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().strip().split())
p = list(map(int, input().strip().split()))
q = list(map(int, input().strip().split()))
r = list(map(int, input().strip().split()))
p.sort()
q.sort()
r.sort()
sum = 0
for i in range(X):
    if len(r) > 0:
        if r[-1] > p[-1]:
            sum += r[-1]
            del r[-1]
        else:
            sum += p[-1]
            del p[-1]
    else:
        sum += p[-1]
        del p[-1]
for i in range(Y):
    if len(r) > 0:
        if r[-1] > q[-1]:
            sum += r[-1]
            del r[-1]
        else:
            sum += q[-1]
            del q[-1]
    else:
        sum += q[-1]
        del q[-1]
print(sum)

Submission status: Wrong Answer

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 numpy as np
p = np.sort(p)[-x:]
q = np.sort(q)[-y:]
r = np.sort(r)

pq = np.sort(np.r_[p, q])
f = True
i = 1
while True:
    f = (r[-i:] > pq[:i]).all()
    if not f:
        break
    else:
        i += 1

i -= 1
if i:
    print(np.r_[r[-i:], pq[i:]].sum())
else:
    print(pq.sum())

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop


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

    P = []
    Q = []
    R = []
    for v in input().split():
        heappush(P, -int(v))
    for v in input().split():
        heappush(Q, -int(v))
    for v in input().split():
        heappush(R, -int(v))

    score = 0
    cp = 0
    cq = 0
    cr = 0

    XY = X + Y

    while len(P) > 0 and len(Q) > 0 and len(R) > 0 and cp < X and cq < Y and cp + cq + cr < XY:
        p = -P[0]
        q = -Q[0]
        r = -R[0]
        if p > q:
            if p > r:
                score -= heappop(P)
                cp += 1
            else:
                score -= heappop(R)
                cr += 1
        else:
            if q > r:
                score -= heappop(Q)
                cq += 1
            else:
                score -= heappop(R)
                cr += 1

    while len(P) > 0 and len(R) > 0 and cp < X and cp + cq + cr < XY:
        p = -P[0]
        r = -R[0]
        if p >= r:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(R)
            cr += 1

    while len(Q) > 0 and len(R) > 0 and cq < Y and cp + cq + cr < XY:
        q = -Q[0]
        r = -R[0]
        if q >= r:
            score -= heappop(Q)
            cq += 1
        else:
            score -= heappop(R)
            cr += 1

    while cp < X and cp + cq + cr < XY:
        if len(P) > 0:
            score -= heappop(P)
            cp += 1
        else:
            score -= heappop(R)
            cr += 1

    while cq < Y and cp + cq + cr < XY:
        if len(Q) > 0:
            score -= heappop(Q)
            cq += 1
        else:
            score -= heappop(R)
            cr += 1

    print(score)


main()

Submission status: Wrong Answer

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: Wrong Answer

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 = sorted(p + r, reverse=True)
q = sorted(q + r, reverse=True)
print(sum(p[:X]) + sum(q[:Y]))

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
lia=list(map(int,input().split()))
lib=list(map(int,input().split()))
lic=list(map(int,input().split()))
lia.sort(reverse=True)
lib.sort(reverse=True)
lic.sort(reverse=True)
ans=lia[:x]+lib[:y]
ans.sort(reverse=True)
cnt=0
now=0
while cnt < x+y and now < c:
  if lic[now] > ans[cnt]:
    ans[cnt]=lic[now]
    now+=1
  cnt+=1
print(sum(ans))

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heapify, heappush


x, y, a, b, c = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
AB = A[:x] + (B[:y])

heapify(AB)
heapify(C)
ans = 0
for _ in range(len(C)):
    c = heappop(C)
    ab = heappop(AB)
    if ab > c:
        heappush(AB, ab)
        break
    heappush(AB, c)
ans = sum(AB)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
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)
red_que = p[:x]
green_que = q[:y]
red_que.reverse()
green_que.reverse()
red_count = 0
green_count = 0
for i in range(c):
    if red_que[red_count] < green_que[green_count]:
        if red_que[0] < r[i]:
            red_que[0] = 0
            red_count += 1
            bisect.insort_left(red_que, r[i])
    else:
        if green_que[0] < r[i]:
            green_que[0] = 0
            green_count += 1
            bisect.insort_left(green_que, r[i])
print(sum(green_que) + sum(red_que))

Submission status: Wrong Answer

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

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

x,y,a,b,c = [int(i) for i in readline().split()]
p = [int(i) for i in readline().split()]
q = [int(i) for i in readline().split()]
r = [int(i) for i in readline().split()]

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

from heapq import *

hq = p[:x]+q[:y]
hq.sort(reverse=True)

res = []
rr = r[:]
while hq:
    v = hq.pop()
    while r and r[-1] < v:
        r.pop()
    if not r:
        res.append(v)

print(sum(res) + sum(rr[:x+y-len(res)]))

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# heapq
import heapq
class Reverse:
    def __init__(self, val):
        self.val = val
        
    def __lt__(self, other):
        return self.val > other.val
        
    def __repr__(self):
        return repr(self.val)
    
class Heapq:
    def __init__(self, arr, desc = False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)
 
    def pop(self):
        if self.desc: return heapq.heappop(self.hq).val
        else: return heapq.heappop(self.hq)
 
    def push(self, a):
        if self.desc: heapq.heappush(self.hq, Reverse(a))
        else: heapq.heappush(self.hq, a)
 
    def top(self):
        if self.desc: return self.hq[0].val
        else: return self.hq[0]

from operator import itemgetter
def index_sort(A):
    A_sort = sorted(enumerate(A),key=itemgetter(1))
    index = [a[0] for a in A_sort]
    sorted_A = [a[1] for a in A_sort]
    return index, sorted_A

X,Y,A,B,C = LI()
p = LI()
q = LI()
r = LI()

Z = []
for i in range(A):
    Z.append((p[i],0)) # red
for i in range(B):
    Z.append((q[i],1)) # green
for i in range(C):
    Z.append((r[i],2))

Z.sort(key=lambda x:-x[0])
index = [Z[i][1] for i in range(len(Z))]

R = []
G = []
M = []
Rm = Heapq([])
Gm = Heapq([])
ans = 0

for i in range(X+Y):
    ans += Z[i][0]
    if Z[i][1] == 0:
        R.append(Z[i][0])
        Rm.push(Z[i][0])
    elif Z[i][1] == 1:
        G.append(Z[i][0])
        Gm.push(Z[i][0])
    else:
        M.append(Z[i][0])
        Rm.push(Z[i][0])
        Gm.push(Z[i][0])

nr = len(R)
ng = len(G)
nm = len(M)
for i in range(X+Y,A+B+C):
    if ng+nm < Y:
        if Z[i][1] == 0:
            continue
        else:
            v = Rm.pop()
            Gm.push(Z[i][0])
            ng += 1
            nr -= 1
            ans -= v
            ans += Z[i][0]
    elif nr+nm < X:
        if Z[i][1] == 1:
            continue
        else:
            v = Gm.pop()
            Rm.push(Z[i][0])
            nr += 1
            ng -= 1
            ans -= v
            ans += Z[i][0]
    else:
        break

print(ans)

Submission status: Wrong Answer

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()))
L = p + r
L.sort(reverse = True)
ans = 0
for i in range(X):
  ans += int(L[i])
for i in range(X):  
  L.remove(L[i])  
M = q + L
M.sort(reverse = True)
abc = 0  
for i in range(Y):
  abc += int(M[i])
print(ans + abc)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
xx=x
yy=y
p=sorted(list(map(int,input().split())))
q=sorted(list(map(int,input().split())))
r=sorted(list(map(int,input().split())))[::-1]
z=0
rr=r[0]
for i in range(a)[::-1]:
  if p[i]>=rr and x:
    z+=p[i]
    x-=1
for i in range(b)[::-1]:
  if q[i]>=rr and y:
    z+=q[i]
    y-=1
rl=0
if x:
  p=p[:a-xx+x]
  p=p[::-1]
  pl=0
  for _ in range(x):
    if rl<c:
      if p[pl]<r[rl]:
        z+=r[rl]
        rl+=1
      else:
        z+=p[pl]
        pl+=1
if y:
  q=q[:b-yy+y]
  q=q[::-1]
  ql=0
  for _ in range(y):
    if rl<c:
      if q[ql]<r[rl]:
        z+=r[rl]
        rl+=1
      else:
        z+=q[ql]
        ql+=1
print(z)

Submission status: Wrong Answer

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

p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
r_list = list(map(int, input().split()))

p_list.sort()
p_list.reverse()
q_list.sort()
q_list.reverse()
r_list.sort()
r_list.reverse()


total = sum(p_list[:X]) + sum(q_list[:Y])

x = X - 1
y = Y - 1
c = 0
while c < C:
	changed = False
	if y >= 0 and (x < 0 or p_list[x] >= q_list[y]):
		if q_list[y] < r_list[c]:
			total += r_list[c] - q_list[y]
			y -= 1
			c += 1
			changed = True
	if c >= C:
		break
	if x >= 0 and (y < 0 or p_list[x] <= q_list[y]):
		if p_list[x] < r_list[c]:
			total += r_list[c] - p_list[x]
			y -= 1
			c += 1
			changed = True
	if not changed:
		break

print(total)

Submission status: Wrong Answer

Solution code:
x, y, al, bl, cl = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
am = x-1
bm = y-1
cm = 0
while True:
    #print(am)
    #print(bm)
    #print(cm)
    if cm >= cl:
        break
    elif (c[cm] <= a[am]) & (c[cm] <= b[bm]):
        break
    elif (c[cm] > a[am]) & (c[cm] <= b[bm]):
        cm += 1
        am -= 1
    elif (c[cm] <= a[am]) & (c[cm] > b[bm]):
        cm += 1
        bm -= 1
    else:
        if (c[cm]-a[am]) < (c[cm]-b[bm]):
            cm += 1
            bm -= 1
        else:
            cm += 1
            am -= 1
ans = 0
for i in range(am+1):
    ans += a[i]
for j in range(bm+1):
    ans += b[j]
if cm != 0:
    for k in range(cm):
        ans += c[k]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
import heapq

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()
    q.sort()
    r.sort()
    p = p[-X:]
    q = q[-Y:]
    r = r[-X-Y:]
    use = p+q
    heapq.heapify(use)
    heapq.heapify(r)
    suse = []
    while len(use)>0 and len(r)>0:
        unt = heapq.heappop(use)
        nc = heapq.heappop(r)
        if nc > unt:
            suse.append(nc)
        else:
            heapq.heappush(use,unt)

    print(sum(use)+sum(suse))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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()))
maze = [['',0] for i in range(a+b+c)]
for i in range(a):
    maze[i] = ["r",p[i]]
for i in range(b):
    maze[a + i] = ["g",q[i]]
for i in range(c):
    maze[a + b + i] = ["n",r[i]]

maze.sort(reverse=True,key = lambda x:x[1])

ans = 0
index = 0
for i in range(x):
    while index < (a+b+c):
        if maze[index][0] == "r" or maze[index][0] == "n":
            ans += maze[index][1]
            index += 1
            break
        index += 1
index = 0
for i in range(y):
    while index < (a+b+c):
        if maze[index][0] == "g" or maze[index][0] == "n":
            ans += maze[index][1]
            index += 1
            break
        index += 1

ans1 = 0
index = 0
for i in range(y):
    while index < (a+b+c):
        if maze[index][0] == "g" or maze[index][0] == "n":
            ans1 += maze[index][1]
            index += 1
            break
        index += 1
for i in range(x):
    while index < (a+b+c):
        if maze[index][0] == "r" or maze[index][0] == "n":
            ans1 += maze[index][1]
            index += 1
            break
        index += 1
index = 0

print(max(ans,ans1))

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted([(i,0) for i in p])
q = sorted([(i,1) for i in q])
r = sorted([(i,2) for i in r])
used = [0] * c
apple = sorted(p + q + r, reverse=True)
P,Q,R = 0,0,0
ans = 0
for i,k in apple[:x+y]:
  if k == 0:
    if P < x:
      P += 1
      ans += i
  elif k == 1:
    if Q < y:
      Q += 1
      ans += i
  else:
    R += 1
    ans += i
res = x + y - (P+Q+R)
if res:
  if P == x:
    apple = sorted(q[Q:] + r[R:],reverse=True)
    for i,k in apple[:res]:
      ans += i
  elif Q == y:
    apple = sorted(p[P:]+r[R:],reverse=True)
    for i,k in apple[:res]:
      ans += i
print(ans)

Submission status: Wrong Answer

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()
p = p[::-1]
q = q[::-1]
r = r[::-1]

ir = 0
ig = 0

red = 0
for i in range(x):
    red += p[i]
    ir = i
green = 0
for i in range(y):
    green += q[i]
    ig = i

ino = 0
while (0 <= ir or 0 <= ig) and ino < c:
    iir = ir
    iig = ig
    iino = ino
    if ig < 0:
        if p[ir] < r[ino]:
            red -= p[ir]
            red += r[ino]
            ino += 1
            ir -= 1
    if ir < 0:
        if q[ig] < r[ino]:
            green -= q[ig]
            green += r[ino]
            ino += 1
            ig -= 1

    if p[ir] < q[ig]:
        if p[ir] < r[ino]:
            red -= p[ir]
            red += r[ino]
            ino += 1
            ir -= 1
    else:
        if q[ig] < r[ino]:
            green -= q[ig]
            green += r[ino]
            ino += 1
            ig -= 1

    if iir == ir and iig == ig and iino == ino:
        break

print(green + red)

Submission status: Wrong Answer

Solution code:
#500
X, Y, A, B, C = map(int,input().split())
p0 = list(map(int,input().split()))
q0 = list(map(int,input().split()))
r0 = list(map(int,input().split()))
p = sorted(p0, reverse = True); p.append(0)
q = sorted(q0, reverse = True); q.append(0)
r = sorted(r0, reverse = True); r.append(0)

happy = 0
i = 0
j = 0
k = 0
red = 0
green = 0
white = 0

for _ in range(X+Y):
    if r[k] > p[i] and r[k] > q[j]:
        happy += r[k]
        white += 1
        k += 1
    elif p[i] > q[j]:
        if red != X:
            happy += p[i]
            red += 1
            i += 1
        elif q[j] > r[k] and green != Y:
            happy += q[j]
            green += 1
            j += 1
    else:
        if green != Y:
            happy += q[j]
            green += 1
            j += 1
        elif p[i] > r[k] and red != X:
            happy += p[i]
            red += 1
            i += 1
            
print(happy)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
ps = sorted(list(map(int, input().split())), reverse=True)
qs = sorted(list(map(int, input().split())), reverse=True)
rs = sorted(list(map(int, input().split())), reverse=True)

tr = ps[:x]
tg = qs[:y]
added = 0
for e in rs:
  if len(tg) != 0 and len(tr) != 0:
    if tr[-1] > tg[-1] and e > tg[-1]:
      tg.pop()
      added += e
    elif tg[-1] > tr[-1] and e > tr[-1]:
      tr.pop()
      added += e
    else:
      break
  elif len(tg) != 0:
    if e > tg[-1]:
      tg.pop()
      added += e
    else:
      break
  elif len(tr) != 0:
    if e > tr[-1]:
      tr.pop()
      added += e
    else:
      break
  else:
    break
print(sum(tg)+sum(tr)+added)

Submission status: Wrong Answer

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

reds.sort(reverse=True)
greens.sort(reverse=True)
nos.sort(reverse=True)

bests = reds[:X]+greens[:Y]
bests.sort(reverse=True)

very_bests = []

for best in bests:
        if best > nos[0]:
                very_bests.append(best)
        else:
                very_bests.append(nos[0])
                nos = nos[1:]

print(sum(very_bests))

Submission status: Wrong Answer

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 = [(i,0) for i in p]
q = [(i,1) for i in q]
r = [(i,2) for i in r]
used = [0] * c
apple = sorted(p + q + r, reverse=True)
P,Q,R = 0,0,0
ans = 0
for i,k in apple[:x+y]:
  if k == 0:
    if P < x:
      P += 1
      ans += i
  elif k == 1:
    if Q < y:
      Q += 1
      ans += i
  else:
    R += 1
    ans += i
res = x + y - (P+Q+R)
if res:
  if P == x:
    apple = sorted(q[Q:] + r[R:],reverse=True)
    for i,k in apple[:res]:
      ans += i
  elif Q == y:
    apple = sorted(p[P:]+r[R:],reverse=True)
    for i,k in apple[:res]:
      ans += i
print(ans)

Submission status: Wrong Answer

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()
P = P[:X]
Q = Q[:Y]
S = []
#print(P,Q,S)
for _ in range(X+Y):
    if len(R) == 0:
        break
    s = R[-1]
    if len(P) == 0:
        p = 1000000000000
    elif len(Q) == 0:
        q = 1000000000000
    else:
        p = P[-1]
        q = Q[-1]
    #print(p,q)
    if p >= q:
        if q < s:
            del Q[-1]
            del R[-1]
            S.append(s)
        else:
            break
    else:
        if p < s:
            del P[-1]
            del R[-1]
            S.append(s)
        else:
            break
#print(P,Q,S)
ans = sum(P) + sum(Q) + sum(S)
print(ans)

Submission status: Wrong Answer

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

p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
r_list = list(map(int, input().split()))

p_list.sort()
p_list.reverse()
q_list.sort()
q_list.reverse()
r_list.sort()
r_list.reverse()


total = sum(p_list[:X]) + sum(q_list[:Y])

x = X - 1
y = Y - 1
c = 0
while c < C:
	changed = False
	if y >= 0 and (x < 0 or p_list[x] >= q_list[y]):
		if q_list[y] < r_list[c]:
			total += r_list[c] - q_list[y]
			y -= 1
			c += 1
			changed = True
	elif x >= 0 and (y < 0 or p_list[x] <= q_list[y]):
		if p_list[x] < r_list[c]:
			total += r_list[c] - p_list[x]
			y -= 1
			c += 1
			changed = True
	if not changed:
		break

print(total)

Submission status: Wrong Answer

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)
res = sum(p[:X])+sum(q[:Y])
m = X-1
n = Y-1
o = 0
while o<C:
    if p[m]<q[n]:
        if p[m]<r[o]:
            res = res + r[o]-p[m]
            m -= 1
            o += 1
        else:break
    else:
        if q[n]<r[o]:
            res = res + r[o]-q[n]
            n -= 1
            o += 1
        else:break
print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, A, B, C, P, Q, R):
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort()
    P = P[:N]
    Q = Q[:M]

    S = []
    while True:
        if not R:
            break
        if P and Q and P[-1] < R[-1] and Q[-1] < R[-1]:
            if P and P[-1] > R[-1]:
                Q.pop()
                S.append(R.pop())
            else:
                P.pop()
                S.append(R.pop())
        elif P and P[-1] < R[-1]:
            P.pop()
            S.append(R.pop())
        elif Q and Q[-1] < R[-1]:
            Q.pop()
            S.append(R.pop())
        else:
            break

    return sum(P) + sum(Q) + sum(S)


def main():
    N, M, A, B, C = read_int_n()
    P = read_int_n()
    Q = read_int_n()
    R = read_int_n()
    print(slv(N, M, A, B, C, P, Q, R))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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()))
maze = [['',0] for i in range(a+b+c)]
for i in range(a):
    maze[i] = ["r",p[i]]
for i in range(b):
    maze[a + i] = ["g",q[i]]
for i in range(c):
    maze[a + b + i] = ["n",r[i]]

maze.sort(reverse=True,key = lambda x:x[1])

ans = 0
index = 0
for i in range(x):
    while index < (a+b+c):
        if maze[index][0] == "r" or maze[index][0] == "n":
            ans += maze[index][1]
            index += 1
            break
        index += 1
index = 0
for i in range(y):
    while index < (a+b+c):
        if maze[index][0] == "g" or maze[index][0] == "n":
            ans += maze[index][1]
            index += 1
            break
        index += 1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    x,y,a,b,c = map(int,input().split())
    red = list(map(int,input().split()))
    green = list(map(int,input().split()))
    colorless = list(map(int,input().split()))

    red.sort()
    green.sort()
    colorless.sort()

    total = 0
    while x > 0:
        if (not red) or (not colorless):
            break
        
        if red[-1] >= colorless[-1]:
            total += red.pop()
        else:
            total += colorless.pop()

        x -= 1

    while x > 0:
        if red:
            total += red.pop()
        elif colorless:
            total += colorless.pop()
        else:
            break

        x -= 1

    while y > 0:
        if (not green) or (not colorless):
            break
        
        if green[-1] >= colorless[-1]:
            total += green.pop()
        else:
            total += colorless.pop()

        y -= 1

    while y > 0:
        if green:
            total += green.pop()
        elif colorless:
            total += colorless.pop()
        else:
            break

        y -= 1

    print(total)

main()