Problem:


Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

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

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

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

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

Eat the 2-nd red apple.

Eat the 1-st green apple.

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

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

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

Sample Output 3

74



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


Sample output:
12

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import heapq as hp

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

q1 = []
S1 = 0
for p in P:
    hp.heappush(q1, p)
    S1 += p

while len(q1) > X:
    p = hp.heappop(q1)
    S1 -= p

q2 = []
S2 = 0
for q in Q:
    hp.heappush(q2, q)
    S2 += q

while len(q2) > Y:
    p = hp.heappop(q2)
    S2 -= p


tmp = S1 + S2
ans = S1 + S2
R.sort(reverse=True)
for r in R:
    p1 = hp.heappop(q1)
    p2 = hp.heappop(q2)
    if p1 <= p2:
        if r >= p1:
            tmp += r - p1
            ans = max(ans, tmp)
            hp.heappush(q1, r)
            hp.heappush(q2, p2)
        else:
            break
    else:
        if r >= p2:
            tmp += r - p2
            ans = max(ans, tmp)
            hp.heappush(q1, p1)
            hp.heappush(q2, r)
        else:
            break

print(ans)

Submission status: Accepted

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


RED = list(map(int,input().split()))
GREEN = list(map(int,input().split()))
NON = list(map(int,input().split()))

ALL = []
for i in range(A):
    ALL.append([RED[i],'red'])
for i in range(B):
    ALL.append([GREEN[i],'green'])    
for i in range(C):
    ALL.append([NON[i],'non'])  
    
ALL.sort()
ALL = ALL[::-1]

red = 0
green = 0
non = 0
now = 0
ans = 0

while 1:
    tmpS,tmpcolor = ALL[now]
    
    if tmpcolor == 'red':
        if red<X and red+green+non<X+Y:
            ans += tmpS
            red += 1
    elif tmpcolor == 'green':
        if green<Y and red+green+non<X+Y:
            ans += tmpS
            green += 1
    elif tmpcolor == 'non':
        if red+green+non<X+Y:
            ans += tmpS
            non += 1
    if red+green+non==X+Y:
        break
    now += 1
    
print(ans)

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


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

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

ps = sum(P[:X])
qs = sum(Q[:Y])
r_cum = cumsum(R)

x = X - 1
y = Y - 1

ans = ps + qs
# 何個着色するか
for cnt in range(1, min(X + Y, C) + 1):
    if x >= 0 and y >= 0 and P[x] < Q[y] or y < 0:
        ps -= P[x]
        x -= 1
    else:
        qs -= Q[y]
        y -= 1
    s = r_cum[cnt - 1] + ps + qs
    ans = max(ans, s)
print(ans)

Submission status: Accepted

Solution code:
x, y, a, b, c = [int(i) for i in input().split()]
p = [(int(i),0) for i in input().split()]
q = [(int(i),1) for i in input().split()]
r = [(int(i), 2) for i in input().split()]
pqr = p + q + r
pqr.sort(reverse=1)
i = 0
j = 0
ind = 0
ans = 0
for _ in range(x + y):
    if i < x and j < y:
        aa = pqr[ind]
        ans += aa[0]
        if aa[1] == 0:
            i += 1
        elif aa[1] == 1:
            j += 1
        ind += 1
    elif i < x:
        while pqr[ind][1] == 1:
            ind += 1
        aa = pqr[ind]
        ans += aa[0]
        if aa[1] == 0:
            i += 1
        elif aa[1] == 1:
            j += 1
        ind += 1
    else:
        while pqr[ind][1] == 0:
            ind += 1
        aa = pqr[ind]
        ans += aa[0]
        if aa[1] == 0:
            i += 1
        elif aa[1] == 1:
            j += 1
        ind += 1
print(ans)

Submission status: Accepted

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

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

