Problem:


Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

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

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

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

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

Eat the 2-nd red apple.

Eat the 1-st green apple.

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

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

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

Sample Output 3

74



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


Sample output:
12

Submission status: Wrong Answer

Solution code:
from collections import deque
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()))
r.sort(reverse=True)
xlis = p[len(p)-x:]
ylis = q[len(q)-y:]
heapq.heapify(xlis)
heapq.heapify(ylis)
r = deque(r)
while True:
    if len(r) == 0:
        break
    xs = heapq.heappop(xlis)
    ys = heapq.heappop(ylis)
    rm = r.popleft()
    if rm < xs and rm < ys:
        heapq.heappush(xlis,xs)
        heapq.heappush(ylis,ys)
        break
    elif xs < ys and xs < rm:
        heapq.heappush(xlis,rm)
        heapq.heappush(ylis,ys)
    elif ys <= xs and ys < rm:
        heapq.heappush(ylis,rm)
        heapq.heappush(xlis,xs)
    else:
        heapq.heappush(xlis,xs)
        heapq.heappush(ylis,ys)
print(sum(xlis)+sum(ylis))

Submission status: Wrong Answer

Solution code:
import sys, re
import math
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

# -*- coding: utf-8 -*-

x, y, a, b, c = MAP()
p = LIST()
q = LIST()
r = LIST()
all = {}
all.update(dict(zip(p, ["r"] * a)))
all.update(dict(zip(q, ["g"] * b)))
all.update(dict(zip(r, ["n"] * c)))
sortAll = dict( reversed( sorted( all.items() ) ) )

xc = 0
yc = 0
count = 0
ans = 0

for value, type in sortAll.items():
    if type == "r" and xc < x:
        count += 1
        xc += 1
        ans += value
    elif type == "g" and yc < y:
        count += 1
        yc += 1
        ans += value
    elif type == "n":
        count += 1
        ans += value

    if count == x + y:
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = (int(x) for x in input().split())
list_p = list(map(int,input().split()))
list_q = list(map(int,input().split()))
list_r = list(map(int,input().split()))
list_p.reverse()
list_q.reverse()
list_r.reverse()
list_total =[]
for i in range(x):
    list_total.append(list_p[i])
for i in range(y):
    list_total.append(list_q[i])
list_total.sort()
for i in range(c):
    if list_r[0]>=list_total[0]:
        list_total[0]=list_r[0]
        list_total.sort()
        list_r.pop(0)
    else:
        break
print(sum(list_total))

Submission status: Wrong Answer

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

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

