Problem:


Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

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

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

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

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

Eat the 2-nd red apple.

Eat the 1-st green apple.

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

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

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

Sample Output 3

74



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


Sample output:
12

Submission status: Runtime Error

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

import heapq

p = list(map(lambda x: x * -1, p))
q = list(map(lambda x: x * -1, q))
r = list(map(lambda x: x * -1, r))
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)

P = []
Q = []
for _ in range(X):
    P.append(heapq.heappop(p)*-1)
for _ in range(Y):
    Q.append(heapq.heappop(q)*-1)

a = 1
b = 1

total = 0
now = heapq.heappop(r)*-1
while now > P[-a] or now > Q[-b]:
    if P[-a] > Q[-b]:
        total += now - Q[-b]
        b += 1
    else:
        total += now - P[-a]
        a += 1

    if len(r) == 0:
        break
    now = heapq.heappop(r)*-1

print(total + sum(P) + sum(Q))

Submission status: Runtime Error

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

sum=0
for i in range(x):
    if max(p) < max(r):
        sum+=max(r)
        r.remove(max(r))
    else:
        sum+=max(p)
        p.remove(max(p))

for i in range(y):
    if max(q) < max(r):
        sum+=max(r)
        r.remove(max(r))
    else:
        sum+=max(q)
        q.remove(max(q))

print(sum)

Submission status: Runtime Error

Solution code:
import heapq
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()))

ri=sorted(r)[::-1]
heapq.heapify(pi)
heapq.heapify(qi)

try:
    while pi[0]<ri[0] or qi[0]<ri[0]:
        if pi[0]<qi[0]:
            heapq.heappop(pi)
            heapq.heappush(pi,ri[0])
            ri.pop(0)
        else:
            heapq.heappop(qi)
            heapq.heappush(qi,ri[0])
            ri.pop(0)
except IndexError:
    pass
print(sum(pi)+sum(qi))

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 p[lsp] >= q[lsq]:
            y -= 1
            lsq -= 1
        elif p[lsp] < q[lsq]:
            x -= 1
            lsp -= 1

print(ans)

Submission status: Runtime Error

Solution code:
import math
x,y,a,b,c= map(int,input().split())
res1 = list(map(int,input().split()))
res2 = list(map(int,input().split()))
res3 = list(map(int,input().split()))
res4 = []

summ = 0
res4 = res1.copy()
res4.extend(res3)
res4.sort()
i = -1
for _ in range(x):
    summ += res4[i]
    if res4[i] not in res1 and res4[i] in res3:
        res3.remove(res4[i])
    else:
        res1.remove(res4[i])
    i -= 1
res2.extend(res3)
res2.sort()
i = -1
for _ in range(y):
    summ += res2[i]
    i -= 1
print(summ)

Submission status: Runtime Error

Solution code:
x,y,A,B,C,*a=map(int,open(0).read().split())
a,b,c=a[:A],a[A:A+B],a[A+B:]
a.sort()
b.sort()
c.sort()
a,b=a[A-x:],b[B-y:]
d,e,f=a[0],b[0],c[-1]
D=E=0
F=-1
for _ in range(min(C,x+y)):
    if f>e>=d:
        a[D]=f
        D+=1
        d=a[D]
    elif f>d>=e:
        b[E]=f
        E+=1
        e=b[F]
    elif e>f>=d:
        a[D]=f
        D+=1
        d=a[D]
    elif d>f>=e:
        b[E]=f
        E+=1
        e=b[F]
    else:break   
    F-=1
    if F!=-C-1:
        f=c[F]    
print(sum(a+b))

Submission status: Runtime Error

Solution code:
##E
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)

psum = 0

for i in range(X):
    if p[i] > r[0]:
        psum += p[i]
    else:
        psum += rsort[0]
        r.pop(0)
for i in range(Y):
    if q[i] > r[0]:
        psum += q[i]
    else:
        psum += r[0]
        r.pop(0)
print(psum)

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)

S = P[:X] + Q[:Y]
# print(S)

S_wa = []
wa = 0
for s in S[::-1]:
    wa += s
    S_wa.append(wa)
# print(S_wa)
big = S_wa[-1]
ans = S_wa[-1]

R_wa = []
wa = 0
for r in R:
    wa += r
    R_wa.append(wa)
