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:
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()
red = []
green = []
less = []
for i in range(x):
    red.append(p[-i-1])
for i in range(y):
    green.append(q[-i-1])
# print(red, green)
for i in range(c):
    if len(red) > 0 and len(green) > 0:
        if red[-1] < green[-1]:
            if red[-1] < r[-i-1]:
                del red[-1]
                less.append(r[-i-1])
        elif red[-1] > green[-1]:
            if green[-1] <= r[-i-1]:
                del green[-1]
                less.append(r[-i-1])
        else:
            if len(red) > 1 and len(green) > 1:
                if red[-2] < green[-2]:
                    if red[-1] < r[-i-1]:
                        del red[-1]
                        less.append(r[-i-1])
                else:
                    if green[-1] <= r[-i-1]:
                        del green[-1]
                        less.append(r[-i-1])
            else:
                if red[-1] < r[-i-1]:
                    del red[-1]
                    less.append(r[-i-1])
ans = sum(red) + sum(green) + sum(less)
# print(red, green, less)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

x, y, a, b, c = map(int, input().split())
pp = list(map(lambda x: -int(x), input().split()))
qq = list(map(lambda x: -int(x), input().split()))
rr = list(map(lambda x: -int(x), input().split()))

heapq.heapify(pp)
heapq.heapify(qq)
heapq.heapify(rr)

ans = 0
while x > 0 or y > 0:
    if len(rr) == 0:
        while x > 0:
            ans += -heapq.heappop(pp)
            x -= 1
        while y > 0:
            ans += -heapq.heappop(qq)
            y -= 1
        break
    
    if x > 0 and y > 0:
        if -pp[0] > -qq[0]:
            if len(rr) > 0 and -pp[0] < -rr[0]:
                ans += -heapq.heappop(rr)
            else:
                ans += -heapq.heappop(pp)
            x -= 1
        else:
            if len(rr) > 0 and -qq[0] < -rr[0]:
                ans += -heapq.heappop(rr)
            else:
                ans += -heapq.heappop(qq)
            y -= 1
    elif x > 0:
        if len(rr) > 0 and -pp[0] < -rr[0]:
            ans += -heapq.heappop(rr)
        else:
            ans += -heapq.heappop(pp)
        x -= 1
    elif y > 0:
        if len(rr) > 0 and -qq[0] < -rr[0]:
            ans += -heapq.heappop(rr)
        else:
            ans += -heapq.heappop(qq)
        y -= 1

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()))
lis=[]
for i in p:
  lis.append((i,0))
for i in q:
  lis.append((i,1))
for i in r:
  lis.append((i,2))
lis.sort(reverse=True)
xsum,ysum=0,0
xcnt,ycnt=0,0
for i in range(len(lis)):
  if xcnt==x and ycnt==y:
    break
  if xcnt<x:
    if lis[i][1]==0 or lis[i][1]==2:
      xsum+=lis[i][0]
      xcnt+=1
  if ycnt<y:
    if lis[i][1]==1 or lis[i][1]==2:
      ysum+=lis[i][0]
      ycnt+=1
print(xsum+ysum)

Submission status: Wrong Answer

Solution code:
from collections import deque
X,Y,A,B,C = map(int,input().split())
P = deque(map(int,input().split()))
Q = deque(map(int,input().split()))
R = deque(map(int,input().split()))
p = sorted(P,reverse=True)
q = sorted(Q,reverse=True)
r = deque(sorted(R,reverse=True))
p1 = deque(p[0:X])
q1 = deque(q[0:Y])
while len(r) > 0:
    if min(p1[X-1],q1[Y-1]) < r[0]:
        if min(p1[X-1],q1[Y-1]) == p1[-1]:
            p1.pop()
            p1.append(r.popleft())
            p1 = sorted(p1)
        else:
            q1.pop()
            q1.append(r.popleft())
            q1 = sorted(q1)
    else:
        break
ans = sum(p1) + sum(q1)
print(ans)

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