ans = sum(p[:x]) + sum(q[:y])
x -= 1
y -= 1
#print(ans)
for z in r:
    if y >= 0 and x >= 0:
        if p[x] >= q[y]:
            if z > q[y]:
                #print(z, q[y], y)
                ans += z - q[y]
                y -= 1
            else:
                break
        else:
            if z > p[x]:
                ans += z - p[x]
                x -= 1
            else:
                break
    elif x >= 0:
        if z > p[x]:
            ans += z - p[x]
            x -= 1
        else:
            break
    elif y >= 0:
        if z > q[y]:
            ans += z - q[y]
            y -= 1
        else:
            break
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
def examA():
    S = SI()
    if S[2]==S[3] and S[4]==S[5]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    X = I()
    ans = 0
    ans += (X//500) * 1000
    X %= 500
    ans += (X//5) * 5
    print(ans)
    return

def examC():
    K, N = LI()
    A = LI()
    L = [0]*N
    L[0] = K-A[-1] + A[0]
    for i in range(1,N):
        L[i] = A[i]-A[i-1]
    ans = sum(L) - max(L)
    #print(L)
    print(ans)
    return

def examD():
    def bfs(n, e, fordfs):
        # 点の数、スタートの点、有向グラフ
        W = [-1] * n
        # 各点の状態量、最短距離とか,見たかどうかとか
        W[e] = 0
        que = deque()
        que.append(e)
        while que:
            now = que.popleft()
            nowW = W[now]
            for ne in fordfs[now]:
                if W[ne] == -1:
                    W[ne] = nowW + 1
                    que.append(ne)
        return W
    N, X, Y = LI()
    V = [[]for _ in range(N)]
    for i in range(N-1):
        V[i].append(i+1)
        V[i+1].append(i)
    V[X-1].append(Y-1)
    V[Y-1].append(X-1)
    #print(V)
    ans = [0]*(N-1)
    for i in range(N):
        L = bfs(N,i,V)
        for l in L:
            if l==0:
                continue
            ans[l-1] += 1
    for v in ans:
        print(v//2)
    return

def examE():
    X, Y, A, B, C = LI()
    P = LI()
    Q = LI()
    R = LI()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    cnt = sum(P[:X]) + sum(Q[:Y])
    #print(cnt)
    X -= 1; Y -= 1
    for c in range(C):
        if X>=0 and Y>=0:
            if P[X] <= Q[Y]:
                if P[X] >= R[c]:
                    break
                cnt += (R[c] - P[X])
                X -= 1
            else:
                if Q[Y] >= R[c]:
                    break
                cnt += (R[c] - Q[Y])
                Y -= 1
        elif X>=0:
            if P[X] >= R[c]:
                break
            cnt += (R[c] - P[X])
            X -= 1
        elif Y>=0:
            if Q[Y] >= R[c]:
                break
            cnt += (R[c] - Q[Y])
            Y -= 1
    ans = cnt
    print(ans)
    return

def examF():
    N = I()
    V = [[]for _ in range(N)]

    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examE()

"""

"""

Submission status: Accepted

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

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

ans = sum(p[:x] + q[:y])
i = x-1
j = y-1

for k in r:
    if i >= 0 and j >= 0:
        if p[i] <= q[j] and p[i] < k:
            ans += k - p[i]
            i -= 1
            continue
        elif p[i] > q[j] and q[j] < k:
            ans += k - q[j]
            j -= 1
            continue
    elif i == -1 and j >= 0:
        if q[j] < k:
            ans += k - q[j]
            j -= 1
            continue
    elif i >= 0 and j == -1:
        if p[i] < k:
            ans += k - p[i]
            i -= 1
            continue
    break
print(ans)

Submission status: Accepted

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

X,Y,A,B,C=map(int,input().split())
P=sorted(map(int,input().split()),reverse=True)
Q=sorted(map(int,input().split()),reverse=True)
R=list(map(int,input().split()))

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

print(sum(C[:X+Y]))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

x, y, a, b, c = MAP()
p = LIST()
q = LIST()
r = LIST()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pq = p[:x] + q[:y]
pq.sort()
pqsum = 0
rsum = 0
d = 0
for i in range(min(c, x + y)):
    pqsum += pq[i]
    rsum += r[i]
    if rsum - pqsum > d:
        d = rsum - pqsum
print(sum(pq) + d)

Submission status: Accepted

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
que = []
for i in range(a):
    que.append(p[i]*3)
for i in range(b):
    que.append(q[i]*3+1)
for i in range(c):
    que.append(r[i]*3+2)
que.sort(reverse = True)
cnt = [0]*2
sm = 0
up = [x, y]
ans = 0
for i in range(a+b+c):
    v = que[i]
    if v%3 == 2:
        sm+=1
    elif cnt[v%3] == up[v%3]:
        continue
    else:
        cnt[v%3]+=1
        sm+=1
    ans+=v//3
    if sm == x+y:
        break
print(ans)

Submission status: Accepted

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

Ps.sort()
Qs.sort()
Ss = Ps[-X:] + Qs[-Y:]
Ss.sort()

Rs.sort()

for i, s in enumerate(Ss):
    r = Rs.pop()
    if s < r:
        Ss[i] = r
    else:
        break
    if len(Rs) == 0:
        break

print(sum(Ss))

Submission status: Accepted

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

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

a = sorted(P[:X] + Q[:Y])
ans = sum(a)

for i in range(min(X+Y, C)):
    if a[i] < R[i]:
        ans += R[i] - a[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

tmp = p[-x:] + q[-y:]
tmp.sort()

from collections import deque
tmp = deque(tmp)

while r and tmp[0] < r[-1]:
    tmp.popleft()
    tmp.append(r.pop())

print(sum(list(tmp)))

Submission status: Accepted

Solution code:
from heapq import heappush,heappop
x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
b = list(map(int,input().split()))
r.sort()
g.sort()
b.sort()
hq = []
for i in range(x):
  heappush(hq,r[-i-1])
for i in range(y):
  heappush(hq,g[-i-1])
while b:
  a = heappop(hq)
  if a < b[-1]:
    c = b.pop()
    heappush(hq,c)
  else:
    heappush(hq,a)
    break
print(sum(hq))

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
P = [int(a) for a in input().split()]
Q = [int(a) for a in input().split()]
R = [int(a) for a in input().split()]
S = sorted([p * 4 for p in P] + [q * 4 + 1 for q in Q] + [r * 4 + 2 for r in R])[::-1]

Z = X + Y
ans = 0
for s in S:
    a, t = s>>2, s & 3
    if t == 0:
        if X:
            ans += a
            X -= 1
            Z -= 1
    elif t == 1:
        if Y:
            ans += a
            Y -= 1
            Z -= 1
    else:
        ans += a
        Z -= 1
    if not Z: break
print(ans)

Submission status: Accepted

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


R, B = p[:x], q[:y]
heapify(R)
heapify(B)
for i in range(c):
	if min(R[0], B[0]) > r[i]:
		break
	if R[0] < B[0]:
		heappop(R)
		heappush(R, r[i])
	else:
		heappop(B)
		heappush(B, r[i])

print(sum(R) + sum(B))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    apple = []
    for p in P:
        apple.append((p, 1))
    for q in Q:
        apple.append((q, 2))
    for r in R:
        apple.append((r, 3))
    apple.sort(key=lambda x: x[0])

    XY = X+Y
    ans = 0
    while True:
        s, flg = apple.pop()
        if flg == 1 and X == 0:
            continue
        if flg == 2 and Y == 0:
            continue
        ans += s
        if flg == 1:
            X -= 1
        elif flg == 2:
            Y -= 1
        XY -= 1
        if XY == 0:
            break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

X, Y, A, B, C = na()
P = na()
Q = na()
R = na()
ls = []
for lp in P:
    ls.append([lp, 0])
for lp in Q:
    ls.append([lp, 2])
for lp in R:
    ls.append([lp, 1])
ls.sort(reverse=True)

up = 0
down = 0
ans = 0
for item in ls:
    if item[1] == 0:
        if X > 0:
            X -= 1
            ans += item[0]
        elif X == 0 and Y > 0 and up > 0:
            up -= 1
            down += 1
            Y -= 1
            ans += item[0]
    elif item[1] == 2:
        if Y > 0:
            Y -= 1
            ans += item[0]
        elif Y == 0 and X > 0 and down > 0:
            down -= 1
            up += 1
            X -= 1
            ans += item[0]
    else:
        if X > 0:
            X -= 1
            ans += item[0]
            up += 1
        elif Y > 0:
            Y -= 1
            ans += item[0]
            down += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


x, y, a, b, c = LI()
P = LI()
Q = LI()
R = LI()
L = []
for p in P:
    L += [(p, 0)]

for q in Q:
    L += [(q, 1)]

for r in R:
    L += [(r, 2)]

L.sort(reverse=True)
ret = 0
z = 0
for l, i in L:
    if i == 0 and x:
        x -= 1
        ret += l
    elif i == 1 and y:
        y -= 1
        ret += l
    elif i == 2:
        z += 1
        ret += l
    if x + y == z:
        break


print(ret)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

x,y,a,b,c = nm()
p = nl()
q = nl()
r = nl()
p.sort()
q.sort()
l = p[-x:] + q[-y:]
l.sort()
r.sort()
for i in range(min(x+y, c)):
    if l[i] < r[-1]:
        l[i] = r.pop()
    else:
        break
print(sum(l))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7


def inp(): return int(sys.stdin.readline())


def inpl(): return list(map(int, sys.stdin.readline().split()))


def inpl_str(): return list(sys.stdin.readline().split())


X, Y, A, B, C = inpl()
pp = inpl()
qq = inpl()
rr = inpl()

apples = []
apples += [(p, 1) for p in pp]
apples += [(q, 2) for q in qq]
apples += [(r, 3) for r in rr]

apples.sort(reverse=True)

ans = 0
S = X+Y
for a, i in apples:
    if i == 1 and X > 0:
        ans += a
        X -= 1
        S -= 1
    elif i == 2 and Y > 0:
        ans += a
        Y -= 1
        S -= 1
    elif i == 3:
        ans += a
        S -= 1

    if S == 0:
        break

print(ans)

Submission status: Runtime Error

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

x,y,a,b,c=sep()
red=lis()
green=lis()
colorless=lis()
red.sort(reverse=True)
green.sort(reverse=True)
colorless.sort()
red=red[:x][::-1]
green=green[:y][::-1]
i=0
j=0
while(colorless):
    if red[i]<=green[j]:
        if colorless[-1]>red[i]:
            red[i]=colorless[-1]
            colorless.pop()
            i+=1
        else:
            break
    else:
        if colorless[-1]>green[j]:
            green[j]=colorless[-1]
            colorless.pop()
            j+=1
        else:
            break
#print(red,green)
print(sum(red) + sum(green))

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

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

r.sort(reverse=True)

ans=sum(sin)

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

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

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

r.sort(reverse=True)

ans=sum(sin)

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

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

cntx=0
cnty=0

ans=0


for i in range(a):
    if p[i]>=r[-1]:
        ans+=p[i]
    else:
        ans+=r.pop()

    cntx+=1    

    if cntx==x:
        break

for j in range(b):
    if q[j]>=r[-1]:
        ans+=q[j]
    else:
        ans+=r.pop()

    cnty+=1

    if cnty==y:
        break

print(ans)

Submission status: Runtime Error

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()))
v = 0

for _ in range(x):
    if len(r):
        if p[-1] > r[-1]:
            v += p.pop()
        else:
            v += r.pop()
    else:
        v += p.pop()

for _ in range(y):
    if len(r):
        if q[-1] > r[-1]:
            v += q.pop()
        else:
            v += r.pop()
    else:
        v += r.pop()

print(v)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define UM unordered_map
#define MP map
#define ST set
#define US unordered_set
#define V vector
#define S string
#define D deque
#define L list
#define Q queue
#define PQ priority_queue
#define D deque
#define PII pair<int, int>
#define P pair
#define DB double
#define LDB long double
#define FOR(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define FFOR(i, n) for (int(i) = 0; (i) <= (n); ++(i))
#define RFOR(i, n) for (int(i) = (n)-1; (i) >= 0; --(i))
#define all(grid) (grid).begin(), (grid).end()
#define rall(grid) (grid).rbegin(), (grid).rend()

const int dr[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};

template <typename T>
void read(T& a) {
  a = 0;
  char c;
  while ((c = cin.peek()) < 48 or c > 57) cin.get();
  while ((c = cin.get()) > 47 and c < 58) a = (a << 3) + (a << 1) + c - 48;
}

template <typename T, typename... Args>
void read(T& a, Args&&... args) {
  read(a);
  read(forward<Args>(args...)...);
}

template <typename T>
void write(const S& sep, const S& end, T n) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10) c[i] = n % 10;

  for (--i; i >= 0; --i) cout.put(c[i] + 48);

  cout.write(end.c_str(), end.size() * sizeof(char));
}

