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 collections import deque
x0,y0,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 = deque(p)
q = deque(q)
r = deque(r)
x,y = 0,0
red = p.pop()
green = q.pop()
mu = r.pop()
ans = 0

def newmu():
  if r:
    return r.pop()
  return 0
def newred():
  if p:
    return p.pop()
  return 0
def newgreen():
  if q:
    return q.pop()
  return 0


while x < x0 or y < y0:
  if x < x0 and y < y0:
    if red >= mu and green >= mu:
      ans += red + green
      red = newred()
      green = newgreen()
    elif red >= mu:
      ans += red + mu
      red = newred()
      mu = newmu()
    elif green >= mu:
      ans += green + mu
      green = newgreen()
      mu = newmu()
    elif r:
      r2 = r.pop()
      if red >= green and red >= r2:
        ans += mu + red
        mu = r2
        red = newred()
      elif green >= red and green >= r2:
        ans += mu + green
        mu = r2
        green = newgreen()
      else:
        ans += mu + r2
        mu = newmu()
    elif red >= green:
      ans += mu + red
      red = newred()
      mu = newmu()
    else:
      ans += mu + green
      green = newgreen()
      mu = newmu()
    x += 1
    y += 1
  elif x < x0:
    if red >= mu:
      ans += red
      red = newred()
    else:
      ans += mu
      mu = newmu()
    x += 1
  else:
    if green >= mu:
      ans += green
      green = newgreen()
    else:
      ans += mu
      mu = newmu()
    y += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  x,y,a,b,c=LI()
  la=LI()
  lb=LI()
  lc=LI()

  la.sort(reverse=True)
  lb.sort(reverse=True)
  lc.sort(reverse=True)

  ans_list=[]
  ans_list+=la[:x]
  ans_list+=lb[:y]

  ans_list.sort(reverse=True)
  # print(ans_list)
  i=1
  for _ in range(c):
    if len(lc)==0:
      break
    if x+y<len(lc):
      lc.pop()
    else:
      _c=lc.pop()
      if ans_list[-i]<_c:
        ans_list[-i]=_c
        i+=1

  # print(ans_list)
  return sum(ans_list)

# main()
print(main())

Submission status: Wrong Answer

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:]
    score = sum(red) + sum(blue)
    noc = sorted(noc)[::-1]
    for n in noc:
        r = red[0]
        b = blue[0]
        if min(r, b) < n:
            if r > b:
                blue.pop(0)
                noc.pop(0)
                score += n-b 
            if b > r:
                red.pop(0)
                noc.pop(0)
                score += n-r
        else:
            break
    print(score)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq

heap_x = []
heap_y = []

X, Y, A, B, C = map(int, input().split())
P = list(map(lambda x: int(x)*(-1), input().split()))
Q = list(map(lambda x: int(x)*(-1), input().split()))
R = list(map(lambda x: int(x)*(-1), input().split()))
heapq.heapify(P)
heapq.heapify(Q)
heapq.heapify(R)

for i in range(X+Y):
    if len(heap_x) == X:
        if Q[0] < R[0]:
            heapq.heappush(heap_y, heapq.heappop(Q))
            heapq.heappush(Q, 0)
        else:
            heapq.heappush(heap_y, heapq.heappop(R))
            heapq.heappush(R, 0)
    elif len(heap_y) == Y:
        if P[0] < R[0]:
            heapq.heappush(heap_x, heapq.heappop(P))
            heapq.heappush(P, 0)
        else:
            heapq.heappush(heap_x, heapq.heappop(R))
            heapq.heappush(R, 0)
    else:
        if P[0] <= Q[0]:
            if P[0] < R[0]:
                heapq.heappush(heap_x, heapq.heappop(P))
                heapq.heappush(P, 0)
            else:
                heapq.heappush(heap_x, heapq.heappop(R))
                heapq.heappush(R, 0)
        else:
            if Q[0] < R[0]:
                heapq.heappush(heap_y, heapq.heappop(Q))
                heapq.heappush(Q, 0)
            else:
                heapq.heappush(heap_y, heapq.heappop(R))
                heapq.heappush(R, 0)

