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:
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 = P[:X]
green = Q[:Y]
ans = sum(red) + sum(green)

ridx = 0
redidx = -1
greenidx = -1

for i in range(X+Y):
    if red[redidx] < green[greenidx] and abs(redidx) <= len(red):
        if red[redidx] < R[ridx]:
            ans -= red[redidx]
            ans += R[ridx]
            ridx += 1
            if abs(redidx) < len(red):
                redidx -= 1
    elif red[redidx] > green[greenidx] and abs(greenidx) <= len(green):
        if green[greenidx] < R[ridx]:
            ans -= green[greenidx]
            ans += R[ridx]
            ridx += 1
            if abs(greenidx) < len(green):
                greenidx -= 1

    if ridx == len(R):
        break

print(ans)

Submission status: Wrong Answer

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    X, Y, A, B, C = iim()
    P = list(iim())
    Q = list(iim())
    R = list(iim())

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

    p1 = [0]*(X+1)
    for i in range(X):
        p1[i+1] = p1[i] + P[i]
    q1 = [0]*(Y+1)
    for i in range(Y):
        q1[i+1] = q1[i] + Q[i]

    si = [X-1, Y-1]
    S = [P, Q]
    if P[si[0]] < Q[si[1]]:
        idx = 0
    else:
        idx = 1
    ans = 0
    P[0] = Q[0] = float("inf")
    for i in range(C):
        if S[idx][si[idx]] < R[i]:
            si[idx] -= 1
            ans += R[i]
            if P[si[0]] < Q[si[1]]:
                idx = 0
            else:
                idx = 1
        else:
            break

    print(p1[si[0]+1] + q1[si[1]+1] + ans)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
from itertools import *

def merge(x,y):
  while 1:
    if x and y and x[-1] >= y[-1] or (not (x and y)) and x:
      yield x.pop()
    elif y:
      yield y.pop()
    else:
      return

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

S = sum(islice(merge(P,R), X))
S += sum(islice(merge(Q,R), Y))
print(S)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,Y,A,B,C = map(int, raw_input().split())
P = map(int, raw_input().split())
Q = map(int, raw_input().split())
R = map(int, raw_input().split())

Ps = sorted(P, reverse=True)
Qs = sorted(Q, reverse=True)
Rs = sorted(R, reverse=True)

Ps = Ps[:X+Y]
Qs = Qs[:X+Y]

pi,qi,ri = 0,0,0
pc,qc,rc = 0,0,0

PP, QQ = [],[]
for i in range(X+Y):
    if pi >= X and qi >= Y:
        # print "beeak"
        break
    if pi >= X and qi < Y:
        # print qi
        a = sorted(Qs + Rs, reverse=True)
        QQ = a[:Y]
        # QQ.append(Qs[qi])
        # qi += 1
        # qc += 1
        break
    if pi < X and qi >= Y:
        a = sorted(Ps + Rs, reverse=True)
        PP = a[:X]
        # print pi
        # PP.append(Ps[pi])
        # pi += 1
        # pc += 1
        # continue
        break
    # print i, pi, qi

    if Qs[qi] < Ps[pi] and pi < X:
        # print Ps[pi]
        PP.append(Ps[pi])
        pi += 1
        pc += 1
    elif qi < Y:
        # print Qs[qi]
        QQ.append(Qs[qi])
        qi += 1
        qc += 1

# print PP
# print QQ

V = sorted(PP + QQ ,reverse=True)

print V
print sum(V)
# r = Rs[0]
# ans = -1
# for i,v in enumerate(V):
#     if v < r:
#         a = V[:i]
#         b = Rs[:X+Y-i]
#         o = X+Y-(len(a)+len(b))
#         # print o
#         # print V[:i+o]
#         # print Rs[:X+Y-i]
#         print V[:i+o] + Rs[:X+Y-i]
#         ans = sum(V[:i+o]) + sum(Rs[:X+Y-i])
#         break

# if ans == -1:
#     print sum(V)
# else:
#     print ans

"""
2 2 2 2 1
8 1
9 1
2
"""

"""
2 2 2 2 4
1 1
1 1
2 2 2 2
"""

"""
2 2 2 2 4
9 2
1 2
2 2 2 2
"""

"""
3 1 3 1 3
7 7 7
9
8 8 8
"""

"""
3 1 3 1 3
8 8 8
7
9 9 9
"""

Submission status: Wrong Answer