ans = sum(P) + sum(Q)
idxp = -1
idxq = -1

for ri in range(C):
    r = R[ri]
    if idxp >= -X and idxq >= -Y:
        if P[idxp] <= Q[idxq] and P[idxp] < R[ri]:
            ans += R[ri] - P[idxp]
            idxp -= 1
        elif Q[idxq] <= P[idxp] and Q[idxq] < R[ri]:
            ans += R[ri] - Q[idxq]
            idxq -= 1
    elif idxp >= -X:
        if P[idxp] < R[ri]:
            ans += R[ri] - P[idxp]
            idxp -= 1
    elif idxq >= -Y:
        if Q[idxq] < R[ri]:
            ans += R[ri] - Q[idxq]
            idxq -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
base = input().split()
x = int(base[0])
y = int(base[1])
a = int(base[2])
b = int(base[3])
c = int(base[4])
red = input().split()
green = input().split()
apple = input().split()
redx = red+apple
redx.sort(reverse=True)
greenx = green+apple
greenx.sort(reverse=True)
ans = []
for i in range(x):
    ans.append(int(redx[i]))
for i in range(y):
    ans.append(int(greenx[i]))
print(sum(ans))

Submission status: Wrong Answer

Solution code:
import math
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=0
i=0
j=0
k=0
# red=0
# green=0
while i<len(p) and j<len(q) and k<len(r) and x>0 and y>0:
	if p[i]>=q[j]:
		if p[i]>=r[k]:
			if x>0:
				x-=1
				ans+=p[i]
				i+=1
			else:
				if q[j]>=r[k]:
					ans+=q[j]
					j+=1
					y-=1
				else:
					ans+=r[k]
					k+=1
					y-=1
		else:
			if x>0:
				x-=1
				ans+=r[k]
				k+=1
			else:
				if q[j]>=r[k]:
					ans+=q[j]
					j+=1
					y-=1
				else:
					ans+=r[k]
					k+=1
					y-=1
	else:
		if q[j]>=r[k]:
			if y>0:
				ans+=q[j]
				j+=1
				y-=1
			else:
				if p[i]>=r[k]:
					ans+=q[i]
					i+=1
					x-=1
				else:
					ans+=r[k]
					k+=1
					x-=1
		else:
			if y>0:
				y-=1
				ans+=r[k]
				k+=1
			else:
				if p[i]>=r[k]:
					ans+=p[i]
					i+=1
					x-=1
				else:
					ans+=r[k]
					k+=1
					x-=1
while i<len(p) and k<len(r):
	if p[i]>=r[k]:
			if x>0:
				x-=1
				ans+=p[i]
				i+=1
			else:
				break
	else:
		if x>0:
			x-=1
			ans+=r[k]
			k+=1
		else:
			break
while j<len(q) and k<len(r):
	if q[j]>=r[k]:
			if y>0:
				y-=1
				ans+=q[j]
				j+=1
			else:
				break
	else:
		if y>0:
			y-=1
			ans+=r[k]
			k+=1
		else:
			break

while i<len(p) and x>0:
	ans+=p[i]
	i+=1
	x-=1
while j<len(q) and y>0:
	ans+=q[j]
	j+=1
	y-=1
print(ans)

Submission status: Wrong Answer

Solution code:
# E
X, Y, NA, NB, NC = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())

A = sorted(A)[-X:]
B = sorted(B)[-Y:]
C = sorted(C)[::-1]

ans = sum(A) + sum(B)


i = 0
j = 0
num_A = len(A)
num_B = len(B)
for c in C:
    if i < num_A and j < num_B:
        if A[i] < B[j] and A[i] < c:

            ans += c - A[i]
            i += 1
        elif A[i] >= B[j] and B[j] < c:

            ans += c - B[j]
            j += 1

    elif i < num_A:
        if A[i] < c:
            i += 1
            ans += c - A[i]

    elif j < num_B:
        if B[j] < c:
            j += 1
            ans += c - B[j]

    else:
        break

