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
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_right as br #bisect_left(list, value)
#from fractions import gcd
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

#hpp = []
#hpq = []
#hpr = []
#for pi in p: heapq.heappush(hpp, -pi)
#for qi in q: heapq.heappush(hpq, -qi)
#for ri in r: heapq.heappush(hpr, -ri)

#hppr = []
#for pi, ri in zip(p, r):
#    heapq.heappush(hppr, -pi)
#    heapq.heappush(hppr, -ri)


def brr(a, x):
    if a[0] < x:
        return 0
    if x <= a[-1]:
        return len(a)

    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

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

usedrcnt = 0
for ri in r:
    ind = brr(p, ri)
    if ind < X:
        usedrcnt += 1

res = 0
res += sum(p[:X-usedrcnt])
#print(' after p:', res)
res += sum(r[:usedrcnt])
#print(' after r:', res)

usedrcnt2 = 0
for ri in r[usedrcnt:]:
    ind = brr(q, ri)
    if ind < Y:
        usedrcnt2 += 1

res += sum(q[:Y-usedrcnt2])
#print(' after q:', res)
res += sum(r[usedrcnt:usedrcnt+usedrcnt2])
#print(' after r:', res)

print(res)

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)

red = P[:X]
green = Q[:Y]
ans = sum(red) + sum(green)

ridx = 0

for i in range(X+Y):
    if red[-1] < green[-1]:
        if red[-1] < R[ridx]:
            ans -= red[-1]
            ans += R[ridx]
            ridx += 1
    else:
        if green[-1] < R[ridx]:
            ans -= green[-1]
            ans += R[ridx]
            ridx += 1

    if ridx == len(R):
        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()
p.reverse()
q.sort()
q.reverse()
r.sort()
ans = 0
for i in range(x):
    ans += p[i]
for i in range(y):
    ans += q[i]
#print(ans)
a1 = x-1
b1 = y-1
a2 = True
b2 = True
for i in range(c):
    if r[i] > p[a1] and p[a1] <= q[b1] and a2:
        ans += r[i] - p[a1]
        a1 -= 1
        if a1 < 0:
            a2 = False
    elif r[i] > q[b1] and q[b1] < p[a1] and b2:
        ans += r[i] - q[b1]
        b1 -= 1
        if b1 < 0:
            b2 = False
print(ans)

# 1 2 2 2 1
# 4 2
# 5 1
# 3
# a1 = 0
# b1 = 1
# p[0](4)

Submission status: Wrong Answer

Solution code:
def main():

    import heapq
    x, y, a, b, c = map(int, input().split())
    rlis = list(map(int, input().split()))
    glis = list(map(int, input().split()))
    clis = list(map(int, input().split()))

    lis = []
    rlis.extend(clis)
    glis.extend(clis)
    anslis1 = heapq.nlargest(x,rlis)
    anslis2 = heapq.nlargest(y,glis)
    anslis1.extend(anslis2)

    print(sum(int(i) for i in anslis1))


if __name__ == '__main__':
    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])

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

Submission status: Wrong Answer

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

Ps = sorted(P, reverse=True)
Qs = sorted(Q, reverse=True)
Rs = sorted(R, reverse=True)

Ps = Ps[:X+Y]
Qs = Qs[:X+Y]

pi,qi,ri = 0,0,0
pc,qc,rc = 0,0,0

PP, QQ = [],[]
for i in range(X+Y):
    if pi >= X and qi >= Y:
        # print "beeak"
        break
    if pi >= X and qi < Y:
        # print qi
        QQ.append(Qs[qi])
        qi += 1
        qc += 1
        continue
    if pi < X and qi >= Y:
        # print pi
        PP.append(Ps[pi])
        pi += 1
        pc += 1
        continue
    # print i, pi, qi

    if Qs[qi] < Ps[pi] and pi < X:
        # print Ps[pi]
        PP.append(Ps[pi])
        pi += 1
        pc += 1
    elif qi < Y:
        # print Qs[qi]
        QQ.append(Qs[qi])
        qi += 1
        qc += 1

# print PP
# print QQ

V = sorted(PP + QQ ,reverse=True)

# print V
r = Rs[0]
for i,v in enumerate(V):
    if v < r:
        # print V[:i]
        # print Rs[:X+Y-i]
        ans = sum(V[:i]) + sum(Rs[:X+Y-i])
        print ans
        exit()

print sum(V)

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

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

dp = [0] * X

# print(dp)
for i in range(X):
    if len(r) > 0 and r[0] > p[0]:
        dp[i] = r[0]
        r.pop(0)
    else:
        dp[i] = p[0]
        p.pop(0)
