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())
Ps = list(map(int,input().split()))
Qs = list(map(int,input().split()))
Rs = list(map(int,input().split()))

Ps.sort(reverse=True)
Qs.sort(reverse=True)
Rs.sort(reverse=True)

apple = Ps[:x] + Qs[:y]
apple.sort()

point = sum(apple)
kosuu = min(x+y, c)

for i in range(kosuu):
    if apple[i] < Rs[i]:
        point = point - apple[i] + Rs[i]
    else:
        break
print(point)

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(0)
q.append(0)
r.append(0)

i, j, k = 0, 0, 0
ans = 0

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

for l in range(x + y):
    cand = [p[i], q[j], r[k]]
    # print(max(cand))
    index = cand.index(max(cand))
    if index == 0:
        ans += p[i]
        i += 1
        if i == x:
            i = a
    elif index == 1:
        ans += q[j]
        j += 1
        if j == y:
            j = b
    else:
        ans += r[k]
        k += 1
    
print(ans)

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
r=sorted(list(map(int,input().split())),reverse=True)
g=sorted(list(map(int,input().split())),reverse=True)
w=list(map(int,input().split()))

p=sorted(r[:x]+g[:y]+w,reverse=True)
print(sum(p[:x+y]))

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

a=p[0:X]+q[0:Y]
heapq.heapify(a)

#print(a)
#print(a[0])

flag=True
while flag:
  if len(r)==0:
    flag=False
  elif a[0]<r[0]:
    heapq.heappop(a)
    heapq.heappush(a,r[0])
    r.pop(0)
  else:
    flag=False
print(sum(a))

Submission status: Accepted

Solution code:
X,Y,_,_,_ = map(int, input().strip().split())
R = list(map(int, input().strip().split()))
G = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))

apples = [(k, 'r') for k in R] + [(k, 'g') for k in G] + [(k, 'a') for k in A]
apples = sorted(apples, reverse=True)

ans = 0
flag = True
status = {k:0 for k in ['r','g','a']}

for score,color in apples:
  if color == 'r' and status['r'] < X:
    ans += score
    status['r'] += 1
  elif color == 'g' and status['g'] < Y:
    ans += score
    status['g'] += 1
  elif color == 'a':
    ans += score
    status['a'] += 1

  if status['r'] + status['g'] + status['a'] == X+Y: break

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())))
eat=sorted(p[:x]+q[:y],reverse=True)
score=sum(eat)
while r:
    be=eat.pop()
    af=r.pop()
    if be<af:
        score+=af-be
    else:
        break
print(score)

Submission status: Accepted

Solution code:


import heapq as hq
import bisect as bs

def resolve():
    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:]
    Q = Q[B-Y:]
    
    PQ =[]
    for i in P:
        PQ.append([i,0])    
    for i in Q:
        PQ.append([i,1])

    PQ.sort()
    mn = PQ[0][0]

    point =bs.bisect_right(R,mn)
    R=R[point:]

    for i in R:
        PQ.append([i,2])
    PQ.sort(reverse= True)
    AC=0
    BC=0
    CC=0

    score =0
    R = []
    for i in PQ:
        if i[1]==0 and AC < X:
            score += i[0]
            AC+=1
        if i[1]==1 and BC < Y:
            score += i[0] 
            BC+=1
        if i[1]==2:
            R.append(i[0])
            CC+=1
        
        if AC + BC +CC >= X+Y:
            break

    for i in range(CC):
        score+=R[i]
    
    print(score)








if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    x, y, a, b, c = [int(s) for s in input().strip().split()]
    P = [int(s) for s in input().strip().split()]
    Q = [int(s) for s in input().strip().split()]
    R = [int(s) for s in input().strip().split()]

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

    P, Q = P[:x], Q[:y]
    result = 0
    while P and Q and R and min(P[-1], Q[-1]) < R[-1]:
        if P[-1] <= Q[-1]:
            P.pop()
        else:
            Q.pop()
        result += R.pop()
    while P and R and P[-1] < R[-1]:
        P.pop()
        result += R.pop()
    while Q and R and Q[-1] < R[-1]:
        Q.pop()
        result += R.pop()
    result += sum(P) + sum(Q)
    print(result)


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

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

z = sorted(p+q+r,reverse=True)
ans = 0
c = 0
for i in z:
    if x+y-c==0:
        break
    elif p and i == p[-1] and x>0:
        ans+=i
        p.pop()
        x -= 1
    elif q and i == q[-1] and y>0:
        ans += i
        q.pop()
        y -= 1
    elif i == r[-1] and x+y-c>0:
        ans += i
        r.pop()
        c += 1
