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:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int(): return int(input())

def map_int_input(): return map(int, input())

MII = map_int_input

def MII_split(): return map(int, input().split())

def MII_to_index(): return map(to_index, input())

def MII_split_to_index(): return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs(): return list(map(int, input()))

LII = list_int_inputs

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

# 2次元リスト化
def LII_2D(rows_number): return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number): return [LII_split() for _ in range(rows_number)]

X, Y, A, B, C = MII_split()
p_list = LII_split()
q_list = LII_split()
r_list = LII_split()

prices = sorted(p_list, reverse=True)[0:X] + sorted(q_list, reverse=True)[0:Y] + r_list
prices = sorted(prices, reverse=True)
# print(prices)
print(sum(prices[0:X + Y]))

Submission status: Accepted

Solution code:
def main():
    x, y, a, b, c = map(int, input().split())
    al = sorted(list(map(int, input().split())), reverse=True)[:x]
    bl = sorted(list(map(int, input().split())), reverse=True)[:y]
    cl = sorted(list(map(int, input().split())), reverse=True)
    ans = 0
    xi, yi = 0, 0
    for i in cl:
        if al:
            ai = al[-1]
        else:
            ai = 10000000000
        if bl:
            bi = bl[-1]
        else:
            bi = 10000000000
        if ai < bi:
            if al:
                m = al.pop()
            else:
                m = 0
        else:
            if bl:
                m = bl.pop()
            else:
                m = 0
                
        if m < i:
            ans += i
        else:
            ans += m
            break
    ans += sum(al)+sum(bl)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
arrayP = list(map(int, input().split()))
arrayQ = list(map(int, input().split()))
arrayR = list(map(int, input().split()))
arrayP.sort(reverse=True)
arrayQ.sort(reverse=True)
arrayR.sort(reverse=True)
arrayAns = []
for i in range(x):
    arrayAns.append(int(arrayP[i]))
for i in range(y):
    arrayAns.append(int(arrayQ[i]))
arrayAns.sort()
for i in range(min(len(arrayR), len(arrayAns))):
    if arrayAns[i] <= int(arrayR[i]):
        arrayAns[i] = int(arrayR[i])
    else:
        break
print(sum(arrayAns))

Submission status: Accepted

Solution code:
##E
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)

psum = 0

for i in range(X):
        r.append(p[i])
for i in range(Y):
        r.append(q[i])
        
r.sort(reverse=True)
ans = 0

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

        
print(ans)

Submission status: Accepted

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

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

p = sorted(pp+q)

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

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

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

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int, raw_input().split())
As=map(int, raw_input().split())
Bs=map(int, raw_input().split())
Cs=map(int, raw_input().split())
X=x
Y=y
newarr=[]
res=0
for i in xrange(len(As)):
    newarr.append([As[i], 0])
for i in xrange(len(Bs)):
    newarr.append([Bs[i], 1])
for i in xrange(len(Cs)):
    newarr.append([Cs[i], 2])
newarr.sort(reverse=True)
i=-1
t=0
while True:
    i+=1
    if newarr[i][1]==0:
        if x>0:
            x-=1
        else:
            continue
    elif newarr[i][1]==1:
        if y>0:
            y-=1
        else:
            continue
    res+=newarr[i][0]
    # print newarr[i], X, Y, t
    t+=1
    if t>=X+Y:
        break

# print newarr
print res

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()))
ans = []
P.sort()
Q.sort()
R.sort()
for i in range(x):
    ans.append(P[a-i-1])
for i in range(y):
    ans.append(Q[b-i-1])
flag = True
ans.sort()
ans_sum = sum(ans)
cur = c-1
temp = 0
for i in range(c):
    if cur >= 0 and temp <= (x+y-1):
        if R[cur] >= ans[temp]:
            ans_sum += R[cur]-ans[temp]
            cur -= 1
            temp += 1
print(ans_sum)

Submission status: Accepted

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

    red_eat = ps[:x]
    blue_eat = qs[:y]

    eats = sorted(red_eat + blue_eat + rs, reverse=True)
    have = eats[:(x+y)]
    points = sum(have)
    print(points)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
from itertools import count

