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 heapq
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()))
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    h = []
    for i in p[:X]:
        heapq.heappush(h, (i, 'r'))
    for i in q[:Y]:
        heapq.heappush(h, (i, 'g'))
    a = sum(p[:X]) + sum(q[:Y])
    ra = 0
    ga = 0
    i = 0
    while h:
        t = heapq.heappop(h)
        if r[i] < t[0]:
            break
        if t[1] == 'r' and ra <= X:
            a += r[i]
            a -= t[0]
            ra += 1
            i += 1
        elif t[1] == 'g' and ga <= Y:
            ga += 1
            a += r[i]
            a -= t[0]
            i += 1
        if ra == X and ga == Y:
            break
        if i >= C:
            break
    return a
print(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()))

all = []
for i,val in enumerate([p,q,r]):
    for j in val:
        all.append([j,i])

all.sort(reverse=True)

cnt = [0]*3
ans = 0
for tmp in all:
    i,j = tmp
    if(cnt[0]==x)&(j==0):
        continue
    if(cnt[1]==y)&(j==1):
        continue

    ans += i
    cnt[j] += 1

    if(sum(cnt) == x+y):
        break

print(ans)

Submission status: Accepted

Solution code:
def main():
    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()))
    hq = [None]*(A+B+C)
    for i, x in enumerate(p):
        hq[i] = (-x, 0)
    for i, x in enumerate(q):
        hq[A+i] = (-x, 1)
    for i, x in enumerate(r):
        hq[A+B+i] = (-x, 2)

    heapq.heapify(hq)

    hq2 = []

    x = 0
    y = 0
    ans = 0
    while(len(hq2) < X+Y - (x+y)):
        a, b = heapq.heappop(hq)
        a = -a
        if b == 0:
            if x < X:
                x += 1
                ans += a
            else:
                continue
        elif b == 1:
            if y < Y:
                y += 1
                ans += a
            else:
                continue
        else:
            hq2.append(a)
        if x == X and y == Y:
            break

    if x < X or y < Y:
        ans += sum(hq2)
    print(ans)


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(reverse=True)

ep = p[:X]
eq = q[:Y]
epq = ep + eq
epq.sort()

ans = sum(epq)
for i, mr in enumerate(r):
    if i >= len(epq):
        break

    if mr > epq[i]:
        ans += mr-epq[i]
    else:
        break

print(ans)

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 = sorted(A)
B = sorted(B)
C = sorted(C)

t = 0
s = 0

A = A[-x:]
B = B[-y:]

C.extend(A)
C.extend(B)
C = sorted(C)