print(-(sum(heap_x)+sum(heap_y)))

Submission status: Wrong Answer

Solution code:
import math



red,green,x,y,z = map(int,input().split())
red_data = list(map(int,input().split()))
green_data = list(map(int,input().split()))
blank_data = list(map(int,input().split()))


red_data.sort(reverse=True)
green_data.sort(reverse=True)
blank_data.sort(reverse=True)


# check if colourless apples are better
red_index = red-1
green_index = green-1


for blank in blank_data:
    red_gap = blank - red_data[red_index]
    green_gap = blank - green_data[green_index]
    
    if max(red_gap,green_gap) <= 0:
        break
    
    if red_gap >= green_gap:
        red_data[red_index] = blank
        red_index -= 1
    
    if green_gap > red_gap:
        green_data[green_index] = blank
        green_index -= 1






total = sum(red_data[0:red]) + sum(green_data[0:green])

print(total)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
a=list(map(int,input().rstrip().split()))
b=list(map(int,input().rstrip().split()))
c=list(map(int,input().rstrip().split()))
c.sort(reverse=True)
d=0
a1=a+c
a1.sort(reverse=True)
for i in range(x):
    d=d+a1[i]
l=x-len(c)
b1=b+c[l:len(c)]
b1.sort(reverse=True)
for j in range(y):
    d=d+b1[j]
print(d)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
x,y,a,b,c=[int(i) for i in input().split()]
p=[[int(i),1] for i in input().split()]
q=[[int(i),2] for i in input().split()]
r=[[int(i),0] for i in input().split()]
ans=[]
ans.extend(p)
ans.extend(q)
ans.extend(r)
ans.sort(key= lambda x: x[0],reverse=True)
rans=0 ; 
ap=[0,0,0]
for i in range(a):
    ap[0]+=p[i][0]
for i in range(b):
    ap[1]+=q[i][0]
for i in range(c):
    ap[2]+=r[i][0]
for i in ans:
    if x+y==0: break
    if i[1]==1:
        if x==0: continue
        x-=1
        a-=1
        rans+=i[0]
        ap[0]-=i[0]
    if i[1]==2:
        if y==0: continue
        y-=1
        b-=1
        rans+=i[0]
        ap[1]-=i[0]
    if i[1]==0:
        if ap[0]>ap[2]:
            x-=1
            rans+=i[0]
            ap[0]-=i[0]
        else:
            y-=1
            rans+=i[0]
            ap[1]-=i[0]
        c-=1
print(rans)

Submission status: Wrong Answer

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

xx = sorted(xx, reverse=True)[:x]
yy = sorted(yy, reverse=True)[:y]
rr = sorted(rr, reverse=True)[:x+y]

ri = -1
xi = x-1
yi = y-1
happy = 0
for i in range(x+y-len(rr)-1):
    if (xx[xi] <= yy[yi]) and(xx[xi] < rr[ri+1]):
        xi -= 1
        ri +=1
        continue
    if (yy[yi] <= xx[xi]) and(yy[yi] < rr[ri+1]):
        yi -= 1
        ri +=1
        continue
print(sum(xx[:xi+1]) + sum(yy[:yi+1]) + sum(rr[:ri+1]))

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = list(map(int, input().split(' ')))
pa = list(map(int, input().split(' ')))
qb = list(map(int, input().split(' ')))
rc = list(map(int, input().split(' ')))

pa = sorted(pa)[len(pa) - x:]
qb = sorted(qb)[len(qb) - y:]
rc = sorted(rc, reverse=True)

ai = 0
bi = 0
result = sum(pa) + sum(qb)

# for r in rc:
#     if bi >= b and ai >= a:
#         break
    
#     if bi >= b or pa[ai] <= qb[bi]:
#         if r > pa[ai]:
#             result = result - pa[ai] + r
#             ai += 1
            
