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:
mod = 10**9 + 7

def iip(listed = False):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        #print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod

#####################################################ライブラリ集ここまで

def main():
    X, Y, A, B, C = iip()
    p = iip(listed=True)
    q = iip(listed=True)
    r = iip(listed=True)

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

    red = p[:X]
    blue = q[:Y]
    red.sort()
    blue.sort()

    ir = 0
    ib = 0
    for nocolor in r:
        if min(red[ir], blue[ib]) < nocolor:
            if red[ir] < blue[ib]:
                red[ir] = nocolor
                if ir < len(red)-1:
                    ir += 1
            else:
                blue[ib] = nocolor
                if ir < len(red)-1:
                    ib +=1
        else:
            break

    print(sum(red)+sum(blue))



    pass

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
mod = 10**9 + 7

def iip(listed = False):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        #print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod

#####################################################ライブラリ集ここまで

def main():
    X, Y, A, B, C = iip()
    p = iip(listed=True)
    q = iip(listed=True)
    r = iip(listed=True)

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

    red = p[:X]
    blue = q[:Y]
    red.sort()
    blue.sort()

    ir = 0
    ib = 0
    for nocolor in r:
        if min(red[ir], blue[ib]) < nocolor:
            if red[ir] < blue[ib]:
                red[ir] = nocolor
                ir += 1
            else:
                blue[ib] = nocolor
                ib +=1
        else:
            break

    print(sum(red)+sum(blue))



    pass

if __name__ == "__main__":
    main()

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)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
            elif Q < B:
                if r[C-i-1] > q[Q]:
                    Q += 1
                    R -= 1
                else:
                    break
            else:
                break
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        else:
            break
    elif Q < B:
        if r[C-i-1] > q[Q]:
            Q += 1
            R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

Submission status: Runtime Error

Solution code:
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(int,input().split()))
lst_r = sorted(lst_r, ascending=False)[:x]
lst_g = sorted(lst_g, ascending=False)[:y]
lst_all = sorted(lst_r + lst_g + lst_n, ascending=False)[:x+y]
print(sum(lst_all))

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

rest = [[0] + sorted(p),
        [0] + sorted(q),
        [0] + sorted(r)]

ans = 0
count = [0, 0, 0]
while sum(count) < (X + Y):

    candidates = [rest[i][-1] if count[i] == X else 0 for i in range(3)]

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

    count[eat_idx] += 1
    ans += rest[eat_idx].pop()

print(ans)

Submission status: Runtime Error

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

Alist.sort(reverse = True)
Blist.sort(reverse = True)
cl.sort(reverse = True)
list=[]

for i in range(X):
    list.append(Alist[i])
for i in range(Y):
    list.append(Blist[i])
list.sort()

k = min (A + B, C)

for i in range(k):
    if cl[0] > list[i]:
        list[i] = cl[0]
        cl.pop(0)
ans = 0
for i in list:
    ans += i

print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
tmp = 0
p.sort(reverse=True)
p.append(0)
q.sort(reverse=True)
q.append(0)
r.sort(reverse=True)
r.append(0)
z = X+Y
for i in range(z):
    if X>0 and ((p[0]>=q[0] and p[0]>=r[0]) or (Y==0 and p[0]>=r[0])):
        tmp = tmp+p[0]
        p.pop(0)
        X -= 1
    elif Y>0 and ((q[0]>=p[0] and q[0]>=r[0]) or (X==0 and q[0]>=r[0])):
        tmp = tmp+q[0]
        q.pop(0)
        Y -= 1
    elif r[0]>p[0] and r[0]>q[0]:
        tmp = tmp+r[0]
        r.pop(0)
    else:
        p = 100/0
        
print(tmp)

Submission status: Runtime Error

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

Alist.sort(reverse = True)
Blist.sort(reverse = True)
cl.sort(reverse = True)
list=[]

for i in range(X):
    list.append(Alist[i])
for i in range(Y):
    list.append(Blist[i])
list.sort()

for i in range(C):
    if cl[0] > list[i]:
        list[i] = cl[0]
        cl.pop(0)
ans = 0
for i in list:
    ans += i

