Problem:


Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

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

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

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

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

Eat the 2-nd red apple.

Eat the 1-st green apple.

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

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

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

Sample Output 3

74



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


Sample output:
12

Submission status: Runtime Error

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

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  value = int(value)
  if len(p_list) >= x:
    if p_list[x-1] < value:
      insort(p_list,value)
  else:
    insort(p_list,value)

for value in input().split():
  value = int(value)
  if len(q_list) >= y:
    if q_list[y-1] < value:
      insort(q_list,value)
  else:
    insort(q_list,value)
  
for value in input().split():
  value = int(value)
  if len(r_list) >= x+y:
    if r_list[x+y-1] < value:
      insort(r_list.value)
  else:
    insort(r_list,value)
  
red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)

Submission status: Runtime Error

Solution code:
import heapq
X, Y, a, b, c = map(int, input().split())
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)

ans = 0
for _ in range(X):
    ap1 = heapq.heappop(p)
    ap2 = heapq.heappop(r)
    if ap1 <= ap2:
        ans -= ap1
        heapq.heappush(r, ap2)
    else:
        ans -= ap2
        heapq.heappush(p, ap1)

for _ in range(Y):
    ap1 = heapq.heappop(q)
    ap2 = heapq.heappop(r)
    if ap1 <= ap2:
        ans -= ap1
        heapq.heappush(r, ap2)
    else:
        ans -= ap2
        heapq.heappush(q, ap1)

print(ans)

Submission status: Runtime Error

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

pp = p[:x]
qq = q[:y]
rr = []
for k in r:
    if pp[-1] < qq[-1]:
        if pp[-1] < k:
            pp.pop()
            rr.append(k)
        else:
            break
    else:
        if qq[-1] < k:
            qq.pop()
            rr.append(k)
        else:
            break

print(sum(pp)+sum(qq)+sum(rr))

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.append(0)
q.append(0)
r.append(0)
p.sort()
q.sort()
r.sort()

ans = 0

while True:
    if X > 0:
        if len(p) == 0:
            ans += r.pop()
            X -= 1
        elif len(r) == 0:
            ans += p.pop()
            X -= 1
    if Y > 0:
        if len(q) == 0:
            ans += r.pop()
            Y -= 1
        elif len(r) == 0:
            ans += q.pop()
            Y -= 1
    if X > 0 and 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 and 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 0 < len(r):
        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
    if X <= 0 and Y <= 0:
        break
print(ans)

Submission status: Runtime Error

Solution code:
import sys

X, Y, A, B, C = map(int, sys.stdin.readline().strip().split())

P = list(map(int, sys.stdin.readline().strip().split()))
Q = list(map(int, sys.stdin.readline().strip().split()))
R = list(map(int, sys.stdin.readline().strip().split()))

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

P = P[:X]
P.sort()
Q = Q[:Y]
Q.sort()

ans = 0
i, j, k = 0, 0, 0
while C > k:

    if P[i] >= Q[j]:
        if R[k] > Q[j] and Y > j:
            Q[j] = R[k]
            j += 1
            k += 1
        elif R[k] > P[i] and X > i:
            P[i] = R[k]
            i += 1
            k += 1
        else:
            break

    elif P[i] < Q[j]:
        if R[k] > P[i] and X > i:
            P[i] = R[k]
            i += 1
            k += 1
        elif R[k] > Q[j] and Y > j:
            Q[j] = R[k]
            j += 1
            k += 1
        else:
            break
        

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

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int,input().split()) 
P = [list(map(int,input().split())) for i in range(3)]
Red = sorted(P[0], reverse=True)
Green = sorted(P[1], reverse=True)
Trans = sorted(P[2], reverse=True)

pre_max = sum(Red[:X]) + sum(Green[:Y])
Red = Red[:X]
Green = Green[:Y]
for i in range (C):
  maxo = pre_max
  if (Trans[i] > Red[-i+1]):
    maxo= pre_max + Trans[i] - Red[-i+1]
  elif (Trans[i] > Green[-i+1]):
    maxo = pre_max + Trans[i] - Green[-i+1]
    
print(maxo)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    X, Y, A, B, C = input_nums()
    P = input_nums()
    Q = input_nums()
    R = input_nums()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    res = []
    res += P[:X]
    res += Q[:Y]
    res.sort(reverse=True)
    index = 0
    maxindex = len(R)-1
    for i in range(len(res)):
        if res[i] < R[index] and index <= maxindex:
            res[i] = R[index]
            index += 1
    print(sum(res))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