#     elif ai >= a or pa[ai] >= qb[bi]:
#         if r > qb[bi]:
#             result = result - qb[bi] + r
#             bi += 1
print(result)

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

ri=sorted(r)[::-1]

pi=sorted(p)[::-1][:x]
qi=sorted(q)[::-1][:x]
heapq.heapify(pi)
heapq.heapify(qi)

try:
    while pi[0]<ri[0] or qi[0]<ri[0]:
        if pi[0]<qi[0]:
            heapq.heappop(pi)
            heapq.heappush(pi,ri[0])
            ri.pop(0)
        else:
            heapq.heappop(qi)
            heapq.heappush(qi,ri[0])
            ri.pop(0)
except IndexError:
    pass
print(sum(pi)+sum(qi))

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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

ans = sum(p[0:X]) + sum(q[0:Y])

for i in range(C):
    if p and q and r:
        if p[-1] <= q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        elif q[-1] <= p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif p and r:
        if p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif q and r:
        if q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        else:
            break
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
la=list(map(int,input().split()))
lb=list(map(int,input().split()))
lc=list(map(int,input().split()))
la.sort()
lb.sort()
lc.sort()
summ=0
while(x>0 or y>0):

	if(x>0 and y>0):
		if min(la[a-1],lb[b-1])<lc[c-1] :
		
			summ+=lc[c-1]+max(la[a-1],lb[b-1])
			
				#summ+=lc[c-1]
			if(max(la[a-1],lb[b-1])==la[a-1]):
				a-=1
				c-=1
				x-=1
				y-=1
			else:
				b-=1
				c-=1
				x-=1
				y-=1

		else:
			summ+=la[a-1]+lb[b-1]
			a-=1
			b-=1
			x-=1
			y-=1
	else:
		if(x!=0):
			if(la[a-1]<lc[c-1]):
				summ+=lc[c-1]
				c-=1
				x-=1
			else:
				summ+=la[a-1]
				a-=1
				x-=1
		if(y!=0):
			if(lb[b-1]<lc[c-1]):
				summ+=lc[c-1]
				c-=1
				y-=1
			else:
				summ+=lb[b-1]
				b-=1
				y-=1


print(summ)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = (int(x) for x in input().split())
p = sorted((int(x) for x in input().split()), reverse=True)
q = sorted((int(x) for x in input().split()), reverse=True)
r = sorted((int(x) for x in input().split()), reverse=True)
ans = sum(p[:x]) + sum(q[:y])
cnt = 0
cnt_a = 0
cnt_b = 0
for cnt in range(c):
    if r[cnt] < p[x-cnt_a-1] and r[cnt] < q[y-cnt_b-1] and x-cnt_a-1 > -1 and y-cnt_b-1 > -1:
        break
    if p[x-cnt_a-1] < q[y-cnt_b-1] and x-cnt_a-1 > -1:
        ans = (ans-p[x-cnt_a-1]) + r[cnt] 
        cnt_a += 1
        # cnt+=1
    elif p[x-cnt_a-1] > q[y-cnt_b-1] and y-cnt_b-1 > -1:
        ans = (ans-q[y-cnt_b-1]) + r[cnt] 
        cnt_b += 1
        # cnt += 1
    else:
        # determine side
        j = 0
        try:
            while p[x-cnt_a-1-j] == q[y-cnt_b-1-j]:
                j += 1
            if p[x-cnt_a-1-j] < q[y-cnt_b-1-j] and x-cnt_a-1 > -1:
                ans = (ans-p[x-cnt_a-1]) + r[cnt] 
                cnt_a += 1
                # cnt+=1
            elif y-cnt_b-1 > -1:
                ans = (ans-q[y-cnt_b-1]) + r[cnt] 
                cnt_b += 1
                # cnt += 1
        except:
            if x > y and x-cnt_a-1 > -1:
                ans = (ans-p[x-cnt_a-1]) + r[cnt] 
                cnt_a += 1
                # cnt+=1
            elif y-cnt_b-1 > -1:
                ans = (ans-q[y-cnt_b-1]) + r[cnt] 
                cnt_b += 1
                # cnt += 1
