Problem:


Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

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

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

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

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

Eat the 2-nd red apple.

Eat the 1-st green apple.

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

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

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

Sample Output 3

74



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


Sample output:
12

Submission status: Runtime Error

Solution code:
eat_RED,eat_GR,hav_RED,hav_GR,hav_CL=map(int, input().split())
RED_yam=list(map(int, input().split()))
GR_yam=list(map(int, input().split()))
CL_yam=list(map(int, input().split()))

RED_sub=[]
GR_sub=[]
for i in range(eat_RED):
    RED_sub.append(max(RED_yam))
    RED_yam.remove(max(RED_yam))
for i in range(eat_GR):
    GR_sub.append(max(GR_yam))
    GR_yam.remove(max(GR_yam))
flag=True
while flag:
    flag=False
    if min(RED_sub)<max(CL_yam) and min(GR_sub)<max(CL_yam):
        flag=True
        if min(RED_sub)<min(GR_sub):
            RED_sub.remove(min(RED_sub))
            RED_sub.append(max(CL_yam))
            CL_yam.remove(max(CL_yam))
        else:
            GR_sub.remove(min(GR_sub))
            GR_sub.append(max(CL_yam))
            CL_yam.remove(max(CL_yam))
    elif min(RED_sub)<max(CL_yam):
        flag=True
        RED_sub.remove(min(RED_sub))
        RED_sub.append(max(CL_yam))
        CL_yam.remove(max(CL_yam))
    elif min(GR_sub)<max(CL_yam):
        flag=True
        GR_sub.remove(min(GR_sub))
        GR_sub.append(max(CL_yam))
        CL_yam.remove(max(CL_yam))
print(sum(GR_sub)+sum(RED_sub))

Submission status: Runtime Error

Solution code:
import collections
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)
    countA = 0
    countB = 0
    ans = 0
    i = 0
    for j in range(X):
        if j < C:
            if p[j] > r[i]:
                countA += 1
                ans += p[j]
            else:
                ans += r[i]
                i += 1
        else:
            ans += p[j]
            countA += 1
    for j in range(Y):
        if j < B:
            if q[j] > r[i]:
                countB += 1
                ans += q[j]
            else:
                ans += r[i]
                i += 1
        else:
            ans += q[j]
            countB += 1
    return ans


if __name__ == '__main__':
    # main()
    print(main())

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = [int(i) for i in input().split(" ")]
q = [int(i) for i in input().split(" ")]
r = [int(i) for i in input().split(" ")]
p = sorted(p)
q = sorted(q)
r = sorted(r)
x_ = a-x
y_ = b-y
ans = sum(p[x_:]) + sum(q[y_:])
for i in r[::-1]:
    if p[x_] > q[y_]:
        if q[y_] < i:
            ans += i - q[y_]
            y_ += 1
        else:
            break
    else:
        if p[x_] < i:
            ans += i - p[x_]
            x_ += 1
        else:
            break
print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int, input().strip().split())
p = list(map(int, input().strip().split()))
q = list(map(int, input().strip().split()))
r = list(map(int, input().strip().split()))
p.sort()
q.sort()
r.sort()
sum = 0
a = []
b = []
for i in range(X):
    if len(r) > 0:
        if r[-1] > p[-1]:
            sum += r[-1]
            del r[-1]
        else:
            sum += p[-1]
            a.append(p[-1])
            del p[-1]
    else:
        sum += p[-1]
        a.append(p[-1])
        del p[-1]
for i in range(Y):
    if len(r) > 0:
        if r[-1] > q[-1]:
            sum += r[-1]
            del r[-1]
        else:
            sum += q[-1]
            b.append(q[-1])
            del q[-1]
    else:
        sum += q[-1]
        b.append(q[-1])
        del q[-1]


a.sort()
b.sort()
for i in range(X-len(a)):
    if q[-1] > a[-1]:
        sum = sum - a[-1] + q[-1]
        del a[-1]
        del q[-1]
for i in range(Y-len(b)):
    if p[-1] > b[-1]:
        sum = sum - b[-1] + p[-1]
        del b[-1]
        del p[-1]

print(sum)

Submission status: Runtime Error