listp = sorted(listp, reverse=True)
listq = sorted(listq, reverse=True)
listr = sorted(listr, reverse=True)
listp = listp[:X]
listq = listq[:Y]

eat_noapple = []
oisisa = 0

for noapple in listr:
    if listp[-1] >= listq[-1] and listq[-1] < noapple:
        listq.pop(-1)
        eat_noapple.append(noapple)
    elif listp[-1] < listq[-1] and listp[-1] < noapple:
        listp.pop(-1)
        eat_noapple.append(noapple)
    else:
        break
try:
    for papple in listq:
        oisisa += papple
except:
    a = 0
try:
    for qapple in listp:
        oisisa += qapple
except:
    a = 0
try:
    for rapple in eat_noapple:
        oisisa += rapple
except:
    a = 0
print(oisisa)

Submission status: Runtime Error

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

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

p_point = 0
q_point = 0
r_point = 0

ans = 0

for i in range(x):
    if p[p_point] > r[r_point]:
        ans += p[p_point]
        p_point += 1
    else:
        ans += r[r_point]
        r_point += 1

for i in range(y):
    if q[q_point] > r[r_point]:
        ans += q[q_point]
        q_point += 1
    else:
        ans += r[r_point]
        r_point += 1
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from collections import deque

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

r.sort()

if A < X:
  p += r[-(X - A):]
  r = r[:-(X - A)]
  A = X

if B < Y:
  q += r[-(Y - B):]
  r = r[:-(Y - B)]
  B = Y

p.sort()
q.sort()

pq = deque((heapq.merge(p[A - X:], q[B - Y:])))

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

print(sum(pq))

Submission status: Runtime Error

Solution code:


import sys
# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())

def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())

# 入力全てを整数に変換したものの配列を受け取る
# def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def LI(): return list(map(lambda x:int(x)*(-1), sys.stdin.readline().split()))
# from heapq import heappop,heappush,heapify
import heapq
x,y,a,b,c = MI()
p = LI()
q = LI()
r = LI()

result = 0
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
used_c = []
used_a = []


for _ in range(x):
	if not r==[]:
		max_a = heapq.heappop(p)*(-1)
		max_c = heapq.heappop(r)*(-1)
		print(max_a,max_c,'a')
		if max_a>=max_c:
			result += max_a
			a -= 1
			heapq.heappush(r,max_c*(-1))
			heapq.heappush(used_a,max_a)
		else:
			result += max_c
			c -= 1
			heapq.heappush(p,max_a*(-1))
			heapq.heappush(used_c,max_c)
	else:
		max_a = heapq.heappop(p)*(-1)
		result += max_a
		heapq.heappush(used_a,max_a)
		a -= 1

for _ in range(y):
	if not r==[]:
		max_b = heapq.heappop(q)*(-1)
		max_c = heapq.heappop(r)*(-1)
		if max_b>=max_c:
			result += max_b
			b -= 1
			heapq.heappush(r,max_c*(-1))
		else:
			result += max_c
			c -= 1
			heapq.heappush(q,max_b*(-1))
			heapq.heappush(used_c,max_c)
	else:
		max_b = heapq.heappop(q)*(-1)
		result += max_b
		b -= 1

while(1):
	if not used_c==[] and p==[]:
		max_c = heapq.heappop(used_c)
		max_a = heapq.heappop(p)*(-1)
		if max_a>max_c:
			result += max_a
			result -= max_c
		else:
			break
	if used_c==[] or p==[]:
		break

print(result)

Submission status: Runtime Error

Solution code:
def main():
    X, Y, A, B, C = [int(i) for i in input().split()]
    red = [int(i) for i in input().split()]
    blue = [int(i) for i in input().split()]
    noc = [int(i) for i in input().split()]
    red = sorted(red)[-X:]
    blue = sorted(blue)[-Y:]
    rb = sorted(red + blue)
    score = sum(rb)
    noc = sorted(noc)[::-1]
    print(rb)
    print(noc)

    s_noc = 0
    s_rb = 0
    for i in range(min(A+B, C)):
        if rb[i] > noc[i]:
            break
        s_noc += noc[i]
        s_rb += rb[i]

    print(score + s_noc - s_rb)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    X, Y, A, B, C = input_nums()
    P = input_nums()
    Q = input_nums()
    R = input_nums()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    res = []
    res += P[:X]
    res += Q[:Y]
    res.sort(reverse=True)
    index = 0
    for i in range(len(res)):
        if res[i] < R[index]:
            res[i] = R[index]
            index += 1
    print(sum(res))
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())))[a-x:]
q = sorted(list(map(int,input().split())))[b-y:]
r = sorted(list(map(int,input().split())), reverse = True)
P=0
Q=0
for i in range(c):
  if p[P] < r[i] or q[Q] < r[i]:
    if p[P] >= q[Q]:
      q[Q]=r[i]
      Q += 1
    else:
      p[P]=r[i]
      P += 1
  else:
    break