# print(dp)
red = sum(dp)

dp2 = [0] * Y
for i in range(Y):
    if len(r) > 0 and r[0] > q[0]:
        dp2[i] = r[0]
        r.pop(0)
    else:
        dp2[i] = q[0]
        q.pop(0)

# print(dp2)
green = sum(dp2)
print(red + green)

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 = X # num of red 
r = 0 # num of noncolor
while p>0 and r<C and P[p-1]<R[r]:
    p -= 1
    r += 1

q = Y # num of green
while q>0 and r<C and Q[q-1]<R[r]:
    q -= 1
    r += 1

ans = y = sum(P[:p]) + sum(Q[:q]) + sum(R[:r])
for p in range(p+1, X+1):
    y = y + P[p-1]
    if Q[q-1]<R[r-1]:
        y -= Q[q-1]
        q -= 1
    else:
        y -= R[r-1]
        r -= 1

    ans = max(ans, y)

print(ans)

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 lp < A-1:
            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:
from collections import deque
x,y,a,b,c = map(int,input().split())
p1 = list(map(int,input().split()))
q1 = list(map(int,input().split()))
r1 = list(map(int,input().split()))
ans = []
p1.sort(reverse=1)
q1.sort(reverse=1)
r1.sort(reverse=1)


p=deque(p1[0:x])
q=deque(q1[0:y])
r=deque(r1[0:x+y])

lp = p.pop()
lq = q.pop()

rr = r.popleft()

while 1:
#     print(lp,lq,rr)
    if x > 0 and lp <= lq or y == 0:
        if lp >= rr:
            ans.append(lp)
            
        else:
            ans.append(rr)
            r.append(0)
            rr = r.popleft()
        if x > 1:
                lp = p.pop()
        x -= 1
    if y > 0:
        if lq >= rr:
            ans.append(lq)
            
        else:
            ans.append(rr)
            r.append(0)
            rr = r.popleft()
        if y > 1:
                lq = q.pop()
        y -= 1
    if x == 0 and y == 0:
        print(sum(ans))
        exit()

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)


ans = 0

while (p[0] > r[0]):
    ans += p.pop(0)
    x -= 1
    if (x == 0):
        break
while (q[0] > r[0]):
    ans += q.pop(0)
    y-=1
    if (y == 0):
        break

if (x == 0 and y == 0):
    print(ans)
else:
    aps = sorted(p[0: x] + q[0: y] + r[0: x + y], reverse=True)
    print(aps)
    print(ans+sum(aps[0:x+y]))

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
P=sorted(list(map(int,input().split())))[::-1]
Q=sorted(list(map(int,input().split())))[::-1]
R=sorted(list(map(int,input().split())))[::-1]+[0]*(x+y)
L=sorted(P[:x]+Q[:y])[::-1]
l=r=0
for i in range(x+y):    
    if r+R[i]<l+L[-i-1]:
        print(sum(L[:x+y-i])+r)
        exit()
    r+=R[i]
    l+=L[-i-1]
print(r)

Submission status: Wrong Answer

Solution code:
import heapq

XYABC = list(map(int, input().split()))
p = input().split()
q = input().split()
r = input().split()

mp = list(map(lambda x:int(x)*(-1), p)) #各要素を-1倍
heapq.heapify(mp)
mq = list(map(lambda x:int(x)*(-1), q)) #各要素を-1倍
heapq.heapify(mq)
mr = list(map(lambda x:int(x)*(-1), r)) #各要素を-1倍
heapq.heapify(mr)

#print(heapq.heappop(mp)*(-1))  最大値の取得と表示

red = []
green = []
muji = []

for i in range(XYABC[0]):
    red.append(heapq.heappop(mp)*(-1))
for i in range(XYABC[1]):
    green.append(heapq.heappop(mq)*(-1))
mrange = XYABC[0] + XYABC[1] if (XYABC[0] + XYABC[1]) < len(mr) else len(mr)
for i in range(mrange):
    muji.append(heapq.heappop(mr)*(-1))

sum = 0
rindex = 0
mindex = 0
for i in range(XYABC[0]):
    if ((mindex < mrange) and red[rindex] < muji[mindex]):
        sum += muji[mindex]
        mindex += 1
    else:
        sum += red[rindex]
        rindex += 1

gindex = 0
for i in range(XYABC[1]):
    if ((mindex < mrange) and green[gindex] < muji[mindex]):
        sum += muji[mindex]
        mindex += 1
    else:
        sum += green[gindex]
        gindex += 1


print(sum)

Submission status: Wrong Answer