X, Y, A, B, C = map(int, input().split())
P = [int(n) for n in input().split()]
Q = [int(n) for n in input().split()]
R = [int(n) for n in input().split()]


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

P = deque(P[-X:])
Q = deque(Q[-Y:])
R = deque(R)

for i in count():
    if not R or P[0] >= R[0] <= Q[0]:
        break
    if R[0] >= P[0] <= Q[0]:
        P.append(R.popleft())
        P.popleft()
    elif R[0] >= Q[0] <= P[0]:
        Q.append(R.popleft())
        Q.popleft()

print(sum(P) + sum(Q))

Submission status: Accepted

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

pq = sorted(p[-X:] + q[-Y:])
ans = sum(pq)

if pq[0] < max(r):
    left = 0
    right = len(r)
    while left < right - 1:
        mid = (left + right) // 2
        if pq[mid] >= r[mid]:
            right = mid
        else:
            left = mid
    ans = ans + sum(r[:right]) - sum(pq[:right])

print(ans)

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)

answer = 0
_ids = [0,0,0]

while sum(_ids) < x+y:
    _answer = []
    _x, _y, _k = _ids
    if _x != x:
        _answer += [p[_x]]
    else:
        _answer += [0]
    if _y != y:
        _answer += [q[_y]]
    else:
        _answer += [0]
    if _k != c:
        _answer += [r[_k]]
    else:
        _answer += [0]
    tmp = 0
    for i, _a in enumerate(_answer):
        if tmp < _a:
            tmp = _a
            _id = i
    answer += tmp
    _ids[_id] += 1
    
print(answer)

Submission status: Accepted

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()))
P.sort(reverse=True)
Q.sort(reverse=True)
P = sorted(P[:X])
Q = sorted(Q[:Y])
R.sort()
while R and (P[0] < R[-1] or Q[0] < R[-1]):
    if P[0] < Q[0]:
        heapq.heappush(P, R.pop())
        heapq.heappop(P)
    else:
        heapq.heappush(Q, R.pop())
        heapq.heappop(Q)
print(sum(P) + sum(Q))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import decimal
import heapq
import collections
import itertools
from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

x, y, a, b, c = map(int, input().split())
p = getInputListInt()
q = getInputListInt()
r = getInputListInt()

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

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

tmp_p_min = min(x, c)
tmp_p = p[:tmp_p_min]
tmp_q_min = min(y, c)
tmp_q = q[:tmp_q_min]

tmp_list = tmp_p+tmp_q+r
tmp_list.sort()
newlist = tmp_list[-tmp_p_min-tmp_q_min:]

ans = sum(p[tmp_p_min:]) + sum(q[tmp_q_min:]) + sum(newlist)
print(ans)

Submission status: Accepted

Solution code:
def input_multiple_number():
    return map(int, input().split())
def input_multiple_number_as_list():
    return list(map(int, input().split()))

xgiven,ygiven,agiven,bgiven,cgiven = input_multiple_number()

p_array = input_multiple_number_as_list()
q_array = input_multiple_number_as_list()
r_array = input_multiple_number_as_list()
INF = 99999999999999999
p_array.sort(reverse=True)
q_array.sort(reverse=True)
r_array.sort(reverse=True)

#ぐりいでぃい
i = xgiven
p_array = p_array[0:xgiven]
q_array = q_array[0:ygiven]
xxx_array = p_array+q_array+r_array


xxx_array.sort(reverse=True)
nnn_arrat = xxx_array[0:xgiven+ygiven]

value=0
for aaaaa in nnn_arrat:
	value += aaaaa

print(value)

Submission status: Accepted

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

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

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

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

M.sort(reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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)
qs = sorted(qs)
rs = sorted(rs)

nx = 0
ny = 0
nc = 0
sums = 0
for i in range(X+Y):
    a = b = c = 0
    if nx < X:
        a = ps[A-1-nx]
    if ny < Y :
        b = qs[B-1-ny]
    if nc < C:
        c = rs[C-1-nc]
    
    if a >= b and a >= c:
        sums += a
        # ps.pop(a)
        nx += 1
        
    if b > a and b >= c:
        sums += b
        # qs.pop(b)
        ny += 1
        
    if c > a and c > b:
        sums += c
        # rs.pop(c)
        nc += 1
        
print(sums)

