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:
import collections
def main():

    X, Y, A, B, C = map(int, input().split())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    r = list(map(int, input().split()))
    p.sort(reverse = True)
    q.sort(reverse = True)
    r.sort(reverse = True)
    countA = 0
    countB = 0
    countC = 0
    ans = 0
    i = 0
    j = 0
    k = 0
    while countA < X and countB < Y and countC < min(C, X+Y):
        v = max([p[i], q[j], r[k]])
        if p[i] != v and q[j] != v:
            ans += r[k]
            countC += 1
            k += 1
        elif p[i] == v:
            ans += p[i]
            countA += 1
            i += 1
        elif q[j] == v:
            ans += q[j]
            countB += 1
            j += 1

    if countC == X+Y:
        return ans

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

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

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



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

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())))[-x:]
q=sorted(list(map(int,input().split())))[-y:]
s=sorted(p+q)
r=sorted(list(map(int,input().split())))
ans=sum(s)
j=0
for i in range(len(r)):
  if s[j]<r[i]:
    ans+=(-s[j]+r[i])
    j+=1
    break
  if j>=len(s):
    break
print(ans)

Submission status: Wrong Answer

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


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


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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.q = l
        heapq.heapify(self.q)
        return

    def push(self, n):
        heapq.heappush(self.q, n)
        return

    def pop(self):
        return heapq.heappop(self.q)


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    X, Y, A, B, C = Scanner.map_int()
    P = Scanner.map_int()
    Q = Scanner.map_int()
    R = Scanner.map_int()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    P = P[:X]
    Q = Q[:Y]
    ans = 0
    p, q, r = 0, 0, 0
    while X > 0:
        if r <= len(R) and R[r] > P[p]:
            ans += R[r]
            r += 1
        else:
            ans += P[p]
            p += 1
        X -= 1
    while Y > 0:
        if r < len(R) and R[r] > Q[q]:
            ans += R[r]
            r += 1
        else:
            ans += Q[q]
            q += 1
        Y -= 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().split())
