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 bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 300010
MOD = 10**9+7
spf = [i for i in range(MAXN)]
spf[0]=spf[1] = -1
def sieve():
    for i in range(2,MAXN,2):
        spf[i] = 2
    for i in range(3,int(MAXN**0.5)+1):
        if spf[i]==i:
            for j in range(i*i,MAXN,i):
                if spf[j]==j:
                    spf[j]=i
def fib(n,m):
    if n == 0:
        return [0, 1]
    else:
        a, b = fib(n // 2)
        c = ((a%m) * ((b%m) * 2 - (a%m)))%m
        d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m
        if n % 2 == 0:
            return [c, d]
        else:
            return [d, c + d]

def charIN(x= ' '):
    return(sys.stdin.readline().strip().split(x))

def arrIN(x = ' '):
    return list(map(int,sys.stdin.readline().strip().split(x)))

def ncr(n,r):
    num=den=1
    for i in range(r):
        num = (num*(n-i))%MOD
        den = (den*(i+1))%MOD

    return (num*(pow(den,MOD-2,MOD)))%MOD

def flush():
    return sys.stdout.flush()

'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
x,y,a,b,c = arrIN()
r = arrIN()
g = arrIN()
cl = arrIN()
sr = sum(r)
sg = sum(g)
for i in range(c):
    if sr>sg:
        g.append(cl[i])
        sg+=cl[i]
    elif sg>sr:
        r.append(cl[i])
        sr+=cl[i]
    else:
        if x>y:
            r.append(cl[i])
            sr+=cl[i]
        else:
            g.append(cl[i])
            sg+=cl[i]

r.sort(reverse=True)
g.sort(reverse=True)

print(sum(r[:x])+sum(g[:y]))

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)

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
        if y > 0:
            q = qs[-1]
            qs.pop(-1)
            score += q
            y -= 1

    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
        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
        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 > q:
                    # 赤にして食べる
                    x -= 1
                else:
                    # 緑にして食べる
                    y -= 1
            elif p > r:
                ps.pop(-1)
                score += p
                x -= 1
            elif q > r:
                qs.pop(-1)
                score += q
                y -= 1

print(score)

Submission status: Wrong Answer

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

p_list = p_list[a - x:]
q_list = q_list[b - y:]

pq_list = p_list + q_list
pq_list = sorted(pq_list)
# print(pq_list, r_list)

for r in r_list:
  for i, pq in enumerate(pq_list):
    if r > pq:
      pq_list[i] = r
      break

print(sum(pq_list))

Submission status: Wrong Answer

Solution code:
#200329E
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)
R = sorted(r)
Red = p + r
Red = sorted(Red)
Deli =[]

for i in range(X):
    Deli.append(Red[len(Red)-1])
    if Red[len(Red)-1] == P[len(P)-1]:
        Red[len(Red)-1] = 0
        #print(Red)
        Red = sorted(Red)
    else:
        Red[len(Red)-1] = 0
        Red = sorted(Red)
        R[len(R)-1]= 0
        R = sorted(R)

Green = q + R
Green = sorted(Green)
for i in range(Y):
    Deli.append(Green[len(Green)-1])
    Green[len(Green)-1] = 0
    Green = sorted(Green)
    
#print(Deli)
print(int(sum(Deli)))

Submission status: Wrong Answer

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

pl = list(map(int,input().split()))
ql = list(map(int,input().split()))
rl = list(map(int,input().split()))

pl = sorted(pl)
ql = sorted(ql)
rl = sorted(rl)
import itertools
pl_ac = [0] + list(itertools.accumulate(pl))
ql_ac = [0] + list(itertools.accumulate(ql))
rl_ac = [0] + list(itertools.accumulate(rl))



import bisect
ans = 0
#　赤色をi個食べるとすると無職x-i
for i in range(x+1):
    tmp = 0
    if i > a or x - i > c or b + c - (x - i) < y:
        continue
    #あか
    tmp += pl_ac[-1] - pl_ac[-1-i]
    # #無職
    # tmp += rl_ac[-1] - ql_ac[-1-(x-i)]
    #残りの無職
    if x-i == 0:
        rl_c = rl[:]
    else:
        rl_c = rl[:-(x-i)]
    # ub以上のりんご何個ある？
    ub = 10 ** 9 + 1

    lb = -1
    while True:
        if ub == lb:
            target = ub
            break
        target = (ub + lb) // 2 + 1
        idx = bisect.bisect_left(ql, target)
        idx2 = bisect.bisect_left(rl_c, target)
        if (b - idx) + (c - (x - i) - idx2) >= y:
            lb = target
        else:
            ub = target - 1
    idx = bisect.bisect_left(ql, target)
    idx2 = bisect.bisect_left(rl_c, target)
    tmp += ql_ac[-1] - ql_ac[idx]
    tmp += rl_ac[-1] - rl_ac[idx2]
    ans = max(tmp, ans)

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