Solution code:
import sys
import itertools
input = sys.stdin.readline

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)
cumsum_p = list(itertools.accumulate(p))
cumsum_q = list(itertools.accumulate(q))
cnt_p = 0
cnt_q = 0
for i in range(c):
    # print(p[x - 1 - cnt_p], q[y - 1 - cnt_q], cnt_q, cnt_p, cumsum_q, cumsum_p)
    if x - 1 - cnt_p < 0 and y - 1 - cnt_q < 0:
        break
    if x - 1 - cnt_p < 0:
        if r[i] > p[x - 1 - cnt_p]:
            cumsum_p[x - 1] += r[i] - p[x - 1 - cnt_p]
            cnt_p += 1
        else:
            break
    if y - 1 - cnt_q < 0:
        if r[i] > q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    if p[x - 1 - cnt_p] > q[y - 1 - cnt_q]:
        if r[i] > q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    else:
        if r[i] > p[x - 1 - cnt_p]:
            cumsum_p[x - 1] += r[i] - p[x - 1 - cnt_p]
            cnt_p += 1
        else:
            break
print(cumsum_p[x - 1] + cumsum_q[y - 1])

Submission status: Wrong Answer

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 len(nc) > 0 and r[0] < nc[0]:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1

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

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

        elif len(nc) > 0 and  g[0] >= nc[0]:
            ans += -1 * nc[0]
            heapq.heappop(nc)
            Y -= 1
        else :
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1
            

print(ans)

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().split())
p = sorted([int(i) for i in input().split()], reverse=True)
q = sorted([int(i) for i in input().split()], reverse=True)
r = sorted([int(i) for i in input().split()], reverse=True)
l = sorted(p[:X] + q[:Y],reverse=True)

i = 0
ans = 0
for a in l:
    if i < C and r[i] < a:
        ans += a
    elif i < C and r[i] >= a:
        ans += r[i]
        i += 1
        
print(ans)

Submission status: Wrong Answer

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

# print(p, q, r)
pp = x - 1
qp = y - 1
rp = 0
while rp < c:
    if r[rp] > p[pp] or r[rp] > q[qp]:
        if (p[pp] >= q[qp] or qp < 0) and (pp >= 0 or qp >= 0):
            q[qp] = r[rp]
            qp -= 1
        elif (p[pp] >= q[qp] or pp < 0) and (pp >= 0 or qp >= 0):
            p[pp] = r[rp]
            pp -= 1
    rp += 1

# print(p,q)
print(sum(p[:x]) + sum(q[:y]))

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()))
S=[[0,'pqr'] for j in range(A+B+C)]
countX=X
countY=Y
count=0
for i in range(A):
    S[i]=[p[i],'p']
for i in range(B):
    S[A+i]=[q[i],'q']
for i in range(C):
    S[A+B+i]=[r[i],'r']
S_sorted = sorted(S, key=lambda x: x[0])
for i in range(X+Y):
    if S_sorted[len(S_sorted)-i-1][1]=='p':
        countX-=1
    elif S_sorted[len(S_sorted)-i-1][1]=='q':
        countY-=1
    else:
        if countX==0:
            countX-=1
        else:
            countY-=1
    count+=S_sorted[len(S_sorted)-i-1][0]
    

print(count)

Submission status: Wrong Answer

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

print(R, G, M)

R.sort(reverse=True)
G.sort(reverse=True)

for i in range(X):
  M.append(R[i])

for i in range(Y):
  M.append(G[i])

M.sort(reverse=True)

ans = 0
for i in range(X + Y):
  ans = M[i] + ans

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

Submission status: Wrong Answer

Solution code:
import heapq

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

p = sorted(map(int, input().split()), reverse=True)
q = sorted(map(int, input().split()), reverse=True)
r = sorted(map(int, input().split()), reverse=False)

red = p[:X]
green = q[:Y]
nocolor = r


heapq.heapify(red)
heapq.heapify(green)
# heapq.heapify(nocolor)

for nc in nocolor:
    R = red[0]
    G = green[0]
    if R > nc and G > nc:
        break
    if nc > R and nc > G:
        if R > G:
            q = heapq.heappop(green)
            heapq.heappush(green,nc)
            continue

        else:
            q = heapq.heappop(red)
            heapq.heappush(red,nc)
            continue

    if nc > R:
        q = heapq.heappop(red)
        heapq.heappush(red,nc)
        continue
    if nc > G:
        q = heapq.heappop(green)
        heapq.heappush(green,nc)
        continue



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

Submission status: Wrong Answer

Solution code:
import sys 
# input = sys.stdin.readline().strip()
x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
s = []
for i in p:
    s.append((i,0))
for i in q:
    s.append((i,1))
for i in r:
    s.append((i,2))