template <typename T, typename... Args>
void write(const S& sep, const S& end, T n, Args&&... args) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10) c[i] = n % 10;

  for (--i; i >= 0; --i) cout.put(c[i] + 48);

  cout.write(sep.c_str(), sep.size() * sizeof(char));
  write(sep, end, forward<Args>(args...)...);
}
void solve() {
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  V<unsigned LL> red(a), green(b), colourless(c);
  FOR(i, a) {
      cin >> red[i];
  }
  FOR(i, b) {
      cin >> green[i];
  }
  FOR(i, c) {
      cin >> colourless[i];
  }
  sort(rall(red));
  sort(rall(green));
  FOR(i, x) {
      colourless.push_back(red[i]);
  }
  FOR(i, x) {
      colourless.push_back(green[i]);
  }
  sort(rall(colourless));
  cout << accumulate(begin(colourless), begin(colourless) + x + y, 0ULL) << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}

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

x=X-1
y=Y-1

p=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)

#print(p[:X],q[:Y])

if p[x]>=q[y]:
  a=q[y]
  f=1
else:
  a=p[x]
  f=2
while a<r[0] and len(r)>=1:
#  print(a,r[0])
  if f==1 :
#    print('f=1')
    q[y]=r[0]
    y-=1
    r.pop(0)
  elif f==2 :