print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
q.sort()
r.sort()
r = sorted(r,reverse = True)
p = p[A-X:]
q = q[B-Y:]
if X+Y<C:
    r = r[:X+Y]
px = 0
qx = 0
for i in range(C):
    if px == len(p) and qx == len(q):
        break
    if px == len(p):
        if min(q[qx],r[i])==q[qx]:
            q[qx] =r[i]
            qx += 1
        else:
            break
    if qx == len(q):
        if min(p[px],r[i])==p[px]:
            p[px] =r[i]
            px += 1
        else:
            break
    else:
        if min(p[px],q[qx],r[i]) ==p[px]:
            p[px] = r[i]
            px +=1
        elif min(p[px],q[qx],r[i]) ==q[qx]:
            q[qx] = r[i]
            qx += 1
        else:
            break
print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
import sys,math,copy,queue,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
X,Y,A,B,C = LI()
p = LI()
q = LI()
r = LI()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pa =  [0] + list(itertools.accumulate(p))
qa =  [0] + list(itertools.accumulate(q))
p = [INF] + p
q = [INF] + q
red = X
green = Y
t = 0
pl = 0
while p[red] < r[t] or q[green] < r[t]:
    pl += r[t]
    t += 1
    elif p[red] < q[green] :
        red -= 1
    else:
        green -= 1
    if t >=C or (green ==0 and red ==0): break
print (pa[red] + qa[green] + pl)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
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()
z = p[-x:].extend(q[-y:]).extend(r)
z.sort()
print(sum(z[-x-y:]))

Submission status: Runtime Error

Solution code:
'''

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

'''
o=input().rstrip().split(' ')
x=int(o[0])
y=int(o[1])
a=int(o[2])
b=int(o[3])
c=int(o[4])
H=0;
R=input().rstrip().split(' ')
G=input().rstrip().split(' ')
ANY=input().rstrip().split(' ')
R.sort(key=int,reverse=True)
G.sort(key=int,reverse=True)
ANY.sort(key=int,reverse=True)
while(x>0):
    if int(R[0]) > int(ANY[0]):
        H+=int(R[0])
        del(R[0])
        x-=1;
    else:
        break;
while(y>0):
    if int(G[0]) > int(ANY[0]):
        H+=int(G[0])
        del(G[0])
        y-=1;
    else:
        break;
if(x>0 and y==0):
    for i in range(0,x):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(R[0])
            del(R[0])
elif(y>0 and x==0):
    for i in range(0,y):
        if i<len(ANY):
            H+=int(ANY[i]);
        else:
            H+=int(G[0])
            del(G[0])
else:
    FF=x+y;
    if FF<=len(ANY):
        for i in range(0,FF):
            H+=int(ANY[i])
    else:
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
            if x>0 and int(R[0]) <= int(G[0]):
                del(R[0])
                x-=1;
            elif y>0 and int(G[0]) <= int(R[0]):
                del(G[0])
                y-=1;
        XX=x;
        YY=y;
        for i in range(0,XX):
            H+=int(R[0])
            del(R[0])
        for i in range(0,YY):
            H+=int(G[0])
            del(G[0])