print(sum(C[-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 = sorted(p)
q = sorted(q)
baseline = p[-x:] + q[-y:]

r = deque(sorted(r))
baseline = deque(sorted(baseline))

while len(r) != 0:
    v = r.pop()
    if v > baseline[0]:
        baseline.popleft()
        baseline.append(v)
    else:
        break

print(sum(baseline))

Submission status: Accepted

Solution code:
x,y,a,b,c = map(int,input().split())
r = sorted(list(map(int,input().split())))[::-1]
g = sorted(list(map(int,input().split())))[::-1]
c = sorted(list(map(int,input().split())))[::-1]
l =[]
count = 0
count1=0
for i in range(x):
  l.append(r[i])
for i in range(y):
  l.append(g[i])
l = sorted(l)
while count1< len(l) and count<len(c):
  if l[count1] >= c[count]:
    count1+=1
  else:
    l[count1] = c[count]
    count1+=1
    count+=1
print(sum(l))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    x,y,a,b,c = LI()
    p = LI()
    q = LI()
    r = LI()
    s = [(i,0) for i in p]+[(i,1) for i in q]+[(i,2) for i in r]
    s.sort(reverse = True)
    ans = 0
    f = [0,0]
    l = [x,y]
    q = deque()
    q2 = deque()
    for a,i in s:
        if i > 1:
            q.append(a)
        else:
            if f[i] < l[i]:
                ans += a
                f[i] += 1
                q2.append(a)
    s = ans
    while q and q2:
        s -= q2.pop()
        s += q.popleft()
        if ans < s:
            ans = s
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    xy = sorted(p[:X] + q[:Y])
    ans = sum(p[:X]) + sum(q[:Y])
    z = 0
    while z < X + Y and z < C and xy[z] < r[z]:
        ans += r[z] - xy[z]
        z += 1
    print(ans)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)


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 = sorted(P,reverse=True)
Q = sorted(Q,reverse=True)
R = sorted(R,reverse=True)
P = P[:X]
Q = Q[:Y]
P.append(-100)
Q.append(-100)
R.append(-100)

ans = sum(P)+sum(Q)

p=0
q=0
r=0

x = 0
y = 0

ans = 0
while(x<=X or y<=Y):
    mm = max(P[p],Q[q],R[r])
    if R[r] == mm:
        ans += R[r]
        r+=1
    elif P[p]==mm:
        ans += P[p]
        p+=1
        x+=1
    elif Q[q]==mm:
        ans += Q[q]
        q+=1
        y+=1
    if (x+y+r)==(X+Y):
        break
    # print(p,q,r)

print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = list(map(int, input().split()))
ppp = list(map(int, input().split()))
qqq = list(map(int, input().split()))
rrr = list(map(int, input().split()))
ppp.sort(reverse=True)
qqq.sort(reverse=True)
rrr.sort(reverse=True)
ppqq = ppp[:x] + qqq[:y]
ppqq.sort(reverse=True)
ppqq.insert(0, 0)
rrr.insert(0, 0)

i = x + y
j = 1
while i > 0 and j <= c and ppqq[i] < rrr[j]:
    i -= 1
    j += 1
print(sum(ppqq[:i + 1]) + sum(rrr[:j]))

Submission status: Accepted

Solution code:
import heapq

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.sort()
Q.sort()
total = P[A-X:] + Q[B-Y:]
total.sort()
heapq.heapify(R)
i = 0
while True:
    try:
        n = -heapq.heappop(R)
        if i < len(total) and total[i] < n:
            total[i] = n
            i += 1
        else:
            break
    except:
        break
print(sum(total))

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

    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    p = p[:x]
    q = q[:y]
    ans = p+q
    ans.sort()
    now = 0
    for i in range(x+y):
        if r[now] > ans[i]:
            ans[i] = r[now]
            now += 1
            if now == c:
                break
    print(sum(ans))


main()

Submission status: Accepted

Solution code:
#heapだけ？
#x<=a,y<=b
import heapq
x,y,a,b,c = map(int, input().split( ))

*p, = map(int, input().split( ))
*q, = map(int, input().split( ))
*r, = map(int, input().split( ))
#p = [(pi,0) for pi in p_]
#q = [(qi,1) for qi in q_]
#r = [(ri,2) for ri in r_]
p.sort();q.sort();r.sort()##
p = p[-x:]
q = q[-y:]
e = p+q
heapq.heapify(e)
for ri in r:
    heapq.heappush(e,ri)
    heapq.heappop(e)
print(sum(e))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    import heapq

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

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

    RE=P[:X]
    QE=Q[:Y]

    heapq.heapify(RE)
    heapq.heapify(QE)

    for r in R:
        if RE[0]<QE[0]:
            if RE[0]<r:
                heapq.heappushpop(RE,r)
        else:
            if QE[0]<r:
                heapq.heappushpop(QE,r)

    print(sum(list(RE))+sum(list(QE)))




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 = sorted(p, reverse = True)[:x]
q = sorted(q, reverse = True)[:y]
r.sort(reverse = True)

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

almost_ans = sorted(p + q)
# print("almost_ans", almost_ans)

colored_ind = 0
uncol_ind = 0
while colored_ind < len(almost_ans) and uncol_ind < len(r) and almost_ans[colored_ind] < r[uncol_ind]:
	almost_ans[colored_ind] = r[uncol_ind]
	colored_ind += 1
	uncol_ind += 1

print(sum(almost_ans))

Submission status: Accepted

Solution code:
from heapq import heapify, heappush, heappop
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()))


rs.sort(key=lambda x: -x)
hp = []
hq = []
for p in ps:
    heappush(hp, p)
    if len(hp) > X:
        heappop(hp)

for q in qs:
    heappush(hq, q)
    if len(hq) > Y:
        heappop(hq)

for r in rs:
    if len(hq) < Y:
        heappush(hq, r)
    elif len(hp) < X:
        heappush(hp, r)
    elif hp[0] < hq[0]:
        heappush(hp, r)
        heappop(hp)
    else:
        heappush(hq, r)
        heappop(hq)

