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 heapq
X, Y, a, b, c = map(int, input().split())
p = list(map(lambda x: -int(x), input().split()))
q = list(map(lambda x: -int(x), input().split()))
r = list(map(lambda x: -int(x), input().split()))
p.sort()
q.sort()
t = p[:X+1]
s = q[:Y+1]
heapq.heapify(t)
heapq.heapify(s)
heapq.heapify(r)

ans = 0
for _ in range(min(X, Y)):
    ap1 = heapq.heappop(t)
    ap2 = heapq.heappop(s)
    ap3 = heapq.heappop(r)
    if ap1 < ap2:
        if ap3 < ap2:
            ap = ap1 + ap3
            heapq.heappush(s, ap2)
        else:
            ap = ap1 + ap2
            heapq.heappush(r, ap3)
    else:
        if ap3 < ap1:
            ap = ap3 + ap2
            heapq.heappush(t, ap1)
        else:
            ap = ap1 + ap2
            heapq.heappush(r, ap3)
    ans -= ap

if X < Y:
    for _ in range(abs(X-Y)):
        ap2 = heapq.heappop(s)
        ap3 = heapq.heappop(r)
        if ap2 < ap3:
            ans -= ap2
            heapq.heappush(r, ap3)
        else:
            ans -= ap3
            heapq.heappush(s, ap2)
else:
    for _ in range(abs(X-Y)):
        ap2 = heapq.heappop(t)
        ap3 = heapq.heappop(r)
        if ap2 < ap3:
            ans -= ap2
            heapq.heappush(r, ap3)
        else:
            ans -= ap3
            heapq.heappush(t, ap2)
            
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
input = sys.stdin.readline

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)
cumsum_p = list(itertools.accumulate(p))
cumsum_q = list(itertools.accumulate(q))
cnt_p = 0
cnt_q = 0
for i in range(c):
    # print(p[x - 1 - cnt_p], q[y - 1 - cnt_q], cnt_q, cnt_p, cumsum_q, cumsum_p)
    if x - 1 - cnt_p < 0 and y - 1 - cnt_q < 0:
        break
    if x - 1 - cnt_p < 0:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    if y - 1 - cnt_q < 0:
        if r[i] >= p[x - 1 - cnt_p]:
            cumsum_p[x - 1] += r[i] - p[x - 1 - cnt_p]
            cnt_p += 1
        else:
            break
    if p[x - 1 - cnt_p] > q[y - 1 - cnt_q]:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    else:
        if r[i] >= p[x - 1 - cnt_p]:
            cumsum_p[x - 1] += r[i] - p[x - 1 - cnt_p]
            cnt_p += 1
        else:
            break
print(cumsum_p[x - 1] + cumsum_q[y - 1])

Submission status: Wrong Answer

Solution code:
import sys,math,copy,queue,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
X,Y,A,B,C = LI()
p = LI()
q = LI()
r = LI()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pa =  [0] + list(itertools.accumulate(p))
qa =  [0] + list(itertools.accumulate(q))
p = [0] + p
q = [0] + q
red = X
green = Y
t = 0
pl = 0
while p[red] < r[t] or q[green] < r[t]:
    pl += r[t]
    t += 1
    if red == 0:
        green -= 1
    elif green == 0:
        red -= 1

    elif p[red] < q[green] :
        red -= 1
    else:
        green -= 1
    if t >=C or (green ==0 and red ==0): break
print (pa[red] + qa[green] + pl)

Submission status: Wrong Answer

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

pq = sorted(p[:X] + q[:Y],lambda x:-x)
for i in range(X+Y):
  if r[i] < pq[i]:
    break
print(sum(r[:i] + pq[i:]))

Submission status: Wrong Answer

Solution code:
from itertools import *

def merge(x,y):
  while 1:
    if x and y and x[-1] >= y[-1] or (not (x and y)) and x:
      yield x.pop()
    elif y:
      yield y.pop()
    else:
      return

X, Y, A, B, C = map(int, input().split())
P = sorted(map(int, input().split()))
Q = sorted(map(int, input().split()))
R = sorted(map(int, input().split()))