print(ans)

Submission status: Wrong Answer

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

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

# X,Y個を越えたP,Qは使わない
P=P[-X:]
Q=Q[-Y:]
R=R[-(X+Y):]
# print(P,Q)
PQ=sorted(P+Q)
# print("PQ",PQ)

allsum=sum(PQ)

ind=0
for i in range(len(R)):
#  print(R[i],allsum)
  if R[i]>PQ[ind]:
    # 無色のリンゴを代わりに食べる
    allsum+=R[i]-PQ[ind]
  #  print("pick",R[i],allsum)
    ind+=1
    if ind==len(PQ):
      break
#  else:
#    print(R[i],"is not useful for",PQ)
        
print(allsum)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = map(int,input().split())
ar = list(map(int,input().split()))
br = list(map(int,input().split()))
cr = list(map(int,input().split()))
ar.sort(reverse=True)
br.sort(reverse=True)
cr.sort(reverse=True)
ar = ar[:x]
br = br[:y]
i,c=0,0
while i<(min(x,y)):
    if(ar[i]<cr[c]):
        ar[i]=cr[c]
        c+=1
    elif(br[i]<cr[c]):
        br[i]=cr[c]
        c+=1
    i+=1
g=max(x,y)
while i <g:
    if(g==x):
        if(ar[i]<cr[c]):
            ar[i]=cr[c]
            c+=1
    if g==y:
        if(br[i]<cr[c]):
            br[i]=cr[c]
            c+=1
    i+=1
ans = sum(ar)+sum(br)
print(ans)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
P=sorted(list(map(int,input().split())))[::-1]
Q=sorted(list(map(int,input().split())))[::-1]
R=sorted(list(map(int,input().split())))[::-1]+[0]*(max(x+y-c,0))
L=sorted(P[:x]+Q[:y])[::-1]
l=r=0
for i in range(x+y): 
    if r+R[i]<l+L[-i-1]:
        print(sum(L[:x+y-i]+sorted(L[x+y-i:]+R[:i])[::-1][:i]))
        exit()
    r+=R[i]
    l+=L[-i-1]
print(r)

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

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

# dp = [[0] * Y for _ in range(X)]

def red_first():
    dp = [0] * X
    dp2 = [0] * Y
    p_idx = 0
    q_idx = 0
    r_idx = 0

    # print(dp)
    for i in range(X):
        if r_idx < len(r) and r[r_idx] > p[p_idx]:
            dp[i] = r[r_idx]
            r_idx += 1
        else:
            dp[i] = p[p_idx]
            p_idx += 1
    # print(dp)
    red = sum(dp)

    for i in range(Y):
        if r_idx < len(r) and r[r_idx] > q[q_idx]:
            dp2[i] = r[r_idx]
            r_idx += 1
        else:
            dp2[i] = q[q_idx]
            q_idx += 1

    # print(dp2)
    green = sum(dp2)
    return red + green

def green_first():
    dp = [0] * X
    dp2 = [0] * Y
    p_idx = 0
    q_idx = 0
    r_idx = 0

    for i in range(Y):
        if r_idx < len(r) and r[r_idx] > q[q_idx]:
            dp2[i] = r[r_idx]
            r_idx += 1
        else:
            dp2[i] = q[q_idx]
            q_idx += 1

    # print(dp2)
    green = sum(dp2)

    # print(dp)
    for i in range(X):
        if r_idx < len(r) and r[r_idx] > p[p_idx]:
            dp[i] = r[r_idx]
            r_idx += 1
        else:
            dp[i] = p[p_idx]
            p_idx += 1
    # print(dp)
    red = sum(dp)    
    return red + green

print(max(red_first(), green_first()))

Submission status: Wrong Answer

Solution code:
from sys import stdin

x, y,a,b,c = [int(x) for x in stdin.readline().rstrip().split()]