s.sort(reverse=True)
ans = 0
for i,j in s:
    if x<=0 and y<=0:
        break
    if j==2:
        if x>y:
            x-=1
            ans+=i
        else:
            y-=1
            ans+=i
    else:
        if j==1 and y>0:
            y-=1
            ans+=i
        if j==0 and x>0:
            x-=1
            ans+=i 
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0
x,y,a,b,c=LI()
p=LI()
q=LI()
r=LI()
q=[(-i,0) for i in q]
r=[(-i,1) for i in r]
g=q+r
heapify(g)
#show(g)
c=0
bl=0
while c<y:
    x,t=g.pop()
    ans+=-x
    bl+=t
    c+=1

while g:
    x,t=g.pop()
    if t==1:
        p.append(-x)
p.sort(key=lambda x:-x)
ans+=-sum(p[:x])

print(ans)
'''
show(x,y,(a,b,c))
show(p,q,r)
show(sc)
'''

Submission status: Wrong Answer

Solution code:
X,Y,A,B,C = list(map(int,input().split()))
p = sorted(list(map(int,input().split())),key=lambda x:-x) + [0]
q = sorted(list(map(int,input().split())),key=lambda x:-x) + [0]
r = sorted(list(map(int,input().split())),key=lambda x:-x) + [0]

pi = 0
qi = 0
ri = 0
res = 0
while pi + qi + ri != X + Y:
  if  ( qi == X or r[ri] >= q[qi]) and (pi == Y or r[ri] >= p[pi]):
    #print("r")
    res += r[ri]
    ri += 1
  elif ( qi == Y or p[pi] >= q[qi] ) and p[pi] >= r[ri]:
    # print("p")
    res += p[pi]
    pi += 1
  elif ( pi == X or q[qi] >= p[pi] ) and q[qi] >= r[ri]:
    # print("q")
    res += q[qi]
    qi += 1
  # print(pi + qi + ri != X + Y)
print(res)

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().split())
p = sorted(map(int, input().split()), reverse=True)[:X]
q = sorted(map(int, input().split()), reverse=True)[:Y]
r = sorted(map(int, input().split()), reverse=True)

sump = sum(p)
sumq = sum(q)
ans = sump + sumq
for c in range(C):
    rc = r[c]
    if p and p[-1] < rc:
        sump -= p.pop()
        sump += rc
    elif q and q[-1] < rc:
        sumq -= q.pop()
        sumq += rc
    ans = max(ans, sump + sumq)
print(ans)

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
rcounter = 0
while X > 0 and Y > 0 and len(r) > 0:
    if p[-1] > q[-1] and p[-1] > r[-1]:
        X -= 1
        sum += p[-1]
        del p[-1]
    elif q[-1] > p[-1] and q[-1] > r[-1]:
        Y -= 1
        sum += q[-1]
        del q[-1]
    elif r[-1] > p[-1] and r[-1] > q[-1]:
        X -= 1
        Y -= 1
        sum += r[-1]
        del r[-1]
if X == 0:
    NEW = q + r
    NEW.sort()
    for i in range(Y):
        sum += NEW[-1]
        del NEW[-1]
if Y == 0:
    NEW = p + r
    NEW.sort()
    for i in range(X):
        sum += NEW[-1]
        del NEW[-1]
if len(r) == 0:
    for i in range(X):
        sum += p[-1]
        del p[-1]
    for i in range(Y):
        sum += q[-1]
        del q[-1]
NEW = p + q
NEW.sort()
for i in range(rcounter):
    sum += NEW[-1]
    del NEW[-1]
print(sum)