print(sum(hq) + sum(hp))

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 = sorted(list(map(int, input().split())), reverse=True)[:X+Y]
pqr = sorted(p+q+r, reverse=True)
print(sum(pqr[: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)

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

print(sum(S[0:(X+Y)]))

Submission status: Accepted

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

arr = p[:X] + q[:Y]
heapq.heapify(arr)
for n in r:
    if n < arr[0]:
        break
    if n > arr[0]:
        heapq.heappop(arr)
        heapq.heappush(arr, n)
print(sum(arr))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

from collections import deque

x,y,a,b,c = l_in()
P = sorted(l_in(), reverse=True)
Q = sorted(l_in(), reverse=True)
R = deque(sorted(l_in(), reverse=True))


res = []
for i in range(x):
    res.append(P[i])
for j in range(y):
    res.append(Q[j])

res.sort()

for i in range(x+y):
    if len(R) > 0 and res[i] < R[0]:
        res[i] = R.popleft()

print(sum(res))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    #→1,.→0として読み込む
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '#' else 0 for s in read()[:-1]])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    return ret


from heapq import heapify, heappop, heappush, heappushpop


class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)

    def push(self, item):
        heappush(self.heap, item)

    def pop(self):
        return heappop(self.heap)

    def pushpop(self, item):
        return heappushpop(self.heap, item)

    def __call__(self):
        return self.heap

    def __len__(self):
        return len(self.heap)


MOD = 10**9 + 7
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

X, Y, A, B, C = read_ints()
P = read_ints()
Q = read_ints()
R = read_ints()  # これは適切に着色できる

# ソートして、価値の低いものから捨てていくのが良さそうだが
P.sort(reverse=True)
Q.sort(reverse=True)

tmp = [-x for x in (P[:X] + Q[:Y])]

pq = PriorityQueue(tmp)
for r in R:
    pq.push(-r)

ans = 0
for _ in range(X + Y):
    ans += pq.pop()
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()
r.sort()

usep = a - x
useq = b - y
ans = sum(p[usep:]) + sum(q[useq:])
p.append(10 ** 9 + 1)
q.append(10 ** 9 + 1)

for i in range(c):
    if p[usep] < q[useq]:
        if r[c - i - 1] > p[usep]:
            ans = ans + r[c - i - 1] - p[usep]
            usep += 1
        else:
            break
    else:
        if r[c - i - 1] > q[useq]:
            ans = ans + r[c - i - 1] - q[useq]
            useq += 1
        else:
            break

print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int,input().split())
S =[[],[],[]]
S[0] = list(map(int,input().split()))
S[1] = list(map(int,input().split()))
S[2] = list(map(int,input().split()))

T = []
for i in range(3):
  for s in S[i]:
    T.append((s,i))
T.sort(reverse=True)

U = []
i = 0
x = 0
y = 0
while(len(U) < X+Y):
  a,b = T[i]
  if b == 0:
    if x == X:
      i += 1
      continue
    else:
      x += 1
  elif b == 1:
    if y == Y:
      i += 1
      continue
    else:
      y += 1
  U.append(a)
  i += 1
print(sum(U))

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

x, y, a, b, c = get_nums_l()
ppp = get_nums_l()
qqq = get_nums_l()
rrr = get_nums_l()

ppp.sort(reverse=True)
qqq.sort(reverse=True)
rrr.sort(reverse=True)

xxx = ppp[:x] + qqq[:y]
xxx.sort()

ans = 0
for i in range(x+y):
    x = xxx[i]
    if i < len(rrr):
        x = max(x, rrr[i])
    ans += x

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(reverse=True)
Q.sort(reverse=True)
Z = P[:X]+Q[:Y]+R
Z.sort(reverse=True)
print(sum(Z[:X+Y]))

Submission status: Accepted

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

from heapq import heappush, heappop

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

hP = []
hQ = []

P.sort(reverse = True)
Q.sort(reverse = True)
#print(P, Q)

for i in range(min(A, X)):
    heappush(hP, P[i])
for _ in range(X - A):
    heappush(hP, 0)
#print(hP)

for i in range(min(B, Y)):
    heappush(hQ, Q[i])
for _ in range(Y - B):
    heappush(hQ, 0)
#print(hQ)

for r in R:
    hp = heappop(hP)
    hq = heappop(hQ)
    if r <= hp and r <= hq:
        heappush(hP, hp)
        heappush(hQ, hq)
    else:
        if hp <= hq:
            heappush(hP, r)
            heappush(hQ, hq)
        else:
            heappush(hP, hp)
            heappush(hQ, r)