print(ans)

Submission status: Accepted

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


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


Conf = []
for p in P:
    Conf.append([p,0])
for p in Q:
    Conf.append([p,1])
for p in R:
    Conf.append([p,2])

Conf.sort(reverse = True)
ans = 0
cnt = [0]*3
limit = [X,Y]
for p,idx in Conf:
    if idx==2:
        ans += p
        cnt[idx] += 1
    else:
        if cnt[idx]==limit[idx]:
            continue
        else:
            cnt[idx] += 1
            ans += p
    
    if sum(cnt)==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()))

data = []

for i in p:
    data.append((i, 'r'))
for i in q:
    data.append((i, 'g'))
for i in r:
    data.append((i, 'n'))

data.sort(reverse=True)
total = x + y
result = 0

for i in data:
    if i[1] == 'r' and x != 0:
        x -= 1
        total -= 1
        result += i[0]
    elif i[1] == 'g' and y != 0:
        y -= 1
        total -= 1
        result += i[0]
    elif i[1] == 'n':
        total -= 1
        result += i[0]
    if total == 0:
        break

print(result)

Submission status: Accepted

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

ps = ps[:X]
qs = qs[:Y]
cnt_c = 0
ans = sum(ps)+sum(qs)
INF = 10**10
while cnt_c < C:
    if len(ps) != 0:
        p = ps.pop()
    else:
        p = INF
    if len(qs) != 0:
        q = qs.pop()
    else:
        q = INF
    r = rs[cnt_c]
    if p - r >= 0 and q - r >= 0:
        break
    elif r - p > r - q: #pのほうが差分が大きい
        qs.append(q)
        ans -= p
        ans += r
    else:
        ps.append(p)
        ans -= q
        ans += r
    cnt_c += 1
print(ans)



"""
2 2 2 2 4
2 4
5 1
10 10 10 10 10
"""

Submission status: Accepted

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


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

P = sorted(P, reverse=True)[:X]
Q = sorted(Q, reverse=True)[:Y]

heapq.heapify(P)
heapq.heapify(Q)
R.sort()

for r in R:
    X = P if P[0] < Q[0] else Q

    if X[0] < r:
        heapq.heappushpop(X, r)

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

Submission status: Accepted

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

X, Y, A, B, C = map(int, input().split())
p = sorted(map(int, input().split()), reverse=True)
q = sorted(map(int, input().split()), reverse=True)
r = sorted(map(int, input().split()), reverse=True)

pq = p[:X] + q[:Y]
if len(pq) < X + Y:
    pq += [0] * (X + Y - len(pq))
pq.sort()
for i in range(min(C, len(pq))):
    if pq[i] < r[i]:
        pq[i] = r[i]
print(sum(pq))

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

r = sorted(list(map(int, input().split())),reverse=True)
ans = 0
data_pq = [0]
for s in pq:
    data_pq.append(data_pq[-1] + s)

data_r = [0]
for t in r:
    data_r.append(data_r[-1] + t)


for i in range(c+1):
    if x + y - i < 0:break
    ans = max(ans,data_r[i] + data_pq[x+y-i])

print(ans)

Submission status: Accepted

Solution code:
from heapq import heapify, heappop, heappush, heappushpop


class PriorityQueue:
    def __init__(self, heap):
        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)


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

    AB = sorted(p, reverse=True)[:X] + sorted(q, reverse=True)[:Y]
    ans = sum(AB)
    AB_q = PriorityQueue(AB)
    ab_now = AB_q.pop()

    for r_ in r:
        if r_ > ab_now:
            ans = ans - ab_now + r_
            AB_q.push(r_)
            ab_now = AB_q.pop()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

apples = []
apples.extend(p[:X])
apples.extend(q[:Y])

apples.sort(reverse=True)
apples.extend(r)

apples.sort(reverse=True)
print(sum(apples[:(X+Y)]))

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

m=p[:x]+q[:y]+r
m.sort(reverse=1)

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

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
red = list(reversed(sorted(list(map(int, input().split())))))
green = list(reversed(sorted(list(map(int, input().split())))))
white = list(reversed(sorted(list(map(int, input().split())))))

result = 0
count = 0
ri = 0
gi = 0
wi = 0