Solution code:
from collections import deque
x,y,a,b,c = map(int,input().split())
p1 = list(map(int,input().split()))
q1 = list(map(int,input().split()))
r1 = list(map(int,input().split()))
ans = []
p1.sort(reverse=1)
q1.sort(reverse=1)
r1.sort(reverse=1)


p=deque(p1[0:x])
q=deque(q1[0:y])
r=deque(r1[0:x+y])

lp = p.pop()
lq = q.pop()

rr = r.popleft()

while 1:
    print(lp,lq,rr)
    if x > 0 and lp <= lq or y == 0:
        if lp >= rr:
            ans.append(lp)
            
        else:
            ans.append(rr)
            r.append(0)
            rr = r.popleft()
        if x > 1:
                lp = p.pop()
        x -= 1
    if y > 0:
        if lq >= rr:
            ans.append(lq)
            
        else:
            ans.append(rr)
            r.append(0)
            rr = r.popleft()
        if y > 1:
                lq = q.pop()
        y -= 1
    if x == 0 and y == 0:
        print(sum(ans))
        exit()

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

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

pi = 0
qi = 0

for rv in r :
  if 0 <= pi < X and p[pi] < rv :
    p[pi] = rv
    pi += 1
    continue
    
  if 0 <= qi < Y and q[qi] < rv :
    q[qi] = rv
    qi += 1
    continue
    
print(sum(p[:X]) + sum(q[:Y]))

Submission status: Wrong Answer

Solution code:
X, Y, A, B, C = map(int, input().split())
apples = [(0, int(x)) for x in input().split()]
apples.extend((1, int(x)) for x in input().split())
apples.extend((2, int(x)) for x in input().split())
apples.sort(key=lambda t: t[1], reverse=True)
print(sum(t[1] for t in apples[:X + Y]))

Submission status: Wrong Answer

Solution code:
# 2020/03/28
# AtCoder Beginner Contest 160 - E

# Input
x, y, a, b, c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.append(-1000000000)
q.append(-1000000000)
r.append(-1000000000)

# Init
ans = 0

# Sort
p.sort()
p.reverse()
q.sort()
q.reverse()
r.sort()
r.reverse()

fullx = x
fully = y

# Calc
pidx = qidx = ridx = 0
while fullx > 0 or fully > 0:

    # Case Painting
    if max(p[pidx], q[qidx]) < r[ridx]:
        ans = ans + r[ridx]
        ridx = ridx + 1
        if fullx <= 0:
            fully = fully - 1
        elif fully <= 0:
            fullx = fullx - 1
        else:
            if p[pidx] > q[qidx]:
                fullx = fullx - 1
            else:
                fully = fully - 1
    # No Painting
    else:
        if fullx <= 0:
            ans = ans + q[qidx]
            qidx = qidx + 1
            fully = fully - 1
        elif fully <= 0:
            ans = ans + p[pidx]
            pidx = pidx + 1
            fullx = fullx - 1
        else:
            if p[pidx] > q[qidx]:
                ans = ans + p[pidx]
                pidx = pidx + 1
                fullx = fullx - 1
            else:
                ans = ans + q[qidx]
                qidx = qidx + 1
                fully = fully - 1

# Output
print(ans)

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

aa = sorted(map(int, input().split()), reverse=True)
bb = sorted(map(int, input().split()), reverse=True)
cc = sorted(map(int, input().split()), reverse=True)
heap = []
used = set()
for i in range(c):
    heappush(heap, [-cc[i], 2, i])
for i in range(x):
    heappush(heap, [-aa[i], 0, i])

ans = 0
for i in range(x):
    value, color, pos = heappop(heap)
    ans -= value
    if color == 2:
        used.add(pos)

i = 0
heap = []
while len(heap) < y and i < c:
    if i not in used:
        heappush(heap, -cc[i])
    i += 1
for i in range(y):
    heappush(heap, -bb[i])

for i in range(y):
    value = heappop(heap)
    ans -= value

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

x,y,a,b,c = [int(c) for c in input().split()]
ps = [int(c) for c in input().split()]
qs = [int(c) for c in input().split()]
rs = [int(c) for c in input().split()]

ps.sort(reverse=True)
qs.sort(reverse=True)
rs.sort(reverse=True)

pind = x-1
qind = y-1
rind = 0
for i in range(c+1):
    p = ps[pind]
    q = qs[qind]
    r = rs[rind]
    if min(p,q)>= r:
        break
    if p <= q:
        ps[pind] = r
        pind -= 1
    else:
        qs[qind] = r
        qind -= 1
    rind += 1
    if rind >= c:
        break