print(sum(hP) + sum(hQ))

Submission status: Accepted

Solution code:
x,y,a,b,c = (int(num) for num in input().split())
p = [int(num) for num in input().split()]
q = [int(num) for num in input().split()]
r = [int(num) for num in input().split()]
apples = []
for i in range(a):
    apples.append((p[i],"r"))
for i in range(b):
    apples.append((q[i],"g"))    
for i in range(c):
    apples.append((r[i],"n"))
    
apples.sort(reverse = True)

cntr = 0
cntg = 0
cntall = 0
i = 0
ans = 0
#print(apples)
while True:
    if apples[i][1] == "r" and cntr < x:
        ans += apples[i][0]
        cntr += 1
        cntall += 1
    if apples[i][1] == "g" and cntg < y:
        ans += apples[i][0]
        cntg += 1
        cntall += 1
    if apples[i][1] == "n":
        ans += apples[i][0]
        cntall += 1
        
    if cntall == x + y:
        break
    i += 1
    
print(ans)

Submission status: Accepted

Solution code:
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    x, y, a, b, c = map(int, sys.stdin.readline().split())
    r = list(map(int, sys.stdin.readline().split()))
    g = list(map(int, sys.stdin.readline().split()))
    blind = list(map(int, sys.stdin.readline().split()))
    r.sort(reverse=True)
    g.sort(reverse=True)
    blind.sort(reverse=True)
    red_need = r[:x]
    green_need = g[:y]
    res = red_need + green_need
    res.sort()
    #print(res)
    for i in range(min(c,len(res))):
        if blind[i] > res[i]:
            res[i] = blind[i]
    print(sum(res))


if __name__ == "__main__":
    # IOE()
    main()

Submission status: Accepted

Solution code:
from heapq import heapify, heappushpop

X, Y, A, B, C = map(int, input().split())
# 大きい順にソートして受け取っておく
p = sorted(list(map(int, input().split())))[::-1]
q = sorted(list(map(int, input().split())))[::-1]
r = sorted(list(map(int, input().split())))[::-1]
ans_list = p[:X] + q[:Y]  # 着色しない場合
heapify(ans_list)
for ri in r:
    if ri > ans_list[0]:
        heappushpop(ans_list, ri)
    else:
        break
print(sum(ans_list))

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    x,y,a,b,c = inpm()
    p = inpl()
    q = inpl()
    r = inpl()
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    ans = 0
    que = []
    for i in range(x):
        ans += p[i]
        que.append([p[i],0])
    for i in range(y):
        ans += q[i]
        que.append([q[i],1])
    que.sort()
    cntx = x
    cnty = y
    indexq = 0
    indexr = 0
    while not (indexr == len(r) or indexq == len(que)):
        if r[indexr] <= que[indexq][0]:
            break
        if cntx == 0 and not(que[indexq][1]):
            indexq += 1
            continue
        if cnty == 0 and que[indexq][1]:
            indexq += 1
            continue
        ans += r[indexr]-que[indexq][0]
        if que[indexq][1]:
            cnty -= 1
        else:
            cntx -= 1
        indexr += 1
        indexq += 1
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
x, y, A, B, C = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a = sorted(a)[::-1]
b = sorted(b)[::-1]
c = sorted(c)

s = 0

a = a[:x]
b = b[:y]
s += sum(a)
s += sum(b)

a = deque(a)
b = deque(b)
c = deque(c)

a_nex = a.pop()
b_nex = b.pop()
c_nex = c.pop()

while True:
#     print(s)
    if a_nex > b_nex:
        if c_nex > b_nex:
            s = s - b_nex + c_nex
            if len(b) == 0:
                b_nex = int(2e9)
            else:
                b_nex = b.pop()
            if len(c) == 0:
                break
            c_nex = c.pop()
    else:
        if c_nex > a_nex:
            s = s - a_nex + c_nex
            if len(a) == 0:
                a_nex = int(2e9)
            else:
                a_nex = a.pop()
            if len(c) == 0:
                break
            c_nex = c.pop()

    if c_nex <= a_nex and c_nex <= b_nex:
        break

print(s)

Submission status: Accepted

Solution code:
x,y,a,b,c,*pqr = map(int,open(0).read().split())
tag = ['a']*a + ['b']*b + ['c']*c
pqr_tag = [[pqr,tag] for pqr,tag in zip(pqr,tag)]
pqr_tag.sort(key = lambda x:x[0],reverse = True)

