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:
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.append(-1)
q.append(-1)
r.append(-1)
def count_sort(arr):
    max_num = max(arr)
    min_num = min(arr)
    count = [0] * (max_num - min_num + 1)
    for ele in arr:
        count[ele - min_num] += 1

    return [ele for ele, cnt in enumerate(count, start=min_num) for __ in range(cnt)]
ps=count_sort(p)
qs=count_sort(q)
rs=count_sort(r)
d=0
e=0
f=0
ans=0
def me1():
    global ans
    
    if max(ps[-1],qs[-1],rs[-1])==ps[-1]:
        ans+=ps[-1]
        ps.pop(-1)
        global d
        d+=1
    elif max(ps[-1],qs[-1],rs[-1])==qs[-1]:
        ans+=qs[-1]
        qs.pop(-1)
        global e
        e+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
def me2():
    global ans
    if max(ps[-1],rs[-1])==ps[-1]:
        ans+=ps[-1]
        ps.pop(-1)
        global d
        d+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
def me3():
    global ans
    if max(qs[-1],rs[-1])==qs[-1]:
        ans+=qs[-1]
        qs.pop(-1)
        global e
        e+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
for i in range(x+y):
    #print(ans)
    #print(i)
    
    if d>=x:
        #print('x')
        me3()
    elif e>=y:
        #print('y')
        me2()
    else:
        #print('z')
        me1()
print(ans)

Submission status: Runtime Error

Solution code:

x,y,a,b,c = map(int, input().split())
A = list(map(int, input().split())) 
B = list(map(int, input().split())) 
C = list(map(int, input().split())) 

A.sort()
B.sort()
C.sort(reverse=True)

X = A[len(A)-x:]
Y = B[len(B)-y:]

print(X,Y,C)

xi = 0
yi = 0
ci = 0
for ci in range(c):
    if C[ci] <= X[xi] and C[ci] <= Y[yi]:
        break
    
    if X[xi] < Y[yi]:
        if C[ci] > X[xi]:
            X[xi] = C[ci]
            xi = xi + 1
    else:
        if C[ci] > Y[yi]:
            Y[yi] = C[ci]
            yi = yi + 1

print(X,Y)
print(sum(X)+sum(Y))

Submission status: Runtime Error

Solution code:
import heapq

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

mp = list(map(lambda x:int(x)*(-1), p)) #各要素を-1倍
heapq.heapify(mp)
mq = list(map(lambda x:int(x)*(-1), q)) #各要素を-1倍
heapq.heapify(mq)
mr = list(map(lambda x:int(x)*(-1), r)) #各要素を-1倍
heapq.heapify(mr)

#print(heapq.heappop(mp)*(-1))  最大値の取得と表示

red = []
green = []
muji = []

for i in range(XYABC[0]):
    red.append(heapq.heappop(mp)*(-1))
for i in range(XYABC[1]):
    green.append(heapq.heappop(mq)*(-1))
mrange = XYABC[0] + XYABC[1] if (XYABC[0] + XYABC[1]) < len(mr) else len(mr)
for i in range(mrange):
    muji.append(heapq.heappop(mr)*(-1))

sum = 0
rindex = 0
mindex = 0
for i in range(XYABC[0]):
    if ((mindex <= mrange) and red[rindex] < muji[mindex]):
        sum += muji[mindex]
        mindex += 1
    else:
        sum += red[rindex]
        rindex += 1

gindex = 0
for i in range(XYABC[1]):
    if ((mindex <= mrange) and green[gindex] < muji[mindex]):
        sum += muji[mindex]
        mindex += 1
    else:
        sum += green[gindex]
        gindex += 1


print(sum)

Submission status: Runtime Error

Solution code:
x,y,a,b,c = map(int, input().split())
al = list(map(int, int().split()))
bl = list(map(int, int().split()))
cl = list(map(int, int().split()))
al.sort()
bl.sort()
cl.sort()
result = 0
i = a-1
j = a-x
while i>=j:
  if(al[i]>= max(cl)):
    result = result+al[i]
  else:
    result = result+max(cl)
  i-=1
k = b-1
l = b-y
while k>=l:
  if(bl[k]>= max(cl)):
    result = result+bl[k]
  else:
    result = result +max(cl)
  k-=1
print(result)

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.sort()
Q.sort()
R.sort()