Submission status: Accepted

Solution code:
#
# n, x, y = (int(x) for x in input().split())
# if y < x:
#     x, y = y, x
#
# def count(k):
#     sol = 0
#     sol += max(0, n - (y-x-1) - k)
#     d = y-x+1
#     if 2*k <= d:
#         if d > 2:
#             sol += d
#
#             if k == 1:
#                 sol -= 1
#     d//2
#
#
#
#

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()
q.sort()
r.sort()
i = a-x
j = b - y
k = c-1
sol = sum(p[-x:])+sum(q[-y:])
while k>= 0 and i <a and j < b:
    d1 = r[k] - p[i]
    d2 = r[k] - q[j]
    if d1 < 0 and d2 < 0:
        break
    if d1 > d2:
        sol += d1
        i += 1
        k-=1
    else:
        sol += d2
        j += 1
        k -= 1

while k>=0 and i < a:
    d1 = r[k] - p[i]
    if d1 < 0:
        break

    sol += d1
    i += 1
    k-=1

while k>=0 and j < a:
    d1 = r[k] - q[j]
    if d1 < 0:
        break

    sol += d1
    j += 1
    k-=1

print(sol)

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)

if x <= len(p):
    pp = p[:x]
else:
    pp = p + r[:x-len(p)]

if y <= len(q):
    qq = q[:y]
else:
    qq = q + r[:y-len(q)]
    
rr = []
for k in r:
    if len(pp) >= 1 and len(qq) >= 1:
        if pp[-1] < qq[-1]:
            if pp[-1] < k:
                pp.pop()
                rr.append(k)
            else:
                break
        else:
            if qq[-1] < k:
                qq.pop()
                rr.append(k)
            else:
                break
    elif len(pp) >= 1:
        if pp[-1] < k:
            pp.pop()
            rr.append(k)
        else:
            break
    elif len(qq) >= 1:
        if qq[-1] < k:
            qq.pop()
            rr.append(l)
        else:
            break
    else:
        break

print(sum(pp)+sum(qq)+sum(rr))

Submission status: Accepted

Solution code:
import heapq
 
X, Y, A, B, C = [int(i) for i in input().split()]
P = sorted([int(i) for i in input().split()])[-X:]
Q = sorted([int(i) for i in input().split()])[-Y:]
R = sorted([int(i) for i in input().split()])[::-1]
heapq.heapify(P)
heapq.heapify(Q)
i = 0
while(True):
    if P[0] >= R[i] and Q[0] >= R[i]:
        break
    if P[0] > Q[0]:
        if Q[0] < R[i]:
            heapq.heappop(Q)
            heapq.heappush(Q, R[i])
    else:
        if P[0] < R[i]:
            heapq.heappop(P)
            heapq.heappush(P, R[i])
    i += 1
    if i == len(R):
        break