print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
import heapq
x,y,a,b,c=map(int,input().split())
def _int(x):
    return -int(x)
p=list(map(_int,input().split()))
q=list(map(_int,input().split()))
r=list(map(_int,input().split()))
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
ans=0
for i in range(x):
    _p,_r=heapq.heappop(p),heapq.heappop(r)
    if _p<_r:
        ans-=_p
        heapq.heappush(r,_r)
    else:
        ans-=_r
        heapq.heappush(p,_p)
for i in range(y):
    _q,_r=heapq.heappop(q),heapq.heappop(r)
    if _q<_r:
        ans-=_q
        heapq.heappush(r,_r)
    else:
        ans-=_r
        heapq.heappush(q,_q)
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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
x=0
y=0
R=0
z=0
s=0
while (x<X or y<Y) and z<X+Y:
  if (max(p[0],q[0],r[0])==p[0] or (y==Y and r[0]<p[0])) and x<X:
    s+=p[0]
    x+=1
    p[0]=p[x]
    z+=1
  elif (max(p[0],q[0],r[0])==q[0] or (x==X and r[0]<q[0])) and y<Y:
    s+=q[0]
    y+=1
    q[0]=q[y]
    z+=1
  else:
    s+=r[0]
    R+=1
    r[0]=r[R]
    z+=1
print(s)

Submission status: Runtime Error

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

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

p = p[:x]
q = q[:y]
p.append(0)
q.append(0)

wa = x + y

lsp = x - 1
lsq = y - 1

pi = 0
qi = 0
ri = 0

ans = 0

for i in range(wa):
    if x > 0 and p[pi] == max(p[pi], q[qi], r[ri]):
        ans += p[pi]
        pi += 1
        x -= 1
    elif y > 0 and q[qi] == max(p[pi], q[qi], r[ri]):
        ans += q[qi]
        qi += 1
        y -= 1
    elif r[ri] == max(p[pi], q[qi], r[ri]):
        ans += r[ri]
        ri += 1
        if y > 0 and p[lsp] >= q[lsq]:
            y -= 1
            if lsq == 0:
                continue
            else:
                lsq -= 1
        elif x > 0 and p[lsp] < q[lsq]:
            x -= 1
            if lsp == 0:
                continue
            else:
                lsp -= 1

print(ans)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
R = sorted(list(map(int, input().split())), reverse=True)
G = sorted(list(map(int, input().split())), reverse=True)
M = sorted(list(map(int, input().split())), reverse=True)

ans, r, g, m = 0, 0, 0, 0
mr = R[x - 1]
mg = G[y - 1]

for i in range(x + y):
    print(r, g, m)
    if r >= x:
        if G[g] > M[m] or m >= c:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
    elif g >= y:
        if R[r] > M[m] or m >= c:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] >= G[g]:
        if R[r] > M[m] or m >= c:
            ans += R[r]
            r += 1
        else:
            ans += M[m]
            m += 1
    elif R[r] < G[g] or r >= x:
        if G[g] > M[m] or m >= c:
            ans += G[g]
            g += 1
        else:
            ans += M[m]
            m += 1
print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
l=[]
#print(p,q,r)
delicious=0
for i in range(X):
    l.append(p[i])
for i in range(Y):
    l.append(q[i])
l.sort(reverse=True)
#print(l)
for i in range(len(l)-C):
    #print(l[i])
    delicious=delicious+l[i]
lidx=len(l)-C
cidx=0
#print(delicious,lidx,cidx,l,r,l[lidx],r[cidx])
for j in range(C):
    if l[lidx]<=r[cidx]:
        delicious=delicious+r[cidx]
        cidx=cidx+1
    else:
        delicious = delicious + l[lidx]
        lidx=lidx+1
    #print(delicious)
print(delicious)

Submission status: Runtime Error