def sort(L):
  if len(L)==1:
    return L
  left = sort(L[:len(L)//2])
  right = sort(L[len(L)//2:])
  result = []
  left.append(-1)
  right.append(-1)
  left_index = 0
  right_index = 0
  while len(result)<len(L):
    if left[left_index]>=right[right_index]:
      result.append(left[left_index])
      left_index += 1
    else:
      result.append(right[right_index])
      right_index += 1
  return result

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

P_index = X-1
Q_index = Y-1
R_index = 0

while True:
  if P[P_index] <= Q[Q_index] and R[R_index]>P[P_index]:
    P[P_index] = R[R_index]
    P_index = min(0, P_index-1)
    R_index += 1
  elif P[P_index] > Q[Q_index] and R[R_index]>Q[Q_index]:
    Q[Q_index] = R[R_index]
    Q_index = min(0, Q_index-1)
    R_index += 1
  else:
    break
  if R_index>=len(R):
    break

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

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p=sorted(p)[a-x:]
q=sorted(q)[b-y:]
r=sorted(r,reverse=True)
minp_idx=0
minq_idx=0
for i in range(c):
  if minp_idx>=x and minq_idx>=y:
    break
  elif minp_idx>=x:
    if q[minq_idx]<r[i]:
      q[minq_idx]=r[i]
      minq_idx+=1
    else:
      break
  elif minq_idx>=y:
    if p[minp_idx]<r[i]:
      p[minp_idx]=r[i]
      minp_idx+=1
    else:
      break
  elif p[minp_idx]==q[minq_idx]:
    tmp=minp_idx
    tmp2=minq_idx
    smaller=False
    while tmp<x and tmp2<y:
      if p[tmp]<q[tmp2]:
        smaller=minp_idx
        minp_idx+=1
        break
      elif p[tmp]>q[tmp2]:
        smaller=minq_idx
        minq_idx+=1
        break
      else:
        tmp+=1
        tmp2+=1
    if smaller:
      if p[smaller]<r[i]:
        p[smaller]=r[i]
      else:
        break
    else:
      if x-minp_idx>y-minq_idx:
        if p[minp_idx]<r[i]:
          p[minp_idx]=r[i]
          minp_idx+=1
        else:
          break
      else:
        if q[minq_idx]<r[i]:
          q[minq_idx]=r[i]
          minq_idx+=1
        else:
          break
  elif p[minp_idx]<q[minq_idx]:
    if p[minp_idx]<r[i]:
      p[minp_idx]=r[i]
      minp_idx+=1
    else:
      break
  elif p[minp_idx]>q[minq_idx]:
    if q[minq_idx]<r[i]:
      q[minq_idx]=r[i]
      minq_idx+=1
    else:
      break
print(p)
print(q)
print(sum(p)+sum(q))

Submission status: Wrong Answer

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]

X, Y, A, B, C = mi()
p = sorted(li())[-X:]
q = sorted(li())[-Y:]
r = sorted(li())

#len_r = len(r)-1
a_p = p.pop(-1)
a_r = r.pop(-1)
cnt = 0

for i in range(X):
    if a_p >= a_r:
        cnt += a_p
        if p!=[]:
            a_p = p.pop(-1)
        else:
            break
    else:
        cnt += a_r
        if r==[]:
            #cnt += sum(p[-(X-i):]) + a_p
            a_r = 0
            #break
        else:
            a_r = r.pop(-1)
            #len_r -= 1
        

a_q = q.pop(-1)

for i in range(Y):
    if a_q >= a_r:
        cnt += a_q
        #a_q = q.pop(-1)
        if q!=[]:
            a_q = q.pop(-1)
        else:
            break
    else:
        cnt += a_r
        if r==[]:
        #if i!=(Y-1):
            #cnt += sum(q[-(Y-i):]) + a_q
            #break
            #break
            a_r = 0
        else:
            a_r = r.pop(-1)
            #len_r -= 1

print(cnt)

Submission status: Wrong Answer

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(reverse=True)
Qs.sort(reverse=True)
Rs.sort(reverse=True)

count = 0
p_count = 0
q_count = 0
r_count = 0
for i in range(X+Y):
    if Ps[0] > Qs[0] and Ps[0] > Rs[0]:
        count += Ps[0]
        Ps.pop(0)
        p_count += 1
        if p_count == X:
            Ps = [0]
    elif Qs[0] > Ps[0] and Qs[0] > Rs[0]:
        count += Qs[0]
        Qs.pop(0)
        q_count += 1
        if q_count == Y:
            Qs = [0]
    elif Rs[0] >= Ps[0] and Rs[0] >= Qs[0]:
        count += Rs[0]
        Rs.pop(0)
        r_count += 1
        if r_count == C:
            Rs = [0]

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np
X, Y, A, B, C = map(int,input().split())
Reds = list(map(int, input().split()))
Greens = list(map(int, input().split()))
Ns = list(map(int, input().split()))

Reds = np.array(Reds)
Greens = np.array(Greens)
Ns = np.array(Ns)

Reds = np.sort(Reds)[::-1]
Greens = np.sort(Greens)[::-1]
Ns = np.sort(Ns)[::-1]

sum = 0
x_min_idx=X-1
y_min_idx=Y-1

for n in Ns:
    if Reds[x_min_idx] < Greens[y_min_idx]:
        if Reds[x_min_idx] < n:
            Reds[x_min_idx]=n
            x_min_idx += -1
    else:
        if Greens[y_min_idx] < n:
            Greens[y_min_idx]=n
            y_min_idx += -1

for i in range(X):
    sum += Reds[i]
for i in range(Y):
    sum += Greens[i]
print(sum)

Submission status: Wrong Answer

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]

    exit()
    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: Wrong Answer

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)
q = sorted((int(x) for x in input().split()), reverse=True)
r = sorted((int(x) for x in input().split()), reverse=True)
ans = sum(p[:x]) + sum(q[:y])
cnt = 0
cnt_a = 0
cnt_b = 0
for cnt in range(c):
    if (r[cnt] < p[x-cnt_a-1] and r[cnt] < q[y-cnt_b-1]) or (x-cnt_a-1 < 0 and y-cnt_b-1 < 0):
        break
    if p[x-cnt_a-1] < q[max(0,y-cnt_b-1)] and x-cnt_a-1 > -1:
        ans = (ans-p[x-cnt_a-1]) + r[cnt] 
        cnt_a += 1
        # cnt+=1
    elif p[max(0, x-cnt_a-1)] > q[y-cnt_b-1] and y-cnt_b-1 > -1:
        ans = (ans-q[y-cnt_b-1]) + r[cnt] 
        cnt_b += 1
        # cnt += 1
    else:
        # determine side
        j = 0
        while p[x-cnt_a-1-j] == q[y-cnt_b-1-j] and x-cnt_a-1-j > -1 and y-cnt_b-1-j > -1:
            j += 1
        if p[x-cnt_a-1-j] < q[y-cnt_b-1-j] and x-cnt_a-1 > -1:
            ans = (ans-p[x-cnt_a-1]) + r[cnt] 
            cnt_a += 1
            # cnt+=1
        elif p[x-cnt_a-1-j] > q[y-cnt_b-1-j] and y-cnt_b-1 > -1:
            ans = (ans-q[y-cnt_b-1]) + r[cnt] 
            cnt_b += 1
            # cnt += 1
        elif x > y and x-cnt_a-1 > -1:
            ans = (ans-p[x-cnt_a-1]) + r[cnt] 
            cnt_a += 1
            # cnt+=1
        elif y-cnt_b-1 > -1:
            ans = (ans-q[y-cnt_b-1]) + r[cnt] 
            cnt_b += 1
                # cnt += 1
        else:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = map(int,input().split())
dx = list(map(int,input().split()))
dy = list(map(int,input().split()))
r = list(map(int,input().split()))
r.sort()
dx.sort()
dy.sort()
dx = dx[-x:]
dy = dy[-y:]
print(sum(dx+dy))

Submission status: Wrong Answer

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

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 or len(P) < a or len(Q) < b:
        break
    now = heapq.heappop(r) * -1

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

Submission status: Wrong Answer

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

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
result = 0
Z = 0
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
p.append(-1)
q.append(-1)
r.append(-1)

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] and r[rindex] >0:
        
            result += r[rindex]
            rindex += 1
            Z+=1

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