#    print('f=2')
    p[x]=r[0]
    x-=1
    r.pop(0)
  else:
#    print('break1')
    break
    
#  print(x,y,p[x],q[y])
    
  if (y==-1 and x>=0) or (x>=0 and p[x]<q[y]):
#    print('a=p[x]')
    a=p[x]
    f=2
  elif (x==-1 and y>=0) or (y>=0 and p[x]>=q[y]):
#    print('a=q[y]')
    a=q[y]
    f=1
  else:
#    print('break2')
    break

ans=0
#print(p[:X],q[:Y])
for i in p[:X]:
  ans+=i
for i in q[:Y]:
  ans+=i

print(ans)

Submission status: Runtime Error

Solution code:
from functools import reduce

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

x=X-1
y=Y-1

p=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)


if p[x]>=q[y]:
  a=q[y]
  f=1
else:
  a=p[x]
  f=2
while a<r[0] and len(r)>=1:
  if f==1 :
    q[y]=r[0]
    y-=1
    r.pop(0)
  elif f==2 :
    p[x]=r[0]
    x-=1
    r.pop(0)
  else:
    break
  if (y==-1 and x>=0) or (x>=0 and p[y]>q[x]):
    a=p[x]
    f=2
  elif (x==-1 and y>=0) or (y>=0 and p[x]>=q[y]):
    a=q[y]
    f=1
  else:
    break

