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: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
reds=sorted(list(map(int,input().split())))
greens=sorted(list(map(int,input().split())))
neutral=sorted(list(map(int,input().split())))

reds=reds[a-x:]
greens=greens[b-y:]
total=sorted(reds+greens+neutral)

print(sum(total[len(total)-x-y:]))

Submission status: Accepted

Solution code:
import sys
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)

ans = sum(P[:X]) + sum(Q[:Y])
x = X-1
y = Y-1
for z in range(C):
    if x < 0:
        if R[z] > Q[y]:
            ans += R[z] - Q[y]
            y -= 1
    elif y < 0:
        if R[z] > P[x]:
            ans += R[z] - P[x]
            x -= 1
    else:
        if R[z] > P[x] >= Q[y]:
            ans += R[z] - Q[y]
            y -= 1
        elif R[z] > Q[y] > P[x]:
            ans += R[z] - P[x]
            x -= 1
        elif P[x] >= R[z] > Q[y]:
            ans += R[z] - Q[y]
            y -= 1
        elif Q[y] >= R[z] > P[x]:
            ans += R[z] - P[x]
            x -= 1
print(ans)

Submission status: Accepted

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)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r = r + p
r = r + q
r = sorted(r)
print(sum(r[(len(r)-x-y):]))

Submission status: Accepted

Solution code:
from collections import deque
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
q.sort()
r.sort()
p = deque(p)
q = deque(q)
r = deque(r)
p.appendleft(0)
q.appendleft(0)
r.appendleft(0)


a = p.pop()
b = q.pop()
c = r.pop()
x = 0
y = 0
ans = 0
for i in range(X+Y):
    if x < X and y < Y:
        if a >= b and a >= c:
            ans += a
            x += 1
            a = p.pop()
        elif b >= c:
            ans += b
            y += 1
            b = q.pop()
        else:
            ans += c
            c = r.pop()
    elif x < X:
        if a >= c:
            ans += a
            x += 1
            a = p.pop()
        else:
            ans += c
            c = r.pop()
    else:
        if b >= c:
            ans += b
            y += 1
            b = q.pop()
        else:
            ans += c
            c = r.pop()

print(ans)

Submission status: Accepted

Solution code:
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()))
    t = [1]
    t.sort()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)

    s = P[:x] + Q[:y]
    s.sort()
    for i in range(x + y):
        if i < c:
            if s[i] < R[i]:
                s[i] = R[i]
            else:
                break
        else:
            break
    print(sum(s))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

RO=list(map(int,input().split()))
GO=list(map(int,input().split()))
TO=list(map(int,input().split()))

RO.sort()
GO.sort()

R=RO[A-X:A]
G=GO[B-Y:B]

TO.extend(R)
TO.extend(G)

TO.sort(reverse=True)

T=TO[0:X+Y]

print(sum(T))

Submission status: Accepted

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()))
IronMan = sorted(p)
CaptainAmerica = sorted(q)
for i in range(X):
    r.append(IronMan.pop())
for i in range(Y):
    r.append(CaptainAmerica.pop())
SpiderMan = sorted(r)[::-1]
print(sum(SpiderMan[:X+Y]))

Submission status: Accepted

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

new = sorted(r+g+w,reverse=True)
print(sum(new[:X+Y]))

Submission status: Accepted

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

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

AD = P[:X] + Q[:Y] + R
AD.sort(reverse=True)
print(sum(AD[:(X+Y)]))
exit(0)

### zanshi
ans = sum(P[:X]) + sum(Q[:Y])

pi, qi, ri = X-1, Y-1, 0
for _ in range(C+10):
  if P[pi] < R[ri] and P[pi] <= Q[qi]:
    ans += R[ri] - P[pi]
    pi, ri = pi-1, ri+1
  elif Q[qi] < R[ri] and P[pi] >= Q[qi]:
    ans += R[ri] - Q[qi]
    qi, ri = qi-1, ri+1
  if pi < 0 or qi < 0 or ri >= C:
    break
print(ans)

Submission status: Accepted

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

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 = sorted(p, reverse=True)
q = sorted(q, reverse=True)
r = sorted(r, reverse=True)

p = p[:x]
q = q[:y]

p_and_q = p + q

import heapq