p = [int(x) for x in stdin.readline().rstrip().split()]
q = [int(x) for x in stdin.readline().rstrip().split()]
r = [int(x) for x in stdin.readline().rstrip().split()]

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

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

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

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

he = 0

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

Submission status: 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()

pi = A - 1
qi = B - 1
ri = C - 1

yummy = 0
i = 1
while i <= X + Y:
  if pi > A - X - 1 and qi > B - Y - 1:
    a = p[pi]
    b = q[qi]
    c = r[ri]
    if c < a and c < b:
      yummy += a + b
      pi -= 1
      qi -= 1
      i += 2
    else:
      if a < b:
        yummy += c + b
        qi -= 1
        ri -= 1
        i += 1
      else:
        yummy += a + c
        pi -= 1
        ri -= 1
        i += 1
  elif pi > A - X - 1:
    a = p[pi]
    c = r[ri]
    if a < c:
      yummy += c
      ri -= 1
      i += 1
    else:
      yummy += a
      pi -= 1
      i += 1
  elif qi > B - Y - 1:
    b = q[qi]
    c = r[ri]
    if b < c:
      yummy += c
      ri -= 1
      i += 1
    else:
      yummy += b
      qi -= 1
      i += 1

print(yummy)

Submission status: Wrong Answer

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

from bisect import insort

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

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

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

sum_num = 0

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

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
P=sorted(list(map(int,input().split())))[::-1]
Q=sorted(list(map(int,input().split())))[::-1]
R=sorted(list(map(int,input().split())))[::-1]+[0]*(max(x+y-c,0))
L=sorted(P[:x]+Q[:y])[::-1]
print(L)
print(R)
l=r=0
for i in range(x+y): 
    if r+R[i]<l+L[-i-1]:
        print(sum(L[:x+y-i]+sorted(L[x+y-i:]+R[:i])[::-1][:i]))
        exit()
    r+=R[i]
    l+=L[-i-1]
print(r)

Submission status: Wrong Answer

Solution code:
import heapq as hp
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()))

lp = hp.nlargest(x, p)
lq = hp.nlargest(y, q)
pq = lp + lq
lpq = hp.nlargest(x+y, pq)
lr = hp.nlargest(c, r)
d = min(len(lr),len(lpq))
for i in range(1,d):
  if lpq[x+y-i] < lr[i-1]:
    lpq[x+y-i] = lr[i-1]
  else:
    break

ans = 0
for i in range(0,len(lpq)):
  ans += lpq[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(reverse=True)
pi = 0
qi = 0
ri = 0
pl = len(p)
ql = len(q)
rl = len(r)
scr = 0
for j in range(X+Y):
  mx = max(p[pi],q[qi],r[ri])
  scr += mx
  if mx == p[pi]:
    A -= 1
    if pi+1 >= pl or A <= 0:
      p.append(0)
      pi = -1
      A = 0
    else:
      pi += 1
  elif mx == q[qi]:
    B -= 1
    if qi+1 >= ql or B <= 0:
      q.append(0)
      qi = -1
      B = 0
    else:
      qi += 1
  else:
    if ri+1 >= rl or C <= 0:
      r.append(0)
      ri = -1
      C = 0
    else:
      ri += 1
      
print(scr)

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):
    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 sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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_idx = 0
q_idx = 0
r_idx = 0

p = P[p_idx]
q = Q[q_idx]
r = R[r_idx]


ans = 0
while X > 0 or Y > 0:
    #print("X=", X, "Y=", Y)
    #print("p,q,r", p, q, r)
    #print("ans", ans)
    #print("p,q,r,X,Y,ans", p, q, r, X, Y, ans)

    m = max(p, q, r)

    if m == -1:
        break

    if m == r:
        ans += r
        r_idx += 1

        if r_idx == C:
            r = -1
        else:
            r = R[r_idx]

        lp = p_idx + X - 1
        lq = q_idx + Y - 1

        if lp <= A-1 and lq <= B-1:
            p_l = P[lp]
            q_l = Q[lq]

            if q_l <= p_l:
                Y -= 1
            else:
                X -= 1
        elif B-1 < lq:
            Y -= 1
        else:
            X -= 1

    elif m == p:
        ans += p
        p_idx += 1
        X -= 1

        if p_idx == A or X == 0:
            p = -1
        else:
            p = P[p_idx]
    elif m == q:
        ans += q
        q_idx += 1
        Y -= 1

        if q_idx == B or Y == 0:
            q = -1
        else:
            q = Q[q_idx]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