ans=0
for i in p[:X]:
  ans+=i
for i in q[:Y]:
  ans+=i

print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A ,B, C = list(map(int, input().split()))
P = list(map(int, input().split()))
P.sort(reverse = True)
P = P[:X]
Q = list(map(int, input().split()))
Q.sort(reverse = True)
Q = Q[:Y]
R = list(map(int, input().split()))
R.sort(reverse = True)
ans = 0
for i in range(C):
  if(P[-1] < Q[-1]):
    if(P[-1] < R[i]):
      ans += R[i]
      P.pop()
  else:
    if(Q[-1] < R[i]):
      ans += R[i]
      Q.pop()
    else:
      break
ans += sum(P) + sum(Q)
print(ans)

Submission status: Runtime Error

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 == a or q_pos == b or r_pos == c:
        break
print(sum(plst) + sum(qlst))

Submission status: Runtime Error

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] = qlst[r_pos]
        p_pos += 1
        r_pos += 1
    else:
        qlst[q_pos] = rlst[r_pos]
        q_pos += 1
        r_pos += 1
    if p_pos == a or q_pos == b or r_pos == c:
        break
print(sum(plst) + sum(qlst))

Submission status: Runtime Error

Solution code:
from collections import deque

X, Y, A, B, C = [int(x) for x in input().split()]
p = deque(sorted([int(x) for x in input().split()]))
q = deque(sorted([int(x) for x in input().split()]))
r = deque(sorted([int(x) for x in input().split()]))

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

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

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