Solution code:
x,y,a,b,c=map(int,input().split())
p,q,r=[reversed(sorted(list(map(int,input())))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque


def main():
    X, Y, A, B, C = map(int, input().split(' '))
    a_list = list(map(int, input().split(' ')))
    b_list = list(map(int, input().split(' ')))
    c_list = list(map(int, input().split(' ')))
    a_deq = deque(sorted(a_list, reverse=True)[:X])
    b_deq = deque(sorted(b_list, reverse=True)[:Y])
    c_deq = deque(sorted(c_list, reverse=True))
    result = sum(a_deq) + sum(b_deq)
    while len(c_deq) != 0:
        min_ab = 0
        if a_deq[-1] < b_deq[-1]:
            min_ab = a_deq.pop()
        else:
            min_ab = b_deq.pop()
        if min_ab >= c_deq[0]:
            break
        result += c_deq.popleft() - min_ab

    print(result)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
x, y, a, b, c = list(map(int, input().split()))
ret = (a + b) * [0]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
X = []
Y = []
A.sort()
B.sort()
C.sort()
if a < x :
    X = ((x - a) * [0]) + A
else:
    X = A[(a - x):]
if b < y:
    Y = ((y - b) * [0]) + B
else:
    Y = B[(b - y):]
idxA = 0
idxB = 0
idxC = c - 1
for i in reversed(range(c)):
    if idxA == len(X) and idxB == len(Y):
        break
    if idxA == len(X):
        if C[idxC] > Y[idxB]:
            Y[idxB] = C[idxC]
            idxB += 1
        else:
            break
    if idxB == len(Y):
        if C[idxC] > X[idxA]:
            X[idxA] = C[idxC]
            idxA += 1
        else:
            break
    if C[idxC] < min(X[idxA], Y[idxB]):
        break
    if X[idxA] <= Y[idxB]:
        X[idxA] = C[idxC]
        idxA += 1
    else:
        Y[idxB] = C[idxC]
        idxB += 1
print((sum(X) + sum(Y)))

Submission status: Runtime Error

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)

while(True):
    if P[0] >= R[0] and Q[0] >= R[0]:
        break
    if P[0] > Q[0]:
        if Q[0] < R[0]:
            heapq.heappop(Q)
            heapq.heappush(Q, R[0])
    else:
        if P[0] < R[0]:
            heapq.heappop(P)
            heapq.heappop(P, R[0])
    R = R[1:]
    if len(R) == 0:
        break
print(sum(P) + sum(Q))

Submission status: Runtime Error

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

p =sorted(p)[::-1][:x][::-1]
q =sorted(q)[::-1][:y][::-1]
r =sorted(r)[::-1][:x+y]


pp = 0
qq = 0
for rr in r:
    if p[pp]<rr or q[qq]<rr:
        if p[pp]<q[qq]:
            p[pp] = rr
            pp += 1
        else:
            q[qq] = rr
            qq +=1

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

Submission status: Runtime Error

Solution code:
import heapq as hq
X,Y,A,B,C=map(int,input().split(' '))
P,Q,R=[sorted(list(map(int,input().split(' '))),reverse=True) for i in range(3)]
P=P[:X]
Q=Q[:Y]
hq.heapify(P)
hq.heapify(Q)
R=R[::-1]
while len(R)>0:
    p=hq.heappop(P)
    q=hq.heappop(Q)
    r=R.pop()
    if r<min(p,q):
        hq.heappush(P,p)
        hq.heappush(Q,q)
        break
    if p<=q and p<r:
        hq.heappush(P,r)
        hq.heappush(Q,q)
    elif p>q and q<r:
        hq.heappush(P,p)
        hq.heappush(Q,r)
print(sum(P)+sum(Q))

Submission status: Runtime Error

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]"):
    sp = list(reversed(sorted(p)))
    sq = list(reversed(sorted(q)))
    sr = list(sorted(r))
    # print(X)
    # print(Y)
    spi = sp[:X]
    sqi = sq[:Y]
    # print(spi)
    # print(sqi)
    # print(sr)
    rIndex = len(sr)-1
    pIndex = len(spi)-1
    qIndex = len(sqi)-1
    while rIndex >= 0:
        if (pIndex < 0 or sr[rIndex] < spi[pIndex]) and (qIndex < 0 or sr[rIndex] < sqi[qIndex]):
            break
        if pIndex < 0:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
            continue
        if qIndex < 0:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
            continue
        if sr[rIndex] - spi[pIndex] > sr[rIndex] - sqi[qIndex]:
            spi[pIndex] = sr[rIndex]
            pIndex-=1
        else:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
        rIndex-=1
    print(sum(spi)+sum(sqi))
    return


# 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: Runtime Error

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