d = 0
for i in range(x):
    if r and r[-1] > p[-1]:
        d += r.pop(-1)
    else:
        d += p.pop(-1)

for i in range(y):
    if r and r[-1] > q[-1]:
        d += r.pop(-1)
    else:
        d += q.pop(-1)

print(d)

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:
x, y, a, b, c = map(int, input().split())

ra = list(map(int, input().split()))
gb = list(map(int, input().split()))
sc = list(map(int, input().split()))

ra.sort(reverse=True)
gb.sort(reverse=True)
sc.sort(reverse=True)

curr_red_min_val = ra[x-1]
curr_green_min_val = gb[y-1]
tmp_x = x
tmp_y = y

for i in range(c):
  if curr_red_min_val < curr_green_min_val:
    if sc[i] > curr_red_min_val:
      ra[tmp_x-1] = sc[i]
      tmp_x -= 1
      curr_red_min_val = ra[tmp_x-1]
  else:
    if sc[i] > curr_green_min_val:
      gb[tmp_y-1] = sc[i]
      tmp_y -= 1
      curr_green_min_val = gb[tmp_y-1]
      
print(sum(ra[:x])+sum(gb[:y]))

Submission status: Wrong Answer

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

def solve():
    X, Y, A, B, C = map(int, input().split())
    P = sorted(map(int, input().split()), reverse=1)[:X]
    Q = sorted(map(int, input().split()), reverse=1)[:Y]
    R = sorted(map(int, input().split()))
    PQ = sorted(P+Q, reverse=1)
    res = 0
    for a in PQ:
        if R and a < R[-1]:
            res += R.pop()
        else:
            res += a

    return str(res)

if __name__ == '__main__':
    print(solve())

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)
q.sort(reverse=True)
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
        else:
            if reds[0] < greens[0]:
                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
    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:
from heapq import *
from itertools import *

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

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

  def __iter__(self):
    return self

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

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

Submission status: Wrong Answer

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_ind -= 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: Wrong Answer

Solution code:
import sys
x, y, a, b, c = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
p.sort(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)
ri = max(0, x - a) + max(0, y - b)
pi = min(a, x)
qi = min(b, y)
while ri < len(r) and (0 < pi or 0 < qi):
    if 0 < pi and p[pi - 1] < r[ri] and (qi <= 0 or p[pi - 1] < q[qi - 1]):
        ri += 1
        pi -= 1
    elif 0 < qi and q[qi - 1] < r[ri] and (pi <= 0 or q[qi - 1] < p[pi - 1]):
        ri += 1
        qi -= 1
    else:
        break
print(sum(p[:pi]) + sum(q[:qi]) + sum(r[:ri]))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

l=list(map(int,input().split()))
r=list(map(int,input().split()))
g=list(map(int,input().split()))
c=list(map(int,input().split()))

r.sort()
g.sort()
c.sort()
r=r[::-1]
g=g[::-1]
c=c[::-1]

ar=[]

if(l[0]>=l[2]):
    ar=ar+g[0:l[0]+1]
    if(l[1]>=l[3]):
        ar=ar+g[0:l[1]+1]
    else:
        ar=ar+g
else:
    ar=ar+r
    if(l[1]>=l[3]):
        ar=ar+g[0:l[1]+1]
    else:
        ar=ar+g
ar=ar+c

per=list(permutations(ar,l[0]+l[1]))
tot=[]
for i in range(len(per)):
    tot.append(sum(per[i]))
tot.sort()
tot=tot[::-1]
print(tot[0])

Submission status: Wrong Answer

Solution code:
import math



red,green,x,y,z = map(int,input().split())
red_data = list(map(int,input().split()))
green_data = list(map(int,input().split()))
blank_data = list(map(int,input().split()))


red_data.sort(reverse=True)
green_data.sort(reverse=True)
blank_data.sort(reverse=True)


# check if colourless apples are better
red_index = red-1
green_index = green-1


for blank in blank_data:
    red_gap = -1
    if red_index >= 1:
        red_gap = blank - red_data[red_index]
    
    green_gap = -1
    if green_index >=1:
        green_gap = blank - green_data[green_index]
    
    if max(red_gap,green_gap) <= 0:
        break
    
    if red_gap >= green_gap:
        red_data[red_index] = blank
        red_index -= 1
    
    if green_gap > red_gap:
        green_data[green_index] = blank
        green_index -= 1






total = sum(red_data[0:red]) + sum(green_data[0:green])

print(total)

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=sorted(list(map(int, input().split())))[a-x:]
q=sorted(list(map(int, input().split())))[b-y:]
r=sorted(list(map(int, input().split())),reverse=True)
import heapq
eat=p+q
heapq.heapify(eat)
for i in r:
	j=heapq.heappop(eat)
	if j<i:
		heapq.heappush(eat,i)
	else:
		heapq.heappush(eat,j)