print(sum(P) + sum(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)
s = p[0:X] + q[0:Y] + r

s.sort(reverse=True)
ans = 0

for i in range(X+Y):
    ans += s[i]
    
print(ans)

Submission status: Accepted

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

listp = sorted(listp, reverse=True)
listq = sorted(listq, reverse=True)
listr = sorted(listr, reverse=True)
listp = listp[:X]
listq = listq[:Y]
listr = listr[:X+Y]
listp.insert(0, 1000000000000)
listq.insert(0, 1000000000000)


eat_noapple = []
oisisa = 0

for noapple in listr:
    if listp[-1] >= listq[-1] and listq[-1] < noapple:
        listq.pop(-1)
        eat_noapple.append(noapple)
    elif listp[-1] < listq[-1] and listp[-1] < noapple:
        listp.pop(-1)
        eat_noapple.append(noapple)
    else:
        break
listp.pop(0)
listq.pop(0)

try:
    for papple in listq:
        oisisa += papple
except:
    a = 0
try:
    for qapple in listp:
        oisisa += qapple
except:
    a = 0
try:
    for rapple in eat_noapple:
        oisisa += rapple
except:
    a = 0
print(oisisa)

Submission status: Accepted

Solution code:
import sys
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(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)
ri = 0
pi = x
qi = y
while ri < len(r) and (0 < pi or 0 < qi):
    if 0 < pi and p[pi - 1] < r[ri] and (qi <= 0 or p[pi - 1] <= q[qi - 1]):
        ri += 1
        pi -= 1
    elif 0 < qi and q[qi - 1] < r[ri] and (pi <= 0 or q[qi - 1] <= p[pi - 1]):
        ri += 1
        qi -= 1
    else:
        break
print(sum(p[:pi]) + sum(q[:qi]) + sum(r[:ri]))

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

r = deque(r)
deque_p = deque(p[:X])
deque_q = deque(q[:Y])
deque_r = deque([])

if r:
    while min(deque_p[-1], deque_q[-1]) < r[0]:
        if deque_p[-1] <= deque_q[-1]:
            deque_p.pop()
            deque_r.append(r.popleft())
        else:
            deque_q.pop()
            deque_r.append(r.popleft())

        if (not r) or (not deque_p) or (not deque_q):
            break

if r and deque_p:
    while deque_p[-1] < r[0]:
        deque_p.pop()
        deque_r.append(r.popleft())

        if (not r) or (not deque_p):
            break

if r and deque_q:
    while deque_q[-1] < r[0]:
        deque_q.pop()
        deque_r.append(r.popleft())

        if (not r) or (not deque_q):
            break
    

ans = sum(deque_p) + sum(deque_q) + sum(deque_r)

print(ans)

Submission status: Accepted

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

ra = list(map(int, input().split()))
gb = list(map(int, input().split()))
sc = list(map(int, input().split()))

ra.sort(reverse=True)
gb.sort(reverse=True)
sc.sort(reverse=True)

curr_red_min_val = ra[x-1]
curr_green_min_val = gb[y-1]
tmp_x = x
tmp_y = y

for i in range(c):
  if tmp_x > 0 and curr_red_min_val < curr_green_min_val:
    if sc[i] > curr_red_min_val:
      ra[tmp_x-1] = sc[i]
      tmp_x -= 1
      if tmp_x != 0:
        curr_red_min_val = ra[tmp_x-1]
      else:
        curr_red_min_val = ra[0]
  elif tmp_y > 0:
    if sc[i] > curr_green_min_val:
      gb[tmp_y-1] = sc[i]
      tmp_y -= 1
      if tmp_y != 0:
        curr_green_min_val = gb[tmp_y-1]
      else:
        curr_green_min_val = gb[0]
      
print(sum(ra[:x])+sum(gb[:y]))

Submission status: Accepted

Solution code:
from collections import deque

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)

opt_p = deque(p[:x])
opt_q = deque(q[:y])
opt_r = deque(r[:x+y])

while len(opt_r) > 0:
    if opt_p[-1] < opt_r[0] or opt_q[-1] < opt_r[0]:
        if opt_p[-1] < opt_q[-1]:
            opt_p.pop()
            opt_p.appendleft(opt_r.popleft())
        else:
            opt_q.pop()
            opt_q.appendleft(opt_r.popleft())
    else:
        break

print(sum(opt_p) + sum(opt_q))

Submission status: Accepted

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.sort()
B.sort()
C.sort(reverse=True)
A=A[a-x:]
B=B[b-y:]

i=0
j=0
k=0
lea=len(A)
leb=len(B)
while(True):
    if len(C)<1:
        break
    if i<lea and j<leb:
        if A[i]>B[j]:
            if B[j]<C[0]:
                B[j]=C.pop(0)
                
                j+=1
            else:
                break
        else:
            if A[i]<C[0]:
                A[i]=C.pop(0)
                i+=1
            else:
                break
    elif i<lea and j>=leb:
        if A[i]<C[0]:
                A[i]=C.pop(0)
                i+=1
        else:
            break
    elif i>=lea and j<leb:
        if B[j]<C[0]:
                B[j]=C.pop(0)
                j+=1
        else:
            break
    else:
        break
sum=0


for s in range(len(A)):
    sum+=A[s]
for t in range(len(B)):
    sum+=B[t]
print(sum)

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

eat = []

# 食べる
p.sort(reverse=True)
if X > A:
    eat.extend(p)
else:
    eat.extend(p[:X])

q.sort(reverse=True)
if Y > B:
    eat.extend(q)
else:
    eat.extend(q[:Y])
loss = X+Y - len(eat)

