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: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
p=[0]+sorted(list(map(int,input().split())))[a-x:]
q=[0]+sorted(list(map(int,input().split())))[b-y:]
r=[0]+sorted(list(map(int,input().split())))
pi=-1
qi=-1
ri=-1
gs=0
rs=0
ws=0
s=0              
while gs+rs+ws!=x+y:
    print(r[ri],p[pi],q[qi])
    if r[ri]>=q[qi]>=p[pi]:
        s += r[ri]
        ws+=1
        ri-=1

    elif r[ri]>=p[pi]>=q[qi]:
        s += r[ri]
        ws += 1
        ri -= 1

    elif q[qi]>=r[ri]>=p[pi]:
        s += q[qi]
        gs += 1
        qi -= 1

    elif q[qi]>=p[pi]>=r[ri]:
        s += q[qi]
        gs += 1
        qi -= 1

    elif p[pi]>=r[ri]>=q[qi]:
        s += p[pi]
        rs += 1
        pi -= 1

    elif p[pi]>=q[qi]>=r[ri]:
        s += p[pi]
        rs += 1
        pi -= 1

print(s)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
p=[0]+sorted(list(map(int,input().split())))
q=[0]+sorted(list(map(int,input().split())))
r=[0]+sorted(list(map(int,input().split())))
pi=-1
qi=-1
ri=-1
gs=0
rs=0
ws=0
s=0              
while gs+rs+ws!=x+y:
    if r[ri]>=q[qi]>=p[pi]:
        s += r[ri]
        ws+=1
        ri-=1

    elif r[ri]>=p[pi]>=q[qi]:
        s += r[ri]
        ws += 1
        ri -= 1

    elif q[qi]>=r[ri]>=p[pi]:
        s += q[qi]
        gs += 1
        qi -= 1

    elif q[qi]>=p[pi]>=r[ri]:
        s += q[qi]
        gs += 1
        qi -= 1

    elif p[pi]>=r[ri]>=q[qi]:
        s += p[pi]
        rs += 1
        pi -= 1

    elif p[pi]>=q[qi]>=r[ri]:
        s += p[pi]
        rs += 1
        pi -= 1

print(s)

Submission status: Wrong Answer

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

p_array_sort = sorted(p_array, reverse=True)
q_array_sort = sorted(q_array, reverse=True)
r_array_sort = sorted(r_array, reverse=True)

p_i = x - 1
q_i = y - 1

for r in r_array_sort:
    p = p_array_sort[p_i]
    q = q_array_sort[q_i]
    if r <= p and r <= q:
        break
    elif q <= p:
        q_array_sort[q_i] = r
        q_i -= 1
    else:
        p_array_sort[p_i] = r
        p_i -= 1

ans = sum(p_array_sort[:x]) + sum(q_array_sort[:y])
print(ans)

Submission status: Wrong Answer

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


sin=p[a-x:]+q[b-y:]
sin.sort()
sin.reverse()

r.sort()
r.reverse()

ans=sum(sin)

for i in sin:
  if r[0]>i:
    ans+=r[0]-i
    r.pop(0)
    
    if len(r)==0:
      break
    

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


X,Y,A,B,C = MI()
P,Q,R = LI(),LI(),LI()

Z = [(P[i],1) for i in range(A)] + [(Q[i],0) for i in range(B)] + [(R[i],-1) for i in range(C)]
Z.sort(reverse=True)
a,b,c = 0,0,0
n = 0
ans = 0
for x in Z:
    if n == X+Y:
        break
    s,t = x
    if t == 1 and a+c == X:
        continue
    elif t == 0 and b+c == Y:
        continue
    else:
        n += 1
        ans += s
        if t == 1:
            a += 1
        elif t == 0:
            b += 1
        else:
            c += 1

print(ans)

Submission status: Wrong Answer

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)
ans = sum(p[:X]) + sum(q[:Y])
x = X-1
y = Y-1
m = min(p[x],q[y])
while len(r)>0 and r[0] > m:
    if p[x] > q[y]:
        ans += r[0] - q[y]
        y -= 1
    else:
        ans += r[0] - p[x]
        x -= 1
    r.pop(0)
    if y >= 0 and x >= 0:
        m = min(p[x],q[y])
    elif x >= 0 and y<0:
        m = p[x]
    elif y >= 0 and x<0:
        m = q[y]
    else:
        m = 10 ** 9 + 1