INF = 10 ** 10
MOD = 10 ** 9 + 7
from heapq import heapify,heappop,heappush
input = sys.stdin.readline

def main():
    x,y,a,b,c = map(int,input().split())
    R = list(map(int,input().split()))
    G = list(map(int,input().split()))
    M = list(map(int,input().split()))
    i,j,k = 0,0,0
    R.sort(reverse = True)
    G.sort(reverse = True)
    M.sort(reverse = True)
    
    ans = 0
    while i < x and j < y and k + i + j <= x + y and k < c:
        tmp = max(R[i],G[j],M[k])
        ans += tmp
        if tmp == R[i]:
            i += 1
        elif tmp == G[j]:
            j += 1
        else:
            k += 1
    
    if i + j + k == x + y:
        print(ans)
        return
    
    elif k == c:
        while i < x and j < y and i + j+ c <= x + y:
            tmp = max(R[i],G[j])
            ans += tmp
            if tmp == R[i]:
                i += 1
            elif tmp == G[j]:
                j += 1

    if i + j + k == x + y:
        print(ans)
        return            

    elif i == x:
        t = y - k - j
        for _ in range(t):
            if k < c and G[j] < M[k]:
                ans += M[k]
                k += 1
            else:
                ans += G[j]
                j += 1
    elif j == y:
        t = x - i - k
        for _ in range(t):
            if k < c and R[i] < M[k]:
                ans += M[k]
                k += 1
            else:
                ans += R[i]
                i += 1


    
    print(ans)

if __name__=='__main__':
    main()

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)
s=0

for i in range(min(x,y)):
    s=s+max(p[0],q[0])
    if p[0]>=q[0]:
        del p[0]
        s=s+max(q[0],r[0])
        if q[0]>=r[0]:
            del q[0]
        else:
            if len(r)==1:
                r[0]=0
            else:
                del r[0]
    else:
        del q[0]
        s=s+max(p[0],r[0])
        if p[0]>=r[0]:
            del p[0]
        else:
            if len(r)==1:
                r[0]=0
            else:
                del r[0]

if x>=y:
    for i in range(x-y):
        s=s+max(p[0],r[0])
        if p[0]>=r[0]:
            del p[0]
        else:
            if len(r)==1:
                r[0]=0
            else:
                del r[0]
else:
    for i in range(y-x):
        s=s+max(q[0],r[0])
        if q[0]>=r[0]:
            del q[0]
        else:
            if len(r)==1:
                r[0]=0
            else:
                del r[0]       
print(s)

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().split())
apples = [(0, int(x)) for x in input().split()]
apples.extend((1, int(x)) for x in input().split())
apples.extend((2, int(x)) for x in input().split())
apples.sort(key=lambda t: t[1], reverse=True)

red = 0
green = 0
for a in apples[:X + Y]:
    if a[0] == 0:
        red += 1
    elif a[0] == 1:
        green += 1

eat_red = 0
eat_green = 0
score = 0
for a in apples:
    if a[0] == 0 and eat_red < X:
        eat_red += 1
        red -= 1
    elif a[0] == 1 and eat_green < Y:
        eat_green += 1
        green -= 1
    elif a[0] == 2:
        if eat_red < X and eat_green == Y:
            eat_red += 1
        elif eat_red == X and eat_green < Y:
            eat_green += 1
        else:
            if red > green:
                eat_green += 1
                green -= 1
            else:
                eat_red += 1
                red -= 1

    if eat_red == X and eat_green == Y:
        break

    score += a[1]

print(score)