heapq.heapify(p_and_q)

while r:
    temp = max(heapq.heappop(p_and_q), r.pop())
    heapq.heappush(p_and_q, temp)

print(sum(p_and_q))

Submission status: Accepted

Solution code:
#141dの応用
from collections import deque

X,Y,A,B,C = map(int, input().split())
p_list = [int(e) for e in input().split()]
q_list = [int(e) for e in input().split()]
r_list = [int(e) for e in input().split()]

p_list.sort(reverse=True)
q_list.sort(reverse=True)
r_list.sort(reverse=False)

#リストから高い方のみ取り出し
red_apples = p_list[0:min(A,X)]
green_apples = q_list[0:min(B,Y)]

colorless_apples = r_list

#print("R:",red_apples)
#print("G:",green_apples)
#print("*:",colorless_apples)
#print("########################")

#不足分をまず食べておく
if len(red_apples) < X:
    shortage = X - len(red_apples)
    red_apples += colorless_apples[0:shortage]
    del colorless_apples[0:shortage]
if len(green_apples) < Y:
    shortage = Y - len(green_apples)
    green_apples += colorless_apples[0:shortage]
    del colorless_apples[0:shortage]

#print("R:",red_apples)
#print("G:",green_apples)
#print("*:",colorless_apples)
#print("##########################")

eat_apples = red_apples + green_apples
eat_apples.sort(reverse=True)

#print("E:",eat_apples)
#print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")

lowest_apple = eat_apples.pop(-1)

replaced_deque = deque() #左高 右低

#replaced_deque | eat_apples | colorless_apples
#| 1 1 2 5 7 8 12 35 58 98 | 1 3 5 7 9
#3 | 1 2 5 7 8 12 35 58 98 | 3 5 7 9
#3 3 | 2 5 7 8 12 35 58 98 | 5 7 9
#3 3 5 | 5 7 8 12 35 58 98 | 7 9
#3 3 5 | 5 7 8 12 35 58 98 | 7 9
#3 5 7 | 5 7 8 12 35 58 98 | 9
#5 7 9 | 5 7 8 12 35 58 98 |

#リンゴを低い方から、残った無色に入れ替えた方がよければ入れ替えていく。入れ替える際はより低い方を選択する
for i in range(len(colorless_apples)):

    #print("eat_apples    :" ,eat_apples)
    #print("replaced_deque:" ,replaced_deque)
    #print("lowest        :" ,lowest_apple)
    #print("candidate     :" ,colorless_apples[i])
    #print("##########################")

    if colorless_apples[i] > lowest_apple:
        replaced_deque.appendleft(colorless_apples[i])

        if len(eat_apples) > 0:
            if replaced_deque[-1] < eat_apples[-1]:
                lowest_apple = replaced_deque.pop()
            else:
                lowest_apple = eat_apples.pop(-1)
        else:
            lowest_apple = replaced_deque.pop()

print(sum(eat_apples) + sum(replaced_deque) + lowest_apple)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
rs = list(map(int, input().split()))
ps = sorted(ps, reverse=True)[:X]
qs = sorted(qs, reverse=True)[:Y]
rs = sorted(ps+qs+rs, reverse=True)[:X+Y]
r = sum(rs)
print(r)

Submission status: Accepted

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

for _ in range(x):
    r.append(p.pop())

for _ in range(y):
    r.append(q.pop())

r.sort()
ans = 0

for _ in range(x+y):
    ans += r.pop()

print(ans)

Submission status: Accepted

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

print(sum(r[:x+y]))

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def Yes(): print("Yes"); return
def No(): print("No"); return

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def Base_10_to_n(X, n):
  if X//n:
    return Base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def Base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

X,Y,A,B,C = inputIL()
p = inputIL()+[0]*X
q = inputIL()+[0]*Y
r = inputIL()

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

p = p[:X]
q = q[:Y]

a = p+q+r

a.sort(reverse=True)

print(sum(a[:X+Y]))

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def Yes(): print("Yes"); return
def No(): print("No"); return

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def Base_10_to_n(X, n):
  if X//n:
    return Base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def Base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

X,Y,A,B,C = inputIL()
p = inputIL()+[0]*X
q = inputIL()+[0]*Y
r = inputIL()

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