print(ans)

Submission status: Wrong Answer

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)
ans = sum(p[:X]) + sum(q[:Y])
x = X-1
y = Y-1
m = min(p[x],q[y])
while len(r)>0 and r[0] > m:
    if p[x] > q[y]:
        ans += r[0] - q[y]
        y -= 1
    else:
        ans += r[0] - p[x]
        x -= 1
    r.pop(0)
    if y >= 0 and x >= 0:
        m = min(p[x],q[y])
    elif x >= 0 and y<0:
        m = p[x]
    elif y >= 0 and x<0:
        m = q[y]
    else:
        m = float("inf")
print(ans)

Submission status: Wrong Answer

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)
ans = sum(p[:X]) + sum(q[:Y])
x = X-1
y = Y-1
m = min(p[x],q[y])
while len(r)>0 and r[0] > m:
    if p[x] > q[y]:
        ans += r[0] - q[y]
        y -= 1
        if y < 0:
            y = 0
    else:
        ans += r[0] - p[x]
        x -= 1
        if x < 0:
            x = 0
    r.pop(0)
    m = min(p[x],q[y])
print(ans)

Submission status: Wrong Answer

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)
ans = sum(p[:X]) + sum(q[:Y])
x = X-1
y = Y-1
m = min(p[x],q[y])
while len(r)>0 and r[0] > m:
    if p[x] > q[y]:
        ans += r[0] - q[y]
        y -= 1
    else:
        ans += r[0] - p[x]
        x -= 1
    r.pop(0)
    m = min(p[x],q[y])
print(ans)

Submission status: Wrong Answer

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

buy = r[(a-x):]+g[(b-y):]
buy.sort()
s = 0

for i in range(x+y):
  if un != [] and buy[i] < un[0]:
    p = un.pop(0)
    s += p
  else:
    s += buy[i]
    
print(s)

Submission status: Wrong Answer

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

buy = r[(a-x):]+g[(b-y):]
buy.sort()
s = sum(buy)

for i in range(x+y):
  if un != [] and buy[i] < un[0]:
    p = un.pop(0)
    s = s-buy[i]+p
    
print(s)

Submission status: Wrong Answer

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

from collections import deque

p = sorted(p[a-x:])
q = sorted(q[b-y:])
t = deque(sorted(p+q))
r = deque(reversed(sorted(r)))
# print(t,r)

while r:
    if t[0] < r[0]:
        t.popleft()
        w = r.popleft()
        t.append(w)
    else:
        break

ans = sum(t)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from itertools import chain
# from itertools import combinations as comb
# from bisect import bisect_left, bisect_right, insort_left, insort_right
# from collections import Counter
# import numpy as np


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = sorted(p)
    q = sorted(q)
    r = sorted(r, reverse=True)
    # p, q を A, B の長さまで切り詰める
    p = p[-X:]
    q = q[-Y:]
    # 足りない分は r から充足する
    rn = X - len(p) + Y - len(q)
    # ここまでの美味しさの和
    answer = sum(p) + sum(q) + sum(r[:rn])
    # p, q に番兵を足して処理を簡単にする
    p = p + [float('inf')]
    q = q + [float('inf')]
    i = 0
    j = 0
    for k in range(rn, len(r)):
        if p[i] < q[j]:
            if p[i] < r[k]:
                answer += (r[k] - p[i])
                i += 1
            else:
                break
        else:
            if q[j] < r[k]:
                answer += (r[k] - q[i])
                j += 1
            else:
                break
    return answer

def main():
    tokens = chain(*(line.split() for line in sys.stdin))
    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]"
    answer = solve(X, Y, A, B, C, p, q, r)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
plst = list(map(int, input().split()))
qlst = list(map(int, input().split()))
rlst = list(map(int, input().split()))
plst.sort()
qlst.sort()
rlst.sort(reverse = True)
plst = plst[-x:]
qlst = qlst[-y:]
r_pos = 0
p_pos = 0
q_pos = 0
while 1:
    if rlst[r_pos] <= plst[p_pos] and rlst[r_pos] <= qlst[q_pos]:
        break
    elif plst[p_pos] < qlst[q_pos]:
        plst[p_pos] = rlst[r_pos]
        p_pos += 1
        r_pos += 1
    else:
        qlst[q_pos] = rlst[r_pos]
        q_pos += 1
        r_pos += 1
    if p_pos == x or q_pos == y or r_pos == c:
        break