eat = {'a':0,'b':0,'c':0}
eat_max = {'a':x,'b':y}
eat_sum = 0
ans = 0
for taste,tag in pqr_tag:
    
    if tag=='c':
        ans += taste
        eat_sum += 1
    elif eat[tag]<eat_max[tag]:
        eat[tag] += 1
        ans += taste
        eat_sum += 1
    
    if eat_sum == x+y:break
        
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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

a = 0
b = 0
c = 0

ans = 0
for _ in range(X+Y):
    if a < X and b < Y and c < C:
        if p[a] >= q[b] and p[a] >= r[c]:
            ans += p[a]
            a += 1
        elif q[b] >= p[a] and q[b] >= r[c]:
            ans += q[b]
            b += 1
        else:
            ans += r[c]
            c += 1
    elif a >= X and b < Y and c < C:
        if q[b] >= r[c]:
            ans += q[b]
            b += 1
        else:
            ans += r[c]
            c += 1
    elif a < X and b >= Y and c < C:
        if p[a] >= r[c]:
            ans += p[a]
            a += 1
        else:
            ans += r[c]
            c += 1
    elif a < X and b < Y and c >= C:
        if p[a] >= q[b]:
            ans += p[a]
            a += 1
        else:
            ans += q[b]
            b += 1
    elif a >= X and b < Y and c >= C:
        ans += q[b]
        b += 1
    elif a < X and b >= Y and c >= C:
        ans += p[a]
        a += 1
print(ans)

Submission status: Accepted

Solution code:
import heapq

x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
w = list(map(int,input().split()))

r.sort(key = lambda x:-x)
g.sort(key = lambda x:-x)
w.sort(key = lambda x:-x)


rg = []
for i in range(x):
    rg.append(r[i])

for i in range(y):
    rg.append(g[i])

rg.sort(key = lambda x:x)


ind = 0
ans = 0
for i in range(x+y):
    if ind <= c-1 and w[ind] > rg[i]:
        ans += w[ind]
        ind += 1
    else:
        ans += rg[i]

print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int,input().split())
alist = list(map(int, input().split()))
alist=sorted(alist, reverse=True)
blist = list(map(int, input().split()))
blist=sorted(blist, reverse=True)
clist = list(map(int, input().split()))
clist=sorted(clist, reverse=True)

ringo=[]
for i in range(x):
    ringo.append(alist[i])
for j in range(y):
    ringo.append(blist[j])

ringo=sorted(ringo)
swit=0
now=0
#print(ringo)
while swit==0:
    if ringo[now]<=clist[now]:
        ringo[now]=clist[now]
        now+=1
        if now>=len(ringo) or now>=len(clist):
            swit=1
    else:
        swit=1

print(sum(ringo))

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)
r.sort()
s=p[:x]+q[:y]
heapq.heapify(s)
for i in range(c)[::-1]:
    h=heapq.heappop(s)
    heapq.heappush(s,max(h,r[i]))
print(sum(s))

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p_list = sorted(list(map(int, input().split())), reverse=True)
q_list = sorted(list(map(int, input().split())), reverse=True)
r_list = sorted(list(map(int, input().split())), reverse=True)
z_list = sorted(p_list[:x] + q_list[:y])
ans = sum(z_list)
for r, z in zip(r_list, z_list):
    if r < z:
        break
    ans += r - z
print(ans)

Submission status: Accepted

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)

pq = p[:x] + q[:y]
pq.sort()
i = 0
while i<x+y and i < c and pq[i]<r[i]:
    i += 1
ans = r[:i] + pq[i:]
s = sum(ans)
print(s)

Submission status: Accepted

Solution code:
def main():
    import sys
    input=sys.stdin.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.sort()
    Q.sort()
    R.sort()
    P_=P[a-x:][::-1]
    Q_=Q[b-y:][::-1]
    ans=sum(P_)+sum(Q_)
    for i in range(c):
        r=R.pop()
        if not P_:
          	P_.append(float("INF"))
        if not Q_:
          	Q_.append(float("INF"))
        if r<=P_[-1] and r<=Q_[-1]:
            break
        elif r>P_[-1] and P_[-1]<=Q_[-1]:
            p=P_.pop()
            ans+=r-p
        else:
            q=Q_.pop()
            ans+=r-q
    print(ans)
if __name__ == '__main__':
    main()