p = p[:X]
q = q[:Y]

wp = p.pop()
wq = q.pop()

eat_p = []
eat_q = []

while C:
  br = r.pop()
  C -= 1
  if min(wp,wq) < br:
    if wp<wq:
      eat_p.append(br)
      if p:
        wp = p.pop()
      else:
        wp = INF
    else:
      eat_q.append(br)
      if q:
        wq = q.pop()
      else:
        wq = INF
        
eat_p += p
eat_q += q
if wp != INF:
  eat_p.append(wp)
if wq != INF:
  eat_q.append(wq)

print(sum(eat_p)+sum(eat_q))

Submission status: Accepted

Solution code:
X, Y, A, B, C = list(map(int,input().split()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
 
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
 
p.append(float("inf"))
q.append(float("inf")) 

Z = 0
while Z < C:
  if min(p[X - 1], q[Y - 1]) < r[Z]:
    if p[X - 1] < q[Y - 1]:
      X -= 1
      Z += 1
    else:
      Y -= 1
      Z += 1
    continue
  break
 
print(sum(p[:X]) + sum(q[:Y]) + sum(r[:Z]))

Submission status: Accepted

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)
P.append(float("inf"))
Q.append(float("inf"))
ans=sum(P[:X]+Q[:Y])
rfinish=False
gfinish=False
r,g,f=X-1,Y-1,0
while f<C and (R[f]>P[r] or R[f]>Q[g]):
    if P[r]<Q[g]:
        ans=ans-P[r]+R[f]
        r-=1
        f+=1
    else:
        ans=ans-Q[g]+R[f]
        g-=1
        f+=1
print(ans)

Submission status: Accepted

Solution code:
from heapq import heapify, heappop
from collections import deque

x, y, a, b, c = list(map(int, input().split(' ')))
p = list(map(int, input().split(' ')))
q = list(map(int, input().split(' ')))
r = list(map(lambda x: -1 * int(x), input().split(' ')))

p = sorted(p, reverse=True)
q = sorted(q, reverse=True)

s = p[:x] + q[:y]
s.sort()

heapify(r)

for i, _s in enumerate(s):
    if len(r) == 0:
        break

    n = -1 * heappop(r)
    if n > _s:
        s[i] = n

print(sum(s))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
P = sorted(P, reverse=True)[:X]
Q = sorted(Q, reverse=True)[:Y]
Ans = []
for p in P:
    Ans.append((p, 1))
for q in Q:
    Ans.append((q, 2))
for r in R:
    Ans.append((r, 3))
Ans.sort(reverse=True)

x = 0
y = 0
num = 0
ans = 0

for i, j in Ans:
    if j == 1:
        if x == X:
            continue
        else:
            x += 1
            num += 1
            ans += i
    elif j == 2:
        if y == Y:
            continue
        else:
            y += 1
            num += 1
            ans += i
    else:
        num += 1
        ans += i
    if num == X+Y:
        break
print(ans)

Submission status: Accepted

Solution code:
X, Y, R, G, N = map(int, input().split())
r = [int(x) for x in input().split()]
g = [int(x) for x in input().split()]
n = [int(x) for x in input().split()]
r = sorted(r)[R-X:]
g = sorted(g)[G-Y:]

apple = r + g + n
apple = sorted(apple)[len(apple)-(X+Y):]
print(sum(apple))

Submission status: Accepted

Solution code:
X, Y, R, G, N = map(int, input().split())
r = [int(x) for x in input().split()]
g = [int(x) for x in input().split()]
n = [int(x) for x in input().split()]
r = sorted(r)[R-X:]
g = sorted(g)[G-Y:]
n = sorted(n, reverse=True)

nr = 0; ng = 0; nn = 0
for i in range(N):
    if n[nn] <= min(r[nr], g[ng]):
        break
    if r[nr] < g[ng]:
        r[nr] = n[nn]
        nr += 1
        if nr == X:
            nr = X-1
    else:
        g[ng] = n[nn]
        ng += 1
        if ng == Y:
            ng = Y-1
    nn += 1

print(sum(r) + sum(g))

Submission status: Accepted

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

Submission status: Accepted

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)
result = sum(p[0:x]) + sum(q[0:y])
red = x-1
green = y-1
colorless = 0
#print(p,q,r)
while colorless < c:
	#print(red,green,colorless)
	if red >= 0 and green >= 0:
		if p[red] < q[green]:
			if r[colorless] > p[red]:
				result -= p[red]
				result += r[colorless]
				colorless += 1
				red -= 1
			else:
				break
		else:
			if r[colorless] > q[green]:
				result -= q[green]
				result += r[colorless]
				colorless += 1
				green -= 1
			else:
				break
	elif red >= 0:
		if r[colorless] > p[red]:
			result -= p[red]
			result += r[colorless]
			colorless += 1
			red -= 1
		else:
			break
	elif green >= 0:
		if r[colorless] > q[green]:
			result -= q[green]
			result += r[colorless]
			colorless += 1
			green -= 1
		else:
			break