p, q, r = A - X, B - Y, C - 1
ans = 0

Amin = P[p]
Bmin = Q[q]
Cmax = R[r]
for i in range(C):
    if Cmax <= min(Amin, Bmin):
        break
    else:
        if Amin <= Bmin and p < A:
            ans += Cmax
            r -= 1
            p += 1
            Amin = P[p]
        elif Amin >= Bmin and q < B:
            ans += Cmax
            r -= 1
            q += 1
            Bmin = Q[q]
        Cmax = R[r]
ans += sum(P[p:])+sum(Q[q:])
print(ans)

Submission status: Runtime Error

Solution code:
import heapq as hq

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

pq1 = []; hq.heapify(pq1) # 昇順
for i in range(X):
    hq.heappush(pq1, p[i])
for i in range(Y):
    hq.heappush(pq1, q[i])
pq2 = []; hq.heapify(pq2) # 降順で用いる
for i in range(C):
    hq.heappush(pq2, -r[i])
while (pq1[0] < -pq2[0]):
    hq.heappush(pq1, -hq.heappop(pq2))
    hq.heappop(pq1) # 順序を変えてみた
print(sum(pq1))

Submission status: Runtime Error

Solution code:
def resolve():
    X, Y, A, B, C = map(int, input().split())
    p = sorted(list(map(int, input().split())), reverse=True)
    q = sorted(list(map(int, input().split())), reverse=True)
    r = sorted(list(map(int, input().split())), reverse=True)
    pp, pq, pr = 0, 0, 0
    x, y = 0, 0
    ans = 0
    while X > 0 and Y > 0:
        while X > 0 and p[pp] > r[pr]:
            ans += p[pp]
            pp += 1
            X -= 1

        while Y > 0 and q[pq] > r[pr]:
            ans += q[pq]
            pq += 1
            Y -= 1
		
        if not X and not Y:
            break
        ans += r[pr]
        pr += 1
        if not X or X < Y:
            Y -= 1
        else:
            X -= 1

    print(ans)

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()))
r.append(0)
p.sort()
q.sort()
r.sort()
ans = 0
for i in range(1,X+1):
    if(p[A-i] > r[C]):
        ans += p[A-i]
    else:
        ans += r[C]
        del r[C]
for i in range(1,Y+1):
    if(q[B-i] > r[C]):
        ans += q[A-i]
    else:
        ans += r[C]
        del r[C]
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    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()]
    from heapq import heapify, heappop
    heapify(P)
    heapify(Q)
    heapify(R)
    eatP = 0
    eatQ = 0
    eatR = 0
    ans = 0
    while X != eatP and Y != eatQ and eatP + eatQ + eatR < X + Y:
        cur = min(P[0], Q[0], R[0])
        ans -= cur
        if R[0] == cur:
            heappop(R)
            eatR += 1
        elif P[0] == cur:
            heappop(P)
            eatP += 1
        elif Q[0] == cur:
            heappop(Q)
            eatQ += 1

    if eatP + eatQ + eatR == X + Y:
        return print(ans)
    if X != eatP:
        while X != eatP and eatP + eatQ + eatR < X + Y:
            cur = min(P[0], R[0])
            ans -= cur
            if R[0] == cur:
                heappop(R)
                eatR += 1
            elif P[0] == cur:
                heappop(P)
                eatP += 1
    elif Y != eatQ:
        while Y != eatQ and eatP + eatQ + eatR < X + Y:
            cur = min(Q[0], R[0])
            ans -= cur
            if R[0] == cur:
                heappop(R)
                eatR += 1
            elif Q[0] == cur:
                heappop(Q)
                eatQ += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = [-1 * i for i in p]
    q = [-1 * i for i in q]
    r = [-1 * i for i in r]
    heapq.heapify(p)
    heapq.heapify(q)
    heapq.heapify(r)

    result = 0
    for i in range(X):
        smallest_p = heapq.nsmallest(1, p)
        smallest_r = heapq.nsmallest(1, r)
        if smallest_p < smallest_r:
            temp = heapq.heappop(p) * -1
            result += temp
        else:
            temp = heapq.heappop(r) * -1
            result += temp
    #    print(temp)
    #print('---')

    for i in range(Y):
        smallest_q = heapq.nsmallest(1, q)
        smallest_r = heapq.nsmallest(1, r)
        if smallest_q < smallest_r:
            temp = heapq.heappop(q) * -1
            result += temp
        else:
            temp = heapq.heappop(r) * -1
            result += temp
    #    print(temp)
    print(result)
    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:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    X, Y, A, B, C = lr()
    p = sorted(lr())[-X:][::-1]
    q = sorted(lr())[-Y:][::-1]
    r = sorted(lr())
    tmp = r.pop()
    pt = p[-1]
    qt = q[-1]
    ext = [0]
    while (tmp > pt or tmp > qt) and len(r) >= 0:
        if pt > qt:
            g = q.pop()
            ext.append(tmp)
            qt = q[-1]
        else:
            g = p.pop()
            ext.append(tmp)
            pt = p[-1]
        if len(r) > 0:
            tmp = r.pop()
    print(sum(p)+sum(q)+sum(ext))
