Problem:


Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74



Sample input:
1 2 2 2 1
2 4
5 1
3


Sample output:
12

Submission status: Runtime Error

Solution code:
# Problem E - Red and Green Apples

from collections import deque

# input
X, Y, A, B, C = map(int, input().split())
red_list = list(map(int, input().split())) # red apple
green_list = list(map(int, input().split())) # green apple
transparent_list = list(map(int, input().split())) # transparent apple

# initialization
red_list = deque(sorted(red_list, reverse=True))
green_list = deque(sorted(green_list, reverse=True))
transparent_list = deque(sorted(transparent_list, reverse=True))
ans_list = deque([])

# count
while X>0 or Y>0:
    red = -1
    green = -1
    toumei = -1
    if C>=0:
        toumei = transparent_list[0]
    max_v = 0

    if X>0:
        red = red_list[0]

    if Y>0:
        green = green_list[0]

    # max value check
    if red>=green and red>=toumei:
        max_v = red_list.popleft()
        X -= 1
    elif green>=red and green>=toumei:
        max_v = green_list.popleft()
        Y -= 1
    else:
        # min value check
        if X>0 and Y>0:
            if red<=green:
                max_v = transparent_list.popleft()
                X -= 1
                C -= 1
                red_list.popleft()
            else:
                max_v = transparent_list.popleft()
                Y -= 1
                C -= 1
                green_list.popleft()
        elif X>0:
            max_v = transparent_list.popleft()
            X -= 1
            C -= 1
            red_list.popleft()
        else:
            max_v = transparent_list.popleft()
            Y -= 1
            C -= 1
            green_list.popleft()

    # list insert
    ans_list.append(max_v)

# output
ans = sum(ans_list)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
#from collections import deque
input = sys.stdin.buffer.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)
ans = p[:x]
ans[x:x] = q[:y]
ans=sorted(ans)
#print(ans)
i = 0
result = 0
for A in ans:
    if A < r[i]:
        result += r[i]
        if i < c:
            i += 1
        continue
    result += A
print(result)

Submission status: Runtime Error

Solution code:
import sys
#from collections import deque
input = sys.stdin.buffer.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)
ans = p[:x]
ans[x:x] = q[:y]
ans=sorted(ans,reverse=True)
i = 0
result = 0
for A in ans:
    if A < r[i]:
        result += r[i]
        i += 1
        continue
    result += A
print(result)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split())) + [-1]
q = list(map(int, input().split())) + [-1]
r = list(map(int, input().split())) + [-1]
p.sort()
q.sort()
r.sort()

summ = 0
Z = 0
while X + Y - Z:
    if r[-1] * (C - Z > 0) < p[-1] * (X > 0) > q[-1] * (Y > 0):
        summ += p.pop()
        X -= 1
    elif p[-1] * (X > 0) < q[-1] * (Y > 0) > r[-1] * (C - Z > 0):
        summ += q.pop()
        Y -= 1
    else:
        summ += r.pop()
        Z += 1

print(summ)

Submission status: Runtime Error

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

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

# step1: sort red/green apples in descending order
p.sort(reverse=True)
q.sort(reverse=True)

# step2:clip necessary apples
p = p[:x]
q = q[:y]

# step3: sort red/green apples in ascending order
p.sort()
q.sort()

# step4: sort non-color apples in descending order
r.sort(reverse=True)

# step5:swap apples to eliminate the least delicious apple
r_idx = g_idx = 0
for idx in range(c):

    if r_idx >= x or g_idx >= y:
        break

    if r[idx] < p[r_idx] and r[idx] < q[g_idx]:
        break

    if r[idx] > p[r_idx] and r[idx] > q[g_idx]:
        if p[r_idx] < q[g_idx]:
            p[r_idx], r[idx] = r[idx], p[r_idx]
            r_idx += 1
        else:
            q[g_idx], r[idx] = r[idx], q[g_idx]
            g_idx += 1

    if r[idx] > p[r_idx]:
        p[r_idx], r[idx] = r[idx], p[r_idx]
        r_idx += 1

    if r[idx] > q[g_idx]:
        q[g_idx], r[idx] = r[idx], q[g_idx]
        g_idx += 1

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

Submission status: Runtime Error

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

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

# step1: sort red/green apples in descending order
p.sort(reverse=True)
q.sort(reverse=True)

# step2:clip necessary apples
p = p[:x]
q = q[:y]

# step3: sort red/green apples in ascending order
p.sort()
q.sort()