# print(R_wa)

for i in range(len(R_wa)):
    ans = max(ans, big + R_wa[i] - S_wa[i])
print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int,input().split())
P = [int(_) for _ in input().split()]
Q = [int(_) for _ in input().split()]
R = [int(_) for _ in input().split()]

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

P = P[A-X:]
Q = Q[B-Y:]

i = 0
j = 0

while min(P[i],Q[j])<R[i+j] and i+j<C:
    if P[i] < Q[j]:
        i += 1
    else:
        j += 1
    if i+j == C:
        break

ans = 0
for e in P[i:]:
    ans = ans + e
for e in Q[j:]:
    ans = ans + e
for a in range(i+j):
    ans = ans + R[a]

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(reverse=True)

p_x = p[A-X:A]
q_y = q[B-Y:B]


ans = 0
for i in range(C):
    if p_x[0] <= q_y[0]:
        if p_x[0] >= r[i]:
            break
        ans += r[i]
        p_x.pop(0)
    elif q_y[0] < p_x[0]:
        if q_y[0] >= r[i]:
            break
        ans += r[i]
        q_y.pop(0)

print(ans+sum(q_y)+sum(p_x))

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)[::-1]
q = sorted(q)[::-1]
r = sorted(r)[::-1]

Ca = 0
Cb = 0
Cc = 0

ans = 0

while Ca + Cb + Cc != X + Y:
    if Ca == X:
        if q[Cb] > r[Cc]:
            ans = ans + q[Cb]
            Cb = Cb + 1
        else:
            ans = ans + r[Cc]
            Cc = Cc + 1
    else:
        if Cb == Y:
            if p[Ca] > r[Cc]:
                ans = ans + p[Ca]
                Ca = Ca + 1
            else:
                ans = ans + r[Cc]
                Cc = Cc + 1
        else:
            if p[Ca] >= q[Cb] and p[Ca] > r[Cc]:
                ans = ans + p[Ca]
                Ca = Ca + 1
            elif q[Cb] > p[Ca] and q[Cb] > r[Cc]:
                ans = ans + q[Cb]
                Cb = Cb + 1
            else:
                ans = ans + r[Cc]
                Cc = Cc + 1
print(ans)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
La = sorted(list(map(int, input().split())), reverse=True)
Lb = sorted(list(map(int, input().split())), reverse=True)
Lc = sorted(list(map(int, input().split())), reverse=True)

L = La[:x]
L.extend(Lb[:y])
L.sort(reverse=False)

for i in range(c):
    if L[i] < Lc[i]:
        L[i] = Lc[i]

print(sum(L))

Submission status: Runtime Error

Solution code:
from collections import deque
x0,y0,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
q.sort()
r.sort()
p = deque(p)
q = deque(q)
r = deque(r)
x,y = 0,0
red = p.pop()
green = q.pop()
mu = r.pop()
ans = 0

def newmu():
  if r:
    return r.pop()
  return 0


while x < x0 or y < y0:
  if x < x0 and y < y0:
    if red >= mu and green >= mu:
      ans += red + green
      red = p.pop()
      green = q.pop()
    elif red >= mu:
      ans += red + mu
      red = p.pop()
      mu = newmu()
    elif green >= mu:
      ans += green + mu
      green = q.pop()
      mu = newmu()
    elif r:
      r2 = r.pop()
      if red >= green and red >= r2:
        ans += mu + red
        mu = r2
        red = p.pop()
      elif green >= red and green >= r2:
        ans += mu + green
        mu = r2
        green = q.poop()
      else:
        ans += mu + r2
        mu = newmu()
    elif red >= green:
      ans += mu + red
      red = p.pop()
      newmu()
    else:
      ans += mu + green
      green = q.pop()
      newmu()
    x += 1
    y += 1
  elif x < x0:
    if red >= mu:
      ans += red
      red = p.pop()
    else:
      ans += mu
      newmu()
    x += 1
  else:
    if green >= mu:
      ans += green
      green = q.pop()
    else:
      ans += mu
      newmu()
    y += 1
print(ans)

Submission status: Runtime Error

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

P = sorted(P, reverse=True)
Q = sorted(Q, reverse=True)
R = sorted(R, reverse=True)