resolve()

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.sort()
q.sort()
r.sort()
red_remain = X
green_remain = Y
ans = 0
flag = False
for i in range(X+Y):
    if flag:
        flag = False
        continue
    if red_remain == 0 or len(p) == 0:
        if green_remain == 0 or len(q) == 0:
            ans += r.pop(-1)
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
        else:
            ans += q.pop(-1)
            green_remain -= 1
    elif green_remain == 0 or len(q) == 0:
        if p[-1] < r[-1]:
            ans += r.pop(-1)
        else:
            ans += p.pop(-1)
            red_remain -= 1
    else:
        if p[-1] < r[-1] and q[-1] < r[-1]:
            ans += r.pop(-1)
        elif p[-1] == q[-1]:
            if i == X+Y-1:
                ans += p[-1]
            else:
                flag = True
                ans += p.pop(-1) + q.pop(-1)
                red_remain -= 1
                green_remain -= 1
        elif p[-1] < q[-1]:
            ans += q.pop(-1)
            green_remain -= 1
        else:
            ans += p.pop((-1))
            red_remain -= 1
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()))

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


red=0
for _ in range(x):
	pMax=max(p)
	rMax=max(r)
	if pMax >rMax:
		red+=pMax
		p.pop(p.index(pMax))
	else:
		red+=rMax
		r.pop(r.index(rMax))

white=0
for _ in range(y):
	qMax=max(q)
	rMax=max(r)
	if qMax >rMax:
		red+=qMax
		q.pop(q.index(qMax))
	else:
		red+=rMax
		r.pop(r.index(rMax))

print(white+red)

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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
cur = sum(p[:X]) + sum(q[:Y])

eat = p[:X] + q[:Y]
eat.sort()
for i in range(C):
    if r[i] > eat[i]:
        eat[i] = r[i]

ans = sum(eat)
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()))

p = sorted(p)[a-x:]
q = sorted(q)[b-y:]
r = sorted(r, reverse=True)

pi = 0
qi = 0
ri = 0
while True:
    if ri > c-1: break
    if pi <= a-1 and qi <= b-1:
        if p[pi] < q[qi]:
            if p[pi] < r[ri]:
                p[pi] = r[ri]
                pi += 1
                ri += 1
            else:
                break
        else:
            if q[qi] < r[ri]:
                q[qi] = r[ri]
                qi += 1
                ri += 1
            else:
                break
    elif pi <= a-1:
        if p[pi] < r[ri]:
            p[pi] = r[ri]
            pi += 1
            ri += 1
        else:
            break
    elif qi <= b-1:
        if q[qi] < r[ri]:
            q[qi] = r[ri]
            qi += 1
            ri += 1
        else:
            break
    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())).sort()
q=list(map(int,input().split())).sort()
r=list(map(int,input().split())).sort()


red=0
for _ in range(x):
	pMax=max(p)
	rMax=max(r)
	if pMax >rMax:
		red+=pMax
		p.pop(p.index(pMax))
	else:
		red+=rMax
		r.pop(r.index(rMax))

white=0
for _ in range(y):
	qMax=max(q)
	rMax=max(r)
	if qMax >rMax:
		red+=qMax
		q.pop(q.index(qMax))
	else:
		red+=rMax
		r.pop(r.index(rMax))

print(white+red)

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

p.reverse()
q.reverse()
r.reverse()

qi = 0
ri = 0