Solution code:
def main():
    X, Y, A, B, C = [int(i) for i in input().split()]
    red = [int(i) for i in input().split()]
    blue = [int(i) for i in input().split()]
    noc = [int(i) for i in input().split()]
    red = sorted(red)[-X:]
    blue = sorted(blue)[-Y:]
    rb = sorted(red + blue)
    score = sum(rb)
    noc = sorted(noc)[::-1]

    s_noc = 0
    s_rb = 0
    for i in range(max(A+B, C)):
        if rb[i] > noc[i]:
            break
        s_noc += noc[i]
        s_rb += rb[i]

    print(score + s_noc - s_rb)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

r = deque(r)
deque_p = deque(p[:X])
deque_q = deque(q[:Y])
deque_r = deque([])

while min(deque_p[-1], deque_q[-1]) < r[0]:
    if deque_p[-1] <= deque_q[-1]:
        deque_p.pop()
        deque_r.append(r.popleft())
    else:
        deque_q.pop()
        deque_r.append(r.popleft())

    if not r:
        break

ans = sum(deque_p) + sum(deque_q) + sum(deque_r)

print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int,input().split()) 
P = [list(map(int,input().split())) for i in range(3)]
Red = sorted(P[0], reverse=True)
Green = sorted(P[1], reverse=True)
Trans = sorted(P[2], reverse=True)

pre_max = sum(Red[:X]) + sum(Green[:Y])
Red = Red[:X]
Green = Green[:Y]
for i in range (C):
  
  if (Trans[i] > Red[-i+1]):
    max = pre_max + Trans[i] - Red[-i+1]
  if (Trans[i] > Green[-i+1]):
    max = pre_max + Trans[i] - Green[-i+1]
    
print(max)

Submission status: Runtime Error

Solution code:
#文字列の入力
A = input()
#文字列の入力
p = input()
#文字列の入力
q = input()
#文字列の入力
r = input()



#コンマで区切る
A_list = A.split(' ')
#for i in range(len(A_list)):
    #A_list[i] = int(A_list[i])
    

#コンマで区切る
p_list = p.split(' ')
for i in range(len(p_list)):
    p_list[i] = int(p_list[i])

#コンマで区切る
q_list = q.split(' ')
for i in range(len(q_list)):
    q_list[i] = int(q_list[i])

#コンマで区切る
r_list = r.split(' ')
for i in range(len(r_list)):
    r_list[i] = int(r_list[i])
    
ans_list = []
#無色を考えない
for i in range(int(A_list[0])):
  ans_list.append(max(p_list))
  p_list.remove(max(p_list))
for i in range(int(A_list[1])):
  ans_list.append(max(q_list))
  q_list.remove(max(q_list))

for i in range(len(ans_list)):
  if(ans_list[i] < max(r_list)):
    ans_list[i] = max(r_list)
    r_list.remove(max(r_list))
    
    
ans = sum(ans_list)


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()))
s = p + q + r
S = sorted(s, reverse = True)
i = 0
j = 0
ans = 0
a = 0
b = 0
c = 0
while i < (X + Y):
  if S[j] in r:
    c += 1
    r.remove(s[j])
    ans += S[j]
    i += 1
  elif S[j] in p and a < X:
    a += 1
    p.remove(S[j])
    ans += S[j]
    i += 1
  elif S[j] in q and b < Y:
    b += 1
    q.remove(S[j])
    ans += S[j]
    i += 1
  j += 1
print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
#dp問題と予想
ans=0
P=[]
for i in range(X):
    P.append(p[i])
    ans+=p[i]
P.sort()
Q=[]
for i in range(Y):
    Q.append(q[i])
    ans+=q[i]
Q.sort()
rc=0
gc=0
tc=0
fr=False
fg=False
while(1):
    val1=ans
    val2=ans
    if tc>(C-1):
        break
    if rc>(A-1):
        fr=True
    if gc>(B-1):
        fg=True
    if fr and fg:
        break
    if fr:
        b=r[tc]-Q[gc]
        if b<=0:
            break
        val2+=b
        ans=val2
        gc+=1
        tc+=1
        continue
    if gc:
        a=r[tc]-P[rc]
        if a<=0:
            break
        val1+=a
        ans=val1
        rc+=1
        tc+=1
        continue
    a=r[tc]-P[rc]
    b=r[tc]-Q[gc]
    if a<=0 and b<=0:
        break

    val1+=a
    val2+=b
    if val1>val2:
        rc+=1
        tc+=1
        ans=val1
    else:
        gc+=1
        tc+=1
        ans=val2

print(ans)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