X_ind = X - 1
Y_ind = Y - 1
P_score = sum(P[:X])
Q_score = sum(Q[:Y])
for r in R:
    p_flag = r > P[X_ind]
    q_flag = r > Q[Y_ind]
    if p_flag and q_flag:
        if P[X_ind] < Q[Y_ind]: # paint the apple of r to be Red
            P_score = P_score + r - P[X_ind]
            X_tmp -= 1
        else:
            if P_score < Q_score: # paint the apple of r to be Red
                P_score = P_score + r - P[X_ind]
                X_ind -= 1
            else: # paint the apple of r to be blue
                Q_score = Q_score + r - Q[Y_ind]
                Y_ind -= 1
    elif p_flag and not(q_flag):
        P_score = P_score + r - P[X_ind]
        X_ind -= 1
    elif not(p_flag) and q_flag:
        Q_score = Q_score + r - Q[Y_ind]
        Y_ind -= 1
    else:
        break
        
print(P_score + Q_score)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = list(map(int, input().split(' ')))
pa = list(map(int, input().split(' ')))
qb = list(map(int, input().split(' ')))
rc = list(map(int, input().split(' ')))

pa = sorted(pa)[len(pa) - x:]
qb = sorted(qb)[len(qb) - y:]
rc = sorted(rc, reverse=True)

ai = 0
bi = 0
result = sum(pa) + sum(qb)

for r in rc:
    if bi >= b and ai >= a:
        break
    
    if bi >= b or pa[ai] <= qb[bi]:
        if r > pa[ai]:
            result = result - pa[ai] + r
            ai += 1
    elif ai >= a or pa[ai] >= qb[bi]:
        if r > qb[bi]:
            result = result - qb[bi] + r
            bi += 1
print(result)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
p = sorted(map(int, input().split()))[::-1]
q = sorted(map(int, input().split()))[::-1]
r = sorted(map(int, input().split()))[::-1]

z = 0
red, green = 0, 0
ans = 0
while True:
    finish = True
    if p[x] <= q[y]:
        if z < c and p[x] < r[z]:
            x -= 1
            z += 1
            finish = False
    else:
        if z < c and q[y] < r[z]:
            y -= 1
            z += 1
            finish = False
    if finish:
        break
print(sum(p[:x]) + sum(q[:y]) + sum(q[:z]))

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

print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ux_1 = 0
uy_1 = 0
for i in range(X):
    ux_1 += max(max(A),max(C_copy))
    if max(A) >= max(C_copy):
        A.pop(A.index(max(A)))
    else:
        C_copy.pop(C_copy.index(max(C_copy)))
for i in range(Y):
    uy_1 += max(max(B),max(C_copy))
    if max(B) >= max(C_copy):
        B.pop(B.index(max(B)))
    else:
        C_copy.pop(C_copy.index(max(C_copy)))
print(ux_1 + uy_1)

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)

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

ridx = 0
redidx = -1
greenidx = -1

for i in range(X+Y):
    if red[redidx] < green[greenidx] and abs(redidx) <= len(red):
        if red[redidx] < R[ridx]:
            ans -= red[redidx]
            ans += R[ridx]
            ridx += 1
            redidx -= 1
    elif red[redidx] > green[greenidx] and abs(greenidx) <= len(green):
        if green[greenidx] < R[ridx]:
            ans -= green[greenidx]
            ans += R[ridx]
            ridx += 1
            greenidx -= 1

    if ridx == len(R):
        break

print(ans)

Submission status: Runtime Error

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


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p = sorted(p, reverse = True)
    q = sorted(q, reverse = True)
    r = sorted(r, reverse = True)

    result = 0
    for i in range(X):
        if p[0] < r[0]:
            result += r[0]
            del r[0]
        else:
            result += p[0]
            del p[0]

    for i in range(Y):
        if q[0] < r[0]:
            result += r[0]
            del r[0]
        else:
            result += q[0]
            del q[0]
    print(result)
    return

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

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

    for i in range(Y):
        smallest_q = heapq.nsmallest(1, q)
        smallest_r = heapq.nsmallest(1, r)
        if smallest_q < smallest_r:
            temp = heapq.heappop(q) * -1
            result += temp
        else:
            temp = heapq.heappop(r) * -1
            result += temp
    #    print(temp)
    print(result)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import bisect


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