# 足りない分を補充する
while loss > 0:
    r.sort(reverse=True)
    eat.append(r.pop(0))    
    loss -= 1

# rの方が大きいものがあれば入れ替える
eat.sort()
r.sort()
for i in range(len(eat)):
    if len(r) != 0 and eat[i] < r[-1]:
        eat[i] = r.pop(-1)
    else:
        break

print(sum(eat))

Submission status: Accepted

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+=lc[:(x+y)]

  ans_list.sort(reverse=True)


  # print(ans_list)
  return sum(ans_list[:x+y])

# main()
print(main())

Submission status: Accepted

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

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

P = P[A-X:]
Q = Q[B-Y:]

P.append(10**9+1)
Q.append(10**9+1)
R.append(0)


i = 0
j = 0


while min(P[i],Q[j])<R[i+j]:
    if P[i] < Q[j]:
        i += 1
    else:
        j += 1
    if i+j == C:
        break

P.remove(10**9+1)
Q.remove(10**9+1)
R.remove(0)

ans = 0
for e in P[i:]:
    ans = ans + e
for e in Q[j:]:
    ans = ans + e
for a in range(i+j):
    ans = ans + R[a]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from collections import deque


def main():
    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)

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

    li = sorted(pp + qq + r, reverse=True)
    M = sum(li[:X+Y])

    print(M)



if __name__ == '__main__':
    main()

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()
from collections import deque
cr = 0
cg = 0
p = p[:x]
q = q[:y]
p = deque(p)
q = deque(q)
for i in range(c-1, -1, -1):
    x = p.pop()
    y = q.pop()
    z = min(x, y)
    if x < y:
        q.append(y)
        if r[i] > z:
            p.appendleft(r[i])
        else:
            p.append(x)
    else:
        p.append(x)
        if r[i] > z:
            q.appendleft(r[i])
        else:
            q.append(y)
print(sum(p) + sum(q))

Submission status: Accepted

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

tmp = list(map(int, input().split()))
tmp.sort(reverse=True)
red = [2e+9]
red.extend(tmp)

tmp = list(map(int, input().split()))
tmp.sort(reverse=True)
green = [2e+9]
green.extend(tmp)
none = list(map(int, input().split()))
none.sort(reverse=True)

n_red = x
n_green = y
n_none = 0


while(True):
  if(red[n_red] >= green[n_green]):
    if(green[n_green] <= none[n_none]):
      n_green -= 1
      n_none += 1
    else:
      break
  else: # redの最小の方が小さい時
    if(red[n_red] <= none[n_none]):
      n_red -= 1
      n_none += 1
    else:
      break
  if(n_none == len(none)):
    break
#

print(int(sum(red[1:n_red+1])
          +sum(green[1:n_green+1])
          +sum(none[:n_none])))

Submission status: Accepted

Solution code:
import heapq

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


#print(p)
p.sort()
p=p[:x]
q.sort()
q=q[:y]
r.sort()
r=r[:min(len(r),x+y)]
#print(p,q,r)

heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)

result=0

xx=x
yy=y
result=0
rrr=min(len(r),x+y)
for _ in range(x+y):
    #heappop(a)
    #heapq.heappush(a, -2) 

    if xx>0 and yy>0 and rrr>0:
        qq=heapq.heappop(q)
        pp=heapq.heappop(p)
        rr=heapq.heappop(r)
        
        maximum=min(pp,qq,rr)
        if qq==maximum:
            result+=-1*qq
            yy=yy-1
            heapq.heappush(p, pp) 
            heapq.heappush(r, rr) 
        elif pp==maximum:
            result+=-1*pp
            xx=xx-1
            heapq.heappush(q,qq)
            heapq.heappush(r, rr) 
        else:
            result+=-1*rr
            rrr=rrr-1
            heapq.heappush(p, pp)
            heapq.heappush(q, qq) 
    
    elif yy>0 and rrr>0:
        qq=heapq.heappop(q)
        rr=heapq.heappop(r)
        
        maximum=min(qq,rr)
        if qq==maximum:
            result+=-1*qq
            yy=yy-1
            heapq.heappush(r, rr) 
        else:
            result+=-1*rr
            rrr=rrr-1
            heapq.heappush(q, qq) 
    elif xx>0  and rrr>0:
        pp=heapq.heappop(p)
        rr=heapq.heappop(r)
        
        maximum=min(pp,rr)
        if pp==maximum:
            result+=-1*pp
            xx=xx-1
            heapq.heappush(r, rr) 
        else:
            result+=-1*rr
            rrr=rrr-1
            heapq.heappush(p, pp)
    else:
        qq=heapq.heappop(q)
        pp=heapq.heappop(p)
        
        maximum=min(pp,qq)
        if qq==maximum:
            result+=-1*qq
            yy=yy-1
            heapq.heappush(p, pp) 
        elif pp==maximum:
            result+=-1*pp
            xx=xx-1
            heapq.heappush(q,qq)
        
    
    