x, y, a, b, c = LI()
p = LI()
q = LI()
r = LI()

p.sort(reverse=1)
q.sort(reverse=1)
r.sort(reverse=1)
r = list(accumulate(r))
ans = 0
h = []
s = sum(p[:x])
s += sum(q[:y])
for i in p[:x]:
    heappush(h, i)
for i in q[:y]:
    heappush(h, i)
ans = s
for i in range(c):
    f = heappop(h)
    s -= f
    tmp = s + r[i]
    ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

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

total = 0
while x > 0 and y > 0:
  if P[-1] >= Q[-1]:
    x -= 1
    if R[-1] > P[-1]:
      total += R.pop()
    else:
      total += P.pop()
  else:
    y -= 1
    if R[-1] > Q[-1]:
      total += R.pop()
    else:
      total += Q.pop()
      
while x > 0:
  x -= 1
  if R[-1] > P[-1]:
    total += R.pop()
  else:
    total += P.pop()

while y > 0:
  y -= 1
  if R[-1] > Q[-1]:
    total += R.pop()
  else:
    total += Q.pop()
print(total)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
q.sort()
r.sort()
red = []
green = []
less = []
for i in range(x):
    red.append(p[-i-1])
for i in range(y):
    green.append(q[-i-1])
# print(red, green)
for i in range(c):
    if red[-1] < green[-1]:
        if red[-1] <= r[-i-1]:
            del red[-1]
            less.append(r[-i-1])
    else:
        if green[-1] <= r[-i-1]:
            del green[-1]
            less.append(r[-i-1])
ans = sum(red) + sum(green) + sum(less)
# print(red, green, less)
print(ans)

Submission status: Runtime Error

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

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


arr = p + q
arr.sort()
#print(arr)
#print(r)

for i in range(len(r)):
    if arr[i] <= r[i]:
        arr[i] = r[i]

print(sum(arr))

Submission status: Runtime Error

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
L = p + r
L.sort(reverse = True)
ans = 0
for i in range(X):
  ans += int(L[i])
for i in range(X):  
  L.remove(L[i])
set(L) = set(L)-set(p)  
M = q + list(L)
M.sort(reverse = True)
abc = 0  
for i in range(Y):
  abc += int(M[i])
print(ans + abc)

Submission status: Runtime Error

Solution code:
def resolve():
    x, y, a, b, c = map(int, input().split())
    p = sorted(list(map(int, input().split())))[-x:]
    q = sorted(list(map(int, input().split())))[-y:]
    r = sorted(list(map(int, input().split())), reverse=True)
	p = sorted(p + q)
    for i in range(min(a + b, c)):
        if p[i] < r[i]:
            p[i] = r[i]
        else:
            break
    print(sum(p))


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

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

r.sort(reverse=True)
g.sort(reverse=True)
nc.sort(reverse=True)

selected = r[:X] + g[:Y]
selected.sort()

for i in range(X+Y):
  if selected[i] < nc[i]:
    selected[i] = nc[i]
    
print(sum(selected))

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
p.sort()
q.sort()
r.sort(reverse=True)
p = p[A - X:]
q = q[B - Y:]
pi = 0
qi = 0
for v in r:
    if p[pi] < v or q[qi] < v:
        if p[pi] < q[qi]:
            p[pi] = v
            pi = min(pi + 1, A - 1)
        else:
            q[qi] = v
            qi = min(qi + 1, B - 1)
    else:
        break
print(sum(p) + sum(q))

Submission status: Runtime Error

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
q.sort()
r.sort()
ans = 0
for i in range(1,X+1):
    if(p[A-i] >= r[C-1]):
        ans += p[A-i]
    else:
        ans += r[C-1]
        del r[C-1]
for i in range(1,Y+1):
    if(q[B-i] >= r[C-1]):
        ans += q[A-i]
    else:
        ans += r[C-1]
        del r[C-1]
print(ans)

Submission status: Runtime Error

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

r.sort(reverse=True)
g.sort(reverse=True)
nc.sort(reverse=True)

selected = r[:X] + g[:Y]
selected.sort()

for i in range(C):
  if selected[i] < nc[i]:
    selected[i] = nc[i]
    
print(sum(selected))

Submission status: Runtime Error

Solution code:

import collections
import heapq
import sys
from functools import lru_cache

sys.setrecursionlimit(20000)

pq = []
ans = 0