S = sum(islice(merge(P,R), X-1))
S += sum(islice(merge(Q,R), Y-1))
if len(R) == C:
  S += max(P[-1], Q[-1]) + R[-1]
else:
  S += next(merge(P,R)) + next(merge(Q,R))
  
print(S)

Submission status: Wrong Answer

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p = sorted(p)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if (q[Q] >= p[P]):
                P += 1
                R -= 1
        elif Q < B:
            if (r[C-i-1] > q[Q]):
                Q += 1
                R -= 1
            else:
                break
    elif Q < B:
        if (r[C-i-1] > q[Q]):
                Q += 1
                R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

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())))[::-1]

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

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

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

if index == -1:
    print(ans_sum[-1])
else:
    print(ans_sum[-1] + (r_sum[index] - ans_sum[index]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
X,Y,A,B,C = [int(i) for i in input().split()]

P = [int(i) for i in input().split()] + [-1]
Q = [int(i) for i in input().split()] + [-1]
R = [int(i) for i in input().split()] + [-1]

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

a = b = c = 0
val = 0

#print(P, Q, R)

while X > 0 or Y > 0:
    if X > 0:
        #print('X:', Y, P[a], Q[b], R[c])
        if (Y <= 0 or P[a] < Q[b]) and P[a] < R[c]:
            val += R[c]
            c += 1
        else:
            val += P[a]
            a += 1
        X -= 1
        if X <= 0:
            a = -1
    
    if Y > 0:
        #print('Y:', Y, P[a], Q[b], R[c])
        if (X <= 0 or Q[b] < P[a]) and Q[b] < R[c]:
            val += R[c]
            c += 1
        else:
            val += Q[b]
            b += 1
        Y -= 1
        if Y <= 0:
            b = -1

print(val)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    X, Y, A, B, C = map(int, input().split())
    pA = sorted(map(int, input().split()), reverse=True)
    pB = sorted(map(int, input().split()), reverse=True)
    pC = sorted(map(int, input().split()), reverse=True)

    # dp[red_count][green_count] = (value, red-index, green-index, non-index)
    dp = defaultdict(lambda: defaultdict(lambda: (0, 0, 0, 0)))

    for i in range(1, max(X, Y) + 1):
        ri = i
        gi = i - 1
        if 0 < ri:
            v, r, g, n = dp[ri - 1][gi]
            dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        if 0 < gi:
            v, r, g, n = dp[ri][gi - 1]
            dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        ri = i - 1
        gi = i
        if 0 < ri:
            v, r, g, n = dp[ri - 1][gi]
            dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        if 0 < gi:
            v, r, g, n = dp[ri][gi - 1]
            dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        ri = i
        gi = i
        if 0 < ri:
            v, r, g, n = dp[ri - 1][gi]
            dp[ri][gi] = max(dp[ri][gi], (v + pA[r], r + 1, g, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))
        if 0 < gi:
            v, r, g, n = dp[ri][gi - 1]
            dp[ri][gi] = max(dp[ri][gi], (v + pB[g], r, g + 1, n),
                             (v + pC[n] if n < C else v, r, g, n + 1))

    print(dp[X][Y][0])


main()

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
if x > y:
    red_list = split_input_2nd + split_input_4th
    red_list = sorted(red_list, reverse=True)
    for i in range(x):
        result += int(red_list[i])
    
    green_list = split_input_3rd
    green_list = sorted(green_list, reverse=True)
    for i in range(y):
        result += int(green_list[i])
else:
    green_list = split_input_3rd + split_input_4th
    green_list = sorted(green_list, reverse=True)
    for i in range(y):
        result += int(green_list[i])
    
    red_list = split_input_2nd
    red_list = sorted(red_list, reverse=True)
    for i in range(x):
        result += int(red_list[i])
            
print(result)

Submission status: Wrong Answer

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

P_up = P[-X:]
Q_up = Q[-Y:]

P_up.extend(Q_up)
P_up.extend(R)

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

Submission status: Wrong Answer

Solution code:
import sys
import itertools
input = sys.stdin.readline

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)
cumsum_p = list(itertools.accumulate(p))
cumsum_q = list(itertools.accumulate(q))
cnt_p = 0
cnt_q = 0
for i in range(c):
    # print(p[x - 1 - cnt_p], q[y - 1 - cnt_q], cnt_q, cnt_p, cumsum_q, cumsum_p)
    if x - 1 - cnt_p < 0 and y - 1 - cnt_q < 0:
        break
    if x - 1 - cnt_p < 0:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    if y - 1 - cnt_q < 0:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    if p[x - 1 - cnt_p] > q[y - 1 - cnt_q]:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    else:
        if r[i] >= p[x - 1 - cnt_p]:
            cumsum_p[x - 1] += r[i] - p[x - 1 - cnt_p]
            cnt_p += 1
        else:
            break
print(cumsum_p[x - 1] + cumsum_q[y - 1])

Submission status: Wrong Answer

Solution code:
from itertools import *

def merge(x,y):
  while 1:
    if x and y and x[-1] >= y[-1] or (not (x and y)) and x:
      yield x.pop()
    elif y:
      yield y.pop()
    else:
      return

X, Y, A, B, C = map(int, input().split())
P = sorted(map(int, input().split()))
Q = sorted(map(int, input().split()))
R = sorted(map(int, input().split()))

S = sum(islice(merge(P,R), X-1))
S += sum(islice(merge(Q,R), Y-1))
if len(R) == C:
  S += max(P[-1], Q[-1]) + R[-1]
else:
  S += P[-1] + Q[-1]
  
print(S)

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)
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))
  else:
      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))
  else:
      ans1+=int(s_arr_c.pop(0))