print(result)

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

T = [(0, "r")] * (A+B+C)
for a in range(A):
    T[a] = (P[a], "r")
for b in range(B):
    T[b+A] = (Q[b], "g")
for c in range(C):
    T[c+A+B] = (R[c], "t")

T.sort(reverse=True)

countR = 0
countG = 0
countT = 0

ans = 0
i = 0
while countR + countG + countT < X + Y:
    v, color = T[i]
    if color == "r":
        if countR < X:
            ans += v
            countR += 1
    elif color == "g":
        if countG < Y:
            ans += v
            countG += 1
    else:
        ans += v
        countT += 1
    i += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
  x,y,a,b,c=map(int,input().split())
  ps=sorted(list(map(int,input().split())))
  qs=sorted(list(map(int,input().split())))
  rs=sorted(list(map(int,input().split())))
  points=sorted(ps[-x:]+qs[-y:])
  return sum(sorted(points+rs)[-x-y:])
print(main())

Submission status: Accepted

Solution code:
import heapq

# print('input >>')
X, Y, A, B, C = map(int,(input().split()))
ps = list(map(int,(input().split())))
qs = list(map(int,(input().split())))
rs = list(map(lambda x:int(x)*-1,(input().split())))

ps.sort()
qs.sort()
rs.sort()

ps = ps[-X:]
qs = qs[-Y:]

heapq.heapify(ps)
heapq.heapify(qs)
heapq.heapify(rs)

while True:
    if len(rs) == 0:
        break
    max_r = heapq.heappop(rs)
    min_p = heapq.heappop(ps)
    min_q = heapq.heappop(qs)

    min_pq = min(min_p, min_q)

    if max_r*-1 > min_pq:
        if min_p < min_q:
            heapq.heappush(ps, max_r*-1)
            heapq.heappush(qs, min_q)
        else:
            heapq.heappush(qs, max_r*-1)
            heapq.heappush(ps, min_p)
    else:
        heapq.heappush(qs, min_q)
        heapq.heappush(ps, min_p)
        break

# print('-----output-----')
print(sum(ps) + sum(qs))

Submission status: Accepted

Solution code:
import sys

X, Y, A, B, C = map(int, sys.stdin.readline().strip().split())

P = list(map(int, sys.stdin.readline().strip().split()))
Q = list(map(int, sys.stdin.readline().strip().split()))
R = list(map(int, sys.stdin.readline().strip().split()))

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

P = P[:X]
P.sort()
Q = Q[:Y]
Q.sort()

ans = 0
i, j, k = 0, 0, 0
while C > k:
    if X > i and Y > j:

        if P[i] >= Q[j]:
            if R[k] > Q[j] and Y > j:
                Q[j] = R[k]
                j += 1
                k += 1
            elif R[k] > P[i] and X > i:
                P[i] = R[k]
                i += 1
                k += 1
            else:
                break
 
        elif P[i] < Q[j]:
            if R[k] > P[i] and X > i:
                P[i] = R[k]
                i += 1
                k += 1
            elif R[k] > Q[j] and Y > j:
                Q[j] = R[k]
                j += 1
                k += 1
            else:
                break
    elif X > i:
        if R[k] > P[i]:
            P[i] = R[k]
            i += 1
            k += 1
        else:
            break
    elif Y > j:
        if R[k] > Q[j]:
            Q[j] = R[k]
            j += 1
            k += 1
    else:
        break

# print(P)
# print(Q)
print(sum(P) + sum(Q))