Submission status: Wrong Answer

Solution code:
X,Y,A,B,C=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
ip=X-1
iq=Y-1
ir=0
#ans=0
P=p[0:X]
Q=q[0:Y]
#ans+=sum(P)+sum(Q)
while True:
    if ir>=C:
        break
    #print(ir,ip,iq)
    if r[ir]<P[ip] and r[ir]<Q[iq]:
        break
    if P[ip]<Q[iq]:
        P[ip]=r[ir]
        #ans+=r[ir]-P[ip]
        if ip!=0:
            ip-=1
    else:
        Q[iq]=r[ir]
        #ans+=r[ir]-Q[iq]
        if iq!=0:
            iq-=1
    ir+=1
    print(P,Q)
print(sum(P)+sum(Q))

Submission status: Wrong Answer

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

p.sort(reverse=True)
p = p[:x]
q.sort(reverse=True)
q = q[:y]
r.sort(reverse=True)

reds = deque(p)
greens = deque(q)
uncolor = deque(r)

# できるだけ値が大きいやつを使いたい
ans = 0
red_cnt = 0
green_cnt = 0

while True:
    # print(red_cnt, green_cnt, reds, greens, uncolor)
    if red_cnt == x and green_cnt == y:
        break

    # 赤の処理をするケース
    # 緑が終わってる、赤が緑より大きい
    if len(greens) == 0:
        if len(uncolor) > 0:
            if reds[0] >= uncolor[0]:
                eat = reds.popleft()
            else:
                eat = uncolor.popleft()
        else:
            eat = reds.popleft()
        ans += eat
        red_cnt += 1
        continue

    if len(reds) == 0:
        if len(uncolor) > 0:
            if greens[0] >= uncolor[0]:
                eat = greens.popleft()
            else:
                eat = uncolor.popleft()
        else:
            eat = greens.popleft()
        ans += eat
        green_cnt += 1
        continue

    if len(uncolor) == 0:
        for i in range(x - red_cnt):
            eat = reds.popleft()
            ans += eat
        for i in range(y - green_cnt):
            eat = greens.popleft()
            ans += eat
        red_cnt = x
        green_cnt = y
        continue

    if red_cnt < x and green_cnt < y:
        if reds[0] > greens[0] and reds[0] >= uncolor[0]:
            eat = reds.popleft()
            ans += eat
            red_cnt += 1
        elif reds[0] < greens[0] and greens[0] >= uncolor[0]:
            eat = greens.popleft()
            ans += eat
            green_cnt += 1
        elif reds[0] == greens[0]:
            eat = greens.popleft()
            ans += eat
            green_cnt += 1
        elif reds[0] > greens[0]:
            if greens[0] >= uncolor[0]:
                eat = greens.popleft()
            else:
                eat = uncolor.popleft()
            ans += eat
            green_cnt += 1
        else:
            if reds[0] >= uncolor[0]:
                eat = reds.popleft()
            else:
                eat = uncolor.popleft()
            ans += eat
            red_cnt += 1
    elif red_cnt < x:
        if reds[0] >= uncolor[0]:
            eat = reds.popleft()
        else:
            eat = uncolor.popleft()
        ans += eat
        red_cnt += 1
    else:
        if greens[0] >= uncolor[0]:
            eat = greens.popleft()
        else:
            eat = uncolor.popleft()
        ans += eat
        green_cnt += 1