print(ans1)

Submission status: Wrong Answer

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    X, Y, A, B, C = iim()
    P = list(iim())
    Q = list(iim())
    R = list(iim())

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

    p1 = [0]*(X+1)
    for i in range(X):
        p1[i+1] = p1[i] + P[i]
    q1 = [0]*(Y+1)
    for i in range(Y):
        q1[i+1] = q1[i] + Q[i]

    si = [X-1, Y-1]
    S = [P, Q]
    if P[si[0]] < Q[si[1]]:
        idx = 0
    else:
        idx = 1
    ans = 0
    for i in range(C):
        if S[idx][si[idx]] < R[i]:
            si[idx] -= 1
            ans += R[i]
            if si[idx] < 0:
                S[idx][0] = float("inf")
                si[idx] = 0
            if P[si[0]] < Q[si[1]]:
                idx = 0
            else:
                idx = 1
        else:
            break

    print(p1[si[0]+1] + q1[si[1]+1] + ans)

if __name__ == "__main__":
    resolve()

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()))
R.sort(reverse=True)
if X > A:
    get_P= P.extend(R[:X-A+1])
    get_P.sort(reverse=True)
    R=R[X-A+1:]
else:
    P.sort(reverse=True)
    get_P=P[:X]
if Y > B:
    get_Q= Q.extend(Q[:Y-B+1])
    R=R[Y-B+1]
    get_Q.sort(reverse=True)
else:
    Q.sort(reverse=True)
    get_Q=Q[:Y]

while(get_P[-1]<R[0] or get_Q[-1]<R[0]):
    if get_P[-1]<get_Q[-1]:
        get_P[-1] = R.pop(0)
    else:get_Q[-1] = R.pop(0)
    if R==[]:break
print(sum(get_P)+sum(get_Q))

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]:
    i+=1
    if len(R)>=i or len(S)>=i:
        break
if i==0:
    R=[]
print(sum(S[i:])+sum(R[-i:]))

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
if x > y:
    red_list = split_input_2nd + split_input_4th
    red_list = sorted(red_list, reverse=True)
    print('red_list: ', red_list)
    for i in range(x):
        result += int(red_list[i])
        print('red_list[i]: ', red_list[i])
    
    green_list = split_input_3rd
    green_list = sorted(green_list, reverse=True)
    print('green_list', green_list)
    for i in range(y):
        result += int(green_list[i])
        print('green_list[i]: ', green_list[i])