print(sum(plst) + sum(qlst))

Submission status: Wrong Answer

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

red_eaten = 0
green_eaten = 0
white_eaten = 0

ans = 0

while red_eaten < x and green_eaten < y and red_eaten+green_eaten+white_eaten<x+y:
    tmp = max(P[red_eaten], Q[green_eaten], R[white_eaten])
    if tmp == P[red_eaten]:
        ans += tmp
        red_eaten += 1
    elif tmp == Q[green_eaten]:
        ans += tmp
        green_eaten += 1
    else:
        ans = tmp
        white_eaten += 1

if red_eaten+green_eaten+white_eaten<x+y:
    if red_eaten < x:
        remain = P[red_eaten:] + R[white_eaten:]
    else:
        remain = Q[green_eaten:] + R[white_eaten:]
    remain.sort(reverse=True)
    ans += sum(remain[:(x+y-red_eaten-green_eaten-white_eaten)])

print(ans)

Submission status: Wrong Answer

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=[0]+p
q=[0]+q
r=[0]+r
nowp=x
nowq=y
nowr=0
while nowp>0 and nowr<=c-1:
    if p[nowp]<r[nowr+1]:
        nowp+=-1
        nowr+=1
    else:
        break
while nowq>0 and nowr<=c-1:
    if q[nowq]<r[nowr+1]:
        nowq+=-1
        nowr+=1
    else:
        break
ans=0
for i in range(1,nowp+1):
    ans+=p[i]
for j in range(1,nowq+1):
    ans+=q[j]
for u in range(1,nowr+1):
    ans+=r[u]
print(ans)

Submission status: Wrong Answer

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=[0]+p
q=[0]+q
r=[0]+r
nowp=x
nowq=y
nowr=0
while nowp>0 and nowr<=c-1:
    if p[nowp]<=r[nowr+1]:
        nowp+=-1
        nowr+=1
    else:
        break
while nowq>0 and nowr<=c-1:
    if q[nowq]<=r[nowr+1]:
        nowq+=-1
        nowr+=1
    else:
        break
ans=0
for i in range(1,nowp+1):
    ans+=p[i]
for j in range(1,nowq+1):
    ans+=q[j]
for u in range(1,nowr+1):
    ans+=r[u]
print(ans)

Submission status: Wrong Answer

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=[0]+p
q=[0]+q
r=[0]+r
nowp=x
nowq=y
nowr=0
while nowp>0 and nowr<=c-1:
    if p[nowp]<r[nowr+1]:
        nowp+=-1
        nowr+=1
    else:
        break
while nowq>0 and nowr<=c-1:
    if q[nowq]<=r[nowr+1]:
        nowq+=-1
        nowr+=1
    else:
        break
ans=0
for i in range(1,nowp+1):
    ans+=p[i]
for j in range(1,nowq+1):
    ans+=q[j]
for u in range(1,nowr+1):
    ans+=r[u]
print(ans)

Submission status: Wrong Answer

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)

eat_p = 0
eat_q = 0
eat_r = 0

ans = 0

while x and y:
    np = P[eat_p]
    nq = Q[eat_q]
    nr = R[eat_r]
    tmp = max(np, nq, nr)
    if tmp == np:
        eat_p += 1
        x -= 1
        ans += np
    elif tmp == nq:
        eat_q += 1
        y -= 1
        ans += nq
    else:
        ans += nr
        eat_r += 1
        if np <= nq:
            x -= 1
        else:
            y -= 1

while x and eat_r < c:
    np = P[eat_p]
    nr = R[eat_r]
    tmp = max(np, nr)
    if tmp == np:
        eat_p += 1
        x -= 1
        ans += np
    else:
        ans += nr
        eat_r += 1
        x -= 1

if x:
    ans += sum(P[eat_p:eat_p+x])

while y and eat_r < c:
    nq = Q[eat_q]
    nr = R[eat_r]
    tmp = max(nq, nr)
    if tmp == nq:
        eat_q += 1
        y -= 1
        ans += nq
    else:
        ans += nr
        eat_r += 1
        y -= 1