print(ans)

Submission status: Wrong Answer

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

tmp = list(map(int, input().split()))
tmp.sort(reverse=True)
red = [1e+6]
red.extend(tmp)

tmp = list(map(int, input().split()))
tmp.sort(reverse=True)
green = [1e+6]
green.extend(tmp)
none = list(map(int, input().split()))
none.sort(reverse=True)

n_red = x
n_green = y
n_none = 0


while(True):
  if(red[n_red] >= green[n_green]):
    if(green[n_green] <= none[n_none]):
      n_green -= 1
      n_none += 1
    else:
      break
  else: # redの最小の方が小さい時
    if(red[n_red] <= none[n_none]):
      n_red -= 1
      n_none += 1
    else:
      break
  if(n_none == len(none)):
    break
#
print(int(sum(red[:n_red+1])
          +sum(green[:n_green+1])
          +sum(none[:n_none])
          - 2e+6))

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = map(int,input().split())
p = input().split()
p = [int(s) for s in p]
q = input().split()
q = [int(s) for s in q]
r = input().split()
r = [int(s) for s in r]
reds = sorted(p,reverse=True)
reds = reds[:x]
greens = sorted(q,reverse=True)
greens = greens[:y]
allints = sorted(reds + greens)
minofall = allints[0]
r = [i for i in r if i > minofall]
del allints[:len(r)]
print(sum(allints+r))

Submission status: Wrong Answer

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

P.extend(Q)
P.extend(R)

P.sort()
a=-(X+Y)
print(sum(P[a:]))