print(H)

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)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
            elif Q < B:
                if r[C-i-1] > q[Q]:
                    Q += 1
                    R -= 1
                else:
                    break
            else:
                break
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        else:
            break
    elif Q < B:
        if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
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()
z = p[-x:].extend(q[-y:]).extend(r)
z.sort()
print(sum(z[-x-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)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
            elif Q < B:
                if r[C-i-1] > q[Q]:
                    Q += 1
                    R -= 1
                else:
                    break
            else:
                break
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        else:
    elif Q < B:
        if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

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=0
green=0
clear=0
tmp=0

while True:
    if red == x and green != y:
        mini=q[-y+green]
        green+=1
    elif red != x and green == y:
        mini=p[-x+red]
        red += 1
    elif red == x and green == y:
        break;
    
    elif p[-x+red] < q[-y+green]:
        mini=p[-x+red]
        red += 1
    elif p[-x+red] > q[-y+green]:
        mini=q[-y+green]
        green+=1

    if r[-1-clear] > mini:
        tmp += (r[-1-clear] - mini)
        clear+=1
    else:
        break;


ans=sum(p[a-x:a])+sum(q[b-y:b])+tmp
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.append(100001);Q.append(100001);R.append(0)
P.sort(reverse=True);Q.sort(reverse=True);R.sort(reverse=True)

p_slice,q_slice,r_slice = x,y,0
while True:
    if P[p_slice]>Q[q_slice] and Q[q_slice]<R[r_slice]:
        q_slice-=1;r_slice+=1
    elif Q[q_slice]>=P[p_slice] and P[q_slice]<R[r_slice]:
        p_slice-=1;r_slice+=1
    else:
        break
print(sum(P[1:p_slice+1])+sum(Q[1:q_slice+1])+sum(R[:r_slice]))

Submission status: Runtime Error

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

s_a = [int(i) for i in list(input().split())]
s_b = [int(i) for i in list(input().split())]
s_c = [int(i) for i in list(input().split())]

s_a.sort(reverse = True)
s_b.sort(reverse = True)
s_c.sort(reverse = True)

x_a = s_a[0:x]
x_b = s_b[0:y]


x_a.append(x_b)
x_a.append(s_c)
x_a.sort(reverse = True)
ans = x_a[0:x+y]
print(sum(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)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
            elif r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        else:
            break
    elif Q < B:
        if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(4100000)

x,y,a,b,c = map(int,input(),split())
p = sort(list(map(int,input(),split())))
q = sort(list(map(int,input(),split())))
r = list(map(int,input(),split()))
ans = 0

for k in range(1,x+1):
    r.append(p[-k])
for v in range(1,y+1):
    r.append(p[-v])

rs = sort(r)

for i in range(1,X+Y+1):
    ans += rs[-i]
print(str(ans))

Submission status: Runtime Error

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

for k in range(1,x+1):
    r.append(p[-k])
for v in range(1,y+1):
    r.append(p[-v])

rs = sort(r)

for i in range(1,X+Y+1):
    ans += rs[-i]
print(str(ans))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in stdin.readline().rstrip().split()]
q = [[int(x), "B"]  for x in stdin.readline().rstrip().split()]
r = [[int(x), "C"] for x in stdin.readline().rstrip().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]
apples = p+q+r

apples.sort(reverse=True)
#print(apples)
ans = 0
pend = 0

while (X > 0 or Y > 0) and X+Y > pend:
    if apples[0][1] == "C":
        pend += 1
        ans += apples[0][0]
        apples.pop(0)
    elif apples[0][1] == "A":
        if X > 0:
            ans += apples[0][0]
            apples.pop(0)
            X -= 1
        else:
            apples.pop(0)
    elif apples[0][1] == "B":
        if Y>0:
            ans += apples[0][0]
            apples.pop(0)
            Y -= 1
        else:
            apples.pop(0)
    #print(ans)
    #print("XY", X, Y, pend)
#print("XY", X, Y, pend)
print(ans)

Submission status: Runtime Error

Solution code:
from functools import lru_cache

X, Y, A, B, C = [int(x) for x in input().split()]
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
R = [int(x) for x in input().split()]

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

red = P[:X]
green = Q[:Y]
best = sum(red) + sum(green)

for x in R:
    a1, a2 = red, green
    if a1[-1] > a2[-1]:
        a1, a2 = a2, a1
    assert a1[-1] <= a2[-1]
    if a1[-1] < x:
        best -= a1.pop()
        best += x
print(best)

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
ans += sum(p[:x])+sum(q[:y])
lp = (p[:x])
lq = (q[:y])
#print(lp)
#print(lq)
flag = 0
for i in range(x):
    if lp[i] < r[0]:
        del lp[:i]
        flag = 1
        break
if flag == 0:
  lp = []
  
flag = 0
for i in range(y):
    if lq[i] < r[0]:
        del lq[:i]
        flag = 1
        break
if flag == 0:
  lq = []
        
temp1 = 0
temp2 = 0
#print(lp)
#print(lq)
for i in range(1,10**9):
    #if lp[i] >= r[i] and lq[i] >= r[i]:
    #    break
    if i == max(x,y) or i == c+1 or (len(lq) < i and len(lp) < i) or (lp[-i] < r[i-1] and lq[-i] < r[i-1]):
      break
    elif (i == y or len(lq) < i) and lp[-i] < r[i-1]:
        temp2 = ans - lp[-i] + r[i-1]
    elif (i == x or len(lp) < i) and lq[-i] < r[i-1]:
        temp1 = ans - lq[-i] + r[i-1]
    else:
        temp1 = ans - lp[-i] + r[i-1]
        temp2 = ans - lq[-i] + r[i-1]
    ans = max(temp1,temp2,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)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
            elif Q < B:
                if r[C-i-1] > q[Q]:
                    Q += 1
                    R -= 1
                else:
                    break
            else:
                break
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
    elif Q < B:
        if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

Submission status: Runtime Error

Solution code:
from sys import stdin
input = sys.stdin.readline
X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in stdin.readline().rstrip().split()]
q = [[int(x), "B"]  for x in stdin.readline().rstrip().split()]
r = [[int(x), "C"] for x in stdin.readline().rstrip().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]
apples = p+q+r

apples.sort(reverse=True)
#print(apples)
ans = 0
pend = 0

while (X > 0 or Y > 0) and X+Y > pend:
    if apples[0][1] == "C":
        pend += 1
        ans += apples[0][0]
        apples.pop(0)
    elif apples[0][1] == "A":
        if X > 0:
            ans += apples[0][0]
            apples.pop(0)
            X -= 1
        else:
            apples.pop(0)
    elif apples[0][1] == "B":
        if Y>0:
            ans += apples[0][0]
            apples.pop(0)
            Y -= 1
        else:
            apples.pop(0)
    #print(ans)
    #print("XY", X, Y, pend)
#print("XY", X, Y, pend)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

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

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

ans = 0

while True:
    if X == 0 and Y == 0:
        break
    if X > 0:
        if len(p) == 0 or len(r) == 0:
            if len(p) == 0:
                ans += r.pop()
                X -= 1
            if len(r) == 0:
                ans += p.pop()
                X -= 1
        else:
            if p[-1] >= r[-1]:
                ans += p.pop()
                X -= 1
            if X > 0 and Y == 0 and p[-1] < r[-1]:
                ans += r.pop()
                X -= 1
    if Y > 0:
        if len(q) == 0 and len(r) == 0:
            if len(q) == 0:
                ans += r.pop()
                Y -= 1
            if len(r) == 0:
                ans += q.pop()
                Y -= 1
        else:
            if q[-1] >= r[-1]:
                ans += q.pop()
                Y -= 1
            if Y > 0 and X == 0 and q[-1] < r[-1]:
                ans += r.pop()
                Y -= 1
    if X > 0 and Y > 0:
        if p[-1] <= r[-1] or q[-1] <= r[-1]: 
            if p[-X] >= q[-Y] and X > 0:
                ans += r.pop()
                Y -= 1
            if p[-X] < q[-Y] and Y > 0:
                ans += r.pop()
                X -= 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 = sorted(p)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
            elif r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
    elif Q < B:
        if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

Submission status: Runtime Error

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    x,y,a,b,c=map(int,input().split())
    p=sorted(list(map(int,input().split())), reverse=True)[:x]
    q=sorted(list(map(int,input().split())), reverse=True)[:y]
    r=sorted(list(map(int,input().split())), reverse=True)
    print(sum(sorted(p+q+r), reverse=True)[:x+y])

if __name__ == '__main__':
    main()

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())))
S=sorted(P[A-X:]+Q[B-Y:])
ans=sum(S)
for i in range(min(R,S)):
    if S[i]<R[-1-i]:
        S[i]=R[-1-i]
    else:
        break