def merge(s, t):
    u = []

    while s + t:
        
        if s == [] or t == []:
            return u + s + t

        if s[0] >= t[0]:
            u.append(s[0])
            s = s[1:]
        else: # s0 > t0
            u.append(t[0])
            t = t[1:]

    return u

def merge_sort(A):
    while len(A) > 1:
        B = []
        for _ in range(len(A) // 2):
            B.append(merge(A[0], A[1]))
            A = A[2:]
        B += A
        A = B

    return(A[0])

X, Y, A, B, C = [int(x) for x in input().split()]
p = deque(merge_sort([[int(x)] for x in input().split()]))
q = deque(merge_sort([[int(x)] for x in input().split()]))
r = deque(merge_sort([[int(x)] for x in input().split()]))

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

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

print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=map(int,input().split())
P=sorted(map(int,input().split()), reverse=True)[:X]
Q=sorted(map(int,input().split()), reverse=True)[:Y]
R=sorted(map(int,input().split()), reverse=True)[:X+Y]
i,j=X-1,Y-1
for r in R:
    if i==-1 and j==-1:
        break
    elif j==-1:
        if r>P[i]:
            p[i]=r
            i-=1
        else:
            break
    elif i==-1:
        if r>Q[j]:
            Q[j]=r
            j-=1
        else:
            break
    elif P[i]>Q[j]:
        if r>Q[j]:
            Q[j]=r
            j-=1
        else:
            break
    else:
        if r>P[i]:
            p[i]=r
            i-=1
        else:
            break
print(sum(P)+sum(Q))

Submission status: Runtime Error

Solution code:
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)
Sp,Sq,Sr=[0],[0],[0]
for i in range(A):
    Sp.append(Sp[-1]+P[i])
for i in range(B):
    Sq.append(Sq[-1]+Q[i])
l,r=0,2*10**14
while not l+1==r:
    x=(l+r)//2
    tmp=Sp[X]+Sq[Y]
    i,j,k=X,Y,0
    while tmp<x and k<C:
        if i==0 and j==0:
            break
        elif i==0:
            if R[k]>Q[j-1]:
                tmp+=R[k]-Q[j-1]
                j-=1
                k+=1
            else:
                break
        elif j==0:
            if R[k]>P[i-1]:
                tmp+=R[k]-P[i-1]
                i-=1
                k+=1
            else:
                break
        elif P[i-1]>Q[j-1]:
            if R[k]>Q[j-1]:
                tmp+=R[k]-Q[j-1]
                j-=1
                k+=1
            else:
                break
        else:
            if R[k]>P[j-1]:
                tmp+=R[k]-P[i-1]
                i-=1
                k+=1
            else:
                break
    if tmp>=x:
        l=x
    else:
        r=x

print(l)