else:
    green_list = split_input_3rd + split_input_4th
    green_list = sorted(green_list, reverse=True)
    print('green_list', green_list)
    for i in range(y):
        result += int(green_list[i])
        print('green_list[i]: ', green_list[i])
    
    red_list = split_input_2nd
    red_list = sorted(red_list, reverse=True)
    print('red_list: ', red_list)
    for i in range(x):
        result += int(red_list[i])
        print('red_list[i]: ', red_list[i])
            
print(result)

Submission status: Wrong Answer

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
r.append(0)
p.sort()
q.sort()
r.sort()
ans = 0
np = []
nq = []
for i in range(X,0,-1):
    np.append(p[A-i])
for i in range(Y,0,-1):
    nq.append(q[B-i])
for i in range(X+Y):
    nr = r[-1]
    if(X > 0 and Y > 0):
        if(np[X-1] > nq[Y-1]):
            if(np[X-1] > nr):
                ans += np[X-1]
                X -= 1
            else:
                ans += nr
        elif(nq[Y-1] > nr):
            ans += nq[Y-1]
            Y -= 1
        else:
            ans += nr
    elif(X > 0):
        if(np[X-1] > nr):
            ans += np[X-1]
            X -= 1
        else:
            ans += nr
    else:
        if(nq[Y-1] > nr):
            ans += nq[Y-1]
            Y -= 1
        else:
            ans += nr
print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":

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

    Red = list(map(int, input().split()))
    Red.sort()
    Red = Red[::-1]
    Green = list(map(int, input().split()))
    Green.sort()
    Green = Green[::-1]
    NoColor = list(map(int, input().split()))
    NoColor.sort()
    NoColor = NoColor[::-1]

    if len(Red) <= X:
        ER = Red
    else:
        ER = Red[:X]

    if len(Green) <= Y:
        EG = Green
    else:
        EG = Green[:X]

    while True:
        if len(ER) < X and len(EG) < Y:
            if ER[-1] < EG[-1]:
                ER.append(NoColor.pop(0))
                continue
            else:
                EG.append(NoColor.pop(0))
                continue
        if len(ER) < X:
            ER.append(NoColor.pop(0))
            continue
        if len(EG) < X:
            EG.append(NoColor.pop(0))
            continue

        if len(NoColor) == 0:
            break

        if NoColor[0] < ER[-1] and NoColor[0] < EG[-1]:
            break

        if ER[-1] <= EG[-1]:
            ER.pop()
            ER.append(NoColor.pop(0))
        else:
            EG.pop()
            EG.append(NoColor.pop(0))

    print(sum(ER) + sum(EG))
    # print(ER)
    # print(EG)
    # print(NoColor)

Submission status: Wrong Answer

Solution code:
from collections import Counter


def main():
    x, y, a, b, c = map(int, input().split(" "))
    ps = sorted(list(map(int, input().split(" "))), reverse=True)
    qs = sorted(list(map(int, input().split(" "))), reverse=True)
    rs = sorted(list(map(int, input().split(" "))), reverse=True)

    red_eat = ps[:x]
    blue_eat = qs[:y]

    #eats = sorted(red_eat + blue_eat + rs, reverse=True)
    eats = sorted(red_eat + blue_eat + rs, reverse=True)
    #print(eats)
    #eats_dict = collections.Counter(eats)
    #print(eats_dict)

    eats = sorted(red_eat + rs, reverse=True)
    have_red = eats[:x]
    res = eats[x:]
    C = list((Counter(res) & Counter(rs)).elements())


    eats = sorted(blue_eat + C, reverse=True)
    have_blue = eats[:y]
    res = eats[y:]

    points = sum(have_blue + have_red)
    print(points)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    x,y,a,b,c = map(int,input().split())
    p = list(map(lambda x:int(x)*(-1),input().split()))
    q = list(map(lambda x:int(x)*(-1),input().split()))
    r = list(map(lambda x:int(x)*(-1),input().split()))
    heapq.heapify(p)
    heapq.heapify(q)
    heapq.heapify(r)
    heapq.heappush(r,0)
    ans = 0
    x1 = x
    y1 = y
    for i in range(x+y):
        if x==0:
            if q[0]<=r[0]:
                ans -= heapq.heappop(q)
            else:
                ans -= heapq.heappop(r)
        elif y==0:
            if p[0]<=r[0]:
                ans -= heapq.heappop(p)
            else:
                ans -= heapq.heappop(r)
        else:
            if a==0:
                y -= 1
                if q[0]<=r[0]:
                    ans -= heapq.heappop(q)
                else:
                    ans -= heapq.heappop(r)
            elif b==0:
                x -= 1
                if p[0]<=r[0]:
                    ans -= heapq.heappop(p)
                else:
                    ans -= heapq.heappop(r)
            elif p[0]<=r[0]:
                x -= 1
                ans -= heapq.heappop(p)
                a -= 1
            elif q[0]<=r[0]:
                y -= 1
                ans -= heapq.heappop(q)
                b -= 1
            else:
                if p[0]<q[0]:
                    x -= 1
                else:
                    y -= 1
                ans -= heapq.heappop(r)
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