print(ans)

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)
  i=1
  for _ in range(c):
    if a+b<len(lc):
      lc.pop()
    else:
      _c=lc.pop()
      if ans_list[-i]<_c:
        ans_list[-i]=_c
        i+=1

  return sum(ans_list)

# main()
print(main())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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_idx = 0
q_idx = 0
r_idx = 0

p = P[p_idx]
q = Q[q_idx]
r = R[r_idx]


ans = 0
while X > 0 or Y > 0:
    #print("X=", X, "Y=", Y)
    #print("p,q,r", p, q, r)
    #print("ans", ans)
    #print("p,q,r,X,Y,ans", p, q, r, X, Y, ans)

    m = max(p, q, r)

    if m == -1:
        break

    if m == r:
        ans += r
        r_idx += 1

        if r_idx == C:
            r = -1
        else:
            r = R[r_idx]

        lp = p_idx + X - 1
        lq = q_idx + Y - 1

        if lp <= A-1 and lq <= B-1:
            p_l = P[lp]
            q_l = Q[lq]

            if q_l < p_l:
                Y -= 1
            else:
                X -= 1
        elif B-1 < lq:
            Y -= 1
        else:
            X -= 1

    elif m == p:
        ans += p
        p_idx += 1
        X -= 1

        if p_idx == A or X == 0:
            p = -1
        else:
            p = P[p_idx]
    elif m == q:
        ans += q
        q_idx += 1
        Y -= 1

        if q_idx == B or Y == 0:
            q = -1
        else:
            q = Q[q_idx]

print(ans)

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]:
            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:
from itertools import accumulate

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

ans = sum(P) + sum(Q)
idxp = -1
idxq = -1

print(ans)
for ri in range(C):
    r = R[ri]
    if idxp >= -X and idxq >= -Y:
        if P[idxp] <= Q[idxq] and P[idxp] < R[ri]:
            ans += R[ri] - P[idxp]
            idxp -= 1
        elif Q[idxq] <= P[idxp] and Q[idxq] < R[ri]:
            ans += R[ri] - Q[idxq]
            idxq -= 1
    elif idxp >= -X:
        if P[idxp] < R[ri]:
            ans += R[ri] - P[idxp]
            idxp -= 1
    elif idxq >= -Y:
        if Q[idxq] < R[ri]:
            ans += R[ri] - Q[idxq]
            idxq -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

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

ans = 0
i, j, k = 0, 0, 0
while X > i or Y > j:
    if k >= C:
        break

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

# print(P)
# print(Q)
print(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())),reverse=True)
q=sorted(list(map(int,input().split())),reverse=True)
r=sorted(list(map(int,input().split())),reverse=True)
P=p[0]
Q=q[0]
R=r[0]
M=0
def compere(a,A,W):
    global r,R,M
    for i in range(W):
        if A>=R:
            M+=a.pop(0)
            if a:
                A=a[0]
        else:
            M+=r.pop(0)
            if r:
                R=r[0]
compere(p,P,X)
compere(q,Q,Y)

print(M)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n) 

def SS(L):
    S = [0] * (len(L))
    for i, l in enumerate(L):
        S[i + 1] = S[i] + l

    return S