*P, = map(int, input().split())
*Q, = map(int, input().split())
*R, = map(int, input().split())
P = sorted(P, reverse=True)[:X]
Q = sorted(Q, reverse=True)[:-Y]
print(sum(sorted(P + Q + R, reverse=True)[:-(X + Y)]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import heapq
import sys
input = sys.stdin.readline

X, Y, A, B, C = map(int, input().rstrip().split())
a = list(map(int, input().rstrip().split()))
b = sorted(list(map(int, input().rstrip().split())))[::-1] + [-1]
c = sorted(list(map(int, input().rstrip().split())))[::-1] + [-1]

color = []
bc = []
ib = 0
ic = 0
for i in range(B + C): 
    if b[ib] > c[ic]:
        bc.append(b[ib])
        color.append(0)
        ib += 1
    else:
        bc.append(c[ic])
        color.append(1)
        ic += 1
bc += [-1]
color += [0]
  
for i in range(Y, len(bc)):
    if color[i] == 1:
        a.append(bc[i])
a = sorted(a, key=lambda x: -x)
a = a[:X]
heapq.heapify(a)

ib = Y
total = 0
for i in range(Y):
    if color[Y-i-1] == 0:
        total += bc[Y-i-1]
    else: # 透明の場合
        if a[0] <= bc[Y-i-1]:
            if a[0] <= bc[ib]: # 無色を移動
                total += bc[ib]
                heapq.heappush(a, bc[Y-i-1])
                heapq.heappop(a)
                ib += 1
                while color[ib] == 1: # 範囲外の無色は使用不可
                    ib += 1
            else:
                total += bc[Y-i-1]
        else:
            total += bc[Y-i-1]
 
print(total + sum(a))

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

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

print(sum(l[:x+y]))

Submission status: Wrong Answer

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

while X > 0 or Y > 0:
  if len(p) != 0:
    Pmax = p[0]
  else:
    Pmax = 0

  if len(q) != 0:
    Qmax = q[0]
  else:
    Qmax = 0

  if len(r) != 0:
    Rmax = r[0]
  else:
    Rmax = 0  

  if Pmax >= Qmax and Pmax >= Rmax :
    if X > 0:
      ans = ans + Pmax
      X = X-1
    del p[0]


  elif Qmax >= Rmax  :
    if Y > 0:
      ans = ans + Qmax
      Y = Y-1
    del q[0]

  else:
    if X == 0 or Y == 0:
      if X == 0:
        ans = ans + Rmax
        Y = Y-1
      else :
        ans = ans + Rmax
        X = X-1
    
    else:

      if Pmax >= Qmax :
        ans = ans + Rmax
        Y = Y-1
      else:
        ans = ans + Rmax
        X = X-1
    del r[0]
      

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 22:06:13 2020

@author: Kanaru Sato
"""

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 = []
Q = []
R = []

for i in range(a):
    P.append([P_[i],1])
for i in range(b):
    Q.append([Q_[i],2])
for i in range(c):
    R.append([R_[i],3])

p = P+R
p.sort(reverse=True, key=lambda x: x[0])
q = Q+R
q.sort(reverse=True, key=lambda x: x[0])
##########
pr = 0
for i in range(x):
    if p[i][1] == 3:
        pr += 1

qr = 0
for i in range(y):
    if q[i][1] == 3:
        qr += 1
        
n = min(pr,qr)

############

prest = p[x:len(p)+1]
qrest = q[y:len(q)+1]
rest = prest+qrest
rest.sort(reverse=True, key=lambda x: x[0])

diff = abs(pr-qr)

sum = 0
sumcount = 0
rcount = 0
while sumcount <= n-1:
    if rest[i][1] == 3:
        rcount += 1
        if rcount > diff:
            sum += rest[i][0]
            sumcount += 1
    else:
        sum += rest[i][0]
        sumcount += 1

rsum = 0
if pr <= qr:
    for i in range(x):
        if p[i][1] == 3:
            rsum += p[i][0]
else:
    for i in range(y):
        if q[i][1] == 3:
            rsum += q[i][0]

psum = 0
for i in range(x):
    psum += p[i][0]
qsum = 0
for i in range(y):
    qsum += q[i][0]

ans = psum + qsum -rsum + sum

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline=sys.stdin.buffer.readline
X,Y,A,B,C = map(int, readline().rstrip().split())
arr_a = readline().rstrip().decode().split()
arr_b = readline().rstrip().decode().split()
arr_c = readline().rstrip().decode().split()
s_arr_a = sorted(arr_a,reverse=True)
s_arr_b = sorted(arr_b,reverse=True)
s_arr_c = sorted(arr_c,reverse=True)
s2_arr_a = s_arr_a[:] #.copy()
s2_arr_b = s_arr_b[:] #.copy()
s2_arr_c = s_arr_c[:] #.copy()
ans1 = 0
ans2 = 0
for i in range(X):
  if len(s_arr_a) > 0 and len(s_arr_c) > 0:
    if int(s_arr_a[0]) > int(s_arr_c[0]):
      ans1+=int(s_arr_a.pop(0))
    else:
      ans1+=int(s_arr_c.pop(0))
  elif len(s_arr_a) > 0:
      ans1+=int(s_arr_a.pop(0))
  elif len(s_arr_c) > 0:
      ans1+=int(s_arr_c.pop(0))
for i in range(Y):
  if len(s_arr_b) > 0 and len(s_arr_c) > 0:
    if int(s_arr_b[0]) > int(s_arr_c[0]):
      ans1+=int(s_arr_b.pop(0))
    else:
      ans1+=int(s_arr_c.pop(0))
  elif len(s_arr_b) > 0:
      ans1+=int(s_arr_b.pop(0))
  elif len(s_arr_c) > 0:
      ans1+=int(s_arr_c.pop(0))

for i in range(Y):
  if len(s2_arr_b) > 0 and len(s2_arr_c) > 0:
    if int(s2_arr_b[0]) > int(s2_arr_c[0]):
      ans2+=int(s2_arr_b.pop(0))
    else:
      ans2+=int(s2_arr_c.pop(0))
  elif len(s2_arr_b) > 0:
      ans2+=int(s2_arr_b.pop(0))
  elif len(s2_arr_c) > 0:
      ans2+=int(s2_arr_c.pop(0))

for i in range(X):
  if len(s2_arr_a) > 0 and len(s2_arr_c) > 0:
    if int(s2_arr_a[0]) > int(s2_arr_c[0]):
      ans2+=int(s2_arr_a.pop(0))
    else:
      ans2+=int(s2_arr_c.pop(0))
  elif len(s2_arr_a) > 0:
      ans2+=int(s2_arr_a.pop(0))
  elif len(s2_arr_c) > 0:
      ans2+=int(s2_arr_c.pop(0))
if ans1 >= ans2:
  print(ans1)
else:
  print(ans2)

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

if y>x:
  z=x
  x=y
  y=z
  o=p
  p=q
  q=o
for i in range(y):
    if p[0]>=q[0]:
      if q[0]>=r[0]:
        ans+=p[0]
        del p[0]
        ans+=q[0]
        del q[0]
      else:       
        ans+=p[0]
        del p[0]
        ans+=r[0]
        del r[0]    
    elif p[0]<=r[0]:       
      ans+=q[0]
      del q[0]
      ans+=r[0]
      del r[0]
    else:
        ans+=p[0]
        del p[0]
        ans+=q[0]
        del q[0]
      
for i in range(y,x):
    if p[0]>=r[0]:
      ans+=p[0]
      del p[0]
    else:
      ans+=r[0]
      del r[0]

      
print(ans)

Submission status: Wrong Answer

Solution code:
def LI():
	return [ int(s) for s in input().split() ]
  
X,Y,A,B,C = LI()
if X > Y:
    X,Y = Y,X
    A,B = B,A
    y = LI()
    x = LI()
    c = LI()
else:
    x = LI()
    y = LI()
    c = LI()
    
x = sorted(x,reverse = True)
y = sorted(y,reverse = True)
c = sorted(c,reverse = True)

x_count = 0
y_count = 0
c_count = 0

sweet = 0

for i in range(1, Y+1):
    if i <= X:
        if c_count < C:
            if x[x_count] > y[y_count] and y[y_count] < c[c_count]:
                sweet += (x[x_count] + c[c_count])
                x_count += 1
                c_count += 1
            elif x[x_count] <= y[y_count] and x[x_count] < c[c_count]:
                sweet += (y[y_count] + c[c_count])
                y_count += 1
                c_count+=1
            else:
                sweet+=(x[x_count] + y[y_count])
                x_count+=1
                y_count+=1
        else:
            sweet+=(x[x_count] + y[y_count])
            x_count+=1
            y_count+=1
    else:
        if c_count < C:
            if y[y_count] < c[c_count]:
                sweet+=c[c_count]
                c_count+=1
            else:
                sweet+=y[y_count]
                y_count+=1
        else:
            sweet+=y[y_count]
            y_count+=1
print(sweet)

Submission status: Wrong Answer

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

p = p[-X:]
q = q[-X:]

p.extend(q)
p.extend(r)

p.sort()

print(sum(p[-(X+Y):]))

Submission status: Wrong Answer

Solution code:
def main():
    x, y, a, b, c = map(int, input().split())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    r = list(map(int, input().split()))
    p.sort()
    q.sort()
    for i in range(-1, -x - 1, -1):
        r.append(p[i])
    for i in range(-1, -y - 1, -1):
        r.append(q[i])
    r.sort(reverse=True)
    print(sum(r[x + y:]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

r = []
g = []
nc = []
for p in P:
    heapq.heappush(r, -p)

for q in Q:
    heapq.heappush(g, -q)

for i in R:
    heapq.heappush(nc, -i)

ans = 0
#print(r,g,nc)
while X > 0 or Y > 0:
    if len(nc) > 0:
        if X > 0 and Y > 0:
            if r[0] <= nc[0]:
                ans += -1 * r[0]
                heapq.heappop(r)
                X -= 1
            elif g[0] <= nc[0]:
                ans += -1 * g[0]
                heapq.heappop(g)
                Y -= 1
            else:
                if r[0] >= g[0]:
                    ans += -1 * nc[0]
                    heapq.heappop(nc)
                    X -= 1
                else:
                    ans += -1 * nc[0]
                    heapq.heappop(nc)
                    Y -= 1
        elif X > 0:
            if r[0] <= nc[0]:
                ans += -1 * r[0]
                heapq.heappop(r)
                X -= 1
            else:
                ans += -1 * nc[0]
                heapq.heappop(nc)
                X -= 1
        else:
            if g[0] <= nc[0]:
                ans += -1 * g[0]
                heapq.heappop(g)
                Y -= 1
            else:
                ans += -1 * nc[0]
                heapq.heappop(nc)
                Y -= 1

    else:
        if X > 0 and Y > 0:
            if r[0] < g[0]:
                ans += -1 * r[0]
                heapq.heappop(r)
                X -= 1
            else:
                ans += -1 * g[0]
                heapq.heappop(g)
                Y -= 1

        elif X > 0:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1
        else:
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1


print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import heapq
import sys
input = sys.stdin.readline

X, Y, A, B, C = map(int, input().rstrip().split())
a = list(map(int, input().rstrip().split()))
b = sorted(list(map(int, input().rstrip().split())))[::-1] + [-1]
c = sorted(list(map(int, input().rstrip().split())))[::-1] + [-1]

color = []
bc = []
ib = 0
ic = 0
for i in range(B + C): 
    if b[ib] > c[ic]:
        bc.append(b[ib])
        color.append(0)
        ib += 1
    else:
        bc.append(c[ic])
        color.append(1)
        ic += 1
bc += [-1]
color += [0]

        
for i in range(Y, len(bc)):
    if color[i] == 1:
        a.append(bc[i])
a = sorted(a)
a = a[-X:]
heapq.heapify(a)

ib = Y
total = 0
for i in range(Y):
    if color[Y-i-1] == 0:
        total += bc[Y-i-1]
    else: # 透明の場合
        if a[0] < bc[Y-i-1]:
            if a[0] < bc[ib]: # 無色を移動
                total += bc[ib]
                #bisect.insort(a, -bc[Y-i-1])
                heapq.heappush(a, bc[Y-i-1])
                heapq.heappop(a)
                ib += 1
                while color[ib] == 1: # 範囲外の無色は使用不可
                    ib += 1
            else:
                total += bc[Y-i-1]
        else:
            total += bc[Y-i-1]
 
print(total + sum(a))

Submission status: Wrong Answer

Solution code:
import heapq as hq

x,y,a,b,c=map(int,input().split())
R=list(map(int,input().split()))
R=list(map(lambda x: x*(-1), R))
G=list(map(int,input().split()))
G=list(map(lambda x: x*(-1), G))
N=list(map(int,input().split()))
N=list(map(lambda x: x*(-1), N))
hq.heapify(R)
hq.heapify(G)
hq.heapify(N)
r=-hq.heappop(R)
g=-hq.heappop(G)
n=-hq.heappop(N)

ans=0
now=x+y
while now:
    if r<=n and g<=n and c>0:
        ans+=n
        c-=1
        if N:
            n=-hq.heappop(N)
        else:
            n=0
    elif (r<=g and y>0) or x==0:
        ans+=g
        y-=1
        if G:
            g=-hq.heappop(G)
        else:
            g=0
    else:
        ans+=r
        x-=1
        if R:
            r=-hq.heappop(R)
        else:
            r=0
    now-=1
print(ans)

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]


sum_a = x_a + x_b + s_c
sum_a.sort(reverse = True)

print(sum(sum_a))

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 (x-1-cnt_a) < 0 and  y-1-cnt_b < 0:
        break
    pa = max(0, x-1-cnt_a)
    pb = max(0, y-1-cnt_b)
    if r[cnt] < p[pa] and r[cnt] < q[pb]:
        break
    if p[pa] > q[pb]:
        ans  = ans - q[pb] + r[cnt]
        cnt_b += 1
        q[pb] = r[cnt]
    elif p[pa] < q[pb]:
        ans = ans - p[pa] + r[cnt]
        cnt_a += 1
        q[pa] = r[cnt]
    else:
        j = 0
        while p[pa-j] == q[pb-j] and pa - j > -1 and pb - j > -1:
            j += 1
        if p[pa-j] > q[pb-j]:
            ans  = ans - q[pb] + r[cnt]
            cnt_b += 1
            q[pb] = r[cnt]
        elif p[pa-j] < q[pb-j]:
            ans = ans - p[pa] + r[cnt]
            cnt_a += 1
            q[pa] = r[cnt]
        elif x > y and x-1-cnt_a >= 0:
            ans = ans - p[pa] + r[cnt]
            cnt_a += 1
            q[pa] = r[cnt]
        elif y-1-cnt_b >= 0:
            ans  = ans - q[pb] + r[cnt]
            cnt_b += 1
            q[pb] = r[cnt]
        elif x-1-cnt_a >= 0:
            ans = ans - p[pa] + r[cnt]
            cnt_a += 1
            q[pa] = r[cnt]
        else:
            break
    
print(ans)

Submission status: Wrong Answer

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

if __name__ == "__main__":
    X, Y, A, B, C = MI()
    p = LI()
    p.reverse()
    q = LI()
    q.reverse()
    pq = p[:X] + q[:Y] + LI()
    pq.reverse()
    print(sum(pq[:X+Y]))

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)
p = p[:x]
q = q[:y]
result = sum(p+q)
p = [10**9+1]+p
q = [10**9+1]+q

ii, jj = x+y, c
for i in range(c):
    if p[x]<r[c-1]:
        result += r[c-1]-p[x]
        c -= 1
        x -= 1
    elif q[y]<r[c-1]:
        result += r[c-1]-q[y]
        c -= 1
        y -= 1
    else:
        break
    if c<0:
        break
print(result)

Submission status: Wrong Answer

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

if __name__ == "__main__":
    X, Y, A, B, C = MI()
    p = LI()
    p.reverse()
    q = LI()
    q.reverse()
    pq = p[:X] + q[:Y] + LI()
    pq.sort()
    print(sum(pq[:X+Y]))

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()
p = p[:x]
q = q[:y]
result = sum(p+q)
p = [10**9+1]+p
q = [10**9+1]+q

ii, jj = x+y, c
for i in range(c):
    if p[x]<r[c-1]:
        result += r[c-1]-p[x]
        c -= 1
        x -= 1
    elif q[y]<r[c-1]:
        result += r[c-1]-q[y]
        c -= 1
        y -= 1
    else:
        break
    if c<0:
        break
print(result)

Submission status: Wrong Answer

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

if __name__ == "__main__":
    X, Y, A, B, C = MI()
    p = LI()
    p.reverse()
    q = LI()
    q.reverse()
    pq = p[:X] + q[:Y] + LI()
    print(sum(pq[:X+Y]))

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)

A_p = p[:X]
B_q = q[:Y]
C_r = r

ans = A_p + B_q + C_r
ans.sort(reverse=True)
sum(ans[: X+Y])

Submission status: Wrong Answer

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

i = 0
ans = 0
for a in l:
    if i < C and r[i] <= a:
        ans += a
    elif i < C and r[i] > a:
        ans += r[i]
        i += 1
    else:
        ans += a

print(ans)

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

Submission status: Wrong Answer

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

r = []
g = []
nc = []
for p in P:
    heapq.heappush(r, -p)

for q in Q:
    heapq.heappush(g, -q)

for i in R:
    heapq.heappush(nc, -i)

ans = 0
#print(r,g,nc)
while X > 0 or Y > 0:
    if len(nc) > 0:
        if X > 0 and Y > 0:
            if r[0] <= nc[0]:
                ans += -1 * r[0]
                heapq.heappop(r)
                X -= 1
            elif g[0] <= nc[0]:
                ans += -1 * g[0]
                heapq.heappop(g)
                Y -= 1
            else:
                if r[0] >= g[0]:
                    ans += -1 * nc[0]
                    heapq.heappop(nc)
                    X -= 1
                else:
                    ans += -1 * nc[0]
                    heapq.heappop(nc)
                    Y -= 1
        elif X > 0:
            if r[0] < nc[0]:
                ans += -1 * r[0]
                heapq.heappop(r)
                X -= 1
            else:
                ans += -1 * nc[0]
                heapq.heappop(nc)
                X -= 1
        else:
            if g[0] < nc[0]:
                ans += -1 * g[0]
                heapq.heappop(g)
                Y -= 1
            else:
                ans += -1 * nc[0]
                heapq.heappop(nc)
                Y -= 1

    else:
        if X > 0 and Y > 0:
            if r[0] < g[0]:
                ans += -1 * r[0]
                heapq.heappop(r)
                X -= 1
            else:
                ans += -1 * g[0]
                heapq.heappop(g)
                Y -= 1

        elif X > 0:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1
        else:
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1


print(ans)

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 (x-1-cnt_a) < 0 and  y-1-cnt_b < 0:
        break
    pa = max(0, x-1-cnt_a)
    pb = max(0, y-1-cnt_b)
    if r[cnt] < p[pa] and r[cnt] < q[pb]:
        break
    if p[pa] > q[pb]:
        ans  = ans - q[pb] + r[cnt]
        cnt_b += 1
        q[pb] = r[cnt]
    elif p[pa] < q[pb]:
        ans = ans - p[pa] + r[cnt]
        cnt_a += 1
        q[pa] = r[cnt]
    else:
        j = 0
        while p[pa-j] == q[pb-j] and pa - j > -1 and pb - j > -1:
            j += 1
        if p[pa-j] > q[pb-j]:
            ans  = ans - q[pb] + r[cnt]
            cnt_b += 1
            q[pb] = r[cnt]
        elif p[pa-j] < q[pb-j]:
            ans = ans - p[pa] + r[cnt]
            cnt_a += 1
            q[pa] = r[cnt]
        elif x > y and x-1-cnt_a >= 0:
            ans = ans - p[pa] + r[cnt]
            cnt_a += 1
            q[pa] = r[cnt]
        elif y-1-cnt_b >= 0:
            ans  = ans - q[pb] + r[cnt]
            cnt_b += 1
            q[pb] = r[cnt]
        else:
            break
    
print(ans)

Submission status: Wrong Answer

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

i = 0
ans = 0
for a in l:
    if i < C and r[i] <= a:
        ans += a
    elif i < C and r[i] > a:
        ans += r[i]
        i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
input_1st = input()
input_2nd = input()
input_3rd = input()
input_4th = input()

split_input_1st = input_1st.split()
split_input_2nd = list(map(int, input_2nd.split()))
split_input_3rd = list(map(int, input_3rd.split()))
split_input_4th = list(map(int, input_4th.split()))

x = int(split_input_1st[0])
y = int(split_input_1st[1])
a = int(split_input_1st[2])
b = int(split_input_1st[3])
c = int(split_input_1st[4])

result = 0

red_apple_list = split_input_2nd
green_apple_list = split_input_3rd
if x > y:
    for i in range(len(split_input_4th)):
        if i % 2 == 0:
            red_apple_list.append(split_input_4th[i])
        else:
            green_apple_list.append(split_input_4th[i])
    red_apple_list = sorted(red_apple_list, reverse=True)
    green_apple_list = sorted(green_apple_list, reverse=True)
else:
    for i in range(len(split_input_4th)):
        if i % 2 != 0:
            red_apple_list.append(split_input_4th[i])
        else:
            green_apple_list.append(split_input_4th[i])
    red_apple_list = sorted(red_apple_list, reverse=True)
    green_apple_list = sorted(green_apple_list, reverse=True)
for i in range(x):
    result += red_apple_list[i]
for i in range(y):
    result += green_apple_list[i]
    
print(result)