print(sum(ps[:x]) + sum(qs[:y]))

Submission status: Wrong Answer

Solution code:
x, y, na, nb, nc = map(int, input().split()) 
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

a = a[:x]
b = b[:y]

ai = len(a) - 1
bi = len(b) - 1
for i in c:
    if a[ai] < b[bi]:
        if i > a[ai] and ai >= 0:
            a[ai] = i
            bi -= 1
        elif i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        else:
            break
    else:
        if i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        elif i > a[ai] and ai >= 0:
            a[ai] = i
            bi -= 1
        else:
            break
print(sum(a)+sum(b))

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
La = sorted(list(map(int, input().split())), reverse=True)
Lb = sorted(list(map(int, input().split())), reverse=True)
Lc = sorted(list(map(int, input().split())), reverse=True)

L = La[:x]
L.extend(Lb[:y])
L.sort(reverse=False)

for i in range(c):
    if L[0] < Lc[0]:
        L[0] = Lc[0]

print(sum(L))

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
a=list(map(int,input().rstrip().split()))
b=list(map(int,input().rstrip().split()))
c=list(map(int,input().rstrip().split()))
d=0
a1=a+c
a1.sort(reverse=True)
b1=b+c
b1.sort(reverse=True)
for i in range(x):
    d=d+a1[i]
for j in range(y):
    d=d+b1[j]
print(d)

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=p[(a-x):]
q=q[(b-y):]
res=sum(p)+sum(q)
tp=0
tq=0
tr=c-1
while 1:#(tr>=0 and (tp<x or tq<y) and (p[tp]<r[tr] or q[tq]<r[tr])):
  if(tr>=0 and tp<x and p[tp]<q[tq] ):
    res+=r[tr]-p[tp]
    tp+=1
  elif(tr>=0 and tq<y):
    res+=r[tr]-q[tq]
    tq+=1
  tr-=1
  if(tr<0 or tp<=x or tq<=y):
    break
print(res)

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()
p.reverse()
q.sort()
q.reverse()
r.sort()
ans = 0
for i in range(x):
    ans += p[i]
for i in range(y):
    ans += q[i]
#print(ans)
a1 = x-1
b1 = y-1
for i in range(c):
    if r[i] > p[a1] and p[a1] <= q[b1]:
        ans += r[i] - p[a1]
        a1 -= 1
    elif r[i] > q[b1] and q[b1] < p[a1]:
        ans += r[i] - q[b1]
        b1 -= 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()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

ep = p[:X][::-1]
eq = q[:Y][::-1]
ans = sum(ep)+sum(eq)

ip = 0
iq = 0
for pr in r:
    if ip >= X:
        if iq >= Y:
            break
        elif eq[iq] < pr:
            ans += -eq[iq] + pr
            iq += 1
    elif iq >= Y:
        if ep[ip] < pr:
            ans += -ep[ip] + pr
            ip += 1
    else:
        if ep[ip] < eq[iq]:
            if pr > ep[ip]:
                ans += -ep[ip] + pr
                ip += 1
            else:
                break
        else:
            if pr > eq[iq]:
                ans += -eq[iq] + pr
                ip += 1
            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(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)


ans = 0
tX = X # Num of Ori. Red
tY = Y # Num of Ori. Green

for i in range(X):
  ans += P[i]
for i in range(Y):
  ans += Q[i]

temp = ans
#Update Section:
for i in range(C): # i+1 = Num of Non. Color
  if tX > 0 and tY > 0:
      if P[tX-1] < Q[tY-1]:
        temp += R[i] - P[tX-1]
        tX -= 1
      else:
        temp += R[i] - Q[tY-1]
        tY -= 1
  elif tX == 0:
    temp += R[i] - Q[tY-1]
    tY -= 1
  else:# tX == 0:
    temp += R[i] - P[tX-1]
    tX -= 1
  ans = max(ans, temp)
  
print(ans)

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

for i in range(C):
    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
print(ans)

Submission status: Wrong Answer

Solution code:
x,y,a,b,c=map(int,input().split())
G=[[0]*2 for i in range(a+b+c)]
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
for i in range(a):
  G[i][0]=A[i]
  G[i][1]=0
for i in range(b):
  G[i+a][0]=B[i]
  G[i+a][1]=1
for i in range(c):
  G[i+a+b][0]=C[i]
  G[i+a+b][1]=2