lis = [0]

for i in range(B+C):

    if (qi == Y or qi == B) and ri==C:
        lis.append(float("-inf"))
    elif qi == Y or qi == B:
        lis.append(lis[-1] + r[ri])
        ri += 1
    elif ri == C:
        lis.append(lis[-1] + q[qi])
        qi += 1
    elif q[qi] > r[ri]:
        lis.append(lis[-1] + q[qi])
        qi += 1
    else:
        lis.append(lis[-1] + r[ri])

ans = 0

n = 0
for i in range(X+1):

    if i != 0:
        n += p[i-1]

    ans = max(ans , n + lis[X+Y-i])

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())))
P_=P[A-X:]
Q_=Q[B-Y:]
ans=sum(P_)+sum(Q_)
for i in range(X+Y):
    if len(R)==0 or len(P)+len(Q)==0 or min(P_[0],Q_[0])>R[-1]:
        break
    ans-=min(P_,Q_).pop(0)
    ans+=R.pop(-1)
print(ans)

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

X = A[(a - x):]
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[i] > Y[idxB]:
            Y[idxB] = C[i]
            idxB += 1
        else:
            break
    if idxB == len(Y):
        if C[i] > X[idxA]:
            X[idxA] = C[i]
            idxA += 1
        else:
            break
    if C[i] <= min(X[idxA], Y[idxB]):
        break
    if X[idxA] <= Y[idxB]:
        X[idxA] = C[i]
        idxA += 1
    else:
        Y[idxB] = C[i]
        idxB += 1
print((sum(X) + sum(Y)))

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 Pl == len(P):
        if Q[-1 - Ql] < R[0 + Rb]:
            Q[-1 - Ql] = R[0 + Rb]
            Ql = Ql + 1
        else:
            break
    elif Ql == len(Q):
        if P[-1 - Pl] < R[0 + Rb]:
            P[-1 - Pl] = R[0 + Rb]
            Pl = Pl + 1
        else:
            break

    elif 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:
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.append(-1)
q.append(-1)
r.append(-1)
def count_sort(arr):
    max_num = max(arr)
    min_num = min(arr)
    count = [0] * (max_num - min_num + 1)
    for ele in arr:
        count[ele - min_num] += 1

    return [ele for ele, cnt in enumerate(count, start=min_num) for __ in range(cnt)]
a=[9,1,3,2,6]
ps=count_sort(p)
qs=count_sort(q)
rs=count_sort(r)
d=0
e=0
f=0
ans=0
def me1():
    global ans
    
    if max(ps[-1],qs[-1],rs[-1])==ps[-1]:
        ans+=ps[-1]
        ps.pop(-1)
        global d
        d+=1
    elif max(ps[-1],qs[-1],rs[-1])==qs[-1]:
        ans+=qs[-1]
        qs.pop(-1)
        global e
        e+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
def me2():
    global ans
    if max(ps[-1],rs[-1])==ps[-1]:
        ans+=ps[-1]
        ps.pop(-1)
        global d
        d+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
def me3():
    global ans
    if max(qs[-1],rs[-1])==qs[-1]:
        ans+=qs[-1]
        qs.pop(-1)
        global e
        e+=1
    else:
        ans+=rs[-1]
        rs.pop(-1)
        global f
        f+=1
for i in range(x+y):
    #print(ans)
    #print(i)
    
    if d>=x:
        #print('x')
        me3()
    elif e>=y:
        #print('y')
        me2()
    else:
        #print('z')
        me1()
print(ans)

Submission status: Runtime Error