ans_p = sorted(p, reverse=True)[:x][::-1]
ans_q = sorted(q, reverse=True)[:y][::-1]
r.sort(reverse=True)

p_index = 0
q_index = 0
used_r_sum = 0

for max_r in r:
    if ans_p[p_index] < ans_q[q_index]:
        if ans_p[p_index] < max_r:
            p_index += 1
            used_r_sum += max_r
        else:
            break
    else:
        if ans_q[q_index] < max_r:
            q_index += 1
            used_r_sum += max_r
        else:
            break


print(sum(ans_p[p_index:])+sum(ans_q[q_index:])+used_r_sum)

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

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

EP=P[0:X]
EQ=Q[0:Y]

    # for i in range(X):
    #     EP.append(P[i])
        
    # for i in range(Y):
    #     EQ.append(Q[i])

EP.reverse()
EQ.reverse()

ans=sum(EP)+sum(EQ)

p=0
q=0

for i in range(C):
    if R[i]>EP[p] or R[i]>EQ[q]:
        if EP[p]<EQ[q]:
            ans+=R[i]
            ans-=EP[p]
            p+=1
        else:
            ans+=R[i]
            ans-=EQ[q]
            q+=1
    else:
        break

print(ans)

Submission status: Runtime Error

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  x,y,a,b,c=LI()
  la=LI()
  lb=LI()
  lc=LI()

  la.sort(reverse=True)
  lb.sort(reverse=True)
  lc.sort(reverse=True)

  ans_list=[]
  ans_list+=la[:x]
  ans_list+=lb[:y]

  ans_list.sort(reverse=True)
  i=1
  for _ in range(c):
    if a+b<len(lc):
      lc.pop()
    else:
      _c=lc.pop()
      if ans_list[-i]<=_c:
        ans_list[-i]=_c
        i+=1

  return sum(ans_list)

# main()
print(main())

Submission status: Runtime Error

Solution code:
X,Y,A,B,C =map(int,input().rstrip().split(" "))
RA = list(map(int,input().rstrip().split(" ")))
GA = list(map(int,input().rstrip().split(" ")))
WA = list(map(int,input().rstrip().split(" ")))
RA.sort()
RA.reverse()
GA.sort()
GA.reverse()
WA.sort()
WA.reverse()
eatRA = X
eatGA = Y
eatWA = 0
ans = 0
RAeatList = RA[:X]
GAeatList = GA[:Y]
while RAeatList[-1] < WA[0] or GAeatList[-1] < WA[0]:
  n= 1
  while RAeatList[-1 * n] == GAeatList[-1 * n] or n == min(X,Y) :
    n -= 1
  if RAeatList[-1 * n] < GAeatList[-1 * n]:
    RAeatList.pop(len(RAeatList) -1)
    RAeatList.insert(0,WA[0])
  else:
    GAeatList.pop(len(RAeatList) -1)
    GAeatList.insert(0,WA[0])
  if len(WA) == 1:
    break
  WA.pop(0)
  if eatWA == C:
    break

for i in range(X):
  ans += RAeatList[i]