GG=sorted(G,reverse=True)
c=0
ca=0
cb=0
i=-1
ans=0
f=0
ff=0
while ca<x and cb<y and c<x+y:
  i=i+1
  ans=ans+GG[i][0]
  if GG[i][1]==0:
    ca=ca+1
    c=c+1
    if ca==x:
      f=1
  elif GG[i][1]==1:
    cb=cb+1
    c=c+1
    if cb==y:
      f=2
  else:
    c=c+1
  if c==x+y:
    ff=1
if ff==1:
  print(ans)
  exit()
if f==1:
  ans=0
  for i in range(x):
    ans=ans+A[i]
  GGG=sorted(G[a:a+b+c],reverse=True)
  for i in range(y):
    ans=ans+GGG[i][0]
  print(ans)
if f==2:
  ans=0
  for i in range(y):
    ans=ans+B[i]
  GGG=sorted(G[0:a]+G[a+b:a+b+c],reverse=True)
  for i in range(x):
    ans=ans+GGG[i][0]
  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()
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])
        else:
            if green[-1] <= r[-i-1]:
                del green[-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:
x, y, na, nb, nc = map(int, input().split()) 
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

a = a[:x]
b = b[:y]

ai = len(a) - 1
bi = len(b) - 1
for i in c:
    if a[ai] > b[bi]:
        if i > a[ai] and ai >= 0:
            a[ai] = i
            bi -= 1
        elif i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        else:
            break
    else:
        if i > b[bi] and bi >= 0:
            b[bi] = i
            bi -= 1
        elif i > a[ai] and ai >= 0:
            a[ai] = i
            bi -= 1
        else:
            break
print(sum(a)+sum(b))

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()
hr = [-i for i in r]
hg = [-i for i in g]
hc = [-i for i in cl]
hr1 = [-i for i in r]
hg1 = [-i for i in g]
hc1 = [-i for i in cl]
heapify(hr)
heapify(hg)
heapify(hc)
heapify(hr1)
heapify(hg1)
heapify(hc1)
ans=ans1 = 0

for i in range(x):
    f = 0
    if hr:
        a1 = heappop(hr)
        f+=1
    if hc:
        f+=1
        a2 = heappop(hc)

    if f==2:
        if abs(a1)>=abs(a2):
            ans+=abs(a1)
            heappush(hc,a2)
        else:
            ans+=abs(a2)
            heappush(hr,a1)
    else:
        ans+=abs(a1)

for i in range(y):
    f = 0
    if hg:
        a1 = heappop(hg)
        f+=1
    if hc:
        f+=1
        a2 = heappop(hc)

    if f==2:
        if abs(a1)>=abs(a2):
            ans+=abs(a1)
            heappush(hc,a2)
        else:
            ans+=abs(a2)
            heappush(hg,a1)
    else:
        ans+=abs(a1)

for i in range(y):
    f = 0
    if hg1:
        a1 = heappop(hg1)
        f+=1
    if hc1:
        f+=1
        a2 = heappop(hc1)

    if f==2:
        if abs(a1)>=abs(a2):
            ans1+=abs(a1)
            heappush(hc1,a2)
        else:
            ans1+=abs(a2)
            heappush(hg1,a1)
    else:
        ans1+=abs(a1)

for i in range(x):
    f = 0
    if hr1:
        a1 = heappop(hr1)
        f+=1
    if hc1:
        f+=1
        a2 = heappop(hc1)

    if f==2:
        if abs(a1)>=abs(a2):
            ans1+=abs(a1)
            heappush(hc1,a2)
        else:
            ans1+=abs(a2)
            heappush(hr1,a1)
    else:
        ans1+=abs(a1)
print(max(ans,ans1))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    sp = list(reversed(sorted(p)))
    sq = list(reversed(sorted(q)))
    sr = list(sorted(r))
    # print(X)
    # print(Y)
    spi = sp[:X]
    sqi = sq[:Y]
    # print(spi)
    # print(sqi)
    # print(sr)
    rIndex = len(sr)-1
    pIndex = len(spi)-1
    qIndex = len(sqi)-1
    while rIndex >= 0:
        if (pIndex < 0 or sr[rIndex] < spi[pIndex]) and (qIndex < 0 or sr[rIndex] < sqi[qIndex]):
            break
        if pIndex < 0:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
            continue
        if qIndex < 0:
            spi[pIndex] = sr[rIndex]
            pIndex-=1
            continue
        if sr[rIndex] - spi[pIndex] > sr[rIndex] - sqi[qIndex]:
            spi[pIndex] = sr[rIndex]
            pIndex-=1
        else:
            sqi[qIndex] = sr[rIndex]
            qIndex-=1
        rIndex-=1
    print(sum(spi)+sum(sqi))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)

if __name__ == '__main__':
    main()