# step4: sort non-color apples in descending order
r.sort(reverse=True)

# step5:swap apples to eliminate the least delicious apple
r_idx = g_idx = 0
for idx in range(c):
    if r[idx] < p[r_idx] and r[idx] < q[g_idx]:
        break

    if r[idx] > p[r_idx] and r[idx] > q[g_idx]:
        if p[r_idx] < q[g_idx]:
            p[r_idx], r[idx] = r[idx], p[r_idx]
            r_idx += 1
        else:
            q[g_idx], r[idx] = r[idx], q[g_idx]
            g_idx += 1

    if r[idx] > p[r_idx]:
        p[r_idx], r[idx] = r[idx], p[r_idx]
        r_idx += 1

    if r[idx] > q[g_idx]:
        q[g_idx], r[idx] = r[idx], q[g_idx]
        g_idx += 1

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

Submission status: Runtime Error

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

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

x, y, a, b, c = map(int, input().split())
p = rl()
q = rl()
r = rl()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
res = 0
i = j = k = 0
cp = cq = 0
while cp < x or cq < y:
    if cp < x:
        if r[k] > p[i]:
            res += r[k]
            k += 1
        else:
            res += p[i]
            i += 1
        cp += 1
    if cq < y:
        if r[k] > q[j]:
            res += r[k]
            k += 1
        else:
            res += q[j]
            j += 1
        cq += 1
print(res)

Submission status: Runtime Error

Solution code:
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p=p.sort()
q=q.sort()
r=r.sort()
p=p[:x+1]
q=q[:y+1]
pqr=[]
pqr=p+q+r
pqr=pqr.sort()
print(sum(pqr[:x+y]))

Submission status: Runtime Error

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,Q = p[:X],q[:Y]

for i in range(C):
  if min(P[-i-1],Q[-i-1]) > r[i]:
    break
  elif min(P[-i-1],Q[-i-1]) <= r[i]:
    if P[-i-1] < Q[-i-1]:
      P[-i-1] = r[i]
    else:
      Q[-i-1] = r[i]
print(sum(P)+sum(Q))

Submission status: Runtime Error

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

red_apples = sorted([int(i) for i in input().split()])
green_apples = sorted([int(i) for i in input().split()])
no_apples = sorted([int(i) for i in input().split()])[::-1]
ans = sum(red_apples[::-1][:x]) + sum(green_apples[::-1][:y])

i = 0
j = 0
k = 0
# 赤と緑の小さいやつを無職のりんごの大きいもので置き換えればよさそう
for i, n in enumerate(no_apples):
    # print(green_apples[j])
    if red_apples[i] < green_apples[j] and red_apples[i] < n:
        ans -= red_apples[i] + n
        i += 1
        # print('hoge')
    elif green_apples[j] < red_apples[i] and green_apples[j] < n:
        # print('ans', ans)
        ans = ans - green_apples[j] + n
        # print('fuga', j, green_apples[j], n, ans)
        j += 1
    if i == x + y - 1:
        break
    # print(n, ans)
print(ans)

Submission status: Runtime Error

Solution code:
x,y,a,b,c=map(int,input().split())
AC=[None]*(a+b+c)
for i in range(a):
  AC[i]=(int(input()),1)
for i in range(a,a+b):
  AC[i]=(int(input()),2)
for i in range(a+b,a+b+c):
  AC[i]=(int(input()),0)
AC.sort(reverse=1)

red=0
green=0
cnt=0
res=0
for a,c in AC:
  if c==0:
    res+=a
    cnt+=1
  elif c==1:
    if red<x:
      res+=a
      red+=1
      cnt+=1
  else:
    if green<y:
      res+=a
      green+=1
      cnt+=1
  if cnt==x+y:
    print(res)
    exit(0)

Submission status: Runtime Error

Solution code:
#from typing import MutableSequence