Submission status: Wrong Answer

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

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(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

flg = lst_r > lst_g

cur = -1 * heappop(lst_n)
while len(lst_n)>0:
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
    else:
        break
print(sum(lst_g)+sum(lst_r))

Submission status: Wrong Answer

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

ps = sorted(ps)
qs = sorted(qs)
rs = sorted(rs)

p_sum = sum(ps)
q_sum = sum(qs)

score = 0
while True:
    if x == 0 and y == 0:
        break
    if len(rs) == 0:
        # 無色のりんごは残っていない
        if x > 0:
            p = ps[-1]
            ps.pop(-1)
            score += p
            x -= 1
            p_sum -= p
        if y > 0:
            q = qs[-1]
            qs.pop(-1)
            score += q
            y -= 1
            q_sum -= q

    else:
        if x > 0 and y == 0:
            # 緑は残っていない → 赤か無色を食べる
            p = ps[-1]
            r = rs[-1]
            x -= 1
            if r > p:
                rs.pop(-1)
                score += r
            else:
                ps.pop(-1)
                score += p
                p_sum -= p
        elif y > 0 and x == 0:
            # 赤は残っていない → 緑か無色を食べる
            q = qs[-1]
            r = rs[-1]
            y -= 1
            if r > q:
                rs.pop(-1)
                score += r
            else:
                qs.pop(-1)
                score += q
                q_sum -= q
        elif x > 0 and y > 0:
            # 赤も緑も無色もある。どれを食べるか。
            p = ps[-1]
            q = qs[-1]
            r = rs[-1]
            if r >= p and r >= q:
                # 無色を食べたほうが良い。残りの美味しさをなるべく多く残すほうが良い。
                rs.pop(-1)
                score += r
                if p_sum < q_sum:
                    # 赤にして食べる
                    x -= 1
                else:
                    # 緑にして食べる
                    y -= 1
            elif p > r:
                ps.pop(-1)
                score += p
                x -= 1
                p_sum -= p
            elif q > r:
                qs.pop(-1)
                score += q
                y -= 1
                q_sum -= q

print(score)

Submission status: Wrong Answer

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

pp=0
pq=0
pr=0
s=0
while x+y > 0:
    if x>0 and y>0 and pr < c:
        if p[pp]>=q[pq]:
            if p[pp]>r[pr]:
                s+=p[pp]
                pp+=1
                x-=1
            else:
                s+=r[pr]
                pr+=1
                x-=1
        else:
            if q[pq]>r[pr]:
                s+=q[pq]
                pq+=1
                y-=1
            else:
                s+=r[pr]
                pr+=1
                y-=1
    elif x>0 and y==0 and pr < c:
        if p[pp]>r[pr]:
            s+=p[pp]
            pp+=1
            x-=1
        else:
            s+=r[pr]
            pr+=1
            x-=1
    elif x==0 and y>0 and pr < c:
        if q[pq]>r[pr]:
            s+=q[pq]
            pq+=1
            y-=1
        else:
            s+=r[pr]
            pr+=1
            y-=1
    elif x>0 and y==0 and pr == c:
        s+=p[pp]
        pp+=1
        x-=1
    elif x==0 and y>0 and pr == c:
        s+=q[pq]
        pq+=1
        y-=1

print(s)

Submission status: Wrong Answer

Solution code:
import heapq

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

p = sorted(map(int, input().split()), reverse=True)
q = sorted(map(int, input().split()), reverse=True)
r = sorted(map(int, input().split()), reverse=False)

red = p[:X]
green = q[:Y]
nocolor = r


heapq.heapify(red)
heapq.heapify(green)
# heapq.heapify(nocolor)

for nc in nocolor:
    R = red[0]
    G = green[0]
    if R > nc and G > nc:
        break
    if nc > R and nc > G:
        if R > G:
            q = heapq.heappop(green)
            heapq.heappush(green,nc)
        else:
            q = heapq.heappop(red)
            heapq.heappush(red,nc)
    if nc > R:
        q = heapq.heappop(red)
        heapq.heappush(red,nc)
    if nc > G:
        q = heapq.heappop(green)
        heapq.heappush(green,nc)


print(sum(red)+sum(green))

Submission status: Wrong Answer

Solution code:
from collections import deque
X, Y, A, B, C = map(int,input().split())
p = sorted(list(map(int,input().split())))[::-1]
q = sorted(list(map(int,input().split())))[::-1]
r = sorted(list(map(int,input().split())))[::-1]
p = deque(p)
q = deque(q)
r = deque(r)
ans = 0

while len(p) > 0 and X > 0 and p[0] > r[0]:
    ans += p.popleft()
    X -= 1
while len(q) > 0 and Y > 0 and q[0] > r[0]:
    ans += q.popleft()
    Y -= 1

while X > 0 or Y > 0:
    if X > 0 and Y > 0 and len(r) > 0 and p[0] < q[0]:
        ans += r.popleft()
        X -= 1
    elif X > 0 and Y > 0 and len(r) > 0 and p[0] > q[0]:
        ans += r.popleft()
        Y -= 1
    elif X == 0 and Y > 0:
        if len(r) == 0:
            ans += q.popleft()
            Y -= 1
        elif r[0] > q[0]:
            ans += r.popleft()
            Y -= 1
        else:
            ans += q.popleft()
            Y-= 1
    else:
        if len(r) == 0:
            ans += p.popleft()
            X -= 1
        elif r[0] > p[0]:
            ans += r.popleft()
            X -= 1
        else:
            ans += p.popleft()
            X -= 1
print(ans)

Submission status: Wrong Answer

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
while leftX != 0 or leftY != 0:
    if leftX != 0:
        if len(r) != 0 and p[-1] < r[-1]:
            result += r.pop()
        else:
            result += p.pop()
        leftX -= 1
    if leftY != 0:
        if len(r) != 0 and q[-1] < r[-1]:
            result += r.pop()
        else:
            result += q.pop()
        leftY -= 1

print(result)

Submission status: Wrong Answer

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()]
p = sorted(p)
q = sorted(q)
r = sorted(r)
z = 0
answer = 0
for i in range(x+y):
    if x > 0 and y > 0:
        t = min(p[a-x],q[b-y])
        if p[a-x] >= q[b-y]:
            y -= 1
        else:
            x -= 1
    elif x == 0 and y > 0:
        t = q[b-y]
        y -= 1
    else:
        t = p[a-x]
        x -= 1
    if z < c:
        t = max(t,r[c-z-1])
        if t >= r[z]:
            z += 1
    answer += t