def solve(x, y, a, b, c, redApples, greenApples, colorlessApples):
    for red in redApples:
        heapq.heappush(pq, (-red, 1))

    for green in greenApples:
        heapq.heappush(pq, (-green, 2))

    for colorless in colorlessApples:
        heapq.heappush(pq, (-colorless, 3))

    traverse(0, 0, 0)

    return ans


@lru_cache(None)
def traverse(eatenRed, eatenGreen, deliciousness):
    global ans
    ans = max(ans, deliciousness)

    if eatenRed + eatenGreen < n:
        d, appleType = heapq.heappop(pq)
        d = abs(d)

        if appleType == 1 and eatenRed < x:
            traverse(eatenRed + 1, eatenGreen, deliciousness + d)
        elif appleType == 2 and eatenGreen < y:
            traverse(eatenRed, eatenGreen + 1, deliciousness + d)
        elif appleType == 3:
            if eatenRed < x:
                traverse(eatenRed + 1, eatenGreen, deliciousness + d)
                heapq.heappush(pq, (-d, appleType))

            if eatenGreen < y:
                traverse(eatenRed, eatenGreen + 1, deliciousness + d)
        else:
            if len(pq) > 0:
                traverse(eatenRed, eatenGreen, deliciousness)


[x, y, a, b, c] = list(map(int, input().strip().split()))

n = x + y

redApples = list(map(int, input().strip().split()))
greenApples = list(map(int, input().strip().split()))
colorlessApples = list(map(int, input().strip().split()))

print(solve(x, y, a, b, c, redApples, greenApples, colorlessApples))

Submission status: Runtime Error

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

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

Apple = sorted(Red + Green)
for i in range(C):
    print(Apple[i], r[i])
    if Apple[i] < r[i]:
        Apple[i] = r[i]
print(sum(Apple))

Submission status: Runtime Error

Solution code:
x,y,a,b,c=map(int,input().split())
p,q,r=[reversed(sorted(list(map(int,input()))) for i in range(3))]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)

Submission status: Runtime Error

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

red = sorted(red, reverse=True)[:X]
green = sorted(green, reverse=True)[:Y]
ans = sum(red) + sum(green)

white = sorted(white, reverse=True)
for i in range(len(white)):
    current = white[i]
    if red[-1] < green[-1] and current > red[-1] and len(red) != 0:
        ans += current - red[-1]
        red.pop(-1)
    elif red[-1] >= green[-1] and current > green[-1] and len(green) != 0:
        ans += current - green[-1]
        green.pop(-1)
    else:
        break

print(ans)

Submission status: Runtime Error

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, A + B)):
    if pq[i] < r[i]:
        pq[i] = r[i]
print(sum(pq))

Submission status: Runtime Error

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,y,a,b,c = mi()
p = li()
q = li()
r = li()

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


qp = True
qq = True
plef = a - x
qlef = b - y
ans = 0
rlen = len(r) -1
while True:
    if rlen <0:
        break
    
    if plef == x:
        if qlef == y:
            break
        else :
            ima = q[qlef]
            tmp = "q"
    elif qlef == y:
        ima = p[plef]
        tmp = "p"
    elif p[plef] >= q[qlef]:
        ima = q[qlef]
        tmp = "q"
    else:
        ima = p[plef]
        tmp = "p"
    if ima < r[rlen]:
        ans += r[rlen]
        rlen -= 1
        if tmp == "q":
            qlef += 1
        else:
            plef += 1
    
    else:
        break
    
for i in range(plef,a):
    ans += p[i]

for i in range(qlef,b):
    ans +=q[i]
print(ans)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
Pl = 0
Ql = 0
Rb = 0

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

P = P[0:x]
Q = Q[0:y]

while P[-1 - Pl] < R[0 + Rb] or Q[-1 - Ql] < R[0 + Rb]:
    if Q[-1 - Ql] >= P[-1 - Pl]:
        P[-1 - Pl] = R[0 + Rb]
        Pl = Pl + 1
    else:
        Q[-1 - Ql] = R[0 + Rb]
        Ql = Ql + 1

    Rb = Rb + 1
    if Rb == len(R):
        break

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

Submission status: Runtime Error

Solution code:
from heapq import *
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):
    ans = max(ans,data_r[i] + data_pq[x+y-i])

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int, read().split())
AB = zip(m, m)

MOD = 10 ** 9 + 7