print(sum(S))

Submission status: Runtime Error

Solution code:
from collections import deque
x, y, a, b, c = map(int, input().split())
num = max(a, b, c)
P = sorted(list(map(int, input().split())), reverse=1)
for _ in range(num - a):
    P.append(0)
pq = deque(P)
Q = sorted(list(map(int, input().split())), reverse=1)
for _ in range(num - b):
    Q.append(0)
qq = deque(Q)
R = sorted(list(map(int, input().split())), reverse=1)
for _ in range(num - c):
    R.append(0)
rq = deque(R)
mx = max(x, y)
ans = 0
for i in range(1, mx+1):
    p = pq.popleft()
    q = qq.popleft()
    r = rq.popleft()
    if i <= x and i <= y:
        if p < q:
            if p < r:
                ans += r
            else:
                ans += p
                rq.appendleft(r)
            nen = rq.popleft()
            if q < nen:
                ans += nen
            else:
                ans += q
                rq.appendleft(nen)
        if p > q:
            if q < r:
                ans += r
            else:
                ans += q
                rq.appendleft(r)
            nen = rq.popleft()
            if p < nen:
                ans += nen
            else:
                ans += p
                rq.appendleft(nen)
    elif i <= x:
        if p < r:
            ans += r
        else:
            ans += p
            rq.appendleft(r)
    else:
        if q < r:
            ans += r
        else:
            ans += q
            rq.appendleft(r)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np