import heapq

X, Y, A, B, C = map(int, input().split())
p = list(map(lambda x:int(x)*(-1), (input().split())))
heapq.heapify(p)
q = list(map(lambda x:int(x)*(-1), (input().split())))
heapq.heapify(q)
r = list(map(lambda x:int(x)*(-1), (input().split())))
heapq.heapify(r)
# print(p)

ans = 0
while r and (X>0 or Y>0):
    if p and p[0] < r[0] and X>0:
        ans -= heapq.heappop(p)
        X -= 1
    elif q and q[0] < r[0] and Y>0:
        ans -= heapq.heappop(q)
        Y -= 1
    elif p and q and p[0] < q[0] and Y>0:
        ans -= heapq.heappop(r)
        Y -= 1
    elif X>0:
        ans -= heapq.heappop(r)
        X -= 1
    else:
        ans -= heapq.heappop(r)
        Y -= 1
if X>0:
    for i in range(X):
        ans -= heapq.heappop(p)
if Y>0:
    for i in range(Y):
        ans -= heapq.heappop(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()))

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

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

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


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

Submission status: Wrong Answer

Solution code:
x,y,a,b,c = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = list(map(int,input().split()))
zen = []

for i in range(a):
    zen.append((P[i],'P'))
for i in range(b):
    zen.append((Q[i],'Q'))
for i in range(c):
    zen.append((R[i],'R'))
zen = sorted(zen,reverse=True)

i=0
ans = 0
red = 0
green = 0
white = 0
while red+green+white!=a+b:
    aji,color = zen[i]
    if red<=a and green <=b:
        if color == 'P':
            red+=1
        elif color =='Q':
            green+=1
        else:
            white+=1
        ans += aji
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(' ')))


summ=0
m1= p+r
m1.sort()
m1.reverse()
summ+=sum(m1[:x])
m1=m1[x:]
m2=m1+q
m2.sort()
m2.reverse()
summ+=sum(m2[:y])

print(summ)

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

sum = 0
for i in range(n):
    sum += rest[i][0]

diff = abs(pr-qr)

count = 0

rsum = 0
if pr <= qr:
    for i in range(x):
        if p[i][1] == 3:
            count += 1
            if count > diff:
                rsum += p[i][0]
else:
    for i in range(y):
        if q[i][1] == 3:
            count += 1
            if count > diff:
                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:
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()
temp = [[i,0] for i in r]+[[i,1] for i in g]+[[i,2] for i in cl]
temp.sort(reverse=True,key = lambda x:x[0])
i = 0
ans = 0
while x>0 or y>0:
    if temp[i][1]==0:
        if x>0:
            ans+=temp[i][0]
            x-=1
    elif temp[i][1]==1:
        if y>0:
            ans+=temp[i][0]
            y-=1
    else:
        if x>y:
            ans+=temp[i][0]
            x-=1
        elif y>x:
            ans+=temp[i][0]
            y-=1
        else:
            j = i
            while j<len(temp) and temp[j][1]==2:
                j+=1
            if j==len(temp):
                ans+=temp[i][0]
                x-=1
            else:
                if temp[j][1]==1:
                    ans+=temp[i][0]
                    x-=1
                else:
                    ans+=temp[i][0]
                    y-=1
    i+=1
print(ans)

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

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