Solution code:
x,y,a,b,c=map(int,input().split())
p,q,r=[reversed(sorted(list(map(int,input().split())))) 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
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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
P_deque = deque(P)
Q_deque = deque(Q)
R_deque = deque(R)

red_counter = 0
green_counter = 0

ans = 0

for i in range(X+Y):
  if red_counter == X:
    P_deque = [-1]
  elif green_counter == Y:
    Q_deque = [-1]

  if len(R) != 0:
    if P_deque[0] >= Q_deque[0] and P_deque[0] >= R_deque[0]:
      ans += P_deque.popleft()
      red_counter += 1
    elif Q_deque[0] >= P_deque[0] and Q_deque[0] >= R_deque[0]:
      ans += Q_deque.popleft()
      green_counter +=1
    else:
      ans += R_deque.popleft()
  else:
    if P_deque[0] >= Q_deque[0]:
      ans += P_deque.popleft()
      red_counter += 1
    elif Q_deque[0] >= P_deque[0]:
      ans += Q_deque.popleft()
      green_counter +=1

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

#red
red=0
for _ in range(x):
	pMax=max(p)
	rMax=max(r)
	if pMax >rMax:
		red+=pMax
		p.pop(p.index(pMax))
	else:
		red+=rMax
		r.pop(r.index(rMax))

white=0
for _ in range(y):
	qMax=max(q)
	rMax=max(r)
	if qMax >rMax:
		red+=qMax
		q.pop(q.index(qMax))
	else:
		red+=rMax
		r.pop(r.index(rMax))

print(white+red)

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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pi = 0
qi = 0
ri = 0
pl = len(p)
ql = len(q)
rl = len(r)
scr = 0
for j in range(X+Y):
  mx = max(p[pi],q[qi],r[ri])
  scr += mx
  if mx == p[pi]:
    A -= 1
    if pi >= pl or A == 0:
      p.append(0)
      pi = -1
    else:
      pi += 1
  elif mx == q[qi]:
    B -= 1
    if qi >= ql or B == 0:
      q.append(0)
      qi = -1
    else:
      qi += 1
  else:
    if ri < rl:
      ri += 1
    else:
      r.append(0)
      ri = -1
      
print(scr)

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()))
new_p=sorted(p)
new_q=sorted(q)
white=sorted(r)
red=p[A-X:A]
green=q[B-Y:B]
ir=0
ig=0
while ir<=X and ig<=Y:
	for i in range(C):
		if red[ir]<green[ig] and red[ir]<white[i]:
			red[ir]=white[i]
			ir=ir+1
		elif green[ig]<red[ir] and green[ig]<white[i]:
			green[ig]=white[i]
			ig=ig+1
		else:
			ir=ir+1
            ig=ig+1
Sr=0
Sg=0
for ir in range(X):
	Sr=Sr+red[ir]
for ig in range(Y):
	Sg=Sg+green[ig]
print(Sr+Sg)

Submission status: Runtime Error

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

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_apples = sorted(p + q + r, key=lambda x: -x)

p_rest = sorted(p)
q_rest = sorted(q)
r_rest = sorted(r)

ans = 0
p_count = 0
q_count = 0
r_count = 0
while (p_count + q_count + r_count) < (X + Y):
    if p_count < X and q_count < Y:
        candidates = [p_rest[-1], q_rest[-1], r_rest[-1]]
    elif p_count < X and q_count == Y:
        candidates = [p_rest[-1], 0, r_rest[-1]]
    elif p_count == X and q_count < Y:
        candidates = [0, q_rest[-1], r_rest[-1]]

    wanna_eat = max(candidates)
    eat_idx = candidates.index(wanna_eat)

    if eat_idx == 0:
        p_count += 1
        ans += p_rest.pop()
    elif eat_idx == 1:
        q_count += 1
        ans += q_rest.pop()
    else:
        r_count += 1
        ans += r_rest.pop()
    # print(candidates, eat_idx, wanna_eat, p_count, q_count, r_count)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
x,y,a,b,c = map(int,input().split())
P = deque(sorted(list(map(int,input().split())),reverse=True)[:x])
Q = deque(sorted(list(map(int,input().split())),reverse=True)[:y])
R = deque(sorted(list(map(int,input().split())),reverse=True))
p = P.pop()
q = Q.pop()
r = R.popleft()
ans = 0
while (p < r or q < r) and r != 0:
    ans += r
    if len(R) != 0:   
        r = R.pop()
    else:
        r = 0
    if p < q:
        p = P.pop()
    else:
        q = Q.pop()
ans += sum(P)+sum(Q)+p+q
print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=list(map(int,input().split()))
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
eat=[]
eat.extend(p[:X])
eat.extend(q[:Y])
eat.sort(reverse=True)
i=0
result=0
for v in eat:
  if v<r[i]:
    result+=r[i]
    i+=1
  else:
    result+=v
print(result)

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 len(red) > 0:
        if red[-1] <= green[-1]:
            if red[-1] <= r[-i-1]:
                del red[-1]
                less.append(r[-i-1])
                continue
    if len(green) > 0:
        if red[-1] > green[-1]:
            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())