"""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:
def main():
    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:
            try:
                m = al.pop()
            except:
                m = 0
        else:
            try:
                m = bl.pop()
            except:
                m = 0
        if m < i:
            ans += i
        else:
            ans += m
            break
    ans += sum(al)+sum(bl)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#E - Red and Green Apples
X,Y,A,B,C= map(int, input().split())
a =sorted(list(map(int, input().split())),reverse=True)
b =sorted(list(map(int, input().split())),reverse=True)
c =sorted(list(map(int, input().split())),reverse=True)

DP_a =[]
DP_b =[]
j = 1
k = 1
a_s =sum(a[:X])
b_s =sum(b[:Y])

for i in range(C):
    if c[i] > a[X-j] or c[i] > b[Y-k]:
        if c[i] > a[X-j] and a[X-j] > b[Y-k]:
            j += 1
            if j > X:
                DP_a.append(c[i])
            else:
                pass
        elif c[i] > b[Y-k] and b[Y-k] >= a[X-j]:
            if k > Y:
                DP_b.append(c[i])
            else:
                pass
        else:
            pass
    else:
        break

a_s = a_s-sum(a[X-len(DP_a):X])+sum(DP_a)
b_s = b_s-sum(b[Y-len(DP_b):Y])+sum(DP_b)


print(a_s+b_s)

Submission status: Wrong Answer

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

t = input().split()
x = int(t[0])
y = int(t[1])
a = int(t[2])
b = int(t[3])
c = int(t[4])

p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

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

pp = p[:a]
qq = q[:b]

for rr in r:
    if pp[-1] > qq[-1]:
        if qq[-1] < rr:
            qq[-1] = rr
            qq.sort()
            qq.reverse()
        else:
            break
    else:
        if pp[-1] < rr:
            pp[-1] = rr
            pp.sort()
            pp.reverse()
        else:
            break
print(sum(pp) + sum(qq))

Submission status: Wrong Answer

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)
la=10000000000000000000
ans=0
for i in range(x):
    if len(r)==0:
        _p=heapq.heappop(p)
        ans-=_p
        if i==x-1:
            la=-_p
        continue
    _p,_r=heapq.heappop(p),heapq.heappop(r)
    if _p<=_r:
        ans-=_p
        heapq.heappush(r,_r)
        if i==x-1:
            la=-_p
    else:
        ans-=_r
        heapq.heappush(p,_p)

for i in range(y):
    if len(r)==0:
        _q=heapq.heappop(q)
        ans-=_q
        if i==y-1:
            la=min(la,-_q)
        continue
    _q,_r=heapq.heappop(q),heapq.heappop(r)
    if _q<=_r:
        ans-=_q
        heapq.heappush(r,_r)
        if i==y-1:
            la=min(la,-_q)
    else:
        ans-=_r
        heapq.heappush(q,_q)
if len(r)==c:
    print(ans-r[0]-la)
else:
    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()

a1 = x
b1 = y
for i in range(c):
  if (p[a1-1] >= r[i]) and (q[b1-1] >= r[i]):
    continue
  else:
    if p[a1-1] < q[b1-1]:
      p[a1-1] = r[i]
      if a1 > 0:
        a1 -= 1
    else:
      q[b1-1] = r[i]
      if b1 > 0:
        b1 -= 1
print(sum(p[:x])+sum(q[:y]))

Submission status: Wrong Answer

Solution code:
from collections import deque

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.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

s = deque(p[:X])
t = deque(q[:Y])
r = deque(r)

while r:
  rv = r.popleft()
  sv = s.pop()
  tv = t.pop()
  if sv < rv:
    s.append(rv)
    t.append(tv)
  elif tv < rv:
    s.append(sv)
    t.append(rv)
  else:
    s.append(sv)
    t.append(tv)

print(sum(s)+sum(t))

Submission status: Wrong Answer

Solution code:
def Main(X, Y, A, B, C, p, q, r):
    p.sort()
    q.sort()
    r.sort()
    ans = 0
    wa = X+Y
    for i in range(wa):
        if(len(r) and len(p) and len(q)):
            if p[-1] < r[-1] and q[-1] < r[-1]:
                ans += r.pop()
            else:
                if X > 0 and Y > 0:
                    if p[-1] >= q[-1]:
                        ans += p.pop()
                        X -= 1
                    else:
                        ans += q.pop()
                        Y -= 1
                elif X == 0:
                    ans += q.pop()
                    Y -= 1
                elif Y == 0:
                    ans += p.pop()
                    X -= 1
        elif(len(r) == 0 and len(p) and len(q)):
            if X > 0 and Y > 0:
                if p[-1] >= q[-1]:
                    ans += p.pop()
                    X -= 1
                else:
                    ans += q.pop()
                    Y -= 1
            elif X == 0:
                ans += q.pop()
                Y -= 1
            elif Y == 0:
                ans += p.pop()
                X -= 1         
        elif(len(r) and len(p)==0 and len(q)):
            if q[-1] < r[-1]:
                ans += r.pop()
            else:
                ans += q.pop()
                Y -= 1
        elif(len(r) and len(p) and len(q)==0):
            if p[-1] < r[-1] or X == 0:
                ans += r.pop()
            else:
                ans += p.pop()
                X -= 1  
        elif(len(r)==0 and len(p)==0 and len(q)):
            ans += q.pop()
        elif(len(r)==0 and len(p) and len(q)==0):
            ans += p.pop()
        elif(len(r) and len(p)==0 and len(q)==0):
            ans += r.pop()
    return ans
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()))
    print(Main(X, Y, A, B, C, p, q, r))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

flg = lst_r > lst_g

cur = -1 * heappop(lst_n)
while len(lst_n)>=0:
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
        flg = lst_r > lst_g
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
        flg = lst_r > lst_g
    else:
        break
print(sum(lst_g)+sum(lst_r))

Submission status: Wrong Answer

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]

X, Y, A, B, C = mi()
p = sorted(li())[-X:]
q = sorted(li())[-Y:]
r = sorted(li())

#len_r = len(r)-1
a_p = p.pop(-1)
a_r = r.pop(-1)
cnt = 0

for i in range(X):
    if a_p >= a_r:
        cnt += a_p
        if p!=[]:
            a_p = p.pop(-1)
        else:
            break
    else:
        cnt += a_r
        if r==[]:
            a_r = 0
        else:
            a_r = r.pop(-1)
        

a_q = q.pop(-1)

for i in range(Y):
    if a_q >= a_r:
        cnt += a_q
        if q!=[]:
            a_q = q.pop(-1)
        else:
            break
    else:
        cnt += a_r
        if r==[]:
            a_r = 0
        else:
            a_r = r.pop(-1)

print(cnt)

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)

ans = sum(p[:x]) + sum(q[:y])

i, j = 0, 0
while i < x and j < y and i + j < c:
    change = min(p[x - 1 - i], q[y - 1 - j])
    if r[i + j] > change:
        ans += r[i + j] - change
    else:
        break
    if p[x - 1 - i] < q[y - 1 - j]:
        i += 1
    elif p[x - 1 - i] > q[y - 1 - j]:
        j += 1
    else:
        s, t = 0, 0
        while i + s < x and j + t < y and i + j + s + t < c:
            if p[x - 1 - i - s] < q[y - 1 - j - s]:
                i += 1
                break
            elif p[x - 1 - i - s] > q[y - 1 - j - s]:
                j += 1
                break
            else:
                s += 1
                t += 1
                if not(i + s < x and j + t < y and i + j + s + t < c):
                    i += 1
                    break

print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
a,b,c,d,e=a.split(" ")
a=int(a)
b=int(b)
c=int(c)
d=int(d)
e=int(e)

redtemp=input().split(" ")
greentemp=input().split(" ")
whitetemp=input().split(" ")
red=[]
green=[]
white=[]
for i in range(c):
  red.append(int(redtemp[i]))
  
for i in range(d):
  green.append(int(greentemp[i]))
for i in range(e):
  white.append(int(whitetemp[i]))
red.sort(reverse=True)
green.sort(reverse=True)
white.sort(reverse=True)
value=sum(red[0:a])+sum(green[0:b])
greencount=0
redcount=0

for i in range(e):
  if (redcount>a or greencount>b):
    break
  if red[a-redcount-1]<white[e-i-1] and red[a-redcount-1]<green[b-greencount-1]:
    value-=red[a-redcount-1]
    value+=white[e-i-1]
    redcount+=1
  if green[a-greencount-1]<white[e-i-1] and red[a-redcount-1]>green[b-greencount-1]:
    value-=green[b-greencount-1]
    value+=white[e-i-1]
    greencount+=1
print(value)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = list(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)
cnt = 0
i = 0
while x > 0:
    if len(r) == 0:
        if len(p) == 0:
            break
        else:
            cnt =+ p.pop(0)
    elif p[0] >= r[0]:
        cnt =+ p.pop(0)
    else:
        cnt =+ r.pop(0)
    x =- 1
while y > 0:
    if len(r) == 0:
        if len(q) == 0:
            break
        else:
            cnt =+ q.pop(0)
    elif q[0] >= r[0]:
        cnt =+ q.pop(0)
    else:
        cnt =+ r.pop(0)
    y =- 1    
print(cnt)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
p += [0]
q = list(map(int, input().split()))
q += [0]
r = list(map(int, input().split()))
r += [0]
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
n, m, l = 0, 0, 0
ans = 0
for i in range(x+y):
    if (p[n] >= r[l] or q[m] >= r[l]) and n < x and m < y:
        if p[n] >= q[m]:
            ans += p[n]
            n += 1
        else:
            ans += q[m]
            m += 1
    elif n == x:
        if q[m] >= r[l]:
            ans += q[m]
            m += 1
        else:
            ans += r[l]
            m += 1
            l += 1
    elif m == y:
        if p[n] >= r[l]:
            ans += p[n]
            n += 1
        else:
            ans += r[l]
            n += 1
            l += 1    
    else:
        if p[n] >= q[m]:
            ans += r[l]
            m += 1
            l += 1
        else:
            ans += r[l]
            n += 1
            l += 1
print(ans)