graph = [[] for _ in range(N + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

root = 1
parent = [0] * (N + 1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)

fact = [1] * (N + 10)
for n in range(1, N + 10):
    fact[n] = n * fact[n - 1] % MOD
fact_inv = [1] * (N + 10)
fact_inv[-1] = pow(fact[-1], MOD - 2, MOD)
for n in range(N + 9, 0, -1):
    fact_inv[n - 1] = fact_inv[n] * n % MOD

size_d = [0] * (N + 1)
dp_d = [1] * (N + 1)

for v in order[::-1]:
    dp_d[v] *= fact[size_d[v]]
    dp_d[v] %= MOD
    p = parent[v]
    s = size_d[v] + 1
    size_d[p] += s
    dp_d[p] *= fact_inv[s] * dp_d[v]
    dp_d[p] %= MOD

size_u = [N - 2 - x for x in size_d]
dp_u = [1] * (N + 1)

for v in order[1:]:
    p = parent[v]
    x = dp_d[p]
    x *= dp_u[p]
    x *= fact_inv[size_d[p]]
    x *= fact[size_d[v] + 1]
    x *= pow(dp_d[v], MOD - 2, MOD)
    x *= fact[size_u[v]]
    x *= fact_inv[size_u[p] + 1]
    dp_u[v] = x % MOD

for xd, xu, sd, su in zip(dp_d[1:], dp_u[1:], size_d[1:], size_u[1:]):
    su += 1
    x = xd * xu * fact[sd + su] * fact_inv[sd] * fact_inv[su] % MOD
    print(x)

Submission status: Runtime Error

Solution code:

import collections
import heapq
import sys
from functools import lru_cache

sys.setrecursionlimit(20000)

pq = []
ans = 0


def solve(x, y, a, b, c, redApples, greenApples, colorlessApples):
    for red in redApples:
        heapq.heappush(pq, (-red, 1))

    for green in greenApples:
        heapq.heappush(pq, (-green, 2))

    for colorless in colorlessApples:
        heapq.heappush(pq, (-colorless, 3))

    traverse(0, 0, 0)

    return ans


@lru_cache(None)
def traverse(eatenRed, eatenGreen, deliciousness):
    global ans
    ans = max(ans, deliciousness)

    if eatenRed + eatenGreen < n:
        d, appleType = heapq.heappop(pq)
        d = abs(d)

        if appleType == 1 and eatenRed < x:
            traverse(eatenRed + 1, eatenGreen, deliciousness + d)
        elif appleType == 2 and eatenGreen < y:
            traverse(eatenRed, eatenGreen + 1, deliciousness + d)
        elif appleType == 3:
            if eatenRed < x:
                traverse(eatenRed + 1, eatenGreen, deliciousness + d)
                heapq.heappush(pq, (-d, appleType))

            if eatenGreen < y:
                traverse(eatenRed, eatenGreen + 1, deliciousness + d)
        else:
            traverse(eatenRed, eatenGreen, deliciousness)


[x, y, a, b, c] = list(map(int, input().strip().split()))

n = x + y

redApples = list(map(int, input().strip().split()))
greenApples = list(map(int, input().strip().split()))
colorlessApples = list(map(int, input().strip().split()))

print(solve(x, y, a, b, c, redApples, greenApples, colorlessApples))

Submission status: Runtime Error

Solution code:
import heapq


def resolve():
    x, y, a, b, c = map(int, input().split())
    p = sorted(list(map(int, input().split())))[-x:]
    q = sorted(list(map(int, input().split())))[-y:]
    r = sorted(list(map(int, input().split())), reverse=True)
    heapq.heapify(p)
    for i in q:
        heapq.heappush(p, i)
    ans = 0
    for i in range(min(a + b, c)):
        ans += max(heapq.heappop(p), r[i])
    if len(p):
        ans += sum(p)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

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

p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p=sorted(p,reverse=True)[:x]
q=sorted(q,reverse=True)[:y]
r=sorted(r)

p.extend(q)
p=sorted(p)

ans=0
for i in range(x+y):
    if p[-1]<r[-1]:
        ans+=r[-1]
        r.pop(-1)
    else:
        ans+=p[-1]
        p.pop(-1)
        
print(ans)

Submission status: Runtime Error

Solution code:

import collections
import heapq

pq = []
ans = 0


def solve(x, y, a, b, c, redApples, greenApples, colorlessApples):
    for red in redApples:
        heapq.heappush(pq, (-red, 1))

    for green in greenApples:
        heapq.heappush(pq, (-green, 2))

    for colorless in colorlessApples:
        heapq.heappush(pq, (-colorless, 3))

    traverse(0, 0, 0)

    return ans


def traverse(eatenRed, eatenGreen, deliciousness):
    global ans
    ans = max(ans, deliciousness)

    if eatenRed + eatenGreen < n:
        d, appleType = heapq.heappop(pq)
        d = abs(d)

        if appleType == 1 and eatenRed < x:
            traverse(eatenRed + 1, eatenGreen, deliciousness + d)
        elif appleType == 2 and eatenGreen < y:
            traverse(eatenRed, eatenGreen + 1, deliciousness + d)
        elif appleType == 3:
            if eatenRed < x:
                traverse(eatenRed + 1, eatenGreen, deliciousness + d)
                heapq.heappush(pq, (-d, appleType))

            if eatenGreen < y:
                traverse(eatenRed, eatenGreen + 1, deliciousness + d)
        else:
            traverse(eatenRed, eatenGreen, deliciousness)


[x, y, a, b, c] = list(map(int, input().strip().split()))

n = x + y

redApples = list(map(int, input().strip().split()))
greenApples = list(map(int, input().strip().split()))
colorlessApples = list(map(int, input().strip().split()))

print(solve(x, y, a, b, c, redApples, greenApples, colorlessApples))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)

N, X, Y=map(int, input().split())
ans=[0 for _ in range(N)]
link=[[]]+[[2]]+[[i-1, i+1] for i in range(2, N)]+[[N-1]]
link[X].append(Y)
link[Y].append(X)

def f(p, length, moto):
  ans[length]+=1
  for data in link[p]:
    if data!=moto:
      f(data, length+1, p)
      
for i in range(1, N+1):
  for data in link[i]:
    f(data, 1, i)
    
print(*ans[1:], sep='\n')

Submission status: Runtime Error

Solution code:
from collections import deque

x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
q.sort()
r.sort(reverse=True)
xlis = deque(p[len(p)-x:])
ylis = deque(q[len(q)-y:])
r = deque(r)
while True:
    if len(r) == 0:
        break
    xs = xlis.popleft()
    ys = ylis.popleft()
    rm = r.popleft()
    if rm < xs and rm < ys:
        xlis.append(xs)
        ylis.append(ys)
        break
    if xs < ys and xs < rm:
        xlis.append(rm)
        ylis.append(ys)
    elif ys < xs and ys < rm:
        ylis.append(rm)
        xlis.append(xs)
print(sum(xlis)+sum(ylis))

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
Pl = 0
Ql = 0
Rb = 0

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

P = P[0:x]
Q = Q[0:y]

while P[-1 - Pl] < R[0] or Q[-1 - Ql] < R[0]:
    if Q[-1 - Ql] >= P[-1 - Pl]:
        P[-1 - Pl] = R[0 + Rb]
        #P.sort(reverse = True)
        Pl = Pl + 1
    else:
        Q[-1 - Ql] = R[0 + Rb]
        #Q.sort(reverse=True)
        Ql = Ql + 1

    Rb = Rb + 1
    if Rb == len(R):
        break

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

Submission status: Runtime Error

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]
pq.sort()
for i in range(min(C, A + B)):
    if pq[i] < r[i]:
        pq[i] = r[i]
print(sum(pq))

Submission status: Runtime Error

Solution code:
import heapq


def resolve():
    x, y, a, b, c = map(int, input().split())
    p = sorted(list(map(int, input().split())))[-x:]
    q = sorted(list(map(int, input().split())))[-y:]
    r = sorted(list(map(int, input().split())), reverse=True)
    heapq.heapify(p)
    for i in q:
        heapq.heappush(p, i)
    ans = 0
    for i in range(min(a + b, c)):
        ans += max(heapq.heappop(p), r[i])
    print(ans + sum(p))


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

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)
# qs = list(map(int, input().split()))
rs = sorted(list(map(int, input().split())),reverse=True)

# cnt_x = 0
# cnt_y = 0
ps = ps[:X]
qs = qs[:Y]
cnt_c = 0
ans = sum(ps)+sum(qs)
while cnt_c < C:
    p = ps.pop()
    q = qs.pop()
    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)

Submission status: Runtime Error

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

input = sys.stdin.buffer.readline()

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

heapify(AB)
heapify(C)
ans = 0
for _ in range(len(C)):
    c = heappop(C)
    ab = heappop(AB)
    if ab > c:
        heappush(AB, ab)
        break
    heappush(AB, c)
ans = sum(AB)
print(ans)