def main():
    X, Y, A, B, C = read_values()
    P = sorted(read_list(), reverse=True) + [-1]
    Q = sorted(read_list(), reverse=True) + [-1]
    R = sorted(read_list(), reverse=True) + [-1]

    x = 0
    y = 0
    r = 0

    res = 0
    for _ in range(X + Y):
        a = P[x]
        b = Q[y]
        c = R[r]

        if c >= a and c >= a:
            res += c
            r += 1
            continue

        if a > b:
            if x < X:
                res += a
                x += 1
                continue
            
            if b > c:
                res += b
                y += 1
            else:
                res += c
                r += 1

        if y < Y:
            res += b
            y += 1
            continue

        if a > c:
            res += a
            x += 1
        else:
            res += c
            r += 1


    print(res)
    
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    X, Y, A, B, C = lr()
    p = sorted(lr())[-X:][::-1]
    q = sorted(lr())[-Y:][::-1]
    r = sorted(lr())
    tmp = r.pop()
    pt = p[-1]
    qt = q[-1]
    ext = [0]
    while (tmp > pt or tmp > qt) and len(r) >= 0:
        if pt > qt:
            g = q.pop()
            ext.append(tmp)
            if len(q) == 0:
                qt = pow(10, 9)+1
            else:
                qt = q[-1]
        else:
            g = p.pop()
            ext.append(tmp)
            if len(p) == 0:
                pt = pow(10, 9)+1
            else:
                pt = p[-1]
        if len(r) > 0:
            tmp = r.pop()
    print(sum(p)+sum(q)+sum(ext))
resolve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 21:48:49 2020