X, Y, A, B, C = i_l()
p = i_l()
q = i_l()
r = i_l()

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

p_i = 0
q_i = 0
r_i = 0

ans1 = 0
for i in range(X):
    if p[i] > r[r_i] or r_i > C-1:
        ans1 += p[i]
    else:
        ans1 += r[r_i]
        r_i += 1
for i in range(Y):
    if q[i] > r[r_i] or r_i > C-1:
        ans1 += q[i]
    else:
        ans1 += r[r_i]
        r_i += 1


ans2 = 0
r_i = 0
for i in range(X):
    if p[i] > r[r_i] or r_i > C-1:
        ans2 += p[i]
    else:
        ans2 += r[r_i]
        r_i += 1
for i in range(Y):
    if q[i] > r[r_i] or r_i > C-1:
        ans2 += q[i]
    else:
        ans2 += r[r_i]
        r_i += 1

ans3 = 0
if X + Y <= C:
    ans3 = sum(r[:(X+Y)])

print(max(ans1, ans2, ans3))

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()))
result = 0
Z = 0
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

pindex = 0
qindex = 0
rindex = 0
while (X>0 or Y>0) and X+Y-Z>0:
        
    
    if (X>0 or Y>0) and X+Y-Z>0:
        if p[pindex]>=q[qindex] and p[pindex]>=r[rindex] and X > 0:
            result += p[pindex]
            pindex += 1
            X-=1
        elif q[qindex]>=p[pindex] and q[qindex]>=r[rindex] and Y > 0:
            result += q[qindex]
            
            qindex +=1
            Y-=1
        elif r[rindex]>=q[qindex] and r[rindex]>=p[pindex]:
        
            result += r[rindex]
            rindex += 1
            Z+=1

        elif X > 0:
            result += p[pindex]
            pindex += 1
            X-=1
        elif Y > 0:
            result += q[qindex]
            
            qindex +=1
            Y-=1
       
print(result)

Submission status: Runtime Error

Solution code:
import heapq
def E():
    X, Y, A, B, C = map(int, input().split())
    reds = []
    heapq.heapify(reds)
    for n in input().split():
        heapq.heappush(reds, int(n))
    greens = []
    heapq.heapify(greens)
    for n in input().split():
        heapq.heappush(greens, int(n))
    colorless = []
    heapq.heapify(colorless)
    for n in input().split():
        heapq.heappush(colorless, -int(n))
    
    redx = heapq.nlargest(X, reds)
    greeny = heapq.nlargest(Y, greens)
    colorless_val = 0
    while True:
        if not colorless:
            break
        cur = abs(heapq.heappop(colorless))
        if cur > redx[-1]:
            if redx[-1] > greeny[-1]:
                greeny.pop()
            else:
                redx.pop()
            colorless_val += cur
        elif cur > greeny[-1]:
            if greeny[-1] > redx[-1]:
                redx.pop()
            else:
                greeny.pop()
            colorless_val += cur
        else:
            break
    print(sum(redx)+sum(greeny)+colorless_val)

E()

Submission status: Runtime Error

Solution code:
from sys import stdin

x, y,a,b,c = [int(x) for x in stdin.readline().rstrip().split()]

p = [int(x) for x in stdin.readline().rstrip().split()]
q = [int(x) for x in stdin.readline().rstrip().split()]
r = [int(x) for x in stdin.readline().rstrip().split()]

#print(p)
#print(q)
#print(r)

ps = sorted(p, reverse=True)
qs = sorted(q, reverse=True)
#rs = sorted(p, reverse=True)

#print(ps[0:x])
#print(qs[0:y])

pss = ps[0:x]
pss.extend(qs[0:y])
#print(pss)

he = 0

for i in range(a+b):
    for j in range(len(r)):
        if pss[i] < r[j]:
            pss[i] = r.pop(j)
            #print("pop")
            #print(r)
            break
    #print(he)
    he += pss[i]
print(he)

Submission status: Runtime Error

Solution code:
import heapq
from collections import deque

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

r.sort()

if A < X:
  p += r[C - (X - A):]
  r = r[:C - (X - A)]
  A = X

if B < Y:
  q += r[C - (Y - B):]
  r = r[:C - (Y - B)]
  B = Y

p.sort()
q.sort()

pq = deque((heapq.merge(p[A - X:], q[B - Y:])))

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

print(sum(pq))

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.append(0)
q.append(0)
r.append(0)
p.sort()
q.sort()
r.sort()

ans = 0