print(result)

Submission status: Accepted

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)

C = P[:X] + Q[:Y] + R
C.sort(reverse=True)

print(sum(C[:X+Y]))

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(reversed(sorted(list(map(int, input().split())))))
q = list(reversed(sorted(list(map(int, input().split())))))
r = list(reversed(sorted(list(map(int, input().split())))))
del p[x:]
del q[y:]
l = list(reversed(sorted(p + q + r)))
print(sum(l[:x + y]))

Submission status: Accepted

Solution code:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
#      .      '                    Udit Gupta @luctivud         ,              
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                            ##     ##  #######  # #  ######
                            ##     ##  ##   ##  ###    ##
                            ##     ##  ##    #  # #    ##
                            #########  #######  # #    ##
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
import math
# sys.setrecursionlimit(10**6)

def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end="tst, ")

MODPRIME = int(1e9+7); BABYMOD = 998244353;
# ################################ HELPER PROGRAMS USED ###################################
# ################################## MAIN STARTS HERE #####################################
# for _testcases_ in range(int(input())):
x, y, a, b, c = get_ints()
la = get_array()
lb = get_array()
lc = get_array()
la.sort(reverse = True)
lb.sort(reverse = True)
for i in range(x):
    lc.append(la[i])
for i in range(y):
    lc.append(lb[i])
lc.sort(reverse = True)
sum = 0
for i in range(x+y):
    sum += lc[i]
print(sum)


# #########################################################################################
'''
THE LOGIC AND APPROACH IS BY ME @luctivud.
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
(I Own the code if no link is provided here or I may have missed mentioning it)
: DO NOT PLAGIARISE.
'''

Submission status: Accepted

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

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

p_rev = sorted(p, reverse=True)
q_rev = sorted(q, reverse=True)
pq_rev = sorted(p_rev[:x] + q_rev[:y] , reverse=True)

total = sum(sorted(pq_rev + r, reverse=True)[:x+y])
print(total)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    x, y, a, b, c = map(int, input().split())
    pqr = []
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    r = list(map(int, input().split()))
    for i in range(a):
        pqr.append([p[i], 1])
    for i in range(b):
        pqr.append([q[i], 2])
    for i in range(c):
        pqr.append([r[i], 0])
    pqr.sort(reverse=True)

    ans = 0
    c_a = 0
    c_b = 0
    c_c = 0
    for v, k in pqr:
        if k == 1 and c_a < x:
            ans += v
            c_a += 1
        elif k == 2 and c_b < y:
            ans += v
            c_b += 1
        elif k == 0 and c_a + c_b + c_c < x + y:
            ans += v
            c_c += 1
        if c_a + c_b + c_c == x + y:
            print(ans)
            exit()


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

x,y,a,b,c=MI()
ps=sorted(LI(),reverse=True)
qs=sorted(LI(),reverse=True)
rs=sorted(LI(),reverse=True)
ps=ps[0:x]
qs=qs[0:y]
u=rs+ps+qs
u.sort(reverse=True)
print(sum(u[0:x+y]))

Submission status: Accepted

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

p.sort(reverse=True)
q.sort(reverse=True)
p=p[:x]
q=q[:y]
r.extend(p)
r.extend(q)
r.sort(reverse=True)

ans=sum(r[:x+y])

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