print(list(eat))
print(sum(list(eat)))

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque

if __name__ == '__main__':
    x,y,a,b,c = map(int, input().split())
    p = [int(i) for i in input().split()]
    q = [int(i) for i in input().split()]
    r = [int(i) for i in input().split()]
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    pd =deque()
    pd.extend(p[0:x])
    qd = deque()
    qd.extend(q[0:y])
    rd = deque()
    rd.extend(r)
    sum = 0
    i = 0
    while i <(x+y):
        if len(pd)>0 and pd[0] > rd[0]:
            sum = sum +pd[0]
            pd.popleft()
            i = i+1
        if len(qd)>0 and qd[0] > rd[0]:
            sum = sum +qd[0]
            qd.popleft()
            i = i+1
        if len(pd)==0 and len(qd)>0 and qd[0] < rd[0]:
            sum = sum +rd[0]
            rd.popleft()
            qd.pop()
            i = i+1
        if len(qd)==0 and len(pd)>0 and pd[0] < rd[0]:
            sum = sum +rd[0]
            rd.popleft()
            pd.pop()
            i = i+1

        if len(pd)>0 and pd[0] < rd[0] and len(qd)>0 and qd[0] < rd[0]:
            absp = rd[0] - pd[len(pd)-1]
            absq = rd[0] - qd[len(qd)-1]
            if absp < absq:
                pd.pop()
            else:
                qd.pop()
            sum = sum +rd[0]
            rd.popleft()
            i = i + 1
        i = i +1
    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())))
S=sorted(P[A-X:]+Q[B-Y:])
i=0
while S[i]<R[-1-i]:
    if len(R)>i or len(S)>i:
        break
    i+=1
print(sum(S[i+1:])+sum(R[-1-i:]))

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
re=list(map(int,input().split()))
gr=list(map(int,input().split()))
mu=list(map(int,input().split()))
re.sort(reverse=1)
gr.sort(reverse=1)
mu.sort(reverse=1)
ans=sum(re[:x])
ind=0
for i in range(x):
  v=re[x-1-i]
  if i+1>c:
    break
  w=mu[i]
  if v<w:
    ans+=w-v
  else:
    ind=i
    break

ans+=sum(gr[:y])
for i in range(y):
  v=gr[y-1-i]
  if ind+i+1>c:
    break
  w=mu[ind+i]
  if v<=w:
    ans+=w-v
  else:
    ind=i
    break





print(ans)
#print(re,gr,mu,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()))
rlen = len(R)
if (rlen < x + y):
    max = rlen
else:
    max = x + y

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

p = P[:x]
q = Q[:y]
r = R[:max]

print(p)
print(q)
print(r)
p.extend(q)
p.sort()
print(p)

ans = sum(p)

for i in range(max):
    diff = r[i] - p[i]
    if (diff > 0):
        ans = ans + diff
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
X, Y, A, B, C = map(int, input().split())


def int_(num_str):
    return int(num_str) * -1


plist = list(map(int_, input().split()))
qlist = list(map(int_, input().split()))
rlist = list(map(int_, input().split()))
heapify(plist)
heapify(qlist)
heapify(rlist)
ans = 0
while X > 0 or Y > 0:
    if rlist:
        r = heappop(rlist)
    else:
        r = 100

    if X == 0:
        q = heappop(qlist)
        if q < r:
            ans += q
            if r != 100:
                heappush(rlist, r)
        else:
            ans += r
            heappush(qlist, q)
        Y -= 1
    elif Y == 0:
        p = heappop(plist)
        if p < r:
            ans += p
            if r != 100:
                heappush(rlist, r)
        else:
            ans += r
            heappush(plist, p)
        X -= 1
    else:
        p = heappop(plist)
        q = heappop(qlist)
        if p < q:
            heappush(qlist, q)
            if p < r:
                ans += p
                if r != 100:
                    heappush(rlist, r)
            else:
                ans += r
                heappush(plist, p)
            X -= 1
        else:
            heappush(plist, p)
            if q < r:
                ans += q
                if r != 100:
                    heappush(rlist, r)
            else:
                ans += r
                heappush(qlist, q)
            Y -= 1
    # print(ans, X, Y)
print(-ans)

Submission status: Wrong Answer

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()))
maze = [['',0] for i in range(a+b)]
for i in range(a):
    maze[i] = ["r",p[i]]
for i in range(b):
    maze[a + i] = ["g",q[i]]
#for i in range(c):
#    maze[a + b + i] = ["n",r[i]]