while count < x + y:
	best = -1
	if wi < c:
		best = white[wi]

	if gi < y:
		best = max(best, green[gi])

	if ri < x:
		best = max(best, red[ri])

	if gi < y and best == green[gi]:
		gi += 1
	else:
		if ri < x and best == red[ri]:
			ri += 1
		else:
			wi += 1

	result += best
	count += 1


#print(ri, gi, wi)
print(result)

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()))
#x,y,a,b,c=[10**5 for _ in range(5)]
#p=[1 for i in range(a)]
#q=[1 for i in range(a)]
#r=[1 for i in range(a)]
p.sort()
p.reverse()
q.sort()
q.reverse()
total=p[:x]+q[:y]+r
total.sort()
total.reverse()
print(sum(total[: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()
q.sort()
r.sort()

count_p = 0
count_q = 0
count_r = 0
summ = 0

for i in range(X+Y):
        
    if count_p==A:
        p = [0]
    if count_q==B:
        q = [0]
    if count_r==C:
        r = [0]
    
    if count_p==X:
        p[-1] = 0
    if count_q==Y:
        q[-1] = 0
    
    if (r[-1] >= p[-1] and r[-1] >= q[-1]):
        count_r += 1
        summ += r.pop(-1)
    elif p[-1] >= q[-1]:
        count_p += 1
        summ += p.pop(-1)
    else:
        count_q += 1
        summ += q.pop(-1)
        
print(summ)

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=p[:X]+q[:Y]
P.sort(reverse=True)
Q = [0]
for sp in P:
    Q.append(Q[-1]+sp)
R = [0]
for sr in r:
    R.append(R[-1]+sr)

res = 0
for i in range(min(X+Y,C)+1):
    res = max(res,Q[X+Y-i]+R[i])
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()))

P = sorted(P, reverse=True)
Q = sorted(Q, reverse=True)
R = sorted(R, reverse=True)

PQ_eat = P[:x] + Q[:y]
PQ_eat = sorted(PQ_eat)

r_now = 0
ans = sum(PQ_eat)
# print(PQ_eat)
# print(R)
for i in range(len(PQ_eat)):
    if r_now >= len(R):
        break
    if PQ_eat[i] < R[r_now]:
        ans -= PQ_eat[i]
        ans += R[r_now]
        r_now += 1
    else:
        break
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()))
    p.sort(reverse=True)
    q.sort(reverse=True)
    for i in range(x):
        r.append(p[i])
    for j in range(y):
        r.append(q[j])
    r.sort(reverse=True)
    ans=sum(r[:x+y])
    print(ans)
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()
q.sort()
r.sort()

ans = 0
while x != 0 or y != 0:
    if r != []:
        if x > 0 and y > 0:
            maxi = max(p[-1], q[-1], r[-1])
            if maxi == p[-1]:
                ans += p.pop()
                x -= 1
            elif maxi == q[-1]:
                ans += q.pop()
                y -= 1
            else:
                ans += r.pop()
                if p[-x] > q[-y]:
                    y -= 1
                else:
                    x -= 1
        elif x == 0:
            maxi = max(q[-1], r[-1])
            if maxi == q[-1]:
                ans += q.pop()
                y -= 1
            else:
                ans += r.pop()
                y -= 1
        else:
            maxi = max(p[-1], r[-1])
            if maxi == p[-1]:
                ans += p.pop()
                x -= 1
            else:
                ans += r.pop()
                x -= 1
    else:
        if x > 0 and y > 0:
            maxi = max(p[-1], q[-1])
            if maxi == p[-1]:
                ans += p.pop()
                x -= 1
            elif maxi == q[-1]:
                ans += q.pop()
                y -= 1
        elif x == 0:
            ans += q.pop()
            y -= 1
        else:
            ans += p.pop()
            x -= 1

print(ans)

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
lia=list(map(int,input().split()))
lib=list(map(int,input().split()))
lic=list(map(int,input().split()))
lia.sort(reverse=True)
lib.sort(reverse=True)
lic.sort(reverse=True)
ans=lia[:x]+lib[:y]
ans.sort()
cnt=0
now=0
while cnt < x+y and now < c:
  if lic[now] > ans[cnt]:
    ans[cnt]=lic[now]
    now+=1
  cnt+=1