X, Y, A, B, C = lr()
P = lr(); P.sort(reverse=True)
Q = lr(); Q.sort(reverse=True)
R = lr(); R.sort(reverse=True)
P = P[:X]
Q = Q[:Y]
Z = P + Q + R
Z.sort(reverse=True)
answer = sum(Z[:X+Y])
print(answer)

Submission status: Accepted

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)

l = p[:x]
l.extend(q[:y])
l.extend(r)
l.sort(reverse=True)
ans = sum(l[:x+y])

print(ans)

Submission status: Accepted

Solution code:
import heapq
def main():
    X, Y, A, B, C = (int(_) for _ in input().split())
    p = sorted([int(_) for _ in input().split()])[::-1]
    q = sorted([int(_) for _ in input().split()])[::-1]
    r = sorted([int(_) for _ in input().split()])[::-1]
    output = 0
    apple = list()
    heapq.heapify(apple)
    for i in range(X):
        heapq.heappush(apple, p[i])
        output += p[i]
    for i in range(Y):
        heapq.heappush(apple, q[i])
        output += q[i]
    i = 0
    while i < C:
        x = heapq.heappop(apple)
        if x < r[i]:
            heapq.heappush(apple, r[i])
            output = output - x + r[i]
            i += 1
        else: break

    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
import math
import sys

x,y,a,b,c = list(map(int,sys.stdin.readline().split()))
p = sorted(list(map(int,sys.stdin.readline().split())), reverse=True)
q = sorted(list(map(int,sys.stdin.readline().split())), reverse=True)
r = sorted(list(map(int,sys.stdin.readline().split())), reverse=True)
eat=[]

if x<len(p):
    [eat.append(red) for red in p[:x]]
else:
    [eat.append(red) for red in p]
if y<len(q):
    [eat.append(green) for green in q[:y]]
else:
    [eat.append(green) for green in q]
[eat.append(apple) for apple in r]
eat = sorted(eat, reverse=True)
#print(sum(eat[:x + y]))
ans = 0
for taste in eat[:x + y]:
    ans += taste 
print(ans)

Submission status: Accepted

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

all_apples = []
for i in range(max(X,Y,C)):
  if(i<X):
    all_apples.append([P[-(i+1)],'r'])
  if(i<Y):
    all_apples.append([Q[-(i+1)],'g'])
  if(i<C):
    all_apples.append([R[i],'any'])

all_apples.sort()

ans = 0
for i in range(X+Y):
 ans += all_apples[-(i+1)][0]
print(ans)

Submission status: Accepted

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,reverse=True)
q = sorted(q,reverse=True)
r = sorted(r,reverse=True)

lis = p[:x]
lis.extend(q[:y])

lis = sorted(lis)

#print(lis)
ans = []
lr = len(r)
for i in range(len(lis)):
    if i<lr:
        ans.append(max(lis[i],r[i]))
    else:
        ans.append(lis[i])

print(sum(ans))

Submission status: Accepted

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

tmp = sorted(map(int, input().split()), key=lambda x: -x)[:X] \
      + sorted(map(int, input().split()), key=lambda x: -x)[:Y] \
      + sorted(map(int, input().split()), key=lambda x: -x)

print(sum(sorted(tmp, key=lambda x: -x)[:X + Y]))

Submission status: Accepted

Solution code:
x,y,nr,ng,nm=map(int,input().split())
r=list(map(int,input().split()))
g=list(map(int,input().split()))
m=list(map(int,input().split()))
r.sort(reverse=True)
g.sort(reverse=True)
a=r[:x]+g[:y]+m
a.sort(reverse=True)
print(sum(a[:x+y]))

Submission status: Accepted

Solution code:
q=list(map(int,input().split()))
X=q[0]
Y=q[1]
A=q[2]
B=q[3]
C=q[4]
r=list(map(int,input().split()))
g=list(map(int,input().split()))
n=list(map(int,input().split()))
r=sorted(r,reverse=True)
g=sorted(g,reverse=True)
n=sorted(n,reverse=True)
A=[]
for i in range(X):
    A.append(r[i])
for i in range(Y):
    A.append(g[i])
A=sorted(A,reverse=True)
s=0
for i in range(min(len(A),len(n))):
    if A[-1]>=n[i]:
        s=i
        break
    A.pop(-1)
    s=i+1
for i in range(s):
    A.append(n[i])
print(sum(A))