if y:
    ans += sum(Q[eat_q:eat_q+y])

print(ans)

Submission status: Wrong Answer

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)
np=[0]
nq=[0]
nr=[0]
for i in range(len(p)):
    np.append(np[-1]+p[i])
for j in range(len(q)):
    nq.append(nq[-1]+q[j])
for u in range(len(r)):
    nr.append(nr[-1]+r[u]) 
nowp=x-1
nowq=y-1
nowr=0
while nowr<=c-1 and nowp>=0 and nowq>=0 and (r[nowr]>p[nowp] or r[nowr]>q[nowq]) :
    if nowr<=c-1 and r[nowr]>p[nowp] and nowp>0:
        nowr+=1
        nowp+=-1
    elif nowr<=c-1 and r[nowr]>q[nowq] and nowq>0:
        nowr+=1
        nowq+=-1
print(np[nowp+1]+nq[nowq+1]+nr[nowr])

Submission status: Wrong Answer

Solution code:
def resolve():
    X, Y, A, B, C = list(map(int, input().split()))
    import heapq
    P = list(map(lambda x: -int(x), input().split()))
    Q = list(map(lambda x: -int(x), input().split()))
    R = list(map(lambda x: -int(x), input().split()))
    heapq.heapify(P)
    heapq.heapify(Q)
    heapq.heapify(R)
    x = 0
    y = 0
    ans = 0
    while not (X == x and Y == y):
        red = -heapq.heappop(P) if x < X and len(P) != 0 else 0
        green = -heapq.heappop(Q) if y < Y and len(Q) != 0 else 0
        white = -heapq.heappop(R) if len(R) != 0 else 0
        if (red > green and red >= white) or (red == green and X - x > Y - y):
            heapq.heappush(Q, -green)
            heapq.heappush(R, -white)
            x += 1
            ans += red
        elif green >= red and green >= white:
            heapq.heappush(P, -red)
            heapq.heappush(R, -white)
            y += 1
            ans += green
        else:
            heapq.heappush(P, -red)
            heapq.heappush(Q, -green)
            if X == x:
                y += 1
            elif Y == y:
                x += 1
            else:
                if red > green:
                    y += 1
                elif red == green:
                    if Y - y < X - x:
                        x += 1
                    else:
                        y += 1
                else:
                    x += 1
            ans += white
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    X, Y, A, B, C = list(map(int, input().split()))
    import heapq
    P = list(map(lambda x: -int(x), input().split()))
    Q = list(map(lambda x: -int(x), input().split()))
    R = list(map(lambda x: -int(x), input().split()))
    heapq.heapify(P)
    heapq.heapify(Q)
    heapq.heapify(R)
    x = 0
    y = 0
    ans = 0
    while not (X == x and Y == y):
        red = -heapq.heappop(P) if x < X and len(P) != 0 else 0
        green = -heapq.heappop(Q) if y < Y and len(Q) != 0 else 0
        white = -heapq.heappop(R) if len(R) != 0 else 0
        if (red > green and red >= white) or (red == green and X - x > Y - y):
            heapq.heappush(Q, -green)
            heapq.heappush(R, -white)
            x += 1
            ans += red
        elif green >= red and green >= white:
            heapq.heappush(P, -red)
            heapq.heappush(R, -white)
            y += 1
            ans += green
        else:
            heapq.heappush(P, -red)
            heapq.heappush(Q, -green)
            if X - x < Y - y:
                y += 1
            else:
                x += 1
            ans += white
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    X, Y, A, B, C = list(map(int, input().split()))
    import heapq
    P = list(map(lambda x: -int(x), input().split()))
    Q = list(map(lambda x: -int(x), input().split()))
    R = list(map(lambda x: -int(x), input().split()))
    heapq.heapify(P)
    heapq.heapify(Q)
    heapq.heapify(R)
    x = 0
    y = 0
    ans = 0
    while not (X == x and Y == y):
        red = -heapq.heappop(P) if x < X and len(P) != 0 else 0
        green = -heapq.heappop(Q) if y < Y and len(Q) != 0 else 0
        white = -heapq.heappop(R) if len(R) != 0 else 0
        maxv = max(red, green, white)
        if red == maxv:
            heapq.heappush(Q, -green)
            heapq.heappush(R, -white)
            x += 1
        if green == maxv:
            heapq.heappush(P, -red)
            heapq.heappush(R, -white)
            y += 1
        if white == maxv:
            heapq.heappush(P, -red)
            heapq.heappush(Q, -green)
            if X - x < Y - y:
                y += 1
            else:
                x += 1
        ans += maxv
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque

X,Y,A,B,C = map(int, input().split())
A=[int(a) for a in input().split()]
B=[int(b) for b in input().split()]
C=[int(c) for c in input().split()]
A=sorted(A, reverse=True)[:X]
B=sorted(B, reverse=True)[:Y]
AB=deque(sorted(A+B))

for c in sorted(C, reverse=True):
  if c > AB[0]:
    AB.popleft()
    AB.append(c)
    break

print(sum(AB))

Submission status: Wrong Answer

Solution code:
from collections import deque


X,Y,A,B,C = map(int, input().split())
A=[int(a) for a in input().split()]
B=[int(b) for b in input().split()]
C=[int(c) for c in input().split()]
A=sorted(A, reverse=True)[:X]
B=sorted(B, reverse=True)[:Y]
AB=deque(sorted(A+B))

for c in C:
  if c > AB[0]:
    AB.popleft()
    AB.append(c)
    break

print(sum(AB))

Submission status: Wrong Answer

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

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


X, Y, A, B, C = map(int, input().split())
apples_A = sorted(list(map(int, input().split())))
apples_B = sorted(list(map(int, input().split())))
apples_C = sorted(list(map(int, input().split())))

# 赤のリンゴからおいしさが大きい順に X 個選ぶ
# 緑のリンゴからおいしさが大きい順に Y 個選ぶ
# 無色のリンゴからおいしさが最も大きいリングを選び、そのリンゴ以上の美味しさでかつ美味しさが最も小さい赤 or 緑リンゴと交換する
# 上記の交換をできるだけ多く行う

res_A = apples_A[A - X:]
res_B = apples_B[B - Y:]

while apples_C:
    candidate = apples_C.pop()
    min_apple = min(res_A[0], res_B[0])
    if candidate <= min_apple:
        break

    if res_A[0] <= res_B[0]:
        # there is apple which has minimum value in A
        res_A[0] = candidate
    else:
        res_B[0] = candidate

ans = sum(res_A) + sum(res_B)
print(ans)

Submission status: Wrong Answer

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

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


X, Y, A, B, C = map(int, input().split())
apples_A = sorted(list(map(int, input().split())))
apples_B = sorted(list(map(int, input().split())))
apples_C = sorted(list(map(int, input().split())))

# 赤のリンゴからおいしさが大きい順に X 個選ぶ
# 緑のリンゴからおいしさが大きい順に Y 個選ぶ
# 無色のリンゴからおいしさが最も大きいリングを選び、そのリンゴ以上の美味しさでかつ美味しさが最も小さい赤 or 緑リンゴと交換する
# 上記の交換をできるだけ多く行う

res_A = apples_A[A - X:]
res_B = apples_B[B - Y:]

while apples_C:
    candidate = apples_C.pop()
    min_apple = min(res_A[0], res_B[0])
    if candidate < min_apple:
        break

    if res_A[0] <= res_B[0]:
        # there is apple which has minimum value in A
        res_A[0] = candidate
    else:
        res_B[0] = candidate

ans = sum(res_A) + sum(res_B)
print(ans)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
p = sorted(map(int, input().split()))
q = sorted(map(int, input().split()))
r = sorted(map(int, input().split()))
ans = 0
for i in range(a):
    if len(p) > 0 and len(r) > 0:
        if p[-1] >= r[-1]:
            ans += p[-1]
            p.pop()
        else:
            ans += r[-1]
            r.pop()
    else:
        if len(p) == 0:
            ans += r[-1]
        else:
            ans += p[-1]
            
for i in range(b):
    if len(q) > 0 and len(r) > 0:
        if q[-1] >= r[-1]:
            ans += q[-1]
            q.pop()
        else:
            ans += r[-1]
            r.pop()
    else:
        if len(q) == 0:
            ans += r[-1]
        else:
            ans += q[-1]