@author: liang
"""
def min_search(a,b,c):
    if a < b:
        if a < c:
            return a
        else:
            return c
    else:
        if b < c:
            return b
        else:
            return c
        
        
X, Y, A, B, C = map(int,input().split())
red_apples = list(map(int,input().split()))
green_apples = list(map(int,input().split()))
colorless_apples = list(map(int,input().split()))

red_apples.sort(reverse=True)
green_apples.sort(reverse=True)
colorless_apples.sort(reverse=True)

#print(red_apples)
#print(green_apples)
#print(colorless_apples)

result_red = red_apples[0:X]
result_green = green_apples[0:Y]

#print(result_red)
#print(result_green)

i = X - 1
j = Y - 1
k = 0

while i >= 0 and j >= 0 and k <= C-1:
    mini = min_search(result_red[i], result_green[j], colorless_apples[k])
    if mini == colorless_apples[k]:
        break
    elif mini == result_red[i]:
        result_red[i] = colorless_apples[k]
        i -= 1
        k += 1
    else:
        result_green[j] = colorless_apples[k]
        j -= 1
        k += 1


score = sum(result_red) + sum(result_green)

print(score)

Submission status: Wrong Answer

Solution code:
import bisect
x,y,a,b,c=map(int,input().split())
A=sorted(list(map(int,input().split())))[::-1]
B=sorted(list(map(int,input().split())))[::-1]
C=sorted(list(map(int,input().split())))[::-1]

l=A[:x]+B[:y]
l=sorted(l)
lim=l[0]

for tt in C:
    if lim>tt:
        continue
    index=bisect.bisect_left(l,tt)
    l.insert(index,tt)
    lim=l[-x-y+1]
l=l[::-1]
print(sum(l[:x+y]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-*-

from sys import stdin


def main():
    # X, Y, A, B, C = list(map(int, stdin.readline().split()))
    # p = list(map(int, stdin.readline().split()))
    # q = list(map(int, stdin.readline().split()))
    # r = list(map(int, stdin.readline().split()))

    X, Y, A, B, C = 1, 2, 2, 2, 1
    p = [2, 4]
    q = [5, 1]
    r = [3]
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)

    oisisa = 0
    x, y, z = 0, 0, 0
    for i in range(0, X + Y):
        m_p = p[0] if len(p) > 0 else 0
        m_q = q[0] if len(q) > 0 else 0
        m_r = r[0] if len(r) > 0 else 0
        m = max( [m_p, m_q, m_r] )
        if x < X and m_p == m:
            # pから取る
            oisisa += p.pop(0)
            x += 1

        elif y < Y and m_q == m:
            # qから取る
            oisisa += q.pop(0)
            y += 1
        else:
            # rから取る
            oisisa += r.pop(0)
            z += 1

        if x + y + z == X + Y:
            print(oisisa)
            return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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 = sorted(p)
q = sorted(q)
r = sorted(r)
x_ = a-x
y_ = b-y
flag = 0
ans = sum(p[x_:]) + sum(q[y_:])
for i in range(c-1, -1, -1):
    if p[x_] > q[y_]:
        if q[y_] < r[i]:
            ans += r[i] - q[y_]
            y_ += 1
            if y_>=b:
                flag = 1
                break
        else:
            break
    else:
        if p[x_] < r[i]:
            ans += r[i] - p[x_]
            x_ += 1
            if x_>=a:
                flag = 2
                break
        else:
            break
    if flag == 1:
        for j in range(i-1, -1, -1):
            if x_>=a:
                break
            if p[x_] < r[j]:
                ans += r[j] - p[x_]
                x_ += 1
            else:
                break
    elif flag == 2:
        for j in range(i-1, -1, -1):
            if y_>=b:
                break
            if q[y_] < r[j]:
                ans += r[j] - q[y_]
                y_ += 1
            else:
                break
            
print(ans)

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()
p = deque(p[:X][::-1])
q = deque(q[:Y][::-1])
ans = 0
while X + Y > len(p) + len(q):
    ans += r.pop()
while r:
    R = r.pop()
    if p[0] < q[0]:
        if p[0] < R:
            p.popleft()
            p.append(R)
        else:
            break
    if p[0] > q[0]:
        if q[0] < R:
            q.popleft()
            q.append(R)
        else:
            break
ans += sum(p) + sum(q)
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(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pi = 0
qi = 0
ri = 0
pl = len(p)
ql = len(q)
rl = len(r)
scr = 0
for j in range(X+Y):
  mx = max(p[pi],q[qi],r[ri])
  scr += mx
  if mx == p[pi]:
    A -= 1
    if pi+1 >= pl or A == 0:
      p.append(0)
      pi = -1
    else:
      pi += 1
  elif mx == q[qi]:
    B -= 1
    if qi+1 >= ql or B == 0:
      q.append(0)
      qi = -1
    else:
      qi += 1
  else:
    if ri+1 >= rl:
      r.append(0)
      ri = -1
    else:
      ri += 1
      
print(scr)

Submission status: Wrong Answer

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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_idx = 0
q_idx = 0
r_idx = 0

p = P[p_idx]
q = Q[q_idx]
r = R[r_idx]


ans = 0
while X > 0 or Y > 0:
    #print("X=", X, "Y=", Y)
    #print("p,q,r", p, q, r)
    #print("ans", ans)
    #print("p,q,r,X,Y,ans", p, q, r, X, Y, ans)

    m = max(p, q, r)

    if m == -1:
        break

    if m == r:
        ans += r
        r_idx += 1

        if r_idx == C:
            r = -1
        else:
            r = R[r_idx]

        lp = p_idx + X - 1
        lq = q_idx + Y - 1

        if lp <= A-1 and lq <= B-1:
            p_l = P[lp]
            q_l = Q[lq]

            if q_l < p_l:
                Y -= 1
            else:
                X -= 1
        elif A-1 < lp:
            X -= 1
        else:
            Y -= 1

    elif m == p:
        ans += p
        p_idx += 1
        X -= 1

        if p_idx == A or X == 0:
            p = -1
        else:
            p = P[p_idx]
    elif m == q:
        ans += q
        q_idx += 1
        Y -= 1

        if q_idx == B or Y == 0:
            q = -1
        else:
            q = Q[q_idx]

print(ans)

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
arrayP = list(input().split())
arrayQ = list(input().split())
arrayR = list(input().split())
arrayP.sort(reverse=True)
arrayQ.sort(reverse=True)
arrayR.sort(reverse=True)
arrayAns = []
for i in range(x):
    arrayAns.append(int(arrayP[i]))
for i in range(y):
    arrayAns.append(int(arrayQ[i]))
arrayAns.sort()
for i in range(len(arrayR)):
    if arrayAns[0] <= int(arrayR[i]):
        arrayAns[0] = int(arrayR[i])
        arrayAns.sort()
print(sum(arrayAns))

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().split())
P = [-1] + sorted(list(map(int, input().split())))
Q = [-1] + sorted(list(map(int, input().split())))
R = [-1] + sorted(list(map(int, input().split())))

i, ans = 0, 0
N = X+Y
while i < N:
  max_V = max(P[-1], Q[-1], R[-1])
  if P[-1] == max_V:
    P.pop(-1)
  elif Q[-1] == max_V:
    Q.pop(-1)
  else:
    R.pop(-1)
  ans += max_V
  i += 1
  
print(ans)

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools
import heapq
#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


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

    nowx=0
    nowy=0
    nowc=0
    tempp=0
    tempq=0
    tempr=0
    total=0
    for i in range(1,x+y+1):
        tempp = INFI
        tempq = INFI
        tempr = INFI
        if nowx<x:
            tempp=heapq.heappop(p)
        if nowy<y:
            tempq=heapq.heappop(q)
        if nowc<c:
            tempc=heapq.heappop(r)
        tempmax=min(tempp,tempq,tempc)
        if tempmax==tempp:
            nowx+=1
            heapq.heappush(q,tempq)
            heapq.heappush(r,tempc)
            total+=tempp
        elif tempmax==tempq:
            nowy+=1
            heapq.heappush(p,tempp)
            heapq.heappush(r,tempc)
            total+=tempq
        elif tempmax==tempc:
            total += tempc
            nowc+=1
            if nowx>=x:
                nowy+=1
                heapq.heappush(p,tempp)
                heapq.heappush(q,tempq)
            elif nowy>=y:
                nowx+=1
                heapq.heappush(p,tempp)
                heapq.heappush(q,tempq)
            else:
                if tempp<=tempq:
                    nowy += 1
                    heapq.heappush(p, tempp)
                    heapq.heappush(q, tempq)
                else:
                    nowx += 1
                    heapq.heappush(p, tempp)
                    heapq.heappush(q, tempq)

    print(total*(-1))


if __name__ == "__main__":

    main()

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = [int(i) for i in input().split()]
pa = [int(i) for i in input().split()]
pb = [int(i) for i in input().split()]
pc = [int(i) for i in input().split()]

pa = sorted(pa, reverse=True)
pa = pa[:x]
pb = sorted(pb, reverse=True)
pb = pb[:y]
pc = sorted(pc, reverse=True)

l = pa + pb
l = sorted(l, reverse=True)

for i in range(len(l)):
  for j in range(len(pc)):
    if l[i] < pc[j]:
      l[i] = pc[j]
      pc.pop(j)
      break
      
print(sum(l))

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+c)]
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])

ans = 0
index = 0
erannda = set()
for i in range(x):
    while index < (a+b+c):
        if maze[index][0] == "r" or maze[index][0] == "n":
            ans += maze[index][1]
            erannda.add(index)
            index += 1
            break
        index += 1
index = 0
for i in range(y):
    while index < (a+b+c):
        if (maze[index][0] == "g" or maze[index][0] == "n") and (not (index in erannda)):
            ans += maze[index][1]
            index += 1
            break
        index += 1

ans1 = 0
index = 0
erannda = set()
for i in range(y):
    while index < (a+b+c):
        if maze[index][0] == "g" or maze[index][0] == "n":
            ans1 += maze[index][1]
            erannda.add(index)
            index += 1
            break
        index += 1
index = 0
for i in range(x):
    while index < (a+b+c):
        if (maze[index][0] == "r" or maze[index][0] == "n") and (not index in erannda):
            ans1 += maze[index][1]
            index += 1
            break
        index += 1
#print(maze)
print(max(ans,ans1))

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
pqr = [map(int, input().split()) for i in range(3)]

A = []
for i in range(3):
        for j in pqr[i]:
                A.append([j, i])
A.sort(key=lambda x:-x[0])

ans = 0
cnt = 0
for v, t in A:
        if t==0:
                if x:
                        x -= 1
                        ans += v
        elif t==1:
                if y:
                        y -= 1
                        ans += v
        else:
                if x+y>cnt:
                        cnt += 1
                        ans += v
                        if cnt==x+y: break
                else:
                        break
print(ans)