while True:
    if X > 0:
        if len(p) == 0:
            ans += r.pop()
            X -= 1
        elif len(r) == 0:
            ans += p.pop()
            X -= 1
    if Y > 0:
        if len(q) == 0:
            ans += r.pop()
            Y -= 1
        elif len(r) == 0:
            ans += q.pop()
            X -= 1
    if X > 0 and 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 and 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 0 < len(r):
        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
    if X <= 0 and Y <= 0:
        break
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    X, Y, A, B, C = map(int, input().split())
    pA = sorted(map(int, input().split()), reverse=True)
    pB = sorted(map(int, input().split()), reverse=True)
    pC = sorted(map(int, input().split()), reverse=True)

    # dp[red_count][green_count] = (value, red-index, green-index, non-index)
    dp = [[(0, 0, 0, 0) for _ in range(Y + 1)] for _ in range(X + 1)]

    for i in range(1, max(X, Y) + 1):
        ri = i
        gi = i - 1
        if 0 < ri:
            v, r, g, n = dp[ri - 1][gi]
            dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        if 0 < gi:
            v, r, g, n = dp[ri][gi - 1]
            dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        ri = i - 1
        gi = i
        if 0 < ri:
            v, r, g, n = dp[ri - 1][gi]
            dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        if 0 < gi:
            v, r, g, n = dp[ri][gi - 1]
            dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        ri = i
        gi = i
        if 0 < ri:
            v, r, g, n = dp[ri - 1][gi]
            dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        if 0 < gi:
            v, r, g, n = dp[ri][gi - 1]
            dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))

    print(dp[X][Y][0])


main()

Submission status: Runtime Error

Solution code:
x, y, a, b, c = [int(arg) for arg in input().split()]
p = [int(arg) for arg in input().split()]
q = [int(arg) for arg in input().split()]
r = [int(arg) for arg in input().split()]

result = 0
leftX, leftY = x, y
p.sort()
q.sort()
r.sort()
while leftX != 0 and r[-1] <= p[-1]:
    result += p.pop()
    leftX -= 1
while leftY != 0 and r[-1] <= q[-1]:
    result += q.pop()
    leftY -= 1
for _ in range(leftX + leftY):
    result += r.pop()

print(result)

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)

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

x_a.reverse()
x_b.reverse()

s = 0
v = 0
t = 0
while s <= x and v <= y and t <= c:
    if x_a[s] <= x_b[v]:
        #print('B')
        if x_a[s] < s_c[t]:
            x_a[s] = s_c[t]
            s += 1
            t += 1
        else:
            break
    else:
        #print('A')
        if x_b[v] < s_c[t]:
            x_b[v] = s_c[t]
            v += 1
            t += 1
        else:
            break

ans = sum(x_a) + sum(x_b)
print(ans)

Submission status: Runtime Error

Solution code:
from heapq import *
from itertools import *

class cached_iter(object):
  def __init__(self, it):
    self.it = iter(it)
    self.prev = None
    self._back = False

  def __next__(self):
    if self._back:
      self._back = False
      if self.prev:
        return self.prev
    self.prev = next(self.it)
    return self.prev
        
  def back(self):
    self._back = True

  def __iter__(saelf):
    return self

X, Y, A, B, C = map(int, input().split())
P = sorted(map(lambda x:-int(x), input().split()))
Q = sorted(map(lambda x:-int(x), input().split()))
R = cached_iter(sorted(map(lambda x:-int(x), input().split())))

S = sum(islice(merge(P,R), X))
R.back()
S += sum(islice(merge(Q,R), Y))
print(-S)

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()))
s = p + q + r
S = sorted(s, reverse = True)
print(S)
i = 0
j = 0
ans = 0
a = 0
b = 0
c = 0
while i < (X + Y):
  if S[j] in r:
    c += 1
    r.remove(s[j])
    ans += S[j]
    i += 1
  elif S[j] in p and a < X:
    a += 1
    p.remove(S[j])
    ans += S[j]
    i += 1
  elif S[j] in q and b < Y:
    b += 1
    q.remove(S[j])
    ans += S[j]
    i += 1
  j += 1
print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=map(int,input().split())
P=sorted(list(map(int,input().split())))
Q=sorted(list(map(int,input().split())))
R=sorted(list(map(int,input().split())))
S=sorted(P[A-X:]+Q[B-Y:])
i=0
while S[i]<R[-1-i]:
    if len(R)>i or len(S)>i:
        break
    i+=1
print(sum(S[i+1:])+R[-1-i:])