#maze.sort(reverse=True,key = lambda x:x[1])
r.sort(reverse=True)
p.sort(reverse=True)
q.sort(reverse=True)
maze = p[:x] + q[:y]
ans = sum(maze)
dpc = [0]*(c+1)
dpa = [0]*(a+b+1)
maze.sort()
for i in range(c):
    dpc[i+1] = dpc[i] + r[i] 
for i in range(x+y):
    dpa[i+1] = dpa[i]+maze[i]

for i in range(min(x+y,c)):
    ans = max(ans,ans - dpa[i+1] + dpc[i+1])
print(ans)

Submission status: Wrong Answer

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

import heapq
ans = 0
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
red = 0
g = 0
p1 = heapq.heappop(p)*(-1)
q1= heapq.heappop(q)*(-1)
r1 = heapq.heappop(r)*(-1)
for i in range(x+y):
    if c<0:
        if red==x and g<y:
            ans += q1
            if q:
                q1 = heapq.heappop(q)*(-1)
            g += 1
        elif red<x and g==y:
            ans += p1
            if p:
                heapq.heappop(p)*(-1)
            red += 1
        else:
            if p1>q1:
                ans += p1
                if p:
                    p1 = heapq.heappop(p)*(-1)
                red += 1
            else:
                ans += q1
                if q:
                    q1 = heapq.heappop(q)*(-1)
                g += 1
    elif red<x and g<y:
        if max(p1,q1)>=r1:
            if p1 > q1:
                ans += p1
                if p:
                    p1 = heapq.heappop(p)*(-1)
                red += 1
            else:
                ans += q1
                if q:
                    q1 = heapq.heappop(q)*(-1)
                g += 1
        else:
            if p1>q1:
                ans += r1
                if r:
                    r1 = heapq.heappop(r)*(-1)
                g += 1
            else:
                ans += r1
                if r:
                    r1 = heapq.heappop(r)*(-1)
                red += 1
                c -= 1

    elif red==x and g<y:
        if q1>=r1:
            ans += q1
            if q:
                q1 = heapq.heappop(q)*(-1)
            g += 1
        else:
            ans += r1
            if r:
                r1 = heapq.heappop(r)*(-1)
            g+= 1
            c -= 1
    elif red<x and g==y:
        if p1>=r1:
            ans += p1
            if p:
                p1 = heapq.heappop(p)*(-1)
            red += 1
        else:
            ans += r1
            if r:
                r1 = heapq.heappop(r)*(-1)
            red+= 1
            c -= 1
    elif red==x and g==y:
        ans += r1
        if r:
            r1 = heapq.heappop(r)*(-1)
        c-=1
print(ans)

Submission status: Wrong Answer

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

pp = x - 1
qp = y - 1
rp = c - 1
while rp >= 0:
    if r[rp] > p[pp] or r[rp] > q[qp]:
        if p[pp] >= q[qp]:
            q[qp] = r[rp]
            qp -= 1
        else:
            p[pp] = r[rp]
            pp -= 1
    rp -= 1

print(sum(p[:x]) + sum(q[:y]))

Submission status: Wrong Answer

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)
  # print(ans_list)
  i=0
  for _ in range(c):
    if len(lc)==0:
      break
    if x+y<len(lc):
      lc.pop()
    else:
      _c=lc.pop()
      if ans_list[i]<_c:
        ans_list[i]=_c
      i+=1
    # print(ans_list)

  # print(ans_list)
  return sum(ans_list)

# main()
print(main())

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 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()

ans = sum(p[0:X]) + sum(q[0:Y])
p = sorted(p[0:X], reverse=True)
p = sorted(q[0:Y], reverse=True)

for i in range(C):
    if p and q and r:
        if p[-1] <= q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        elif q[-1] <= p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif p and r:
        if p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif q and r:
        if q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        else:
            break
    else:
        break

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

pi = A - 1
qi = B - 1
ri = C - 1

yummy = 0
i = 1
while i <= X + Y:
  if pi > A - X - 1 and qi > B - Y - 1:
    a = p[pi]
    b = q[qi]
    c = r[ri]
    if c < a and c < b:
      yummy += a + b
      pi -= 1
      qi -= 1
      i += 2
    else:
      if a < b:
        yummy += c + b
        qi -= 1
        ri -= 1
        i += 2
      else:
        yummy += a + c
        pi -= 1
        ri -= 1
        i += 2
  elif pi > A - X - 1:
    a = p[pi]
    c = r[ri]
    if a < c:
      yummy += c
      ri -= 1
      i += 1
    else:
      yummy += a
      pi -= 1
      i += 1
  elif qi > B - Y - 1:
    b = q[qi]
    c = r[ri]
    if b < c:
      yummy += c
      ri -= 1
      i += 1
    else:
      yummy += b
      qi -= 1
      i += 1

print(yummy)

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 r in R:
        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
    return score

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