print(sum(ans))

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=sorted(p, reverse=True)[:x]
q=sorted(q, reverse=True)[:y]
r.sort(reverse=True)
s = p+q
res = sum(s)
heapq.heapify(s)
for val in r:
    suffix = heapq.heappop(s)
    if suffix < val:
        heapq.heappush(s, val)
        res += val-suffix
    else:
        break
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()))

P_ = [0]*a
Q_ = [0]*b
for i in range(a):
    P_[i] = P[i]*(-1)

for i in range(b):
    Q_[i] = Q[i]*(-1)

import heapq
heapq.heapify(P_)
heapq.heapify(Q_)

R.sort(reverse=True)

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

P_ = P[0:x]
Q_ = Q[0:y]
import heapq
heapq.heapify(P_)
heapq.heapify(Q_)
for i in range(c):
    r = R[i]
    p = heapq.heappop(P_)
    q = heapq.heappop(Q_)
    if p <= r and r <= q:
        heapq.heappush(P_, r)
        heapq.heappush(Q_, q)
    elif q <= r and r <= p:
        heapq.heappush(P_, p)
        heapq.heappush(Q_, r)
    elif p <= q <= r:
        heapq.heappush(P_, r)
        heapq.heappush(Q_, q)
    elif q <= p <= r:
        heapq.heappush(P_, p)
        heapq.heappush(Q_, r)
    else:
        heapq.heappush(P_, p)
        heapq.heappush(Q_, q)
#print(P_)
#print(Q_)
print(sum(P_)+sum(Q_))

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()
P = P[::-1]
Q = Q[::-1]
R = R[::-1]


ans = 0

np = nq = nr = 0
x = y = 0
k = 0

while k < X + Y:
    p = -1 if np == X else P[np]
    q = -1 if nq == Y else Q[nq]
    r = -1 if nr == C else R[nr]
    # print(p, q, r)

    if p == max(p, q, r):
        ans += p
        np += 1
    elif q == max(p, q, r):
        ans += q
        nq += 1
    else:
        ans += r
        nr += 1
    k += 1

print(ans)

Submission status: Accepted

Solution code:


def solve(X, Y, P, Q, R):
    P = sorted(P, reverse=True)[:X]
    Q = sorted(Q, reverse=True)[:Y]
    R.sort(reverse=True)
    PQ = sorted(P + Q)
    i = 0
    while i < len(R) and i < len(PQ) and R[i] > PQ[i]:
        PQ[i], R[i] = R[i], PQ[i]
        i += 1
    return sum(PQ)


if __name__ == "__main__":
    X, Y, A, B, C = tuple(map(int, input().split(" ")))
    P = list(map(int, input().split(" ")))
    Q = list(map(int, input().split(" ")))
    R = list(map(int, input().split(" ")))
    print(solve(X, Y, P, Q, R))

Submission status: Accepted

Solution code:
import sys
import heapq

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()
r=[-r[i] for i in range(0,C)]
apple=p[A-X:A]+q[B-Y:B]
heapq.heapify(apple)
heapq.heapify(r)
ans=0
while True:
    if apple and r:
        a=heapq.heappop(apple)
        b=-heapq.heappop(r)
        if b>a:
            ans+=b
        else:
            heapq.heappush(apple,a)
            break
    else:
        break

apple=list(apple)
print(ans+sum(apple))

Submission status: Accepted

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

apples = sorted(chain(((v,0) for v in P), ((v,1) for v in Q),((v,2) for v in R)))

res = 0
total = 0
x = 0
y = 0
for v,k in reversed(apples):
    if k == 0:
        if x < X:
            x += 1
            total += 1
            res += v
    elif k == 1:
        if y < Y:
            y += 1
            total += 1
            res += v
    else:
        total += 1
        res += v
    if total == X+Y:
        break

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

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

x_2 = 0
y_2 = 0

for i in range(c):
  minimum = r[x_2 + y_2]
  if x > x_2:
    minimum = min(minimum, p[x - x_2 - 1])
  if y > y_2:
    minimum = min(minimum, q[y - y_2 - 1])
  
  if x > x_2 and minimum == p[x - x_2 - 1]:
    x_2 += 1
  elif y > y_2 and minimum == q[y - y_2 - 1]:
    y_2 += 1

print(sum(p[:(x - x_2)]) + sum(q[:(y - y_2)]) + sum(r[:(x_2 + y_2)]))

Submission status: Accepted

Solution code:
import numpy as np

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

p_selected = p_sorted[0:X]
q_selected = q_sorted[0:Y]