Submission status: Wrong Answer

Solution code:
def inputs(): return [int(x) for x in input().split()]
from collections import defaultdict
import bisect
x,y,a,b,c = inputs()
p = inputs()
p.sort()
p = p[::-1]
p = p[:x]
p = p[::-1]
q = inputs()
q.sort()
q = q[::-1]
q=q[:y]
q = q[::-1]
r = inputs()
r.sort()
r = r[::-1]
ans = 0
for i in r:
    if i<=p[0] and i<=q[0]:
        break
    red = bisect.bisect_left(p,i)
    green = bisect.bisect_left(q,i)
    if q[green-1]<p[red-1]:
        q[green-1]=i
    else:
        p[red-1]=i
print(sum(p)+sum(q))

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(p)
q=sorted(q)
r=sorted(r)
p=p[(a-x):]
q=q[(b-y):]
res=sum(p)+sum(q)
tp=0
tq=0
tr=c-1
while 1:
  if(tr<0 or tp>x or tq>y):
    break
  if(tp>=x):
    pp=0
  else:
    pp=p[tp]
  if(tq>=y):
    qq=0
  else:
    qq=q[tq]

  if(pp>=r[tr] and qq>=r[tr]):
    break
  if(tr>=0 and tp<x and pp<qq ):
    res+=r[tr]-pp
    tp+=1
  elif(tr>=0 and tq<y):
    res+=r[tr]-qq
    tq+=1
  tr-=1
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
x,y,a,b,c = map(int,input().split())
P = deque(sorted(list(map(int,input().split())),reverse=True)[:x])
Q = deque(sorted(list(map(int,input().split())),reverse=True)[:y])
R = deque(sorted(list(map(int,input().split())),reverse=True))
p = P.pop()
q = Q.pop()
r = R.popleft()
ans = 0
while (p < r or q < r) and r != 0:
    ans += r
    if len(R) != 0:   
        r = R.pop()
    else:
        r = 0
    if p < q:
        if len(P) != 0:   
            p = P.pop()
        else:
            p = 10**10
    else:
        if len(Q) != 0:   
            q = Q.pop()
        else:
            q = 10**10
            
ans += sum(P)+sum(Q)
if p != 10**10:
    ans += p
if q != 10**10:
    ans += q
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
x,y,a,b,c=[int(i) for i in input().split()]
p=[[int(i),1] for i in input().split()]
q=[[int(i),2] for i in input().split()]
r=[[int(i),0] for i in input().split()]
ans=[]
ans.extend(p)
ans.extend(q)
ans.extend(r)
ans.sort(key= lambda x: x[0],reverse=True)
rans=0 ; 
ap=[0,0,0]
for i in range(a):
    ap[0]+=p[i][0]
for i in range(b):
    ap[1]+=q[i][0]
for i in range(c):
    ap[2]+=r[i][0]
for i in ans:
    if x+y==0: break
    if i[1]==1:
        if x==0: continue
        x-=1
        a-=1
        rans+=i[0]
        ap[0]-=i[0]
    if i[1]==2:
        if y==0: continue
        y-=1
        b-=1
        rans+=i[0]
        ap[2]-=i[0]
    if i[1]==0:
        if ap[0]>ap[2]:
            x-=1
            rans+=i[0]
            ap[0]-=i[0]
        else:
            y-=1
            rans+=i[0]
            ap[2]-=i[0]
        c-=1
print(rans)

Submission status: Wrong Answer

Solution code:
x, y, na, nb, nc = map(int, input().split()) 
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
a = a[:x]
b = b[:y]

ai = len(a) - 1
bi = len(b) - 1
print(a, b)
print(ai, bi)
for i in c:
    if a[ai] < b[bi]:
        if i > a[ai] and ai >= 0:
            a[ai] = i
            ai -= 1
        elif i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        else:
            break
    else:
        if i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        elif i > a[ai] and ai >= 0:
            a[ai] = i
            ai -= 1
        else:
            break
print(sum(a)+sum(b))