def qsort(a: int, left: int, right: int) -> None:
  pl=left
  pr=right
  x=a[(left+right)//2]

  while pl<=pr:
    while a[pl]>x: pl+=1    # 降順
    while a[pr]<x: pr-=1
    if pl <= pr:
      a[pl],a[pr] = a[pr],a[pl]
      pl+=1
      pr-=1
  
  if left  <pr: qsort(a,left,pr)
  if right >pl: qsort(a,pl,right)
    
def quick_sort(a: int) -> None:
  qsort(a,0,len(a)-1)
  
if __name__ == '__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()))

  quick_sort(p)
  quick_sort(q)
  quick_sort(r)
  print(p)
  print(q)

  j=0
  k=0
  for i in range(C):
    if p[X-j-1] < r[i] and  q[Y-k-1] < r[i]:
      if q[Y-k-1] < p[X-j-1]:
        q[Y-k-1]=r[i]
        k+=1
      else:
        p[X-j-1]=r[i]
        j+=1
    elif p[X-j-1] < r[i]:
      p[X-j-1]=r[i]
      j+=1
    elif q[Y-k-1] < r[i]:
      q[Y-k-1] = r[i]
      k+=1
    
    print(i)
    print(p)
    print(q)
      
  x=0
  for i in range(X):
    x+=p[i]
  for i in range(Y):
    x+=q[i]
  print(x)

Submission status: Runtime Error

Solution code:
from collections import deque
x,y,a,b,c = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
#入力を受け取った
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
d = deque(r)
#ソートした。rはdequeにした
aka = x-1#赤いリンゴのうち、交換するか調べるもの
midori = y-1#緑リンゴ同上
while True:
  siraberu = d.popleft()#無職リンゴのうち一番美味しいものを取り出す
  if min(p[aka],q[midori]) < siraberu:#調べるリンゴのうちのまずい方がsiraberuよりまずいか
    if p[aka] <= q[midori]:#調べる赤リンゴの方がまずいとき
      p[aka] = siraberu#交換
      aka = aka - 1#調べるものを一個ずらす
    else:
      q[midori] = siraberu
      midori = midori - 1
  if aka == -1:
    aka = 0
  elif midori == -1:
    midori = 0
  else:
    break
pd = deque(p)
qd = deque(q)
ans = 0
for i in range(x):
  ans = ans + pd.popleft()
for i in range(y):
  ans = ans + qd.popleft()
print(ans)

Submission status: Runtime Error

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

 P = sorted(P)
 Q = sorted(Q)
 R = sorted(R)

 
A= P[:X] + Q[:Y] + R
A = sorted(A)
print(sum(A[:X+Y]))

Submission status: Runtime Error

Solution code:
from heapq import heappop, heappush

x, y, a, b, c = map(int, input().split())
apple = []
for p in map(int, input().split()):
  heappush(apple, (-p, True))
for q in map(int, input().split()):
  heappush(apple, (-q, False))
for r in map(int, input().split()):
  heappush(apple, (-r, None))
delicious = 0
colorless = 0
while x + y > colorless:
  minus_taste, color = heappop(apple)
  if color is True:
    if x > 0:
      delicious -= minus_taste
      x -= 1
  elif color is False:
    if y > 0:
      delicious -= minus_taste
      y -= 1
  else:
    delicious -= minus_taste
    colorless += 1
print(delicious)

Submission status: Runtime Error

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

p.sort()
q.sort()
r.sort()
ans = 0
#red
for i in range(X):
	if p[-1] >= r[-1]:
		ans += p.pop(-1)
	else:
		ans += r.pop(-1)
#green
for i in range(Y):
	if q[-1] >= r[-1]:
		ans += q.pop(-1)
	else:
		ans += r.pop(-1)

print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
X,Y,A,B,C = [int(x) for x in stdin.readline().rstrip().split()]
P = [int(x) for x in stdin.readline().rstrip().split()]
Q = [int(x) for x in stdin.readline().rstrip().split()]
R = [int(x) for x in stdin.readline().rstrip().split()]
 
P.sort()
Q.sort()
R.sort()
 
P = P[A-X:]
Q = Q[B-Y:]

ans = 0
while True:
    if P and Q:
        if P[-1] > Q[-1] and X > 0:
            if P[-1] > R[-1]:
                ans += P.pop()
            else:
                ans += R.pop()
            X -= 1
        else:
            if Q[-1] > R[-1]:
                ans += Q.pop()
            else:
                ans += R.pop()
            Y -= 1
    elif P and R:
        if P[-1] > R[-1]:
                ans += P.pop()
        else:
            ans += R.pop()
        X -= 1
    elif Q and R:
        if Q[-1] > R[-1]:
                ans += Q.pop()
        else:
            ans += R.pop()
        Y -= 1
    elif P:
        ans += P.pop()
        X -= 1
    elif Q:
        ans += Q.pop()
        Y -= 1
        
    if X == 0 and Y == 0:
            break
print(ans)

Submission status: Runtime Error

Solution code:
import sys
X, Y, A, B, C = sys.stdin.readline().split()
X = int(X)
Y = int(Y)
C = int(C)
scoreA = map(int, sys.stdin.readline().split())
scoreB = map(int, sys.stdin.readline().split())
scoreC = map(int, sys.stdin.readline().split())
scoreA.sort(reverse=True)
scoreB.sort(reverse=True)
scoreC.sort(reverse=True)
scoreA = scoreA[:X-1]
scoreB = scoreB[:Y-1]
scoreAll = scoreA + scoreB + scoreC
scoreAll.sort(reverse=True)
print(sum(scoreAll[:X+Y-1]))

Submission status: Runtime Error

Solution code:
x,y,a,b,c = map(int,input().split())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
p.sort(reverse=True)
q.sort(reverse=True)
for i in range(x): r.append(p[i])
for i in range(y): r.append(q[i])
r.sort(reverse=True)
print(sum(r[:x+y])

Submission status: Runtime Error

Solution code:
    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.append(0)
    B.append(0)
    C.append(0)
    ux_1 = 0
    uy_1 = 0
    for i in range(X):
        ux_1 += max(max(A),max(C))
        if max(A) >= max(C):
            A.pop(A.index(max(A)))
        else:
            C.pop(C.index(max(C)))
    for i in range(Y):
        uy_1 += max(max(B),max(C))
        if max(B) >= max(C):
            B.pop(B.index(max(B)))
        else:
            C.pop(C.index(max(C)))
    print(ux_1+uy_1)

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

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

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(r):
    if(i<len(p) and j<len(q)):
        if(val<=p[i] and val <= q[j]):
            break
        if(val>p[i] and val<q[j]):
            res=res+val-p[i]
            i=i+1
        if(val<p[i] and val>q[j]):
            res=res+val-q[j]
            j=j+1
        if(val>p[i] and val>q[j]):
            if(p[i]>q[j]):
                res=res+val-q[j]
                j=j+1
            else:
                res=res+val-p[i]
                i=i+1
    if(i<len(p) and j==len(q)):
        if(val<=p[i]):
            break
        if(val>p[i]):
            res=res+val-p[i]
            i=i+1
    if(i==len(p) and j<len(q)):
        if(val<=q[j]):
            break
        if(val>q[j]):
            res=res+val-q[j]
            j=j+1
    if(i==len(p) and j==len(q)):
        break
        
print(res)

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

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

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(R):
    if(i<len(p) and j<len(q)):
        if(val<=p[i] and val <= q[j]):
            break
        if(val>p[i] and val<q[j]):
            res=res+val-p[i]
            i=i+1
        if(val<p[i] and val>q[j]):
            res=res+val-q[j]
            j=j+1
        if(val>p[i] and val>q[j]):
            if(p[i]>q[j]):
                res=res+val-q[j]
                j=j+1
            else:
                res=res+val-p[i]
                i=i+1
    if(i<len(p) and j==len(q)):
        if(val<=p[i]):
            break
        if(val>p[i]):
            res=res+val-p[i]
            i=i+1
    if(i==len(p) and j<len(q)):
        if(val<=q[j]):
            break
        if(val>q[j]):
            res=res+val-q[j]
            j=j+1
    if(i==len(p) and j==len(q)):
        break
        
print(res)

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

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

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(r):
    if(val<=p[i] and val <= q[j]):
        break
    if(val>p[i] and val<q[j]):
        res=res+val-p[i]
        i=i+1
    if(val<p[i] and val>q[j]):
        res=res+val-q[j]
        j=j+1
    if(val>p[i] and val>q[j]):
        if(p[i]>q[j]):
            res=res+val-q[j]
            j=j+1
        else:
            res=res+val-p[i]
            i=i+1
        
print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
def main():
    X, Y, A, B, C = map(int, input().split())
    
    red = deque(sorted(map(int, input().split()), reverse=True))
    green = deque(sorted(map(int, input().split()), reverse=True))
    nocolor = deque(sorted(map(int, input().split()), reverse=True))

    INF = 10 ** 9 + 1
    times = A + B + C - (X + Y)
    for _ in range(times):
        red_num = len(red)
        green_num = len(green)
        nocolor_num = len(nocolor)

        if red_num and red_num + nocolor_num > X:
            r = red[-1]
        else:
            r = INF
        
        if green_num and green_num + nocolor_num > Y:
            g = green[-1]
        else:
            g = INF
        
        if nocolor_num:
            no = nocolor[-1]
        else:
            no = INF
        
        l = [no, r, g]
        idx = l.index(min(l))
        if idx == 0:
            red.pop()
        if idx == 1:
            green.pop()
        if idx == 2:
            nocolor.pop()

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

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

p_list = list(reversed(sorted(p_list)))
p_list = p_list[:X]
# print(X, p_list)

q_list = list(reversed(sorted(q_list)))
q_list = q_list[:Y]
# print(Y, q_list)

r_list = list(reversed(sorted(r_list)))
# print(r_list)


p_i = -1
q_i = -1
r_i = 0

while True:
    if r_i == C:
        break

    if p_i + A >= 0 and q_i + B >= 0:
        if p_list[p_i] <= q_list[q_i]:
            if p_list[p_i] < r_list[r_i]:
                p_list[p_i] = r_list[r_i]
                p_i -= 1
                r_i += 1
            else:
                break
        elif q_list[q_i] <= p_list[p_i]:
            if q_list[q_i] < r_list[r_i]:
                q_list[q_i] = r_list[r_i]
                q_i -= 1
                r_i += 1
            else:
                break
    elif p_i + A >= 0:
        if p_list[p_i] < r_list[r_i]:
            p_list[p_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
        else:
            break
    elif q_i + B >= 0:
        if q_list[q_i] < r_list[r_i]:
            q_list[q_i] = r_list[r_i]
            q_i -= 1
            r_i += 1
        else:
            break
    else:
        break

d = 0
d += sum(p_list)
d += sum(q_list)

print(d)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p_eat = sorted(p, reverse=True)[:x]
q_eat = sorted(q, reverse=True)[:y]
r_eat = sorted(r, reverse=True)

def nap(x_rem, y_rem, x_i, y_i, r_i, sum_yammy):
    if x_rem == 0 and y_rem == 0:
        return sum_yammy
    elif x_rem == 0:
        if y_i < y:
            nap1 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        else:
            nap1 = 0
        if r_i < c:
            nap2 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return max([nap1, nap2])
    elif y_rem == 0:
        if x_i < x:
            nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        else:
            nap1 = 0
        if r_i < c:
            nap2 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return max([nap1, nap2])
    else:
        if x_i < x:
            nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        else:
            nap1 = 0
        if y_i < y:
            nap2 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        else:
            nap2 = 0
        if r_i < c:
            nap3 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
            nap4 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap3, nap4 = 0, 0
        return max([nap1, nap2, nap3, nap4])

print(nap(x, y, 0, 0, 0, 0))

Submission status: Runtime Error

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

p_list = list(reversed(sorted(p_list)))
p_list = p_list[:X]
# print(X, p_list)

q_list = list(reversed(sorted(q_list)))
q_list = q_list[:Y]
# print(Y, q_list)

r_list = list(reversed(sorted(r_list)))
# print(r_list)

p_i = -1
q_i = -1
r_i = 0

while True:
    if p_i + A >= 0 and p_list[p_i] < q_list[q_i]:
        if p_list[p_i] < r_list[r_i]:
            p_list[p_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
            if r_i == C:
                break
        else:
            break
    elif q_i + B >= 0 and q_list[q_i] < p_list[p_i]:
        if q_list[q_i] < r_list[r_i]:
            q_list[q_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
            if r_i == C:
                break
        else:
            break
    else:
        break

d = 0
d += sum(p_list)
d += sum(q_list)

print(d)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p_eat = sorted(p, reverse=True)[:x]
q_eat = sorted(q, reverse=True)[:y]
r_eat = sorted(r, reverse=True)

def nap(x_rem, y_rem, x_i, y_i, r_i, sum_yammy):
    if x_rem == 0 and y_rem == 0:
        return sum_yammy
    elif x_rem == 0:
        if y_i < y:
            nap1 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        else:
            nap1 = 0
        if r_i < c:
            nap2 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return nap1 if nap1>nap2 else nap2
    elif y_rem == 0:
        if x_i < x:
            nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        else:
            nap1 = 0
        if r_i < c:
            nap2 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return nap1 if nap1>nap2 else nap2
    else:
        if x_i < x:
            nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        else:
            nap1 = 0
        if y_i < y:
            nap2 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        else:
            nap2 = 0
        if r_i < c:
            nap3 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
            nap4 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap3, nap4 = 0, 0
        return max([nap1, nap2, nap3, nap4])

print(nap(x, y, 0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p_eat = sorted(p, reverse=True)
q_eat = sorted(q, reverse=True)
r_eat = sorted(r, reverse=True)

def nap(x_rem, y_rem, x_i, y_i, r_i, sum_yammy):
    if x_rem == 0 and y_rem == 0:
        return sum_yammy
    elif x_rem == 0:
        nap1 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        if r_i < c:
            nap2 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return nap1 if nap1>nap2 else nap2
    elif y_rem == 0:
        nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        if r_i < c:
            nap2 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return nap1 if nap1>nap2 else nap2
    else:
        nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        nap2 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        if r_i < c:
            nap3 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
            nap4 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap3, nap4 = 0, 0
        return max([nap1, nap2, nap3, nap4])

print(nap(x, y, 0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p_eat = sorted(p, reverse=True)[:x]
q_eat = sorted(q, reverse=True)[:y]
r_eat = sorted(r, reverse=True)

def nap(x_rem, y_rem, x_i, y_i, r_i, sum_yammy):
    if x_rem == 0 and y_rem == 0:
        return sum_yammy
    elif x_rem == 0:
        nap1 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        if r_i < c:
            nap2 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return nap1 if nap1>nap2 else nap2
    elif y_rem == 0:
        nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        if r_i < c:
            nap2 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap2 = 0
        return nap1 if nap1>nap2 else nap2
    else:
        nap1 = nap(x_rem-1, y_rem, x_i+1, y_i, r_i, sum_yammy+p_eat[x_i])
        nap2 = nap(x_rem, y_rem-1, x_i, y_i+1, r_i, sum_yammy+q_eat[y_i])
        if r_i < c:
            nap3 = nap(x_rem-1, y_rem, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
            nap4 = nap(x_rem, y_rem-1, x_i, y_i, r_i+1, sum_yammy+r_eat[r_i])
        else:
            nap3, nap4 = 0, 0
        return max([nap1, nap2, nap3, nap4])

print(nap(x, y, 0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
def qsort(a: int, left: int, right: int) -> None:
  pl=left
  pr=right
  x=a[(left+right)//2]

  while pl<=pr:
    while a[pl]>x: pl+=1    # 降順
    while a[pr]<x: pr-=1
    if pl <= pr:
      a[pl],a[pr] = a[pr],a[pl]
      pl+=1
      pr-=1
  
  if left  <pr: qsort(a,left,pr)
  if right >pl: qsort(a,pl,right)
    
def quick_sort(a: int) -> None:
  qsort(a,0,len(a)-1)
  
if __name__ == '__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()))

  quick_sort(p)
  quick_sort(q)
  
  for i in range(C):
    if p[X-i-1] < r[i]:
      if q[Y-i-1] < p[X-i-1]:q[Y-i-1] = r[i]
      else:p[X-i-1] = r[i]
    else:  
      if q[Y-i-1] < r[i]:q[Y-i-1] = r[i]
  
  x=0
  for i in range(X):
    x+=p[i]
  for i in range(Y):
    x+=q[i]
  print(x)

Submission status: Runtime Error

Solution code:
def merge(l):
    t = [-1]*len(l)
    if(len(l) == 2):
        if(l[0]<l[1]):
            t[1] = l[0]
            t[0] = l[1]
        else:
            t=l
    elif(len(l) > 2):
        x = merge(l[:len(l)//2])
        y = merge(l[len(l)//2:])
        p,q = 0,0
        for i in range(len(l)):
            if(p<len(x) and q<len(y)):
                if(x[p] > y[q]):
                    t[i] = x[p]
                    p += 1
                else:
                    t[i] = y[q]
                    q += 1
            elif(p == len(x)):
                t[i] = y[q]
                q += 1
            else:
                t[i] = x[p]
                p += 1
    else:
        t = l
    return t
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()))
a = [x,y,0]
P = merge(p)
Q = merge(q)
R = merge(r)
X = P[:x]
Y = Q[:y]
for i in range(x+y):
    if(a[2] == c):
        break
    elif(a[0] == 0):
        if(Y[a[1]-1] < R[a[2]]):
                a[1] -= 1
                a[2] += 1
            else:
                break
    elif(a[1] == 0):
        if(X[a[0]-1] < R[a[2]]):
                a[0] -= 1
                a[2] += 1
            else:
                break
    else:
        if(X[a[0]-1] > Y[a[1]-1]):
            if(Y[a[1]-1] < R[a[2]]):
                a[1] -= 1
                a[2] += 1
            else:
                break
        else:
            if(X[a[0]-1] < R[a[2]]):
                a[0] -= 1
                a[2] += 1
            else:
                break
m = 0
for i in range(a[0]):
    m += P[i]
for i in range(a[1]):
    m += Q[i]
for i in range(a[2]):
    m += R[i]
print(m)