print(answer)

Submission status: Wrong Answer

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(x+y):
    for j in range(len(r)):
        if pss[i] < r[j]:
            pss[i] = r[j]
            #print("pop")
            #print(r)
            break
    #print(he)
    he += pss[i]

'''
for i in range(x):
    for j in range(len(r)):
        if ps[i] < r[j]:
            ps[i] = r.pop(j)
            #print("pop")
            #print(r)
            break
    #print(he)
    he += ps[i]

for i in range(y):
    for j in range(len(r)):
        if qs[i] < r[j]:
            qs[i] = r.pop(j)
            #print("pop")
            #print(r)
            break
    #print(he)
    he += qs[i]
'''
print(he)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)
sum = 0
for i in range(x):
    sum += p[i]
for j in range(y):
    sum += q[j]
x -= 1
y -= 1
j = 1
for i in range(c):
    if x > 0 and y > 0:
        if p[x] >= q[y]:
            if q[y] < r[i]:
                sum += r[i] - q[y]
                y -= 1
            else:
                j = 0
        else:
            if p[x] < r[i]:
                sum += r[i] - p[x]
                x -= 1
            else:
                j = 0
    elif x > 0:
        if p[x] < r[i]:
            sum + r[i] - p[x]
            x -= 1
        else:
            j = 0
    elif y > 0:
        if q[y] < r[i]:
            sum += r[i] - q[y]
            y -= 1
        else:
            j = 0
    else:
        j = 0
    if j == 0:
        break
print(sum)

Submission status: Wrong Answer

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

ans = sorted(p[A-X:] + q[B-Y:])
ans_sum = [ans[0]]
for i in range(1, X+Y):
    ans_sum.append(ans_sum[i-1] + ans[i])

r_sum = [r[-1]]
for i in range(1, C):
    r_sum.append(r_sum[i-1] + r[-i-1])

index = -1
for i in range(min(C, X+Y)):
    if r_sum[i] < ans_sum[i]:
        index = i
        break

if index == 0:
    print(ans_sum[-1])
elif index == -1 and C > X+Y:
    print(r_sum[X+Y-1])
else:
    if index == -1:
        index = C
    print(ans_sum[-1] + (r_sum[index-1] - ans_sum[index-1]))

Submission status: Wrong Answer

Solution code:
from pprint import pprint
from bisect import bisect_left
import numpy


def read():
    X, Y, A, B, C = list(map(int, input().strip().split()))
    P = list(map(int, input().strip().split()))
    Q = list(map(int, input().strip().split()))
    R = list(map(int, input().strip().split()))
    return X, Y, A, B, C, P, Q, R

def argsort(x, reverse=False):
    return sorted(range(len(x)), key=lambda k: x[k], reverse=reverse)

def solve(X, Y, A, B, C, P, Q, R):
    P = list(sorted(P, reverse=True))
    Q = list(sorted(Q, reverse=True))
    R = list(sorted(R, reverse=True))

    i = X
    j = Y
    score = sum(P[0:i]) + sum(Q[0:j])
    for k, r in enumerate(R):
        if i > 0 and j > 0:
            if P[i-1] < Q[j-1] and P[i-1] < r:
                score = score - P[i-1] + r
                i -= 1
            elif Q[j-1] < P[i-1] and Q[j-1] < r:
                score = score - Q[j-1] + r
                j -= 1
            else:
                break
        elif i > 0:
            if P[i-1] < Q[j-1] and P[i-1] < r:
                score = score - P[i-1] + r
                i -= 1
            else:
                break
        elif j > 0:
            if Q[j-1] < P[i-1] and Q[j-1] < r:
                score = score - Q[j-1] + r
                j -= 1
            else:
                break
        else:
            break
    return score

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Submission status: Wrong Answer

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

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

ans = sum(p[:x]) + sum(q[:y])

i, j = 0, 0
while i < x and j < y and i + j < c:
    change = min(p[x - 1 - i], q[y - 1 - j])
    if r[i + j] > change:
        ans += r[i + j] - change
    else:
        break
    if p[x - 1 - i] < q[y - 1 - j]:
        i += 1
    else:
        j += 1

print(ans)