list_selected = sorted((p_selected + q_selected + r_sorted), reverse=True)
list_top = list_selected[0:X+Y]
answer = np.sum(list_top)
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 = sorted(p,reverse=True)
q = sorted(q,reverse=True)
r = sorted(r,reverse=True)
p = p[:X]
q = q[:Y]
tmp_rev = p+q
tmp = sorted(tmp_rev)
ans = sum(tmp)
a = len(tmp)
b = len(r)
if a<=b:
  for x,y in zip(tmp,r[:a]):
    if y>x:
      ans+=y-x
    else:
      break
else:
  for x,y in zip(tmp[:b],r):
    if y>x:
      ans+=y-x
    else:
      break
      
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, A, B, C, P, Q, R):
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort()
    P = P[:N]
    Q = Q[:M]

    S = P + Q
    heapq.heapify(S)
    for r in R:
        if r > S[0]:
            heapq.heappop(S)
            heapq.heappush(S, r)

    return sum(S)


def main():
    N, M, A, B, C = read_int_n()
    P = read_int_n()
    Q = read_int_n()
    R = read_int_n()
    print(slv(N, M, A, B, C, P, Q, R))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = [0] * 3
    U = [0] * 2
    U[0], U[1], N[0], N[1], N[2] = map(int, input().split())
    V = [None] * 3
    V[0] = sorted(list(map(int, input().split())), reverse=True)
    V[1] = sorted(list(map(int, input().split())), reverse=True)
    V[2] = sorted(list(map(int, input().split())), reverse=True)
    idx = [0] * 3
    total = 0
    while sum(idx) < sum(U):
        mx = (0, -1)
        for i in range(3):
            if idx[i] < N[i] and (i >= 2 or idx[i] < U[i]):
                mx = max(mx, (V[i][idx[i]], i))
        idx[mx[1]] += 1
        total += mx[0]
    print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x,y,a,b,c = (int(x) for x 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()]
s = []
p.sort()
p.reverse()
q.sort()
q.reverse()
r.sort()
r.reverse()

if a>=x and b>=y:
    s = p[0:x] + q[0:y]
elif a>=x and b<y:
    s = p[0:a] + q + r[0:y-b]
    del r[0:y-b]
elif a<x and b>=y:
    s = p + r[0:x-a] + q[0:b]
    del r[0:x-a]
else:
    s = p + q + r[0:x+y-a-b]
    del r[0:x+y-a-b]
s.sort()
i = 0
while s[i]<r[i]:
    s[i] = r[i]
    i += 1
    if i>=len(r):
        break

print(sum(s))

Submission status: Accepted

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

tr = ps[:x]
tg = qs[:y]
added = 0
for e in rs:
  if len(tg) != 0 and len(tr) != 0:
    if tr[-1] >= tg[-1] and e > tg[-1]:
      tg.pop()
      added += e
    elif tg[-1] > tr[-1] and e > tr[-1]:
      tr.pop()
      added += e
    else:
      break
  elif len(tg) != 0:
    if e > tg[-1]:
      tg.pop()
      added += e
    else:
      break
  elif len(tr) != 0:
    if e > tr[-1]:
      tr.pop()
      added += e
    else:
      break
  else:
    break
print(sum(tg)+sum(tr)+added)

Submission status: Accepted

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

p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
r_list = list(map(int, input().split()))

p_list.sort()
p_list.reverse()
q_list.sort()
q_list.reverse()
r_list.sort()
r_list.reverse()


total = sum(p_list[:X]) + sum(q_list[:Y])

x = X - 1
y = Y - 1
c = 0
while c < C:
	changed = False
	if y >= 0 and (x < 0 or p_list[x] >= q_list[y]):
		if q_list[y] < r_list[c]:
			total += r_list[c] - q_list[y]
			y -= 1
			c += 1
			changed = True
	if c >= C:
		break
	if x >= 0 and (y < 0 or p_list[x] <= q_list[y]):
		if p_list[x] < r_list[c]:
			total += r_list[c] - p_list[x]
			x -= 1
			c += 1
			changed = True
	if not changed:
		break

print(total)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = sorted(p, reverse=True)
    q = sorted(q, reverse=True)
    d = sorted(p[:X] + q[:Y] + r)
    print(sum(sorted(d, reverse=True)[:X+Y]))

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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.sort(reverse=True)
q.sort(reverse=True)
s = p[:x] + q[:y] + r
s.sort(reverse=True)
print(sum(s[:x + y]))