print(ans)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
p = sorted(map(int, input().split()))
q = sorted(map(int, input().split()))
r = sorted(map(int, input().split()))
ans = 0
for i in range(x):
    if len(p) > 0 and len(r) > 0:
        if p[-1] >= r[-1]:
            ans += p[-1]
            p.pop()
        else:
            ans += r[-1]
            r.pop()
    else:
        if len(p) == 0:
            ans += r[-1]
        else:
            ans += p[-1]
            
for i in range(y):
    if len(q) > 0 and len(r) > 0:
        if q[-1] >= r[-1]:
            ans += q[-1]
            q.pop()
        else:
            ans += r[-1]
            r.pop()
    else:
        if len(q) == 0:
            ans += r[-1]
        else:
            ans += q[-1]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())),reverse=True)
q=sorted(list(map(int,input().split())),reverse=True)
r=deque(sorted(list(map(int,input().split())),reverse=True))
p=deque(p[:x])
q=deque(q[:y])
ans=sum(p)+sum(q)

while p[-1]<r[0] or q[-1]<r[0]:
  if p[-1]<q[-1]:
    ans+=r[0]-p[-1]
    r.popleft()
    p.pop()
  else:
    ans+=r[0]-q[-1]
    r.popleft()
    q.pop()
  if p==deque([]) or q==deque([]) or r==deque([]):
    break
print(ans)

Submission status: Wrong Answer

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

while p[-1]<r[0] or q[-1]<r[0]:
  if p[-1]<q[-1]:
    ans+=r[0]-p[-1]
    r.pop(0)
    p.pop(-1)
  else:
    ans+=r[0]-q[-1]
    r.pop(0)
    q.pop(-1)
  if p==[] or q==[] or r==[]:
    break
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import*
x,y,a,b,c=map(int,input().split())
p=[0]+sorted(list(map(int,input().split())))[::-1]
q=[0]+sorted(list(map(int,input().split())))[::-1]
r=[0]+sorted(list(map(int,input().split())))[::-1]
ps=list(accumulate(p))
qs=list(accumulate(q))
ans=0
for v in r:
  if x and ps[x-1]+v>ps[x] and p[x]<q[y]:
    ans+=v
    x-=1
  elif y and qs[y-1]+v>qs[y]:
    ans+=v
    y-=1
print(ans+ps[x]+qs[y])

Submission status: Wrong Answer

Solution code:
from itertools import*
x,y,a,b,c=map(int,input().split())
p=[0]+sorted(list(map(int,input().split())))[::-1]
q=[0]+sorted(list(map(int,input().split())))[::-1]
r=[0]+sorted(list(map(int,input().split())))[::-1]
ps=list(accumulate(p))
qs=list(accumulate(q))
ans=0
for v in r:
  if ps[x-1]+v>ps[x] and p[x]<q[y]:
    ans+=v
    x-=1
  elif qs[y-1]+v>qs[y]:
    ans+=v
    y-=1
print(ans+ps[x]+qs[y])

Submission status: Wrong Answer

Solution code:
from itertools import*
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())))[::-1]
q=sorted(list(map(int,input().split())))[::-1]
r=sorted(list(map(int,input().split())))[::-1]
sa=list(accumulate(p))[x-1]
sb=list(accumulate(q))[y-1]
for i,v in enumerate(r):
  if x and v>p[x-1]:
    if p[x-1]<q[y-1] or y==0:
      sa+=v-p[x-1]
      x-=1
  elif y and v>q[y-1]:
    sb+=v-q[y-1]
    y-=1
print(sa+sb)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

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)

p1 = deque(p)
q1 = deque(q)
r1 = deque(r)
p2 = copy.deepcopy(p1)
q2 = copy.deepcopy(q1)
r2 = copy.deepcopy(r1)


ans = 0

ans1 = 0
for _ in range(X):
    if len(r1) > 0:
        if p1[0] > r1[0]:
            ans1 += p1.popleft()
        else:
            ans1 += r1.popleft()
    else:
        ans1 += p1.popleft()

for _ in range(Y):
    if len(r1) > 0:
        if q1[0] > r1[0]:
            ans1 += q1.popleft()
        else:
            ans1 += r1.popleft()
    else:
        ans1 += q1.popleft()