A = list(map(int, input().split())) 
B = list(map(int, input().split())) 
C = list(map(int, input().split())) 

A.sort()
B.sort()
C.sort(reverse=True)

X = A[len(A)-x:]
Y = B[len(B)-y:]


xi = 0
yi = 0
ci = 0
for ci in range(c):
    if C[ci] <= X[xi] and C[ci] <= Y[yi]:
        break
    
    if X[xi] < Y[yi]:
        if C[ci] > X[xi]:
            X[xi] = C[xi]
            xi = xi + 1
    else:
        if C[ci] > Y[yi]:
            Y[yi] = C[xi]
            yi = yi + 1

#print(X,Y)
print(sum(X)+sum(Y))

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)
Q = sorted(q,reverse=True)
R = sorted(r,reverse=True)
ans = 0
for i in range(X):
    ans += max(P[0],R[0])
    if P[0] >= R[0]:
        del P[0]
    else:
        del R[0]
  #  print(ans)    
for j in range(Y):
    ans += max(Q[0],R[0])
    if Q[0] >= R[0]:
        del Q[0]
    else:
        del R[0]
  #  print(ans)    
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()))

p = sorted(p)[a-x:]
q = sorted(q)[b-y:]
r = sorted(r, reverse=True)

pi = 0
qi = 0
ri = 0
while True:
    if ri > c-1: break
    if p[pi] < q[qi]:
        if p[pi] < r[ri]:
            p[pi] = r[ri]
            pi += 1
            ri += 1
        else:
            break
    else:
        if q[qi] < r[ri]:
            q[qi] = r[ri]
            qi += 1
            ri += 1
        else:
            break

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

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 < C:
            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:
from collections import deque
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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
P_deque = deque(P)
Q_deque = deque(Q)
R_deque = deque(R)

red_counter = 0
green_counter = 0

ans = 0

for i in range(X+Y):
  if red_counter == X:
    P_deque = [-1]
  elif green_counter == Y:
    Q_deque = [-1]

  if len(R) != 0:
    if P_deque[0] >= Q_deque[0] and P_deque[0] >= R_deque[0]:
      ans += P_deque.popleft()
      red_counter += 1
    elif Q_deque[0] >= P_deque[0] and Q_deque[0] >= R_deque[0]:
      ans += Q_deque.popleft()
      green_counter +=1
    else:
      ans += R_deque.popleft()

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heappop, heappush
x,y,a,b,c = map(int, input().split())

aa = sorted(map(int, input().split()), reverse=True)
bb = sorted(map(int, input().split()), reverse=True)
cc = sorted(map(int, input().split()), reverse=True)
heap = []
used = {}
for i in range(min(c,x)):
    heappush(heap, [-cc[i], 2, i])
for i in range(x):
    heappush(heap, [-aa[i], 0, i])

ans = 0
for i in range(x):
    value, color, pos = heappop(heap)
    ans -= value
    if color == 2:
        used.add(pos)

i = 0
heap = []
while len(heap) < y and i < c:
    if i not in used:
        heappush(heap, -cc[i])
    i += 1
for i in range(y):
    heappush(heap, -bb[i])

for i in range(y):
    value = heappop(heap)
    ans -= value

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()))
r.append(0)
p.sort()
q.sort()
r.sort()
ans = 0
for i in range(1,X+1):
    if(p[A-i] >= r[C]):
        ans += p[A-i]
    else:
        ans += r[C]
        del r[C]
for i in range(1,Y+1):
    if(q[B-i] >= r[C]):
        ans += q[A-i]
    else:
        ans += r[C]
        del r[C]
print(ans)

Submission status: Runtime Error

Solution code:
N = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]

def change(A, C):

    for i in range(len(A)):
        if A[i] < C[0]:
            A[i] = C[0]
            del(C[0])
    return A, C

def solve(N, A, B, C):
    X = N[0]
    Y = N[1]
    
    A = sorted(A, reverse=True)
    B = sorted(B, reverse=True)
    C = sorted(C, reverse=True)
    
    hoge, c = change(A[:X], C)
    hoge2, c = change(B[:Y], c)

    return sum(hoge) + sum(hoge2)

print(solve(N, A, B, C))