for i in range(Y):
  ans += GAeatList[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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

a = p[:X]
b = q[:Y]
p_c = -1
q_c = -1
r_c = 0
k = 0

ans = sum(a)+sum(b)
while(True):
    if a[p_c] >= b[q_c]:
        if b[q_c] < r[r_c]:
            ans += ( r[r_c] - b[q_c] )
            r_c += 1
            q_c -= 1
        else:
            k = 1

    else:
        if a[p_c] < r[r_c]:
            ans += (r[r_c] - a[p_c])
            r_c += 1
            p_c -= 1
        else:
            k = 1

    if k == 1 or r_c >= C:
        break

print(ans)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C=map(int, input().split())
p=list(map(int, input().split()))
p.sort()
q=list(map(int, input().split()))
q.sort()
r=list(map(int, input().split()))
r.sort(reverse=True)

i=-X
j=-Y
k=0
deru=0
while deru=0 and k<C and (r[k]>p[i] or r[k]>q[j]):
  if p[i]>q[j] and j<0:
    j+=1
    k+=1
  elif p[i]>q[j] and j>=0 and i<0:
    i+=1
    k+=1
  elif p[i]<q[j] and i<0:
    i+=1
    k+=1
  elif p[i]<q[j] and i>=0 and j<0:
    j+=1
    k+=1
  else:
    deru=1
    
if i==0:
  a=0
else:
  a=sum(p[i:])
  
if j==0:
  b=0
else:
  b=sum(q[j:])
  
if k==0:
  c=0
else:
  c=sum(r[:k])
  
print(a+b+c)

Submission status: Runtime Error

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

def main():
     x, y, a, b, c = map(int, input().split())

     # A個の赤いりんごの美味しさ
     p = list(map(int, input().split()))
     # B個の緑のりんごの美味しさ
     q = list(map(int, input().split()))
     # C個の無色のりんごの美味しさ
     r = list(map(int, input().split()))

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

     p_cut = p[-x:]
     q_cut = q[-y:]

     # print(p_cut)
     # print(q_cut)

     # 無色を1つも使わない場合をベースにする
     red_del = sum(p_cut)
     gre_del = sum(q_cut)
     base_ans = red_del + gre_del

     red_eat = 0
     gre_eat = 0
     cle_eat = 0
     # 交換できる無色を計算
     for i in range(c):
          if p_cut[red_eat] < q_cut[gre_eat]:
               change = p_cut[red_eat]
               if change < r[cle_eat]:
                    cle_eat += 1
                    red_eat += 1
               else:
                    break
          else:
               change = q_cut[gre_eat]
               if change < r[cle_eat]:
                    cle_eat += 1
                    gre_eat += 1
               else:
                    break
     
     ans = sum(p_cut[red_eat:]) + sum(q_cut[gre_eat:]) + sum(r[:cle_eat])
     print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
x,y,a,b,c = map(int,input().split())
P = deque(sorted(list(map(int,input().split())),reverse=True)[:x])
Q = deque(sorted(list(map(int,input().split())),reverse=True)[:y])
R = deque(sorted(list(map(int,input().split())),reverse=True))
p = P.pop()
q = Q.pop()
r = R.popleft()
ans = 0
while (p < r or q < r) and r != 0:
    ans += r
    if len(R) != 0:   
        r = R.pop()
    else:
        r = 0
    if p < q:
        if len(p) != 0:   
            p = P.pop()
        else:
            p = 10**10
    else:
        if len(Q) != 0:   
            q = Q.pop()
        else:
            q = 10**10
            
ans += sum(P)+sum(Q)
if p != 10**10:
    ans += p
if q != 10**10:
    ans += 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=p[:X]
y=q[:Y]
s=sum(x)+sum(y)
sub=0
for i in range(len(r)):
    if x[-1]>y[-1]:
        t=y.pop(-1)
        if t<r[0]:
            sub=r.pop(0)-t
        else:
            break
    else:
        t=x.pop(-1)
        if t<r[0]:
            sub=r.pop(0)-t
        else:
            break

print(s+sub)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = list(map(int, input().split(' ')))
pa = list(map(int, input().split(' ')))
qb = list(map(int, input().split(' ')))
rc = list(map(int, input().split(' ')))

pa = sorted(pa)[len(pa) - x:]
qb = sorted(qb)[len(qb) - y:]
rc = sorted(rc, reverse=True)

ai = 0
bi = 0
result = sum(pa) + sum(qb)
for r in rc:
    if bi >= b or pa[ai] < qb[bi]:
        if r > pa[ai]:
            result = result - pa[ai] + r
            ai += 1
    elif ai >= a or pa[ai] > qb[bi]:
        if r > qb[bi]:
            result = result - qb[bi] + r
            bi += 1
print(result)

Submission status: Runtime Error

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()
ps = ps[-x:]
qs.sort()
qs = qs[-y:]
pqs = ps + qs
pqs.sort()
rs.sort(reverse=True)

answer = sum(pqs)
for i in range(c):
    diff = rs[i] - pqs[i]
    if diff < 0:
        break
    answer += diff

print(answer)

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = [int(x) for x in input().split()]
p = sorted([int(x) for x in input().split()], reverse=True)[:X]
q = sorted([int(x) for x in input().split()], reverse=True)[:Y]
pq = deque(sorted(p + q, reverse=True))
r = deque(sorted([int(x) for x in input().split()]))

ans = sum(pq)

for i in range(len(r)):
    if r[-1] > pq[-1]:
        ans += r.pop() - pq.pop()

print(ans)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
red = list(map(int, input().split()))
green = list(map(int, input().split()))
clear = list(map(int, input().split()))
red.sort(reverse=True)
green.sort(reverse=True)
clear.sort(reverse=True)

eat = red[0:x:] + green[0:y:]
eat.sort()
i = 0
while clear[i] > eat[0]:
    eat[0] = clear[i]
    clear[i] = 0
    eat.sort()
    i += 1
print(sum(eat))

Submission status: Runtime Error

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

red.sort()
green.sort()
no.sort()
a -= 1
b -= 1
c -= 1

ans = 0
for i in range(x):
    ans += red[a]
    a -= 1
for i in range(y):
    ans += green[b]
    b -= 1
a += 1
b += 1

for i in range(x+y):
    gr = no[c] - red[a]
    gg = no[c] - green[b]

    if gr > gg:
        if gr > 0:
            ans += no[c]
            ans -= red[a]
            c -= 1
            a += 1
    elif gg > gr:
        if gg > 0:
            ans += no[c]
            ans -= green[b]
            c -= 1
            b += 1

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 len(p) == 0:
        ans += r.pop()
        X -= 1
    if Y > 0 and len(q) == 0:
        ans += r.pop()
        Y -= 1
    if X > 0 and p[-1] >= r[-1]:
        ans += p.pop()
        X -= 1
    if Y > 0 and q[-1] >= r[-1]:
        ans += q.pop()
        Y -= 1
    if len(p) == 0:
        p.append(0)
    if len(q) == 0:
        q.append(0)
    if X <= len(p) and Y <= len(q) and 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
from collections import deque

readline = sys.stdin.readline

x,y,a,b,c=map(int,readline().split())
lst=[]
red=deque(sorted(int(i) for i in readline().split())[-x:])
green=deque(sorted(int(i) for i in readline().split())[-y:])
nc=sorted([int(i) for i in readline().split()], reverse=1)

res=0
for i in nc:
  if i>red[0]:
    red.popleft()
    res+=i
  elif i > green[0]:
    green.popleft()
    res+=i
res+=sum(red)+sum(green)
print(res)

Submission status: Runtime Error

Solution code:
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    # ここで分割を行う
    left = arr[:mid]
    right = arr[mid:]

    # 再帰的に分割を行う
    left = merge_sort(left)
    right = merge_sort(right)

    # returnが返ってきたら、結合を行い、結合したものを次に渡す
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    # ソート済み配列をマージするため、それぞれ左から見ていくだけで良い
    while l_i < len(left) and r_i < len(right):
        # ここで=をつけることで安定性を保っている
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    # 上のwhile文のどちらかがFalseになった場合終了するため、あまりをextendする
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

Red, Green, Rnum, Gnum, Nnum = map(int, input().split())
R = list(map(int, input().split()))
G = list(map(int, input().split()))
N = list(map(int, input().split()))

R = merge_sort(R)
G = merge_sort(G)
N = merge_sort(N)

yum = 0

mushoku = N[-1]
# 赤・緑を食べられるだけ食べる
for i in range(Red):
    if R[Red-1] >= mushoku:
        yum += R[Red-1]
        Red -= 1
    else:
        break;

for i in range(Green):
    if G[Green-1] >= mushoku:
        yum += G[Green-1]
        Green -= 1
    else:
        break;

# 無色込み
Invis = -1

for i in range(Red):
    if R[Red-1] >= N[Invis]:
        yum += R[Red-1]
        Red -= 1
    else:
        yum += N[Invis]
        Invis -= 1

for i in range(Green):
    if G[Green-1] >= N[Invis]:
        yum += G[Green-1]
        Green -= 1
    else:
        yum += N[Invis]
        Invis -= 1

print(yum)

Submission status: Runtime Error

Solution code:

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

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

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


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

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

Submission status: Runtime Error

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

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

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

shoku=[]

for i in range(X):
    shoku.append(p[i])

for j in range(Y):
    shoku.append(q[j])

shoku.sort()

r_num=0
for k in range(C):
    if r_num == C:
        break

    if shoku[k] < r[r_num]:
        shoku[k] = r[r_num]
        r_num+=1

print(sum(shoku))