ans2 = 0
for _ in range(Y):
    if len(r2) > 0:
        if q2[0] > r2[0]:
            ans2 += q2.popleft()
        else:
            ans2 += r2.popleft()
    else:
        ans2 += q2.popleft()

for _ in range(X):
    if len(r2) > 0:
        if p2[0] > r2[0]:
            ans2 += p2.popleft()
        else:
            ans2 += r2.popleft()
    else:
        ans2 += p2.popleft()

ans = max(ans1, ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

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)

p1 = deque(p)
q1 = deque(q)
r1 = deque(r)
p2 = copy.deepcopy(p1)
q2 = copy.deepcopy(q1)
r2 = copy.deepcopy(r1)


ans = 0

ans1 = 0
for _ in range(X):
    if len(r1) > 0:
        if p1[0] >= r1[0]:
            ans1 += p1.popleft()
        else:
            ans1 += r1.popleft()
    else:
        ans1 += p1.popleft()

for _ in range(Y):
    if len(r1) > 0:
        if q1[0] >= r1[0]:
            ans1 += q1.popleft()
        else:
            ans1 += r1.popleft()
    else:
        ans1 += q1.popleft()

ans2 = 0
for _ in range(Y):
    if len(r2) > 0:
        if q2[0] >= r2[0]:
            ans2 += q2.popleft()
        else:
            ans2 += r2.popleft()
    else:
        ans2 += q2.popleft()

for _ in range(X):
    if len(r2) > 0:
        if p2[0] >= r2[0]:
            ans2 += p2.popleft()
        else:
            ans2 += r2.popleft()
    else:
        ans2 += p2.popleft()

ans = max(ans1, ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

p = deque(p)
q = deque(q)
r = deque(r)

ans = 0
for _ in range(X):
    if len(r) > 0:
        if p[0] >= r[0]:
            ans += p.popleft()
        else:
            ans += r.popleft()
    else:
        ans += p.popleft()
for _ in range(Y):
    if len(r) > 0:
        if q[0] >= r[0]:
            ans += q.popleft()
        else:
            ans += r.popleft()
    else:
        ans += q.popleft()

print(ans)

Submission status: Wrong Answer

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()))
s = sorted(p)[::-1][:X] + sorted(q)[::-1][:X] + r
ans = sum(s[:X + Y])
print(ans)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
rs = [int(i) for i in input().split()]
gs = [int(i) for i in input().split()]
ws = [int(i) for i in input().split()]

rs.sort(reverse=True)
gs.sort(reverse=True)
ws.sort(reverse=True)
weight = 0
for i in range(x):
  weight += rs[i]
r = x - 1
for i in range(y):
  weight += gs[i]
g = y - 1
w = -1
#print(r,g,w)
#print(weight)
rs += [0]
gs += [0]
#print(rs, gs)
while True:
  if rs[r] < gs[g]:
    if w < c - 1 and rs[r] != 0:
      if rs[r] < ws[w + 1]:
        weight = weight + ws[w + 1] - rs[r]
        w += 1
        r -= 1
        #print(r,g,w)
        #print(weight)
        continue
  else:
    if w < c - 1 and gs[g] != 0:
      if gs[g] < ws[w + 1]:
        weight = weight + ws[w + 1] - gs[g]
        w += 1
        g -= 1
        #print(r,g,w)
        #print(weight)
        continue
  break
print(weight)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
rs = [int(i) for i in input().split()]
gs = [int(i) for i in input().split()]
ws = [int(i) for i in input().split()]

rs.sort(reverse=True)
gs.sort(reverse=True)
ws.sort(reverse=True)
weight = 0
for i in range(x):
  weight += rs[i]
r = x - 1
for i in range(y):
  weight += gs[i]
g = y - 1
w = -1
#print(r,g,w)
#print(weight)
while r > -1 and g > -1 and w < c - 1:
  weight_1 = weight + ws[w + 1] - rs[r]
  weight_2 = weight + ws[w + 1] - gs[g]
  if weight_1 > weight_2:
    if weight_1 > weight:
      weight = weight_1
      r -= 1
      w += 1
      continue
  else:
    if weight_2 > weight:
      weight = weight_2
      g -= 1
      w += 1
      continue
  break
print(weight)