X, Y, A, B, C = (int(i) for i in input().split())
p = [[int(x), "A"] for x in stdin.readline().rstrip().split()]
q = [[int(x), "B"]  for x in stdin.readline().rstrip().split()]
r = [[int(x), "C"] for x in stdin.readline().rstrip().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]
apples = p+q+r

apples.sort(reverse=True)
#print(apples)
ans = 0
pend = 0

while (X > 0 or Y > 0) and X+Y > pend:
    if apples[0][1] == "C":
        pend += 1
        ans += apples[0][0]
        apples.pop(0)
    elif apples[0][1] == "A":
        if X > 0:
            ans += apples[0][0]
            apples.pop(0)
            X -= 1
        else:
            apples.pop(0)
    elif apples[0][1] == "B":
        if Y>0:
            ans += apples[0][0]
            apples.pop(0)
            Y -= 1
        else:
            apples.pop(0)
    #print(ans)
    #print("XY", X, Y, pend)
#print("XY", X, Y, pend)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_right, bisect_left

def to_cumsum(arr, pad=True):
    """
    1次元配列を累積和の1次元配列に変換する
    pad: 先頭に0の項を作る場合はTrue
    """
    cumsum = 0
    padding = 1 if pad else 0
    new = [0] * (len(arr) + padding)
    for i, e in enumerate(arr):
        cumsum += e
        new[i + padding] = cumsum
    return new

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


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

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

Pcum = to_cumsum(P, pad=True)
Qcum = to_cumsum(Q, pad=True)
Rcum = to_cumsum(R, pad=True)


def bi(arr, v):
    l = 0
    r = len(arr) - 1
    while l + 1 < r:
        mid = (l + r) // 2
        if arr[mid] >= v:
            l = mid
        else:
            r = mid
    return r

ans = 0
for i in range(X + 1):
    a = Pcum[i]
    idx = bi(Q, R[X - i - 1])
    a += Qcum[idx]
    a += Rcum[min(idx, len(Rcum) - 1)]
    ans = max(ans, a)
print(ans)

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
    countC = 0
    ans = 0
    i = 0
    j = 0
    k = 0
    while countA < X and countB < Y and countC < C:
        v = max([p[i], q[j], r[k]])
        if p[i] == v:
            ans += p[i]
            countA += 1
            i += 1
        elif q[j] == v:
            ans += q[j]
            countB += 1
            j += 1
        else:
            ans += r[k]
            countC += 1
            k += 1

    if countA == X:
        targ = q[j:] + r[k:]
        targ.sort(reverse = True)
        return ans + sum(targ[:Y-countB-countC])
    if countB == Y:
        targ = p[i:] + r[k:]
        targ.sort(reverse = True)
        return ans + sum(targ[:X-countA-countC])

    pp = [0]
    qq = [0]
    p = p[i:]
    q = q[j:]
    for i in range(len(p)):
        pp.append(pp[-1] + p[i])
    for i in range(len(q)):
        qq.appendO(qq[-1] + q[i])

    out = ans
    for k in range(countC+1):
        m = X - k
        n = Y - (countC - k)
        if 0<= m < len(pp) and 0<=n < len(qq):
            out = max(out, ans + pp[m] + qq[n])
    return ans



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